blob: 1c7d0f8c14c5cf8549345e02c9a5e72b7795030c [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)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#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 Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000403static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static listitem_T *listitem_alloc __ARGS((void));
405static void listitem_free __ARGS((listitem_T *item));
406static void listitem_remove __ARGS((list_T *l, listitem_T *item));
407static long list_len __ARGS((list_T *l));
408static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
409static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
410static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000412static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static void list_append __ARGS((list_T *l, listitem_T *item));
415static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000416static int list_append_string __ARGS((list_T *l, char_u *str, int len));
417static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
419static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
420static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000423static char_u *list2string __ARGS((typval_T *tv, int copyID));
424static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000425static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
426static void set_ref_in_list __ARGS((list_T *l, int copyID));
427static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static void dict_unref __ARGS((dict_T *d));
429static void dict_free __ARGS((dict_T *d));
430static dictitem_T *dictitem_alloc __ARGS((char_u *key));
431static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
432static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
433static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int dict_add __ARGS((dict_T *d, dictitem_T *item));
436static long dict_len __ARGS((dict_T *d));
437static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static char_u *string_quote __ARGS((char_u *str, int function));
443static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
444static int find_internal_func __ARGS((char_u *name));
445static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
446static 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));
447static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000448static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
450static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000469#if defined(FEAT_INS_EXPAND)
470static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
472#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
478static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000504static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000506static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000512static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000520static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000521static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000544static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000550static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000567static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571#ifdef vim_mkdir
572static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
573#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000578static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000579static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000593static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000595static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000602static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000603static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000604static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000609static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000610static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
613#ifdef HAVE_STRFTIME
614static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
616static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000628static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000629static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000630static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000631static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000632static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000634static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000648static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000653static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
654static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static int get_env_len __ARGS((char_u **arg));
656static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000657static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000658static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
659#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
660#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
661 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000662static 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 +0000663static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000664static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000665static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
666static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static typval_T *alloc_tv __ARGS((void));
668static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void init_tv __ARGS((typval_T *varp));
670static long get_tv_number __ARGS((typval_T *varp));
671static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000672static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static char_u *get_tv_string __ARGS((typval_T *varp));
674static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000675static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
679static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
680static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
681static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
682static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
683static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
684static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000685static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000687static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
689static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
690static int eval_fname_script __ARGS((char_u *p));
691static int eval_fname_sid __ARGS((char_u *p));
692static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static ufunc_T *find_func __ARGS((char_u *name));
694static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000695static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000696#ifdef FEAT_PROFILE
697static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000698static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
699static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
700static int
701# ifdef __BORLANDC__
702 _RTLENTRYF
703# endif
704 prof_total_cmp __ARGS((const void *s1, const void *s2));
705static int
706# ifdef __BORLANDC__
707 _RTLENTRYF
708# endif
709 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000710#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000711static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000712static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000713static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void func_free __ARGS((ufunc_T *fp));
715static void func_unref __ARGS((char_u *name));
716static void func_ref __ARGS((char_u *name));
717static 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));
718static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000719static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000720static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
721static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000723/* Character used as separated in autoload function/variable names. */
724#define AUTOLOAD_CHAR '#'
725
Bram Moolenaar33570922005-01-25 22:26:29 +0000726/*
727 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000728 */
729 void
730eval_init()
731{
Bram Moolenaar33570922005-01-25 22:26:29 +0000732 int i;
733 struct vimvar *p;
734
735 init_var_dict(&globvardict, &globvars_var);
736 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000737 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000738 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000739
740 for (i = 0; i < VV_LEN; ++i)
741 {
742 p = &vimvars[i];
743 STRCPY(p->vv_di.di_key, p->vv_name);
744 if (p->vv_flags & VV_RO)
745 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
746 else if (p->vv_flags & VV_RO_SBX)
747 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
748 else
749 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000750
751 /* add to v: scope dict, unless the value is not always available */
752 if (p->vv_type != VAR_UNKNOWN)
753 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000754 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000755 /* add to compat scope dict */
756 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000757 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000758}
759
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000760#if defined(EXITFREE) || defined(PROTO)
761 void
762eval_clear()
763{
764 int i;
765 struct vimvar *p;
766
767 for (i = 0; i < VV_LEN; ++i)
768 {
769 p = &vimvars[i];
770 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000771 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000772 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000773 p->vv_di.di_tv.vval.v_string = NULL;
774 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000775 }
776 hash_clear(&vimvarht);
777 hash_clear(&compat_hashtab);
778
779 /* script-local variables */
780 for (i = 1; i <= ga_scripts.ga_len; ++i)
781 vars_clear(&SCRIPT_VARS(i));
782 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000783 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000784
785 /* global variables */
786 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000787
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000788 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000789 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000790 hash_clear(&func_hashtab);
791
792 /* unreferenced lists and dicts */
793 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000794}
795#endif
796
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 * Return the name of the executed function.
799 */
800 char_u *
801func_name(cookie)
802 void *cookie;
803{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000804 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805}
806
807/*
808 * Return the address holding the next breakpoint line for a funccall cookie.
809 */
810 linenr_T *
811func_breakpoint(cookie)
812 void *cookie;
813{
Bram Moolenaar33570922005-01-25 22:26:29 +0000814 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815}
816
817/*
818 * Return the address holding the debug tick for a funccall cookie.
819 */
820 int *
821func_dbg_tick(cookie)
822 void *cookie;
823{
Bram Moolenaar33570922005-01-25 22:26:29 +0000824 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825}
826
827/*
828 * Return the nesting level for a funccall cookie.
829 */
830 int
831func_level(cookie)
832 void *cookie;
833{
Bram Moolenaar33570922005-01-25 22:26:29 +0000834 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835}
836
837/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000838funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
840/*
841 * Return TRUE when a function was ended by a ":return" command.
842 */
843 int
844current_func_returned()
845{
846 return current_funccal->returned;
847}
848
849
850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 * Set an internal variable to a string value. Creates the variable if it does
852 * not already exist.
853 */
854 void
855set_internal_string_var(name, value)
856 char_u *name;
857 char_u *value;
858{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000859 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000860 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
862 val = vim_strsave(value);
863 if (val != NULL)
864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000865 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000866 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000868 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
871 }
872}
873
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000874static lval_T *redir_lval = NULL;
875static char_u *redir_endp = NULL;
876static char_u *redir_varname = NULL;
877
878/*
879 * Start recording command output to a variable
880 * Returns OK if successfully completed the setup. FAIL otherwise.
881 */
882 int
883var_redir_start(name, append)
884 char_u *name;
885 int append; /* append to an existing variable */
886{
887 int save_emsg;
888 int err;
889 typval_T tv;
890
891 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000892 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000893 {
894 EMSG(_(e_invarg));
895 return FAIL;
896 }
897
898 redir_varname = vim_strsave(name);
899 if (redir_varname == NULL)
900 return FAIL;
901
902 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
903 if (redir_lval == NULL)
904 {
905 var_redir_stop();
906 return FAIL;
907 }
908
909 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000910 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
911 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000912 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
913 {
914 if (redir_endp != NULL && *redir_endp != NUL)
915 /* Trailing characters are present after the variable name */
916 EMSG(_(e_trailing));
917 else
918 EMSG(_(e_invarg));
919 var_redir_stop();
920 return FAIL;
921 }
922
923 /* check if we can write to the variable: set it to or append an empty
924 * string */
925 save_emsg = did_emsg;
926 did_emsg = FALSE;
927 tv.v_type = VAR_STRING;
928 tv.vval.v_string = (char_u *)"";
929 if (append)
930 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
931 else
932 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
933 err = did_emsg;
934 did_emsg += save_emsg;
935 if (err)
936 {
937 var_redir_stop();
938 return FAIL;
939 }
940 if (redir_lval->ll_newkey != NULL)
941 {
942 /* Dictionary item was created, don't do it again. */
943 vim_free(redir_lval->ll_newkey);
944 redir_lval->ll_newkey = NULL;
945 }
946
947 return OK;
948}
949
950/*
951 * Append "value[len]" to the variable set by var_redir_start().
952 */
953 void
954var_redir_str(value, len)
955 char_u *value;
956 int len;
957{
958 char_u *val;
959 typval_T tv;
960 int save_emsg;
961 int err;
962
963 if (redir_lval == NULL)
964 return;
965
966 if (len == -1)
967 /* Append the entire string */
968 val = vim_strsave(value);
969 else
970 /* Append only the specified number of characters */
971 val = vim_strnsave(value, len);
972 if (val == NULL)
973 return;
974
975 tv.v_type = VAR_STRING;
976 tv.vval.v_string = val;
977
978 save_emsg = did_emsg;
979 did_emsg = FALSE;
980 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
981 err = did_emsg;
982 did_emsg += save_emsg;
983 if (err)
984 var_redir_stop();
985
986 vim_free(tv.vval.v_string);
987}
988
989/*
990 * Stop redirecting command output to a variable.
991 */
992 void
993var_redir_stop()
994{
995 if (redir_lval != NULL)
996 {
997 clear_lval(redir_lval);
998 vim_free(redir_lval);
999 redir_lval = NULL;
1000 }
1001 vim_free(redir_varname);
1002 redir_varname = NULL;
1003}
1004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005# if defined(FEAT_MBYTE) || defined(PROTO)
1006 int
1007eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1008 char_u *enc_from;
1009 char_u *enc_to;
1010 char_u *fname_from;
1011 char_u *fname_to;
1012{
1013 int err = FALSE;
1014
1015 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1016 set_vim_var_string(VV_CC_TO, enc_to, -1);
1017 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1018 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1019 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1020 err = TRUE;
1021 set_vim_var_string(VV_CC_FROM, NULL, -1);
1022 set_vim_var_string(VV_CC_TO, NULL, -1);
1023 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1024 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1025
1026 if (err)
1027 return FAIL;
1028 return OK;
1029}
1030# endif
1031
1032# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1033 int
1034eval_printexpr(fname, args)
1035 char_u *fname;
1036 char_u *args;
1037{
1038 int err = FALSE;
1039
1040 set_vim_var_string(VV_FNAME_IN, fname, -1);
1041 set_vim_var_string(VV_CMDARG, args, -1);
1042 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1043 err = TRUE;
1044 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1045 set_vim_var_string(VV_CMDARG, NULL, -1);
1046
1047 if (err)
1048 {
1049 mch_remove(fname);
1050 return FAIL;
1051 }
1052 return OK;
1053}
1054# endif
1055
1056# if defined(FEAT_DIFF) || defined(PROTO)
1057 void
1058eval_diff(origfile, newfile, outfile)
1059 char_u *origfile;
1060 char_u *newfile;
1061 char_u *outfile;
1062{
1063 int err = FALSE;
1064
1065 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1066 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1067 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1068 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1069 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1070 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1071 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1072}
1073
1074 void
1075eval_patch(origfile, difffile, outfile)
1076 char_u *origfile;
1077 char_u *difffile;
1078 char_u *outfile;
1079{
1080 int err;
1081
1082 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1083 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1084 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1085 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1086 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1087 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1088 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1089}
1090# endif
1091
1092/*
1093 * Top level evaluation function, returning a boolean.
1094 * Sets "error" to TRUE if there was an error.
1095 * Return TRUE or FALSE.
1096 */
1097 int
1098eval_to_bool(arg, error, nextcmd, skip)
1099 char_u *arg;
1100 int *error;
1101 char_u **nextcmd;
1102 int skip; /* only parse, don't execute */
1103{
Bram Moolenaar33570922005-01-25 22:26:29 +00001104 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 int retval = FALSE;
1106
1107 if (skip)
1108 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001109 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 else
1112 {
1113 *error = FALSE;
1114 if (!skip)
1115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001116 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 }
1119 }
1120 if (skip)
1121 --emsg_skip;
1122
1123 return retval;
1124}
1125
1126/*
1127 * Top level evaluation function, returning a string. If "skip" is TRUE,
1128 * only parsing to "nextcmd" is done, without reporting errors. Return
1129 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1130 */
1131 char_u *
1132eval_to_string_skip(arg, nextcmd, skip)
1133 char_u *arg;
1134 char_u **nextcmd;
1135 int skip; /* only parse, don't execute */
1136{
Bram Moolenaar33570922005-01-25 22:26:29 +00001137 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 char_u *retval;
1139
1140 if (skip)
1141 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 retval = NULL;
1144 else
1145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 retval = vim_strsave(get_tv_string(&tv));
1147 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 if (skip)
1150 --emsg_skip;
1151
1152 return retval;
1153}
1154
1155/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001156 * Skip over an expression at "*pp".
1157 * Return FAIL for an error, OK otherwise.
1158 */
1159 int
1160skip_expr(pp)
1161 char_u **pp;
1162{
Bram Moolenaar33570922005-01-25 22:26:29 +00001163 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001164
1165 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001167}
1168
1169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 * Top level evaluation function, returning a string.
1171 * Return pointer to allocated memory, or NULL for failure.
1172 */
1173 char_u *
1174eval_to_string(arg, nextcmd)
1175 char_u *arg;
1176 char_u **nextcmd;
1177{
Bram Moolenaar33570922005-01-25 22:26:29 +00001178 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 char_u *retval;
1180
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001181 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 retval = NULL;
1183 else
1184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001185 retval = vim_strsave(get_tv_string(&tv));
1186 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 }
1188
1189 return retval;
1190}
1191
1192/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001193 * Call eval_to_string() without using current local variables and using
1194 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 */
1196 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001197eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 char_u *arg;
1199 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001200 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
1202 char_u *retval;
1203 void *save_funccalp;
1204
1205 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001206 if (use_sandbox)
1207 ++sandbox;
1208 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001210 if (use_sandbox)
1211 --sandbox;
1212 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 restore_funccal(save_funccalp);
1214 return retval;
1215}
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217/*
1218 * Top level evaluation function, returning a number.
1219 * Evaluates "expr" silently.
1220 * Returns -1 for an error.
1221 */
1222 int
1223eval_to_number(expr)
1224 char_u *expr;
1225{
Bram Moolenaar33570922005-01-25 22:26:29 +00001226 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001228 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
1230 ++emsg_off;
1231
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 retval = -1;
1234 else
1235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001236 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239 --emsg_off;
1240
1241 return retval;
1242}
1243
Bram Moolenaara40058a2005-07-11 22:42:07 +00001244/*
1245 * Prepare v: variable "idx" to be used.
1246 * Save the current typeval in "save_tv".
1247 * When not used yet add the variable to the v: hashtable.
1248 */
1249 static void
1250prepare_vimvar(idx, save_tv)
1251 int idx;
1252 typval_T *save_tv;
1253{
1254 *save_tv = vimvars[idx].vv_tv;
1255 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1256 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1257}
1258
1259/*
1260 * Restore v: variable "idx" to typeval "save_tv".
1261 * When no longer defined, remove the variable from the v: hashtable.
1262 */
1263 static void
1264restore_vimvar(idx, save_tv)
1265 int idx;
1266 typval_T *save_tv;
1267{
1268 hashitem_T *hi;
1269
1270 clear_tv(&vimvars[idx].vv_tv);
1271 vimvars[idx].vv_tv = *save_tv;
1272 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1273 {
1274 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1275 if (HASHITEM_EMPTY(hi))
1276 EMSG2(_(e_intern2), "restore_vimvar()");
1277 else
1278 hash_remove(&vimvarht, hi);
1279 }
1280}
1281
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001282#if defined(FEAT_SYN_HL) || defined(PROTO)
1283/*
1284 * Evaluate an expression to a list with suggestions.
1285 * For the "expr:" part of 'spellsuggest'.
1286 */
1287 list_T *
1288eval_spell_expr(badword, expr)
1289 char_u *badword;
1290 char_u *expr;
1291{
1292 typval_T save_val;
1293 typval_T rettv;
1294 list_T *list = NULL;
1295 char_u *p = skipwhite(expr);
1296
1297 /* Set "v:val" to the bad word. */
1298 prepare_vimvar(VV_VAL, &save_val);
1299 vimvars[VV_VAL].vv_type = VAR_STRING;
1300 vimvars[VV_VAL].vv_str = badword;
1301 if (p_verbose == 0)
1302 ++emsg_off;
1303
1304 if (eval1(&p, &rettv, TRUE) == OK)
1305 {
1306 if (rettv.v_type != VAR_LIST)
1307 clear_tv(&rettv);
1308 else
1309 list = rettv.vval.v_list;
1310 }
1311
1312 if (p_verbose == 0)
1313 --emsg_off;
1314 vimvars[VV_VAL].vv_str = NULL;
1315 restore_vimvar(VV_VAL, &save_val);
1316
1317 return list;
1318}
1319
1320/*
1321 * "list" is supposed to contain two items: a word and a number. Return the
1322 * word in "pp" and the number as the return value.
1323 * Return -1 if anything isn't right.
1324 * Used to get the good word and score from the eval_spell_expr() result.
1325 */
1326 int
1327get_spellword(list, pp)
1328 list_T *list;
1329 char_u **pp;
1330{
1331 listitem_T *li;
1332
1333 li = list->lv_first;
1334 if (li == NULL)
1335 return -1;
1336 *pp = get_tv_string(&li->li_tv);
1337
1338 li = li->li_next;
1339 if (li == NULL)
1340 return -1;
1341 return get_tv_number(&li->li_tv);
1342}
1343#endif
1344
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001345/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001346 * Top level evaluation function.
1347 * Returns an allocated typval_T with the result.
1348 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001349 */
1350 typval_T *
1351eval_expr(arg, nextcmd)
1352 char_u *arg;
1353 char_u **nextcmd;
1354{
1355 typval_T *tv;
1356
1357 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001358 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001359 {
1360 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001361 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001362 }
1363
1364 return tv;
1365}
1366
1367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1369/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001370 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001372 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001374 static int
1375call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 char_u *func;
1377 int argc;
1378 char_u **argv;
1379 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
Bram Moolenaar33570922005-01-25 22:26:29 +00001382 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 long n;
1384 int len;
1385 int i;
1386 int doesrange;
1387 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001388 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaar33570922005-01-25 22:26:29 +00001390 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001392 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
1394 for (i = 0; i < argc; i++)
1395 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001396 /* Pass a NULL or empty argument as an empty string */
1397 if (argv[i] == NULL || *argv[i] == NUL)
1398 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001399 argvars[i].v_type = VAR_STRING;
1400 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001401 continue;
1402 }
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 /* Recognize a number argument, the others must be strings. */
1405 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1406 if (len != 0 && len == (int)STRLEN(argv[i]))
1407 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001408 argvars[i].v_type = VAR_NUMBER;
1409 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411 else
1412 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001413 argvars[i].v_type = VAR_STRING;
1414 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
1416 }
1417
1418 if (safe)
1419 {
1420 save_funccalp = save_funccal();
1421 ++sandbox;
1422 }
1423
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1425 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001427 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (safe)
1429 {
1430 --sandbox;
1431 restore_funccal(save_funccalp);
1432 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001433 vim_free(argvars);
1434
1435 if (ret == FAIL)
1436 clear_tv(rettv);
1437
1438 return ret;
1439}
1440
1441/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001442 * Call vimL function "func" and return the result as a string.
1443 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444 * Uses argv[argc] for the function arguments.
1445 */
1446 void *
1447call_func_retstr(func, argc, argv, safe)
1448 char_u *func;
1449 int argc;
1450 char_u **argv;
1451 int safe; /* use the sandbox */
1452{
1453 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001454 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001455
1456 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1457 return NULL;
1458
1459 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 return retval;
1462}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001463
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001464#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001465/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001466 * Call vimL function "func" and return the result as a number.
1467 * Returns -1 when calling the function fails.
1468 * Uses argv[argc] for the function arguments.
1469 */
1470 long
1471call_func_retnr(func, argc, argv, safe)
1472 char_u *func;
1473 int argc;
1474 char_u **argv;
1475 int safe; /* use the sandbox */
1476{
1477 typval_T rettv;
1478 long retval;
1479
1480 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1481 return -1;
1482
1483 retval = get_tv_number_chk(&rettv, NULL);
1484 clear_tv(&rettv);
1485 return retval;
1486}
1487#endif
1488
1489/*
1490 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001491 * Uses argv[argc] for the function arguments.
1492 */
1493 void *
1494call_func_retlist(func, argc, argv, safe)
1495 char_u *func;
1496 int argc;
1497 char_u **argv;
1498 int safe; /* use the sandbox */
1499{
1500 typval_T rettv;
1501
1502 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1503 return NULL;
1504
1505 if (rettv.v_type != VAR_LIST)
1506 {
1507 clear_tv(&rettv);
1508 return NULL;
1509 }
1510
1511 return rettv.vval.v_list;
1512}
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514#endif
1515
1516/*
1517 * Save the current function call pointer, and set it to NULL.
1518 * Used when executing autocommands and for ":source".
1519 */
1520 void *
1521save_funccal()
1522{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001523 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 current_funccal = NULL;
1526 return (void *)fc;
1527}
1528
1529 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001530restore_funccal(vfc)
1531 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001533 funccall_T *fc = (funccall_T *)vfc;
1534
1535 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536}
1537
Bram Moolenaar05159a02005-02-26 23:04:13 +00001538#if defined(FEAT_PROFILE) || defined(PROTO)
1539/*
1540 * Prepare profiling for entering a child or something else that is not
1541 * counted for the script/function itself.
1542 * Should always be called in pair with prof_child_exit().
1543 */
1544 void
1545prof_child_enter(tm)
1546 proftime_T *tm; /* place to store waittime */
1547{
1548 funccall_T *fc = current_funccal;
1549
1550 if (fc != NULL && fc->func->uf_profiling)
1551 profile_start(&fc->prof_child);
1552 script_prof_save(tm);
1553}
1554
1555/*
1556 * Take care of time spent in a child.
1557 * Should always be called after prof_child_enter().
1558 */
1559 void
1560prof_child_exit(tm)
1561 proftime_T *tm; /* where waittime was stored */
1562{
1563 funccall_T *fc = current_funccal;
1564
1565 if (fc != NULL && fc->func->uf_profiling)
1566 {
1567 profile_end(&fc->prof_child);
1568 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1569 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1570 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1571 }
1572 script_prof_restore(tm);
1573}
1574#endif
1575
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577#ifdef FEAT_FOLDING
1578/*
1579 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1580 * it in "*cp". Doesn't give error messages.
1581 */
1582 int
1583eval_foldexpr(arg, cp)
1584 char_u *arg;
1585 int *cp;
1586{
Bram Moolenaar33570922005-01-25 22:26:29 +00001587 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 int retval;
1589 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001590 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1591 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001594 if (use_sandbox)
1595 ++sandbox;
1596 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 retval = 0;
1600 else
1601 {
1602 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001603 if (tv.v_type == VAR_NUMBER)
1604 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001605 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 retval = 0;
1607 else
1608 {
1609 /* If the result is a string, check if there is a non-digit before
1610 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001611 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (!VIM_ISDIGIT(*s) && *s != '-')
1613 *cp = *s++;
1614 retval = atol((char *)s);
1615 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 }
1618 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001619 if (use_sandbox)
1620 --sandbox;
1621 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623 return retval;
1624}
1625#endif
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 * ":let" list all variable values
1629 * ":let var1 var2" list variable values
1630 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 * ":let var += expr" assignment command.
1632 * ":let var -= expr" assignment command.
1633 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 */
1636 void
1637ex_let(eap)
1638 exarg_T *eap;
1639{
1640 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001641 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001642 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 int var_count = 0;
1645 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 expr = skip_var_list(arg, &var_count, &semicolon);
1649 if (expr == NULL)
1650 return;
1651 expr = vim_strchr(expr, '=');
1652 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001654 /*
1655 * ":let" without "=": list variables
1656 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 if (*arg == '[')
1658 EMSG(_(e_invarg));
1659 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 /* ":let var1 var2" */
1661 arg = list_arg_vars(eap, arg);
1662 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001665 list_glob_vars();
1666 list_buf_vars();
1667 list_win_vars();
1668 list_vim_vars();
1669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 eap->nextcmd = check_nextcmd(arg);
1671 }
1672 else
1673 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 op[0] = '=';
1675 op[1] = NUL;
1676 if (expr > arg)
1677 {
1678 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1679 op[0] = expr[-1]; /* +=, -= or .= */
1680 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (eap->skip)
1684 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (eap->skip)
1687 {
1688 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 --emsg_skip;
1691 }
1692 else if (i != FAIL)
1693 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
1698 }
1699}
1700
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701/*
1702 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1703 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001704 * When "nextchars" is not NULL it points to a string with characters that
1705 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1706 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 * Returns OK or FAIL;
1708 */
1709 static int
1710ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1711 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001712 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 int copy; /* copy values from "tv", don't move */
1714 int semicolon; /* from skip_var_list() */
1715 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001716 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717{
1718 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001719 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001721 listitem_T *item;
1722 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723
1724 if (*arg != '[')
1725 {
1726 /*
1727 * ":let var = expr" or ":for var in list"
1728 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001729 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 return FAIL;
1731 return OK;
1732 }
1733
1734 /*
1735 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1736 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001737 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 {
1739 EMSG(_(e_listreq));
1740 return FAIL;
1741 }
1742
1743 i = list_len(l);
1744 if (semicolon == 0 && var_count < i)
1745 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001746 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 return FAIL;
1748 }
1749 if (var_count - semicolon > i)
1750 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001751 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 return FAIL;
1753 }
1754
1755 item = l->lv_first;
1756 while (*arg != ']')
1757 {
1758 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 item = item->li_next;
1761 if (arg == NULL)
1762 return FAIL;
1763
1764 arg = skipwhite(arg);
1765 if (*arg == ';')
1766 {
1767 /* Put the rest of the list (may be empty) in the var after ';'.
1768 * Create a new list for this. */
1769 l = list_alloc();
1770 if (l == NULL)
1771 return FAIL;
1772 while (item != NULL)
1773 {
1774 list_append_tv(l, &item->li_tv);
1775 item = item->li_next;
1776 }
1777
1778 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001779 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 ltv.vval.v_list = l;
1781 l->lv_refcount = 1;
1782
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1784 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785 clear_tv(&ltv);
1786 if (arg == NULL)
1787 return FAIL;
1788 break;
1789 }
1790 else if (*arg != ',' && *arg != ']')
1791 {
1792 EMSG2(_(e_intern2), "ex_let_vars()");
1793 return FAIL;
1794 }
1795 }
1796
1797 return OK;
1798}
1799
1800/*
1801 * Skip over assignable variable "var" or list of variables "[var, var]".
1802 * Used for ":let varvar = expr" and ":for varvar in expr".
1803 * For "[var, var]" increment "*var_count" for each variable.
1804 * for "[var, var; var]" set "semicolon".
1805 * Return NULL for an error.
1806 */
1807 static char_u *
1808skip_var_list(arg, var_count, semicolon)
1809 char_u *arg;
1810 int *var_count;
1811 int *semicolon;
1812{
1813 char_u *p, *s;
1814
1815 if (*arg == '[')
1816 {
1817 /* "[var, var]": find the matching ']'. */
1818 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001819 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 {
1821 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1822 s = skip_var_one(p);
1823 if (s == p)
1824 {
1825 EMSG2(_(e_invarg2), p);
1826 return NULL;
1827 }
1828 ++*var_count;
1829
1830 p = skipwhite(s);
1831 if (*p == ']')
1832 break;
1833 else if (*p == ';')
1834 {
1835 if (*semicolon == 1)
1836 {
1837 EMSG(_("Double ; in list of variables"));
1838 return NULL;
1839 }
1840 *semicolon = 1;
1841 }
1842 else if (*p != ',')
1843 {
1844 EMSG2(_(e_invarg2), p);
1845 return NULL;
1846 }
1847 }
1848 return p + 1;
1849 }
1850 else
1851 return skip_var_one(arg);
1852}
1853
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001854/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001855 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1856 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001857 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 static char_u *
1859skip_var_one(arg)
1860 char_u *arg;
1861{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001862 if (*arg == '@' && arg[1] != NUL)
1863 return arg + 2;
1864 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1865 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866}
1867
Bram Moolenaara7043832005-01-21 11:56:39 +00001868/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001869 * List variables for hashtab "ht" with prefix "prefix".
1870 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001871 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001873list_hashtable_vars(ht, prefix, empty)
1874 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001875 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001876 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001877{
Bram Moolenaar33570922005-01-25 22:26:29 +00001878 hashitem_T *hi;
1879 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001880 int todo;
1881
1882 todo = ht->ht_used;
1883 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1884 {
1885 if (!HASHITEM_EMPTY(hi))
1886 {
1887 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001888 di = HI2DI(hi);
1889 if (empty || di->di_tv.v_type != VAR_STRING
1890 || di->di_tv.vval.v_string != NULL)
1891 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001892 }
1893 }
1894}
1895
1896/*
1897 * List global variables.
1898 */
1899 static void
1900list_glob_vars()
1901{
Bram Moolenaar33570922005-01-25 22:26:29 +00001902 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001903}
1904
1905/*
1906 * List buffer variables.
1907 */
1908 static void
1909list_buf_vars()
1910{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001911 char_u numbuf[NUMBUFLEN];
1912
Bram Moolenaar33570922005-01-25 22:26:29 +00001913 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001914
1915 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1916 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001917}
1918
1919/*
1920 * List window variables.
1921 */
1922 static void
1923list_win_vars()
1924{
Bram Moolenaar33570922005-01-25 22:26:29 +00001925 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001926}
1927
1928/*
1929 * List Vim variables.
1930 */
1931 static void
1932list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933{
Bram Moolenaar33570922005-01-25 22:26:29 +00001934 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935}
1936
1937/*
1938 * List variables in "arg".
1939 */
1940 static char_u *
1941list_arg_vars(eap, arg)
1942 exarg_T *eap;
1943 char_u *arg;
1944{
1945 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001946 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001948 char_u *name_start;
1949 char_u *arg_subsc;
1950 char_u *tofree;
1951 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952
1953 while (!ends_excmd(*arg) && !got_int)
1954 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001955 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001957 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001958 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1959 {
1960 emsg_severe = TRUE;
1961 EMSG(_(e_trailing));
1962 break;
1963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001966 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001967 /* get_name_len() takes care of expanding curly braces */
1968 name_start = name = arg;
1969 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1970 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001972 /* This is mainly to keep test 49 working: when expanding
1973 * curly braces fails overrule the exception error message. */
1974 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001976 emsg_severe = TRUE;
1977 EMSG2(_(e_invarg2), arg);
1978 break;
1979 }
1980 error = TRUE;
1981 }
1982 else
1983 {
1984 if (tofree != NULL)
1985 name = tofree;
1986 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001987 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 else
1989 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001990 /* handle d.key, l[idx], f(expr) */
1991 arg_subsc = arg;
1992 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001995 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001997 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001998 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001999 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002000 case 'g': list_glob_vars(); break;
2001 case 'b': list_buf_vars(); break;
2002 case 'w': list_win_vars(); break;
2003 case 'v': list_vim_vars(); break;
2004 default:
2005 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002006 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002007 }
2008 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002009 {
2010 char_u numbuf[NUMBUFLEN];
2011 char_u *tf;
2012 int c;
2013 char_u *s;
2014
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002015 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002016 c = *arg;
2017 *arg = NUL;
2018 list_one_var_a((char_u *)"",
2019 arg == arg_subsc ? name : name_start,
2020 tv.v_type, s == NULL ? (char_u *)"" : s);
2021 *arg = c;
2022 vim_free(tf);
2023 }
2024 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 }
2027 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002028
2029 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002031
2032 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 }
2034
2035 return arg;
2036}
2037
2038/*
2039 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2040 * Returns a pointer to the char just after the var name.
2041 * Returns NULL if there is an error.
2042 */
2043 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002046 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050{
2051 int c1;
2052 char_u *name;
2053 char_u *p;
2054 char_u *arg_end = NULL;
2055 int len;
2056 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002057 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058
2059 /*
2060 * ":let $VAR = expr": Set environment variable.
2061 */
2062 if (*arg == '$')
2063 {
2064 /* Find the end of the name. */
2065 ++arg;
2066 name = arg;
2067 len = get_env_len(&arg);
2068 if (len == 0)
2069 EMSG2(_(e_invarg2), name - 1);
2070 else
2071 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002072 if (op != NULL && (*op == '+' || *op == '-'))
2073 EMSG2(_(e_letwrong), op);
2074 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002075 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 EMSG(_(e_letunexp));
2077 else
2078 {
2079 c1 = name[len];
2080 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002081 p = get_tv_string_chk(tv);
2082 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002083 {
2084 int mustfree = FALSE;
2085 char_u *s = vim_getenv(name, &mustfree);
2086
2087 if (s != NULL)
2088 {
2089 p = tofree = concat_str(s, p);
2090 if (mustfree)
2091 vim_free(s);
2092 }
2093 }
2094 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002095 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002097 if (STRICMP(name, "HOME") == 0)
2098 init_homedir();
2099 else if (didset_vim && STRICMP(name, "VIM") == 0)
2100 didset_vim = FALSE;
2101 else if (didset_vimruntime
2102 && STRICMP(name, "VIMRUNTIME") == 0)
2103 didset_vimruntime = FALSE;
2104 arg_end = arg;
2105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002107 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002108 }
2109 }
2110 }
2111
2112 /*
2113 * ":let &option = expr": Set option value.
2114 * ":let &l:option = expr": Set local option value.
2115 * ":let &g:option = expr": Set global option value.
2116 */
2117 else if (*arg == '&')
2118 {
2119 /* Find the end of the name. */
2120 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 if (p == NULL || (endchars != NULL
2122 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 EMSG(_(e_letunexp));
2124 else
2125 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126 long n;
2127 int opt_type;
2128 long numval;
2129 char_u *stringval = NULL;
2130 char_u *s;
2131
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132 c1 = *p;
2133 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002134
2135 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002136 s = get_tv_string_chk(tv); /* != NULL if number or string */
2137 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002138 {
2139 opt_type = get_option_value(arg, &numval,
2140 &stringval, opt_flags);
2141 if ((opt_type == 1 && *op == '.')
2142 || (opt_type == 0 && *op != '.'))
2143 EMSG2(_(e_letwrong), op);
2144 else
2145 {
2146 if (opt_type == 1) /* number */
2147 {
2148 if (*op == '+')
2149 n = numval + n;
2150 else
2151 n = numval - n;
2152 }
2153 else if (opt_type == 0 && stringval != NULL) /* string */
2154 {
2155 s = concat_str(stringval, s);
2156 vim_free(stringval);
2157 stringval = s;
2158 }
2159 }
2160 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002161 if (s != NULL)
2162 {
2163 set_option_value(arg, n, s, opt_flags);
2164 arg_end = p;
2165 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002167 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 }
2169 }
2170
2171 /*
2172 * ":let @r = expr": Set register contents.
2173 */
2174 else if (*arg == '@')
2175 {
2176 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 if (op != NULL && (*op == '+' || *op == '-'))
2178 EMSG2(_(e_letwrong), op);
2179 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002180 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 EMSG(_(e_letunexp));
2182 else
2183 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002184 char_u *tofree = NULL;
2185 char_u *s;
2186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002187 p = get_tv_string_chk(tv);
2188 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002190 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002191 if (s != NULL)
2192 {
2193 p = tofree = concat_str(s, p);
2194 vim_free(s);
2195 }
2196 }
2197 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002198 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002199 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002200 arg_end = arg + 1;
2201 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002202 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 }
2204 }
2205
2206 /*
2207 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002210 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002212 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002214 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2218 EMSG(_(e_letunexp));
2219 else
2220 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002221 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 arg_end = p;
2223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 }
2227
2228 else
2229 EMSG2(_(e_invarg2), arg);
2230
2231 return arg_end;
2232}
2233
2234/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002235 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2236 */
2237 static int
2238check_changedtick(arg)
2239 char_u *arg;
2240{
2241 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2242 {
2243 EMSG2(_(e_readonlyvar), arg);
2244 return TRUE;
2245 }
2246 return FALSE;
2247}
2248
2249/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 * Get an lval: variable, Dict item or List item that can be assigned a value
2251 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2252 * "name.key", "name.key[expr]" etc.
2253 * Indexing only works if "name" is an existing List or Dictionary.
2254 * "name" points to the start of the name.
2255 * If "rettv" is not NULL it points to the value to be assigned.
2256 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2257 * wrong; must end in space or cmd separator.
2258 *
2259 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 */
2263 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002264get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002266 typval_T *rettv;
2267 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 int unlet;
2269 int skip;
2270 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002271 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274 char_u *expr_start, *expr_end;
2275 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002276 dictitem_T *v;
2277 typval_T var1;
2278 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002280 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002281 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002282 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002283 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002286 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287
2288 if (skip)
2289 {
2290 /* When skipping just find the end of the name. */
2291 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002292 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002293 }
2294
2295 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002296 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 if (expr_start != NULL)
2298 {
2299 /* Don't expand the name when we already know there is an error. */
2300 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2301 && *p != '[' && *p != '.')
2302 {
2303 EMSG(_(e_trailing));
2304 return NULL;
2305 }
2306
2307 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2308 if (lp->ll_exp_name == NULL)
2309 {
2310 /* Report an invalid expression in braces, unless the
2311 * expression evaluation has been cancelled due to an
2312 * aborting error, an interrupt, or an exception. */
2313 if (!aborting() && !quiet)
2314 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002315 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 EMSG2(_(e_invarg2), name);
2317 return NULL;
2318 }
2319 }
2320 lp->ll_name = lp->ll_exp_name;
2321 }
2322 else
2323 lp->ll_name = name;
2324
2325 /* Without [idx] or .key we are done. */
2326 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2327 return p;
2328
2329 cc = *p;
2330 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (v == NULL && !quiet)
2333 EMSG2(_(e_undefvar), lp->ll_name);
2334 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 if (v == NULL)
2336 return NULL;
2337
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 /*
2339 * Loop until no more [idx] or .key is following.
2340 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002341 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002344 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2345 && !(lp->ll_tv->v_type == VAR_DICT
2346 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 if (!quiet)
2349 EMSG(_("E689: Can only index a List or Dictionary"));
2350 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 if (!quiet)
2355 EMSG(_("E708: [:] must come last"));
2356 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002358
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 len = -1;
2360 if (*p == '.')
2361 {
2362 key = p + 1;
2363 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2364 ;
2365 if (len == 0)
2366 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 if (!quiet)
2368 EMSG(_(e_emptykey));
2369 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002370 }
2371 p = key + len;
2372 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002373 else
2374 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002376 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 if (*p == ':')
2378 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002379 else
2380 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 empty1 = FALSE;
2382 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 if (get_tv_string_chk(&var1) == NULL)
2385 {
2386 /* not a number or string */
2387 clear_tv(&var1);
2388 return NULL;
2389 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 }
2391
2392 /* Optionally get the second index [ :expr]. */
2393 if (*p == ':')
2394 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002399 if (!empty1)
2400 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002402 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 if (rettv != NULL && (rettv->v_type != VAR_LIST
2404 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002405 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 if (!quiet)
2407 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002408 if (!empty1)
2409 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 }
2412 p = skipwhite(p + 1);
2413 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 else
2416 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 if (!empty1)
2421 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002423 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 if (get_tv_string_chk(&var2) == NULL)
2425 {
2426 /* not a number or string */
2427 if (!empty1)
2428 clear_tv(&var1);
2429 clear_tv(&var2);
2430 return NULL;
2431 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002437
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002439 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (!quiet)
2441 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 if (!empty1)
2443 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 }
2448
2449 /* Skip to past ']'. */
2450 ++p;
2451 }
2452
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 {
2455 if (len == -1)
2456 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 if (*key == NUL)
2460 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (!quiet)
2462 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 }
2466 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 lp->ll_list = NULL;
2468 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002469 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002472 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 if (len == -1)
2478 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 }
2481 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 if (len == -1)
2486 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 p = NULL;
2489 break;
2490 }
2491 if (len == -1)
2492 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 }
2495 else
2496 {
2497 /*
2498 * Get the number and item for the only or first index of the List.
2499 */
2500 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 else
2503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 clear_tv(&var1);
2506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 lp->ll_list = lp->ll_tv->vval.v_list;
2509 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2510 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (!quiet)
2513 EMSGN(_(e_listidx), lp->ll_n1);
2514 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 }
2518
2519 /*
2520 * May need to find the item or absolute index for the second
2521 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 * When no index given: "lp->ll_empty2" is TRUE.
2523 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002527 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 if (ni == NULL)
2533 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 if (!quiet)
2535 EMSGN(_(e_listidx), lp->ll_n2);
2536 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 }
2540
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2542 if (lp->ll_n1 < 0)
2543 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2544 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002545 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 if (!quiet)
2547 EMSGN(_(e_listidx), lp->ll_n2);
2548 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002549 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002550 }
2551
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 }
2555
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 return p;
2557}
2558
2559/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 */
2562 static void
2563clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565{
2566 vim_free(lp->ll_exp_name);
2567 vim_free(lp->ll_newkey);
2568}
2569
2570/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002571 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002573 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 */
2575 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002576set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002579 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582{
2583 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 listitem_T *ri;
2585 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586
2587 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 cc = *endp;
2592 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002593 if (op != NULL && *op != '=')
2594 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002596
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002597 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002598 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2599 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002600 {
2601 if (tv_op(&tv, rettv, op) == OK)
2602 set_var(lp->ll_name, &tv, FALSE);
2603 clear_tv(&tv);
2604 }
2605 }
2606 else
2607 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002611 else if (tv_check_lock(lp->ll_newkey == NULL
2612 ? lp->ll_tv->v_lock
2613 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2614 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 else if (lp->ll_range)
2616 {
2617 /*
2618 * Assign the List values to the list items.
2619 */
2620 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002621 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002622 if (op != NULL && *op != '=')
2623 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2624 else
2625 {
2626 clear_tv(&lp->ll_li->li_tv);
2627 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2628 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 ri = ri->li_next;
2630 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2631 break;
2632 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002635 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 ri = NULL;
2638 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002639 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 lp->ll_li = lp->ll_li->li_next;
2642 ++lp->ll_n1;
2643 }
2644 if (ri != NULL)
2645 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002646 else if (lp->ll_empty2
2647 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 : lp->ll_n1 != lp->ll_n2)
2649 EMSG(_("E711: List value has not enough items"));
2650 }
2651 else
2652 {
2653 /*
2654 * Assign to a List or Dictionary item.
2655 */
2656 if (lp->ll_newkey != NULL)
2657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002658 if (op != NULL && *op != '=')
2659 {
2660 EMSG2(_(e_letwrong), op);
2661 return;
2662 }
2663
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002665 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (di == NULL)
2667 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002668 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2669 {
2670 vim_free(di);
2671 return;
2672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002675 else if (op != NULL && *op != '=')
2676 {
2677 tv_op(lp->ll_tv, rettv, op);
2678 return;
2679 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002680 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 /*
2684 * Assign the value to the variable or list item.
2685 */
2686 if (copy)
2687 copy_tv(rettv, lp->ll_tv);
2688 else
2689 {
2690 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002691 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002693 }
2694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695}
2696
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002697/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002698 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2699 * Returns OK or FAIL.
2700 */
2701 static int
2702tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002703 typval_T *tv1;
2704 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 char_u *op;
2706{
2707 long n;
2708 char_u numbuf[NUMBUFLEN];
2709 char_u *s;
2710
2711 /* Can't do anything with a Funcref or a Dict on the right. */
2712 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2713 {
2714 switch (tv1->v_type)
2715 {
2716 case VAR_DICT:
2717 case VAR_FUNC:
2718 break;
2719
2720 case VAR_LIST:
2721 if (*op != '+' || tv2->v_type != VAR_LIST)
2722 break;
2723 /* List += List */
2724 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2725 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2726 return OK;
2727
2728 case VAR_NUMBER:
2729 case VAR_STRING:
2730 if (tv2->v_type == VAR_LIST)
2731 break;
2732 if (*op == '+' || *op == '-')
2733 {
2734 /* nr += nr or nr -= nr*/
2735 n = get_tv_number(tv1);
2736 if (*op == '+')
2737 n += get_tv_number(tv2);
2738 else
2739 n -= get_tv_number(tv2);
2740 clear_tv(tv1);
2741 tv1->v_type = VAR_NUMBER;
2742 tv1->vval.v_number = n;
2743 }
2744 else
2745 {
2746 /* str .= str */
2747 s = get_tv_string(tv1);
2748 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2749 clear_tv(tv1);
2750 tv1->v_type = VAR_STRING;
2751 tv1->vval.v_string = s;
2752 }
2753 return OK;
2754 }
2755 }
2756
2757 EMSG2(_(e_letwrong), op);
2758 return FAIL;
2759}
2760
2761/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002762 * Add a watcher to a list.
2763 */
2764 static void
2765list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002766 list_T *l;
2767 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768{
2769 lw->lw_next = l->lv_watch;
2770 l->lv_watch = lw;
2771}
2772
2773/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002774 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775 * No warning when it isn't found...
2776 */
2777 static void
2778list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002779 list_T *l;
2780 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002781{
Bram Moolenaar33570922005-01-25 22:26:29 +00002782 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002783
2784 lwp = &l->lv_watch;
2785 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2786 {
2787 if (lw == lwrem)
2788 {
2789 *lwp = lw->lw_next;
2790 break;
2791 }
2792 lwp = &lw->lw_next;
2793 }
2794}
2795
2796/*
2797 * Just before removing an item from a list: advance watchers to the next
2798 * item.
2799 */
2800 static void
2801list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002802 list_T *l;
2803 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804{
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002806
2807 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2808 if (lw->lw_item == item)
2809 lw->lw_item = item->li_next;
2810}
2811
2812/*
2813 * Evaluate the expression used in a ":for var in expr" command.
2814 * "arg" points to "var".
2815 * Set "*errp" to TRUE for an error, FALSE otherwise;
2816 * Return a pointer that holds the info. Null when there is an error.
2817 */
2818 void *
2819eval_for_line(arg, errp, nextcmdp, skip)
2820 char_u *arg;
2821 int *errp;
2822 char_u **nextcmdp;
2823 int skip;
2824{
Bram Moolenaar33570922005-01-25 22:26:29 +00002825 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002826 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 typval_T tv;
2828 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002829
2830 *errp = TRUE; /* default: there is an error */
2831
Bram Moolenaar33570922005-01-25 22:26:29 +00002832 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833 if (fi == NULL)
2834 return NULL;
2835
2836 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2837 if (expr == NULL)
2838 return fi;
2839
2840 expr = skipwhite(expr);
2841 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2842 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002843 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002844 return fi;
2845 }
2846
2847 if (skip)
2848 ++emsg_skip;
2849 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2850 {
2851 *errp = FALSE;
2852 if (!skip)
2853 {
2854 l = tv.vval.v_list;
2855 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002856 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002857 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002858 clear_tv(&tv);
2859 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860 else
2861 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002862 /* No need to increment the refcount, it's already set for the
2863 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002864 fi->fi_list = l;
2865 list_add_watch(l, &fi->fi_lw);
2866 fi->fi_lw.lw_item = l->lv_first;
2867 }
2868 }
2869 }
2870 if (skip)
2871 --emsg_skip;
2872
2873 return fi;
2874}
2875
2876/*
2877 * Use the first item in a ":for" list. Advance to the next.
2878 * Assign the values to the variable (list). "arg" points to the first one.
2879 * Return TRUE when a valid item was found, FALSE when at end of list or
2880 * something wrong.
2881 */
2882 int
2883next_for_item(fi_void, arg)
2884 void *fi_void;
2885 char_u *arg;
2886{
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002889 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002890
2891 item = fi->fi_lw.lw_item;
2892 if (item == NULL)
2893 result = FALSE;
2894 else
2895 {
2896 fi->fi_lw.lw_item = item->li_next;
2897 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2898 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2899 }
2900 return result;
2901}
2902
2903/*
2904 * Free the structure used to store info used by ":for".
2905 */
2906 void
2907free_for_info(fi_void)
2908 void *fi_void;
2909{
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002912 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002913 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002915 list_unref(fi->fi_list);
2916 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002917 vim_free(fi);
2918}
2919
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2921
2922 void
2923set_context_for_expression(xp, arg, cmdidx)
2924 expand_T *xp;
2925 char_u *arg;
2926 cmdidx_T cmdidx;
2927{
2928 int got_eq = FALSE;
2929 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002930 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002932 if (cmdidx == CMD_let)
2933 {
2934 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002935 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002936 {
2937 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002938 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002939 {
2940 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942 if (vim_iswhite(*p))
2943 break;
2944 }
2945 return;
2946 }
2947 }
2948 else
2949 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2950 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 while ((xp->xp_pattern = vim_strpbrk(arg,
2952 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2953 {
2954 c = *xp->xp_pattern;
2955 if (c == '&')
2956 {
2957 c = xp->xp_pattern[1];
2958 if (c == '&')
2959 {
2960 ++xp->xp_pattern;
2961 xp->xp_context = cmdidx != CMD_let || got_eq
2962 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2963 }
2964 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002967 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2968 xp->xp_pattern += 2;
2969
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972 else if (c == '$')
2973 {
2974 /* environment variable */
2975 xp->xp_context = EXPAND_ENV_VARS;
2976 }
2977 else if (c == '=')
2978 {
2979 got_eq = TRUE;
2980 xp->xp_context = EXPAND_EXPRESSION;
2981 }
2982 else if (c == '<'
2983 && xp->xp_context == EXPAND_FUNCTIONS
2984 && vim_strchr(xp->xp_pattern, '(') == NULL)
2985 {
2986 /* Function name can start with "<SNR>" */
2987 break;
2988 }
2989 else if (cmdidx != CMD_let || got_eq)
2990 {
2991 if (c == '"') /* string */
2992 {
2993 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2994 if (c == '\\' && xp->xp_pattern[1] != NUL)
2995 ++xp->xp_pattern;
2996 xp->xp_context = EXPAND_NOTHING;
2997 }
2998 else if (c == '\'') /* literal string */
2999 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003000 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3002 /* skip */ ;
3003 xp->xp_context = EXPAND_NOTHING;
3004 }
3005 else if (c == '|')
3006 {
3007 if (xp->xp_pattern[1] == '|')
3008 {
3009 ++xp->xp_pattern;
3010 xp->xp_context = EXPAND_EXPRESSION;
3011 }
3012 else
3013 xp->xp_context = EXPAND_COMMANDS;
3014 }
3015 else
3016 xp->xp_context = EXPAND_EXPRESSION;
3017 }
3018 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019 /* Doesn't look like something valid, expand as an expression
3020 * anyway. */
3021 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 arg = xp->xp_pattern;
3023 if (*arg != NUL)
3024 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3025 /* skip */ ;
3026 }
3027 xp->xp_pattern = arg;
3028}
3029
3030#endif /* FEAT_CMDL_COMPL */
3031
3032/*
3033 * ":1,25call func(arg1, arg2)" function call.
3034 */
3035 void
3036ex_call(eap)
3037 exarg_T *eap;
3038{
3039 char_u *arg = eap->arg;
3040 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003044 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 linenr_T lnum;
3046 int doesrange;
3047 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003048 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003050 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3051 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 if (tofree == NULL)
3053 return;
3054
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003055 /* Increase refcount on dictionary, it could get deleted when evaluating
3056 * the arguments. */
3057 if (fudi.fd_dict != NULL)
3058 ++fudi.fd_dict->dv_refcount;
3059
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3061 len = STRLEN(tofree);
3062 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaar532c7802005-01-27 14:44:31 +00003064 /* Skip white space to allow ":call func ()". Not good, but required for
3065 * backward compatibility. */
3066 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003067 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 if (*startarg != '(')
3070 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003071 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 goto end;
3073 }
3074
3075 /*
3076 * When skipping, evaluate the function once, to find the end of the
3077 * arguments.
3078 * When the function takes a range, this is discovered after the first
3079 * call, and the loop is broken.
3080 */
3081 if (eap->skip)
3082 {
3083 ++emsg_skip;
3084 lnum = eap->line2; /* do it once, also with an invalid range */
3085 }
3086 else
3087 lnum = eap->line1;
3088 for ( ; lnum <= eap->line2; ++lnum)
3089 {
3090 if (!eap->skip && eap->addr_count > 0)
3091 {
3092 curwin->w_cursor.lnum = lnum;
3093 curwin->w_cursor.col = 0;
3094 }
3095 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003097 eap->line1, eap->line2, &doesrange,
3098 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 {
3100 failed = TRUE;
3101 break;
3102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003103 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (doesrange || eap->skip)
3105 break;
3106 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003107 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003108 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003109 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 if (aborting())
3111 break;
3112 }
3113 if (eap->skip)
3114 --emsg_skip;
3115
3116 if (!failed)
3117 {
3118 /* Check for trailing illegal characters and a following command. */
3119 if (!ends_excmd(*arg))
3120 {
3121 emsg_severe = TRUE;
3122 EMSG(_(e_trailing));
3123 }
3124 else
3125 eap->nextcmd = check_nextcmd(arg);
3126 }
3127
3128end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003129 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131}
3132
3133/*
3134 * ":unlet[!] var1 ... " command.
3135 */
3136 void
3137ex_unlet(eap)
3138 exarg_T *eap;
3139{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003140 ex_unletlock(eap, eap->arg, 0);
3141}
3142
3143/*
3144 * ":lockvar" and ":unlockvar" commands
3145 */
3146 void
3147ex_lockvar(eap)
3148 exarg_T *eap;
3149{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003151 int deep = 2;
3152
3153 if (eap->forceit)
3154 deep = -1;
3155 else if (vim_isdigit(*arg))
3156 {
3157 deep = getdigits(&arg);
3158 arg = skipwhite(arg);
3159 }
3160
3161 ex_unletlock(eap, arg, deep);
3162}
3163
3164/*
3165 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3166 */
3167 static void
3168ex_unletlock(eap, argstart, deep)
3169 exarg_T *eap;
3170 char_u *argstart;
3171 int deep;
3172{
3173 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003176 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
3178 do
3179 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003180 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003181 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3182 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003183 if (lv.ll_name == NULL)
3184 error = TRUE; /* error but continue parsing */
3185 if (name_end == NULL || (!vim_iswhite(*name_end)
3186 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003188 if (name_end != NULL)
3189 {
3190 emsg_severe = TRUE;
3191 EMSG(_(e_trailing));
3192 }
3193 if (!(eap->skip || error))
3194 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 break;
3196 }
3197
3198 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003199 {
3200 if (eap->cmdidx == CMD_unlet)
3201 {
3202 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3203 error = TRUE;
3204 }
3205 else
3206 {
3207 if (do_lock_var(&lv, name_end, deep,
3208 eap->cmdidx == CMD_lockvar) == FAIL)
3209 error = TRUE;
3210 }
3211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003213 if (!eap->skip)
3214 clear_lval(&lv);
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 arg = skipwhite(name_end);
3217 } while (!ends_excmd(*arg));
3218
3219 eap->nextcmd = check_nextcmd(arg);
3220}
3221
Bram Moolenaar8c711452005-01-14 21:53:12 +00003222 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003226 int forceit;
3227{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 int ret = OK;
3229 int cc;
3230
3231 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003232 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 cc = *name_end;
3234 *name_end = NUL;
3235
3236 /* Normal name or expanded name. */
3237 if (check_changedtick(lp->ll_name))
3238 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003239 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003240 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003241 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003242 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003243 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3244 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003245 else if (lp->ll_range)
3246 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003248
3249 /* Delete a range of List items. */
3250 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3251 {
3252 li = lp->ll_li->li_next;
3253 listitem_remove(lp->ll_list, lp->ll_li);
3254 lp->ll_li = li;
3255 ++lp->ll_n1;
3256 }
3257 }
3258 else
3259 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003260 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003261 /* unlet a List item. */
3262 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003263 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003264 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003265 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003266 }
3267
3268 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003269}
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271/*
3272 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003273 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 */
3275 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003276do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003278 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 hashtab_T *ht;
3281 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003282 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 ht = find_var_ht(name, &varname);
3285 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003287 hi = hash_find(ht, varname);
3288 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003289 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003290 if (var_check_ro(HI2DI(hi)->di_flags, name))
3291 return FAIL;
3292 delete_var(ht, hi);
3293 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003296 if (forceit)
3297 return OK;
3298 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 return FAIL;
3300}
3301
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003302/*
3303 * Lock or unlock variable indicated by "lp".
3304 * "deep" is the levels to go (-1 for unlimited);
3305 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3306 */
3307 static int
3308do_lock_var(lp, name_end, deep, lock)
3309 lval_T *lp;
3310 char_u *name_end;
3311 int deep;
3312 int lock;
3313{
3314 int ret = OK;
3315 int cc;
3316 dictitem_T *di;
3317
3318 if (deep == 0) /* nothing to do */
3319 return OK;
3320
3321 if (lp->ll_tv == NULL)
3322 {
3323 cc = *name_end;
3324 *name_end = NUL;
3325
3326 /* Normal name or expanded name. */
3327 if (check_changedtick(lp->ll_name))
3328 ret = FAIL;
3329 else
3330 {
3331 di = find_var(lp->ll_name, NULL);
3332 if (di == NULL)
3333 ret = FAIL;
3334 else
3335 {
3336 if (lock)
3337 di->di_flags |= DI_FLAGS_LOCK;
3338 else
3339 di->di_flags &= ~DI_FLAGS_LOCK;
3340 item_lock(&di->di_tv, deep, lock);
3341 }
3342 }
3343 *name_end = cc;
3344 }
3345 else if (lp->ll_range)
3346 {
3347 listitem_T *li = lp->ll_li;
3348
3349 /* (un)lock a range of List items. */
3350 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3351 {
3352 item_lock(&li->li_tv, deep, lock);
3353 li = li->li_next;
3354 ++lp->ll_n1;
3355 }
3356 }
3357 else if (lp->ll_list != NULL)
3358 /* (un)lock a List item. */
3359 item_lock(&lp->ll_li->li_tv, deep, lock);
3360 else
3361 /* un(lock) a Dictionary item. */
3362 item_lock(&lp->ll_di->di_tv, deep, lock);
3363
3364 return ret;
3365}
3366
3367/*
3368 * Lock or unlock an item. "deep" is nr of levels to go.
3369 */
3370 static void
3371item_lock(tv, deep, lock)
3372 typval_T *tv;
3373 int deep;
3374 int lock;
3375{
3376 static int recurse = 0;
3377 list_T *l;
3378 listitem_T *li;
3379 dict_T *d;
3380 hashitem_T *hi;
3381 int todo;
3382
3383 if (recurse >= DICT_MAXNEST)
3384 {
3385 EMSG(_("E743: variable nested too deep for (un)lock"));
3386 return;
3387 }
3388 if (deep == 0)
3389 return;
3390 ++recurse;
3391
3392 /* lock/unlock the item itself */
3393 if (lock)
3394 tv->v_lock |= VAR_LOCKED;
3395 else
3396 tv->v_lock &= ~VAR_LOCKED;
3397
3398 switch (tv->v_type)
3399 {
3400 case VAR_LIST:
3401 if ((l = tv->vval.v_list) != NULL)
3402 {
3403 if (lock)
3404 l->lv_lock |= VAR_LOCKED;
3405 else
3406 l->lv_lock &= ~VAR_LOCKED;
3407 if (deep < 0 || deep > 1)
3408 /* recursive: lock/unlock the items the List contains */
3409 for (li = l->lv_first; li != NULL; li = li->li_next)
3410 item_lock(&li->li_tv, deep - 1, lock);
3411 }
3412 break;
3413 case VAR_DICT:
3414 if ((d = tv->vval.v_dict) != NULL)
3415 {
3416 if (lock)
3417 d->dv_lock |= VAR_LOCKED;
3418 else
3419 d->dv_lock &= ~VAR_LOCKED;
3420 if (deep < 0 || deep > 1)
3421 {
3422 /* recursive: lock/unlock the items the List contains */
3423 todo = d->dv_hashtab.ht_used;
3424 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3425 {
3426 if (!HASHITEM_EMPTY(hi))
3427 {
3428 --todo;
3429 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3430 }
3431 }
3432 }
3433 }
3434 }
3435 --recurse;
3436}
3437
Bram Moolenaara40058a2005-07-11 22:42:07 +00003438/*
3439 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3440 * it refers to a List or Dictionary that is locked.
3441 */
3442 static int
3443tv_islocked(tv)
3444 typval_T *tv;
3445{
3446 return (tv->v_lock & VAR_LOCKED)
3447 || (tv->v_type == VAR_LIST
3448 && tv->vval.v_list != NULL
3449 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3450 || (tv->v_type == VAR_DICT
3451 && tv->vval.v_dict != NULL
3452 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3453}
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3456/*
3457 * Delete all "menutrans_" variables.
3458 */
3459 void
3460del_menutrans_vars()
3461{
Bram Moolenaar33570922005-01-25 22:26:29 +00003462 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003463 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
Bram Moolenaar33570922005-01-25 22:26:29 +00003465 hash_lock(&globvarht);
3466 todo = globvarht.ht_used;
3467 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003468 {
3469 if (!HASHITEM_EMPTY(hi))
3470 {
3471 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003472 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3473 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003474 }
3475 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003476 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477}
3478#endif
3479
3480#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3481
3482/*
3483 * Local string buffer for the next two functions to store a variable name
3484 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3485 * get_user_var_name().
3486 */
3487
3488static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3489
3490static char_u *varnamebuf = NULL;
3491static int varnamebuflen = 0;
3492
3493/*
3494 * Function to concatenate a prefix and a variable name.
3495 */
3496 static char_u *
3497cat_prefix_varname(prefix, name)
3498 int prefix;
3499 char_u *name;
3500{
3501 int len;
3502
3503 len = (int)STRLEN(name) + 3;
3504 if (len > varnamebuflen)
3505 {
3506 vim_free(varnamebuf);
3507 len += 10; /* some additional space */
3508 varnamebuf = alloc(len);
3509 if (varnamebuf == NULL)
3510 {
3511 varnamebuflen = 0;
3512 return NULL;
3513 }
3514 varnamebuflen = len;
3515 }
3516 *varnamebuf = prefix;
3517 varnamebuf[1] = ':';
3518 STRCPY(varnamebuf + 2, name);
3519 return varnamebuf;
3520}
3521
3522/*
3523 * Function given to ExpandGeneric() to obtain the list of user defined
3524 * (global/buffer/window/built-in) variable names.
3525 */
3526/*ARGSUSED*/
3527 char_u *
3528get_user_var_name(xp, idx)
3529 expand_T *xp;
3530 int idx;
3531{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003532 static long_u gdone;
3533 static long_u bdone;
3534 static long_u wdone;
3535 static int vidx;
3536 static hashitem_T *hi;
3537 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
3539 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003540 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003541
3542 /* Global variables */
3543 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003545 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003547 else
3548 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003549 while (HASHITEM_EMPTY(hi))
3550 ++hi;
3551 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3552 return cat_prefix_varname('g', hi->hi_key);
3553 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003555
3556 /* b: variables */
3557 ht = &curbuf->b_vars.dv_hashtab;
3558 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003560 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003561 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003562 else
3563 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003564 while (HASHITEM_EMPTY(hi))
3565 ++hi;
3566 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003568 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003570 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 return (char_u *)"b:changedtick";
3572 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003573
3574 /* w: variables */
3575 ht = &curwin->w_vars.dv_hashtab;
3576 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003578 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003579 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003580 else
3581 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003582 while (HASHITEM_EMPTY(hi))
3583 ++hi;
3584 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003586
3587 /* v: variables */
3588 if (vidx < VV_LEN)
3589 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
3591 vim_free(varnamebuf);
3592 varnamebuf = NULL;
3593 varnamebuflen = 0;
3594 return NULL;
3595}
3596
3597#endif /* FEAT_CMDL_COMPL */
3598
3599/*
3600 * types for expressions.
3601 */
3602typedef enum
3603{
3604 TYPE_UNKNOWN = 0
3605 , TYPE_EQUAL /* == */
3606 , TYPE_NEQUAL /* != */
3607 , TYPE_GREATER /* > */
3608 , TYPE_GEQUAL /* >= */
3609 , TYPE_SMALLER /* < */
3610 , TYPE_SEQUAL /* <= */
3611 , TYPE_MATCH /* =~ */
3612 , TYPE_NOMATCH /* !~ */
3613} exptype_T;
3614
3615/*
3616 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003617 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3619 */
3620
3621/*
3622 * Handle zero level expression.
3623 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003624 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003625 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 * Return OK or FAIL.
3627 */
3628 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003629eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 char_u **nextcmd;
3633 int evaluate;
3634{
3635 int ret;
3636 char_u *p;
3637
3638 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003639 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (ret == FAIL || !ends_excmd(*p))
3641 {
3642 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003643 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 /*
3645 * Report the invalid expression unless the expression evaluation has
3646 * been cancelled due to an aborting error, an interrupt, or an
3647 * exception.
3648 */
3649 if (!aborting())
3650 EMSG2(_(e_invexpr2), arg);
3651 ret = FAIL;
3652 }
3653 if (nextcmd != NULL)
3654 *nextcmd = check_nextcmd(p);
3655
3656 return ret;
3657}
3658
3659/*
3660 * Handle top level expression:
3661 * expr1 ? expr0 : expr0
3662 *
3663 * "arg" must point to the first non-white of the expression.
3664 * "arg" is advanced to the next non-white after the recognized expression.
3665 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003666 * Note: "rettv.v_lock" is not set.
3667 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 * Return OK or FAIL.
3669 */
3670 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003671eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 int evaluate;
3675{
3676 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003677 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
3679 /*
3680 * Get the first variable.
3681 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 return FAIL;
3684
3685 if ((*arg)[0] == '?')
3686 {
3687 result = FALSE;
3688 if (evaluate)
3689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003690 int error = FALSE;
3691
3692 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003694 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003695 if (error)
3696 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698
3699 /*
3700 * Get the second variable.
3701 */
3702 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003703 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 return FAIL;
3705
3706 /*
3707 * Check for the ":".
3708 */
3709 if ((*arg)[0] != ':')
3710 {
3711 EMSG(_("E109: Missing ':' after '?'"));
3712 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return FAIL;
3715 }
3716
3717 /*
3718 * Get the third variable.
3719 */
3720 *arg = skipwhite(*arg + 1);
3721 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3722 {
3723 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 return FAIL;
3726 }
3727 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
3730
3731 return OK;
3732}
3733
3734/*
3735 * Handle first level expression:
3736 * expr2 || expr2 || expr2 logical OR
3737 *
3738 * "arg" must point to the first non-white of the expression.
3739 * "arg" is advanced to the next non-white after the recognized expression.
3740 *
3741 * Return OK or FAIL.
3742 */
3743 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003744eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 int evaluate;
3748{
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 long result;
3751 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003752 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
3754 /*
3755 * Get the first variable.
3756 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 return FAIL;
3759
3760 /*
3761 * Repeat until there is no following "||".
3762 */
3763 first = TRUE;
3764 result = FALSE;
3765 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3766 {
3767 if (evaluate && first)
3768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003769 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003772 if (error)
3773 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 first = FALSE;
3775 }
3776
3777 /*
3778 * Get the second variable.
3779 */
3780 *arg = skipwhite(*arg + 2);
3781 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3782 return FAIL;
3783
3784 /*
3785 * Compute the result.
3786 */
3787 if (evaluate && !result)
3788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003791 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 if (error)
3793 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 if (evaluate)
3796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 rettv->v_type = VAR_NUMBER;
3798 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800 }
3801
3802 return OK;
3803}
3804
3805/*
3806 * Handle second level expression:
3807 * expr3 && expr3 && expr3 logical AND
3808 *
3809 * "arg" must point to the first non-white of the expression.
3810 * "arg" is advanced to the next non-white after the recognized expression.
3811 *
3812 * Return OK or FAIL.
3813 */
3814 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003815eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 int evaluate;
3819{
Bram Moolenaar33570922005-01-25 22:26:29 +00003820 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 long result;
3822 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003823 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
3825 /*
3826 * Get the first variable.
3827 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003828 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 return FAIL;
3830
3831 /*
3832 * Repeat until there is no following "&&".
3833 */
3834 first = TRUE;
3835 result = TRUE;
3836 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3837 {
3838 if (evaluate && first)
3839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003840 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003842 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003843 if (error)
3844 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 first = FALSE;
3846 }
3847
3848 /*
3849 * Get the second variable.
3850 */
3851 *arg = skipwhite(*arg + 2);
3852 if (eval4(arg, &var2, evaluate && result) == FAIL)
3853 return FAIL;
3854
3855 /*
3856 * Compute the result.
3857 */
3858 if (evaluate && result)
3859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003860 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003862 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 if (error)
3864 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866 if (evaluate)
3867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868 rettv->v_type = VAR_NUMBER;
3869 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
3871 }
3872
3873 return OK;
3874}
3875
3876/*
3877 * Handle third level expression:
3878 * var1 == var2
3879 * var1 =~ var2
3880 * var1 != var2
3881 * var1 !~ var2
3882 * var1 > var2
3883 * var1 >= var2
3884 * var1 < var2
3885 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003886 * var1 is var2
3887 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 *
3889 * "arg" must point to the first non-white of the expression.
3890 * "arg" is advanced to the next non-white after the recognized expression.
3891 *
3892 * Return OK or FAIL.
3893 */
3894 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003895eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 int evaluate;
3899{
Bram Moolenaar33570922005-01-25 22:26:29 +00003900 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 char_u *p;
3902 int i;
3903 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003904 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 int len = 2;
3906 long n1, n2;
3907 char_u *s1, *s2;
3908 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3909 regmatch_T regmatch;
3910 int ic;
3911 char_u *save_cpo;
3912
3913 /*
3914 * Get the first variable.
3915 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003916 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 return FAIL;
3918
3919 p = *arg;
3920 switch (p[0])
3921 {
3922 case '=': if (p[1] == '=')
3923 type = TYPE_EQUAL;
3924 else if (p[1] == '~')
3925 type = TYPE_MATCH;
3926 break;
3927 case '!': if (p[1] == '=')
3928 type = TYPE_NEQUAL;
3929 else if (p[1] == '~')
3930 type = TYPE_NOMATCH;
3931 break;
3932 case '>': if (p[1] != '=')
3933 {
3934 type = TYPE_GREATER;
3935 len = 1;
3936 }
3937 else
3938 type = TYPE_GEQUAL;
3939 break;
3940 case '<': if (p[1] != '=')
3941 {
3942 type = TYPE_SMALLER;
3943 len = 1;
3944 }
3945 else
3946 type = TYPE_SEQUAL;
3947 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003948 case 'i': if (p[1] == 's')
3949 {
3950 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3951 len = 5;
3952 if (!vim_isIDc(p[len]))
3953 {
3954 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3955 type_is = TRUE;
3956 }
3957 }
3958 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960
3961 /*
3962 * If there is a comparitive operator, use it.
3963 */
3964 if (type != TYPE_UNKNOWN)
3965 {
3966 /* extra question mark appended: ignore case */
3967 if (p[len] == '?')
3968 {
3969 ic = TRUE;
3970 ++len;
3971 }
3972 /* extra '#' appended: match case */
3973 else if (p[len] == '#')
3974 {
3975 ic = FALSE;
3976 ++len;
3977 }
3978 /* nothing appened: use 'ignorecase' */
3979 else
3980 ic = p_ic;
3981
3982 /*
3983 * Get the second variable.
3984 */
3985 *arg = skipwhite(p + len);
3986 if (eval5(arg, &var2, evaluate) == FAIL)
3987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003988 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 return FAIL;
3990 }
3991
3992 if (evaluate)
3993 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003994 if (type_is && rettv->v_type != var2.v_type)
3995 {
3996 /* For "is" a different type always means FALSE, for "notis"
3997 * it means TRUE. */
3998 n1 = (type == TYPE_NEQUAL);
3999 }
4000 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4001 {
4002 if (type_is)
4003 {
4004 n1 = (rettv->v_type == var2.v_type
4005 && rettv->vval.v_list == var2.vval.v_list);
4006 if (type == TYPE_NEQUAL)
4007 n1 = !n1;
4008 }
4009 else if (rettv->v_type != var2.v_type
4010 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4011 {
4012 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004013 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004014 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004015 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004016 clear_tv(rettv);
4017 clear_tv(&var2);
4018 return FAIL;
4019 }
4020 else
4021 {
4022 /* Compare two Lists for being equal or unequal. */
4023 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4024 if (type == TYPE_NEQUAL)
4025 n1 = !n1;
4026 }
4027 }
4028
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004029 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4030 {
4031 if (type_is)
4032 {
4033 n1 = (rettv->v_type == var2.v_type
4034 && rettv->vval.v_dict == var2.vval.v_dict);
4035 if (type == TYPE_NEQUAL)
4036 n1 = !n1;
4037 }
4038 else if (rettv->v_type != var2.v_type
4039 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4040 {
4041 if (rettv->v_type != var2.v_type)
4042 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4043 else
4044 EMSG(_("E736: Invalid operation for Dictionary"));
4045 clear_tv(rettv);
4046 clear_tv(&var2);
4047 return FAIL;
4048 }
4049 else
4050 {
4051 /* Compare two Dictionaries for being equal or unequal. */
4052 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4053 if (type == TYPE_NEQUAL)
4054 n1 = !n1;
4055 }
4056 }
4057
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004058 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4059 {
4060 if (rettv->v_type != var2.v_type
4061 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4062 {
4063 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004064 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004065 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004066 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004067 clear_tv(rettv);
4068 clear_tv(&var2);
4069 return FAIL;
4070 }
4071 else
4072 {
4073 /* Compare two Funcrefs for being equal or unequal. */
4074 if (rettv->vval.v_string == NULL
4075 || var2.vval.v_string == NULL)
4076 n1 = FALSE;
4077 else
4078 n1 = STRCMP(rettv->vval.v_string,
4079 var2.vval.v_string) == 0;
4080 if (type == TYPE_NEQUAL)
4081 n1 = !n1;
4082 }
4083 }
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 /*
4086 * If one of the two variables is a number, compare as a number.
4087 * When using "=~" or "!~", always compare as string.
4088 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004089 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 n1 = get_tv_number(rettv);
4093 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 switch (type)
4095 {
4096 case TYPE_EQUAL: n1 = (n1 == n2); break;
4097 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4098 case TYPE_GREATER: n1 = (n1 > n2); break;
4099 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4100 case TYPE_SMALLER: n1 = (n1 < n2); break;
4101 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4102 case TYPE_UNKNOWN:
4103 case TYPE_MATCH:
4104 case TYPE_NOMATCH: break; /* avoid gcc warning */
4105 }
4106 }
4107 else
4108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 s1 = get_tv_string_buf(rettv, buf1);
4110 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4112 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4113 else
4114 i = 0;
4115 n1 = FALSE;
4116 switch (type)
4117 {
4118 case TYPE_EQUAL: n1 = (i == 0); break;
4119 case TYPE_NEQUAL: n1 = (i != 0); break;
4120 case TYPE_GREATER: n1 = (i > 0); break;
4121 case TYPE_GEQUAL: n1 = (i >= 0); break;
4122 case TYPE_SMALLER: n1 = (i < 0); break;
4123 case TYPE_SEQUAL: n1 = (i <= 0); break;
4124
4125 case TYPE_MATCH:
4126 case TYPE_NOMATCH:
4127 /* avoid 'l' flag in 'cpoptions' */
4128 save_cpo = p_cpo;
4129 p_cpo = (char_u *)"";
4130 regmatch.regprog = vim_regcomp(s2,
4131 RE_MAGIC + RE_STRING);
4132 regmatch.rm_ic = ic;
4133 if (regmatch.regprog != NULL)
4134 {
4135 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4136 vim_free(regmatch.regprog);
4137 if (type == TYPE_NOMATCH)
4138 n1 = !n1;
4139 }
4140 p_cpo = save_cpo;
4141 break;
4142
4143 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4144 }
4145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004146 clear_tv(rettv);
4147 clear_tv(&var2);
4148 rettv->v_type = VAR_NUMBER;
4149 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 }
4151 }
4152
4153 return OK;
4154}
4155
4156/*
4157 * Handle fourth level expression:
4158 * + number addition
4159 * - number subtraction
4160 * . string concatenation
4161 *
4162 * "arg" must point to the first non-white of the expression.
4163 * "arg" is advanced to the next non-white after the recognized expression.
4164 *
4165 * Return OK or FAIL.
4166 */
4167 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 int evaluate;
4172{
Bram Moolenaar33570922005-01-25 22:26:29 +00004173 typval_T var2;
4174 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 int op;
4176 long n1, n2;
4177 char_u *s1, *s2;
4178 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4179 char_u *p;
4180
4181 /*
4182 * Get the first variable.
4183 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 return FAIL;
4186
4187 /*
4188 * Repeat computing, until no '+', '-' or '.' is following.
4189 */
4190 for (;;)
4191 {
4192 op = **arg;
4193 if (op != '+' && op != '-' && op != '.')
4194 break;
4195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 if (op != '+' || rettv->v_type != VAR_LIST)
4197 {
4198 /* For "list + ...", an illegal use of the first operand as
4199 * a number cannot be determined before evaluating the 2nd
4200 * operand: if this is also a list, all is ok.
4201 * For "something . ...", "something - ..." or "non-list + ...",
4202 * we know that the first operand needs to be a string or number
4203 * without evaluating the 2nd operand. So check before to avoid
4204 * side effects after an error. */
4205 if (evaluate && get_tv_string_chk(rettv) == NULL)
4206 {
4207 clear_tv(rettv);
4208 return FAIL;
4209 }
4210 }
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 /*
4213 * Get the second variable.
4214 */
4215 *arg = skipwhite(*arg + 1);
4216 if (eval6(arg, &var2, evaluate) == FAIL)
4217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return FAIL;
4220 }
4221
4222 if (evaluate)
4223 {
4224 /*
4225 * Compute the result.
4226 */
4227 if (op == '.')
4228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004229 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4230 s2 = get_tv_string_buf_chk(&var2, buf2);
4231 if (s2 == NULL) /* type error ? */
4232 {
4233 clear_tv(rettv);
4234 clear_tv(&var2);
4235 return FAIL;
4236 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004237 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 clear_tv(rettv);
4239 rettv->v_type = VAR_STRING;
4240 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004242 else if (op == '+' && rettv->v_type == VAR_LIST
4243 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004244 {
4245 /* concatenate Lists */
4246 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4247 &var3) == FAIL)
4248 {
4249 clear_tv(rettv);
4250 clear_tv(&var2);
4251 return FAIL;
4252 }
4253 clear_tv(rettv);
4254 *rettv = var3;
4255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 else
4257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 int error = FALSE;
4259
4260 n1 = get_tv_number_chk(rettv, &error);
4261 if (error)
4262 {
4263 /* This can only happen for "list + non-list".
4264 * For "non-list + ..." or "something - ...", we returned
4265 * before evaluating the 2nd operand. */
4266 clear_tv(rettv);
4267 return FAIL;
4268 }
4269 n2 = get_tv_number_chk(&var2, &error);
4270 if (error)
4271 {
4272 clear_tv(rettv);
4273 clear_tv(&var2);
4274 return FAIL;
4275 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (op == '+')
4278 n1 = n1 + n2;
4279 else
4280 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 rettv->v_type = VAR_NUMBER;
4282 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287 return OK;
4288}
4289
4290/*
4291 * Handle fifth level expression:
4292 * * number multiplication
4293 * / number division
4294 * % number modulo
4295 *
4296 * "arg" must point to the first non-white of the expression.
4297 * "arg" is advanced to the next non-white after the recognized expression.
4298 *
4299 * Return OK or FAIL.
4300 */
4301 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 int evaluate;
4306{
Bram Moolenaar33570922005-01-25 22:26:29 +00004307 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 int op;
4309 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 /*
4313 * Get the first variable.
4314 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 return FAIL;
4317
4318 /*
4319 * Repeat computing, until no '*', '/' or '%' is following.
4320 */
4321 for (;;)
4322 {
4323 op = **arg;
4324 if (op != '*' && op != '/' && op != '%')
4325 break;
4326
4327 if (evaluate)
4328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (error)
4332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 else
4335 n1 = 0;
4336
4337 /*
4338 * Get the second variable.
4339 */
4340 *arg = skipwhite(*arg + 1);
4341 if (eval7(arg, &var2, evaluate) == FAIL)
4342 return FAIL;
4343
4344 if (evaluate)
4345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (error)
4349 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
4351 /*
4352 * Compute the result.
4353 */
4354 if (op == '*')
4355 n1 = n1 * n2;
4356 else if (op == '/')
4357 {
4358 if (n2 == 0) /* give an error message? */
4359 n1 = 0x7fffffffL;
4360 else
4361 n1 = n1 / n2;
4362 }
4363 else
4364 {
4365 if (n2 == 0) /* give an error message? */
4366 n1 = 0;
4367 else
4368 n1 = n1 % n2;
4369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 rettv->v_type = VAR_NUMBER;
4371 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 }
4374
4375 return OK;
4376}
4377
4378/*
4379 * Handle sixth level expression:
4380 * number number constant
4381 * "string" string contstant
4382 * 'string' literal string contstant
4383 * &option-name option value
4384 * @r register contents
4385 * identifier variable value
4386 * function() function call
4387 * $VAR environment variable
4388 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004389 * [expr, expr] List
4390 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 *
4392 * Also handle:
4393 * ! in front logical NOT
4394 * - in front unary minus
4395 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004396 * trailing [] subscript in String or List
4397 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 *
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 +00004405eval7(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 Moolenaar071d4272004-06-13 20:20:40 +00004410 long n;
4411 int len;
4412 char_u *s;
4413 int val;
4414 char_u *start_leader, *end_leader;
4415 int ret = OK;
4416 char_u *alias;
4417
4418 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004419 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004420 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004422 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
4424 /*
4425 * Skip '!' and '-' characters. They are handled later.
4426 */
4427 start_leader = *arg;
4428 while (**arg == '!' || **arg == '-' || **arg == '+')
4429 *arg = skipwhite(*arg + 1);
4430 end_leader = *arg;
4431
4432 switch (**arg)
4433 {
4434 /*
4435 * Number constant.
4436 */
4437 case '0':
4438 case '1':
4439 case '2':
4440 case '3':
4441 case '4':
4442 case '5':
4443 case '6':
4444 case '7':
4445 case '8':
4446 case '9':
4447 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4448 *arg += len;
4449 if (evaluate)
4450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004451 rettv->v_type = VAR_NUMBER;
4452 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
4454 break;
4455
4456 /*
4457 * String constant: "string".
4458 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 break;
4461
4462 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004466 break;
4467
4468 /*
4469 * List: [expr, expr]
4470 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 break;
4473
4474 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004475 * Dictionary: {key: val, key: val}
4476 */
4477 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4478 break;
4479
4480 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004481 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004483 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 break;
4485
4486 /*
4487 * Environment variable: $VAR.
4488 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004489 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 break;
4491
4492 /*
4493 * Register contents: @r.
4494 */
4495 case '@': ++*arg;
4496 if (evaluate)
4497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004499 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 if (**arg != NUL)
4502 ++*arg;
4503 break;
4504
4505 /*
4506 * nested expression: (expression).
4507 */
4508 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 if (**arg == ')')
4511 ++*arg;
4512 else if (ret == OK)
4513 {
4514 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004515 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 ret = FAIL;
4517 }
4518 break;
4519
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 break;
4522 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004523
4524 if (ret == NOTDONE)
4525 {
4526 /*
4527 * Must be a variable or function name.
4528 * Can also be a curly-braces kind of name: {expr}.
4529 */
4530 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004531 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004532 if (alias != NULL)
4533 s = alias;
4534
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004535 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 ret = FAIL;
4537 else
4538 {
4539 if (**arg == '(') /* recursive! */
4540 {
4541 /* If "s" is the name of a variable of type VAR_FUNC
4542 * use its contents. */
4543 s = deref_func_name(s, &len);
4544
4545 /* Invoke the function. */
4546 ret = get_func_tv(s, len, rettv, arg,
4547 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004548 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 /* Stop the expression evaluation when immediately
4550 * aborting on error, or when an interrupt occurred or
4551 * an exception was thrown but not caught. */
4552 if (aborting())
4553 {
4554 if (ret == OK)
4555 clear_tv(rettv);
4556 ret = FAIL;
4557 }
4558 }
4559 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004560 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004561 else
4562 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 }
4564
4565 if (alias != NULL)
4566 vim_free(alias);
4567 }
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 *arg = skipwhite(*arg);
4570
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004571 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4572 * expr(expr). */
4573 if (ret == OK)
4574 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
4576 /*
4577 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4578 */
4579 if (ret == OK && evaluate && end_leader > start_leader)
4580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004581 int error = FALSE;
4582
4583 val = get_tv_number_chk(rettv, &error);
4584 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004586 clear_tv(rettv);
4587 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004589 else
4590 {
4591 while (end_leader > start_leader)
4592 {
4593 --end_leader;
4594 if (*end_leader == '!')
4595 val = !val;
4596 else if (*end_leader == '-')
4597 val = -val;
4598 }
4599 clear_tv(rettv);
4600 rettv->v_type = VAR_NUMBER;
4601 rettv->vval.v_number = val;
4602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
4604
4605 return ret;
4606}
4607
4608/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 * Evaluate an "[expr]" or "[expr:expr]" index.
4610 * "*arg" points to the '['.
4611 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4612 */
4613 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004614eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004616 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004618 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619{
4620 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004621 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004623 long len = -1;
4624 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004627
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004630 if (verbose)
4631 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 return FAIL;
4633 }
4634
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004636 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637 /*
4638 * dict.name
4639 */
4640 key = *arg + 1;
4641 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4642 ;
4643 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004644 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004645 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 }
4647 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649 /*
4650 * something[idx]
4651 *
4652 * Get the (first) variable from inside the [].
4653 */
4654 *arg = skipwhite(*arg + 1);
4655 if (**arg == ':')
4656 empty1 = TRUE;
4657 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4658 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004659 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4660 {
4661 /* not a number or string */
4662 clear_tv(&var1);
4663 return FAIL;
4664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665
4666 /*
4667 * Get the second variable from inside the [:].
4668 */
4669 if (**arg == ':')
4670 {
4671 range = TRUE;
4672 *arg = skipwhite(*arg + 1);
4673 if (**arg == ']')
4674 empty2 = TRUE;
4675 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004677 if (!empty1)
4678 clear_tv(&var1);
4679 return FAIL;
4680 }
4681 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4682 {
4683 /* not a number or string */
4684 if (!empty1)
4685 clear_tv(&var1);
4686 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 return FAIL;
4688 }
4689 }
4690
4691 /* Check for the ']'. */
4692 if (**arg != ']')
4693 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004694 if (verbose)
4695 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 clear_tv(&var1);
4697 if (range)
4698 clear_tv(&var2);
4699 return FAIL;
4700 }
4701 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 }
4703
4704 if (evaluate)
4705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004706 n1 = 0;
4707 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 n1 = get_tv_number(&var1);
4710 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 }
4712 if (range)
4713 {
4714 if (empty2)
4715 n2 = -1;
4716 else
4717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718 n2 = get_tv_number(&var2);
4719 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 }
4721 }
4722
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004723 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724 {
4725 case VAR_NUMBER:
4726 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004727 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 len = (long)STRLEN(s);
4729 if (range)
4730 {
4731 /* The resulting variable is a substring. If the indexes
4732 * are out of range the result is empty. */
4733 if (n1 < 0)
4734 {
4735 n1 = len + n1;
4736 if (n1 < 0)
4737 n1 = 0;
4738 }
4739 if (n2 < 0)
4740 n2 = len + n2;
4741 else if (n2 >= len)
4742 n2 = len;
4743 if (n1 >= len || n2 < 0 || n1 > n2)
4744 s = NULL;
4745 else
4746 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4747 }
4748 else
4749 {
4750 /* The resulting variable is a string of a single
4751 * character. If the index is too big or negative the
4752 * result is empty. */
4753 if (n1 >= len || n1 < 0)
4754 s = NULL;
4755 else
4756 s = vim_strnsave(s + n1, 1);
4757 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(rettv);
4759 rettv->v_type = VAR_STRING;
4760 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761 break;
4762
4763 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 if (n1 < 0)
4766 n1 = len + n1;
4767 if (!empty1 && (n1 < 0 || n1 >= len))
4768 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004769 if (verbose)
4770 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 return FAIL;
4772 }
4773 if (range)
4774 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004775 list_T *l;
4776 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777
4778 if (n2 < 0)
4779 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004782 if (verbose)
4783 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784 return FAIL;
4785 }
4786 l = list_alloc();
4787 if (l == NULL)
4788 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 n1 <= n2; ++n1)
4791 {
4792 if (list_append_tv(l, &item->li_tv) == FAIL)
4793 {
4794 list_free(l);
4795 return FAIL;
4796 }
4797 item = item->li_next;
4798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004799 clear_tv(rettv);
4800 rettv->v_type = VAR_LIST;
4801 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004802 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803 }
4804 else
4805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004806 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004808 clear_tv(rettv);
4809 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 }
4811 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812
4813 case VAR_DICT:
4814 if (range)
4815 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004816 if (verbose)
4817 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818 if (len == -1)
4819 clear_tv(&var1);
4820 return FAIL;
4821 }
4822 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004823 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004824
4825 if (len == -1)
4826 {
4827 key = get_tv_string(&var1);
4828 if (*key == NUL)
4829 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004830 if (verbose)
4831 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004832 clear_tv(&var1);
4833 return FAIL;
4834 }
4835 }
4836
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004837 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004838
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004839 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004840 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004841 if (len == -1)
4842 clear_tv(&var1);
4843 if (item == NULL)
4844 return FAIL;
4845
4846 copy_tv(&item->di_tv, &var1);
4847 clear_tv(rettv);
4848 *rettv = var1;
4849 }
4850 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004851 }
4852 }
4853
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004854 return OK;
4855}
4856
4857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 * Get an option value.
4859 * "arg" points to the '&' or '+' before the option name.
4860 * "arg" is advanced to character after the option name.
4861 * Return OK or FAIL.
4862 */
4863 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004866 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 int evaluate;
4868{
4869 char_u *option_end;
4870 long numval;
4871 char_u *stringval;
4872 int opt_type;
4873 int c;
4874 int working = (**arg == '+'); /* has("+option") */
4875 int ret = OK;
4876 int opt_flags;
4877
4878 /*
4879 * Isolate the option name and find its value.
4880 */
4881 option_end = find_option_end(arg, &opt_flags);
4882 if (option_end == NULL)
4883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 EMSG2(_("E112: Option name missing: %s"), *arg);
4886 return FAIL;
4887 }
4888
4889 if (!evaluate)
4890 {
4891 *arg = option_end;
4892 return OK;
4893 }
4894
4895 c = *option_end;
4896 *option_end = NUL;
4897 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
4900 if (opt_type == -3) /* invalid name */
4901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 EMSG2(_("E113: Unknown option: %s"), *arg);
4904 ret = FAIL;
4905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004906 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
4908 if (opt_type == -2) /* hidden string option */
4909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004910 rettv->v_type = VAR_STRING;
4911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 else if (opt_type == -1) /* hidden number option */
4914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 rettv->v_type = VAR_NUMBER;
4916 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else if (opt_type == 1) /* number option */
4919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 rettv->v_type = VAR_NUMBER;
4921 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 else /* string option */
4924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004925 rettv->v_type = VAR_STRING;
4926 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 }
4929 else if (working && (opt_type == -2 || opt_type == -1))
4930 ret = FAIL;
4931
4932 *option_end = c; /* put back for error messages */
4933 *arg = option_end;
4934
4935 return ret;
4936}
4937
4938/*
4939 * Allocate a variable for a string constant.
4940 * Return OK or FAIL.
4941 */
4942 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004943get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 int evaluate;
4947{
4948 char_u *p;
4949 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 int extra = 0;
4951
4952 /*
4953 * Find the end of the string, skipping backslashed characters.
4954 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004955 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
4957 if (*p == '\\' && p[1] != NUL)
4958 {
4959 ++p;
4960 /* A "\<x>" form occupies at least 4 characters, and produces up
4961 * to 6 characters: reserve space for 2 extra */
4962 if (*p == '<')
4963 extra += 2;
4964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
4966
4967 if (*p != '"')
4968 {
4969 EMSG2(_("E114: Missing quote: %s"), *arg);
4970 return FAIL;
4971 }
4972
4973 /* If only parsing, set *arg and return here */
4974 if (!evaluate)
4975 {
4976 *arg = p + 1;
4977 return OK;
4978 }
4979
4980 /*
4981 * Copy the string into allocated memory, handling backslashed
4982 * characters.
4983 */
4984 name = alloc((unsigned)(p - *arg + extra));
4985 if (name == NULL)
4986 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 rettv->v_type = VAR_STRING;
4988 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
4992 if (*p == '\\')
4993 {
4994 switch (*++p)
4995 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 case 'b': *name++ = BS; ++p; break;
4997 case 'e': *name++ = ESC; ++p; break;
4998 case 'f': *name++ = FF; ++p; break;
4999 case 'n': *name++ = NL; ++p; break;
5000 case 'r': *name++ = CAR; ++p; break;
5001 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
5003 case 'X': /* hex: "\x1", "\x12" */
5004 case 'x':
5005 case 'u': /* Unicode: "\u0023" */
5006 case 'U':
5007 if (vim_isxdigit(p[1]))
5008 {
5009 int n, nr;
5010 int c = toupper(*p);
5011
5012 if (c == 'X')
5013 n = 2;
5014 else
5015 n = 4;
5016 nr = 0;
5017 while (--n >= 0 && vim_isxdigit(p[1]))
5018 {
5019 ++p;
5020 nr = (nr << 4) + hex2nr(*p);
5021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023#ifdef FEAT_MBYTE
5024 /* For "\u" store the number according to
5025 * 'encoding'. */
5026 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 else
5029#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 break;
5033
5034 /* octal: "\1", "\12", "\123" */
5035 case '0':
5036 case '1':
5037 case '2':
5038 case '3':
5039 case '4':
5040 case '5':
5041 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 case '7': *name = *p++ - '0';
5043 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 *name = (*name << 3) + *p++ - '0';
5046 if (*p >= '0' && *p <= '7')
5047 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 break;
5051
5052 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (extra != 0)
5055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 break;
5058 }
5059 /* FALLTHROUGH */
5060
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 break;
5063 }
5064 }
5065 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 *arg = p + 1;
5071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 return OK;
5073}
5074
5075/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 * Return OK or FAIL.
5078 */
5079 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005080get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 int evaluate;
5084{
5085 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005086 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005087 int reduce = 0;
5088
5089 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 */
5092 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5093 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005095 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 break;
5098 ++reduce;
5099 ++p;
5100 }
5101 }
5102
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 return FAIL;
5107 }
5108
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 if (!evaluate)
5111 {
5112 *arg = p + 1;
5113 return OK;
5114 }
5115
5116 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118 */
5119 str = alloc((unsigned)((p - *arg) - reduce));
5120 if (str == NULL)
5121 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 rettv->v_type = VAR_STRING;
5123 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005124
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005126 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005128 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005130 break;
5131 ++p;
5132 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136 *arg = p + 1;
5137
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005138 return OK;
5139}
5140
5141/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 * Allocate a variable for a List and fill it from "*arg".
5143 * Return OK or FAIL.
5144 */
5145 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005146get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005148 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 int evaluate;
5150{
Bram Moolenaar33570922005-01-25 22:26:29 +00005151 list_T *l = NULL;
5152 typval_T tv;
5153 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154
5155 if (evaluate)
5156 {
5157 l = list_alloc();
5158 if (l == NULL)
5159 return FAIL;
5160 }
5161
5162 *arg = skipwhite(*arg + 1);
5163 while (**arg != ']' && **arg != NUL)
5164 {
5165 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5166 goto failret;
5167 if (evaluate)
5168 {
5169 item = listitem_alloc();
5170 if (item != NULL)
5171 {
5172 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005173 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 list_append(l, item);
5175 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005176 else
5177 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178 }
5179
5180 if (**arg == ']')
5181 break;
5182 if (**arg != ',')
5183 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 goto failret;
5186 }
5187 *arg = skipwhite(*arg + 1);
5188 }
5189
5190 if (**arg != ']')
5191 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193failret:
5194 if (evaluate)
5195 list_free(l);
5196 return FAIL;
5197 }
5198
5199 *arg = skipwhite(*arg + 1);
5200 if (evaluate)
5201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 rettv->v_type = VAR_LIST;
5203 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005204 ++l->lv_refcount;
5205 }
5206
5207 return OK;
5208}
5209
5210/*
5211 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005212 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005214 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215list_alloc()
5216{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217 list_T *l;
5218
5219 l = (list_T *)alloc_clear(sizeof(list_T));
5220 if (l != NULL)
5221 {
5222 /* Prepend the list to the list of lists for garbage collection. */
5223 if (first_list != NULL)
5224 first_list->lv_used_prev = l;
5225 l->lv_used_prev = NULL;
5226 l->lv_used_next = first_list;
5227 first_list = l;
5228 }
5229 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230}
5231
5232/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005233 * Allocate an empty list for a return value.
5234 * Returns OK or FAIL.
5235 */
5236 static int
5237rettv_list_alloc(rettv)
5238 typval_T *rettv;
5239{
5240 list_T *l = list_alloc();
5241
5242 if (l == NULL)
5243 return FAIL;
5244
5245 rettv->vval.v_list = l;
5246 rettv->v_type = VAR_LIST;
5247 ++l->lv_refcount;
5248 return OK;
5249}
5250
5251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 * Unreference a list: decrement the reference count and free it when it
5253 * becomes zero.
5254 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005255 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005259 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5260 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261}
5262
5263/*
5264 * Free a list, including all items it points to.
5265 * Ignores the reference count.
5266 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005267 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005269 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270{
Bram Moolenaar33570922005-01-25 22:26:29 +00005271 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272
Bram Moolenaard9fba312005-06-26 22:34:35 +00005273 /* Avoid that recursive reference to the list frees us again. */
5274 l->lv_refcount = DEL_REFCOUNT;
5275
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005276 /* Remove the list from the list of lists for garbage collection. */
5277 if (l->lv_used_prev == NULL)
5278 first_list = l->lv_used_next;
5279 else
5280 l->lv_used_prev->lv_used_next = l->lv_used_next;
5281 if (l->lv_used_next != NULL)
5282 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5283
Bram Moolenaard9fba312005-06-26 22:34:35 +00005284 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005286 /* Remove the item before deleting it. */
5287 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 listitem_free(item);
5289 }
5290 vim_free(l);
5291}
5292
5293/*
5294 * Allocate a list item.
5295 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005296 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297listitem_alloc()
5298{
Bram Moolenaar33570922005-01-25 22:26:29 +00005299 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300}
5301
5302/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005303 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 */
5305 static void
5306listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005307 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005309 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 vim_free(item);
5311}
5312
5313/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005314 * Remove a list item from a List and free it. Also clears the value.
5315 */
5316 static void
5317listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005318 list_T *l;
5319 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005320{
5321 list_remove(l, item, item);
5322 listitem_free(item);
5323}
5324
5325/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 * Get the number of items in a list.
5327 */
5328 static long
5329list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005330 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 if (l == NULL)
5333 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005334 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335}
5336
5337/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005338 * Return TRUE when two lists have exactly the same values.
5339 */
5340 static int
5341list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005342 list_T *l1;
5343 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005344 int ic; /* ignore case for strings */
5345{
Bram Moolenaar33570922005-01-25 22:26:29 +00005346 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005347
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005348 if (list_len(l1) != list_len(l2))
5349 return FALSE;
5350
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005351 for (item1 = l1->lv_first, item2 = l2->lv_first;
5352 item1 != NULL && item2 != NULL;
5353 item1 = item1->li_next, item2 = item2->li_next)
5354 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5355 return FALSE;
5356 return item1 == NULL && item2 == NULL;
5357}
5358
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005359#if defined(FEAT_PYTHON) || defined(PROTO)
5360/*
5361 * Return the dictitem that an entry in a hashtable points to.
5362 */
5363 dictitem_T *
5364dict_lookup(hi)
5365 hashitem_T *hi;
5366{
5367 return HI2DI(hi);
5368}
5369#endif
5370
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005371/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005372 * Return TRUE when two dictionaries have exactly the same key/values.
5373 */
5374 static int
5375dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005376 dict_T *d1;
5377 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005378 int ic; /* ignore case for strings */
5379{
Bram Moolenaar33570922005-01-25 22:26:29 +00005380 hashitem_T *hi;
5381 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005382 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005383
5384 if (dict_len(d1) != dict_len(d2))
5385 return FALSE;
5386
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 todo = d1->dv_hashtab.ht_used;
5388 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005390 if (!HASHITEM_EMPTY(hi))
5391 {
5392 item2 = dict_find(d2, hi->hi_key, -1);
5393 if (item2 == NULL)
5394 return FALSE;
5395 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5396 return FALSE;
5397 --todo;
5398 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005399 }
5400 return TRUE;
5401}
5402
5403/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005404 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005405 * Compares the items just like "==" would compare them, but strings and
5406 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005407 */
5408 static int
5409tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 typval_T *tv1;
5411 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005412 int ic; /* ignore case */
5413{
5414 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005415 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005416
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005417 if (tv1->v_type != tv2->v_type)
5418 return FALSE;
5419
5420 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005421 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005422 case VAR_LIST:
5423 /* recursive! */
5424 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5425
5426 case VAR_DICT:
5427 /* recursive! */
5428 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5429
5430 case VAR_FUNC:
5431 return (tv1->vval.v_string != NULL
5432 && tv2->vval.v_string != NULL
5433 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5434
5435 case VAR_NUMBER:
5436 return tv1->vval.v_number == tv2->vval.v_number;
5437
5438 case VAR_STRING:
5439 s1 = get_tv_string_buf(tv1, buf1);
5440 s2 = get_tv_string_buf(tv2, buf2);
5441 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005442 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005443
5444 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005445 return TRUE;
5446}
5447
5448/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 * Locate item with index "n" in list "l" and return it.
5450 * A negative index is counted from the end; -1 is the last item.
5451 * Returns NULL when "n" is out of range.
5452 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 long n;
5457{
Bram Moolenaar33570922005-01-25 22:26:29 +00005458 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 long idx;
5460
5461 if (l == NULL)
5462 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005463
5464 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005466 n = l->lv_len + n;
5467
5468 /* Check for index out of range. */
5469 if (n < 0 || n >= l->lv_len)
5470 return NULL;
5471
5472 /* When there is a cached index may start search from there. */
5473 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005475 if (n < l->lv_idx / 2)
5476 {
5477 /* closest to the start of the list */
5478 item = l->lv_first;
5479 idx = 0;
5480 }
5481 else if (n > (l->lv_idx + l->lv_len) / 2)
5482 {
5483 /* closest to the end of the list */
5484 item = l->lv_last;
5485 idx = l->lv_len - 1;
5486 }
5487 else
5488 {
5489 /* closest to the cached index */
5490 item = l->lv_idx_item;
5491 idx = l->lv_idx;
5492 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 }
5494 else
5495 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005496 if (n < l->lv_len / 2)
5497 {
5498 /* closest to the start of the list */
5499 item = l->lv_first;
5500 idx = 0;
5501 }
5502 else
5503 {
5504 /* closest to the end of the list */
5505 item = l->lv_last;
5506 idx = l->lv_len - 1;
5507 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005509
5510 while (n > idx)
5511 {
5512 /* search forward */
5513 item = item->li_next;
5514 ++idx;
5515 }
5516 while (n < idx)
5517 {
5518 /* search backward */
5519 item = item->li_prev;
5520 --idx;
5521 }
5522
5523 /* cache the used index */
5524 l->lv_idx = idx;
5525 l->lv_idx_item = item;
5526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 return item;
5528}
5529
5530/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005531 * Get list item "l[idx]" as a number.
5532 */
5533 static long
5534list_find_nr(l, idx, errorp)
5535 list_T *l;
5536 long idx;
5537 int *errorp; /* set to TRUE when something wrong */
5538{
5539 listitem_T *li;
5540
5541 li = list_find(l, idx);
5542 if (li == NULL)
5543 {
5544 if (errorp != NULL)
5545 *errorp = TRUE;
5546 return -1L;
5547 }
5548 return get_tv_number_chk(&li->li_tv, errorp);
5549}
5550
5551/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005552 * Locate "item" list "l" and return its index.
5553 * Returns -1 when "item" is not in the list.
5554 */
5555 static long
5556list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005557 list_T *l;
5558 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005559{
5560 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005561 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005562
5563 if (l == NULL)
5564 return -1;
5565 idx = 0;
5566 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5567 ++idx;
5568 if (li == NULL)
5569 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005570 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005571}
5572
5573/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 * Append item "item" to the end of list "l".
5575 */
5576 static void
5577list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005578 list_T *l;
5579 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580{
5581 if (l->lv_last == NULL)
5582 {
5583 /* empty list */
5584 l->lv_first = item;
5585 l->lv_last = item;
5586 item->li_prev = NULL;
5587 }
5588 else
5589 {
5590 l->lv_last->li_next = item;
5591 item->li_prev = l->lv_last;
5592 l->lv_last = item;
5593 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005594 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 item->li_next = NULL;
5596}
5597
5598/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005599 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005600 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 */
5602 static int
5603list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005604 list_T *l;
5605 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005607 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608
Bram Moolenaar05159a02005-02-26 23:04:13 +00005609 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005611 copy_tv(tv, &li->li_tv);
5612 list_append(l, li);
5613 return OK;
5614}
5615
5616/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005617 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005618 * Return FAIL when out of memory.
5619 */
5620 int
5621list_append_dict(list, dict)
5622 list_T *list;
5623 dict_T *dict;
5624{
5625 listitem_T *li = listitem_alloc();
5626
5627 if (li == NULL)
5628 return FAIL;
5629 li->li_tv.v_type = VAR_DICT;
5630 li->li_tv.v_lock = 0;
5631 li->li_tv.vval.v_dict = dict;
5632 list_append(list, li);
5633 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 return OK;
5635}
5636
5637/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005638 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005639 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005640 * Returns FAIL when out of memory.
5641 */
5642 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005643list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005644 list_T *l;
5645 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005646 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005647{
5648 listitem_T *li = listitem_alloc();
5649
5650 if (li == NULL)
5651 return FAIL;
5652 list_append(l, li);
5653 li->li_tv.v_type = VAR_STRING;
5654 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005655 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5656 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005657 return FAIL;
5658 return OK;
5659}
5660
5661/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005662 * Append "n" to list "l".
5663 * Returns FAIL when out of memory.
5664 */
5665 static int
5666list_append_number(l, n)
5667 list_T *l;
5668 varnumber_T n;
5669{
5670 listitem_T *li;
5671
5672 li = listitem_alloc();
5673 if (li == NULL)
5674 return FAIL;
5675 li->li_tv.v_type = VAR_NUMBER;
5676 li->li_tv.v_lock = 0;
5677 li->li_tv.vval.v_number = n;
5678 list_append(l, li);
5679 return OK;
5680}
5681
5682/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005683 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005684 * If "item" is NULL append at the end.
5685 * Return FAIL when out of memory.
5686 */
5687 static int
5688list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005689 list_T *l;
5690 typval_T *tv;
5691 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005692{
Bram Moolenaar33570922005-01-25 22:26:29 +00005693 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005694
5695 if (ni == NULL)
5696 return FAIL;
5697 copy_tv(tv, &ni->li_tv);
5698 if (item == NULL)
5699 /* Append new item at end of list. */
5700 list_append(l, ni);
5701 else
5702 {
5703 /* Insert new item before existing item. */
5704 ni->li_prev = item->li_prev;
5705 ni->li_next = item;
5706 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005707 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005708 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005709 ++l->lv_idx;
5710 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005711 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005712 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005713 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005714 l->lv_idx_item = NULL;
5715 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005716 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005717 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005718 }
5719 return OK;
5720}
5721
5722/*
5723 * Extend "l1" with "l2".
5724 * If "bef" is NULL append at the end, otherwise insert before this item.
5725 * Returns FAIL when out of memory.
5726 */
5727 static int
5728list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 list_T *l1;
5730 list_T *l2;
5731 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005732{
Bram Moolenaar33570922005-01-25 22:26:29 +00005733 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005734
5735 for (item = l2->lv_first; item != NULL; item = item->li_next)
5736 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5737 return FAIL;
5738 return OK;
5739}
5740
5741/*
5742 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5743 * Return FAIL when out of memory.
5744 */
5745 static int
5746list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005747 list_T *l1;
5748 list_T *l2;
5749 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005750{
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005752
5753 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005754 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005755 if (l == NULL)
5756 return FAIL;
5757 tv->v_type = VAR_LIST;
5758 tv->vval.v_list = l;
5759
5760 /* append all items from the second list */
5761 return list_extend(l, l2, NULL);
5762}
5763
5764/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005765 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005767 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 * Returns NULL when out of memory.
5769 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005770 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005771list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005774 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775{
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 list_T *copy;
5777 listitem_T *item;
5778 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779
5780 if (orig == NULL)
5781 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782
5783 copy = list_alloc();
5784 if (copy != NULL)
5785 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005786 if (copyID != 0)
5787 {
5788 /* Do this before adding the items, because one of the items may
5789 * refer back to this list. */
5790 orig->lv_copyID = copyID;
5791 orig->lv_copylist = copy;
5792 }
5793 for (item = orig->lv_first; item != NULL && !got_int;
5794 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 {
5796 ni = listitem_alloc();
5797 if (ni == NULL)
5798 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005799 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005800 {
5801 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5802 {
5803 vim_free(ni);
5804 break;
5805 }
5806 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005808 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809 list_append(copy, ni);
5810 }
5811 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005812 if (item != NULL)
5813 {
5814 list_unref(copy);
5815 copy = NULL;
5816 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 }
5818
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819 return copy;
5820}
5821
5822/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005823 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005824 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005826 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005827list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005828 list_T *l;
5829 listitem_T *item;
5830 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005831{
Bram Moolenaar33570922005-01-25 22:26:29 +00005832 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005834 /* notify watchers */
5835 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005837 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005838 list_fix_watch(l, ip);
5839 if (ip == item2)
5840 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005842
5843 if (item2->li_next == NULL)
5844 l->lv_last = item->li_prev;
5845 else
5846 item2->li_next->li_prev = item->li_prev;
5847 if (item->li_prev == NULL)
5848 l->lv_first = item2->li_next;
5849 else
5850 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005851 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852}
5853
5854/*
5855 * Return an allocated string with the string representation of a list.
5856 * May return NULL.
5857 */
5858 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005859list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005861 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862{
5863 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864
5865 if (tv->vval.v_list == NULL)
5866 return NULL;
5867 ga_init2(&ga, (int)sizeof(char), 80);
5868 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005869 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005870 {
5871 vim_free(ga.ga_data);
5872 return NULL;
5873 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874 ga_append(&ga, ']');
5875 ga_append(&ga, NUL);
5876 return (char_u *)ga.ga_data;
5877}
5878
5879/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 * Join list "l" into a string in "*gap", using separator "sep".
5881 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005882 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005884 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005885list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005887 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 char_u *sep;
5889 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005890 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891{
5892 int first = TRUE;
5893 char_u *tofree;
5894 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005895 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 char_u *s;
5897
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005898 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 {
5900 if (first)
5901 first = FALSE;
5902 else
5903 ga_concat(gap, sep);
5904
5905 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005906 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005908 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005909 if (s != NULL)
5910 ga_concat(gap, s);
5911 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005912 if (s == NULL)
5913 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005915 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005916}
5917
5918/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005919 * Garbage collection for lists and dictionaries.
5920 *
5921 * We use reference counts to be able to free most items right away when they
5922 * are no longer used. But for composite items it's possible that it becomes
5923 * unused while the reference count is > 0: When there is a recursive
5924 * reference. Example:
5925 * :let l = [1, 2, 3]
5926 * :let d = {9: l}
5927 * :let l[1] = d
5928 *
5929 * Since this is quite unusual we handle this with garbage collection: every
5930 * once in a while find out which lists and dicts are not referenced from any
5931 * variable.
5932 *
5933 * Here is a good reference text about garbage collection (refers to Python
5934 * but it applies to all reference-counting mechanisms):
5935 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005936 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005937
5938/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005939 * Do garbage collection for lists and dicts.
5940 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005941 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005942 int
5943garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005944{
5945 dict_T *dd;
5946 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005947 int copyID = ++current_copyID;
5948 buf_T *buf;
5949 win_T *wp;
5950 int i;
5951 funccall_T *fc;
5952 int did_free = FALSE;
5953
5954 /*
5955 * 1. Go through all accessible variables and mark all lists and dicts
5956 * with copyID.
5957 */
5958 /* script-local variables */
5959 for (i = 1; i <= ga_scripts.ga_len; ++i)
5960 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5961
5962 /* buffer-local variables */
5963 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5964 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5965
5966 /* window-local variables */
5967 FOR_ALL_WINDOWS(wp)
5968 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5969
5970 /* global variables */
5971 set_ref_in_ht(&globvarht, copyID);
5972
5973 /* function-local variables */
5974 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5975 {
5976 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5977 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5978 }
5979
5980 /*
5981 * 2. Go through the list of dicts and free items without the copyID.
5982 */
5983 for (dd = first_dict; dd != NULL; )
5984 if (dd->dv_copyID != copyID)
5985 {
5986 dict_free(dd);
5987 did_free = TRUE;
5988
5989 /* restart, next dict may also have been freed */
5990 dd = first_dict;
5991 }
5992 else
5993 dd = dd->dv_used_next;
5994
5995 /*
5996 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005997 * But don't free a list that has a watcher (used in a for loop), these
5998 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005999 */
6000 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006001 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006002 {
6003 list_free(ll);
6004 did_free = TRUE;
6005
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006006 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006007 ll = first_list;
6008 }
6009 else
6010 ll = ll->lv_used_next;
6011
6012 return did_free;
6013}
6014
6015/*
6016 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6017 */
6018 static void
6019set_ref_in_ht(ht, copyID)
6020 hashtab_T *ht;
6021 int copyID;
6022{
6023 int todo;
6024 hashitem_T *hi;
6025
6026 todo = ht->ht_used;
6027 for (hi = ht->ht_array; todo > 0; ++hi)
6028 if (!HASHITEM_EMPTY(hi))
6029 {
6030 --todo;
6031 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6032 }
6033}
6034
6035/*
6036 * Mark all lists and dicts referenced through list "l" with "copyID".
6037 */
6038 static void
6039set_ref_in_list(l, copyID)
6040 list_T *l;
6041 int copyID;
6042{
6043 listitem_T *li;
6044
6045 for (li = l->lv_first; li != NULL; li = li->li_next)
6046 set_ref_in_item(&li->li_tv, copyID);
6047}
6048
6049/*
6050 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6051 */
6052 static void
6053set_ref_in_item(tv, copyID)
6054 typval_T *tv;
6055 int copyID;
6056{
6057 dict_T *dd;
6058 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006059
6060 switch (tv->v_type)
6061 {
6062 case VAR_DICT:
6063 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006065 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006066 /* Didn't see this dict yet. */
6067 dd->dv_copyID = copyID;
6068 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006069 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006070 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006071
6072 case VAR_LIST:
6073 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006074 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006075 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006076 /* Didn't see this list yet. */
6077 ll->lv_copyID = copyID;
6078 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006079 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006080 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006081 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006082 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006083}
6084
6085/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006086 * Allocate an empty header for a dictionary.
6087 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006088 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006089dict_alloc()
6090{
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006092
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006094 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006095 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006096 /* Add the list to the hashtable for garbage collection. */
6097 if (first_dict != NULL)
6098 first_dict->dv_used_prev = d;
6099 d->dv_used_next = first_dict;
6100 d->dv_used_prev = NULL;
6101
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006103 d->dv_lock = 0;
6104 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006105 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006106 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006107 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006108}
6109
6110/*
6111 * Unreference a Dictionary: decrement the reference count and free it when it
6112 * becomes zero.
6113 */
6114 static void
6115dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006116 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006117{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006118 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6119 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006120}
6121
6122/*
6123 * Free a Dictionary, including all items it contains.
6124 * Ignores the reference count.
6125 */
6126 static void
6127dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006129{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006130 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006132 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006133
Bram Moolenaard9fba312005-06-26 22:34:35 +00006134 /* Avoid that recursive reference to the dict frees us again. */
6135 d->dv_refcount = DEL_REFCOUNT;
6136
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006137 /* Remove the dict from the list of dicts for garbage collection. */
6138 if (d->dv_used_prev == NULL)
6139 first_dict = d->dv_used_next;
6140 else
6141 d->dv_used_prev->dv_used_next = d->dv_used_next;
6142 if (d->dv_used_next != NULL)
6143 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6144
6145 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006146 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 todo = d->dv_hashtab.ht_used;
6148 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006149 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006150 if (!HASHITEM_EMPTY(hi))
6151 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006152 /* Remove the item before deleting it, just in case there is
6153 * something recursive causing trouble. */
6154 di = HI2DI(hi);
6155 hash_remove(&d->dv_hashtab, hi);
6156 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157 --todo;
6158 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006159 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006161 vim_free(d);
6162}
6163
6164/*
6165 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 * The "key" is copied to the new item.
6167 * Note that the value of the item "di_tv" still needs to be initialized!
6168 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006169 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006171dictitem_alloc(key)
6172 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173{
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006175
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006177 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006179 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006180 di->di_flags = 0;
6181 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006182 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006183}
6184
6185/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 * Make a copy of a Dictionary item.
6187 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006189dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006190 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191{
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006193
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006195 if (di != NULL)
6196 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006197 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006198 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006199 copy_tv(&org->di_tv, &di->di_tv);
6200 }
6201 return di;
6202}
6203
6204/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006205 * Remove item "item" from Dictionary "dict" and free it.
6206 */
6207 static void
6208dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006209 dict_T *dict;
6210 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211{
Bram Moolenaar33570922005-01-25 22:26:29 +00006212 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006213
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215 if (HASHITEM_EMPTY(hi))
6216 EMSG2(_(e_intern2), "dictitem_remove()");
6217 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006218 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006219 dictitem_free(item);
6220}
6221
6222/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006223 * Free a dict item. Also clears the value.
6224 */
6225 static void
6226dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006227 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006228{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006229 clear_tv(&item->di_tv);
6230 vim_free(item);
6231}
6232
6233/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6235 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006236 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006237 * Returns NULL when out of memory.
6238 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006240dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006242 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006243 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006244{
Bram Moolenaar33570922005-01-25 22:26:29 +00006245 dict_T *copy;
6246 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006247 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006249
6250 if (orig == NULL)
6251 return NULL;
6252
6253 copy = dict_alloc();
6254 if (copy != NULL)
6255 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006256 if (copyID != 0)
6257 {
6258 orig->dv_copyID = copyID;
6259 orig->dv_copydict = copy;
6260 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006262 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006263 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006264 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006265 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006266 --todo;
6267
6268 di = dictitem_alloc(hi->hi_key);
6269 if (di == NULL)
6270 break;
6271 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006272 {
6273 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6274 copyID) == FAIL)
6275 {
6276 vim_free(di);
6277 break;
6278 }
6279 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006280 else
6281 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6282 if (dict_add(copy, di) == FAIL)
6283 {
6284 dictitem_free(di);
6285 break;
6286 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006289
Bram Moolenaare9a41262005-01-15 22:18:47 +00006290 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006291 if (todo > 0)
6292 {
6293 dict_unref(copy);
6294 copy = NULL;
6295 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006296 }
6297
6298 return copy;
6299}
6300
6301/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006302 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006303 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006304 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006305 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006306dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006307 dict_T *d;
6308 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309{
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006311}
6312
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006314 * Add a number or string entry to dictionary "d".
6315 * When "str" is NULL use number "nr", otherwise use "str".
6316 * Returns FAIL when out of memory and when key already exists.
6317 */
6318 int
6319dict_add_nr_str(d, key, nr, str)
6320 dict_T *d;
6321 char *key;
6322 long nr;
6323 char_u *str;
6324{
6325 dictitem_T *item;
6326
6327 item = dictitem_alloc((char_u *)key);
6328 if (item == NULL)
6329 return FAIL;
6330 item->di_tv.v_lock = 0;
6331 if (str == NULL)
6332 {
6333 item->di_tv.v_type = VAR_NUMBER;
6334 item->di_tv.vval.v_number = nr;
6335 }
6336 else
6337 {
6338 item->di_tv.v_type = VAR_STRING;
6339 item->di_tv.vval.v_string = vim_strsave(str);
6340 }
6341 if (dict_add(d, item) == FAIL)
6342 {
6343 dictitem_free(item);
6344 return FAIL;
6345 }
6346 return OK;
6347}
6348
6349/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350 * Get the number of items in a Dictionary.
6351 */
6352 static long
6353dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006355{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006356 if (d == NULL)
6357 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006359}
6360
6361/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006362 * Find item "key[len]" in Dictionary "d".
6363 * If "len" is negative use strlen(key).
6364 * Returns NULL when not found.
6365 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006369 char_u *key;
6370 int len;
6371{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372#define AKEYLEN 200
6373 char_u buf[AKEYLEN];
6374 char_u *akey;
6375 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006378 if (len < 0)
6379 akey = key;
6380 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006382 tofree = akey = vim_strnsave(key, len);
6383 if (akey == NULL)
6384 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006385 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006386 else
6387 {
6388 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006389 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006390 akey = buf;
6391 }
6392
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006394 vim_free(tofree);
6395 if (HASHITEM_EMPTY(hi))
6396 return NULL;
6397 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398}
6399
6400/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006401 * Get a string item from a dictionary.
6402 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006403 * Returns NULL if the entry doesn't exist or out of memory.
6404 */
6405 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006406get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006407 dict_T *d;
6408 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006409 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006410{
6411 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006412 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006413
6414 di = dict_find(d, key, -1);
6415 if (di == NULL)
6416 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006417 s = get_tv_string(&di->di_tv);
6418 if (save && s != NULL)
6419 s = vim_strsave(s);
6420 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006421}
6422
6423/*
6424 * Get a number item from a dictionary.
6425 * Returns 0 if the entry doesn't exist or out of memory.
6426 */
6427 long
6428get_dict_number(d, key)
6429 dict_T *d;
6430 char_u *key;
6431{
6432 dictitem_T *di;
6433
6434 di = dict_find(d, key, -1);
6435 if (di == NULL)
6436 return 0;
6437 return get_tv_number(&di->di_tv);
6438}
6439
6440/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006441 * Return an allocated string with the string representation of a Dictionary.
6442 * May return NULL.
6443 */
6444 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006445dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006447 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006448{
6449 garray_T ga;
6450 int first = TRUE;
6451 char_u *tofree;
6452 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006453 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006454 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006455 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006456 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006457
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006458 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006459 return NULL;
6460 ga_init2(&ga, (int)sizeof(char), 80);
6461 ga_append(&ga, '{');
6462
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006464 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006465 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006466 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006467 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006468 --todo;
6469
6470 if (first)
6471 first = FALSE;
6472 else
6473 ga_concat(&ga, (char_u *)", ");
6474
6475 tofree = string_quote(hi->hi_key, FALSE);
6476 if (tofree != NULL)
6477 {
6478 ga_concat(&ga, tofree);
6479 vim_free(tofree);
6480 }
6481 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006482 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006483 if (s != NULL)
6484 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006485 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 if (s == NULL)
6487 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006488 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006489 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006490 if (todo > 0)
6491 {
6492 vim_free(ga.ga_data);
6493 return NULL;
6494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006495
6496 ga_append(&ga, '}');
6497 ga_append(&ga, NUL);
6498 return (char_u *)ga.ga_data;
6499}
6500
6501/*
6502 * Allocate a variable for a Dictionary and fill it from "*arg".
6503 * Return OK or FAIL. Returns NOTDONE for {expr}.
6504 */
6505 static int
6506get_dict_tv(arg, rettv, evaluate)
6507 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006509 int evaluate;
6510{
Bram Moolenaar33570922005-01-25 22:26:29 +00006511 dict_T *d = NULL;
6512 typval_T tvkey;
6513 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006514 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006516 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006517 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006518
6519 /*
6520 * First check if it's not a curly-braces thing: {expr}.
6521 * Must do this without evaluating, otherwise a function may be called
6522 * twice. Unfortunately this means we need to call eval1() twice for the
6523 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006525 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 if (*start != '}')
6527 {
6528 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6529 return FAIL;
6530 if (*start == '}')
6531 return NOTDONE;
6532 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006533
6534 if (evaluate)
6535 {
6536 d = dict_alloc();
6537 if (d == NULL)
6538 return FAIL;
6539 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006540 tvkey.v_type = VAR_UNKNOWN;
6541 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006542
6543 *arg = skipwhite(*arg + 1);
6544 while (**arg != '}' && **arg != NUL)
6545 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006546 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006547 goto failret;
6548 if (**arg != ':')
6549 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006550 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006551 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006552 goto failret;
6553 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006554 key = get_tv_string_buf_chk(&tvkey, buf);
6555 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006557 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6558 if (key != NULL)
6559 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006560 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006561 goto failret;
6562 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563
6564 *arg = skipwhite(*arg + 1);
6565 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006567 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006568 goto failret;
6569 }
6570 if (evaluate)
6571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006572 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006573 if (item != NULL)
6574 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006575 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006576 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006577 clear_tv(&tv);
6578 goto failret;
6579 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006580 item = dictitem_alloc(key);
6581 clear_tv(&tvkey);
6582 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006583 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006584 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006585 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006586 if (dict_add(d, item) == FAIL)
6587 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006588 }
6589 }
6590
6591 if (**arg == '}')
6592 break;
6593 if (**arg != ',')
6594 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006595 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006596 goto failret;
6597 }
6598 *arg = skipwhite(*arg + 1);
6599 }
6600
6601 if (**arg != '}')
6602 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006603 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006604failret:
6605 if (evaluate)
6606 dict_free(d);
6607 return FAIL;
6608 }
6609
6610 *arg = skipwhite(*arg + 1);
6611 if (evaluate)
6612 {
6613 rettv->v_type = VAR_DICT;
6614 rettv->vval.v_dict = d;
6615 ++d->dv_refcount;
6616 }
6617
6618 return OK;
6619}
6620
Bram Moolenaar8c711452005-01-14 21:53:12 +00006621/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 * Return a string with the string representation of a variable.
6623 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006625 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 * May return NULL;
6628 */
6629 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006630echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006631 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006634 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006636 static int recurse = 0;
6637 char_u *r = NULL;
6638
Bram Moolenaar33570922005-01-25 22:26:29 +00006639 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006640 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006641 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642 *tofree = NULL;
6643 return NULL;
6644 }
6645 ++recurse;
6646
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 switch (tv->v_type)
6648 {
6649 case VAR_FUNC:
6650 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006651 r = tv->vval.v_string;
6652 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006653
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006655 if (tv->vval.v_list == NULL)
6656 {
6657 *tofree = NULL;
6658 r = NULL;
6659 }
6660 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6661 {
6662 *tofree = NULL;
6663 r = (char_u *)"[...]";
6664 }
6665 else
6666 {
6667 tv->vval.v_list->lv_copyID = copyID;
6668 *tofree = list2string(tv, copyID);
6669 r = *tofree;
6670 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006671 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006672
Bram Moolenaar8c711452005-01-14 21:53:12 +00006673 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006674 if (tv->vval.v_dict == NULL)
6675 {
6676 *tofree = NULL;
6677 r = NULL;
6678 }
6679 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6680 {
6681 *tofree = NULL;
6682 r = (char_u *)"{...}";
6683 }
6684 else
6685 {
6686 tv->vval.v_dict->dv_copyID = copyID;
6687 *tofree = dict2string(tv, copyID);
6688 r = *tofree;
6689 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006690 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006691
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006692 case VAR_STRING:
6693 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 *tofree = NULL;
6695 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006696 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006697
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006698 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006699 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006700 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006702
6703 --recurse;
6704 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006705}
6706
6707/*
6708 * Return a string with the string representation of a variable.
6709 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6710 * "numbuf" is used for a number.
6711 * Puts quotes around strings, so that they can be parsed back by eval().
6712 * May return NULL;
6713 */
6714 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006715tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006716 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717 char_u **tofree;
6718 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006719 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006720{
6721 switch (tv->v_type)
6722 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 case VAR_FUNC:
6724 *tofree = string_quote(tv->vval.v_string, TRUE);
6725 return *tofree;
6726 case VAR_STRING:
6727 *tofree = string_quote(tv->vval.v_string, FALSE);
6728 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006729 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006731 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006732 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006734 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006736 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006737}
6738
6739/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006740 * Return string "str" in ' quotes, doubling ' characters.
6741 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006742 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 */
6744 static char_u *
6745string_quote(str, function)
6746 char_u *str;
6747 int function;
6748{
Bram Moolenaar33570922005-01-25 22:26:29 +00006749 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006750 char_u *p, *r, *s;
6751
Bram Moolenaar33570922005-01-25 22:26:29 +00006752 len = (function ? 13 : 3);
6753 if (str != NULL)
6754 {
6755 len += STRLEN(str);
6756 for (p = str; *p != NUL; mb_ptr_adv(p))
6757 if (*p == '\'')
6758 ++len;
6759 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006760 s = r = alloc(len);
6761 if (r != NULL)
6762 {
6763 if (function)
6764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766 r += 10;
6767 }
6768 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006769 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006770 if (str != NULL)
6771 for (p = str; *p != NUL; )
6772 {
6773 if (*p == '\'')
6774 *r++ = '\'';
6775 MB_COPY_CHAR(p, r);
6776 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006777 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778 if (function)
6779 *r++ = ')';
6780 *r++ = NUL;
6781 }
6782 return s;
6783}
6784
6785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 * Get the value of an environment variable.
6787 * "arg" is pointing to the '$'. It is advanced to after the name.
6788 * If the environment variable was not set, silently assume it is empty.
6789 * Always return OK.
6790 */
6791 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006792get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 int evaluate;
6796{
6797 char_u *string = NULL;
6798 int len;
6799 int cc;
6800 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006801 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
6803 ++*arg;
6804 name = *arg;
6805 len = get_env_len(arg);
6806 if (evaluate)
6807 {
6808 if (len != 0)
6809 {
6810 cc = name[len];
6811 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006812 /* first try vim_getenv(), fast for normal environment vars */
6813 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006815 {
6816 if (!mustfree)
6817 string = vim_strsave(string);
6818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 else
6820 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006821 if (mustfree)
6822 vim_free(string);
6823
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 /* next try expanding things like $VIM and ${HOME} */
6825 string = expand_env_save(name - 1);
6826 if (string != NULL && *string == '$')
6827 {
6828 vim_free(string);
6829 string = NULL;
6830 }
6831 }
6832 name[len] = cc;
6833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006834 rettv->v_type = VAR_STRING;
6835 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837
6838 return OK;
6839}
6840
6841/*
6842 * Array with names and number of arguments of all internal functions
6843 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6844 */
6845static struct fst
6846{
6847 char *f_name; /* function name */
6848 char f_min_argc; /* minimal number of arguments */
6849 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006850 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006851 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852} functions[] =
6853{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006854 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {"append", 2, 2, f_append},
6856 {"argc", 0, 0, f_argc},
6857 {"argidx", 0, 0, f_argidx},
6858 {"argv", 1, 1, f_argv},
6859 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006860 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 {"bufexists", 1, 1, f_bufexists},
6862 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6863 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6864 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6865 {"buflisted", 1, 1, f_buflisted},
6866 {"bufloaded", 1, 1, f_bufloaded},
6867 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006868 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {"bufwinnr", 1, 1, f_bufwinnr},
6870 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006871 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {"char2nr", 1, 1, f_char2nr},
6874 {"cindent", 1, 1, f_cindent},
6875 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006876#if defined(FEAT_INS_EXPAND)
6877 {"complete_add", 1, 1, f_complete_add},
6878 {"complete_check", 0, 0, f_complete_check},
6879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006881 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006882 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006884 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006885 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {"delete", 1, 1, f_delete},
6887 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006888 {"diff_filler", 1, 1, f_diff_filler},
6889 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006890 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006892 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {"eventhandler", 0, 0, f_eventhandler},
6894 {"executable", 1, 1, f_executable},
6895 {"exists", 1, 1, f_exists},
6896 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006897 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6899 {"filereadable", 1, 1, f_filereadable},
6900 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006902 {"finddir", 1, 3, f_finddir},
6903 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {"fnamemodify", 2, 2, f_fnamemodify},
6905 {"foldclosed", 1, 1, f_foldclosed},
6906 {"foldclosedend", 1, 1, f_foldclosedend},
6907 {"foldlevel", 1, 1, f_foldlevel},
6908 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006909 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006911 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006912 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006913 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006914 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {"getbufvar", 2, 2, f_getbufvar},
6916 {"getchar", 0, 1, f_getchar},
6917 {"getcharmod", 0, 0, f_getcharmod},
6918 {"getcmdline", 0, 0, f_getcmdline},
6919 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006920 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006922 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006923 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {"getfsize", 1, 1, f_getfsize},
6925 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006926 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006927 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006928 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006929 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006930 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006931 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {"getregtype", 0, 1, f_getregtype},
6933 {"getwinposx", 0, 0, f_getwinposx},
6934 {"getwinposy", 0, 0, f_getwinposy},
6935 {"getwinvar", 2, 2, f_getwinvar},
6936 {"glob", 1, 1, f_glob},
6937 {"globpath", 2, 2, f_globpath},
6938 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006939 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {"hasmapto", 1, 2, f_hasmapto},
6941 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6942 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6943 {"histadd", 2, 2, f_histadd},
6944 {"histdel", 1, 2, f_histdel},
6945 {"histget", 1, 2, f_histget},
6946 {"histnr", 1, 1, f_histnr},
6947 {"hlID", 1, 1, f_hlID},
6948 {"hlexists", 1, 1, f_hlexists},
6949 {"hostname", 0, 0, f_hostname},
6950 {"iconv", 3, 3, f_iconv},
6951 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006952 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006953 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006955 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {"inputrestore", 0, 0, f_inputrestore},
6957 {"inputsave", 0, 0, f_inputsave},
6958 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006959 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006961 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006962 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006963 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006964 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006966 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {"libcall", 3, 3, f_libcall},
6968 {"libcallnr", 3, 3, f_libcallnr},
6969 {"line", 1, 1, f_line},
6970 {"line2byte", 1, 1, f_line2byte},
6971 {"lispindent", 1, 1, f_lispindent},
6972 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006973 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 {"maparg", 1, 2, f_maparg},
6975 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006976 {"match", 2, 4, f_match},
6977 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006978 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006979 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006980 {"max", 1, 1, f_max},
6981 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006982#ifdef vim_mkdir
6983 {"mkdir", 1, 3, f_mkdir},
6984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 {"mode", 0, 0, f_mode},
6986 {"nextnonblank", 1, 1, f_nextnonblank},
6987 {"nr2char", 1, 1, f_nr2char},
6988 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006989 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006990 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006991 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006992 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 {"remote_expr", 2, 3, f_remote_expr},
6994 {"remote_foreground", 1, 1, f_remote_foreground},
6995 {"remote_peek", 1, 2, f_remote_peek},
6996 {"remote_read", 1, 1, f_remote_read},
6997 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006998 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007000 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007002 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007003 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007004 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007005 {"searchpair", 3, 6, f_searchpair},
7006 {"searchpairpos", 3, 6, f_searchpairpos},
7007 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {"server2client", 2, 2, f_server2client},
7009 {"serverlist", 0, 0, f_serverlist},
7010 {"setbufvar", 3, 3, f_setbufvar},
7011 {"setcmdpos", 1, 1, f_setcmdpos},
7012 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007013 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007014 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007015 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {"setreg", 2, 3, f_setreg},
7017 {"setwinvar", 3, 3, f_setwinvar},
7018 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007019 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007020 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007021 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007022 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007023 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024#ifdef HAVE_STRFTIME
7025 {"strftime", 1, 2, f_strftime},
7026#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007027 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007028 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {"strlen", 1, 1, f_strlen},
7030 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007031 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {"strtrans", 1, 1, f_strtrans},
7033 {"submatch", 1, 1, f_submatch},
7034 {"substitute", 4, 4, f_substitute},
7035 {"synID", 3, 3, f_synID},
7036 {"synIDattr", 2, 3, f_synIDattr},
7037 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007038 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007039 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007040 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007041 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007042 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007043 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007045 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {"tolower", 1, 1, f_tolower},
7047 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007048 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {"virtcol", 1, 1, f_virtcol},
7052 {"visualmode", 0, 1, f_visualmode},
7053 {"winbufnr", 1, 1, f_winbufnr},
7054 {"wincol", 0, 0, f_wincol},
7055 {"winheight", 1, 1, f_winheight},
7056 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007057 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007059 {"winrestview", 1, 1, f_winrestview},
7060 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007062 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063};
7064
7065#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7066
7067/*
7068 * Function given to ExpandGeneric() to obtain the list of internal
7069 * or user defined function names.
7070 */
7071 char_u *
7072get_function_name(xp, idx)
7073 expand_T *xp;
7074 int idx;
7075{
7076 static int intidx = -1;
7077 char_u *name;
7078
7079 if (idx == 0)
7080 intidx = -1;
7081 if (intidx < 0)
7082 {
7083 name = get_user_func_name(xp, idx);
7084 if (name != NULL)
7085 return name;
7086 }
7087 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7088 {
7089 STRCPY(IObuff, functions[intidx].f_name);
7090 STRCAT(IObuff, "(");
7091 if (functions[intidx].f_max_argc == 0)
7092 STRCAT(IObuff, ")");
7093 return IObuff;
7094 }
7095
7096 return NULL;
7097}
7098
7099/*
7100 * Function given to ExpandGeneric() to obtain the list of internal or
7101 * user defined variable or function names.
7102 */
7103/*ARGSUSED*/
7104 char_u *
7105get_expr_name(xp, idx)
7106 expand_T *xp;
7107 int idx;
7108{
7109 static int intidx = -1;
7110 char_u *name;
7111
7112 if (idx == 0)
7113 intidx = -1;
7114 if (intidx < 0)
7115 {
7116 name = get_function_name(xp, idx);
7117 if (name != NULL)
7118 return name;
7119 }
7120 return get_user_var_name(xp, ++intidx);
7121}
7122
7123#endif /* FEAT_CMDL_COMPL */
7124
7125/*
7126 * Find internal function in table above.
7127 * Return index, or -1 if not found
7128 */
7129 static int
7130find_internal_func(name)
7131 char_u *name; /* name of the function */
7132{
7133 int first = 0;
7134 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7135 int cmp;
7136 int x;
7137
7138 /*
7139 * Find the function name in the table. Binary search.
7140 */
7141 while (first <= last)
7142 {
7143 x = first + ((unsigned)(last - first) >> 1);
7144 cmp = STRCMP(name, functions[x].f_name);
7145 if (cmp < 0)
7146 last = x - 1;
7147 else if (cmp > 0)
7148 first = x + 1;
7149 else
7150 return x;
7151 }
7152 return -1;
7153}
7154
7155/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007156 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7157 * name it contains, otherwise return "name".
7158 */
7159 static char_u *
7160deref_func_name(name, lenp)
7161 char_u *name;
7162 int *lenp;
7163{
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007165 int cc;
7166
7167 cc = name[*lenp];
7168 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007169 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007170 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007172 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007174 {
7175 *lenp = 0;
7176 return (char_u *)""; /* just in case */
7177 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 *lenp = STRLEN(v->di_tv.vval.v_string);
7179 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007180 }
7181
7182 return name;
7183}
7184
7185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 * Allocate a variable for the result of a function.
7187 * Return OK or FAIL.
7188 */
7189 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7191 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 char_u *name; /* name of the function */
7193 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 char_u **arg; /* argument, pointing to the '(' */
7196 linenr_T firstline; /* first line of range */
7197 linenr_T lastline; /* last line of range */
7198 int *doesrange; /* return: function handled range */
7199 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
7202 char_u *argp;
7203 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 int argcount = 0; /* number of arguments found */
7206
7207 /*
7208 * Get the arguments.
7209 */
7210 argp = *arg;
7211 while (argcount < MAX_FUNC_ARGS)
7212 {
7213 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7214 if (*argp == ')' || *argp == ',' || *argp == NUL)
7215 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7217 {
7218 ret = FAIL;
7219 break;
7220 }
7221 ++argcount;
7222 if (*argp != ',')
7223 break;
7224 }
7225 if (*argp == ')')
7226 ++argp;
7227 else
7228 ret = FAIL;
7229
7230 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007231 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 {
7235 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007236 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007238 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
7241 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007242 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
7244 *arg = skipwhite(argp);
7245 return ret;
7246}
7247
7248
7249/*
7250 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007251 * Return OK when the function can't be called, FAIL otherwise.
7252 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 */
7254 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007255call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007256 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 char_u *name; /* name of the function */
7258 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007261 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 linenr_T firstline; /* first line of range */
7263 linenr_T lastline; /* last line of range */
7264 int *doesrange; /* return: function handled range */
7265 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267{
7268 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269#define ERROR_UNKNOWN 0
7270#define ERROR_TOOMANY 1
7271#define ERROR_TOOFEW 2
7272#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273#define ERROR_DICT 4
7274#define ERROR_NONE 5
7275#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 int error = ERROR_NONE;
7277 int i;
7278 int llen;
7279 ufunc_T *fp;
7280 int cc;
7281#define FLEN_FIXED 40
7282 char_u fname_buf[FLEN_FIXED + 1];
7283 char_u *fname;
7284
7285 /*
7286 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7287 * Change <SNR>123_name() to K_SNR 123_name().
7288 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7289 */
7290 cc = name[len];
7291 name[len] = NUL;
7292 llen = eval_fname_script(name);
7293 if (llen > 0)
7294 {
7295 fname_buf[0] = K_SPECIAL;
7296 fname_buf[1] = KS_EXTRA;
7297 fname_buf[2] = (int)KE_SNR;
7298 i = 3;
7299 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7300 {
7301 if (current_SID <= 0)
7302 error = ERROR_SCRIPT;
7303 else
7304 {
7305 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7306 i = (int)STRLEN(fname_buf);
7307 }
7308 }
7309 if (i + STRLEN(name + llen) < FLEN_FIXED)
7310 {
7311 STRCPY(fname_buf + i, name + llen);
7312 fname = fname_buf;
7313 }
7314 else
7315 {
7316 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7317 if (fname == NULL)
7318 error = ERROR_OTHER;
7319 else
7320 {
7321 mch_memmove(fname, fname_buf, (size_t)i);
7322 STRCPY(fname + i, name + llen);
7323 }
7324 }
7325 }
7326 else
7327 fname = name;
7328
7329 *doesrange = FALSE;
7330
7331
7332 /* execute the function if no errors detected and executing */
7333 if (evaluate && error == ERROR_NONE)
7334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007335 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 error = ERROR_UNKNOWN;
7337
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007338 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 {
7340 /*
7341 * User defined function.
7342 */
7343 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007344
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007346 /* Trigger FuncUndefined event, may load the function. */
7347 if (fp == NULL
7348 && apply_autocmds(EVENT_FUNCUNDEFINED,
7349 fname, fname, TRUE, NULL)
7350 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007352 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 fp = find_func(fname);
7354 }
7355#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007356 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007357 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007358 {
7359 /* loaded a package, search for the function again */
7360 fp = find_func(fname);
7361 }
7362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if (fp != NULL)
7364 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007365 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007367 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007369 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007371 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 else
7374 {
7375 /*
7376 * Call the user function.
7377 * Save and restore search patterns, script variables and
7378 * redo buffer.
7379 */
7380 save_search_patterns();
7381 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007382 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007383 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007385 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7386 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7387 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007388 /* Function was unreferenced while being used, free it
7389 * now. */
7390 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 restoreRedobuff();
7392 restore_search_patterns();
7393 error = ERROR_NONE;
7394 }
7395 }
7396 }
7397 else
7398 {
7399 /*
7400 * Find the function name in the table, call its implementation.
7401 */
7402 i = find_internal_func(fname);
7403 if (i >= 0)
7404 {
7405 if (argcount < functions[i].f_min_argc)
7406 error = ERROR_TOOFEW;
7407 else if (argcount > functions[i].f_max_argc)
7408 error = ERROR_TOOMANY;
7409 else
7410 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007411 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007412 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 error = ERROR_NONE;
7414 }
7415 }
7416 }
7417 /*
7418 * The function call (or "FuncUndefined" autocommand sequence) might
7419 * have been aborted by an error, an interrupt, or an explicitly thrown
7420 * exception that has not been caught so far. This situation can be
7421 * tested for by calling aborting(). For an error in an internal
7422 * function or for the "E132" error in call_user_func(), however, the
7423 * throw point at which the "force_abort" flag (temporarily reset by
7424 * emsg()) is normally updated has not been reached yet. We need to
7425 * update that flag first to make aborting() reliable.
7426 */
7427 update_force_abort();
7428 }
7429 if (error == ERROR_NONE)
7430 ret = OK;
7431
7432 /*
7433 * Report an error unless the argument evaluation or function call has been
7434 * cancelled due to an aborting error, an interrupt, or an exception.
7435 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436 if (!aborting())
7437 {
7438 switch (error)
7439 {
7440 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007441 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 break;
7443 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007444 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445 break;
7446 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007447 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 name);
7449 break;
7450 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007451 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 name);
7453 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007454 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007455 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007456 name);
7457 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 }
7459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
7461 name[len] = cc;
7462 if (fname != name && fname != fname_buf)
7463 vim_free(fname);
7464
7465 return ret;
7466}
7467
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007468/*
7469 * Give an error message with a function name. Handle <SNR> things.
7470 */
7471 static void
7472emsg_funcname(msg, name)
7473 char *msg;
7474 char_u *name;
7475{
7476 char_u *p;
7477
7478 if (*name == K_SPECIAL)
7479 p = concat_str((char_u *)"<SNR>", name + 3);
7480 else
7481 p = name;
7482 EMSG2(_(msg), p);
7483 if (p != name)
7484 vim_free(p);
7485}
7486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487/*********************************************
7488 * Implementation of the built-in functions
7489 */
7490
7491/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007492 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 */
7494 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007495f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 typval_T *argvars;
7497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007501 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007502 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007504 if ((l = argvars[0].vval.v_list) != NULL
7505 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7506 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007508 }
7509 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007510 EMSG(_(e_listreq));
7511}
7512
7513/*
7514 * "append(lnum, string/list)" function
7515 */
7516 static void
7517f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 typval_T *argvars;
7519 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007520{
7521 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007522 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 list_T *l = NULL;
7524 listitem_T *li = NULL;
7525 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007526 long added = 0;
7527
Bram Moolenaar0d660222005-01-07 21:51:51 +00007528 lnum = get_tv_lnum(argvars);
7529 if (lnum >= 0
7530 && lnum <= curbuf->b_ml.ml_line_count
7531 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007532 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007533 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007534 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007535 l = argvars[1].vval.v_list;
7536 if (l == NULL)
7537 return;
7538 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007540 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007541 for (;;)
7542 {
7543 if (l == NULL)
7544 tv = &argvars[1]; /* append a string */
7545 else if (li == NULL)
7546 break; /* end of list */
7547 else
7548 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007549 line = get_tv_string_chk(tv);
7550 if (line == NULL) /* type error */
7551 {
7552 rettv->vval.v_number = 1; /* Failed */
7553 break;
7554 }
7555 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007556 ++added;
7557 if (l == NULL)
7558 break;
7559 li = li->li_next;
7560 }
7561
7562 appended_lines_mark(lnum, added);
7563 if (curwin->w_cursor.lnum > lnum)
7564 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007566 else
7567 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568}
7569
7570/*
7571 * "argc()" function
7572 */
7573/* ARGSUSED */
7574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007575f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 typval_T *argvars;
7577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007579 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580}
7581
7582/*
7583 * "argidx()" function
7584 */
7585/* ARGSUSED */
7586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007587f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 typval_T *argvars;
7589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007591 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592}
7593
7594/*
7595 * "argv(nr)" function
7596 */
7597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007599 typval_T *argvars;
7600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601{
7602 int idx;
7603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007604 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007606 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608 rettv->vval.v_string = NULL;
7609 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610}
7611
7612/*
7613 * "browse(save, title, initdir, default)" function
7614 */
7615/* ARGSUSED */
7616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 typval_T *argvars;
7619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
7621#ifdef FEAT_BROWSE
7622 int save;
7623 char_u *title;
7624 char_u *initdir;
7625 char_u *defname;
7626 char_u buf[NUMBUFLEN];
7627 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007628 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007630 save = get_tv_number_chk(&argvars[0], &error);
7631 title = get_tv_string_chk(&argvars[1]);
7632 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7633 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007635 if (error || title == NULL || initdir == NULL || defname == NULL)
7636 rettv->vval.v_string = NULL;
7637 else
7638 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007639 do_browse(save ? BROWSE_SAVE : 0,
7640 title, defname, NULL, initdir, NULL, curbuf);
7641#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007642 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007643#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007645}
7646
7647/*
7648 * "browsedir(title, initdir)" function
7649 */
7650/* ARGSUSED */
7651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007653 typval_T *argvars;
7654 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007655{
7656#ifdef FEAT_BROWSE
7657 char_u *title;
7658 char_u *initdir;
7659 char_u buf[NUMBUFLEN];
7660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007661 title = get_tv_string_chk(&argvars[0]);
7662 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007664 if (title == NULL || initdir == NULL)
7665 rettv->vval.v_string = NULL;
7666 else
7667 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007668 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007672 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673}
7674
Bram Moolenaar33570922005-01-25 22:26:29 +00007675static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007676
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677/*
7678 * Find a buffer by number or exact name.
7679 */
7680 static buf_T *
7681find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686 if (avar->v_type == VAR_NUMBER)
7687 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007688 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007691 if (buf == NULL)
7692 {
7693 /* No full path name match, try a match with a URL or a "nofile"
7694 * buffer, these don't use the full path. */
7695 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7696 if (buf->b_fname != NULL
7697 && (path_with_url(buf->b_fname)
7698#ifdef FEAT_QUICKFIX
7699 || bt_nofile(buf)
7700#endif
7701 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007702 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007703 break;
7704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
7706 return buf;
7707}
7708
7709/*
7710 * "bufexists(expr)" function
7711 */
7712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 typval_T *argvars;
7715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718}
7719
7720/*
7721 * "buflisted(expr)" function
7722 */
7723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 typval_T *argvars;
7726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 buf_T *buf;
7729
7730 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732}
7733
7734/*
7735 * "bufloaded(expr)" function
7736 */
7737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007738f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 typval_T *argvars;
7740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
7742 buf_T *buf;
7743
7744 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007745 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746}
7747
Bram Moolenaar33570922005-01-25 22:26:29 +00007748static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007749
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750/*
7751 * Get buffer by number or pattern.
7752 */
7753 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007755 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 int save_magic;
7759 char_u *save_cpo;
7760 buf_T *buf;
7761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762 if (tv->v_type == VAR_NUMBER)
7763 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007764 if (tv->v_type != VAR_STRING)
7765 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 if (name == NULL || *name == NUL)
7767 return curbuf;
7768 if (name[0] == '$' && name[1] == NUL)
7769 return lastbuf;
7770
7771 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7772 save_magic = p_magic;
7773 p_magic = TRUE;
7774 save_cpo = p_cpo;
7775 p_cpo = (char_u *)"";
7776
7777 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7778 TRUE, FALSE));
7779
7780 p_magic = save_magic;
7781 p_cpo = save_cpo;
7782
7783 /* If not found, try expanding the name, like done for bufexists(). */
7784 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786
7787 return buf;
7788}
7789
7790/*
7791 * "bufname(expr)" function
7792 */
7793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007795 typval_T *argvars;
7796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 buf_T *buf;
7799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007800 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802 buf = get_buf_tv(&argvars[0]);
7803 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 --emsg_off;
7809}
7810
7811/*
7812 * "bufnr(expr)" function
7813 */
7814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 typval_T *argvars;
7817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007820 int error = FALSE;
7821 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007823 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007825 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007826 --emsg_off;
7827
7828 /* If the buffer isn't found and the second argument is not zero create a
7829 * new buffer. */
7830 if (buf == NULL
7831 && argvars[1].v_type != VAR_UNKNOWN
7832 && get_tv_number_chk(&argvars[1], &error) != 0
7833 && !error
7834 && (name = get_tv_string_chk(&argvars[0])) != NULL
7835 && !error)
7836 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7837
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842}
7843
7844/*
7845 * "bufwinnr(nr)" function
7846 */
7847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007849 typval_T *argvars;
7850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
7852#ifdef FEAT_WINDOWS
7853 win_T *wp;
7854 int winnr = 0;
7855#endif
7856 buf_T *buf;
7857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007858 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861#ifdef FEAT_WINDOWS
7862 for (wp = firstwin; wp; wp = wp->w_next)
7863 {
7864 ++winnr;
7865 if (wp->w_buffer == buf)
7866 break;
7867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871#endif
7872 --emsg_off;
7873}
7874
7875/*
7876 * "byte2line(byte)" function
7877 */
7878/*ARGSUSED*/
7879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 typval_T *argvars;
7882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
7884#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886#else
7887 long boff = 0;
7888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007889 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007893 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 (linenr_T)0, &boff);
7895#endif
7896}
7897
7898/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007899 * "byteidx()" function
7900 */
7901/*ARGSUSED*/
7902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 typval_T *argvars;
7905 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007906{
7907#ifdef FEAT_MBYTE
7908 char_u *t;
7909#endif
7910 char_u *str;
7911 long idx;
7912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007913 str = get_tv_string_chk(&argvars[0]);
7914 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007915 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007916 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007917 return;
7918
7919#ifdef FEAT_MBYTE
7920 t = str;
7921 for ( ; idx > 0; idx--)
7922 {
7923 if (*t == NUL) /* EOL reached */
7924 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007925 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007928#else
7929 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007931#endif
7932}
7933
7934/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007935 * "call(func, arglist)" function
7936 */
7937 static void
7938f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 typval_T *argvars;
7940 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007941{
7942 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007944 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007946 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007948
7949 rettv->vval.v_number = 0;
7950 if (argvars[1].v_type != VAR_LIST)
7951 {
7952 EMSG(_(e_listreq));
7953 return;
7954 }
7955 if (argvars[1].vval.v_list == NULL)
7956 return;
7957
7958 if (argvars[0].v_type == VAR_FUNC)
7959 func = argvars[0].vval.v_string;
7960 else
7961 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007962 if (*func == NUL)
7963 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007964
Bram Moolenaare9a41262005-01-15 22:18:47 +00007965 if (argvars[2].v_type != VAR_UNKNOWN)
7966 {
7967 if (argvars[2].v_type != VAR_DICT)
7968 {
7969 EMSG(_(e_dictreq));
7970 return;
7971 }
7972 selfdict = argvars[2].vval.v_dict;
7973 }
7974
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007975 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7976 item = item->li_next)
7977 {
7978 if (argc == MAX_FUNC_ARGS)
7979 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007980 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007981 break;
7982 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007983 /* Make a copy of each argument. This is needed to be able to set
7984 * v_lock to VAR_FIXED in the copy without changing the original list.
7985 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007986 copy_tv(&item->li_tv, &argv[argc++]);
7987 }
7988
7989 if (item == NULL)
7990 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007991 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7992 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007993
7994 /* Free the arguments. */
7995 while (argc > 0)
7996 clear_tv(&argv[--argc]);
7997}
7998
7999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 * "char2nr(string)" function
8001 */
8002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008004 typval_T *argvars;
8005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
8007#ifdef FEAT_MBYTE
8008 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008009 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 else
8011#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013}
8014
8015/*
8016 * "cindent(lnum)" function
8017 */
8018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 typval_T *argvars;
8021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022{
8023#ifdef FEAT_CINDENT
8024 pos_T pos;
8025 linenr_T lnum;
8026
8027 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8030 {
8031 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 curwin->w_cursor = pos;
8034 }
8035 else
8036#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008037 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038}
8039
8040/*
8041 * "col(string)" function
8042 */
8043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 typval_T *argvars;
8046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 colnr_T col = 0;
8049 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008050 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008052 fp = var2fpos(&argvars[0], FALSE, &fnum);
8053 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {
8055 if (fp->col == MAXCOL)
8056 {
8057 /* '> can be MAXCOL, get the length of the line then */
8058 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8059 col = STRLEN(ml_get(fp->lnum)) + 1;
8060 else
8061 col = MAXCOL;
8062 }
8063 else
8064 {
8065 col = fp->col + 1;
8066#ifdef FEAT_VIRTUALEDIT
8067 /* col(".") when the cursor is on the NUL at the end of the line
8068 * because of "coladd" can be seen as an extra column. */
8069 if (virtual_active() && fp == &curwin->w_cursor)
8070 {
8071 char_u *p = ml_get_cursor();
8072
8073 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8074 curwin->w_virtcol - curwin->w_cursor.coladd))
8075 {
8076# ifdef FEAT_MBYTE
8077 int l;
8078
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008079 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 col += l;
8081# else
8082 if (*p != NUL && p[1] == NUL)
8083 ++col;
8084# endif
8085 }
8086 }
8087#endif
8088 }
8089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091}
8092
Bram Moolenaar572cb562005-08-05 21:35:02 +00008093#if defined(FEAT_INS_EXPAND)
8094/*
8095 * "complete_add()" function
8096 */
8097/*ARGSUSED*/
8098 static void
8099f_complete_add(argvars, rettv)
8100 typval_T *argvars;
8101 typval_T *rettv;
8102{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008103 char_u *word;
8104 char_u *extra = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008105 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008106
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008107 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8108 {
8109 word = get_dict_string(argvars[0].vval.v_dict,
8110 (char_u *)"word", FALSE);
8111 extra = get_dict_string(argvars[0].vval.v_dict,
8112 (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008113 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008114 }
8115 else
8116 word = get_tv_string_chk(&argvars[0]);
8117 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008118 rettv->vval.v_number = ins_compl_add(word, -1, icase,
8119 NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008120}
8121
8122/*
8123 * "complete_check()" function
8124 */
8125/*ARGSUSED*/
8126 static void
8127f_complete_check(argvars, rettv)
8128 typval_T *argvars;
8129 typval_T *rettv;
8130{
8131 int saved = RedrawingDisabled;
8132
8133 RedrawingDisabled = 0;
8134 ins_compl_check_keys(0);
8135 rettv->vval.v_number = compl_interrupted;
8136 RedrawingDisabled = saved;
8137}
8138#endif
8139
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140/*
8141 * "confirm(message, buttons[, default [, type]])" function
8142 */
8143/*ARGSUSED*/
8144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008146 typval_T *argvars;
8147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8150 char_u *message;
8151 char_u *buttons = NULL;
8152 char_u buf[NUMBUFLEN];
8153 char_u buf2[NUMBUFLEN];
8154 int def = 1;
8155 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008156 char_u *typestr;
8157 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008159 message = get_tv_string_chk(&argvars[0]);
8160 if (message == NULL)
8161 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008162 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008164 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8165 if (buttons == NULL)
8166 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008167 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008169 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008170 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008172 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8173 if (typestr == NULL)
8174 error = TRUE;
8175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008177 switch (TOUPPER_ASC(*typestr))
8178 {
8179 case 'E': type = VIM_ERROR; break;
8180 case 'Q': type = VIM_QUESTION; break;
8181 case 'I': type = VIM_INFO; break;
8182 case 'W': type = VIM_WARNING; break;
8183 case 'G': type = VIM_GENERIC; break;
8184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 }
8186 }
8187 }
8188 }
8189
8190 if (buttons == NULL || *buttons == NUL)
8191 buttons = (char_u *)_("&Ok");
8192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008193 if (error)
8194 rettv->vval.v_number = 0;
8195 else
8196 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 def, NULL);
8198#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200#endif
8201}
8202
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008203/*
8204 * "copy()" function
8205 */
8206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008207f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008208 typval_T *argvars;
8209 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008210{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008211 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008212}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213
8214/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008215 * "count()" function
8216 */
8217 static void
8218f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 typval_T *argvars;
8220 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008221{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008222 long n = 0;
8223 int ic = FALSE;
8224
Bram Moolenaare9a41262005-01-15 22:18:47 +00008225 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008226 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008227 listitem_T *li;
8228 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008229 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008230
Bram Moolenaare9a41262005-01-15 22:18:47 +00008231 if ((l = argvars[0].vval.v_list) != NULL)
8232 {
8233 li = l->lv_first;
8234 if (argvars[2].v_type != VAR_UNKNOWN)
8235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008236 int error = FALSE;
8237
8238 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008239 if (argvars[3].v_type != VAR_UNKNOWN)
8240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008241 idx = get_tv_number_chk(&argvars[3], &error);
8242 if (!error)
8243 {
8244 li = list_find(l, idx);
8245 if (li == NULL)
8246 EMSGN(_(e_listidx), idx);
8247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008249 if (error)
8250 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008251 }
8252
8253 for ( ; li != NULL; li = li->li_next)
8254 if (tv_equal(&li->li_tv, &argvars[1], ic))
8255 ++n;
8256 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008257 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008258 else if (argvars[0].v_type == VAR_DICT)
8259 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008260 int todo;
8261 dict_T *d;
8262 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008263
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008264 if ((d = argvars[0].vval.v_dict) != NULL)
8265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008266 int error = FALSE;
8267
Bram Moolenaare9a41262005-01-15 22:18:47 +00008268 if (argvars[2].v_type != VAR_UNKNOWN)
8269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008270 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008271 if (argvars[3].v_type != VAR_UNKNOWN)
8272 EMSG(_(e_invarg));
8273 }
8274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008275 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008277 {
8278 if (!HASHITEM_EMPTY(hi))
8279 {
8280 --todo;
8281 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8282 ++n;
8283 }
8284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008285 }
8286 }
8287 else
8288 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008289 rettv->vval.v_number = n;
8290}
8291
8292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8294 *
8295 * Checks the existence of a cscope connection.
8296 */
8297/*ARGSUSED*/
8298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 typval_T *argvars;
8301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303#ifdef FEAT_CSCOPE
8304 int num = 0;
8305 char_u *dbpath = NULL;
8306 char_u *prepend = NULL;
8307 char_u buf[NUMBUFLEN];
8308
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 if (argvars[0].v_type != VAR_UNKNOWN
8310 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312 num = (int)get_tv_number(&argvars[0]);
8313 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008314 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008315 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008318 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321#endif
8322}
8323
8324/*
8325 * "cursor(lnum, col)" function
8326 *
8327 * Moves the cursor to the specified line and column
8328 */
8329/*ARGSUSED*/
8330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 typval_T *argvars;
8333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008336#ifdef FEAT_VIRTUALEDIT
8337 long coladd = 0;
8338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
Bram Moolenaara5525202006-03-02 22:52:09 +00008340 if (argvars[1].v_type == VAR_UNKNOWN)
8341 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008342 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008343
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008344 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008345 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008346 line = pos.lnum;
8347 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008348#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008349 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008350#endif
8351 }
8352 else
8353 {
8354 line = get_tv_lnum(argvars);
8355 col = get_tv_number_chk(&argvars[1], NULL);
8356#ifdef FEAT_VIRTUALEDIT
8357 if (argvars[2].v_type != VAR_UNKNOWN)
8358 coladd = get_tv_number_chk(&argvars[2], NULL);
8359#endif
8360 }
8361 if (line < 0 || col < 0
8362#ifdef FEAT_VIRTUALEDIT
8363 || coladd < 0
8364#endif
8365 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008366 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (line > 0)
8368 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 if (col > 0)
8370 curwin->w_cursor.col = col - 1;
8371#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008372 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373#endif
8374
8375 /* Make sure the cursor is in a valid position. */
8376 check_cursor();
8377#ifdef FEAT_MBYTE
8378 /* Correct cursor for multi-byte character. */
8379 if (has_mbyte)
8380 mb_adjust_cursor();
8381#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008382
8383 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384}
8385
8386/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008387 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 */
8389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008391 typval_T *argvars;
8392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008394 int noref = 0;
8395
8396 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008397 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008398 if (noref < 0 || noref > 1)
8399 EMSG(_(e_invarg));
8400 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008401 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402}
8403
8404/*
8405 * "delete()" function
8406 */
8407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008408f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008409 typval_T *argvars;
8410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008413 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008415 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416}
8417
8418/*
8419 * "did_filetype()" function
8420 */
8421/*ARGSUSED*/
8422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008424 typval_T *argvars;
8425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426{
8427#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008428 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008430 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431#endif
8432}
8433
8434/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008435 * "diff_filler()" function
8436 */
8437/*ARGSUSED*/
8438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008439f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 typval_T *argvars;
8441 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008442{
8443#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008444 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008445#endif
8446}
8447
8448/*
8449 * "diff_hlID()" function
8450 */
8451/*ARGSUSED*/
8452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008453f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008454 typval_T *argvars;
8455 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008456{
8457#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008459 static linenr_T prev_lnum = 0;
8460 static int changedtick = 0;
8461 static int fnum = 0;
8462 static int change_start = 0;
8463 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008464 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008465 int filler_lines;
8466 int col;
8467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008468 if (lnum < 0) /* ignore type error in {lnum} arg */
8469 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008470 if (lnum != prev_lnum
8471 || changedtick != curbuf->b_changedtick
8472 || fnum != curbuf->b_fnum)
8473 {
8474 /* New line, buffer, change: need to get the values. */
8475 filler_lines = diff_check(curwin, lnum);
8476 if (filler_lines < 0)
8477 {
8478 if (filler_lines == -1)
8479 {
8480 change_start = MAXCOL;
8481 change_end = -1;
8482 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8483 hlID = HLF_ADD; /* added line */
8484 else
8485 hlID = HLF_CHD; /* changed line */
8486 }
8487 else
8488 hlID = HLF_ADD; /* added line */
8489 }
8490 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008491 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008492 prev_lnum = lnum;
8493 changedtick = curbuf->b_changedtick;
8494 fnum = curbuf->b_fnum;
8495 }
8496
8497 if (hlID == HLF_CHD || hlID == HLF_TXD)
8498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008499 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008500 if (col >= change_start && col <= change_end)
8501 hlID = HLF_TXD; /* changed text */
8502 else
8503 hlID = HLF_CHD; /* changed line */
8504 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008505 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008506#endif
8507}
8508
8509/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008510 * "empty({expr})" function
8511 */
8512 static void
8513f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 typval_T *argvars;
8515 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008516{
8517 int n;
8518
8519 switch (argvars[0].v_type)
8520 {
8521 case VAR_STRING:
8522 case VAR_FUNC:
8523 n = argvars[0].vval.v_string == NULL
8524 || *argvars[0].vval.v_string == NUL;
8525 break;
8526 case VAR_NUMBER:
8527 n = argvars[0].vval.v_number == 0;
8528 break;
8529 case VAR_LIST:
8530 n = argvars[0].vval.v_list == NULL
8531 || argvars[0].vval.v_list->lv_first == NULL;
8532 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008533 case VAR_DICT:
8534 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008535 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008536 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008537 default:
8538 EMSG2(_(e_intern2), "f_empty()");
8539 n = 0;
8540 }
8541
8542 rettv->vval.v_number = n;
8543}
8544
8545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 * "escape({string}, {chars})" function
8547 */
8548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008549f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 typval_T *argvars;
8551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 char_u buf[NUMBUFLEN];
8554
Bram Moolenaar758711c2005-02-02 23:11:38 +00008555 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8556 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558}
8559
8560/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008561 * "eval()" function
8562 */
8563/*ARGSUSED*/
8564 static void
8565f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 typval_T *argvars;
8567 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008568{
8569 char_u *s;
8570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008571 s = get_tv_string_chk(&argvars[0]);
8572 if (s != NULL)
8573 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008575 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8576 {
8577 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008578 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008579 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008580 else if (*s != NUL)
8581 EMSG(_(e_trailing));
8582}
8583
8584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 * "eventhandler()" function
8586 */
8587/*ARGSUSED*/
8588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 typval_T *argvars;
8591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594}
8595
8596/*
8597 * "executable()" function
8598 */
8599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 typval_T *argvars;
8602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605}
8606
8607/*
8608 * "exists()" function
8609 */
8610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008612 typval_T *argvars;
8613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 char_u *p;
8616 char_u *name;
8617 int n = FALSE;
8618 int len = 0;
8619
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008620 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 if (*p == '$') /* environment variable */
8622 {
8623 /* first try "normal" environment variables (fast) */
8624 if (mch_getenv(p + 1) != NULL)
8625 n = TRUE;
8626 else
8627 {
8628 /* try expanding things like $VIM and ${HOME} */
8629 p = expand_env_save(p);
8630 if (p != NULL && *p != '$')
8631 n = TRUE;
8632 vim_free(p);
8633 }
8634 }
8635 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008636 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 else if (*p == '*') /* internal or user defined function */
8638 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008639 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 }
8641 else if (*p == ':')
8642 {
8643 n = cmd_exists(p + 1);
8644 }
8645 else if (*p == '#')
8646 {
8647#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008648 if (p[1] == '#')
8649 n = autocmd_supported(p + 2);
8650 else
8651 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652#endif
8653 }
8654 else /* internal variable */
8655 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008656 char_u *tofree;
8657 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008659 /* get_name_len() takes care of expanding curly braces */
8660 name = p;
8661 len = get_name_len(&p, &tofree, TRUE, FALSE);
8662 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008664 if (tofree != NULL)
8665 name = tofree;
8666 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8667 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008669 /* handle d.key, l[idx], f(expr) */
8670 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8671 if (n)
8672 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 }
8674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008676 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 }
8678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680}
8681
8682/*
8683 * "expand()" function
8684 */
8685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008686f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 typval_T *argvars;
8688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689{
8690 char_u *s;
8691 int len;
8692 char_u *errormsg;
8693 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8694 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008695 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 rettv->v_type = VAR_STRING;
8698 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 if (*s == '%' || *s == '#' || *s == '<')
8700 {
8701 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 --emsg_off;
8704 }
8705 else
8706 {
8707 /* When the optional second argument is non-zero, don't remove matches
8708 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008709 if (argvars[1].v_type != VAR_UNKNOWN
8710 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008712 if (!error)
8713 {
8714 ExpandInit(&xpc);
8715 xpc.xp_context = EXPAND_FILES;
8716 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8717 ExpandCleanup(&xpc);
8718 }
8719 else
8720 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 }
8722}
8723
8724/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008725 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008726 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008727 */
8728 static void
8729f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 typval_T *argvars;
8731 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008732{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008733 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008734 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008735 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 list_T *l1, *l2;
8737 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008738 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008740
Bram Moolenaare9a41262005-01-15 22:18:47 +00008741 l1 = argvars[0].vval.v_list;
8742 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008743 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8744 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008745 {
8746 if (argvars[2].v_type != VAR_UNKNOWN)
8747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 before = get_tv_number_chk(&argvars[2], &error);
8749 if (error)
8750 return; /* type error; errmsg already given */
8751
Bram Moolenaar758711c2005-02-02 23:11:38 +00008752 if (before == l1->lv_len)
8753 item = NULL;
8754 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008755 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008756 item = list_find(l1, before);
8757 if (item == NULL)
8758 {
8759 EMSGN(_(e_listidx), before);
8760 return;
8761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008762 }
8763 }
8764 else
8765 item = NULL;
8766 list_extend(l1, l2, item);
8767
Bram Moolenaare9a41262005-01-15 22:18:47 +00008768 copy_tv(&argvars[0], rettv);
8769 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008770 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008771 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8772 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008773 dict_T *d1, *d2;
8774 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008775 char_u *action;
8776 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008778 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008779
8780 d1 = argvars[0].vval.v_dict;
8781 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008782 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8783 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784 {
8785 /* Check the third argument. */
8786 if (argvars[2].v_type != VAR_UNKNOWN)
8787 {
8788 static char *(av[]) = {"keep", "force", "error"};
8789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008790 action = get_tv_string_chk(&argvars[2]);
8791 if (action == NULL)
8792 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008793 for (i = 0; i < 3; ++i)
8794 if (STRCMP(action, av[i]) == 0)
8795 break;
8796 if (i == 3)
8797 {
8798 EMSGN(_(e_invarg2), action);
8799 return;
8800 }
8801 }
8802 else
8803 action = (char_u *)"force";
8804
8805 /* Go over all entries in the second dict and add them to the
8806 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008807 todo = d2->dv_hashtab.ht_used;
8808 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008809 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008810 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008811 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008812 --todo;
8813 di1 = dict_find(d1, hi2->hi_key, -1);
8814 if (di1 == NULL)
8815 {
8816 di1 = dictitem_copy(HI2DI(hi2));
8817 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8818 dictitem_free(di1);
8819 }
8820 else if (*action == 'e')
8821 {
8822 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8823 break;
8824 }
8825 else if (*action == 'f')
8826 {
8827 clear_tv(&di1->di_tv);
8828 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8829 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008830 }
8831 }
8832
Bram Moolenaare9a41262005-01-15 22:18:47 +00008833 copy_tv(&argvars[0], rettv);
8834 }
8835 }
8836 else
8837 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008838}
8839
8840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 * "filereadable()" function
8842 */
8843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 typval_T *argvars;
8846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847{
8848 FILE *fd;
8849 char_u *p;
8850 int n;
8851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8854 {
8855 n = TRUE;
8856 fclose(fd);
8857 }
8858 else
8859 n = FALSE;
8860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862}
8863
8864/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008865 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 * rights to write into.
8867 */
8868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008870 typval_T *argvars;
8871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008873 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874}
8875
Bram Moolenaar33570922005-01-25 22:26:29 +00008876static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008877
8878 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008879findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 typval_T *argvars;
8881 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008882 int dir;
8883{
8884#ifdef FEAT_SEARCHPATH
8885 char_u *fname;
8886 char_u *fresult = NULL;
8887 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8888 char_u *p;
8889 char_u pathbuf[NUMBUFLEN];
8890 int count = 1;
8891 int first = TRUE;
8892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008894
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008895 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008897 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8898 if (p == NULL)
8899 count = -1; /* error */
8900 else
8901 {
8902 if (*p != NUL)
8903 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008905 if (argvars[2].v_type != VAR_UNKNOWN)
8906 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8907 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008908 }
8909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008912 do
8913 {
8914 vim_free(fresult);
8915 fresult = find_file_in_path_option(first ? fname : NULL,
8916 first ? (int)STRLEN(fname) : 0,
8917 0, first, path, dir, NULL);
8918 first = FALSE;
8919 } while (--count > 0 && fresult != NULL);
8920 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008921
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008923#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008925#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008927}
8928
Bram Moolenaar33570922005-01-25 22:26:29 +00008929static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8930static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008931
8932/*
8933 * Implementation of map() and filter().
8934 */
8935 static void
8936filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008937 typval_T *argvars;
8938 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008939 int map;
8940{
8941 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008942 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008943 listitem_T *li, *nli;
8944 list_T *l = NULL;
8945 dictitem_T *di;
8946 hashtab_T *ht;
8947 hashitem_T *hi;
8948 dict_T *d = NULL;
8949 typval_T save_val;
8950 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008951 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008952 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008953 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008954 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008955
8956 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008958 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008959 if ((l = argvars[0].vval.v_list) == NULL
8960 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008961 return;
8962 }
8963 else if (argvars[0].v_type == VAR_DICT)
8964 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008965 if ((d = argvars[0].vval.v_dict) == NULL
8966 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 return;
8968 }
8969 else
8970 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008971 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008972 return;
8973 }
8974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 expr = get_tv_string_buf_chk(&argvars[1], buf);
8976 /* On type errors, the preceding call has already displayed an error
8977 * message. Avoid a misleading error message for an empty string that
8978 * was not passed as argument. */
8979 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008980 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008981 prepare_vimvar(VV_VAL, &save_val);
8982 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008983
Bram Moolenaar280f1262006-01-30 00:14:18 +00008984 /* We reset "called_emsg" to be able to detect whether an error
8985 * occurred during evaluation of the expression. "did_emsg" can't be
8986 * used, because it is reset when calling a function. */
8987 save_called_emsg = called_emsg;
8988 called_emsg = FALSE;
8989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 prepare_vimvar(VV_KEY, &save_key);
8993 vimvars[VV_KEY].vv_type = VAR_STRING;
8994
8995 ht = &d->dv_hashtab;
8996 hash_lock(ht);
8997 todo = ht->ht_used;
8998 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009000 if (!HASHITEM_EMPTY(hi))
9001 {
9002 --todo;
9003 di = HI2DI(hi);
9004 if (tv_check_lock(di->di_tv.v_lock, msg))
9005 break;
9006 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009007 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
9008 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009009 break;
9010 if (!map && rem)
9011 dictitem_remove(d, di);
9012 clear_tv(&vimvars[VV_KEY].vv_tv);
9013 }
9014 }
9015 hash_unlock(ht);
9016
9017 restore_vimvar(VV_KEY, &save_key);
9018 }
9019 else
9020 {
9021 for (li = l->lv_first; li != NULL; li = nli)
9022 {
9023 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009024 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009025 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009026 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
9027 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009028 break;
9029 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009030 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009031 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009032 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009035
9036 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009037 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009038
9039 copy_tv(&argvars[0], rettv);
9040}
9041
9042 static int
9043filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009044 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 char_u *expr;
9046 int map;
9047 int *remp;
9048{
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009050 char_u *s;
9051
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009053 s = expr;
9054 if (eval1(&s, &rettv, TRUE) == FAIL)
9055 return FAIL;
9056 if (*s != NUL) /* check for trailing chars after expr */
9057 {
9058 EMSG2(_(e_invexpr2), s);
9059 return FAIL;
9060 }
9061 if (map)
9062 {
9063 /* map(): replace the list item value */
9064 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009065 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009066 *tv = rettv;
9067 }
9068 else
9069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009070 int error = FALSE;
9071
Bram Moolenaare9a41262005-01-15 22:18:47 +00009072 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009073 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009074 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075 /* On type error, nothing has been removed; return FAIL to stop the
9076 * loop. The error message was given by get_tv_number_chk(). */
9077 if (error)
9078 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009079 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009081 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009082}
9083
9084/*
9085 * "filter()" function
9086 */
9087 static void
9088f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 typval_T *argvars;
9090 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009091{
9092 filter_map(argvars, rettv, FALSE);
9093}
9094
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009095/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009096 * "finddir({fname}[, {path}[, {count}]])" function
9097 */
9098 static void
9099f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 typval_T *argvars;
9101 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009102{
9103 findfilendir(argvars, rettv, TRUE);
9104}
9105
9106/*
9107 * "findfile({fname}[, {path}[, {count}]])" function
9108 */
9109 static void
9110f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009111 typval_T *argvars;
9112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009113{
9114 findfilendir(argvars, rettv, FALSE);
9115}
9116
9117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 * "fnamemodify({fname}, {mods})" function
9119 */
9120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009122 typval_T *argvars;
9123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 char_u *fname;
9126 char_u *mods;
9127 int usedlen = 0;
9128 int len;
9129 char_u *fbuf = NULL;
9130 char_u buf[NUMBUFLEN];
9131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 fname = get_tv_string_chk(&argvars[0]);
9133 mods = get_tv_string_buf_chk(&argvars[1], buf);
9134 if (fname == NULL || mods == NULL)
9135 fname = NULL;
9136 else
9137 {
9138 len = (int)STRLEN(fname);
9139 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 vim_free(fbuf);
9148}
9149
Bram Moolenaar33570922005-01-25 22:26:29 +00009150static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151
9152/*
9153 * "foldclosed()" function
9154 */
9155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009157 typval_T *argvars;
9158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 int end;
9160{
9161#ifdef FEAT_FOLDING
9162 linenr_T lnum;
9163 linenr_T first, last;
9164
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9167 {
9168 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9169 {
9170 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 return;
9175 }
9176 }
9177#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179}
9180
9181/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009182 * "foldclosed()" function
9183 */
9184 static void
9185f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009186 typval_T *argvars;
9187 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009188{
9189 foldclosed_both(argvars, rettv, FALSE);
9190}
9191
9192/*
9193 * "foldclosedend()" function
9194 */
9195 static void
9196f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009197 typval_T *argvars;
9198 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009199{
9200 foldclosed_both(argvars, rettv, TRUE);
9201}
9202
9203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 * "foldlevel()" function
9205 */
9206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *argvars;
9209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211#ifdef FEAT_FOLDING
9212 linenr_T lnum;
9213
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 else
9218#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220}
9221
9222/*
9223 * "foldtext()" function
9224 */
9225/*ARGSUSED*/
9226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009227f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009228 typval_T *argvars;
9229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230{
9231#ifdef FEAT_FOLDING
9232 linenr_T lnum;
9233 char_u *s;
9234 char_u *r;
9235 int len;
9236 char *txt;
9237#endif
9238
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009239 rettv->v_type = VAR_STRING;
9240 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009242 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9243 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9244 <= curbuf->b_ml.ml_line_count
9245 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 {
9247 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009248 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9249 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 {
9251 if (!linewhite(lnum))
9252 break;
9253 ++lnum;
9254 }
9255
9256 /* Find interesting text in this line. */
9257 s = skipwhite(ml_get(lnum));
9258 /* skip C comment-start */
9259 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009262 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009263 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009264 {
9265 s = skipwhite(ml_get(lnum + 1));
9266 if (*s == '*')
9267 s = skipwhite(s + 1);
9268 }
9269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 txt = _("+-%s%3ld lines: ");
9271 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009272 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 + 20 /* for %3ld */
9274 + STRLEN(s))); /* concatenated */
9275 if (r != NULL)
9276 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009277 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9278 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9279 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 len = (int)STRLEN(r);
9281 STRCAT(r, s);
9282 /* remove 'foldmarker' and 'commentstring' */
9283 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 }
9286 }
9287#endif
9288}
9289
9290/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009291 * "foldtextresult(lnum)" function
9292 */
9293/*ARGSUSED*/
9294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009295f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009296 typval_T *argvars;
9297 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009298{
9299#ifdef FEAT_FOLDING
9300 linenr_T lnum;
9301 char_u *text;
9302 char_u buf[51];
9303 foldinfo_T foldinfo;
9304 int fold_count;
9305#endif
9306
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307 rettv->v_type = VAR_STRING;
9308 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009309#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009311 /* treat illegal types and illegal string values for {lnum} the same */
9312 if (lnum < 0)
9313 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009314 fold_count = foldedCount(curwin, lnum, &foldinfo);
9315 if (fold_count > 0)
9316 {
9317 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9318 &foldinfo, buf);
9319 if (text == buf)
9320 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009322 }
9323#endif
9324}
9325
9326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 * "foreground()" function
9328 */
9329/*ARGSUSED*/
9330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009332 typval_T *argvars;
9333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336#ifdef FEAT_GUI
9337 if (gui.in_use)
9338 gui_mch_set_foreground();
9339#else
9340# ifdef WIN32
9341 win32_set_foreground();
9342# endif
9343#endif
9344}
9345
9346/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009347 * "function()" function
9348 */
9349/*ARGSUSED*/
9350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009352 typval_T *argvars;
9353 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009354{
9355 char_u *s;
9356
Bram Moolenaara7043832005-01-21 11:56:39 +00009357 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009359 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009360 EMSG2(_(e_invarg2), s);
9361 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009362 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009363 else
9364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009365 rettv->vval.v_string = vim_strsave(s);
9366 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009367 }
9368}
9369
9370/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009371 * "garbagecollect()" function
9372 */
9373/*ARGSUSED*/
9374 static void
9375f_garbagecollect(argvars, rettv)
9376 typval_T *argvars;
9377 typval_T *rettv;
9378{
9379 garbage_collect();
9380}
9381
9382/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009383 * "get()" function
9384 */
9385 static void
9386f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009387 typval_T *argvars;
9388 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009389{
Bram Moolenaar33570922005-01-25 22:26:29 +00009390 listitem_T *li;
9391 list_T *l;
9392 dictitem_T *di;
9393 dict_T *d;
9394 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009395
Bram Moolenaare9a41262005-01-15 22:18:47 +00009396 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009397 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009398 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009400 int error = FALSE;
9401
9402 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9403 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009404 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009405 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009406 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009407 else if (argvars[0].v_type == VAR_DICT)
9408 {
9409 if ((d = argvars[0].vval.v_dict) != NULL)
9410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009411 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009412 if (di != NULL)
9413 tv = &di->di_tv;
9414 }
9415 }
9416 else
9417 EMSG2(_(e_listdictarg), "get()");
9418
9419 if (tv == NULL)
9420 {
9421 if (argvars[2].v_type == VAR_UNKNOWN)
9422 rettv->vval.v_number = 0;
9423 else
9424 copy_tv(&argvars[2], rettv);
9425 }
9426 else
9427 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009428}
9429
Bram Moolenaar342337a2005-07-21 21:11:17 +00009430static 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 +00009431
9432/*
9433 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009434 * Return a range (from start to end) of lines in rettv from the specified
9435 * buffer.
9436 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009437 */
9438 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009439get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009440 buf_T *buf;
9441 linenr_T start;
9442 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009443 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009444 typval_T *rettv;
9445{
9446 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009447
Bram Moolenaar342337a2005-07-21 21:11:17 +00009448 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009449 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009450 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009451 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009452 }
9453 else
9454 rettv->vval.v_number = 0;
9455
9456 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9457 return;
9458
9459 if (!retlist)
9460 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009461 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9462 p = ml_get_buf(buf, start, FALSE);
9463 else
9464 p = (char_u *)"";
9465
9466 rettv->v_type = VAR_STRING;
9467 rettv->vval.v_string = vim_strsave(p);
9468 }
9469 else
9470 {
9471 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009472 return;
9473
9474 if (start < 1)
9475 start = 1;
9476 if (end > buf->b_ml.ml_line_count)
9477 end = buf->b_ml.ml_line_count;
9478 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009479 if (list_append_string(rettv->vval.v_list,
9480 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009481 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009482 }
9483}
9484
9485/*
9486 * "getbufline()" function
9487 */
9488 static void
9489f_getbufline(argvars, rettv)
9490 typval_T *argvars;
9491 typval_T *rettv;
9492{
9493 linenr_T lnum;
9494 linenr_T end;
9495 buf_T *buf;
9496
9497 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9498 ++emsg_off;
9499 buf = get_buf_tv(&argvars[0]);
9500 --emsg_off;
9501
Bram Moolenaar661b1822005-07-28 22:36:45 +00009502 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009503 if (argvars[2].v_type == VAR_UNKNOWN)
9504 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009505 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009506 end = get_tv_lnum_buf(&argvars[2], buf);
9507
Bram Moolenaar342337a2005-07-21 21:11:17 +00009508 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009509}
9510
Bram Moolenaar0d660222005-01-07 21:51:51 +00009511/*
9512 * "getbufvar()" function
9513 */
9514 static void
9515f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 typval_T *argvars;
9517 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009518{
9519 buf_T *buf;
9520 buf_T *save_curbuf;
9521 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009522 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009524 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9525 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009526 ++emsg_off;
9527 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009528
9529 rettv->v_type = VAR_STRING;
9530 rettv->vval.v_string = NULL;
9531
9532 if (buf != NULL && varname != NULL)
9533 {
9534 if (*varname == '&') /* buffer-local-option */
9535 {
9536 /* set curbuf to be our buf, temporarily */
9537 save_curbuf = curbuf;
9538 curbuf = buf;
9539
9540 get_option_tv(&varname, rettv, TRUE);
9541
9542 /* restore previous notion of curbuf */
9543 curbuf = save_curbuf;
9544 }
9545 else
9546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 if (*varname == NUL)
9548 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9549 * scope prefix before the NUL byte is required by
9550 * find_var_in_ht(). */
9551 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009552 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009553 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009554 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009555 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009556 }
9557 }
9558
9559 --emsg_off;
9560}
9561
9562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 * "getchar()" function
9564 */
9565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566f_getchar(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{
9570 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572
9573 ++no_mapping;
9574 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009575 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 /* getchar(): blocking wait. */
9577 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009578 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 /* getchar(1): only check if char avail */
9580 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 else if (error || vpeekc() == NUL)
9582 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 n = 0;
9584 else
9585 /* getchar(0) and char avail: return char */
9586 n = safe_vgetc();
9587 --no_mapping;
9588 --allow_keys;
9589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 if (IS_SPECIAL(n) || mod_mask != 0)
9592 {
9593 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9594 int i = 0;
9595
9596 /* Turn a special key into three bytes, plus modifier. */
9597 if (mod_mask != 0)
9598 {
9599 temp[i++] = K_SPECIAL;
9600 temp[i++] = KS_MODIFIER;
9601 temp[i++] = mod_mask;
9602 }
9603 if (IS_SPECIAL(n))
9604 {
9605 temp[i++] = K_SPECIAL;
9606 temp[i++] = K_SECOND(n);
9607 temp[i++] = K_THIRD(n);
9608 }
9609#ifdef FEAT_MBYTE
9610 else if (has_mbyte)
9611 i += (*mb_char2bytes)(n, temp + i);
9612#endif
9613 else
9614 temp[i++] = n;
9615 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->v_type = VAR_STRING;
9617 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 }
9619}
9620
9621/*
9622 * "getcharmod()" function
9623 */
9624/*ARGSUSED*/
9625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 typval_T *argvars;
9628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631}
9632
9633/*
9634 * "getcmdline()" function
9635 */
9636/*ARGSUSED*/
9637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009639 typval_T *argvars;
9640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 rettv->v_type = VAR_STRING;
9643 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644}
9645
9646/*
9647 * "getcmdpos()" function
9648 */
9649/*ARGSUSED*/
9650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009652 typval_T *argvars;
9653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656}
9657
9658/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009659 * "getcmdtype()" function
9660 */
9661/*ARGSUSED*/
9662 static void
9663f_getcmdtype(argvars, rettv)
9664 typval_T *argvars;
9665 typval_T *rettv;
9666{
9667 rettv->v_type = VAR_STRING;
9668 rettv->vval.v_string = alloc(2);
9669 if (rettv->vval.v_string != NULL)
9670 {
9671 rettv->vval.v_string[0] = get_cmdline_type();
9672 rettv->vval.v_string[1] = NUL;
9673 }
9674}
9675
9676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 * "getcwd()" function
9678 */
9679/*ARGSUSED*/
9680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009682 typval_T *argvars;
9683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685 char_u cwd[MAXPATHL];
9686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009687 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 else
9691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009694 if (rettv->vval.v_string != NULL)
9695 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696#endif
9697 }
9698}
9699
9700/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009701 * "getfontname()" function
9702 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009703/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009706 typval_T *argvars;
9707 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->v_type = VAR_STRING;
9710 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009711#ifdef FEAT_GUI
9712 if (gui.in_use)
9713 {
9714 GuiFont font;
9715 char_u *name = NULL;
9716
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009717 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009718 {
9719 /* Get the "Normal" font. Either the name saved by
9720 * hl_set_font_name() or from the font ID. */
9721 font = gui.norm_font;
9722 name = hl_get_font_name();
9723 }
9724 else
9725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009727 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9728 return;
9729 font = gui_mch_get_font(name, FALSE);
9730 if (font == NOFONT)
9731 return; /* Invalid font name, return empty string. */
9732 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009734 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009735 gui_mch_free_font(font);
9736 }
9737#endif
9738}
9739
9740/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009741 * "getfperm({fname})" function
9742 */
9743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009745 typval_T *argvars;
9746 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009747{
9748 char_u *fname;
9749 struct stat st;
9750 char_u *perm = NULL;
9751 char_u flags[] = "rwx";
9752 int i;
9753
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009757 if (mch_stat((char *)fname, &st) >= 0)
9758 {
9759 perm = vim_strsave((char_u *)"---------");
9760 if (perm != NULL)
9761 {
9762 for (i = 0; i < 9; i++)
9763 {
9764 if (st.st_mode & (1 << (8 - i)))
9765 perm[i] = flags[i % 3];
9766 }
9767 }
9768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009770}
9771
9772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 * "getfsize({fname})" function
9774 */
9775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009777 typval_T *argvars;
9778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
9780 char_u *fname;
9781 struct stat st;
9782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786
9787 if (mch_stat((char *)fname, &st) >= 0)
9788 {
9789 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 }
9794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796}
9797
9798/*
9799 * "getftime({fname})" function
9800 */
9801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009803 typval_T *argvars;
9804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806 char_u *fname;
9807 struct stat st;
9808
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810
9811 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815}
9816
9817/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009818 * "getftype({fname})" function
9819 */
9820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009822 typval_T *argvars;
9823 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009824{
9825 char_u *fname;
9826 struct stat st;
9827 char_u *type = NULL;
9828 char *t;
9829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009833 if (mch_lstat((char *)fname, &st) >= 0)
9834 {
9835#ifdef S_ISREG
9836 if (S_ISREG(st.st_mode))
9837 t = "file";
9838 else if (S_ISDIR(st.st_mode))
9839 t = "dir";
9840# ifdef S_ISLNK
9841 else if (S_ISLNK(st.st_mode))
9842 t = "link";
9843# endif
9844# ifdef S_ISBLK
9845 else if (S_ISBLK(st.st_mode))
9846 t = "bdev";
9847# endif
9848# ifdef S_ISCHR
9849 else if (S_ISCHR(st.st_mode))
9850 t = "cdev";
9851# endif
9852# ifdef S_ISFIFO
9853 else if (S_ISFIFO(st.st_mode))
9854 t = "fifo";
9855# endif
9856# ifdef S_ISSOCK
9857 else if (S_ISSOCK(st.st_mode))
9858 t = "fifo";
9859# endif
9860 else
9861 t = "other";
9862#else
9863# ifdef S_IFMT
9864 switch (st.st_mode & S_IFMT)
9865 {
9866 case S_IFREG: t = "file"; break;
9867 case S_IFDIR: t = "dir"; break;
9868# ifdef S_IFLNK
9869 case S_IFLNK: t = "link"; break;
9870# endif
9871# ifdef S_IFBLK
9872 case S_IFBLK: t = "bdev"; break;
9873# endif
9874# ifdef S_IFCHR
9875 case S_IFCHR: t = "cdev"; break;
9876# endif
9877# ifdef S_IFIFO
9878 case S_IFIFO: t = "fifo"; break;
9879# endif
9880# ifdef S_IFSOCK
9881 case S_IFSOCK: t = "socket"; break;
9882# endif
9883 default: t = "other";
9884 }
9885# else
9886 if (mch_isdir(fname))
9887 t = "dir";
9888 else
9889 t = "file";
9890# endif
9891#endif
9892 type = vim_strsave((char_u *)t);
9893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009895}
9896
9897/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009898 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009899 */
9900 static void
9901f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 typval_T *argvars;
9903 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009904{
9905 linenr_T lnum;
9906 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009907 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009908
9909 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009910 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009911 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009912 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009913 retlist = FALSE;
9914 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009915 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009916 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009917 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009918 retlist = TRUE;
9919 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009920
Bram Moolenaar342337a2005-07-21 21:11:17 +00009921 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009922}
9923
9924/*
Bram Moolenaara5525202006-03-02 22:52:09 +00009925 * "getpos(string)" function
9926 */
9927 static void
9928f_getpos(argvars, rettv)
9929 typval_T *argvars;
9930 typval_T *rettv;
9931{
9932 pos_T *fp;
9933 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009934 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009935
9936 if (rettv_list_alloc(rettv) == OK)
9937 {
9938 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009939 fp = var2fpos(&argvars[0], TRUE, &fnum);
9940 if (fnum != -1)
9941 list_append_number(l, (varnumber_T)fnum);
9942 else
9943 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +00009944 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
9945 : (varnumber_T)0);
9946 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
9947 : (varnumber_T)0);
9948 list_append_number(l,
9949#ifdef FEAT_VIRTUALEDIT
9950 (fp != NULL) ? (varnumber_T)fp->coladd :
9951#endif
9952 (varnumber_T)0);
9953 }
9954 else
9955 rettv->vval.v_number = FALSE;
9956}
9957
9958/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009959 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009960 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009961/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009962 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009963f_getqflist(argvars, rettv)
9964 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009965 typval_T *rettv;
9966{
9967#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +00009968 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009969#endif
9970
9971 rettv->vval.v_number = FALSE;
9972#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009973 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +00009974 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00009975 wp = NULL;
9976 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9977 {
9978 wp = find_win_by_nr(&argvars[0]);
9979 if (wp == NULL)
9980 return;
9981 }
9982
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009983 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009984 }
9985#endif
9986}
9987
9988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 * "getreg()" function
9990 */
9991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009993 typval_T *argvars;
9994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995{
9996 char_u *strregname;
9997 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009998 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009999 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010001 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010002 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010003 strregname = get_tv_string_chk(&argvars[0]);
10004 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010005 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010006 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010009 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 regname = (strregname == NULL ? '"' : *strregname);
10011 if (regname == 0)
10012 regname = '"';
10013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 rettv->vval.v_string = error ? NULL :
10016 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017}
10018
10019/*
10020 * "getregtype()" function
10021 */
10022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010024 typval_T *argvars;
10025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 char_u *strregname;
10028 int regname;
10029 char_u buf[NUMBUFLEN + 2];
10030 long reglen = 0;
10031
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010032 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010033 {
10034 strregname = get_tv_string_chk(&argvars[0]);
10035 if (strregname == NULL) /* type error; errmsg already given */
10036 {
10037 rettv->v_type = VAR_STRING;
10038 rettv->vval.v_string = NULL;
10039 return;
10040 }
10041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 else
10043 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010044 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
10046 regname = (strregname == NULL ? '"' : *strregname);
10047 if (regname == 0)
10048 regname = '"';
10049
10050 buf[0] = NUL;
10051 buf[1] = NUL;
10052 switch (get_reg_type(regname, &reglen))
10053 {
10054 case MLINE: buf[0] = 'V'; break;
10055 case MCHAR: buf[0] = 'v'; break;
10056#ifdef FEAT_VISUAL
10057 case MBLOCK:
10058 buf[0] = Ctrl_V;
10059 sprintf((char *)buf + 1, "%ld", reglen + 1);
10060 break;
10061#endif
10062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 rettv->v_type = VAR_STRING;
10064 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065}
10066
10067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 * "getwinposx()" function
10069 */
10070/*ARGSUSED*/
10071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010073 typval_T *argvars;
10074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010076 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077#ifdef FEAT_GUI
10078 if (gui.in_use)
10079 {
10080 int x, y;
10081
10082 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 }
10085#endif
10086}
10087
10088/*
10089 * "getwinposy()" function
10090 */
10091/*ARGSUSED*/
10092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010094 typval_T *argvars;
10095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098#ifdef FEAT_GUI
10099 if (gui.in_use)
10100 {
10101 int x, y;
10102
10103 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 }
10106#endif
10107}
10108
Bram Moolenaara40058a2005-07-11 22:42:07 +000010109 static win_T *
10110find_win_by_nr(vp)
10111 typval_T *vp;
10112{
10113#ifdef FEAT_WINDOWS
10114 win_T *wp;
10115#endif
10116 int nr;
10117
10118 nr = get_tv_number_chk(vp, NULL);
10119
10120#ifdef FEAT_WINDOWS
10121 if (nr < 0)
10122 return NULL;
10123 if (nr == 0)
10124 return curwin;
10125
10126 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10127 if (--nr <= 0)
10128 break;
10129 return wp;
10130#else
10131 if (nr == 0 || nr == 1)
10132 return curwin;
10133 return NULL;
10134#endif
10135}
10136
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137/*
10138 * "getwinvar()" function
10139 */
10140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010142 typval_T *argvars;
10143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144{
10145 win_T *win, *oldcurwin;
10146 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010147 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 varname = get_tv_string_chk(&argvars[1]);
10151 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 rettv->v_type = VAR_STRING;
10154 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155
10156 if (win != NULL && varname != NULL)
10157 {
10158 if (*varname == '&') /* window-local-option */
10159 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010160 /* Set curwin to be our win, temporarily. Also set curbuf, so
10161 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 oldcurwin = curwin;
10163 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010164 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010166 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167
10168 /* restore previous notion of curwin */
10169 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010170 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 }
10172 else
10173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010174 if (*varname == NUL)
10175 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10176 * scope prefix before the NUL byte is required by
10177 * find_var_in_ht(). */
10178 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010180 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010182 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 }
10184 }
10185
10186 --emsg_off;
10187}
10188
10189/*
10190 * "glob()" function
10191 */
10192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010193f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010194 typval_T *argvars;
10195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196{
10197 expand_T xpc;
10198
10199 ExpandInit(&xpc);
10200 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010201 rettv->v_type = VAR_STRING;
10202 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10204 ExpandCleanup(&xpc);
10205}
10206
10207/*
10208 * "globpath()" function
10209 */
10210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010211f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010212 typval_T *argvars;
10213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010218 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 if (file == NULL)
10220 rettv->vval.v_string = NULL;
10221 else
10222 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223}
10224
10225/*
10226 * "has()" function
10227 */
10228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010229f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010230 typval_T *argvars;
10231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232{
10233 int i;
10234 char_u *name;
10235 int n = FALSE;
10236 static char *(has_list[]) =
10237 {
10238#ifdef AMIGA
10239 "amiga",
10240# ifdef FEAT_ARP
10241 "arp",
10242# endif
10243#endif
10244#ifdef __BEOS__
10245 "beos",
10246#endif
10247#ifdef MSDOS
10248# ifdef DJGPP
10249 "dos32",
10250# else
10251 "dos16",
10252# endif
10253#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010254#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 "mac",
10256#endif
10257#if defined(MACOS_X_UNIX)
10258 "macunix",
10259#endif
10260#ifdef OS2
10261 "os2",
10262#endif
10263#ifdef __QNX__
10264 "qnx",
10265#endif
10266#ifdef RISCOS
10267 "riscos",
10268#endif
10269#ifdef UNIX
10270 "unix",
10271#endif
10272#ifdef VMS
10273 "vms",
10274#endif
10275#ifdef WIN16
10276 "win16",
10277#endif
10278#ifdef WIN32
10279 "win32",
10280#endif
10281#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10282 "win32unix",
10283#endif
10284#ifdef WIN64
10285 "win64",
10286#endif
10287#ifdef EBCDIC
10288 "ebcdic",
10289#endif
10290#ifndef CASE_INSENSITIVE_FILENAME
10291 "fname_case",
10292#endif
10293#ifdef FEAT_ARABIC
10294 "arabic",
10295#endif
10296#ifdef FEAT_AUTOCMD
10297 "autocmd",
10298#endif
10299#ifdef FEAT_BEVAL
10300 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010301# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10302 "balloon_multiline",
10303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304#endif
10305#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10306 "builtin_terms",
10307# ifdef ALL_BUILTIN_TCAPS
10308 "all_builtin_terms",
10309# endif
10310#endif
10311#ifdef FEAT_BYTEOFF
10312 "byte_offset",
10313#endif
10314#ifdef FEAT_CINDENT
10315 "cindent",
10316#endif
10317#ifdef FEAT_CLIENTSERVER
10318 "clientserver",
10319#endif
10320#ifdef FEAT_CLIPBOARD
10321 "clipboard",
10322#endif
10323#ifdef FEAT_CMDL_COMPL
10324 "cmdline_compl",
10325#endif
10326#ifdef FEAT_CMDHIST
10327 "cmdline_hist",
10328#endif
10329#ifdef FEAT_COMMENTS
10330 "comments",
10331#endif
10332#ifdef FEAT_CRYPT
10333 "cryptv",
10334#endif
10335#ifdef FEAT_CSCOPE
10336 "cscope",
10337#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010338#ifdef CURSOR_SHAPE
10339 "cursorshape",
10340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341#ifdef DEBUG
10342 "debug",
10343#endif
10344#ifdef FEAT_CON_DIALOG
10345 "dialog_con",
10346#endif
10347#ifdef FEAT_GUI_DIALOG
10348 "dialog_gui",
10349#endif
10350#ifdef FEAT_DIFF
10351 "diff",
10352#endif
10353#ifdef FEAT_DIGRAPHS
10354 "digraphs",
10355#endif
10356#ifdef FEAT_DND
10357 "dnd",
10358#endif
10359#ifdef FEAT_EMACS_TAGS
10360 "emacs_tags",
10361#endif
10362 "eval", /* always present, of course! */
10363#ifdef FEAT_EX_EXTRA
10364 "ex_extra",
10365#endif
10366#ifdef FEAT_SEARCH_EXTRA
10367 "extra_search",
10368#endif
10369#ifdef FEAT_FKMAP
10370 "farsi",
10371#endif
10372#ifdef FEAT_SEARCHPATH
10373 "file_in_path",
10374#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010375#if defined(UNIX) && !defined(USE_SYSTEM)
10376 "filterpipe",
10377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378#ifdef FEAT_FIND_ID
10379 "find_in_path",
10380#endif
10381#ifdef FEAT_FOLDING
10382 "folding",
10383#endif
10384#ifdef FEAT_FOOTER
10385 "footer",
10386#endif
10387#if !defined(USE_SYSTEM) && defined(UNIX)
10388 "fork",
10389#endif
10390#ifdef FEAT_GETTEXT
10391 "gettext",
10392#endif
10393#ifdef FEAT_GUI
10394 "gui",
10395#endif
10396#ifdef FEAT_GUI_ATHENA
10397# ifdef FEAT_GUI_NEXTAW
10398 "gui_neXtaw",
10399# else
10400 "gui_athena",
10401# endif
10402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403#ifdef FEAT_GUI_GTK
10404 "gui_gtk",
10405# ifdef HAVE_GTK2
10406 "gui_gtk2",
10407# endif
10408#endif
10409#ifdef FEAT_GUI_MAC
10410 "gui_mac",
10411#endif
10412#ifdef FEAT_GUI_MOTIF
10413 "gui_motif",
10414#endif
10415#ifdef FEAT_GUI_PHOTON
10416 "gui_photon",
10417#endif
10418#ifdef FEAT_GUI_W16
10419 "gui_win16",
10420#endif
10421#ifdef FEAT_GUI_W32
10422 "gui_win32",
10423#endif
10424#ifdef FEAT_HANGULIN
10425 "hangul_input",
10426#endif
10427#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10428 "iconv",
10429#endif
10430#ifdef FEAT_INS_EXPAND
10431 "insert_expand",
10432#endif
10433#ifdef FEAT_JUMPLIST
10434 "jumplist",
10435#endif
10436#ifdef FEAT_KEYMAP
10437 "keymap",
10438#endif
10439#ifdef FEAT_LANGMAP
10440 "langmap",
10441#endif
10442#ifdef FEAT_LIBCALL
10443 "libcall",
10444#endif
10445#ifdef FEAT_LINEBREAK
10446 "linebreak",
10447#endif
10448#ifdef FEAT_LISP
10449 "lispindent",
10450#endif
10451#ifdef FEAT_LISTCMDS
10452 "listcmds",
10453#endif
10454#ifdef FEAT_LOCALMAP
10455 "localmap",
10456#endif
10457#ifdef FEAT_MENU
10458 "menu",
10459#endif
10460#ifdef FEAT_SESSION
10461 "mksession",
10462#endif
10463#ifdef FEAT_MODIFY_FNAME
10464 "modify_fname",
10465#endif
10466#ifdef FEAT_MOUSE
10467 "mouse",
10468#endif
10469#ifdef FEAT_MOUSESHAPE
10470 "mouseshape",
10471#endif
10472#if defined(UNIX) || defined(VMS)
10473# ifdef FEAT_MOUSE_DEC
10474 "mouse_dec",
10475# endif
10476# ifdef FEAT_MOUSE_GPM
10477 "mouse_gpm",
10478# endif
10479# ifdef FEAT_MOUSE_JSB
10480 "mouse_jsbterm",
10481# endif
10482# ifdef FEAT_MOUSE_NET
10483 "mouse_netterm",
10484# endif
10485# ifdef FEAT_MOUSE_PTERM
10486 "mouse_pterm",
10487# endif
10488# ifdef FEAT_MOUSE_XTERM
10489 "mouse_xterm",
10490# endif
10491#endif
10492#ifdef FEAT_MBYTE
10493 "multi_byte",
10494#endif
10495#ifdef FEAT_MBYTE_IME
10496 "multi_byte_ime",
10497#endif
10498#ifdef FEAT_MULTI_LANG
10499 "multi_lang",
10500#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010501#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010502#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010503 "mzscheme",
10504#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506#ifdef FEAT_OLE
10507 "ole",
10508#endif
10509#ifdef FEAT_OSFILETYPE
10510 "osfiletype",
10511#endif
10512#ifdef FEAT_PATH_EXTRA
10513 "path_extra",
10514#endif
10515#ifdef FEAT_PERL
10516#ifndef DYNAMIC_PERL
10517 "perl",
10518#endif
10519#endif
10520#ifdef FEAT_PYTHON
10521#ifndef DYNAMIC_PYTHON
10522 "python",
10523#endif
10524#endif
10525#ifdef FEAT_POSTSCRIPT
10526 "postscript",
10527#endif
10528#ifdef FEAT_PRINTER
10529 "printer",
10530#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010531#ifdef FEAT_PROFILE
10532 "profile",
10533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534#ifdef FEAT_QUICKFIX
10535 "quickfix",
10536#endif
10537#ifdef FEAT_RIGHTLEFT
10538 "rightleft",
10539#endif
10540#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10541 "ruby",
10542#endif
10543#ifdef FEAT_SCROLLBIND
10544 "scrollbind",
10545#endif
10546#ifdef FEAT_CMDL_INFO
10547 "showcmd",
10548 "cmdline_info",
10549#endif
10550#ifdef FEAT_SIGNS
10551 "signs",
10552#endif
10553#ifdef FEAT_SMARTINDENT
10554 "smartindent",
10555#endif
10556#ifdef FEAT_SNIFF
10557 "sniff",
10558#endif
10559#ifdef FEAT_STL_OPT
10560 "statusline",
10561#endif
10562#ifdef FEAT_SUN_WORKSHOP
10563 "sun_workshop",
10564#endif
10565#ifdef FEAT_NETBEANS_INTG
10566 "netbeans_intg",
10567#endif
10568#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010569 "spell",
10570#endif
10571#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 "syntax",
10573#endif
10574#if defined(USE_SYSTEM) || !defined(UNIX)
10575 "system",
10576#endif
10577#ifdef FEAT_TAG_BINS
10578 "tag_binary",
10579#endif
10580#ifdef FEAT_TAG_OLDSTATIC
10581 "tag_old_static",
10582#endif
10583#ifdef FEAT_TAG_ANYWHITE
10584 "tag_any_white",
10585#endif
10586#ifdef FEAT_TCL
10587# ifndef DYNAMIC_TCL
10588 "tcl",
10589# endif
10590#endif
10591#ifdef TERMINFO
10592 "terminfo",
10593#endif
10594#ifdef FEAT_TERMRESPONSE
10595 "termresponse",
10596#endif
10597#ifdef FEAT_TEXTOBJ
10598 "textobjects",
10599#endif
10600#ifdef HAVE_TGETENT
10601 "tgetent",
10602#endif
10603#ifdef FEAT_TITLE
10604 "title",
10605#endif
10606#ifdef FEAT_TOOLBAR
10607 "toolbar",
10608#endif
10609#ifdef FEAT_USR_CMDS
10610 "user-commands", /* was accidentally included in 5.4 */
10611 "user_commands",
10612#endif
10613#ifdef FEAT_VIMINFO
10614 "viminfo",
10615#endif
10616#ifdef FEAT_VERTSPLIT
10617 "vertsplit",
10618#endif
10619#ifdef FEAT_VIRTUALEDIT
10620 "virtualedit",
10621#endif
10622#ifdef FEAT_VISUAL
10623 "visual",
10624#endif
10625#ifdef FEAT_VISUALEXTRA
10626 "visualextra",
10627#endif
10628#ifdef FEAT_VREPLACE
10629 "vreplace",
10630#endif
10631#ifdef FEAT_WILDIGN
10632 "wildignore",
10633#endif
10634#ifdef FEAT_WILDMENU
10635 "wildmenu",
10636#endif
10637#ifdef FEAT_WINDOWS
10638 "windows",
10639#endif
10640#ifdef FEAT_WAK
10641 "winaltkeys",
10642#endif
10643#ifdef FEAT_WRITEBACKUP
10644 "writebackup",
10645#endif
10646#ifdef FEAT_XIM
10647 "xim",
10648#endif
10649#ifdef FEAT_XFONTSET
10650 "xfontset",
10651#endif
10652#ifdef USE_XSMP
10653 "xsmp",
10654#endif
10655#ifdef USE_XSMP_INTERACT
10656 "xsmp_interact",
10657#endif
10658#ifdef FEAT_XCLIPBOARD
10659 "xterm_clipboard",
10660#endif
10661#ifdef FEAT_XTERM_SAVE
10662 "xterm_save",
10663#endif
10664#if defined(UNIX) && defined(FEAT_X11)
10665 "X11",
10666#endif
10667 NULL
10668 };
10669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 for (i = 0; has_list[i] != NULL; ++i)
10672 if (STRICMP(name, has_list[i]) == 0)
10673 {
10674 n = TRUE;
10675 break;
10676 }
10677
10678 if (n == FALSE)
10679 {
10680 if (STRNICMP(name, "patch", 5) == 0)
10681 n = has_patch(atoi((char *)name + 5));
10682 else if (STRICMP(name, "vim_starting") == 0)
10683 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010684#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10685 else if (STRICMP(name, "balloon_multiline") == 0)
10686 n = multiline_balloon_available();
10687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688#ifdef DYNAMIC_TCL
10689 else if (STRICMP(name, "tcl") == 0)
10690 n = tcl_enabled(FALSE);
10691#endif
10692#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10693 else if (STRICMP(name, "iconv") == 0)
10694 n = iconv_enabled(FALSE);
10695#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010696#ifdef DYNAMIC_MZSCHEME
10697 else if (STRICMP(name, "mzscheme") == 0)
10698 n = mzscheme_enabled(FALSE);
10699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700#ifdef DYNAMIC_RUBY
10701 else if (STRICMP(name, "ruby") == 0)
10702 n = ruby_enabled(FALSE);
10703#endif
10704#ifdef DYNAMIC_PYTHON
10705 else if (STRICMP(name, "python") == 0)
10706 n = python_enabled(FALSE);
10707#endif
10708#ifdef DYNAMIC_PERL
10709 else if (STRICMP(name, "perl") == 0)
10710 n = perl_enabled(FALSE);
10711#endif
10712#ifdef FEAT_GUI
10713 else if (STRICMP(name, "gui_running") == 0)
10714 n = (gui.in_use || gui.starting);
10715# ifdef FEAT_GUI_W32
10716 else if (STRICMP(name, "gui_win32s") == 0)
10717 n = gui_is_win32s();
10718# endif
10719# ifdef FEAT_BROWSE
10720 else if (STRICMP(name, "browse") == 0)
10721 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10722# endif
10723#endif
10724#ifdef FEAT_SYN_HL
10725 else if (STRICMP(name, "syntax_items") == 0)
10726 n = syntax_present(curbuf);
10727#endif
10728#if defined(WIN3264)
10729 else if (STRICMP(name, "win95") == 0)
10730 n = mch_windows95();
10731#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010732#ifdef FEAT_NETBEANS_INTG
10733 else if (STRICMP(name, "netbeans_enabled") == 0)
10734 n = usingNetbeans;
10735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 }
10737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010738 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739}
10740
10741/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010742 * "has_key()" function
10743 */
10744 static void
10745f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010746 typval_T *argvars;
10747 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010748{
10749 rettv->vval.v_number = 0;
10750 if (argvars[0].v_type != VAR_DICT)
10751 {
10752 EMSG(_(e_dictreq));
10753 return;
10754 }
10755 if (argvars[0].vval.v_dict == NULL)
10756 return;
10757
10758 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010759 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010760}
10761
10762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 * "hasmapto()" function
10764 */
10765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010767 typval_T *argvars;
10768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769{
10770 char_u *name;
10771 char_u *mode;
10772 char_u buf[NUMBUFLEN];
10773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010775 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 mode = (char_u *)"nvo";
10777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779
10780 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010781 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010783 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784}
10785
10786/*
10787 * "histadd()" function
10788 */
10789/*ARGSUSED*/
10790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010792 typval_T *argvars;
10793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794{
10795#ifdef FEAT_CMDHIST
10796 int histype;
10797 char_u *str;
10798 char_u buf[NUMBUFLEN];
10799#endif
10800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 if (check_restricted() || check_secure())
10803 return;
10804#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10806 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 if (histype >= 0)
10808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 if (*str != NUL)
10811 {
10812 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 return;
10815 }
10816 }
10817#endif
10818}
10819
10820/*
10821 * "histdel()" function
10822 */
10823/*ARGSUSED*/
10824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010826 typval_T *argvars;
10827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828{
10829#ifdef FEAT_CMDHIST
10830 int n;
10831 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010832 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010834 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10835 if (str == NULL)
10836 n = 0;
10837 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010839 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010840 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 else
10845 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010846 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847 get_tv_string_buf(&argvars[1], buf));
10848 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851#endif
10852}
10853
10854/*
10855 * "histget()" function
10856 */
10857/*ARGSUSED*/
10858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010860 typval_T *argvars;
10861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862{
10863#ifdef FEAT_CMDHIST
10864 int type;
10865 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010866 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10869 if (str == NULL)
10870 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 {
10873 type = get_histtype(str);
10874 if (argvars[1].v_type == VAR_UNKNOWN)
10875 idx = get_history_idx(type);
10876 else
10877 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10878 /* -1 on type error */
10879 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885}
10886
10887/*
10888 * "histnr()" function
10889 */
10890/*ARGSUSED*/
10891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 typval_T *argvars;
10894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895{
10896 int i;
10897
10898#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 char_u *history = get_tv_string_chk(&argvars[0]);
10900
10901 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 if (i >= HIST_CMD && i < HIST_COUNT)
10903 i = get_history_idx(i);
10904 else
10905#endif
10906 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908}
10909
10910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 * "highlightID(name)" function
10912 */
10913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010915 typval_T *argvars;
10916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919}
10920
10921/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010922 * "highlight_exists()" function
10923 */
10924 static void
10925f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010926 typval_T *argvars;
10927 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010928{
10929 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10930}
10931
10932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 * "hostname()" function
10934 */
10935/*ARGSUSED*/
10936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010938 typval_T *argvars;
10939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940{
10941 char_u hostname[256];
10942
10943 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944 rettv->v_type = VAR_STRING;
10945 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946}
10947
10948/*
10949 * iconv() function
10950 */
10951/*ARGSUSED*/
10952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010954 typval_T *argvars;
10955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956{
10957#ifdef FEAT_MBYTE
10958 char_u buf1[NUMBUFLEN];
10959 char_u buf2[NUMBUFLEN];
10960 char_u *from, *to, *str;
10961 vimconv_T vimconv;
10962#endif
10963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 rettv->v_type = VAR_STRING;
10965 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966
10967#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968 str = get_tv_string(&argvars[0]);
10969 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10970 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 vimconv.vc_type = CONV_NONE;
10972 convert_setup(&vimconv, from, to);
10973
10974 /* If the encodings are equal, no conversion needed. */
10975 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979
10980 convert_setup(&vimconv, NULL, NULL);
10981 vim_free(from);
10982 vim_free(to);
10983#endif
10984}
10985
10986/*
10987 * "indent()" function
10988 */
10989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010991 typval_T *argvars;
10992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993{
10994 linenr_T lnum;
10995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001}
11002
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011003/*
11004 * "index()" function
11005 */
11006 static void
11007f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 typval_T *argvars;
11009 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011010{
Bram Moolenaar33570922005-01-25 22:26:29 +000011011 list_T *l;
11012 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011013 long idx = 0;
11014 int ic = FALSE;
11015
11016 rettv->vval.v_number = -1;
11017 if (argvars[0].v_type != VAR_LIST)
11018 {
11019 EMSG(_(e_listreq));
11020 return;
11021 }
11022 l = argvars[0].vval.v_list;
11023 if (l != NULL)
11024 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011025 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011026 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011027 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011028 int error = FALSE;
11029
Bram Moolenaar758711c2005-02-02 23:11:38 +000011030 /* Start at specified item. Use the cached index that list_find()
11031 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011033 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011034 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011035 ic = get_tv_number_chk(&argvars[3], &error);
11036 if (error)
11037 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011038 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011039
Bram Moolenaar758711c2005-02-02 23:11:38 +000011040 for ( ; item != NULL; item = item->li_next, ++idx)
11041 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011042 {
11043 rettv->vval.v_number = idx;
11044 break;
11045 }
11046 }
11047}
11048
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049static int inputsecret_flag = 0;
11050
11051/*
11052 * "input()" function
11053 * Also handles inputsecret() when inputsecret is set.
11054 */
11055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011057 typval_T *argvars;
11058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011060 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 char_u *p = NULL;
11062 int c;
11063 char_u buf[NUMBUFLEN];
11064 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011066 int xp_type = EXPAND_NOTHING;
11067 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070
11071#ifdef NO_CONSOLE_INPUT
11072 /* While starting up, there is no place to enter text. */
11073 if (no_console_input())
11074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 return;
11077 }
11078#endif
11079
11080 cmd_silent = FALSE; /* Want to see the prompt. */
11081 if (prompt != NULL)
11082 {
11083 /* Only the part of the message after the last NL is considered as
11084 * prompt for the command line */
11085 p = vim_strrchr(prompt, '\n');
11086 if (p == NULL)
11087 p = prompt;
11088 else
11089 {
11090 ++p;
11091 c = *p;
11092 *p = NUL;
11093 msg_start();
11094 msg_clr_eos();
11095 msg_puts_attr(prompt, echo_attr);
11096 msg_didout = FALSE;
11097 msg_starthere();
11098 *p = c;
11099 }
11100 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 if (argvars[1].v_type != VAR_UNKNOWN)
11103 {
11104 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11105 if (defstr != NULL)
11106 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107
Bram Moolenaar4463f292005-09-25 22:20:24 +000011108 if (argvars[2].v_type != VAR_UNKNOWN)
11109 {
11110 char_u *xp_name;
11111 int xp_namelen;
11112 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011113
Bram Moolenaar4463f292005-09-25 22:20:24 +000011114 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011115
Bram Moolenaar4463f292005-09-25 22:20:24 +000011116 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11117 if (xp_name == NULL)
11118 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011119
Bram Moolenaar4463f292005-09-25 22:20:24 +000011120 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011121
Bram Moolenaar4463f292005-09-25 22:20:24 +000011122 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11123 &xp_arg) == FAIL)
11124 return;
11125 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011126 }
11127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 if (defstr != NULL)
11129 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011130 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11131 xp_type, xp_arg);
11132
11133 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 /* since the user typed this, no need to wait for return */
11136 need_wait_return = FALSE;
11137 msg_didout = FALSE;
11138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 cmd_silent = cmd_silent_save;
11140}
11141
11142/*
11143 * "inputdialog()" function
11144 */
11145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011147 typval_T *argvars;
11148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149{
11150#if defined(FEAT_GUI_TEXTDIALOG)
11151 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11152 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11153 {
11154 char_u *message;
11155 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011158 message = get_tv_string_chk(&argvars[0]);
11159 if (argvars[1].v_type != VAR_UNKNOWN
11160 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011161 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 else
11163 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011164 if (message != NULL && defstr != NULL
11165 && do_dialog(VIM_QUESTION, NULL, message,
11166 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 else
11169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011170 if (message != NULL && defstr != NULL
11171 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011172 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 rettv->vval.v_string = vim_strsave(
11174 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 }
11180 else
11181#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183}
11184
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011185/*
11186 * "inputlist()" function
11187 */
11188 static void
11189f_inputlist(argvars, rettv)
11190 typval_T *argvars;
11191 typval_T *rettv;
11192{
11193 listitem_T *li;
11194 int selected;
11195 int mouse_used;
11196
11197 rettv->vval.v_number = 0;
11198#ifdef NO_CONSOLE_INPUT
11199 /* While starting up, there is no place to enter text. */
11200 if (no_console_input())
11201 return;
11202#endif
11203 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11204 {
11205 EMSG2(_(e_listarg), "inputlist()");
11206 return;
11207 }
11208
11209 msg_start();
11210 lines_left = Rows; /* avoid more prompt */
11211 msg_scroll = TRUE;
11212 msg_clr_eos();
11213
11214 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11215 {
11216 msg_puts(get_tv_string(&li->li_tv));
11217 msg_putchar('\n');
11218 }
11219
11220 /* Ask for choice. */
11221 selected = prompt_for_number(&mouse_used);
11222 if (mouse_used)
11223 selected -= lines_left;
11224
11225 rettv->vval.v_number = selected;
11226}
11227
11228
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11230
11231/*
11232 * "inputrestore()" function
11233 */
11234/*ARGSUSED*/
11235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011237 typval_T *argvars;
11238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239{
11240 if (ga_userinput.ga_len > 0)
11241 {
11242 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11244 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 }
11247 else if (p_verbose > 1)
11248 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011249 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 }
11252}
11253
11254/*
11255 * "inputsave()" function
11256 */
11257/*ARGSUSED*/
11258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262{
11263 /* Add an entry to the stack of typehead storage. */
11264 if (ga_grow(&ga_userinput, 1) == OK)
11265 {
11266 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11267 + ga_userinput.ga_len);
11268 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 }
11271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273}
11274
11275/*
11276 * "inputsecret()" function
11277 */
11278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011280 typval_T *argvars;
11281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282{
11283 ++cmdline_star;
11284 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 --cmdline_star;
11287 --inputsecret_flag;
11288}
11289
11290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011291 * "insert()" function
11292 */
11293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011295 typval_T *argvars;
11296 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011297{
11298 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011299 listitem_T *item;
11300 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011302
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011303 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011304 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011305 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011306 else if ((l = argvars[0].vval.v_list) != NULL
11307 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011308 {
11309 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011310 before = get_tv_number_chk(&argvars[2], &error);
11311 if (error)
11312 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011313
Bram Moolenaar758711c2005-02-02 23:11:38 +000011314 if (before == l->lv_len)
11315 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011316 else
11317 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011318 item = list_find(l, before);
11319 if (item == NULL)
11320 {
11321 EMSGN(_(e_listidx), before);
11322 l = NULL;
11323 }
11324 }
11325 if (l != NULL)
11326 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011327 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011328 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011329 }
11330 }
11331}
11332
11333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 * "isdirectory()" function
11335 */
11336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011338 typval_T *argvars;
11339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342}
11343
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011344/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011345 * "islocked()" function
11346 */
11347 static void
11348f_islocked(argvars, rettv)
11349 typval_T *argvars;
11350 typval_T *rettv;
11351{
11352 lval_T lv;
11353 char_u *end;
11354 dictitem_T *di;
11355
11356 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011357 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11358 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011359 if (end != NULL && lv.ll_name != NULL)
11360 {
11361 if (*end != NUL)
11362 EMSG(_(e_trailing));
11363 else
11364 {
11365 if (lv.ll_tv == NULL)
11366 {
11367 if (check_changedtick(lv.ll_name))
11368 rettv->vval.v_number = 1; /* always locked */
11369 else
11370 {
11371 di = find_var(lv.ll_name, NULL);
11372 if (di != NULL)
11373 {
11374 /* Consider a variable locked when:
11375 * 1. the variable itself is locked
11376 * 2. the value of the variable is locked.
11377 * 3. the List or Dict value is locked.
11378 */
11379 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11380 || tv_islocked(&di->di_tv));
11381 }
11382 }
11383 }
11384 else if (lv.ll_range)
11385 EMSG(_("E745: Range not allowed"));
11386 else if (lv.ll_newkey != NULL)
11387 EMSG2(_(e_dictkey), lv.ll_newkey);
11388 else if (lv.ll_list != NULL)
11389 /* List item. */
11390 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11391 else
11392 /* Dictionary item. */
11393 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11394 }
11395 }
11396
11397 clear_lval(&lv);
11398}
11399
Bram Moolenaar33570922005-01-25 22:26:29 +000011400static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011401
11402/*
11403 * Turn a dict into a list:
11404 * "what" == 0: list of keys
11405 * "what" == 1: list of values
11406 * "what" == 2: list of items
11407 */
11408 static void
11409dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011410 typval_T *argvars;
11411 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011412 int what;
11413{
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 list_T *l2;
11415 dictitem_T *di;
11416 hashitem_T *hi;
11417 listitem_T *li;
11418 listitem_T *li2;
11419 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011420 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011421
11422 rettv->vval.v_number = 0;
11423 if (argvars[0].v_type != VAR_DICT)
11424 {
11425 EMSG(_(e_dictreq));
11426 return;
11427 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011428 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011429 return;
11430
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011431 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011432 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011433
Bram Moolenaar33570922005-01-25 22:26:29 +000011434 todo = d->dv_hashtab.ht_used;
11435 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011437 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011439 --todo;
11440 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011441
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011442 li = listitem_alloc();
11443 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011444 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011445 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011446
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011447 if (what == 0)
11448 {
11449 /* keys() */
11450 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011451 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011452 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11453 }
11454 else if (what == 1)
11455 {
11456 /* values() */
11457 copy_tv(&di->di_tv, &li->li_tv);
11458 }
11459 else
11460 {
11461 /* items() */
11462 l2 = list_alloc();
11463 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011464 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011465 li->li_tv.vval.v_list = l2;
11466 if (l2 == NULL)
11467 break;
11468 ++l2->lv_refcount;
11469
11470 li2 = listitem_alloc();
11471 if (li2 == NULL)
11472 break;
11473 list_append(l2, li2);
11474 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011475 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011476 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11477
11478 li2 = listitem_alloc();
11479 if (li2 == NULL)
11480 break;
11481 list_append(l2, li2);
11482 copy_tv(&di->di_tv, &li2->li_tv);
11483 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011484 }
11485 }
11486}
11487
11488/*
11489 * "items(dict)" function
11490 */
11491 static void
11492f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 typval_T *argvars;
11494 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011495{
11496 dict_list(argvars, rettv, 2);
11497}
11498
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011500 * "join()" function
11501 */
11502 static void
11503f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011504 typval_T *argvars;
11505 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011506{
11507 garray_T ga;
11508 char_u *sep;
11509
11510 rettv->vval.v_number = 0;
11511 if (argvars[0].v_type != VAR_LIST)
11512 {
11513 EMSG(_(e_listreq));
11514 return;
11515 }
11516 if (argvars[0].vval.v_list == NULL)
11517 return;
11518 if (argvars[1].v_type == VAR_UNKNOWN)
11519 sep = (char_u *)" ";
11520 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011522
11523 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011524
11525 if (sep != NULL)
11526 {
11527 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011528 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011529 ga_append(&ga, NUL);
11530 rettv->vval.v_string = (char_u *)ga.ga_data;
11531 }
11532 else
11533 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011534}
11535
11536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011537 * "keys()" function
11538 */
11539 static void
11540f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011541 typval_T *argvars;
11542 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011543{
11544 dict_list(argvars, rettv, 0);
11545}
11546
11547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 * "last_buffer_nr()" function.
11549 */
11550/*ARGSUSED*/
11551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011553 typval_T *argvars;
11554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555{
11556 int n = 0;
11557 buf_T *buf;
11558
11559 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11560 if (n < buf->b_fnum)
11561 n = buf->b_fnum;
11562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011564}
11565
11566/*
11567 * "len()" function
11568 */
11569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *argvars;
11572 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011573{
11574 switch (argvars[0].v_type)
11575 {
11576 case VAR_STRING:
11577 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578 rettv->vval.v_number = (varnumber_T)STRLEN(
11579 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011580 break;
11581 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011583 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011584 case VAR_DICT:
11585 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11586 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011587 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011588 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011589 break;
11590 }
11591}
11592
Bram Moolenaar33570922005-01-25 22:26:29 +000011593static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011594
11595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011597 typval_T *argvars;
11598 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011599 int type;
11600{
11601#ifdef FEAT_LIBCALL
11602 char_u *string_in;
11603 char_u **string_result;
11604 int nr_result;
11605#endif
11606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011608 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011612
11613 if (check_restricted() || check_secure())
11614 return;
11615
11616#ifdef FEAT_LIBCALL
11617 /* The first two args must be strings, otherwise its meaningless */
11618 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11619 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011620 string_in = NULL;
11621 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011622 string_in = argvars[2].vval.v_string;
11623 if (type == VAR_NUMBER)
11624 string_result = NULL;
11625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011626 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011627 if (mch_libcall(argvars[0].vval.v_string,
11628 argvars[1].vval.v_string,
11629 string_in,
11630 argvars[2].vval.v_number,
11631 string_result,
11632 &nr_result) == OK
11633 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011634 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011635 }
11636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637}
11638
11639/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011640 * "libcall()" function
11641 */
11642 static void
11643f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011644 typval_T *argvars;
11645 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011646{
11647 libcall_common(argvars, rettv, VAR_STRING);
11648}
11649
11650/*
11651 * "libcallnr()" function
11652 */
11653 static void
11654f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 typval_T *argvars;
11656 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011657{
11658 libcall_common(argvars, rettv, VAR_NUMBER);
11659}
11660
11661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 * "line(string)" function
11663 */
11664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011666 typval_T *argvars;
11667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668{
11669 linenr_T lnum = 0;
11670 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011671 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011673 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 if (fp != NULL)
11675 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677}
11678
11679/*
11680 * "line2byte(lnum)" function
11681 */
11682/*ARGSUSED*/
11683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011685 typval_T *argvars;
11686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687{
11688#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690#else
11691 linenr_T lnum;
11692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011693 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011697 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11698 if (rettv->vval.v_number >= 0)
11699 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700#endif
11701}
11702
11703/*
11704 * "lispindent(lnum)" function
11705 */
11706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011708 typval_T *argvars;
11709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710{
11711#ifdef FEAT_LISP
11712 pos_T pos;
11713 linenr_T lnum;
11714
11715 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11718 {
11719 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 curwin->w_cursor = pos;
11722 }
11723 else
11724#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726}
11727
11728/*
11729 * "localtime()" function
11730 */
11731/*ARGSUSED*/
11732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011734 typval_T *argvars;
11735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738}
11739
Bram Moolenaar33570922005-01-25 22:26:29 +000011740static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741
11742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011744 typval_T *argvars;
11745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 int exact;
11747{
11748 char_u *keys;
11749 char_u *which;
11750 char_u buf[NUMBUFLEN];
11751 char_u *keys_buf = NULL;
11752 char_u *rhs;
11753 int mode;
11754 garray_T ga;
11755
11756 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757 rettv->v_type = VAR_STRING;
11758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 if (*keys == NUL)
11762 return;
11763
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011764 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 else
11767 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011768 if (which == NULL)
11769 return;
11770
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771 mode = get_map_mode(&which, 0);
11772
11773 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011774 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 vim_free(keys_buf);
11776 if (rhs != NULL)
11777 {
11778 ga_init(&ga);
11779 ga.ga_itemsize = 1;
11780 ga.ga_growsize = 40;
11781
11782 while (*rhs != NUL)
11783 ga_concat(&ga, str2special(&rhs, FALSE));
11784
11785 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 }
11788}
11789
11790/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011791 * "map()" function
11792 */
11793 static void
11794f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011795 typval_T *argvars;
11796 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011797{
11798 filter_map(argvars, rettv, TRUE);
11799}
11800
11801/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011802 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 */
11804 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011805f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 typval_T *argvars;
11807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011809 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810}
11811
11812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011813 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 */
11815 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011816f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011817 typval_T *argvars;
11818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011820 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821}
11822
Bram Moolenaar33570922005-01-25 22:26:29 +000011823static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824
11825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011827 typval_T *argvars;
11828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 int type;
11830{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011831 char_u *str = NULL;
11832 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 char_u *pat;
11834 regmatch_T regmatch;
11835 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011836 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 char_u *save_cpo;
11838 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011839 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011840 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011841 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011842 list_T *l = NULL;
11843 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011844 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011845 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846
11847 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11848 save_cpo = p_cpo;
11849 p_cpo = (char_u *)"";
11850
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011851 rettv->vval.v_number = -1;
11852 if (type == 3)
11853 {
11854 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011855 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011856 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011857 }
11858 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 rettv->v_type = VAR_STRING;
11861 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011864 if (argvars[0].v_type == VAR_LIST)
11865 {
11866 if ((l = argvars[0].vval.v_list) == NULL)
11867 goto theend;
11868 li = l->lv_first;
11869 }
11870 else
11871 expr = str = get_tv_string(&argvars[0]);
11872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011873 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11874 if (pat == NULL)
11875 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011876
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011877 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011879 int error = FALSE;
11880
11881 start = get_tv_number_chk(&argvars[2], &error);
11882 if (error)
11883 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011884 if (l != NULL)
11885 {
11886 li = list_find(l, start);
11887 if (li == NULL)
11888 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011889 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011890 }
11891 else
11892 {
11893 if (start < 0)
11894 start = 0;
11895 if (start > (long)STRLEN(str))
11896 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011897 /* When "count" argument is there ignore matches before "start",
11898 * otherwise skip part of the string. Differs when pattern is "^"
11899 * or "\<". */
11900 if (argvars[3].v_type != VAR_UNKNOWN)
11901 startcol = start;
11902 else
11903 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011904 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011905
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011906 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 nth = get_tv_number_chk(&argvars[3], &error);
11908 if (error)
11909 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 }
11911
11912 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11913 if (regmatch.regprog != NULL)
11914 {
11915 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011916
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011917 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011918 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011919 if (l != NULL)
11920 {
11921 if (li == NULL)
11922 {
11923 match = FALSE;
11924 break;
11925 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011926 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011927 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011928 if (str == NULL)
11929 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011930 }
11931
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011932 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011933
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011934 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011935 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011936 if (l == NULL && !match)
11937 break;
11938
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011939 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011940 if (l != NULL)
11941 {
11942 li = li->li_next;
11943 ++idx;
11944 }
11945 else
11946 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011947#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011948 startcol = regmatch.startp[0]
11949 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011950#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011951 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011952#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011953 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011954 }
11955
11956 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011958 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011959 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011960 int i;
11961
11962 /* return list with matched string and submatches */
11963 for (i = 0; i < NSUBEXP; ++i)
11964 {
11965 if (regmatch.endp[i] == NULL)
11966 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011967 if (list_append_string(rettv->vval.v_list,
11968 regmatch.startp[i],
11969 (int)(regmatch.endp[i] - regmatch.startp[i]))
11970 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011971 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011972 }
11973 }
11974 else if (type == 2)
11975 {
11976 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011977 if (l != NULL)
11978 copy_tv(&li->li_tv, rettv);
11979 else
11980 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011982 }
11983 else if (l != NULL)
11984 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 else
11986 {
11987 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 (varnumber_T)(regmatch.startp[0] - str);
11990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 }
11995 }
11996 vim_free(regmatch.regprog);
11997 }
11998
11999theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012000 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 p_cpo = save_cpo;
12002}
12003
12004/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012005 * "match()" function
12006 */
12007 static void
12008f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012009 typval_T *argvars;
12010 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012011{
12012 find_some_match(argvars, rettv, 1);
12013}
12014
12015/*
12016 * "matchend()" function
12017 */
12018 static void
12019f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T *argvars;
12021 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012022{
12023 find_some_match(argvars, rettv, 0);
12024}
12025
12026/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012027 * "matchlist()" function
12028 */
12029 static void
12030f_matchlist(argvars, rettv)
12031 typval_T *argvars;
12032 typval_T *rettv;
12033{
12034 find_some_match(argvars, rettv, 3);
12035}
12036
12037/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012038 * "matchstr()" function
12039 */
12040 static void
12041f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 typval_T *argvars;
12043 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012044{
12045 find_some_match(argvars, rettv, 2);
12046}
12047
Bram Moolenaar33570922005-01-25 22:26:29 +000012048static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012049
12050 static void
12051max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012054 int domax;
12055{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012056 long n = 0;
12057 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012059
12060 if (argvars[0].v_type == VAR_LIST)
12061 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012062 list_T *l;
12063 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012064
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012065 l = argvars[0].vval.v_list;
12066 if (l != NULL)
12067 {
12068 li = l->lv_first;
12069 if (li != NULL)
12070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012071 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012072 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012073 {
12074 li = li->li_next;
12075 if (li == NULL)
12076 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012078 if (domax ? i > n : i < n)
12079 n = i;
12080 }
12081 }
12082 }
12083 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012084 else if (argvars[0].v_type == VAR_DICT)
12085 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012086 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012087 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012088 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012089 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012090
12091 d = argvars[0].vval.v_dict;
12092 if (d != NULL)
12093 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 todo = d->dv_hashtab.ht_used;
12095 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012096 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012097 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012099 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012100 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012101 if (first)
12102 {
12103 n = i;
12104 first = FALSE;
12105 }
12106 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012107 n = i;
12108 }
12109 }
12110 }
12111 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012112 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012113 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012114 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012115}
12116
12117/*
12118 * "max()" function
12119 */
12120 static void
12121f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012122 typval_T *argvars;
12123 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012124{
12125 max_min(argvars, rettv, TRUE);
12126}
12127
12128/*
12129 * "min()" function
12130 */
12131 static void
12132f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012133 typval_T *argvars;
12134 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012135{
12136 max_min(argvars, rettv, FALSE);
12137}
12138
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012139static int mkdir_recurse __ARGS((char_u *dir, int prot));
12140
12141/*
12142 * Create the directory in which "dir" is located, and higher levels when
12143 * needed.
12144 */
12145 static int
12146mkdir_recurse(dir, prot)
12147 char_u *dir;
12148 int prot;
12149{
12150 char_u *p;
12151 char_u *updir;
12152 int r = FAIL;
12153
12154 /* Get end of directory name in "dir".
12155 * We're done when it's "/" or "c:/". */
12156 p = gettail_sep(dir);
12157 if (p <= get_past_head(dir))
12158 return OK;
12159
12160 /* If the directory exists we're done. Otherwise: create it.*/
12161 updir = vim_strnsave(dir, (int)(p - dir));
12162 if (updir == NULL)
12163 return FAIL;
12164 if (mch_isdir(updir))
12165 r = OK;
12166 else if (mkdir_recurse(updir, prot) == OK)
12167 r = vim_mkdir_emsg(updir, prot);
12168 vim_free(updir);
12169 return r;
12170}
12171
12172#ifdef vim_mkdir
12173/*
12174 * "mkdir()" function
12175 */
12176 static void
12177f_mkdir(argvars, rettv)
12178 typval_T *argvars;
12179 typval_T *rettv;
12180{
12181 char_u *dir;
12182 char_u buf[NUMBUFLEN];
12183 int prot = 0755;
12184
12185 rettv->vval.v_number = FAIL;
12186 if (check_restricted() || check_secure())
12187 return;
12188
12189 dir = get_tv_string_buf(&argvars[0], buf);
12190 if (argvars[1].v_type != VAR_UNKNOWN)
12191 {
12192 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012193 prot = get_tv_number_chk(&argvars[2], NULL);
12194 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012195 mkdir_recurse(dir, prot);
12196 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012197 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012198}
12199#endif
12200
Bram Moolenaar0d660222005-01-07 21:51:51 +000012201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 * "mode()" function
12203 */
12204/*ARGSUSED*/
12205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012207 typval_T *argvars;
12208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209{
12210 char_u buf[2];
12211
12212#ifdef FEAT_VISUAL
12213 if (VIsual_active)
12214 {
12215 if (VIsual_select)
12216 buf[0] = VIsual_mode + 's' - 'v';
12217 else
12218 buf[0] = VIsual_mode;
12219 }
12220 else
12221#endif
12222 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12223 buf[0] = 'r';
12224 else if (State & INSERT)
12225 {
12226 if (State & REPLACE_FLAG)
12227 buf[0] = 'R';
12228 else
12229 buf[0] = 'i';
12230 }
12231 else if (State & CMDLINE)
12232 buf[0] = 'c';
12233 else
12234 buf[0] = 'n';
12235
12236 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237 rettv->vval.v_string = vim_strsave(buf);
12238 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239}
12240
12241/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012242 * "nextnonblank()" function
12243 */
12244 static void
12245f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012246 typval_T *argvars;
12247 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012248{
12249 linenr_T lnum;
12250
12251 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012253 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012254 {
12255 lnum = 0;
12256 break;
12257 }
12258 if (*skipwhite(ml_get(lnum)) != NUL)
12259 break;
12260 }
12261 rettv->vval.v_number = lnum;
12262}
12263
12264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 * "nr2char()" function
12266 */
12267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012269 typval_T *argvars;
12270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271{
12272 char_u buf[NUMBUFLEN];
12273
12274#ifdef FEAT_MBYTE
12275 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012276 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 else
12278#endif
12279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012280 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 buf[1] = NUL;
12282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283 rettv->v_type = VAR_STRING;
12284 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012285}
12286
12287/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012288 * "prevnonblank()" function
12289 */
12290 static void
12291f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012292 typval_T *argvars;
12293 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012294{
12295 linenr_T lnum;
12296
12297 lnum = get_tv_lnum(argvars);
12298 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12299 lnum = 0;
12300 else
12301 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12302 --lnum;
12303 rettv->vval.v_number = lnum;
12304}
12305
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012306#ifdef HAVE_STDARG_H
12307/* This dummy va_list is here because:
12308 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12309 * - locally in the function results in a "used before set" warning
12310 * - using va_start() to initialize it gives "function with fixed args" error */
12311static va_list ap;
12312#endif
12313
Bram Moolenaar8c711452005-01-14 21:53:12 +000012314/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012315 * "printf()" function
12316 */
12317 static void
12318f_printf(argvars, rettv)
12319 typval_T *argvars;
12320 typval_T *rettv;
12321{
12322 rettv->v_type = VAR_STRING;
12323 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012324#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012325 {
12326 char_u buf[NUMBUFLEN];
12327 int len;
12328 char_u *s;
12329 int saved_did_emsg = did_emsg;
12330 char *fmt;
12331
12332 /* Get the required length, allocate the buffer and do it for real. */
12333 did_emsg = FALSE;
12334 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012335 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012336 if (!did_emsg)
12337 {
12338 s = alloc(len + 1);
12339 if (s != NULL)
12340 {
12341 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012342 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012343 }
12344 }
12345 did_emsg |= saved_did_emsg;
12346 }
12347#endif
12348}
12349
12350/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012351 * "pumvisible()" function
12352 */
12353/*ARGSUSED*/
12354 static void
12355f_pumvisible(argvars, rettv)
12356 typval_T *argvars;
12357 typval_T *rettv;
12358{
12359 rettv->vval.v_number = 0;
12360#ifdef FEAT_INS_EXPAND
12361 if (pum_visible())
12362 rettv->vval.v_number = 1;
12363#endif
12364}
12365
12366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012367 * "range()" function
12368 */
12369 static void
12370f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012371 typval_T *argvars;
12372 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012373{
12374 long start;
12375 long end;
12376 long stride = 1;
12377 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012378 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012380 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012381 if (argvars[1].v_type == VAR_UNKNOWN)
12382 {
12383 end = start - 1;
12384 start = 0;
12385 }
12386 else
12387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012389 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012391 }
12392
12393 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012394 if (error)
12395 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012396 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012397 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012398 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012399 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012400 else
12401 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012402 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012403 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012404 if (list_append_number(rettv->vval.v_list,
12405 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012406 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012407 }
12408}
12409
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012410/*
12411 * "readfile()" function
12412 */
12413 static void
12414f_readfile(argvars, rettv)
12415 typval_T *argvars;
12416 typval_T *rettv;
12417{
12418 int binary = FALSE;
12419 char_u *fname;
12420 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012421 listitem_T *li;
12422#define FREAD_SIZE 200 /* optimized for text lines */
12423 char_u buf[FREAD_SIZE];
12424 int readlen; /* size of last fread() */
12425 int buflen; /* nr of valid chars in buf[] */
12426 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12427 int tolist; /* first byte in buf[] still to be put in list */
12428 int chop; /* how many CR to chop off */
12429 char_u *prev = NULL; /* previously read bytes, if any */
12430 int prevlen = 0; /* length of "prev" if not NULL */
12431 char_u *s;
12432 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012433 long maxline = MAXLNUM;
12434 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012435
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012436 if (argvars[1].v_type != VAR_UNKNOWN)
12437 {
12438 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12439 binary = TRUE;
12440 if (argvars[2].v_type != VAR_UNKNOWN)
12441 maxline = get_tv_number(&argvars[2]);
12442 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012443
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012444 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012445 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012446
12447 /* Always open the file in binary mode, library functions have a mind of
12448 * their own about CR-LF conversion. */
12449 fname = get_tv_string(&argvars[0]);
12450 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12451 {
12452 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12453 return;
12454 }
12455
12456 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012457 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012458 {
12459 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12460 buflen = filtd + readlen;
12461 tolist = 0;
12462 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12463 {
12464 if (buf[filtd] == '\n' || readlen <= 0)
12465 {
12466 /* Only when in binary mode add an empty list item when the
12467 * last line ends in a '\n'. */
12468 if (!binary && readlen == 0 && filtd == 0)
12469 break;
12470
12471 /* Found end-of-line or end-of-file: add a text line to the
12472 * list. */
12473 chop = 0;
12474 if (!binary)
12475 while (filtd - chop - 1 >= tolist
12476 && buf[filtd - chop - 1] == '\r')
12477 ++chop;
12478 len = filtd - tolist - chop;
12479 if (prev == NULL)
12480 s = vim_strnsave(buf + tolist, len);
12481 else
12482 {
12483 s = alloc((unsigned)(prevlen + len + 1));
12484 if (s != NULL)
12485 {
12486 mch_memmove(s, prev, prevlen);
12487 vim_free(prev);
12488 prev = NULL;
12489 mch_memmove(s + prevlen, buf + tolist, len);
12490 s[prevlen + len] = NUL;
12491 }
12492 }
12493 tolist = filtd + 1;
12494
12495 li = listitem_alloc();
12496 if (li == NULL)
12497 {
12498 vim_free(s);
12499 break;
12500 }
12501 li->li_tv.v_type = VAR_STRING;
12502 li->li_tv.v_lock = 0;
12503 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012504 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012505
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012506 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012507 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012508 if (readlen <= 0)
12509 break;
12510 }
12511 else if (buf[filtd] == NUL)
12512 buf[filtd] = '\n';
12513 }
12514 if (readlen <= 0)
12515 break;
12516
12517 if (tolist == 0)
12518 {
12519 /* "buf" is full, need to move text to an allocated buffer */
12520 if (prev == NULL)
12521 {
12522 prev = vim_strnsave(buf, buflen);
12523 prevlen = buflen;
12524 }
12525 else
12526 {
12527 s = alloc((unsigned)(prevlen + buflen));
12528 if (s != NULL)
12529 {
12530 mch_memmove(s, prev, prevlen);
12531 mch_memmove(s + prevlen, buf, buflen);
12532 vim_free(prev);
12533 prev = s;
12534 prevlen += buflen;
12535 }
12536 }
12537 filtd = 0;
12538 }
12539 else
12540 {
12541 mch_memmove(buf, buf + tolist, buflen - tolist);
12542 filtd -= tolist;
12543 }
12544 }
12545
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012546 /*
12547 * For a negative line count use only the lines at the end of the file,
12548 * free the rest.
12549 */
12550 if (maxline < 0)
12551 while (cnt > -maxline)
12552 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012553 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012554 --cnt;
12555 }
12556
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012557 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012558 fclose(fd);
12559}
12560
12561
Bram Moolenaar0d660222005-01-07 21:51:51 +000012562#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12563static void make_connection __ARGS((void));
12564static int check_connection __ARGS((void));
12565
12566 static void
12567make_connection()
12568{
12569 if (X_DISPLAY == NULL
12570# ifdef FEAT_GUI
12571 && !gui.in_use
12572# endif
12573 )
12574 {
12575 x_force_connect = TRUE;
12576 setup_term_clip();
12577 x_force_connect = FALSE;
12578 }
12579}
12580
12581 static int
12582check_connection()
12583{
12584 make_connection();
12585 if (X_DISPLAY == NULL)
12586 {
12587 EMSG(_("E240: No connection to Vim server"));
12588 return FAIL;
12589 }
12590 return OK;
12591}
12592#endif
12593
12594#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012595static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012596
12597 static void
12598remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012599 typval_T *argvars;
12600 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012601 int expr;
12602{
12603 char_u *server_name;
12604 char_u *keys;
12605 char_u *r = NULL;
12606 char_u buf[NUMBUFLEN];
12607# ifdef WIN32
12608 HWND w;
12609# else
12610 Window w;
12611# endif
12612
12613 if (check_restricted() || check_secure())
12614 return;
12615
12616# ifdef FEAT_X11
12617 if (check_connection() == FAIL)
12618 return;
12619# endif
12620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012621 server_name = get_tv_string_chk(&argvars[0]);
12622 if (server_name == NULL)
12623 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624 keys = get_tv_string_buf(&argvars[1], buf);
12625# ifdef WIN32
12626 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12627# else
12628 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12629 < 0)
12630# endif
12631 {
12632 if (r != NULL)
12633 EMSG(r); /* sending worked but evaluation failed */
12634 else
12635 EMSG2(_("E241: Unable to send to %s"), server_name);
12636 return;
12637 }
12638
12639 rettv->vval.v_string = r;
12640
12641 if (argvars[2].v_type != VAR_UNKNOWN)
12642 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012643 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012644 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012645 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012646
12647 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012648 v.di_tv.v_type = VAR_STRING;
12649 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012650 idvar = get_tv_string_chk(&argvars[2]);
12651 if (idvar != NULL)
12652 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012653 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012654 }
12655}
12656#endif
12657
12658/*
12659 * "remote_expr()" function
12660 */
12661/*ARGSUSED*/
12662 static void
12663f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012664 typval_T *argvars;
12665 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012666{
12667 rettv->v_type = VAR_STRING;
12668 rettv->vval.v_string = NULL;
12669#ifdef FEAT_CLIENTSERVER
12670 remote_common(argvars, rettv, TRUE);
12671#endif
12672}
12673
12674/*
12675 * "remote_foreground()" function
12676 */
12677/*ARGSUSED*/
12678 static void
12679f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012680 typval_T *argvars;
12681 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012682{
12683 rettv->vval.v_number = 0;
12684#ifdef FEAT_CLIENTSERVER
12685# ifdef WIN32
12686 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012687 {
12688 char_u *server_name = get_tv_string_chk(&argvars[0]);
12689
12690 if (server_name != NULL)
12691 serverForeground(server_name);
12692 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012693# else
12694 /* Send a foreground() expression to the server. */
12695 argvars[1].v_type = VAR_STRING;
12696 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12697 argvars[2].v_type = VAR_UNKNOWN;
12698 remote_common(argvars, rettv, TRUE);
12699 vim_free(argvars[1].vval.v_string);
12700# endif
12701#endif
12702}
12703
12704/*ARGSUSED*/
12705 static void
12706f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012707 typval_T *argvars;
12708 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012709{
12710#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012711 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012712 char_u *s = NULL;
12713# ifdef WIN32
12714 int n = 0;
12715# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012716 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012717
12718 if (check_restricted() || check_secure())
12719 {
12720 rettv->vval.v_number = -1;
12721 return;
12722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 serverid = get_tv_string_chk(&argvars[0]);
12724 if (serverid == NULL)
12725 {
12726 rettv->vval.v_number = -1;
12727 return; /* type error; errmsg already given */
12728 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012729# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012730 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012731 if (n == 0)
12732 rettv->vval.v_number = -1;
12733 else
12734 {
12735 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12736 rettv->vval.v_number = (s != NULL);
12737 }
12738# else
12739 rettv->vval.v_number = 0;
12740 if (check_connection() == FAIL)
12741 return;
12742
12743 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012745# endif
12746
12747 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012749 char_u *retvar;
12750
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 v.di_tv.v_type = VAR_STRING;
12752 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012753 retvar = get_tv_string_chk(&argvars[1]);
12754 if (retvar != NULL)
12755 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012756 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012757 }
12758#else
12759 rettv->vval.v_number = -1;
12760#endif
12761}
12762
12763/*ARGSUSED*/
12764 static void
12765f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 typval_T *argvars;
12767 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012768{
12769 char_u *r = NULL;
12770
12771#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 char_u *serverid = get_tv_string_chk(&argvars[0]);
12773
12774 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012775 {
12776# ifdef WIN32
12777 /* The server's HWND is encoded in the 'id' parameter */
12778 int n = 0;
12779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012780 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012781 if (n != 0)
12782 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12783 if (r == NULL)
12784# else
12785 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012787# endif
12788 EMSG(_("E277: Unable to read a server reply"));
12789 }
12790#endif
12791 rettv->v_type = VAR_STRING;
12792 rettv->vval.v_string = r;
12793}
12794
12795/*
12796 * "remote_send()" function
12797 */
12798/*ARGSUSED*/
12799 static void
12800f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012801 typval_T *argvars;
12802 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012803{
12804 rettv->v_type = VAR_STRING;
12805 rettv->vval.v_string = NULL;
12806#ifdef FEAT_CLIENTSERVER
12807 remote_common(argvars, rettv, FALSE);
12808#endif
12809}
12810
12811/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012812 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012813 */
12814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012816 typval_T *argvars;
12817 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012818{
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 list_T *l;
12820 listitem_T *item, *item2;
12821 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012822 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012823 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012824 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 dict_T *d;
12826 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012827
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012828 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012829 if (argvars[0].v_type == VAR_DICT)
12830 {
12831 if (argvars[2].v_type != VAR_UNKNOWN)
12832 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012833 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012834 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 key = get_tv_string_chk(&argvars[1]);
12837 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012839 di = dict_find(d, key, -1);
12840 if (di == NULL)
12841 EMSG2(_(e_dictkey), key);
12842 else
12843 {
12844 *rettv = di->di_tv;
12845 init_tv(&di->di_tv);
12846 dictitem_remove(d, di);
12847 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012848 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012849 }
12850 }
12851 else if (argvars[0].v_type != VAR_LIST)
12852 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012853 else if ((l = argvars[0].vval.v_list) != NULL
12854 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012856 int error = FALSE;
12857
12858 idx = get_tv_number_chk(&argvars[1], &error);
12859 if (error)
12860 ; /* type error: do nothing, errmsg already given */
12861 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012862 EMSGN(_(e_listidx), idx);
12863 else
12864 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012865 if (argvars[2].v_type == VAR_UNKNOWN)
12866 {
12867 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012868 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012869 *rettv = item->li_tv;
12870 vim_free(item);
12871 }
12872 else
12873 {
12874 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012875 end = get_tv_number_chk(&argvars[2], &error);
12876 if (error)
12877 ; /* type error: do nothing */
12878 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012879 EMSGN(_(e_listidx), end);
12880 else
12881 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012882 int cnt = 0;
12883
12884 for (li = item; li != NULL; li = li->li_next)
12885 {
12886 ++cnt;
12887 if (li == item2)
12888 break;
12889 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012890 if (li == NULL) /* didn't find "item2" after "item" */
12891 EMSG(_(e_invrange));
12892 else
12893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012894 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012895 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012896 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012897 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012898 l->lv_first = item;
12899 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012900 item->li_prev = NULL;
12901 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012902 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012903 }
12904 }
12905 }
12906 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012907 }
12908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909}
12910
12911/*
12912 * "rename({from}, {to})" function
12913 */
12914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *argvars;
12917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918{
12919 char_u buf[NUMBUFLEN];
12920
12921 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012924 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12925 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926}
12927
12928/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012929 * "repeat()" function
12930 */
12931/*ARGSUSED*/
12932 static void
12933f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012934 typval_T *argvars;
12935 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012936{
12937 char_u *p;
12938 int n;
12939 int slen;
12940 int len;
12941 char_u *r;
12942 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012943
12944 n = get_tv_number(&argvars[1]);
12945 if (argvars[0].v_type == VAR_LIST)
12946 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012947 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012948 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012949 if (list_extend(rettv->vval.v_list,
12950 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012951 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012952 }
12953 else
12954 {
12955 p = get_tv_string(&argvars[0]);
12956 rettv->v_type = VAR_STRING;
12957 rettv->vval.v_string = NULL;
12958
12959 slen = (int)STRLEN(p);
12960 len = slen * n;
12961 if (len <= 0)
12962 return;
12963
12964 r = alloc(len + 1);
12965 if (r != NULL)
12966 {
12967 for (i = 0; i < n; i++)
12968 mch_memmove(r + i * slen, p, (size_t)slen);
12969 r[len] = NUL;
12970 }
12971
12972 rettv->vval.v_string = r;
12973 }
12974}
12975
12976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977 * "resolve()" function
12978 */
12979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012981 typval_T *argvars;
12982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983{
12984 char_u *p;
12985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987#ifdef FEAT_SHORTCUT
12988 {
12989 char_u *v = NULL;
12990
12991 v = mch_resolve_shortcut(p);
12992 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012993 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996 }
12997#else
12998# ifdef HAVE_READLINK
12999 {
13000 char_u buf[MAXPATHL + 1];
13001 char_u *cpy;
13002 int len;
13003 char_u *remain = NULL;
13004 char_u *q;
13005 int is_relative_to_current = FALSE;
13006 int has_trailing_pathsep = FALSE;
13007 int limit = 100;
13008
13009 p = vim_strsave(p);
13010
13011 if (p[0] == '.' && (vim_ispathsep(p[1])
13012 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13013 is_relative_to_current = TRUE;
13014
13015 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013016 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 has_trailing_pathsep = TRUE;
13018
13019 q = getnextcomp(p);
13020 if (*q != NUL)
13021 {
13022 /* Separate the first path component in "p", and keep the
13023 * remainder (beginning with the path separator). */
13024 remain = vim_strsave(q - 1);
13025 q[-1] = NUL;
13026 }
13027
13028 for (;;)
13029 {
13030 for (;;)
13031 {
13032 len = readlink((char *)p, (char *)buf, MAXPATHL);
13033 if (len <= 0)
13034 break;
13035 buf[len] = NUL;
13036
13037 if (limit-- == 0)
13038 {
13039 vim_free(p);
13040 vim_free(remain);
13041 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 goto fail;
13044 }
13045
13046 /* Ensure that the result will have a trailing path separator
13047 * if the argument has one. */
13048 if (remain == NULL && has_trailing_pathsep)
13049 add_pathsep(buf);
13050
13051 /* Separate the first path component in the link value and
13052 * concatenate the remainders. */
13053 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13054 if (*q != NUL)
13055 {
13056 if (remain == NULL)
13057 remain = vim_strsave(q - 1);
13058 else
13059 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013060 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 if (cpy != NULL)
13062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 vim_free(remain);
13064 remain = cpy;
13065 }
13066 }
13067 q[-1] = NUL;
13068 }
13069
13070 q = gettail(p);
13071 if (q > p && *q == NUL)
13072 {
13073 /* Ignore trailing path separator. */
13074 q[-1] = NUL;
13075 q = gettail(p);
13076 }
13077 if (q > p && !mch_isFullName(buf))
13078 {
13079 /* symlink is relative to directory of argument */
13080 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13081 if (cpy != NULL)
13082 {
13083 STRCPY(cpy, p);
13084 STRCPY(gettail(cpy), buf);
13085 vim_free(p);
13086 p = cpy;
13087 }
13088 }
13089 else
13090 {
13091 vim_free(p);
13092 p = vim_strsave(buf);
13093 }
13094 }
13095
13096 if (remain == NULL)
13097 break;
13098
13099 /* Append the first path component of "remain" to "p". */
13100 q = getnextcomp(remain + 1);
13101 len = q - remain - (*q != NUL);
13102 cpy = vim_strnsave(p, STRLEN(p) + len);
13103 if (cpy != NULL)
13104 {
13105 STRNCAT(cpy, remain, len);
13106 vim_free(p);
13107 p = cpy;
13108 }
13109 /* Shorten "remain". */
13110 if (*q != NUL)
13111 STRCPY(remain, q - 1);
13112 else
13113 {
13114 vim_free(remain);
13115 remain = NULL;
13116 }
13117 }
13118
13119 /* If the result is a relative path name, make it explicitly relative to
13120 * the current directory if and only if the argument had this form. */
13121 if (!vim_ispathsep(*p))
13122 {
13123 if (is_relative_to_current
13124 && *p != NUL
13125 && !(p[0] == '.'
13126 && (p[1] == NUL
13127 || vim_ispathsep(p[1])
13128 || (p[1] == '.'
13129 && (p[2] == NUL
13130 || vim_ispathsep(p[2]))))))
13131 {
13132 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013133 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 if (cpy != NULL)
13135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136 vim_free(p);
13137 p = cpy;
13138 }
13139 }
13140 else if (!is_relative_to_current)
13141 {
13142 /* Strip leading "./". */
13143 q = p;
13144 while (q[0] == '.' && vim_ispathsep(q[1]))
13145 q += 2;
13146 if (q > p)
13147 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13148 }
13149 }
13150
13151 /* Ensure that the result will have no trailing path separator
13152 * if the argument had none. But keep "/" or "//". */
13153 if (!has_trailing_pathsep)
13154 {
13155 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013156 if (after_pathsep(p, q))
13157 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 }
13159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161 }
13162# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164# endif
13165#endif
13166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168
13169#ifdef HAVE_READLINK
13170fail:
13171#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173}
13174
13175/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013176 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177 */
13178 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013179f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013180 typval_T *argvars;
13181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182{
Bram Moolenaar33570922005-01-25 22:26:29 +000013183 list_T *l;
13184 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185
Bram Moolenaar0d660222005-01-07 21:51:51 +000013186 rettv->vval.v_number = 0;
13187 if (argvars[0].v_type != VAR_LIST)
13188 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013189 else if ((l = argvars[0].vval.v_list) != NULL
13190 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013191 {
13192 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013193 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013194 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013195 while (li != NULL)
13196 {
13197 ni = li->li_prev;
13198 list_append(l, li);
13199 li = ni;
13200 }
13201 rettv->vval.v_list = l;
13202 rettv->v_type = VAR_LIST;
13203 ++l->lv_refcount;
13204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205}
13206
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013207#define SP_NOMOVE 0x01 /* don't move cursor */
13208#define SP_REPEAT 0x02 /* repeat to find outer pair */
13209#define SP_RETCOUNT 0x04 /* return matchcount */
13210#define SP_SETPCMARK 0x08 /* set previous context mark */
13211#define SP_START 0x10 /* accept match at start position */
13212#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13213#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013214
Bram Moolenaar33570922005-01-25 22:26:29 +000013215static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013216
13217/*
13218 * Get flags for a search function.
13219 * Possibly sets "p_ws".
13220 * Returns BACKWARD, FORWARD or zero (for an error).
13221 */
13222 static int
13223get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013224 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013225 int *flagsp;
13226{
13227 int dir = FORWARD;
13228 char_u *flags;
13229 char_u nbuf[NUMBUFLEN];
13230 int mask;
13231
13232 if (varp->v_type != VAR_UNKNOWN)
13233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013234 flags = get_tv_string_buf_chk(varp, nbuf);
13235 if (flags == NULL)
13236 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013237 while (*flags != NUL)
13238 {
13239 switch (*flags)
13240 {
13241 case 'b': dir = BACKWARD; break;
13242 case 'w': p_ws = TRUE; break;
13243 case 'W': p_ws = FALSE; break;
13244 default: mask = 0;
13245 if (flagsp != NULL)
13246 switch (*flags)
13247 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013248 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013249 case 'e': mask = SP_END; break;
13250 case 'm': mask = SP_RETCOUNT; break;
13251 case 'n': mask = SP_NOMOVE; break;
13252 case 'p': mask = SP_SUBPAT; break;
13253 case 'r': mask = SP_REPEAT; break;
13254 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013255 }
13256 if (mask == 0)
13257 {
13258 EMSG2(_(e_invarg2), flags);
13259 dir = 0;
13260 }
13261 else
13262 *flagsp |= mask;
13263 }
13264 if (dir == 0)
13265 break;
13266 ++flags;
13267 }
13268 }
13269 return dir;
13270}
13271
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013273 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013275 static int
13276search_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013277 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013278 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279{
13280 char_u *pat;
13281 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013282 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 int save_p_ws = p_ws;
13284 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013285 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013286 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013287 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013288 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013289 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013292 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13293 if (dir == 0)
13294 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013295 if (flags & SP_START)
13296 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013297 if (flags & SP_END)
13298 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013299
13300 /* Optional extra argument: line number to stop searching. */
13301 if (argvars[1].v_type != VAR_UNKNOWN
13302 && argvars[2].v_type != VAR_UNKNOWN)
13303 {
13304 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13305 if (lnum_stop < 0)
13306 goto theend;
13307 }
13308
Bram Moolenaar231334e2005-07-25 20:46:57 +000013309 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013310 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013311 * Check to make sure only those flags are set.
13312 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13313 * flags cannot be set. Check for that condition also.
13314 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013315 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013316 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013319 goto theend;
13320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013322 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013323 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13324 options, RE_SEARCH, (linenr_T)lnum_stop);
13325 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013327 if (flags & SP_SUBPAT)
13328 retval = subpatnum;
13329 else
13330 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013331 if (flags & SP_SETPCMARK)
13332 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013334 if (match_pos != NULL)
13335 {
13336 /* Store the match cursor position */
13337 match_pos->lnum = pos.lnum;
13338 match_pos->col = pos.col + 1;
13339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 /* "/$" will put the cursor after the end of the line, may need to
13341 * correct that here */
13342 check_cursor();
13343 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013344
13345 /* If 'n' flag is used: restore cursor position. */
13346 if (flags & SP_NOMOVE)
13347 curwin->w_cursor = save_cursor;
13348theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013350
13351 return retval;
13352}
13353
13354/*
13355 * "search()" function
13356 */
13357 static void
13358f_search(argvars, rettv)
13359 typval_T *argvars;
13360 typval_T *rettv;
13361{
13362 rettv->vval.v_number = search_cmn(argvars, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363}
13364
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013366 * "searchdecl()" function
13367 */
13368 static void
13369f_searchdecl(argvars, rettv)
13370 typval_T *argvars;
13371 typval_T *rettv;
13372{
13373 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013374 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013375 int error = FALSE;
13376 char_u *name;
13377
13378 rettv->vval.v_number = 1; /* default: FAIL */
13379
13380 name = get_tv_string_chk(&argvars[0]);
13381 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013382 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013383 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013384 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13385 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13386 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013387 if (!error && name != NULL)
13388 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013389 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013390}
13391
13392/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013393 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013395 static int
13396searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013397 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013398 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399{
13400 char_u *spat, *mpat, *epat;
13401 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 int dir;
13404 int flags = 0;
13405 char_u nbuf1[NUMBUFLEN];
13406 char_u nbuf2[NUMBUFLEN];
13407 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013408 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013409 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013412 spat = get_tv_string_chk(&argvars[0]);
13413 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13414 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13415 if (spat == NULL || mpat == NULL || epat == NULL)
13416 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 /* Handle the optional fourth argument: flags */
13419 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013420 if (dir == 0)
13421 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013422
13423 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013424 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13425 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013426 if ((flags & (SP_END | SP_SUBPAT)) != 0
13427 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013428 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013429 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013430 goto theend;
13431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013433 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013434 if (argvars[3].v_type == VAR_UNKNOWN
13435 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 skip = (char_u *)"";
13437 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013439 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013440 if (argvars[5].v_type != VAR_UNKNOWN)
13441 {
13442 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13443 if (lnum_stop < 0)
13444 goto theend;
13445 }
13446 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013447 if (skip == NULL)
13448 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013450 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13451 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013452
13453theend:
13454 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013455
13456 return retval;
13457}
13458
13459/*
13460 * "searchpair()" function
13461 */
13462 static void
13463f_searchpair(argvars, rettv)
13464 typval_T *argvars;
13465 typval_T *rettv;
13466{
13467 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13468}
13469
13470/*
13471 * "searchpairpos()" function
13472 */
13473 static void
13474f_searchpairpos(argvars, rettv)
13475 typval_T *argvars;
13476 typval_T *rettv;
13477{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013478 pos_T match_pos;
13479 int lnum = 0;
13480 int col = 0;
13481
13482 rettv->vval.v_number = 0;
13483
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013484 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013485 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013486
13487 if (searchpair_cmn(argvars, &match_pos) > 0)
13488 {
13489 lnum = match_pos.lnum;
13490 col = match_pos.col;
13491 }
13492
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013493 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13494 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013495}
13496
13497/*
13498 * Search for a start/middle/end thing.
13499 * Used by searchpair(), see its documentation for the details.
13500 * Returns 0 or -1 for no match,
13501 */
13502 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013503do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013504 char_u *spat; /* start pattern */
13505 char_u *mpat; /* middle pattern */
13506 char_u *epat; /* end pattern */
13507 int dir; /* BACKWARD or FORWARD */
13508 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013509 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013510 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013511 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013512{
13513 char_u *save_cpo;
13514 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13515 long retval = 0;
13516 pos_T pos;
13517 pos_T firstpos;
13518 pos_T foundpos;
13519 pos_T save_cursor;
13520 pos_T save_pos;
13521 int n;
13522 int r;
13523 int nest = 1;
13524 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013525 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013526
13527 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13528 save_cpo = p_cpo;
13529 p_cpo = (char_u *)"";
13530
13531 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13532 * start/middle/end (pat3, for the top pair). */
13533 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13534 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13535 if (pat2 == NULL || pat3 == NULL)
13536 goto theend;
13537 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13538 if (*mpat == NUL)
13539 STRCPY(pat3, pat2);
13540 else
13541 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13542 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013543 if (flags & SP_START)
13544 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013545
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 save_cursor = curwin->w_cursor;
13547 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013548 clearpos(&firstpos);
13549 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 pat = pat3;
13551 for (;;)
13552 {
13553 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013554 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13556 /* didn't find it or found the first match again: FAIL */
13557 break;
13558
13559 if (firstpos.lnum == 0)
13560 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013561 if (equalpos(pos, foundpos))
13562 {
13563 /* Found the same position again. Can happen with a pattern that
13564 * has "\zs" at the end and searching backwards. Advance one
13565 * character and try again. */
13566 if (dir == BACKWARD)
13567 decl(&pos);
13568 else
13569 incl(&pos);
13570 }
13571 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572
13573 /* If the skip pattern matches, ignore this match. */
13574 if (*skip != NUL)
13575 {
13576 save_pos = curwin->w_cursor;
13577 curwin->w_cursor = pos;
13578 r = eval_to_bool(skip, &err, NULL, FALSE);
13579 curwin->w_cursor = save_pos;
13580 if (err)
13581 {
13582 /* Evaluating {skip} caused an error, break here. */
13583 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013584 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 break;
13586 }
13587 if (r)
13588 continue;
13589 }
13590
13591 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13592 {
13593 /* Found end when searching backwards or start when searching
13594 * forward: nested pair. */
13595 ++nest;
13596 pat = pat2; /* nested, don't search for middle */
13597 }
13598 else
13599 {
13600 /* Found end when searching forward or start when searching
13601 * backward: end of (nested) pair; or found middle in outer pair. */
13602 if (--nest == 1)
13603 pat = pat3; /* outer level, search for middle */
13604 }
13605
13606 if (nest == 0)
13607 {
13608 /* Found the match: return matchcount or line number. */
13609 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013610 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013612 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013613 if (flags & SP_SETPCMARK)
13614 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 curwin->w_cursor = pos;
13616 if (!(flags & SP_REPEAT))
13617 break;
13618 nest = 1; /* search for next unmatched */
13619 }
13620 }
13621
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013622 if (match_pos != NULL)
13623 {
13624 /* Store the match cursor position */
13625 match_pos->lnum = curwin->w_cursor.lnum;
13626 match_pos->col = curwin->w_cursor.col + 1;
13627 }
13628
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013630 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 curwin->w_cursor = save_cursor;
13632
13633theend:
13634 vim_free(pat2);
13635 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013637
13638 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639}
13640
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013641/*
13642 * "searchpos()" function
13643 */
13644 static void
13645f_searchpos(argvars, rettv)
13646 typval_T *argvars;
13647 typval_T *rettv;
13648{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013649 pos_T match_pos;
13650 int lnum = 0;
13651 int col = 0;
13652
13653 rettv->vval.v_number = 0;
13654
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013655 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013656 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013657
13658 if (search_cmn(argvars, &match_pos) > 0)
13659 {
13660 lnum = match_pos.lnum;
13661 col = match_pos.col;
13662 }
13663
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013664 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13665 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013666
13667}
13668
13669
Bram Moolenaar0d660222005-01-07 21:51:51 +000013670/*ARGSUSED*/
13671 static void
13672f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013673 typval_T *argvars;
13674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676#ifdef FEAT_CLIENTSERVER
13677 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013678 char_u *server = get_tv_string_chk(&argvars[0]);
13679 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680
Bram Moolenaar0d660222005-01-07 21:51:51 +000013681 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013682 if (server == NULL || reply == NULL)
13683 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013684 if (check_restricted() || check_secure())
13685 return;
13686# ifdef FEAT_X11
13687 if (check_connection() == FAIL)
13688 return;
13689# endif
13690
13691 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013693 EMSG(_("E258: Unable to send to client"));
13694 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013696 rettv->vval.v_number = 0;
13697#else
13698 rettv->vval.v_number = -1;
13699#endif
13700}
13701
13702/*ARGSUSED*/
13703 static void
13704f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013705 typval_T *argvars;
13706 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013707{
13708 char_u *r = NULL;
13709
13710#ifdef FEAT_CLIENTSERVER
13711# ifdef WIN32
13712 r = serverGetVimNames();
13713# else
13714 make_connection();
13715 if (X_DISPLAY != NULL)
13716 r = serverGetVimNames(X_DISPLAY);
13717# endif
13718#endif
13719 rettv->v_type = VAR_STRING;
13720 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721}
13722
13723/*
13724 * "setbufvar()" function
13725 */
13726/*ARGSUSED*/
13727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013729 typval_T *argvars;
13730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731{
13732 buf_T *buf;
13733#ifdef FEAT_AUTOCMD
13734 aco_save_T aco;
13735#else
13736 buf_T *save_curbuf;
13737#endif
13738 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013739 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 char_u nbuf[NUMBUFLEN];
13741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013742 rettv->vval.v_number = 0;
13743
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 if (check_restricted() || check_secure())
13745 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13747 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 varp = &argvars[2];
13750
13751 if (buf != NULL && varname != NULL && varp != NULL)
13752 {
13753 /* set curbuf to be our buf, temporarily */
13754#ifdef FEAT_AUTOCMD
13755 aucmd_prepbuf(&aco, buf);
13756#else
13757 save_curbuf = curbuf;
13758 curbuf = buf;
13759#endif
13760
13761 if (*varname == '&')
13762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 long numval;
13764 char_u *strval;
13765 int error = FALSE;
13766
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013768 numval = get_tv_number_chk(varp, &error);
13769 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 if (!error && strval != NULL)
13771 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772 }
13773 else
13774 {
13775 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13776 if (bufvarname != NULL)
13777 {
13778 STRCPY(bufvarname, "b:");
13779 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013780 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 vim_free(bufvarname);
13782 }
13783 }
13784
13785 /* reset notion of buffer */
13786#ifdef FEAT_AUTOCMD
13787 aucmd_restbuf(&aco);
13788#else
13789 curbuf = save_curbuf;
13790#endif
13791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792}
13793
13794/*
13795 * "setcmdpos()" function
13796 */
13797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013798f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013799 typval_T *argvars;
13800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013802 int pos = (int)get_tv_number(&argvars[0]) - 1;
13803
13804 if (pos >= 0)
13805 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806}
13807
13808/*
13809 * "setline()" function
13810 */
13811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013813 typval_T *argvars;
13814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815{
13816 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013817 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013818 list_T *l = NULL;
13819 listitem_T *li = NULL;
13820 long added = 0;
13821 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013823 lnum = get_tv_lnum(&argvars[0]);
13824 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013826 l = argvars[1].vval.v_list;
13827 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013829 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013831
13832 rettv->vval.v_number = 0; /* OK */
13833 for (;;)
13834 {
13835 if (l != NULL)
13836 {
13837 /* list argument, get next string */
13838 if (li == NULL)
13839 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013841 li = li->li_next;
13842 }
13843
13844 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013845 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013846 break;
13847 if (lnum <= curbuf->b_ml.ml_line_count)
13848 {
13849 /* existing line, replace it */
13850 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13851 {
13852 changed_bytes(lnum, 0);
13853 check_cursor_col();
13854 rettv->vval.v_number = 0; /* OK */
13855 }
13856 }
13857 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13858 {
13859 /* lnum is one past the last line, append the line */
13860 ++added;
13861 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13862 rettv->vval.v_number = 0; /* OK */
13863 }
13864
13865 if (l == NULL) /* only one string argument */
13866 break;
13867 ++lnum;
13868 }
13869
13870 if (added > 0)
13871 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872}
13873
13874/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013875 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013876 */
13877/*ARGSUSED*/
13878 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013879set_qf_ll_list(wp, list_arg, action_arg, rettv)
13880 win_T *wp;
13881 typval_T *list_arg;
13882 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013883 typval_T *rettv;
13884{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013885#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013886 char_u *act;
13887 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013888#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013889
Bram Moolenaar2641f772005-03-25 21:58:17 +000013890 rettv->vval.v_number = -1;
13891
13892#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013893 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013894 EMSG(_(e_listreq));
13895 else
13896 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013897 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013898
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013899 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013900 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013901 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013902 if (act == NULL)
13903 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013904 if (*act == 'a' || *act == 'r')
13905 action = *act;
13906 }
13907
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013908 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013909 rettv->vval.v_number = 0;
13910 }
13911#endif
13912}
13913
13914/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013915 * "setloclist()" function
13916 */
13917/*ARGSUSED*/
13918 static void
13919f_setloclist(argvars, rettv)
13920 typval_T *argvars;
13921 typval_T *rettv;
13922{
13923 win_T *win;
13924
13925 rettv->vval.v_number = -1;
13926
13927 win = find_win_by_nr(&argvars[0]);
13928 if (win != NULL)
13929 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13930}
13931
13932/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013933 * "setpos()" function
13934 */
13935/*ARGSUSED*/
13936 static void
13937f_setpos(argvars, rettv)
13938 typval_T *argvars;
13939 typval_T *rettv;
13940{
13941 pos_T pos;
13942 int fnum;
13943 char_u *name;
13944
13945 name = get_tv_string_chk(argvars);
13946 if (name != NULL)
13947 {
13948 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
13949 {
13950 --pos.col;
13951 if (name[0] == '.') /* cursor */
13952 {
13953 if (fnum == curbuf->b_fnum)
13954 {
13955 curwin->w_cursor = pos;
13956 check_cursor();
13957 }
13958 else
13959 EMSG(_(e_invarg));
13960 }
13961 else if (name[0] == '\'') /* mark */
13962 (void)setmark_pos(name[1], &pos, fnum);
13963 else
13964 EMSG(_(e_invarg));
13965 }
13966 }
13967}
13968
13969/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013970 * "setqflist()" function
13971 */
13972/*ARGSUSED*/
13973 static void
13974f_setqflist(argvars, rettv)
13975 typval_T *argvars;
13976 typval_T *rettv;
13977{
13978 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13979}
13980
13981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 * "setreg()" function
13983 */
13984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013986 typval_T *argvars;
13987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988{
13989 int regname;
13990 char_u *strregname;
13991 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 int append;
13994 char_u yank_type;
13995 long block_len;
13996
13997 block_len = -1;
13998 yank_type = MAUTO;
13999 append = FALSE;
14000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014001 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014002 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014004 if (strregname == NULL)
14005 return; /* type error; errmsg already given */
14006 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 if (regname == 0 || regname == '@')
14008 regname = '"';
14009 else if (regname == '=')
14010 return;
14011
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014012 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014014 stropt = get_tv_string_chk(&argvars[2]);
14015 if (stropt == NULL)
14016 return; /* type error */
14017 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 switch (*stropt)
14019 {
14020 case 'a': case 'A': /* append */
14021 append = TRUE;
14022 break;
14023 case 'v': case 'c': /* character-wise selection */
14024 yank_type = MCHAR;
14025 break;
14026 case 'V': case 'l': /* line-wise selection */
14027 yank_type = MLINE;
14028 break;
14029#ifdef FEAT_VISUAL
14030 case 'b': case Ctrl_V: /* block-wise selection */
14031 yank_type = MBLOCK;
14032 if (VIM_ISDIGIT(stropt[1]))
14033 {
14034 ++stropt;
14035 block_len = getdigits(&stropt) - 1;
14036 --stropt;
14037 }
14038 break;
14039#endif
14040 }
14041 }
14042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014043 strval = get_tv_string_chk(&argvars[1]);
14044 if (strval != NULL)
14045 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048}
14049
14050
14051/*
14052 * "setwinvar(expr)" function
14053 */
14054/*ARGSUSED*/
14055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014057 typval_T *argvars;
14058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059{
14060 win_T *win;
14061#ifdef FEAT_WINDOWS
14062 win_T *save_curwin;
14063#endif
14064 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014065 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 char_u nbuf[NUMBUFLEN];
14067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014068 rettv->vval.v_number = 0;
14069
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 if (check_restricted() || check_secure())
14071 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 varp = &argvars[2];
14075
14076 if (win != NULL && varname != NULL && varp != NULL)
14077 {
14078#ifdef FEAT_WINDOWS
14079 /* set curwin to be our win, temporarily */
14080 save_curwin = curwin;
14081 curwin = win;
14082 curbuf = curwin->w_buffer;
14083#endif
14084
14085 if (*varname == '&')
14086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014087 long numval;
14088 char_u *strval;
14089 int error = FALSE;
14090
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092 numval = get_tv_number_chk(varp, &error);
14093 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014094 if (!error && strval != NULL)
14095 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 }
14097 else
14098 {
14099 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14100 if (winvarname != NULL)
14101 {
14102 STRCPY(winvarname, "w:");
14103 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014104 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105 vim_free(winvarname);
14106 }
14107 }
14108
14109#ifdef FEAT_WINDOWS
14110 /* Restore current window, if it's still valid (autocomands can make
14111 * it invalid). */
14112 if (win_valid(save_curwin))
14113 {
14114 curwin = save_curwin;
14115 curbuf = curwin->w_buffer;
14116 }
14117#endif
14118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119}
14120
14121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014122 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 */
14124 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014125f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 typval_T *argvars;
14127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014129 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130
Bram Moolenaar0d660222005-01-07 21:51:51 +000014131 p = get_tv_string(&argvars[0]);
14132 rettv->vval.v_string = vim_strsave(p);
14133 simplify_filename(rettv->vval.v_string); /* simplify in place */
14134 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135}
14136
Bram Moolenaar0d660222005-01-07 21:51:51 +000014137static int
14138#ifdef __BORLANDC__
14139 _RTLENTRYF
14140#endif
14141 item_compare __ARGS((const void *s1, const void *s2));
14142static int
14143#ifdef __BORLANDC__
14144 _RTLENTRYF
14145#endif
14146 item_compare2 __ARGS((const void *s1, const void *s2));
14147
14148static int item_compare_ic;
14149static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014150static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014151#define ITEM_COMPARE_FAIL 999
14152
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014154 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156 static int
14157#ifdef __BORLANDC__
14158_RTLENTRYF
14159#endif
14160item_compare(s1, s2)
14161 const void *s1;
14162 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014164 char_u *p1, *p2;
14165 char_u *tofree1, *tofree2;
14166 int res;
14167 char_u numbuf1[NUMBUFLEN];
14168 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014170 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14171 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014172 if (item_compare_ic)
14173 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014175 res = STRCMP(p1, p2);
14176 vim_free(tofree1);
14177 vim_free(tofree2);
14178 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179}
14180
14181 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014182#ifdef __BORLANDC__
14183_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014185item_compare2(s1, s2)
14186 const void *s1;
14187 const void *s2;
14188{
14189 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014190 typval_T rettv;
14191 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014192 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 /* shortcut after failure in previous call; compare all items equal */
14195 if (item_compare_func_err)
14196 return 0;
14197
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014198 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14199 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14201 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014202
14203 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14204 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014205 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014206 clear_tv(&argv[0]);
14207 clear_tv(&argv[1]);
14208
14209 if (res == FAIL)
14210 res = ITEM_COMPARE_FAIL;
14211 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 /* return value has wrong type */
14213 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14214 if (item_compare_func_err)
14215 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014216 clear_tv(&rettv);
14217 return res;
14218}
14219
14220/*
14221 * "sort({list})" function
14222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014224f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014225 typval_T *argvars;
14226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227{
Bram Moolenaar33570922005-01-25 22:26:29 +000014228 list_T *l;
14229 listitem_T *li;
14230 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014231 long len;
14232 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233
Bram Moolenaar0d660222005-01-07 21:51:51 +000014234 rettv->vval.v_number = 0;
14235 if (argvars[0].v_type != VAR_LIST)
14236 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237 else
14238 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014239 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014240 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014241 return;
14242 rettv->vval.v_list = l;
14243 rettv->v_type = VAR_LIST;
14244 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245
Bram Moolenaar0d660222005-01-07 21:51:51 +000014246 len = list_len(l);
14247 if (len <= 1)
14248 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249
Bram Moolenaar0d660222005-01-07 21:51:51 +000014250 item_compare_ic = FALSE;
14251 item_compare_func = NULL;
14252 if (argvars[1].v_type != VAR_UNKNOWN)
14253 {
14254 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014256 else
14257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014258 int error = FALSE;
14259
14260 i = get_tv_number_chk(&argvars[1], &error);
14261 if (error)
14262 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014263 if (i == 1)
14264 item_compare_ic = TRUE;
14265 else
14266 item_compare_func = get_tv_string(&argvars[1]);
14267 }
14268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269
Bram Moolenaar0d660222005-01-07 21:51:51 +000014270 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014271 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014272 if (ptrs == NULL)
14273 return;
14274 i = 0;
14275 for (li = l->lv_first; li != NULL; li = li->li_next)
14276 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014278 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014279 /* test the compare function */
14280 if (item_compare_func != NULL
14281 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14282 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014283 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014285 {
14286 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014288 item_compare_func == NULL ? item_compare : item_compare2);
14289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014290 if (!item_compare_func_err)
14291 {
14292 /* Clear the List and append the items in the sorted order. */
14293 l->lv_first = l->lv_last = NULL;
14294 l->lv_len = 0;
14295 for (i = 0; i < len; ++i)
14296 list_append(l, ptrs[i]);
14297 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014298 }
14299
14300 vim_free(ptrs);
14301 }
14302}
14303
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014304/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014305 * "soundfold({word})" function
14306 */
14307 static void
14308f_soundfold(argvars, rettv)
14309 typval_T *argvars;
14310 typval_T *rettv;
14311{
14312 char_u *s;
14313
14314 rettv->v_type = VAR_STRING;
14315 s = get_tv_string(&argvars[0]);
14316#ifdef FEAT_SYN_HL
14317 rettv->vval.v_string = eval_soundfold(s);
14318#else
14319 rettv->vval.v_string = vim_strsave(s);
14320#endif
14321}
14322
14323/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014324 * "spellbadword()" function
14325 */
14326/* ARGSUSED */
14327 static void
14328f_spellbadword(argvars, rettv)
14329 typval_T *argvars;
14330 typval_T *rettv;
14331{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014332 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014333 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014334 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014335
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014336 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014337 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014338
14339#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014340 if (argvars[0].v_type == VAR_UNKNOWN)
14341 {
14342 /* Find the start and length of the badly spelled word. */
14343 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14344 if (len != 0)
14345 word = ml_get_cursor();
14346 }
14347 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14348 {
14349 char_u *str = get_tv_string_chk(&argvars[0]);
14350 int capcol = -1;
14351
14352 if (str != NULL)
14353 {
14354 /* Check the argument for spelling. */
14355 while (*str != NUL)
14356 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014357 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014358 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014359 {
14360 word = str;
14361 break;
14362 }
14363 str += len;
14364 }
14365 }
14366 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014367#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014368
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014369 list_append_string(rettv->vval.v_list, word, len);
14370 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014371 attr == HLF_SPB ? "bad" :
14372 attr == HLF_SPR ? "rare" :
14373 attr == HLF_SPL ? "local" :
14374 attr == HLF_SPC ? "caps" :
14375 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014376}
14377
14378/*
14379 * "spellsuggest()" function
14380 */
14381 static void
14382f_spellsuggest(argvars, rettv)
14383 typval_T *argvars;
14384 typval_T *rettv;
14385{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014386#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014387 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014388 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014389 int maxcount;
14390 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014391 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014392 listitem_T *li;
14393 int need_capital = FALSE;
14394#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014395
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014396 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014397 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014398
14399#ifdef FEAT_SYN_HL
14400 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14401 {
14402 str = get_tv_string(&argvars[0]);
14403 if (argvars[1].v_type != VAR_UNKNOWN)
14404 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014405 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014406 if (maxcount <= 0)
14407 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014408 if (argvars[2].v_type != VAR_UNKNOWN)
14409 {
14410 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14411 if (typeerr)
14412 return;
14413 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014414 }
14415 else
14416 maxcount = 25;
14417
Bram Moolenaar4770d092006-01-12 23:22:24 +000014418 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014419
14420 for (i = 0; i < ga.ga_len; ++i)
14421 {
14422 str = ((char_u **)ga.ga_data)[i];
14423
14424 li = listitem_alloc();
14425 if (li == NULL)
14426 vim_free(str);
14427 else
14428 {
14429 li->li_tv.v_type = VAR_STRING;
14430 li->li_tv.v_lock = 0;
14431 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014432 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014433 }
14434 }
14435 ga_clear(&ga);
14436 }
14437#endif
14438}
14439
Bram Moolenaar0d660222005-01-07 21:51:51 +000014440 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014441f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014442 typval_T *argvars;
14443 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014444{
14445 char_u *str;
14446 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014447 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014448 regmatch_T regmatch;
14449 char_u patbuf[NUMBUFLEN];
14450 char_u *save_cpo;
14451 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014452 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014453 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014455
14456 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14457 save_cpo = p_cpo;
14458 p_cpo = (char_u *)"";
14459
14460 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014461 if (argvars[1].v_type != VAR_UNKNOWN)
14462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014463 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14464 if (pat == NULL)
14465 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014467 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014468 }
14469 if (pat == NULL || *pat == NUL)
14470 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014471
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014472 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014474 if (typeerr)
14475 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14478 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014481 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014482 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014483 if (*str == NUL)
14484 match = FALSE; /* empty item at the end */
14485 else
14486 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014487 if (match)
14488 end = regmatch.startp[0];
14489 else
14490 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014491 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14492 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014494 if (list_append_string(rettv->vval.v_list, str,
14495 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014497 }
14498 if (!match)
14499 break;
14500 /* Advance to just after the match. */
14501 if (regmatch.endp[0] > str)
14502 col = 0;
14503 else
14504 {
14505 /* Don't get stuck at the same match. */
14506#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014507 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014508#else
14509 col = 1;
14510#endif
14511 }
14512 str = regmatch.endp[0];
14513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514
Bram Moolenaar0d660222005-01-07 21:51:51 +000014515 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517
Bram Moolenaar0d660222005-01-07 21:51:51 +000014518 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519}
14520
14521#ifdef HAVE_STRFTIME
14522/*
14523 * "strftime({format}[, {time}])" function
14524 */
14525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014527 typval_T *argvars;
14528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529{
14530 char_u result_buf[256];
14531 struct tm *curtime;
14532 time_t seconds;
14533 char_u *p;
14534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014538 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 seconds = time(NULL);
14540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014541 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 curtime = localtime(&seconds);
14543 /* MSVC returns NULL for an invalid value of seconds. */
14544 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014545 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 else
14547 {
14548# ifdef FEAT_MBYTE
14549 vimconv_T conv;
14550 char_u *enc;
14551
14552 conv.vc_type = CONV_NONE;
14553 enc = enc_locale();
14554 convert_setup(&conv, p_enc, enc);
14555 if (conv.vc_type != CONV_NONE)
14556 p = string_convert(&conv, p, NULL);
14557# endif
14558 if (p != NULL)
14559 (void)strftime((char *)result_buf, sizeof(result_buf),
14560 (char *)p, curtime);
14561 else
14562 result_buf[0] = NUL;
14563
14564# ifdef FEAT_MBYTE
14565 if (conv.vc_type != CONV_NONE)
14566 vim_free(p);
14567 convert_setup(&conv, enc, p_enc);
14568 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014569 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570 else
14571# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573
14574# ifdef FEAT_MBYTE
14575 /* Release conversion descriptors */
14576 convert_setup(&conv, NULL, NULL);
14577 vim_free(enc);
14578# endif
14579 }
14580}
14581#endif
14582
14583/*
14584 * "stridx()" function
14585 */
14586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014587f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014588 typval_T *argvars;
14589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590{
14591 char_u buf[NUMBUFLEN];
14592 char_u *needle;
14593 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014594 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 needle = get_tv_string_chk(&argvars[1]);
14599 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014600 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 if (needle == NULL || haystack == NULL)
14602 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603
Bram Moolenaar33570922005-01-25 22:26:29 +000014604 if (argvars[2].v_type != VAR_UNKNOWN)
14605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014606 int error = FALSE;
14607
14608 start_idx = get_tv_number_chk(&argvars[2], &error);
14609 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014610 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014611 if (start_idx >= 0)
14612 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014613 }
14614
14615 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14616 if (pos != NULL)
14617 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618}
14619
14620/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014621 * "string()" function
14622 */
14623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014625 typval_T *argvars;
14626 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014627{
14628 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014629 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014631 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014632 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014633 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014634 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635}
14636
14637/*
14638 * "strlen()" function
14639 */
14640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014641f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 typval_T *argvars;
14643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014645 rettv->vval.v_number = (varnumber_T)(STRLEN(
14646 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647}
14648
14649/*
14650 * "strpart()" function
14651 */
14652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014653f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014654 typval_T *argvars;
14655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656{
14657 char_u *p;
14658 int n;
14659 int len;
14660 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 slen = (int)STRLEN(p);
14665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 n = get_tv_number_chk(&argvars[1], &error);
14667 if (error)
14668 len = 0;
14669 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014670 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671 else
14672 len = slen - n; /* default len: all bytes that are available. */
14673
14674 /*
14675 * Only return the overlap between the specified part and the actual
14676 * string.
14677 */
14678 if (n < 0)
14679 {
14680 len += n;
14681 n = 0;
14682 }
14683 else if (n > slen)
14684 n = slen;
14685 if (len < 0)
14686 len = 0;
14687 else if (n + len > slen)
14688 len = slen - n;
14689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014690 rettv->v_type = VAR_STRING;
14691 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692}
14693
14694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014695 * "strridx()" function
14696 */
14697 static void
14698f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014699 typval_T *argvars;
14700 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014701{
14702 char_u buf[NUMBUFLEN];
14703 char_u *needle;
14704 char_u *haystack;
14705 char_u *rest;
14706 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014707 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014709 needle = get_tv_string_chk(&argvars[1]);
14710 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014711 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014712
14713 rettv->vval.v_number = -1;
14714 if (needle == NULL || haystack == NULL)
14715 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014716 if (argvars[2].v_type != VAR_UNKNOWN)
14717 {
14718 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014719 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014720 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014722 }
14723 else
14724 end_idx = haystack_len;
14725
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014727 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014728 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014729 lastmatch = haystack + end_idx;
14730 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014731 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014732 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733 for (rest = haystack; *rest != '\0'; ++rest)
14734 {
14735 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014736 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014737 break;
14738 lastmatch = rest;
14739 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014740 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014741
14742 if (lastmatch == NULL)
14743 rettv->vval.v_number = -1;
14744 else
14745 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14746}
14747
14748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 * "strtrans()" function
14750 */
14751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014752f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014753 typval_T *argvars;
14754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->v_type = VAR_STRING;
14757 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758}
14759
14760/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014761 * "submatch()" function
14762 */
14763 static void
14764f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014765 typval_T *argvars;
14766 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014767{
14768 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014769 rettv->vval.v_string =
14770 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014771}
14772
14773/*
14774 * "substitute()" function
14775 */
14776 static void
14777f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014778 typval_T *argvars;
14779 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014780{
14781 char_u patbuf[NUMBUFLEN];
14782 char_u subbuf[NUMBUFLEN];
14783 char_u flagsbuf[NUMBUFLEN];
14784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014785 char_u *str = get_tv_string_chk(&argvars[0]);
14786 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14787 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14788 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14789
Bram Moolenaar0d660222005-01-07 21:51:51 +000014790 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014791 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14792 rettv->vval.v_string = NULL;
14793 else
14794 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014795}
14796
14797/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014798 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799 */
14800/*ARGSUSED*/
14801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014802f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014803 typval_T *argvars;
14804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805{
14806 int id = 0;
14807#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014808 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 long col;
14810 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014811 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014813 lnum = get_tv_lnum(argvars); /* -1 on type error */
14814 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14815 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014817 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014818 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014819 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820#endif
14821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823}
14824
14825/*
14826 * "synIDattr(id, what [, mode])" function
14827 */
14828/*ARGSUSED*/
14829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014830f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014831 typval_T *argvars;
14832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833{
14834 char_u *p = NULL;
14835#ifdef FEAT_SYN_HL
14836 int id;
14837 char_u *what;
14838 char_u *mode;
14839 char_u modebuf[NUMBUFLEN];
14840 int modec;
14841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014842 id = get_tv_number(&argvars[0]);
14843 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014844 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 modec = TOLOWER_ASC(mode[0]);
14848 if (modec != 't' && modec != 'c'
14849#ifdef FEAT_GUI
14850 && modec != 'g'
14851#endif
14852 )
14853 modec = 0; /* replace invalid with current */
14854 }
14855 else
14856 {
14857#ifdef FEAT_GUI
14858 if (gui.in_use)
14859 modec = 'g';
14860 else
14861#endif
14862 if (t_colors > 1)
14863 modec = 'c';
14864 else
14865 modec = 't';
14866 }
14867
14868
14869 switch (TOLOWER_ASC(what[0]))
14870 {
14871 case 'b':
14872 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14873 p = highlight_color(id, what, modec);
14874 else /* bold */
14875 p = highlight_has_attr(id, HL_BOLD, modec);
14876 break;
14877
14878 case 'f': /* fg[#] */
14879 p = highlight_color(id, what, modec);
14880 break;
14881
14882 case 'i':
14883 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14884 p = highlight_has_attr(id, HL_INVERSE, modec);
14885 else /* italic */
14886 p = highlight_has_attr(id, HL_ITALIC, modec);
14887 break;
14888
14889 case 'n': /* name */
14890 p = get_highlight_name(NULL, id - 1);
14891 break;
14892
14893 case 'r': /* reverse */
14894 p = highlight_has_attr(id, HL_INVERSE, modec);
14895 break;
14896
14897 case 's': /* standout */
14898 p = highlight_has_attr(id, HL_STANDOUT, modec);
14899 break;
14900
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014901 case 'u':
14902 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14903 /* underline */
14904 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14905 else
14906 /* undercurl */
14907 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 break;
14909 }
14910
14911 if (p != NULL)
14912 p = vim_strsave(p);
14913#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014914 rettv->v_type = VAR_STRING;
14915 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916}
14917
14918/*
14919 * "synIDtrans(id)" function
14920 */
14921/*ARGSUSED*/
14922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014924 typval_T *argvars;
14925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926{
14927 int id;
14928
14929#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014930 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931
14932 if (id > 0)
14933 id = syn_get_final_id(id);
14934 else
14935#endif
14936 id = 0;
14937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014938 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939}
14940
14941/*
14942 * "system()" function
14943 */
14944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014945f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014946 typval_T *argvars;
14947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014949 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014951 char_u *infile = NULL;
14952 char_u buf[NUMBUFLEN];
14953 int err = FALSE;
14954 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014956 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014957 {
14958 /*
14959 * Write the string to a temp file, to be used for input of the shell
14960 * command.
14961 */
14962 if ((infile = vim_tempname('i')) == NULL)
14963 {
14964 EMSG(_(e_notmp));
14965 return;
14966 }
14967
14968 fd = mch_fopen((char *)infile, WRITEBIN);
14969 if (fd == NULL)
14970 {
14971 EMSG2(_(e_notopen), infile);
14972 goto done;
14973 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014974 p = get_tv_string_buf_chk(&argvars[1], buf);
14975 if (p == NULL)
14976 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014977 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14978 err = TRUE;
14979 if (fclose(fd) != 0)
14980 err = TRUE;
14981 if (err)
14982 {
14983 EMSG(_("E677: Error writing temp file"));
14984 goto done;
14985 }
14986 }
14987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014988 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014989
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990#ifdef USE_CR
14991 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014992 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993 {
14994 char_u *s;
14995
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014996 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 {
14998 if (*s == CAR)
14999 *s = NL;
15000 }
15001 }
15002#else
15003# ifdef USE_CRNL
15004 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015005 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 {
15007 char_u *s, *d;
15008
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015009 d = res;
15010 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 {
15012 if (s[0] == CAR && s[1] == NL)
15013 ++s;
15014 *d++ = *s;
15015 }
15016 *d = NUL;
15017 }
15018# endif
15019#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015020
15021done:
15022 if (infile != NULL)
15023 {
15024 mch_remove(infile);
15025 vim_free(infile);
15026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015027 rettv->v_type = VAR_STRING;
15028 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029}
15030
15031/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015032 * "tabpagebuflist()" function
15033 */
15034/* ARGSUSED */
15035 static void
15036f_tabpagebuflist(argvars, rettv)
15037 typval_T *argvars;
15038 typval_T *rettv;
15039{
15040#ifndef FEAT_WINDOWS
15041 rettv->vval.v_number = 0;
15042#else
15043 tabpage_T *tp;
15044 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015045
15046 if (argvars[0].v_type == VAR_UNKNOWN)
15047 wp = firstwin;
15048 else
15049 {
15050 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15051 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015052 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015053 }
15054 if (wp == NULL)
15055 rettv->vval.v_number = 0;
15056 else
15057 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015058 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015059 rettv->vval.v_number = 0;
15060 else
15061 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015062 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015063 if (list_append_number(rettv->vval.v_list,
15064 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015065 break;
15066 }
15067 }
15068#endif
15069}
15070
15071
15072/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015073 * "tabpagenr()" function
15074 */
15075/* ARGSUSED */
15076 static void
15077f_tabpagenr(argvars, rettv)
15078 typval_T *argvars;
15079 typval_T *rettv;
15080{
15081 int nr = 1;
15082#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015083 char_u *arg;
15084
15085 if (argvars[0].v_type != VAR_UNKNOWN)
15086 {
15087 arg = get_tv_string_chk(&argvars[0]);
15088 nr = 0;
15089 if (arg != NULL)
15090 {
15091 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015092 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015093 else
15094 EMSG2(_(e_invexpr2), arg);
15095 }
15096 }
15097 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015098 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015099#endif
15100 rettv->vval.v_number = nr;
15101}
15102
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015103
15104#ifdef FEAT_WINDOWS
15105static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15106
15107/*
15108 * Common code for tabpagewinnr() and winnr().
15109 */
15110 static int
15111get_winnr(tp, argvar)
15112 tabpage_T *tp;
15113 typval_T *argvar;
15114{
15115 win_T *twin;
15116 int nr = 1;
15117 win_T *wp;
15118 char_u *arg;
15119
15120 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15121 if (argvar->v_type != VAR_UNKNOWN)
15122 {
15123 arg = get_tv_string_chk(argvar);
15124 if (arg == NULL)
15125 nr = 0; /* type error; errmsg already given */
15126 else if (STRCMP(arg, "$") == 0)
15127 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15128 else if (STRCMP(arg, "#") == 0)
15129 {
15130 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15131 if (twin == NULL)
15132 nr = 0;
15133 }
15134 else
15135 {
15136 EMSG2(_(e_invexpr2), arg);
15137 nr = 0;
15138 }
15139 }
15140
15141 if (nr > 0)
15142 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15143 wp != twin; wp = wp->w_next)
15144 ++nr;
15145 return nr;
15146}
15147#endif
15148
15149/*
15150 * "tabpagewinnr()" function
15151 */
15152/* ARGSUSED */
15153 static void
15154f_tabpagewinnr(argvars, rettv)
15155 typval_T *argvars;
15156 typval_T *rettv;
15157{
15158 int nr = 1;
15159#ifdef FEAT_WINDOWS
15160 tabpage_T *tp;
15161
15162 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15163 if (tp == NULL)
15164 nr = 0;
15165 else
15166 nr = get_winnr(tp, &argvars[1]);
15167#endif
15168 rettv->vval.v_number = nr;
15169}
15170
15171
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015172/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015173 * "tagfiles()" function
15174 */
15175/*ARGSUSED*/
15176 static void
15177f_tagfiles(argvars, rettv)
15178 typval_T *argvars;
15179 typval_T *rettv;
15180{
15181 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015182 tagname_T tn;
15183 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015184
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015185 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015186 {
15187 rettv->vval.v_number = 0;
15188 return;
15189 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015190
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015191 for (first = TRUE; ; first = FALSE)
15192 if (get_tagfname(&tn, first, fname) == FAIL
15193 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015194 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015195 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015196}
15197
15198/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015199 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015200 */
15201 static void
15202f_taglist(argvars, rettv)
15203 typval_T *argvars;
15204 typval_T *rettv;
15205{
15206 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015207
15208 tag_pattern = get_tv_string(&argvars[0]);
15209
15210 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015211 if (*tag_pattern == NUL)
15212 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015213
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015214 if (rettv_list_alloc(rettv) == OK)
15215 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015216}
15217
15218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 * "tempname()" function
15220 */
15221/*ARGSUSED*/
15222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015223f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015224 typval_T *argvars;
15225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226{
15227 static int x = 'A';
15228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015229 rettv->v_type = VAR_STRING;
15230 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231
15232 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15233 * names. Skip 'I' and 'O', they are used for shell redirection. */
15234 do
15235 {
15236 if (x == 'Z')
15237 x = '0';
15238 else if (x == '9')
15239 x = 'A';
15240 else
15241 {
15242#ifdef EBCDIC
15243 if (x == 'I')
15244 x = 'J';
15245 else if (x == 'R')
15246 x = 'S';
15247 else
15248#endif
15249 ++x;
15250 }
15251 } while (x == 'I' || x == 'O');
15252}
15253
15254/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015255 * "test(list)" function: Just checking the walls...
15256 */
15257/*ARGSUSED*/
15258 static void
15259f_test(argvars, rettv)
15260 typval_T *argvars;
15261 typval_T *rettv;
15262{
15263 /* Used for unit testing. Change the code below to your liking. */
15264#if 0
15265 listitem_T *li;
15266 list_T *l;
15267 char_u *bad, *good;
15268
15269 if (argvars[0].v_type != VAR_LIST)
15270 return;
15271 l = argvars[0].vval.v_list;
15272 if (l == NULL)
15273 return;
15274 li = l->lv_first;
15275 if (li == NULL)
15276 return;
15277 bad = get_tv_string(&li->li_tv);
15278 li = li->li_next;
15279 if (li == NULL)
15280 return;
15281 good = get_tv_string(&li->li_tv);
15282 rettv->vval.v_number = test_edit_score(bad, good);
15283#endif
15284}
15285
15286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 * "tolower(string)" function
15288 */
15289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015290f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015291 typval_T *argvars;
15292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293{
15294 char_u *p;
15295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015296 p = vim_strsave(get_tv_string(&argvars[0]));
15297 rettv->v_type = VAR_STRING;
15298 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299
15300 if (p != NULL)
15301 while (*p != NUL)
15302 {
15303#ifdef FEAT_MBYTE
15304 int l;
15305
15306 if (enc_utf8)
15307 {
15308 int c, lc;
15309
15310 c = utf_ptr2char(p);
15311 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015312 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313 /* TODO: reallocate string when byte count changes. */
15314 if (utf_char2len(lc) == l)
15315 utf_char2bytes(lc, p);
15316 p += l;
15317 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015318 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 p += l; /* skip multi-byte character */
15320 else
15321#endif
15322 {
15323 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15324 ++p;
15325 }
15326 }
15327}
15328
15329/*
15330 * "toupper(string)" function
15331 */
15332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015333f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015334 typval_T *argvars;
15335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015337 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015338 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339}
15340
15341/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015342 * "tr(string, fromstr, tostr)" function
15343 */
15344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015345f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015346 typval_T *argvars;
15347 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015348{
15349 char_u *instr;
15350 char_u *fromstr;
15351 char_u *tostr;
15352 char_u *p;
15353#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015354 int inlen;
15355 int fromlen;
15356 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015357 int idx;
15358 char_u *cpstr;
15359 int cplen;
15360 int first = TRUE;
15361#endif
15362 char_u buf[NUMBUFLEN];
15363 char_u buf2[NUMBUFLEN];
15364 garray_T ga;
15365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015367 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15368 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015369
15370 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015371 rettv->v_type = VAR_STRING;
15372 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015373 if (fromstr == NULL || tostr == NULL)
15374 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015375 ga_init2(&ga, (int)sizeof(char), 80);
15376
15377#ifdef FEAT_MBYTE
15378 if (!has_mbyte)
15379#endif
15380 /* not multi-byte: fromstr and tostr must be the same length */
15381 if (STRLEN(fromstr) != STRLEN(tostr))
15382 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015383#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015384error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015385#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015386 EMSG2(_(e_invarg2), fromstr);
15387 ga_clear(&ga);
15388 return;
15389 }
15390
15391 /* fromstr and tostr have to contain the same number of chars */
15392 while (*instr != NUL)
15393 {
15394#ifdef FEAT_MBYTE
15395 if (has_mbyte)
15396 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015397 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015398 cpstr = instr;
15399 cplen = inlen;
15400 idx = 0;
15401 for (p = fromstr; *p != NUL; p += fromlen)
15402 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015403 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015404 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15405 {
15406 for (p = tostr; *p != NUL; p += tolen)
15407 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015408 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015409 if (idx-- == 0)
15410 {
15411 cplen = tolen;
15412 cpstr = p;
15413 break;
15414 }
15415 }
15416 if (*p == NUL) /* tostr is shorter than fromstr */
15417 goto error;
15418 break;
15419 }
15420 ++idx;
15421 }
15422
15423 if (first && cpstr == instr)
15424 {
15425 /* Check that fromstr and tostr have the same number of
15426 * (multi-byte) characters. Done only once when a character
15427 * of instr doesn't appear in fromstr. */
15428 first = FALSE;
15429 for (p = tostr; *p != NUL; p += tolen)
15430 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015431 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015432 --idx;
15433 }
15434 if (idx != 0)
15435 goto error;
15436 }
15437
15438 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015439 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015440 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015441
15442 instr += inlen;
15443 }
15444 else
15445#endif
15446 {
15447 /* When not using multi-byte chars we can do it faster. */
15448 p = vim_strchr(fromstr, *instr);
15449 if (p != NULL)
15450 ga_append(&ga, tostr[p - fromstr]);
15451 else
15452 ga_append(&ga, *instr);
15453 ++instr;
15454 }
15455 }
15456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015457 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015458}
15459
15460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 * "type(expr)" function
15462 */
15463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015464f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 typval_T *argvars;
15466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015468 int n;
15469
15470 switch (argvars[0].v_type)
15471 {
15472 case VAR_NUMBER: n = 0; break;
15473 case VAR_STRING: n = 1; break;
15474 case VAR_FUNC: n = 2; break;
15475 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015476 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015477 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15478 }
15479 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480}
15481
15482/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015483 * "values(dict)" function
15484 */
15485 static void
15486f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015487 typval_T *argvars;
15488 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015489{
15490 dict_list(argvars, rettv, 1);
15491}
15492
15493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 * "virtcol(string)" function
15495 */
15496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015497f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015498 typval_T *argvars;
15499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500{
15501 colnr_T vcol = 0;
15502 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015503 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015505 fp = var2fpos(&argvars[0], FALSE, &fnum);
15506 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15507 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508 {
15509 getvvcol(curwin, fp, NULL, NULL, &vcol);
15510 ++vcol;
15511 }
15512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015513 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514}
15515
15516/*
15517 * "visualmode()" function
15518 */
15519/*ARGSUSED*/
15520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015522 typval_T *argvars;
15523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524{
15525#ifdef FEAT_VISUAL
15526 char_u str[2];
15527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 str[0] = curbuf->b_visual_mode_eval;
15530 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532
15533 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015534 if ((argvars[0].v_type == VAR_NUMBER
15535 && argvars[0].vval.v_number != 0)
15536 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 curbuf->b_visual_mode_eval = NUL;
15539#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015540 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541#endif
15542}
15543
15544/*
15545 * "winbufnr(nr)" function
15546 */
15547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015549 typval_T *argvars;
15550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551{
15552 win_T *wp;
15553
15554 wp = find_win_by_nr(&argvars[0]);
15555 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559}
15560
15561/*
15562 * "wincol()" function
15563 */
15564/*ARGSUSED*/
15565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015567 typval_T *argvars;
15568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569{
15570 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015571 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572}
15573
15574/*
15575 * "winheight(nr)" function
15576 */
15577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015578f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015579 typval_T *argvars;
15580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581{
15582 win_T *wp;
15583
15584 wp = find_win_by_nr(&argvars[0]);
15585 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015586 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015588 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589}
15590
15591/*
15592 * "winline()" function
15593 */
15594/*ARGSUSED*/
15595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015596f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015597 typval_T *argvars;
15598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599{
15600 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015601 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602}
15603
15604/*
15605 * "winnr()" function
15606 */
15607/* ARGSUSED */
15608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015609f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015610 typval_T *argvars;
15611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612{
15613 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015614
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015616 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015618 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619}
15620
15621/*
15622 * "winrestcmd()" function
15623 */
15624/* ARGSUSED */
15625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015626f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015627 typval_T *argvars;
15628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629{
15630#ifdef FEAT_WINDOWS
15631 win_T *wp;
15632 int winnr = 1;
15633 garray_T ga;
15634 char_u buf[50];
15635
15636 ga_init2(&ga, (int)sizeof(char), 70);
15637 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15638 {
15639 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15640 ga_concat(&ga, buf);
15641# ifdef FEAT_VERTSPLIT
15642 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15643 ga_concat(&ga, buf);
15644# endif
15645 ++winnr;
15646 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015647 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015649 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015651 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015653 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654}
15655
15656/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015657 * "winrestview()" function
15658 */
15659/* ARGSUSED */
15660 static void
15661f_winrestview(argvars, rettv)
15662 typval_T *argvars;
15663 typval_T *rettv;
15664{
15665 dict_T *dict;
15666
15667 if (argvars[0].v_type != VAR_DICT
15668 || (dict = argvars[0].vval.v_dict) == NULL)
15669 EMSG(_(e_invarg));
15670 else
15671 {
15672 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15673 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15674#ifdef FEAT_VIRTUALEDIT
15675 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15676#endif
15677 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
15678
15679 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15680#ifdef FEAT_DIFF
15681 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15682#endif
15683 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15684 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15685
15686 check_cursor();
15687 changed_cline_bef_curs();
15688 invalidate_botline();
15689 redraw_later(VALID);
15690
15691 if (curwin->w_topline == 0)
15692 curwin->w_topline = 1;
15693 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15694 curwin->w_topline = curbuf->b_ml.ml_line_count;
15695#ifdef FEAT_DIFF
15696 check_topfill(curwin, TRUE);
15697#endif
15698 }
15699}
15700
15701/*
15702 * "winsaveview()" function
15703 */
15704/* ARGSUSED */
15705 static void
15706f_winsaveview(argvars, rettv)
15707 typval_T *argvars;
15708 typval_T *rettv;
15709{
15710 dict_T *dict;
15711
15712 dict = dict_alloc();
15713 if (dict == NULL)
15714 return;
15715 rettv->v_type = VAR_DICT;
15716 rettv->vval.v_dict = dict;
15717 ++dict->dv_refcount;
15718
15719 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15720 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15721#ifdef FEAT_VIRTUALEDIT
15722 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15723#endif
15724 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15725
15726 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15727#ifdef FEAT_DIFF
15728 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15729#endif
15730 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15731 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15732}
15733
15734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 * "winwidth(nr)" function
15736 */
15737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015739 typval_T *argvars;
15740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741{
15742 win_T *wp;
15743
15744 wp = find_win_by_nr(&argvars[0]);
15745 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015746 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 else
15748#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015749 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015751 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752#endif
15753}
15754
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015756 * "writefile()" function
15757 */
15758 static void
15759f_writefile(argvars, rettv)
15760 typval_T *argvars;
15761 typval_T *rettv;
15762{
15763 int binary = FALSE;
15764 char_u *fname;
15765 FILE *fd;
15766 listitem_T *li;
15767 char_u *s;
15768 int ret = 0;
15769 int c;
15770
15771 if (argvars[0].v_type != VAR_LIST)
15772 {
15773 EMSG2(_(e_listarg), "writefile()");
15774 return;
15775 }
15776 if (argvars[0].vval.v_list == NULL)
15777 return;
15778
15779 if (argvars[2].v_type != VAR_UNKNOWN
15780 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15781 binary = TRUE;
15782
15783 /* Always open the file in binary mode, library functions have a mind of
15784 * their own about CR-LF conversion. */
15785 fname = get_tv_string(&argvars[1]);
15786 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15787 {
15788 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15789 ret = -1;
15790 }
15791 else
15792 {
15793 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15794 li = li->li_next)
15795 {
15796 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15797 {
15798 if (*s == '\n')
15799 c = putc(NUL, fd);
15800 else
15801 c = putc(*s, fd);
15802 if (c == EOF)
15803 {
15804 ret = -1;
15805 break;
15806 }
15807 }
15808 if (!binary || li->li_next != NULL)
15809 if (putc('\n', fd) == EOF)
15810 {
15811 ret = -1;
15812 break;
15813 }
15814 if (ret < 0)
15815 {
15816 EMSG(_(e_write));
15817 break;
15818 }
15819 }
15820 fclose(fd);
15821 }
15822
15823 rettv->vval.v_number = ret;
15824}
15825
15826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015828 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 */
15830 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015831var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015832 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015834 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015836 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015838 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839
Bram Moolenaara5525202006-03-02 22:52:09 +000015840 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015841 if (varp->v_type == VAR_LIST)
15842 {
15843 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015844 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000015845 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015846
15847 l = varp->vval.v_list;
15848 if (l == NULL)
15849 return NULL;
15850
15851 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015852 pos.lnum = list_find_nr(l, 0L, &error);
15853 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015854 return NULL; /* invalid line number */
15855
15856 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015857 pos.col = list_find_nr(l, 1L, &error);
15858 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015859 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015860 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000015861 /* Accept a position up to the NUL after the line. */
15862 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015863 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015864 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015865
Bram Moolenaara5525202006-03-02 22:52:09 +000015866#ifdef FEAT_VIRTUALEDIT
15867 /* Get the virtual offset. Defaults to zero. */
15868 pos.coladd = list_find_nr(l, 2L, &error);
15869 if (error)
15870 pos.coladd = 0;
15871#endif
15872
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015873 return &pos;
15874 }
15875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015876 name = get_tv_string_chk(varp);
15877 if (name == NULL)
15878 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 if (name[0] == '.') /* cursor */
15880 return &curwin->w_cursor;
15881 if (name[0] == '\'') /* mark */
15882 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015883 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15885 return NULL;
15886 return pp;
15887 }
Bram Moolenaara5525202006-03-02 22:52:09 +000015888
15889#ifdef FEAT_VIRTUALEDIT
15890 pos.coladd = 0;
15891#endif
15892
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015893 if (name[0] == 'w' && lnum)
15894 {
15895 pos.col = 0;
15896 if (name[1] == '0') /* "w0": first visible line */
15897 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015898 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015899 pos.lnum = curwin->w_topline;
15900 return &pos;
15901 }
15902 else if (name[1] == '$') /* "w$": last visible line */
15903 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015904 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015905 pos.lnum = curwin->w_botline - 1;
15906 return &pos;
15907 }
15908 }
15909 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910 {
15911 if (lnum)
15912 {
15913 pos.lnum = curbuf->b_ml.ml_line_count;
15914 pos.col = 0;
15915 }
15916 else
15917 {
15918 pos.lnum = curwin->w_cursor.lnum;
15919 pos.col = (colnr_T)STRLEN(ml_get_curline());
15920 }
15921 return &pos;
15922 }
15923 return NULL;
15924}
15925
15926/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015927 * Convert list in "arg" into a position and optional file number.
15928 * When "fnump" is NULL there is no file number, only 3 items.
15929 * Note that the column is passed on as-is, the caller may want to decrement
15930 * it to use 1 for the first column.
15931 * Return FAIL when conversion is not possible, doesn't check the position for
15932 * validity.
15933 */
15934 static int
15935list2fpos(arg, posp, fnump)
15936 typval_T *arg;
15937 pos_T *posp;
15938 int *fnump;
15939{
15940 list_T *l = arg->vval.v_list;
15941 long i = 0;
15942 long n;
15943
15944 /* List must be: [fnum, lnum, col, coladd] */
15945 if (arg->v_type != VAR_LIST || l == NULL
15946 || l->lv_len != (fnump == NULL ? 3 : 4))
15947 return FAIL;
15948
15949 if (fnump != NULL)
15950 {
15951 n = list_find_nr(l, i++, NULL); /* fnum */
15952 if (n < 0)
15953 return FAIL;
15954 if (n == 0)
15955 n = curbuf->b_fnum; /* current buffer */
15956 *fnump = n;
15957 }
15958
15959 n = list_find_nr(l, i++, NULL); /* lnum */
15960 if (n < 0)
15961 return FAIL;
15962 posp->lnum = n;
15963
15964 n = list_find_nr(l, i++, NULL); /* col */
15965 if (n < 0)
15966 return FAIL;
15967 posp->col = n;
15968
15969#ifdef FEAT_VIRTUALEDIT
15970 n = list_find_nr(l, i, NULL);
15971 if (n < 0)
15972 return FAIL;
15973 posp->coladd = n;
15974#endif
15975
15976 return OK;
15977}
15978
15979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980 * Get the length of an environment variable name.
15981 * Advance "arg" to the first character after the name.
15982 * Return 0 for error.
15983 */
15984 static int
15985get_env_len(arg)
15986 char_u **arg;
15987{
15988 char_u *p;
15989 int len;
15990
15991 for (p = *arg; vim_isIDc(*p); ++p)
15992 ;
15993 if (p == *arg) /* no name found */
15994 return 0;
15995
15996 len = (int)(p - *arg);
15997 *arg = p;
15998 return len;
15999}
16000
16001/*
16002 * Get the length of the name of a function or internal variable.
16003 * "arg" is advanced to the first non-white character after the name.
16004 * Return 0 if something is wrong.
16005 */
16006 static int
16007get_id_len(arg)
16008 char_u **arg;
16009{
16010 char_u *p;
16011 int len;
16012
16013 /* Find the end of the name. */
16014 for (p = *arg; eval_isnamec(*p); ++p)
16015 ;
16016 if (p == *arg) /* no name found */
16017 return 0;
16018
16019 len = (int)(p - *arg);
16020 *arg = skipwhite(p);
16021
16022 return len;
16023}
16024
16025/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016026 * Get the length of the name of a variable or function.
16027 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016029 * Return -1 if curly braces expansion failed.
16030 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 * If the name contains 'magic' {}'s, expand them and return the
16032 * expanded name in an allocated string via 'alias' - caller must free.
16033 */
16034 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016035get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 char_u **arg;
16037 char_u **alias;
16038 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016039 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040{
16041 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 char_u *p;
16043 char_u *expr_start;
16044 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045
16046 *alias = NULL; /* default to no alias */
16047
16048 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16049 && (*arg)[2] == (int)KE_SNR)
16050 {
16051 /* hard coded <SNR>, already translated */
16052 *arg += 3;
16053 return get_id_len(arg) + 3;
16054 }
16055 len = eval_fname_script(*arg);
16056 if (len > 0)
16057 {
16058 /* literal "<SID>", "s:" or "<SNR>" */
16059 *arg += len;
16060 }
16061
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016063 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016065 p = find_name_end(*arg, &expr_start, &expr_end,
16066 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067 if (expr_start != NULL)
16068 {
16069 char_u *temp_string;
16070
16071 if (!evaluate)
16072 {
16073 len += (int)(p - *arg);
16074 *arg = skipwhite(p);
16075 return len;
16076 }
16077
16078 /*
16079 * Include any <SID> etc in the expanded string:
16080 * Thus the -len here.
16081 */
16082 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16083 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016084 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 *alias = temp_string;
16086 *arg = skipwhite(p);
16087 return (int)STRLEN(temp_string);
16088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089
16090 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016091 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092 EMSG2(_(e_invexpr2), *arg);
16093
16094 return len;
16095}
16096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016097/*
16098 * Find the end of a variable or function name, taking care of magic braces.
16099 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16100 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016101 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016102 * Return a pointer to just after the name. Equal to "arg" if there is no
16103 * valid name.
16104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016106find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 char_u *arg;
16108 char_u **expr_start;
16109 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016110 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016112 int mb_nest = 0;
16113 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 char_u *p;
16115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016116 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016118 *expr_start = NULL;
16119 *expr_end = NULL;
16120 }
16121
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016122 /* Quick check for valid starting character. */
16123 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16124 return arg;
16125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016126 for (p = arg; *p != NUL
16127 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016128 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016129 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016130 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016131 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016132 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016133 if (*p == '\'')
16134 {
16135 /* skip over 'string' to avoid counting [ and ] inside it. */
16136 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16137 ;
16138 if (*p == NUL)
16139 break;
16140 }
16141 else if (*p == '"')
16142 {
16143 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16144 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16145 if (*p == '\\' && p[1] != NUL)
16146 ++p;
16147 if (*p == NUL)
16148 break;
16149 }
16150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016151 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016153 if (*p == '[')
16154 ++br_nest;
16155 else if (*p == ']')
16156 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016159 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016161 if (*p == '{')
16162 {
16163 mb_nest++;
16164 if (expr_start != NULL && *expr_start == NULL)
16165 *expr_start = p;
16166 }
16167 else if (*p == '}')
16168 {
16169 mb_nest--;
16170 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16171 *expr_end = p;
16172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 }
16175
16176 return p;
16177}
16178
16179/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016180 * Expands out the 'magic' {}'s in a variable/function name.
16181 * Note that this can call itself recursively, to deal with
16182 * constructs like foo{bar}{baz}{bam}
16183 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16184 * "in_start" ^
16185 * "expr_start" ^
16186 * "expr_end" ^
16187 * "in_end" ^
16188 *
16189 * Returns a new allocated string, which the caller must free.
16190 * Returns NULL for failure.
16191 */
16192 static char_u *
16193make_expanded_name(in_start, expr_start, expr_end, in_end)
16194 char_u *in_start;
16195 char_u *expr_start;
16196 char_u *expr_end;
16197 char_u *in_end;
16198{
16199 char_u c1;
16200 char_u *retval = NULL;
16201 char_u *temp_result;
16202 char_u *nextcmd = NULL;
16203
16204 if (expr_end == NULL || in_end == NULL)
16205 return NULL;
16206 *expr_start = NUL;
16207 *expr_end = NUL;
16208 c1 = *in_end;
16209 *in_end = NUL;
16210
16211 temp_result = eval_to_string(expr_start + 1, &nextcmd);
16212 if (temp_result != NULL && nextcmd == NULL)
16213 {
16214 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16215 + (in_end - expr_end) + 1));
16216 if (retval != NULL)
16217 {
16218 STRCPY(retval, in_start);
16219 STRCAT(retval, temp_result);
16220 STRCAT(retval, expr_end + 1);
16221 }
16222 }
16223 vim_free(temp_result);
16224
16225 *in_end = c1; /* put char back for error messages */
16226 *expr_start = '{';
16227 *expr_end = '}';
16228
16229 if (retval != NULL)
16230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016231 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016232 if (expr_start != NULL)
16233 {
16234 /* Further expansion! */
16235 temp_result = make_expanded_name(retval, expr_start,
16236 expr_end, temp_result);
16237 vim_free(retval);
16238 retval = temp_result;
16239 }
16240 }
16241
16242 return retval;
16243}
16244
16245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016247 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 */
16249 static int
16250eval_isnamec(c)
16251 int c;
16252{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016253 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16254}
16255
16256/*
16257 * Return TRUE if character "c" can be used as the first character in a
16258 * variable or function name (excluding '{' and '}').
16259 */
16260 static int
16261eval_isnamec1(c)
16262 int c;
16263{
16264 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265}
16266
16267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 * Set number v: variable to "val".
16269 */
16270 void
16271set_vim_var_nr(idx, val)
16272 int idx;
16273 long val;
16274{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016275 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276}
16277
16278/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016279 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280 */
16281 long
16282get_vim_var_nr(idx)
16283 int idx;
16284{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016285 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286}
16287
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016288#if defined(FEAT_AUTOCMD) || defined(PROTO)
16289/*
16290 * Get string v: variable value. Uses a static buffer, can only be used once.
16291 */
16292 char_u *
16293get_vim_var_str(idx)
16294 int idx;
16295{
16296 return get_tv_string(&vimvars[idx].vv_tv);
16297}
16298#endif
16299
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300/*
16301 * Set v:count, v:count1 and v:prevcount.
16302 */
16303 void
16304set_vcount(count, count1)
16305 long count;
16306 long count1;
16307{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016308 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16309 vimvars[VV_COUNT].vv_nr = count;
16310 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311}
16312
16313/*
16314 * Set string v: variable to a copy of "val".
16315 */
16316 void
16317set_vim_var_string(idx, val, len)
16318 int idx;
16319 char_u *val;
16320 int len; /* length of "val" to use or -1 (whole string) */
16321{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016322 /* Need to do this (at least) once, since we can't initialize a union.
16323 * Will always be invoked when "v:progname" is set. */
16324 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16325
Bram Moolenaare9a41262005-01-15 22:18:47 +000016326 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016328 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016330 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016332 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333}
16334
16335/*
16336 * Set v:register if needed.
16337 */
16338 void
16339set_reg_var(c)
16340 int c;
16341{
16342 char_u regname;
16343
16344 if (c == 0 || c == ' ')
16345 regname = '"';
16346 else
16347 regname = c;
16348 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016349 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 set_vim_var_string(VV_REG, &regname, 1);
16351}
16352
16353/*
16354 * Get or set v:exception. If "oldval" == NULL, return the current value.
16355 * Otherwise, restore the value to "oldval" and return NULL.
16356 * Must always be called in pairs to save and restore v:exception! Does not
16357 * take care of memory allocations.
16358 */
16359 char_u *
16360v_exception(oldval)
16361 char_u *oldval;
16362{
16363 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016364 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365
Bram Moolenaare9a41262005-01-15 22:18:47 +000016366 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367 return NULL;
16368}
16369
16370/*
16371 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16372 * Otherwise, restore the value to "oldval" and return NULL.
16373 * Must always be called in pairs to save and restore v:throwpoint! Does not
16374 * take care of memory allocations.
16375 */
16376 char_u *
16377v_throwpoint(oldval)
16378 char_u *oldval;
16379{
16380 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016381 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382
Bram Moolenaare9a41262005-01-15 22:18:47 +000016383 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384 return NULL;
16385}
16386
16387#if defined(FEAT_AUTOCMD) || defined(PROTO)
16388/*
16389 * Set v:cmdarg.
16390 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16391 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16392 * Must always be called in pairs!
16393 */
16394 char_u *
16395set_cmdarg(eap, oldarg)
16396 exarg_T *eap;
16397 char_u *oldarg;
16398{
16399 char_u *oldval;
16400 char_u *newval;
16401 unsigned len;
16402
Bram Moolenaare9a41262005-01-15 22:18:47 +000016403 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016404 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016406 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016407 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016408 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 }
16410
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016411 if (eap->force_bin == FORCE_BIN)
16412 len = 6;
16413 else if (eap->force_bin == FORCE_NOBIN)
16414 len = 8;
16415 else
16416 len = 0;
16417 if (eap->force_ff != 0)
16418 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16419# ifdef FEAT_MBYTE
16420 if (eap->force_enc != 0)
16421 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016422 if (eap->bad_char != 0)
16423 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016424# endif
16425
16426 newval = alloc(len + 1);
16427 if (newval == NULL)
16428 return NULL;
16429
16430 if (eap->force_bin == FORCE_BIN)
16431 sprintf((char *)newval, " ++bin");
16432 else if (eap->force_bin == FORCE_NOBIN)
16433 sprintf((char *)newval, " ++nobin");
16434 else
16435 *newval = NUL;
16436 if (eap->force_ff != 0)
16437 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16438 eap->cmd + eap->force_ff);
16439# ifdef FEAT_MBYTE
16440 if (eap->force_enc != 0)
16441 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16442 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016443 if (eap->bad_char != 0)
16444 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16445 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016446# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016447 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016448 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449}
16450#endif
16451
16452/*
16453 * Get the value of internal variable "name".
16454 * Return OK or FAIL.
16455 */
16456 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016457get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 char_u *name;
16459 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016460 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016461 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462{
16463 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016464 typval_T *tv = NULL;
16465 typval_T atv;
16466 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468
16469 /* truncate the name, so that we can use strcmp() */
16470 cc = name[len];
16471 name[len] = NUL;
16472
16473 /*
16474 * Check for "b:changedtick".
16475 */
16476 if (STRCMP(name, "b:changedtick") == 0)
16477 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016478 atv.v_type = VAR_NUMBER;
16479 atv.vval.v_number = curbuf->b_changedtick;
16480 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 }
16482
16483 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484 * Check for user-defined variables.
16485 */
16486 else
16487 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016488 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 }
16492
Bram Moolenaare9a41262005-01-15 22:18:47 +000016493 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016495 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 ret = FAIL;
16498 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016499 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016500 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501
16502 name[len] = cc;
16503
16504 return ret;
16505}
16506
16507/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016508 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16509 * Also handle function call with Funcref variable: func(expr)
16510 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16511 */
16512 static int
16513handle_subscript(arg, rettv, evaluate, verbose)
16514 char_u **arg;
16515 typval_T *rettv;
16516 int evaluate; /* do more than finding the end */
16517 int verbose; /* give error messages */
16518{
16519 int ret = OK;
16520 dict_T *selfdict = NULL;
16521 char_u *s;
16522 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016523 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016524
16525 while (ret == OK
16526 && (**arg == '['
16527 || (**arg == '.' && rettv->v_type == VAR_DICT)
16528 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16529 && !vim_iswhite(*(*arg - 1)))
16530 {
16531 if (**arg == '(')
16532 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016533 /* need to copy the funcref so that we can clear rettv */
16534 functv = *rettv;
16535 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016536
16537 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016538 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016539 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016540 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16541 &len, evaluate, selfdict);
16542
16543 /* Clear the funcref afterwards, so that deleting it while
16544 * evaluating the arguments is possible (see test55). */
16545 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016546
16547 /* Stop the expression evaluation when immediately aborting on
16548 * error, or when an interrupt occurred or an exception was thrown
16549 * but not caught. */
16550 if (aborting())
16551 {
16552 if (ret == OK)
16553 clear_tv(rettv);
16554 ret = FAIL;
16555 }
16556 dict_unref(selfdict);
16557 selfdict = NULL;
16558 }
16559 else /* **arg == '[' || **arg == '.' */
16560 {
16561 dict_unref(selfdict);
16562 if (rettv->v_type == VAR_DICT)
16563 {
16564 selfdict = rettv->vval.v_dict;
16565 if (selfdict != NULL)
16566 ++selfdict->dv_refcount;
16567 }
16568 else
16569 selfdict = NULL;
16570 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16571 {
16572 clear_tv(rettv);
16573 ret = FAIL;
16574 }
16575 }
16576 }
16577 dict_unref(selfdict);
16578 return ret;
16579}
16580
16581/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016582 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16583 * value).
16584 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016585 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016586alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016587{
Bram Moolenaar33570922005-01-25 22:26:29 +000016588 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016589}
16590
16591/*
16592 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 * The string "s" must have been allocated, it is consumed.
16594 * Return NULL for out of memory, the variable otherwise.
16595 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016597alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598 char_u *s;
16599{
Bram Moolenaar33570922005-01-25 22:26:29 +000016600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016602 rettv = alloc_tv();
16603 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016605 rettv->v_type = VAR_STRING;
16606 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607 }
16608 else
16609 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016610 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611}
16612
16613/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016614 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016616 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016617free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016618 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619{
16620 if (varp != NULL)
16621 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016622 switch (varp->v_type)
16623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016624 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016625 func_unref(varp->vval.v_string);
16626 /*FALLTHROUGH*/
16627 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016628 vim_free(varp->vval.v_string);
16629 break;
16630 case VAR_LIST:
16631 list_unref(varp->vval.v_list);
16632 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016633 case VAR_DICT:
16634 dict_unref(varp->vval.v_dict);
16635 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016636 case VAR_NUMBER:
16637 case VAR_UNKNOWN:
16638 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016639 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016640 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016641 break;
16642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 vim_free(varp);
16644 }
16645}
16646
16647/*
16648 * Free the memory for a variable value and set the value to NULL or 0.
16649 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016650 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016652 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653{
16654 if (varp != NULL)
16655 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016656 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016658 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016659 func_unref(varp->vval.v_string);
16660 /*FALLTHROUGH*/
16661 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016662 vim_free(varp->vval.v_string);
16663 varp->vval.v_string = NULL;
16664 break;
16665 case VAR_LIST:
16666 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016667 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016668 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016669 case VAR_DICT:
16670 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016671 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016672 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016673 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016674 varp->vval.v_number = 0;
16675 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016676 case VAR_UNKNOWN:
16677 break;
16678 default:
16679 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016681 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 }
16683}
16684
16685/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016686 * Set the value of a variable to NULL without freeing items.
16687 */
16688 static void
16689init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016690 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016691{
16692 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016693 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016694}
16695
16696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 * Get the number value of a variable.
16698 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016699 * For incompatible types, return 0.
16700 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16701 * caller of incompatible types: it sets *denote to TRUE if "denote"
16702 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703 */
16704 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016705get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016706 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016708 int error = FALSE;
16709
16710 return get_tv_number_chk(varp, &error); /* return 0L on error */
16711}
16712
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016713 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016714get_tv_number_chk(varp, denote)
16715 typval_T *varp;
16716 int *denote;
16717{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016718 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016720 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016722 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016723 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016724 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016725 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016726 break;
16727 case VAR_STRING:
16728 if (varp->vval.v_string != NULL)
16729 vim_str2nr(varp->vval.v_string, NULL, NULL,
16730 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016731 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016732 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016733 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016734 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016735 case VAR_DICT:
16736 EMSG(_("E728: Using a Dictionary as a number"));
16737 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016738 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016739 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016740 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 if (denote == NULL) /* useful for values that must be unsigned */
16743 n = -1;
16744 else
16745 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016746 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747}
16748
16749/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016750 * Get the lnum from the first argument.
16751 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016752 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 */
16754 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016755get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016756 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757{
Bram Moolenaar33570922005-01-25 22:26:29 +000016758 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 linenr_T lnum;
16760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016761 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762 if (lnum == 0) /* no valid number, try using line() */
16763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016764 rettv.v_type = VAR_NUMBER;
16765 f_line(argvars, &rettv);
16766 lnum = rettv.vval.v_number;
16767 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 }
16769 return lnum;
16770}
16771
16772/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016773 * Get the lnum from the first argument.
16774 * Also accepts "$", then "buf" is used.
16775 * Returns 0 on error.
16776 */
16777 static linenr_T
16778get_tv_lnum_buf(argvars, buf)
16779 typval_T *argvars;
16780 buf_T *buf;
16781{
16782 if (argvars[0].v_type == VAR_STRING
16783 && argvars[0].vval.v_string != NULL
16784 && argvars[0].vval.v_string[0] == '$'
16785 && buf != NULL)
16786 return buf->b_ml.ml_line_count;
16787 return get_tv_number_chk(&argvars[0], NULL);
16788}
16789
16790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 * Get the string value of a variable.
16792 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016793 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16794 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 * If the String variable has never been set, return an empty string.
16796 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016797 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16798 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 */
16800 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016801get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016802 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803{
16804 static char_u mybuf[NUMBUFLEN];
16805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016806 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807}
16808
16809 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016810get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016811 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016812 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016814 char_u *res = get_tv_string_buf_chk(varp, buf);
16815
16816 return res != NULL ? res : (char_u *)"";
16817}
16818
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016819 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016820get_tv_string_chk(varp)
16821 typval_T *varp;
16822{
16823 static char_u mybuf[NUMBUFLEN];
16824
16825 return get_tv_string_buf_chk(varp, mybuf);
16826}
16827
16828 static char_u *
16829get_tv_string_buf_chk(varp, buf)
16830 typval_T *varp;
16831 char_u *buf;
16832{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016833 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016835 case VAR_NUMBER:
16836 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16837 return buf;
16838 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016839 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016840 break;
16841 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016842 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016843 break;
16844 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016845 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016846 break;
16847 case VAR_STRING:
16848 if (varp->vval.v_string != NULL)
16849 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016850 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016851 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016852 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016853 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016855 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856}
16857
16858/*
16859 * Find variable "name" in the list of variables.
16860 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016861 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016862 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016865 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016866find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016868 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016871 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872
Bram Moolenaara7043832005-01-21 11:56:39 +000016873 ht = find_var_ht(name, &varname);
16874 if (htp != NULL)
16875 *htp = ht;
16876 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016878 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879}
16880
16881/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016882 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016883 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016885 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016886find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016887 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016888 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016889 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016890{
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 hashitem_T *hi;
16892
16893 if (*varname == NUL)
16894 {
16895 /* Must be something like "s:", otherwise "ht" would be NULL. */
16896 switch (varname[-2])
16897 {
16898 case 's': return &SCRIPT_SV(current_SID).sv_var;
16899 case 'g': return &globvars_var;
16900 case 'v': return &vimvars_var;
16901 case 'b': return &curbuf->b_bufvar;
16902 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016903 case 'l': return current_funccal == NULL
16904 ? NULL : &current_funccal->l_vars_var;
16905 case 'a': return current_funccal == NULL
16906 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016907 }
16908 return NULL;
16909 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016910
16911 hi = hash_find(ht, varname);
16912 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016913 {
16914 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016915 * worked find the variable again. Don't auto-load a script if it was
16916 * loaded already, otherwise it would be loaded every time when
16917 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016918 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016919 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016920 hi = hash_find(ht, varname);
16921 if (HASHITEM_EMPTY(hi))
16922 return NULL;
16923 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016924 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016925}
16926
16927/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016928 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016929 * Set "varname" to the start of name without ':'.
16930 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016931 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016932find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 char_u *name;
16934 char_u **varname;
16935{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016936 hashitem_T *hi;
16937
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 if (name[1] != ':')
16939 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016940 /* The name must not start with a colon or #. */
16941 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 return NULL;
16943 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016944
16945 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016946 hi = hash_find(&compat_hashtab, name);
16947 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016948 return &compat_hashtab;
16949
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016951 return &globvarht; /* global variable */
16952 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 }
16954 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016955 if (*name == 'g') /* global variable */
16956 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016957 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16958 */
16959 if (vim_strchr(name + 2, ':') != NULL
16960 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016961 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016963 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016965 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016966 if (*name == 'v') /* v: variable */
16967 return &vimvarht;
16968 if (*name == 'a' && current_funccal != NULL) /* function argument */
16969 return &current_funccal->l_avars.dv_hashtab;
16970 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16971 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 if (*name == 's' /* script variable */
16973 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16974 return &SCRIPT_VARS(current_SID);
16975 return NULL;
16976}
16977
16978/*
16979 * Get the string value of a (global/local) variable.
16980 * Returns NULL when it doesn't exist.
16981 */
16982 char_u *
16983get_var_value(name)
16984 char_u *name;
16985{
Bram Moolenaar33570922005-01-25 22:26:29 +000016986 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987
Bram Moolenaara7043832005-01-21 11:56:39 +000016988 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 if (v == NULL)
16990 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016991 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992}
16993
16994/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016995 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 * sourcing this script and when executing functions defined in the script.
16997 */
16998 void
16999new_script_vars(id)
17000 scid_T id;
17001{
Bram Moolenaara7043832005-01-21 11:56:39 +000017002 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017003 hashtab_T *ht;
17004 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017005
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17007 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017008 /* Re-allocating ga_data means that an ht_array pointing to
17009 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017010 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017011 for (i = 1; i <= ga_scripts.ga_len; ++i)
17012 {
17013 ht = &SCRIPT_VARS(i);
17014 if (ht->ht_mask == HT_INIT_SIZE - 1)
17015 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017016 sv = &SCRIPT_SV(i);
17017 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017018 }
17019
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 while (ga_scripts.ga_len < id)
17021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017022 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17023 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 }
17026 }
17027}
17028
17029/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017030 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17031 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032 */
17033 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017034init_var_dict(dict, dict_var)
17035 dict_T *dict;
17036 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037{
Bram Moolenaar33570922005-01-25 22:26:29 +000017038 hash_init(&dict->dv_hashtab);
17039 dict->dv_refcount = 99999;
17040 dict_var->di_tv.vval.v_dict = dict;
17041 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017042 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17044 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045}
17046
17047/*
17048 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017049 * Frees all allocated variables and the value they contain.
17050 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 */
17052 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017053vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017054 hashtab_T *ht;
17055{
17056 vars_clear_ext(ht, TRUE);
17057}
17058
17059/*
17060 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17061 */
17062 static void
17063vars_clear_ext(ht, free_val)
17064 hashtab_T *ht;
17065 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066{
Bram Moolenaara7043832005-01-21 11:56:39 +000017067 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017068 hashitem_T *hi;
17069 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070
Bram Moolenaar33570922005-01-25 22:26:29 +000017071 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017072 todo = ht->ht_used;
17073 for (hi = ht->ht_array; todo > 0; ++hi)
17074 {
17075 if (!HASHITEM_EMPTY(hi))
17076 {
17077 --todo;
17078
Bram Moolenaar33570922005-01-25 22:26:29 +000017079 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017080 * ht_array might change then. hash_clear() takes care of it
17081 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017082 v = HI2DI(hi);
17083 if (free_val)
17084 clear_tv(&v->di_tv);
17085 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17086 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017087 }
17088 }
17089 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017090 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091}
17092
Bram Moolenaara7043832005-01-21 11:56:39 +000017093/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017094 * Delete a variable from hashtab "ht" at item "hi".
17095 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017098delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017099 hashtab_T *ht;
17100 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101{
Bram Moolenaar33570922005-01-25 22:26:29 +000017102 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017103
17104 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 clear_tv(&di->di_tv);
17106 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107}
17108
17109/*
17110 * List the value of one internal variable.
17111 */
17112 static void
17113list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017114 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 char_u *prefix;
17116{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017117 char_u *tofree;
17118 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017119 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017120
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017121 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017122 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017123 s == NULL ? (char_u *)"" : s);
17124 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125}
17126
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 static void
17128list_one_var_a(prefix, name, type, string)
17129 char_u *prefix;
17130 char_u *name;
17131 int type;
17132 char_u *string;
17133{
17134 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17135 if (name != NULL) /* "a:" vars don't have a name stored */
17136 msg_puts(name);
17137 msg_putchar(' ');
17138 msg_advance(22);
17139 if (type == VAR_NUMBER)
17140 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017141 else if (type == VAR_FUNC)
17142 msg_putchar('*');
17143 else if (type == VAR_LIST)
17144 {
17145 msg_putchar('[');
17146 if (*string == '[')
17147 ++string;
17148 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017149 else if (type == VAR_DICT)
17150 {
17151 msg_putchar('{');
17152 if (*string == '{')
17153 ++string;
17154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 else
17156 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017157
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017159
17160 if (type == VAR_FUNC)
17161 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162}
17163
17164/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 * If the variable already exists, the value is updated.
17167 * Otherwise the variable is created.
17168 */
17169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017170set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017172 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174{
Bram Moolenaar33570922005-01-25 22:26:29 +000017175 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017177 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017178 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017180 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017181 {
17182 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17183 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17184 ? name[2] : name[0]))
17185 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017186 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017187 return;
17188 }
17189 if (function_exists(name))
17190 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017191 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017192 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017193 return;
17194 }
17195 }
17196
Bram Moolenaara7043832005-01-21 11:56:39 +000017197 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017198 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017199 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017200 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017201 return;
17202 }
17203
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017204 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017205 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017207 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017208 if (var_check_ro(v->di_flags, name)
17209 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017210 return;
17211 if (v->di_tv.v_type != tv->v_type
17212 && !((v->di_tv.v_type == VAR_STRING
17213 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017214 && (tv->v_type == VAR_STRING
17215 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017216 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017217 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017218 return;
17219 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017220
17221 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017222 * Handle setting internal v: variables separately: we don't change
17223 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017224 */
17225 if (ht == &vimvarht)
17226 {
17227 if (v->di_tv.v_type == VAR_STRING)
17228 {
17229 vim_free(v->di_tv.vval.v_string);
17230 if (copy || tv->v_type != VAR_STRING)
17231 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17232 else
17233 {
17234 /* Take over the string to avoid an extra alloc/free. */
17235 v->di_tv.vval.v_string = tv->vval.v_string;
17236 tv->vval.v_string = NULL;
17237 }
17238 }
17239 else if (v->di_tv.v_type != VAR_NUMBER)
17240 EMSG2(_(e_intern2), "set_var()");
17241 else
17242 v->di_tv.vval.v_number = get_tv_number(tv);
17243 return;
17244 }
17245
17246 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247 }
17248 else /* add a new variable */
17249 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017250 /* Make sure the variable name is valid. */
17251 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017252 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17253 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017254 {
17255 EMSG2(_(e_illvar), varname);
17256 return;
17257 }
17258
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017259 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17260 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017261 if (v == NULL)
17262 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017264 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017266 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 return;
17268 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017269 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017272 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017273 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017274 else
17275 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017277 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280}
17281
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017282/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017283 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17284 * Also give an error message.
17285 */
17286 static int
17287var_check_ro(flags, name)
17288 int flags;
17289 char_u *name;
17290{
17291 if (flags & DI_FLAGS_RO)
17292 {
17293 EMSG2(_(e_readonlyvar), name);
17294 return TRUE;
17295 }
17296 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17297 {
17298 EMSG2(_(e_readonlysbx), name);
17299 return TRUE;
17300 }
17301 return FALSE;
17302}
17303
17304/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017305 * Return TRUE if typeval "tv" is set to be locked (immutable).
17306 * Also give an error message, using "name".
17307 */
17308 static int
17309tv_check_lock(lock, name)
17310 int lock;
17311 char_u *name;
17312{
17313 if (lock & VAR_LOCKED)
17314 {
17315 EMSG2(_("E741: Value is locked: %s"),
17316 name == NULL ? (char_u *)_("Unknown") : name);
17317 return TRUE;
17318 }
17319 if (lock & VAR_FIXED)
17320 {
17321 EMSG2(_("E742: Cannot change value of %s"),
17322 name == NULL ? (char_u *)_("Unknown") : name);
17323 return TRUE;
17324 }
17325 return FALSE;
17326}
17327
17328/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017330 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017331 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017334copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017335 typval_T *from;
17336 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017338 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017339 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017340 switch (from->v_type)
17341 {
17342 case VAR_NUMBER:
17343 to->vval.v_number = from->vval.v_number;
17344 break;
17345 case VAR_STRING:
17346 case VAR_FUNC:
17347 if (from->vval.v_string == NULL)
17348 to->vval.v_string = NULL;
17349 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017351 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017352 if (from->v_type == VAR_FUNC)
17353 func_ref(to->vval.v_string);
17354 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017355 break;
17356 case VAR_LIST:
17357 if (from->vval.v_list == NULL)
17358 to->vval.v_list = NULL;
17359 else
17360 {
17361 to->vval.v_list = from->vval.v_list;
17362 ++to->vval.v_list->lv_refcount;
17363 }
17364 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017365 case VAR_DICT:
17366 if (from->vval.v_dict == NULL)
17367 to->vval.v_dict = NULL;
17368 else
17369 {
17370 to->vval.v_dict = from->vval.v_dict;
17371 ++to->vval.v_dict->dv_refcount;
17372 }
17373 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017374 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017376 break;
17377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378}
17379
17380/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017381 * Make a copy of an item.
17382 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017383 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17384 * reference to an already copied list/dict can be used.
17385 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017386 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017387 static int
17388item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017389 typval_T *from;
17390 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017391 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017392 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017393{
17394 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017395 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017396
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017398 {
17399 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017400 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017401 }
17402 ++recurse;
17403
17404 switch (from->v_type)
17405 {
17406 case VAR_NUMBER:
17407 case VAR_STRING:
17408 case VAR_FUNC:
17409 copy_tv(from, to);
17410 break;
17411 case VAR_LIST:
17412 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017413 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017414 if (from->vval.v_list == NULL)
17415 to->vval.v_list = NULL;
17416 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17417 {
17418 /* use the copy made earlier */
17419 to->vval.v_list = from->vval.v_list->lv_copylist;
17420 ++to->vval.v_list->lv_refcount;
17421 }
17422 else
17423 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17424 if (to->vval.v_list == NULL)
17425 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017426 break;
17427 case VAR_DICT:
17428 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017429 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017430 if (from->vval.v_dict == NULL)
17431 to->vval.v_dict = NULL;
17432 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17433 {
17434 /* use the copy made earlier */
17435 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17436 ++to->vval.v_dict->dv_refcount;
17437 }
17438 else
17439 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17440 if (to->vval.v_dict == NULL)
17441 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017442 break;
17443 default:
17444 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017445 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017446 }
17447 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017448 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017449}
17450
17451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 * ":echo expr1 ..." print each argument separated with a space, add a
17453 * newline at the end.
17454 * ":echon expr1 ..." print each argument plain.
17455 */
17456 void
17457ex_echo(eap)
17458 exarg_T *eap;
17459{
17460 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017462 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463 char_u *p;
17464 int needclr = TRUE;
17465 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017466 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467
17468 if (eap->skip)
17469 ++emsg_skip;
17470 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17471 {
17472 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017473 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 {
17475 /*
17476 * Report the invalid expression unless the expression evaluation
17477 * has been cancelled due to an aborting error, an interrupt, or an
17478 * exception.
17479 */
17480 if (!aborting())
17481 EMSG2(_(e_invexpr2), p);
17482 break;
17483 }
17484 if (!eap->skip)
17485 {
17486 if (atstart)
17487 {
17488 atstart = FALSE;
17489 /* Call msg_start() after eval1(), evaluating the expression
17490 * may cause a message to appear. */
17491 if (eap->cmdidx == CMD_echo)
17492 msg_start();
17493 }
17494 else if (eap->cmdidx == CMD_echo)
17495 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017496 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017497 if (p != NULL)
17498 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017500 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017502 if (*p != TAB && needclr)
17503 {
17504 /* remove any text still there from the command */
17505 msg_clr_eos();
17506 needclr = FALSE;
17507 }
17508 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 }
17510 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017511 {
17512#ifdef FEAT_MBYTE
17513 if (has_mbyte)
17514 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017515 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017516
17517 (void)msg_outtrans_len_attr(p, i, echo_attr);
17518 p += i - 1;
17519 }
17520 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017522 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017525 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017527 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 arg = skipwhite(arg);
17529 }
17530 eap->nextcmd = check_nextcmd(arg);
17531
17532 if (eap->skip)
17533 --emsg_skip;
17534 else
17535 {
17536 /* remove text that may still be there from the command */
17537 if (needclr)
17538 msg_clr_eos();
17539 if (eap->cmdidx == CMD_echo)
17540 msg_end();
17541 }
17542}
17543
17544/*
17545 * ":echohl {name}".
17546 */
17547 void
17548ex_echohl(eap)
17549 exarg_T *eap;
17550{
17551 int id;
17552
17553 id = syn_name2id(eap->arg);
17554 if (id == 0)
17555 echo_attr = 0;
17556 else
17557 echo_attr = syn_id2attr(id);
17558}
17559
17560/*
17561 * ":execute expr1 ..." execute the result of an expression.
17562 * ":echomsg expr1 ..." Print a message
17563 * ":echoerr expr1 ..." Print an error
17564 * Each gets spaces around each argument and a newline at the end for
17565 * echo commands
17566 */
17567 void
17568ex_execute(eap)
17569 exarg_T *eap;
17570{
17571 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017572 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 int ret = OK;
17574 char_u *p;
17575 garray_T ga;
17576 int len;
17577 int save_did_emsg;
17578
17579 ga_init2(&ga, 1, 80);
17580
17581 if (eap->skip)
17582 ++emsg_skip;
17583 while (*arg != NUL && *arg != '|' && *arg != '\n')
17584 {
17585 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 {
17588 /*
17589 * Report the invalid expression unless the expression evaluation
17590 * has been cancelled due to an aborting error, an interrupt, or an
17591 * exception.
17592 */
17593 if (!aborting())
17594 EMSG2(_(e_invexpr2), p);
17595 ret = FAIL;
17596 break;
17597 }
17598
17599 if (!eap->skip)
17600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017601 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 len = (int)STRLEN(p);
17603 if (ga_grow(&ga, len + 2) == FAIL)
17604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017605 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 ret = FAIL;
17607 break;
17608 }
17609 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 ga.ga_len += len;
17613 }
17614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017615 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 arg = skipwhite(arg);
17617 }
17618
17619 if (ret != FAIL && ga.ga_data != NULL)
17620 {
17621 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017624 out_flush();
17625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 else if (eap->cmdidx == CMD_echoerr)
17627 {
17628 /* We don't want to abort following commands, restore did_emsg. */
17629 save_did_emsg = did_emsg;
17630 EMSG((char_u *)ga.ga_data);
17631 if (!force_abort)
17632 did_emsg = save_did_emsg;
17633 }
17634 else if (eap->cmdidx == CMD_execute)
17635 do_cmdline((char_u *)ga.ga_data,
17636 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17637 }
17638
17639 ga_clear(&ga);
17640
17641 if (eap->skip)
17642 --emsg_skip;
17643
17644 eap->nextcmd = check_nextcmd(arg);
17645}
17646
17647/*
17648 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17649 * "arg" points to the "&" or '+' when called, to "option" when returning.
17650 * Returns NULL when no option name found. Otherwise pointer to the char
17651 * after the option name.
17652 */
17653 static char_u *
17654find_option_end(arg, opt_flags)
17655 char_u **arg;
17656 int *opt_flags;
17657{
17658 char_u *p = *arg;
17659
17660 ++p;
17661 if (*p == 'g' && p[1] == ':')
17662 {
17663 *opt_flags = OPT_GLOBAL;
17664 p += 2;
17665 }
17666 else if (*p == 'l' && p[1] == ':')
17667 {
17668 *opt_flags = OPT_LOCAL;
17669 p += 2;
17670 }
17671 else
17672 *opt_flags = 0;
17673
17674 if (!ASCII_ISALPHA(*p))
17675 return NULL;
17676 *arg = p;
17677
17678 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17679 p += 4; /* termcap option */
17680 else
17681 while (ASCII_ISALPHA(*p))
17682 ++p;
17683 return p;
17684}
17685
17686/*
17687 * ":function"
17688 */
17689 void
17690ex_function(eap)
17691 exarg_T *eap;
17692{
17693 char_u *theline;
17694 int j;
17695 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697 char_u *name = NULL;
17698 char_u *p;
17699 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017700 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 garray_T newargs;
17702 garray_T newlines;
17703 int varargs = FALSE;
17704 int mustend = FALSE;
17705 int flags = 0;
17706 ufunc_T *fp;
17707 int indent;
17708 int nesting;
17709 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017710 dictitem_T *v;
17711 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017712 static int func_nr = 0; /* number for nameless function */
17713 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017714 hashtab_T *ht;
17715 int todo;
17716 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017717 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718
17719 /*
17720 * ":function" without argument: list functions.
17721 */
17722 if (ends_excmd(*eap->arg))
17723 {
17724 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017725 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017726 todo = func_hashtab.ht_used;
17727 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017728 {
17729 if (!HASHITEM_EMPTY(hi))
17730 {
17731 --todo;
17732 fp = HI2UF(hi);
17733 if (!isdigit(*fp->uf_name))
17734 list_func_head(fp, FALSE);
17735 }
17736 }
17737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 eap->nextcmd = check_nextcmd(eap->arg);
17739 return;
17740 }
17741
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017742 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017743 * ":function /pat": list functions matching pattern.
17744 */
17745 if (*eap->arg == '/')
17746 {
17747 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17748 if (!eap->skip)
17749 {
17750 regmatch_T regmatch;
17751
17752 c = *p;
17753 *p = NUL;
17754 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17755 *p = c;
17756 if (regmatch.regprog != NULL)
17757 {
17758 regmatch.rm_ic = p_ic;
17759
17760 todo = func_hashtab.ht_used;
17761 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17762 {
17763 if (!HASHITEM_EMPTY(hi))
17764 {
17765 --todo;
17766 fp = HI2UF(hi);
17767 if (!isdigit(*fp->uf_name)
17768 && vim_regexec(&regmatch, fp->uf_name, 0))
17769 list_func_head(fp, FALSE);
17770 }
17771 }
17772 }
17773 }
17774 if (*p == '/')
17775 ++p;
17776 eap->nextcmd = check_nextcmd(p);
17777 return;
17778 }
17779
17780 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017781 * Get the function name. There are these situations:
17782 * func normal function name
17783 * "name" == func, "fudi.fd_dict" == NULL
17784 * dict.func new dictionary entry
17785 * "name" == NULL, "fudi.fd_dict" set,
17786 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17787 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017788 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017789 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17790 * dict.func existing dict entry that's not a Funcref
17791 * "name" == NULL, "fudi.fd_dict" set,
17792 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017795 name = trans_function_name(&p, eap->skip, 0, &fudi);
17796 paren = (vim_strchr(p, '(') != NULL);
17797 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 {
17799 /*
17800 * Return on an invalid expression in braces, unless the expression
17801 * evaluation has been cancelled due to an aborting error, an
17802 * interrupt, or an exception.
17803 */
17804 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017805 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017806 if (!eap->skip && fudi.fd_newkey != NULL)
17807 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017808 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 else
17812 eap->skip = TRUE;
17813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 /* An error in a function call during evaluation of an expression in magic
17815 * braces should not cause the function not to be defined. */
17816 saved_did_emsg = did_emsg;
17817 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818
17819 /*
17820 * ":function func" with only function name: list function.
17821 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017822 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 {
17824 if (!ends_excmd(*skipwhite(p)))
17825 {
17826 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017827 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 }
17829 eap->nextcmd = check_nextcmd(p);
17830 if (eap->nextcmd != NULL)
17831 *p = NUL;
17832 if (!eap->skip && !got_int)
17833 {
17834 fp = find_func(name);
17835 if (fp != NULL)
17836 {
17837 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017838 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017840 if (FUNCLINE(fp, j) == NULL)
17841 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 msg_putchar('\n');
17843 msg_outnum((long)(j + 1));
17844 if (j < 9)
17845 msg_putchar(' ');
17846 if (j < 99)
17847 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017848 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 out_flush(); /* show a line at a time */
17850 ui_breakcheck();
17851 }
17852 if (!got_int)
17853 {
17854 msg_putchar('\n');
17855 msg_puts((char_u *)" endfunction");
17856 }
17857 }
17858 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017859 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017861 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 }
17863
17864 /*
17865 * ":function name(arg1, arg2)" Define function.
17866 */
17867 p = skipwhite(p);
17868 if (*p != '(')
17869 {
17870 if (!eap->skip)
17871 {
17872 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017873 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 }
17875 /* attempt to continue by skipping some text */
17876 if (vim_strchr(p, '(') != NULL)
17877 p = vim_strchr(p, '(');
17878 }
17879 p = skipwhite(p + 1);
17880
17881 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17882 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17883
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017884 if (!eap->skip)
17885 {
17886 /* Check the name of the function. */
17887 if (name != NULL)
17888 arg = name;
17889 else
17890 arg = fudi.fd_newkey;
17891 if (arg != NULL)
17892 {
17893 if (*arg == K_SPECIAL)
17894 j = 3;
17895 else
17896 j = 0;
17897 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17898 : eval_isnamec(arg[j])))
17899 ++j;
17900 if (arg[j] != NUL)
17901 emsg_funcname(_(e_invarg2), arg);
17902 }
17903 }
17904
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 /*
17906 * Isolate the arguments: "arg1, arg2, ...)"
17907 */
17908 while (*p != ')')
17909 {
17910 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17911 {
17912 varargs = TRUE;
17913 p += 3;
17914 mustend = TRUE;
17915 }
17916 else
17917 {
17918 arg = p;
17919 while (ASCII_ISALNUM(*p) || *p == '_')
17920 ++p;
17921 if (arg == p || isdigit(*arg)
17922 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17923 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17924 {
17925 if (!eap->skip)
17926 EMSG2(_("E125: Illegal argument: %s"), arg);
17927 break;
17928 }
17929 if (ga_grow(&newargs, 1) == FAIL)
17930 goto erret;
17931 c = *p;
17932 *p = NUL;
17933 arg = vim_strsave(arg);
17934 if (arg == NULL)
17935 goto erret;
17936 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17937 *p = c;
17938 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 if (*p == ',')
17940 ++p;
17941 else
17942 mustend = TRUE;
17943 }
17944 p = skipwhite(p);
17945 if (mustend && *p != ')')
17946 {
17947 if (!eap->skip)
17948 EMSG2(_(e_invarg2), eap->arg);
17949 break;
17950 }
17951 }
17952 ++p; /* skip the ')' */
17953
Bram Moolenaare9a41262005-01-15 22:18:47 +000017954 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 for (;;)
17956 {
17957 p = skipwhite(p);
17958 if (STRNCMP(p, "range", 5) == 0)
17959 {
17960 flags |= FC_RANGE;
17961 p += 5;
17962 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017963 else if (STRNCMP(p, "dict", 4) == 0)
17964 {
17965 flags |= FC_DICT;
17966 p += 4;
17967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 else if (STRNCMP(p, "abort", 5) == 0)
17969 {
17970 flags |= FC_ABORT;
17971 p += 5;
17972 }
17973 else
17974 break;
17975 }
17976
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017977 /* When there is a line break use what follows for the function body.
17978 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17979 if (*p == '\n')
17980 line_arg = p + 1;
17981 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 EMSG(_(e_trailing));
17983
17984 /*
17985 * Read the body of the function, until ":endfunction" is found.
17986 */
17987 if (KeyTyped)
17988 {
17989 /* Check if the function already exists, don't let the user type the
17990 * whole function before telling him it doesn't work! For a script we
17991 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017992 if (!eap->skip && !eap->forceit)
17993 {
17994 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17995 EMSG(_(e_funcdict));
17996 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017997 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018000 if (!eap->skip && did_emsg)
18001 goto erret;
18002
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003 msg_putchar('\n'); /* don't overwrite the function name */
18004 cmdline_row = msg_row;
18005 }
18006
18007 indent = 2;
18008 nesting = 0;
18009 for (;;)
18010 {
18011 msg_scroll = TRUE;
18012 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018013 sourcing_lnum_off = sourcing_lnum;
18014
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018015 if (line_arg != NULL)
18016 {
18017 /* Use eap->arg, split up in parts by line breaks. */
18018 theline = line_arg;
18019 p = vim_strchr(theline, '\n');
18020 if (p == NULL)
18021 line_arg += STRLEN(line_arg);
18022 else
18023 {
18024 *p = NUL;
18025 line_arg = p + 1;
18026 }
18027 }
18028 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 theline = getcmdline(':', 0L, indent);
18030 else
18031 theline = eap->getline(':', eap->cookie, indent);
18032 if (KeyTyped)
18033 lines_left = Rows - 1;
18034 if (theline == NULL)
18035 {
18036 EMSG(_("E126: Missing :endfunction"));
18037 goto erret;
18038 }
18039
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018040 /* Detect line continuation: sourcing_lnum increased more than one. */
18041 if (sourcing_lnum > sourcing_lnum_off + 1)
18042 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18043 else
18044 sourcing_lnum_off = 0;
18045
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 if (skip_until != NULL)
18047 {
18048 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18049 * don't check for ":endfunc". */
18050 if (STRCMP(theline, skip_until) == 0)
18051 {
18052 vim_free(skip_until);
18053 skip_until = NULL;
18054 }
18055 }
18056 else
18057 {
18058 /* skip ':' and blanks*/
18059 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18060 ;
18061
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018062 /* Check for "endfunction". */
18063 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018065 if (line_arg == NULL)
18066 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 break;
18068 }
18069
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018070 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 * at "end". */
18072 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18073 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018074 else if (STRNCMP(p, "if", 2) == 0
18075 || STRNCMP(p, "wh", 2) == 0
18076 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 || STRNCMP(p, "try", 3) == 0)
18078 indent += 2;
18079
18080 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018081 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018083 if (*p == '!')
18084 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 p += eval_fname_script(p);
18086 if (ASCII_ISALPHA(*p))
18087 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018088 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 if (*skipwhite(p) == '(')
18090 {
18091 ++nesting;
18092 indent += 2;
18093 }
18094 }
18095 }
18096
18097 /* Check for ":append" or ":insert". */
18098 p = skip_range(p, NULL);
18099 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18100 || (p[0] == 'i'
18101 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18102 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18103 skip_until = vim_strsave((char_u *)".");
18104
18105 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18106 arg = skipwhite(skiptowhite(p));
18107 if (arg[0] == '<' && arg[1] =='<'
18108 && ((p[0] == 'p' && p[1] == 'y'
18109 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18110 || (p[0] == 'p' && p[1] == 'e'
18111 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18112 || (p[0] == 't' && p[1] == 'c'
18113 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18114 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18115 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018116 || (p[0] == 'm' && p[1] == 'z'
18117 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 ))
18119 {
18120 /* ":python <<" continues until a dot, like ":append" */
18121 p = skipwhite(arg + 2);
18122 if (*p == NUL)
18123 skip_until = vim_strsave((char_u *)".");
18124 else
18125 skip_until = vim_strsave(p);
18126 }
18127 }
18128
18129 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018130 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018131 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018132 if (line_arg == NULL)
18133 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018135 }
18136
18137 /* Copy the line to newly allocated memory. get_one_sourceline()
18138 * allocates 250 bytes per line, this saves 80% on average. The cost
18139 * is an extra alloc/free. */
18140 p = vim_strsave(theline);
18141 if (p != NULL)
18142 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018143 if (line_arg == NULL)
18144 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018145 theline = p;
18146 }
18147
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018148 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18149
18150 /* Add NULL lines for continuation lines, so that the line count is
18151 * equal to the index in the growarray. */
18152 while (sourcing_lnum_off-- > 0)
18153 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018154
18155 /* Check for end of eap->arg. */
18156 if (line_arg != NULL && *line_arg == NUL)
18157 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 }
18159
18160 /* Don't define the function when skipping commands or when an error was
18161 * detected. */
18162 if (eap->skip || did_emsg)
18163 goto erret;
18164
18165 /*
18166 * If there are no errors, add the function
18167 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018168 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018169 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018170 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018171 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018172 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018173 emsg_funcname("E707: Function name conflicts with variable: %s",
18174 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018175 goto erret;
18176 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018177
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018178 fp = find_func(name);
18179 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018181 if (!eap->forceit)
18182 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018183 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018184 goto erret;
18185 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018186 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018187 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018188 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018189 name);
18190 goto erret;
18191 }
18192 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018193 ga_clear_strings(&(fp->uf_args));
18194 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018195 vim_free(name);
18196 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 }
18199 else
18200 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018201 char numbuf[20];
18202
18203 fp = NULL;
18204 if (fudi.fd_newkey == NULL && !eap->forceit)
18205 {
18206 EMSG(_(e_funcdict));
18207 goto erret;
18208 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018209 if (fudi.fd_di == NULL)
18210 {
18211 /* Can't add a function to a locked dictionary */
18212 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18213 goto erret;
18214 }
18215 /* Can't change an existing function if it is locked */
18216 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18217 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018218
18219 /* Give the function a sequential number. Can only be used with a
18220 * Funcref! */
18221 vim_free(name);
18222 sprintf(numbuf, "%d", ++func_nr);
18223 name = vim_strsave((char_u *)numbuf);
18224 if (name == NULL)
18225 goto erret;
18226 }
18227
18228 if (fp == NULL)
18229 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018230 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018231 {
18232 int slen, plen;
18233 char_u *scriptname;
18234
18235 /* Check that the autoload name matches the script name. */
18236 j = FAIL;
18237 if (sourcing_name != NULL)
18238 {
18239 scriptname = autoload_name(name);
18240 if (scriptname != NULL)
18241 {
18242 p = vim_strchr(scriptname, '/');
18243 plen = STRLEN(p);
18244 slen = STRLEN(sourcing_name);
18245 if (slen > plen && fnamecmp(p,
18246 sourcing_name + slen - plen) == 0)
18247 j = OK;
18248 vim_free(scriptname);
18249 }
18250 }
18251 if (j == FAIL)
18252 {
18253 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18254 goto erret;
18255 }
18256 }
18257
18258 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 if (fp == NULL)
18260 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018261
18262 if (fudi.fd_dict != NULL)
18263 {
18264 if (fudi.fd_di == NULL)
18265 {
18266 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018267 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018268 if (fudi.fd_di == NULL)
18269 {
18270 vim_free(fp);
18271 goto erret;
18272 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018273 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18274 {
18275 vim_free(fudi.fd_di);
18276 goto erret;
18277 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018278 }
18279 else
18280 /* overwrite existing dict entry */
18281 clear_tv(&fudi.fd_di->di_tv);
18282 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018283 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018284 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018285 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018286 }
18287
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018289 STRCPY(fp->uf_name, name);
18290 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018292 fp->uf_args = newargs;
18293 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018294#ifdef FEAT_PROFILE
18295 fp->uf_tml_count = NULL;
18296 fp->uf_tml_total = NULL;
18297 fp->uf_tml_self = NULL;
18298 fp->uf_profiling = FALSE;
18299 if (prof_def_func())
18300 func_do_profile(fp);
18301#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018302 fp->uf_varargs = varargs;
18303 fp->uf_flags = flags;
18304 fp->uf_calls = 0;
18305 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018306 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307
18308erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 ga_clear_strings(&newargs);
18310 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018311ret_free:
18312 vim_free(skip_until);
18313 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316}
18317
18318/*
18319 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018320 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018322 * flags:
18323 * TFN_INT: internal function name OK
18324 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 * Advances "pp" to just after the function name (if no error).
18326 */
18327 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018328trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 char_u **pp;
18330 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018331 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018332 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018334 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 char_u *start;
18336 char_u *end;
18337 int lead;
18338 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018340 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018341
18342 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018343 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018345
18346 /* Check for hard coded <SNR>: already translated function ID (from a user
18347 * command). */
18348 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18349 && (*pp)[2] == (int)KE_SNR)
18350 {
18351 *pp += 3;
18352 len = get_id_len(pp) + 3;
18353 return vim_strnsave(start, len);
18354 }
18355
18356 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18357 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018359 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018361
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018362 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18363 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018364 if (end == start)
18365 {
18366 if (!skip)
18367 EMSG(_("E129: Function name required"));
18368 goto theend;
18369 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018370 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018371 {
18372 /*
18373 * Report an invalid expression in braces, unless the expression
18374 * evaluation has been cancelled due to an aborting error, an
18375 * interrupt, or an exception.
18376 */
18377 if (!aborting())
18378 {
18379 if (end != NULL)
18380 EMSG2(_(e_invarg2), start);
18381 }
18382 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018383 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018384 goto theend;
18385 }
18386
18387 if (lv.ll_tv != NULL)
18388 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018389 if (fdp != NULL)
18390 {
18391 fdp->fd_dict = lv.ll_dict;
18392 fdp->fd_newkey = lv.ll_newkey;
18393 lv.ll_newkey = NULL;
18394 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018395 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018396 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18397 {
18398 name = vim_strsave(lv.ll_tv->vval.v_string);
18399 *pp = end;
18400 }
18401 else
18402 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018403 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18404 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018405 EMSG(_(e_funcref));
18406 else
18407 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018408 name = NULL;
18409 }
18410 goto theend;
18411 }
18412
18413 if (lv.ll_name == NULL)
18414 {
18415 /* Error found, but continue after the function name. */
18416 *pp = end;
18417 goto theend;
18418 }
18419
18420 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018421 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018422 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018423 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18424 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18425 {
18426 /* When there was "s:" already or the name expanded to get a
18427 * leading "s:" then remove it. */
18428 lv.ll_name += 2;
18429 len -= 2;
18430 lead = 2;
18431 }
18432 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018433 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018434 {
18435 if (lead == 2) /* skip over "s:" */
18436 lv.ll_name += 2;
18437 len = (int)(end - lv.ll_name);
18438 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018439
18440 /*
18441 * Copy the function name to allocated memory.
18442 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18443 * Accept <SNR>123_name() outside a script.
18444 */
18445 if (skip)
18446 lead = 0; /* do nothing */
18447 else if (lead > 0)
18448 {
18449 lead = 3;
18450 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18451 {
18452 if (current_SID <= 0)
18453 {
18454 EMSG(_(e_usingsid));
18455 goto theend;
18456 }
18457 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18458 lead += (int)STRLEN(sid_buf);
18459 }
18460 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018461 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018462 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018463 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018464 goto theend;
18465 }
18466 name = alloc((unsigned)(len + lead + 1));
18467 if (name != NULL)
18468 {
18469 if (lead > 0)
18470 {
18471 name[0] = K_SPECIAL;
18472 name[1] = KS_EXTRA;
18473 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018474 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018475 STRCPY(name + 3, sid_buf);
18476 }
18477 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18478 name[len + lead] = NUL;
18479 }
18480 *pp = end;
18481
18482theend:
18483 clear_lval(&lv);
18484 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485}
18486
18487/*
18488 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18489 * Return 2 if "p" starts with "s:".
18490 * Return 0 otherwise.
18491 */
18492 static int
18493eval_fname_script(p)
18494 char_u *p;
18495{
18496 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18497 || STRNICMP(p + 1, "SNR>", 4) == 0))
18498 return 5;
18499 if (p[0] == 's' && p[1] == ':')
18500 return 2;
18501 return 0;
18502}
18503
18504/*
18505 * Return TRUE if "p" starts with "<SID>" or "s:".
18506 * Only works if eval_fname_script() returned non-zero for "p"!
18507 */
18508 static int
18509eval_fname_sid(p)
18510 char_u *p;
18511{
18512 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18513}
18514
18515/*
18516 * List the head of the function: "name(arg1, arg2)".
18517 */
18518 static void
18519list_func_head(fp, indent)
18520 ufunc_T *fp;
18521 int indent;
18522{
18523 int j;
18524
18525 msg_start();
18526 if (indent)
18527 MSG_PUTS(" ");
18528 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018529 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 {
18531 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018532 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 }
18534 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018535 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018537 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 {
18539 if (j)
18540 MSG_PUTS(", ");
18541 msg_puts(FUNCARG(fp, j));
18542 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018543 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 {
18545 if (j)
18546 MSG_PUTS(", ");
18547 MSG_PUTS("...");
18548 }
18549 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018550 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018551 if (p_verbose > 0)
18552 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553}
18554
18555/*
18556 * Find a function by name, return pointer to it in ufuncs.
18557 * Return NULL for unknown function.
18558 */
18559 static ufunc_T *
18560find_func(name)
18561 char_u *name;
18562{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018563 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018565 hi = hash_find(&func_hashtab, name);
18566 if (!HASHITEM_EMPTY(hi))
18567 return HI2UF(hi);
18568 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569}
18570
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018571#if defined(EXITFREE) || defined(PROTO)
18572 void
18573free_all_functions()
18574{
18575 hashitem_T *hi;
18576
18577 /* Need to start all over every time, because func_free() may change the
18578 * hash table. */
18579 while (func_hashtab.ht_used > 0)
18580 for (hi = func_hashtab.ht_array; ; ++hi)
18581 if (!HASHITEM_EMPTY(hi))
18582 {
18583 func_free(HI2UF(hi));
18584 break;
18585 }
18586}
18587#endif
18588
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018589/*
18590 * Return TRUE if a function "name" exists.
18591 */
18592 static int
18593function_exists(name)
18594 char_u *name;
18595{
18596 char_u *p = name;
18597 int n = FALSE;
18598
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018599 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018600 if (p != NULL)
18601 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018602 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018603 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018604 else
18605 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018606 vim_free(p);
18607 }
18608 return n;
18609}
18610
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018611/*
18612 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018613 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018614 */
18615 static int
18616builtin_function(name)
18617 char_u *name;
18618{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018619 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18620 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018621}
18622
Bram Moolenaar05159a02005-02-26 23:04:13 +000018623#if defined(FEAT_PROFILE) || defined(PROTO)
18624/*
18625 * Start profiling function "fp".
18626 */
18627 static void
18628func_do_profile(fp)
18629 ufunc_T *fp;
18630{
18631 fp->uf_tm_count = 0;
18632 profile_zero(&fp->uf_tm_self);
18633 profile_zero(&fp->uf_tm_total);
18634 if (fp->uf_tml_count == NULL)
18635 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18636 (sizeof(int) * fp->uf_lines.ga_len));
18637 if (fp->uf_tml_total == NULL)
18638 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18639 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18640 if (fp->uf_tml_self == NULL)
18641 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18642 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18643 fp->uf_tml_idx = -1;
18644 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18645 || fp->uf_tml_self == NULL)
18646 return; /* out of memory */
18647
18648 fp->uf_profiling = TRUE;
18649}
18650
18651/*
18652 * Dump the profiling results for all functions in file "fd".
18653 */
18654 void
18655func_dump_profile(fd)
18656 FILE *fd;
18657{
18658 hashitem_T *hi;
18659 int todo;
18660 ufunc_T *fp;
18661 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018662 ufunc_T **sorttab;
18663 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018664
18665 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018666 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18667
Bram Moolenaar05159a02005-02-26 23:04:13 +000018668 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18669 {
18670 if (!HASHITEM_EMPTY(hi))
18671 {
18672 --todo;
18673 fp = HI2UF(hi);
18674 if (fp->uf_profiling)
18675 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018676 if (sorttab != NULL)
18677 sorttab[st_len++] = fp;
18678
Bram Moolenaar05159a02005-02-26 23:04:13 +000018679 if (fp->uf_name[0] == K_SPECIAL)
18680 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18681 else
18682 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18683 if (fp->uf_tm_count == 1)
18684 fprintf(fd, "Called 1 time\n");
18685 else
18686 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18687 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18688 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18689 fprintf(fd, "\n");
18690 fprintf(fd, "count total (s) self (s)\n");
18691
18692 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18693 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018694 if (FUNCLINE(fp, i) == NULL)
18695 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018696 prof_func_line(fd, fp->uf_tml_count[i],
18697 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018698 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18699 }
18700 fprintf(fd, "\n");
18701 }
18702 }
18703 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018704
18705 if (sorttab != NULL && st_len > 0)
18706 {
18707 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18708 prof_total_cmp);
18709 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18710 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18711 prof_self_cmp);
18712 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18713 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018714}
Bram Moolenaar73830342005-02-28 22:48:19 +000018715
18716 static void
18717prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18718 FILE *fd;
18719 ufunc_T **sorttab;
18720 int st_len;
18721 char *title;
18722 int prefer_self; /* when equal print only self time */
18723{
18724 int i;
18725 ufunc_T *fp;
18726
18727 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18728 fprintf(fd, "count total (s) self (s) function\n");
18729 for (i = 0; i < 20 && i < st_len; ++i)
18730 {
18731 fp = sorttab[i];
18732 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18733 prefer_self);
18734 if (fp->uf_name[0] == K_SPECIAL)
18735 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18736 else
18737 fprintf(fd, " %s()\n", fp->uf_name);
18738 }
18739 fprintf(fd, "\n");
18740}
18741
18742/*
18743 * Print the count and times for one function or function line.
18744 */
18745 static void
18746prof_func_line(fd, count, total, self, prefer_self)
18747 FILE *fd;
18748 int count;
18749 proftime_T *total;
18750 proftime_T *self;
18751 int prefer_self; /* when equal print only self time */
18752{
18753 if (count > 0)
18754 {
18755 fprintf(fd, "%5d ", count);
18756 if (prefer_self && profile_equal(total, self))
18757 fprintf(fd, " ");
18758 else
18759 fprintf(fd, "%s ", profile_msg(total));
18760 if (!prefer_self && profile_equal(total, self))
18761 fprintf(fd, " ");
18762 else
18763 fprintf(fd, "%s ", profile_msg(self));
18764 }
18765 else
18766 fprintf(fd, " ");
18767}
18768
18769/*
18770 * Compare function for total time sorting.
18771 */
18772 static int
18773#ifdef __BORLANDC__
18774_RTLENTRYF
18775#endif
18776prof_total_cmp(s1, s2)
18777 const void *s1;
18778 const void *s2;
18779{
18780 ufunc_T *p1, *p2;
18781
18782 p1 = *(ufunc_T **)s1;
18783 p2 = *(ufunc_T **)s2;
18784 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18785}
18786
18787/*
18788 * Compare function for self time sorting.
18789 */
18790 static int
18791#ifdef __BORLANDC__
18792_RTLENTRYF
18793#endif
18794prof_self_cmp(s1, s2)
18795 const void *s1;
18796 const void *s2;
18797{
18798 ufunc_T *p1, *p2;
18799
18800 p1 = *(ufunc_T **)s1;
18801 p2 = *(ufunc_T **)s2;
18802 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18803}
18804
Bram Moolenaar05159a02005-02-26 23:04:13 +000018805#endif
18806
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018807/* The names of packages that once were loaded is remembered. */
18808static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18809
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018810/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018811 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018812 * Return TRUE if a package was loaded.
18813 */
18814 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018815script_autoload(name, reload)
18816 char_u *name;
18817 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018818{
18819 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018820 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018821 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018822 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018823
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018824 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018825 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018826 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018827 return FALSE;
18828
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018829 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018830
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018831 /* Find the name in the list of previously loaded package names. Skip
18832 * "autoload/", it's always the same. */
18833 for (i = 0; i < ga_loaded.ga_len; ++i)
18834 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18835 break;
18836 if (!reload && i < ga_loaded.ga_len)
18837 ret = FALSE; /* was loaded already */
18838 else
18839 {
18840 /* Remember the name if it wasn't loaded already. */
18841 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18842 {
18843 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18844 tofree = NULL;
18845 }
18846
18847 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018848 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018849 ret = TRUE;
18850 }
18851
18852 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018853 return ret;
18854}
18855
18856/*
18857 * Return the autoload script name for a function or variable name.
18858 * Returns NULL when out of memory.
18859 */
18860 static char_u *
18861autoload_name(name)
18862 char_u *name;
18863{
18864 char_u *p;
18865 char_u *scriptname;
18866
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018867 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018868 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18869 if (scriptname == NULL)
18870 return FALSE;
18871 STRCPY(scriptname, "autoload/");
18872 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018873 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018874 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018875 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018876 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018877 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018878}
18879
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18881
18882/*
18883 * Function given to ExpandGeneric() to obtain the list of user defined
18884 * function names.
18885 */
18886 char_u *
18887get_user_func_name(xp, idx)
18888 expand_T *xp;
18889 int idx;
18890{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018891 static long_u done;
18892 static hashitem_T *hi;
18893 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894
18895 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018897 done = 0;
18898 hi = func_hashtab.ht_array;
18899 }
18900 if (done < func_hashtab.ht_used)
18901 {
18902 if (done++ > 0)
18903 ++hi;
18904 while (HASHITEM_EMPTY(hi))
18905 ++hi;
18906 fp = HI2UF(hi);
18907
18908 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18909 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910
18911 cat_func_name(IObuff, fp);
18912 if (xp->xp_context != EXPAND_USER_FUNC)
18913 {
18914 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018915 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 STRCAT(IObuff, ")");
18917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 return IObuff;
18919 }
18920 return NULL;
18921}
18922
18923#endif /* FEAT_CMDL_COMPL */
18924
18925/*
18926 * Copy the function name of "fp" to buffer "buf".
18927 * "buf" must be able to hold the function name plus three bytes.
18928 * Takes care of script-local function names.
18929 */
18930 static void
18931cat_func_name(buf, fp)
18932 char_u *buf;
18933 ufunc_T *fp;
18934{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018935 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936 {
18937 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018938 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 }
18940 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018941 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942}
18943
18944/*
18945 * ":delfunction {name}"
18946 */
18947 void
18948ex_delfunction(eap)
18949 exarg_T *eap;
18950{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018951 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 char_u *p;
18953 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018954 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955
18956 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018957 name = trans_function_name(&p, eap->skip, 0, &fudi);
18958 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018960 {
18961 if (fudi.fd_dict != NULL && !eap->skip)
18962 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 if (!ends_excmd(*skipwhite(p)))
18966 {
18967 vim_free(name);
18968 EMSG(_(e_trailing));
18969 return;
18970 }
18971 eap->nextcmd = check_nextcmd(p);
18972 if (eap->nextcmd != NULL)
18973 *p = NUL;
18974
18975 if (!eap->skip)
18976 fp = find_func(name);
18977 vim_free(name);
18978
18979 if (!eap->skip)
18980 {
18981 if (fp == NULL)
18982 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018983 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 return;
18985 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018986 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 {
18988 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18989 return;
18990 }
18991
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018992 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018994 /* Delete the dict item that refers to the function, it will
18995 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018996 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018998 else
18999 func_free(fp);
19000 }
19001}
19002
19003/*
19004 * Free a function and remove it from the list of functions.
19005 */
19006 static void
19007func_free(fp)
19008 ufunc_T *fp;
19009{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019010 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019011
19012 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019013 ga_clear_strings(&(fp->uf_args));
19014 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019015#ifdef FEAT_PROFILE
19016 vim_free(fp->uf_tml_count);
19017 vim_free(fp->uf_tml_total);
19018 vim_free(fp->uf_tml_self);
19019#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019020
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019021 /* remove the function from the function hashtable */
19022 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19023 if (HASHITEM_EMPTY(hi))
19024 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019025 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019026 hash_remove(&func_hashtab, hi);
19027
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019028 vim_free(fp);
19029}
19030
19031/*
19032 * Unreference a Function: decrement the reference count and free it when it
19033 * becomes zero. Only for numbered functions.
19034 */
19035 static void
19036func_unref(name)
19037 char_u *name;
19038{
19039 ufunc_T *fp;
19040
19041 if (name != NULL && isdigit(*name))
19042 {
19043 fp = find_func(name);
19044 if (fp == NULL)
19045 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019046 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019047 {
19048 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019049 * when "uf_calls" becomes zero. */
19050 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019051 func_free(fp);
19052 }
19053 }
19054}
19055
19056/*
19057 * Count a reference to a Function.
19058 */
19059 static void
19060func_ref(name)
19061 char_u *name;
19062{
19063 ufunc_T *fp;
19064
19065 if (name != NULL && isdigit(*name))
19066 {
19067 fp = find_func(name);
19068 if (fp == NULL)
19069 EMSG2(_(e_intern2), "func_ref()");
19070 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019071 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 }
19073}
19074
19075/*
19076 * Call a user function.
19077 */
19078 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019079call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 ufunc_T *fp; /* pointer to function */
19081 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 typval_T *argvars; /* arguments */
19083 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 linenr_T firstline; /* first line of range */
19085 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019086 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087{
Bram Moolenaar33570922005-01-25 22:26:29 +000019088 char_u *save_sourcing_name;
19089 linenr_T save_sourcing_lnum;
19090 scid_T save_current_SID;
19091 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019092 int save_did_emsg;
19093 static int depth = 0;
19094 dictitem_T *v;
19095 int fixvar_idx = 0; /* index in fixvar[] */
19096 int i;
19097 int ai;
19098 char_u numbuf[NUMBUFLEN];
19099 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019100#ifdef FEAT_PROFILE
19101 proftime_T wait_start;
19102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103
19104 /* If depth of calling is getting too high, don't execute the function */
19105 if (depth >= p_mfd)
19106 {
19107 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019108 rettv->v_type = VAR_NUMBER;
19109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 return;
19111 }
19112 ++depth;
19113
19114 line_breakcheck(); /* check for CTRL-C hit */
19115
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019116 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019117 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019119 fc.rettv = rettv;
19120 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 fc.linenr = 0;
19122 fc.returned = FALSE;
19123 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019125 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 fc.dbg_tick = debug_tick;
19127
Bram Moolenaar33570922005-01-25 22:26:29 +000019128 /*
19129 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19130 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19131 * each argument variable and saves a lot of time.
19132 */
19133 /*
19134 * Init l: variables.
19135 */
19136 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019137 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019139 /* Set l:self to "selfdict". */
19140 v = &fc.fixvar[fixvar_idx++].var;
19141 STRCPY(v->di_key, "self");
19142 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19143 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19144 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019145 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019146 v->di_tv.vval.v_dict = selfdict;
19147 ++selfdict->dv_refcount;
19148 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019149
Bram Moolenaar33570922005-01-25 22:26:29 +000019150 /*
19151 * Init a: variables.
19152 * Set a:0 to "argcount".
19153 * Set a:000 to a list with room for the "..." arguments.
19154 */
19155 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19156 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019157 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019158 v = &fc.fixvar[fixvar_idx++].var;
19159 STRCPY(v->di_key, "000");
19160 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19161 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19162 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019163 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019164 v->di_tv.vval.v_list = &fc.l_varlist;
19165 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19166 fc.l_varlist.lv_refcount = 99999;
19167
19168 /*
19169 * Set a:firstline to "firstline" and a:lastline to "lastline".
19170 * Set a:name to named arguments.
19171 * Set a:N to the "..." arguments.
19172 */
19173 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19174 (varnumber_T)firstline);
19175 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19176 (varnumber_T)lastline);
19177 for (i = 0; i < argcount; ++i)
19178 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019179 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019180 if (ai < 0)
19181 /* named argument a:name */
19182 name = FUNCARG(fp, i);
19183 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019184 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019185 /* "..." argument a:1, a:2, etc. */
19186 sprintf((char *)numbuf, "%d", ai + 1);
19187 name = numbuf;
19188 }
19189 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19190 {
19191 v = &fc.fixvar[fixvar_idx++].var;
19192 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19193 }
19194 else
19195 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019196 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19197 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019198 if (v == NULL)
19199 break;
19200 v->di_flags = DI_FLAGS_RO;
19201 }
19202 STRCPY(v->di_key, name);
19203 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19204
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019205 /* Note: the values are copied directly to avoid alloc/free.
19206 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019207 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019208 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019209
19210 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19211 {
19212 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19213 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019214 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019215 }
19216 }
19217
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 /* Don't redraw while executing the function. */
19219 ++RedrawingDisabled;
19220 save_sourcing_name = sourcing_name;
19221 save_sourcing_lnum = sourcing_lnum;
19222 sourcing_lnum = 1;
19223 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019224 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 if (sourcing_name != NULL)
19226 {
19227 if (save_sourcing_name != NULL
19228 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19229 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19230 else
19231 STRCPY(sourcing_name, "function ");
19232 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19233
19234 if (p_verbose >= 12)
19235 {
19236 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019237 verbose_enter_scroll();
19238
Bram Moolenaar555b2802005-05-19 21:08:39 +000019239 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 if (p_verbose >= 14)
19241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019243 char_u numbuf[NUMBUFLEN];
19244 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245
19246 msg_puts((char_u *)"(");
19247 for (i = 0; i < argcount; ++i)
19248 {
19249 if (i > 0)
19250 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019251 if (argvars[i].v_type == VAR_NUMBER)
19252 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 else
19254 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019255 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019256 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019258 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 }
19260 }
19261 msg_puts((char_u *)")");
19262 }
19263 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019264
19265 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 --no_wait_return;
19267 }
19268 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019269#ifdef FEAT_PROFILE
19270 if (do_profiling)
19271 {
19272 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19273 func_do_profile(fp);
19274 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019275 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019276 {
19277 ++fp->uf_tm_count;
19278 profile_start(&fp->uf_tm_start);
19279 profile_zero(&fp->uf_tm_children);
19280 }
19281 script_prof_save(&wait_start);
19282 }
19283#endif
19284
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019286 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 save_did_emsg = did_emsg;
19288 did_emsg = FALSE;
19289
19290 /* call do_cmdline() to execute the lines */
19291 do_cmdline(NULL, get_func_line, (void *)&fc,
19292 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19293
19294 --RedrawingDisabled;
19295
19296 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019297 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019299 clear_tv(rettv);
19300 rettv->v_type = VAR_NUMBER;
19301 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 }
19303
Bram Moolenaar05159a02005-02-26 23:04:13 +000019304#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019305 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019306 {
19307 profile_end(&fp->uf_tm_start);
19308 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19309 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
19310 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
19311 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019312 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019313 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019314 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19315 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019316 }
19317 }
19318#endif
19319
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 /* when being verbose, mention the return value */
19321 if (p_verbose >= 12)
19322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019324 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019327 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019328 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019329 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19330 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019333 char_u buf[MSG_BUF_LEN];
19334 char_u numbuf[NUMBUFLEN];
19335 char_u *tofree;
19336
Bram Moolenaar555b2802005-05-19 21:08:39 +000019337 /* The value may be very long. Skip the middle part, so that we
19338 * have some idea how it starts and ends. smsg() would always
19339 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019340 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019341 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019342 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019343 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 }
19345 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019346
19347 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 --no_wait_return;
19349 }
19350
19351 vim_free(sourcing_name);
19352 sourcing_name = save_sourcing_name;
19353 sourcing_lnum = save_sourcing_lnum;
19354 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019355#ifdef FEAT_PROFILE
19356 if (do_profiling)
19357 script_prof_restore(&wait_start);
19358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359
19360 if (p_verbose >= 12 && sourcing_name != NULL)
19361 {
19362 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019363 verbose_enter_scroll();
19364
Bram Moolenaar555b2802005-05-19 21:08:39 +000019365 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019367
19368 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 --no_wait_return;
19370 }
19371
19372 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019373 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374
Bram Moolenaar33570922005-01-25 22:26:29 +000019375 /* The a: variables typevals were not alloced, only free the allocated
19376 * variables. */
19377 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19378
19379 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 --depth;
19381}
19382
19383/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019384 * Add a number variable "name" to dict "dp" with value "nr".
19385 */
19386 static void
19387add_nr_var(dp, v, name, nr)
19388 dict_T *dp;
19389 dictitem_T *v;
19390 char *name;
19391 varnumber_T nr;
19392{
19393 STRCPY(v->di_key, name);
19394 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19395 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19396 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019397 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019398 v->di_tv.vval.v_number = nr;
19399}
19400
19401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 * ":return [expr]"
19403 */
19404 void
19405ex_return(eap)
19406 exarg_T *eap;
19407{
19408 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 int returning = FALSE;
19411
19412 if (current_funccal == NULL)
19413 {
19414 EMSG(_("E133: :return not inside a function"));
19415 return;
19416 }
19417
19418 if (eap->skip)
19419 ++emsg_skip;
19420
19421 eap->nextcmd = NULL;
19422 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019423 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424 {
19425 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019426 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019428 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 }
19430 /* It's safer to return also on error. */
19431 else if (!eap->skip)
19432 {
19433 /*
19434 * Return unless the expression evaluation has been cancelled due to an
19435 * aborting error, an interrupt, or an exception.
19436 */
19437 if (!aborting())
19438 returning = do_return(eap, FALSE, TRUE, NULL);
19439 }
19440
19441 /* When skipping or the return gets pending, advance to the next command
19442 * in this line (!returning). Otherwise, ignore the rest of the line.
19443 * Following lines will be ignored by get_func_line(). */
19444 if (returning)
19445 eap->nextcmd = NULL;
19446 else if (eap->nextcmd == NULL) /* no argument */
19447 eap->nextcmd = check_nextcmd(arg);
19448
19449 if (eap->skip)
19450 --emsg_skip;
19451}
19452
19453/*
19454 * Return from a function. Possibly makes the return pending. Also called
19455 * for a pending return at the ":endtry" or after returning from an extra
19456 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019457 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019458 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 * FALSE when the return gets pending.
19460 */
19461 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019462do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 exarg_T *eap;
19464 int reanimate;
19465 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019466 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467{
19468 int idx;
19469 struct condstack *cstack = eap->cstack;
19470
19471 if (reanimate)
19472 /* Undo the return. */
19473 current_funccal->returned = FALSE;
19474
19475 /*
19476 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19477 * not in its finally clause (which then is to be executed next) is found.
19478 * In this case, make the ":return" pending for execution at the ":endtry".
19479 * Otherwise, return normally.
19480 */
19481 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19482 if (idx >= 0)
19483 {
19484 cstack->cs_pending[idx] = CSTP_RETURN;
19485
19486 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019487 /* A pending return again gets pending. "rettv" points to an
19488 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019490 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 else
19492 {
19493 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019494 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019496 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019498 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 {
19500 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019501 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019502 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 else
19504 EMSG(_(e_outofmem));
19505 }
19506 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508
19509 if (reanimate)
19510 {
19511 /* The pending return value could be overwritten by a ":return"
19512 * without argument in a finally clause; reset the default
19513 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019514 current_funccal->rettv->v_type = VAR_NUMBER;
19515 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 }
19517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019518 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 }
19520 else
19521 {
19522 current_funccal->returned = TRUE;
19523
19524 /* If the return is carried out now, store the return value. For
19525 * a return immediately after reanimation, the value is already
19526 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019527 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019529 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019530 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019532 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 }
19534 }
19535
19536 return idx < 0;
19537}
19538
19539/*
19540 * Free the variable with a pending return value.
19541 */
19542 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019543discard_pending_return(rettv)
19544 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545{
Bram Moolenaar33570922005-01-25 22:26:29 +000019546 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547}
19548
19549/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019550 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 * is an allocated string. Used by report_pending() for verbose messages.
19552 */
19553 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019554get_return_cmd(rettv)
19555 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019557 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019558 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019559 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019561 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019562 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019563 if (s == NULL)
19564 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019565
19566 STRCPY(IObuff, ":return ");
19567 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19568 if (STRLEN(s) + 8 >= IOSIZE)
19569 STRCPY(IObuff + IOSIZE - 4, "...");
19570 vim_free(tofree);
19571 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572}
19573
19574/*
19575 * Get next function line.
19576 * Called by do_cmdline() to get the next line.
19577 * Returns allocated string, or NULL for end of function.
19578 */
19579/* ARGSUSED */
19580 char_u *
19581get_func_line(c, cookie, indent)
19582 int c; /* not used */
19583 void *cookie;
19584 int indent; /* not used */
19585{
Bram Moolenaar33570922005-01-25 22:26:29 +000019586 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019587 ufunc_T *fp = fcp->func;
19588 char_u *retval;
19589 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590
19591 /* If breakpoints have been added/deleted need to check for it. */
19592 if (fcp->dbg_tick != debug_tick)
19593 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019594 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 sourcing_lnum);
19596 fcp->dbg_tick = debug_tick;
19597 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019598#ifdef FEAT_PROFILE
19599 if (do_profiling)
19600 func_line_end(cookie);
19601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602
Bram Moolenaar05159a02005-02-26 23:04:13 +000019603 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019604 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19605 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 retval = NULL;
19607 else
19608 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019609 /* Skip NULL lines (continuation lines). */
19610 while (fcp->linenr < gap->ga_len
19611 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19612 ++fcp->linenr;
19613 if (fcp->linenr >= gap->ga_len)
19614 retval = NULL;
19615 else
19616 {
19617 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19618 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019619#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019620 if (do_profiling)
19621 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019622#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 }
19625
19626 /* Did we encounter a breakpoint? */
19627 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19628 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019629 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019631 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 sourcing_lnum);
19633 fcp->dbg_tick = debug_tick;
19634 }
19635
19636 return retval;
19637}
19638
Bram Moolenaar05159a02005-02-26 23:04:13 +000019639#if defined(FEAT_PROFILE) || defined(PROTO)
19640/*
19641 * Called when starting to read a function line.
19642 * "sourcing_lnum" must be correct!
19643 * When skipping lines it may not actually be executed, but we won't find out
19644 * until later and we need to store the time now.
19645 */
19646 void
19647func_line_start(cookie)
19648 void *cookie;
19649{
19650 funccall_T *fcp = (funccall_T *)cookie;
19651 ufunc_T *fp = fcp->func;
19652
19653 if (fp->uf_profiling && sourcing_lnum >= 1
19654 && sourcing_lnum <= fp->uf_lines.ga_len)
19655 {
19656 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019657 /* Skip continuation lines. */
19658 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19659 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019660 fp->uf_tml_execed = FALSE;
19661 profile_start(&fp->uf_tml_start);
19662 profile_zero(&fp->uf_tml_children);
19663 profile_get_wait(&fp->uf_tml_wait);
19664 }
19665}
19666
19667/*
19668 * Called when actually executing a function line.
19669 */
19670 void
19671func_line_exec(cookie)
19672 void *cookie;
19673{
19674 funccall_T *fcp = (funccall_T *)cookie;
19675 ufunc_T *fp = fcp->func;
19676
19677 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19678 fp->uf_tml_execed = TRUE;
19679}
19680
19681/*
19682 * Called when done with a function line.
19683 */
19684 void
19685func_line_end(cookie)
19686 void *cookie;
19687{
19688 funccall_T *fcp = (funccall_T *)cookie;
19689 ufunc_T *fp = fcp->func;
19690
19691 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19692 {
19693 if (fp->uf_tml_execed)
19694 {
19695 ++fp->uf_tml_count[fp->uf_tml_idx];
19696 profile_end(&fp->uf_tml_start);
19697 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19698 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19699 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19700 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19701 }
19702 fp->uf_tml_idx = -1;
19703 }
19704}
19705#endif
19706
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707/*
19708 * Return TRUE if the currently active function should be ended, because a
19709 * return was encountered or an error occured. Used inside a ":while".
19710 */
19711 int
19712func_has_ended(cookie)
19713 void *cookie;
19714{
Bram Moolenaar33570922005-01-25 22:26:29 +000019715 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716
19717 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19718 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019719 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 || fcp->returned);
19721}
19722
19723/*
19724 * return TRUE if cookie indicates a function which "abort"s on errors.
19725 */
19726 int
19727func_has_abort(cookie)
19728 void *cookie;
19729{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019730 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731}
19732
19733#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19734typedef enum
19735{
19736 VAR_FLAVOUR_DEFAULT,
19737 VAR_FLAVOUR_SESSION,
19738 VAR_FLAVOUR_VIMINFO
19739} var_flavour_T;
19740
19741static var_flavour_T var_flavour __ARGS((char_u *varname));
19742
19743 static var_flavour_T
19744var_flavour(varname)
19745 char_u *varname;
19746{
19747 char_u *p = varname;
19748
19749 if (ASCII_ISUPPER(*p))
19750 {
19751 while (*(++p))
19752 if (ASCII_ISLOWER(*p))
19753 return VAR_FLAVOUR_SESSION;
19754 return VAR_FLAVOUR_VIMINFO;
19755 }
19756 else
19757 return VAR_FLAVOUR_DEFAULT;
19758}
19759#endif
19760
19761#if defined(FEAT_VIMINFO) || defined(PROTO)
19762/*
19763 * Restore global vars that start with a capital from the viminfo file
19764 */
19765 int
19766read_viminfo_varlist(virp, writing)
19767 vir_T *virp;
19768 int writing;
19769{
19770 char_u *tab;
19771 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019772 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773
19774 if (!writing && (find_viminfo_parameter('!') != NULL))
19775 {
19776 tab = vim_strchr(virp->vir_line + 1, '\t');
19777 if (tab != NULL)
19778 {
19779 *tab++ = '\0'; /* isolate the variable name */
19780 if (*tab == 'S') /* string var */
19781 is_string = TRUE;
19782
19783 tab = vim_strchr(tab, '\t');
19784 if (tab != NULL)
19785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 if (is_string)
19787 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019788 tv.v_type = VAR_STRING;
19789 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 }
19792 else
19793 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019794 tv.v_type = VAR_NUMBER;
19795 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019797 set_var(virp->vir_line + 1, &tv, FALSE);
19798 if (is_string)
19799 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 }
19801 }
19802 }
19803
19804 return viminfo_readline(virp);
19805}
19806
19807/*
19808 * Write global vars that start with a capital to the viminfo file
19809 */
19810 void
19811write_viminfo_varlist(fp)
19812 FILE *fp;
19813{
Bram Moolenaar33570922005-01-25 22:26:29 +000019814 hashitem_T *hi;
19815 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019816 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019817 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019818 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019820 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821
19822 if (find_viminfo_parameter('!') == NULL)
19823 return;
19824
19825 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019826
Bram Moolenaar33570922005-01-25 22:26:29 +000019827 todo = globvarht.ht_used;
19828 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019830 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019832 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019833 this_var = HI2DI(hi);
19834 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019836 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019837 {
19838 case VAR_STRING: s = "STR"; break;
19839 case VAR_NUMBER: s = "NUM"; break;
19840 default: continue;
19841 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019842 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019843 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019844 if (p != NULL)
19845 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019846 vim_free(tofree);
19847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 }
19849 }
19850}
19851#endif
19852
19853#if defined(FEAT_SESSION) || defined(PROTO)
19854 int
19855store_session_globals(fd)
19856 FILE *fd;
19857{
Bram Moolenaar33570922005-01-25 22:26:29 +000019858 hashitem_T *hi;
19859 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019860 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 char_u *p, *t;
19862
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 todo = globvarht.ht_used;
19864 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019866 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019868 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019869 this_var = HI2DI(hi);
19870 if ((this_var->di_tv.v_type == VAR_NUMBER
19871 || this_var->di_tv.v_type == VAR_STRING)
19872 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019873 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019874 /* Escape special characters with a backslash. Turn a LF and
19875 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019876 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019877 (char_u *)"\\\"\n\r");
19878 if (p == NULL) /* out of memory */
19879 break;
19880 for (t = p; *t != NUL; ++t)
19881 if (*t == '\n')
19882 *t = 'n';
19883 else if (*t == '\r')
19884 *t = 'r';
19885 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 this_var->di_key,
19887 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19888 : ' ',
19889 p,
19890 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19891 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019892 || put_eol(fd) == FAIL)
19893 {
19894 vim_free(p);
19895 return FAIL;
19896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 vim_free(p);
19898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 }
19900 }
19901 return OK;
19902}
19903#endif
19904
Bram Moolenaar661b1822005-07-28 22:36:45 +000019905/*
19906 * Display script name where an item was last set.
19907 * Should only be invoked when 'verbose' is non-zero.
19908 */
19909 void
19910last_set_msg(scriptID)
19911 scid_T scriptID;
19912{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019913 char_u *p;
19914
Bram Moolenaar661b1822005-07-28 22:36:45 +000019915 if (scriptID != 0)
19916 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019917 p = home_replace_save(NULL, get_scriptname(scriptID));
19918 if (p != NULL)
19919 {
19920 verbose_enter();
19921 MSG_PUTS(_("\n\tLast set from "));
19922 MSG_PUTS(p);
19923 vim_free(p);
19924 verbose_leave();
19925 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019926 }
19927}
19928
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929#endif /* FEAT_EVAL */
19930
19931#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19932
19933
19934#ifdef WIN3264
19935/*
19936 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19937 */
19938static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19939static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19940static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19941
19942/*
19943 * Get the short pathname of a file.
19944 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19945 */
19946 static int
19947get_short_pathname(fnamep, bufp, fnamelen)
19948 char_u **fnamep;
19949 char_u **bufp;
19950 int *fnamelen;
19951{
19952 int l,len;
19953 char_u *newbuf;
19954
19955 len = *fnamelen;
19956
19957 l = GetShortPathName(*fnamep, *fnamep, len);
19958 if (l > len - 1)
19959 {
19960 /* If that doesn't work (not enough space), then save the string
19961 * and try again with a new buffer big enough
19962 */
19963 newbuf = vim_strnsave(*fnamep, l);
19964 if (newbuf == NULL)
19965 return 0;
19966
19967 vim_free(*bufp);
19968 *fnamep = *bufp = newbuf;
19969
19970 l = GetShortPathName(*fnamep,*fnamep,l+1);
19971
19972 /* Really should always succeed, as the buffer is big enough */
19973 }
19974
19975 *fnamelen = l;
19976 return 1;
19977}
19978
19979/*
19980 * Create a short path name. Returns the length of the buffer it needs.
19981 * Doesn't copy over the end of the buffer passed in.
19982 */
19983 static int
19984shortpath_for_invalid_fname(fname, bufp, fnamelen)
19985 char_u **fname;
19986 char_u **bufp;
19987 int *fnamelen;
19988{
19989 char_u *s, *p, *pbuf2, *pbuf3;
19990 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019991 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992
19993 /* Make a copy */
19994 len2 = *fnamelen;
19995 pbuf2 = vim_strnsave(*fname, len2);
19996 pbuf3 = NULL;
19997
19998 s = pbuf2 + len2 - 1; /* Find the end */
19999 slen = 1;
20000 plen = len2;
20001
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020002 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 {
20004 --s;
20005 ++slen;
20006 --plen;
20007 }
20008
20009 do
20010 {
20011 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020012 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 {
20014 --s;
20015 ++slen;
20016 --plen;
20017 }
20018 if (s <= pbuf2)
20019 break;
20020
20021 /* Remeber the character that is about to be blatted */
20022 ch = *s;
20023 *s = 0; /* get_short_pathname requires a null-terminated string */
20024
20025 /* Try it in situ */
20026 p = pbuf2;
20027 if (!get_short_pathname(&p, &pbuf3, &plen))
20028 {
20029 vim_free(pbuf2);
20030 return -1;
20031 }
20032 *s = ch; /* Preserve the string */
20033 } while (plen == 0);
20034
20035 if (plen > 0)
20036 {
20037 /* Remeber the length of the new string. */
20038 *fnamelen = len = plen + slen;
20039 vim_free(*bufp);
20040 if (len > len2)
20041 {
20042 /* If there's not enough space in the currently allocated string,
20043 * then copy it to a buffer big enough.
20044 */
20045 *fname= *bufp = vim_strnsave(p, len);
20046 if (*fname == NULL)
20047 return -1;
20048 }
20049 else
20050 {
20051 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20052 *fname = *bufp = pbuf2;
20053 if (p != pbuf2)
20054 strncpy(*fname, p, plen);
20055 pbuf2 = NULL;
20056 }
20057 /* Concat the next bit */
20058 strncpy(*fname + plen, s, slen);
20059 (*fname)[len] = '\0';
20060 }
20061 vim_free(pbuf3);
20062 vim_free(pbuf2);
20063 return 0;
20064}
20065
20066/*
20067 * Get a pathname for a partial path.
20068 */
20069 static int
20070shortpath_for_partial(fnamep, bufp, fnamelen)
20071 char_u **fnamep;
20072 char_u **bufp;
20073 int *fnamelen;
20074{
20075 int sepcount, len, tflen;
20076 char_u *p;
20077 char_u *pbuf, *tfname;
20078 int hasTilde;
20079
20080 /* Count up the path seperators from the RHS.. so we know which part
20081 * of the path to return.
20082 */
20083 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020084 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 if (vim_ispathsep(*p))
20086 ++sepcount;
20087
20088 /* Need full path first (use expand_env() to remove a "~/") */
20089 hasTilde = (**fnamep == '~');
20090 if (hasTilde)
20091 pbuf = tfname = expand_env_save(*fnamep);
20092 else
20093 pbuf = tfname = FullName_save(*fnamep, FALSE);
20094
20095 len = tflen = STRLEN(tfname);
20096
20097 if (!get_short_pathname(&tfname, &pbuf, &len))
20098 return -1;
20099
20100 if (len == 0)
20101 {
20102 /* Don't have a valid filename, so shorten the rest of the
20103 * path if we can. This CAN give us invalid 8.3 filenames, but
20104 * there's not a lot of point in guessing what it might be.
20105 */
20106 len = tflen;
20107 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20108 return -1;
20109 }
20110
20111 /* Count the paths backward to find the beginning of the desired string. */
20112 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020113 {
20114#ifdef FEAT_MBYTE
20115 if (has_mbyte)
20116 p -= mb_head_off(tfname, p);
20117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 if (vim_ispathsep(*p))
20119 {
20120 if (sepcount == 0 || (hasTilde && sepcount == 1))
20121 break;
20122 else
20123 sepcount --;
20124 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 if (hasTilde)
20127 {
20128 --p;
20129 if (p >= tfname)
20130 *p = '~';
20131 else
20132 return -1;
20133 }
20134 else
20135 ++p;
20136
20137 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20138 vim_free(*bufp);
20139 *fnamelen = (int)STRLEN(p);
20140 *bufp = pbuf;
20141 *fnamep = p;
20142
20143 return 0;
20144}
20145#endif /* WIN3264 */
20146
20147/*
20148 * Adjust a filename, according to a string of modifiers.
20149 * *fnamep must be NUL terminated when called. When returning, the length is
20150 * determined by *fnamelen.
20151 * Returns valid flags.
20152 * When there is an error, *fnamep is set to NULL.
20153 */
20154 int
20155modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20156 char_u *src; /* string with modifiers */
20157 int *usedlen; /* characters after src that are used */
20158 char_u **fnamep; /* file name so far */
20159 char_u **bufp; /* buffer for allocated file name or NULL */
20160 int *fnamelen; /* length of fnamep */
20161{
20162 int valid = 0;
20163 char_u *tail;
20164 char_u *s, *p, *pbuf;
20165 char_u dirname[MAXPATHL];
20166 int c;
20167 int has_fullname = 0;
20168#ifdef WIN3264
20169 int has_shortname = 0;
20170#endif
20171
20172repeat:
20173 /* ":p" - full path/file_name */
20174 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20175 {
20176 has_fullname = 1;
20177
20178 valid |= VALID_PATH;
20179 *usedlen += 2;
20180
20181 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20182 if ((*fnamep)[0] == '~'
20183#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20184 && ((*fnamep)[1] == '/'
20185# ifdef BACKSLASH_IN_FILENAME
20186 || (*fnamep)[1] == '\\'
20187# endif
20188 || (*fnamep)[1] == NUL)
20189
20190#endif
20191 )
20192 {
20193 *fnamep = expand_env_save(*fnamep);
20194 vim_free(*bufp); /* free any allocated file name */
20195 *bufp = *fnamep;
20196 if (*fnamep == NULL)
20197 return -1;
20198 }
20199
20200 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020201 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 {
20203 if (vim_ispathsep(*p)
20204 && p[1] == '.'
20205 && (p[2] == NUL
20206 || vim_ispathsep(p[2])
20207 || (p[2] == '.'
20208 && (p[3] == NUL || vim_ispathsep(p[3])))))
20209 break;
20210 }
20211
20212 /* FullName_save() is slow, don't use it when not needed. */
20213 if (*p != NUL || !vim_isAbsName(*fnamep))
20214 {
20215 *fnamep = FullName_save(*fnamep, *p != NUL);
20216 vim_free(*bufp); /* free any allocated file name */
20217 *bufp = *fnamep;
20218 if (*fnamep == NULL)
20219 return -1;
20220 }
20221
20222 /* Append a path separator to a directory. */
20223 if (mch_isdir(*fnamep))
20224 {
20225 /* Make room for one or two extra characters. */
20226 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20227 vim_free(*bufp); /* free any allocated file name */
20228 *bufp = *fnamep;
20229 if (*fnamep == NULL)
20230 return -1;
20231 add_pathsep(*fnamep);
20232 }
20233 }
20234
20235 /* ":." - path relative to the current directory */
20236 /* ":~" - path relative to the home directory */
20237 /* ":8" - shortname path - postponed till after */
20238 while (src[*usedlen] == ':'
20239 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20240 {
20241 *usedlen += 2;
20242 if (c == '8')
20243 {
20244#ifdef WIN3264
20245 has_shortname = 1; /* Postpone this. */
20246#endif
20247 continue;
20248 }
20249 pbuf = NULL;
20250 /* Need full path first (use expand_env() to remove a "~/") */
20251 if (!has_fullname)
20252 {
20253 if (c == '.' && **fnamep == '~')
20254 p = pbuf = expand_env_save(*fnamep);
20255 else
20256 p = pbuf = FullName_save(*fnamep, FALSE);
20257 }
20258 else
20259 p = *fnamep;
20260
20261 has_fullname = 0;
20262
20263 if (p != NULL)
20264 {
20265 if (c == '.')
20266 {
20267 mch_dirname(dirname, MAXPATHL);
20268 s = shorten_fname(p, dirname);
20269 if (s != NULL)
20270 {
20271 *fnamep = s;
20272 if (pbuf != NULL)
20273 {
20274 vim_free(*bufp); /* free any allocated file name */
20275 *bufp = pbuf;
20276 pbuf = NULL;
20277 }
20278 }
20279 }
20280 else
20281 {
20282 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20283 /* Only replace it when it starts with '~' */
20284 if (*dirname == '~')
20285 {
20286 s = vim_strsave(dirname);
20287 if (s != NULL)
20288 {
20289 *fnamep = s;
20290 vim_free(*bufp);
20291 *bufp = s;
20292 }
20293 }
20294 }
20295 vim_free(pbuf);
20296 }
20297 }
20298
20299 tail = gettail(*fnamep);
20300 *fnamelen = (int)STRLEN(*fnamep);
20301
20302 /* ":h" - head, remove "/file_name", can be repeated */
20303 /* Don't remove the first "/" or "c:\" */
20304 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20305 {
20306 valid |= VALID_HEAD;
20307 *usedlen += 2;
20308 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020309 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 --tail;
20311 *fnamelen = (int)(tail - *fnamep);
20312#ifdef VMS
20313 if (*fnamelen > 0)
20314 *fnamelen += 1; /* the path separator is part of the path */
20315#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020316 while (tail > s && !after_pathsep(s, tail))
20317 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 }
20319
20320 /* ":8" - shortname */
20321 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20322 {
20323 *usedlen += 2;
20324#ifdef WIN3264
20325 has_shortname = 1;
20326#endif
20327 }
20328
20329#ifdef WIN3264
20330 /* Check shortname after we have done 'heads' and before we do 'tails'
20331 */
20332 if (has_shortname)
20333 {
20334 pbuf = NULL;
20335 /* Copy the string if it is shortened by :h */
20336 if (*fnamelen < (int)STRLEN(*fnamep))
20337 {
20338 p = vim_strnsave(*fnamep, *fnamelen);
20339 if (p == 0)
20340 return -1;
20341 vim_free(*bufp);
20342 *bufp = *fnamep = p;
20343 }
20344
20345 /* Split into two implementations - makes it easier. First is where
20346 * there isn't a full name already, second is where there is.
20347 */
20348 if (!has_fullname && !vim_isAbsName(*fnamep))
20349 {
20350 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20351 return -1;
20352 }
20353 else
20354 {
20355 int l;
20356
20357 /* Simple case, already have the full-name
20358 * Nearly always shorter, so try first time. */
20359 l = *fnamelen;
20360 if (!get_short_pathname(fnamep, bufp, &l))
20361 return -1;
20362
20363 if (l == 0)
20364 {
20365 /* Couldn't find the filename.. search the paths.
20366 */
20367 l = *fnamelen;
20368 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20369 return -1;
20370 }
20371 *fnamelen = l;
20372 }
20373 }
20374#endif /* WIN3264 */
20375
20376 /* ":t" - tail, just the basename */
20377 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20378 {
20379 *usedlen += 2;
20380 *fnamelen -= (int)(tail - *fnamep);
20381 *fnamep = tail;
20382 }
20383
20384 /* ":e" - extension, can be repeated */
20385 /* ":r" - root, without extension, can be repeated */
20386 while (src[*usedlen] == ':'
20387 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20388 {
20389 /* find a '.' in the tail:
20390 * - for second :e: before the current fname
20391 * - otherwise: The last '.'
20392 */
20393 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20394 s = *fnamep - 2;
20395 else
20396 s = *fnamep + *fnamelen - 1;
20397 for ( ; s > tail; --s)
20398 if (s[0] == '.')
20399 break;
20400 if (src[*usedlen + 1] == 'e') /* :e */
20401 {
20402 if (s > tail)
20403 {
20404 *fnamelen += (int)(*fnamep - (s + 1));
20405 *fnamep = s + 1;
20406#ifdef VMS
20407 /* cut version from the extension */
20408 s = *fnamep + *fnamelen - 1;
20409 for ( ; s > *fnamep; --s)
20410 if (s[0] == ';')
20411 break;
20412 if (s > *fnamep)
20413 *fnamelen = s - *fnamep;
20414#endif
20415 }
20416 else if (*fnamep <= tail)
20417 *fnamelen = 0;
20418 }
20419 else /* :r */
20420 {
20421 if (s > tail) /* remove one extension */
20422 *fnamelen = (int)(s - *fnamep);
20423 }
20424 *usedlen += 2;
20425 }
20426
20427 /* ":s?pat?foo?" - substitute */
20428 /* ":gs?pat?foo?" - global substitute */
20429 if (src[*usedlen] == ':'
20430 && (src[*usedlen + 1] == 's'
20431 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20432 {
20433 char_u *str;
20434 char_u *pat;
20435 char_u *sub;
20436 int sep;
20437 char_u *flags;
20438 int didit = FALSE;
20439
20440 flags = (char_u *)"";
20441 s = src + *usedlen + 2;
20442 if (src[*usedlen + 1] == 'g')
20443 {
20444 flags = (char_u *)"g";
20445 ++s;
20446 }
20447
20448 sep = *s++;
20449 if (sep)
20450 {
20451 /* find end of pattern */
20452 p = vim_strchr(s, sep);
20453 if (p != NULL)
20454 {
20455 pat = vim_strnsave(s, (int)(p - s));
20456 if (pat != NULL)
20457 {
20458 s = p + 1;
20459 /* find end of substitution */
20460 p = vim_strchr(s, sep);
20461 if (p != NULL)
20462 {
20463 sub = vim_strnsave(s, (int)(p - s));
20464 str = vim_strnsave(*fnamep, *fnamelen);
20465 if (sub != NULL && str != NULL)
20466 {
20467 *usedlen = (int)(p + 1 - src);
20468 s = do_string_sub(str, pat, sub, flags);
20469 if (s != NULL)
20470 {
20471 *fnamep = s;
20472 *fnamelen = (int)STRLEN(s);
20473 vim_free(*bufp);
20474 *bufp = s;
20475 didit = TRUE;
20476 }
20477 }
20478 vim_free(sub);
20479 vim_free(str);
20480 }
20481 vim_free(pat);
20482 }
20483 }
20484 /* after using ":s", repeat all the modifiers */
20485 if (didit)
20486 goto repeat;
20487 }
20488 }
20489
20490 return valid;
20491}
20492
20493/*
20494 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20495 * "flags" can be "g" to do a global substitute.
20496 * Returns an allocated string, NULL for error.
20497 */
20498 char_u *
20499do_string_sub(str, pat, sub, flags)
20500 char_u *str;
20501 char_u *pat;
20502 char_u *sub;
20503 char_u *flags;
20504{
20505 int sublen;
20506 regmatch_T regmatch;
20507 int i;
20508 int do_all;
20509 char_u *tail;
20510 garray_T ga;
20511 char_u *ret;
20512 char_u *save_cpo;
20513
20514 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20515 save_cpo = p_cpo;
20516 p_cpo = (char_u *)"";
20517
20518 ga_init2(&ga, 1, 200);
20519
20520 do_all = (flags[0] == 'g');
20521
20522 regmatch.rm_ic = p_ic;
20523 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20524 if (regmatch.regprog != NULL)
20525 {
20526 tail = str;
20527 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20528 {
20529 /*
20530 * Get some space for a temporary buffer to do the substitution
20531 * into. It will contain:
20532 * - The text up to where the match is.
20533 * - The substituted text.
20534 * - The text after the match.
20535 */
20536 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20537 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20538 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20539 {
20540 ga_clear(&ga);
20541 break;
20542 }
20543
20544 /* copy the text up to where the match is */
20545 i = (int)(regmatch.startp[0] - tail);
20546 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20547 /* add the substituted text */
20548 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20549 + ga.ga_len + i, TRUE, TRUE, FALSE);
20550 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 /* avoid getting stuck on a match with an empty string */
20552 if (tail == regmatch.endp[0])
20553 {
20554 if (*tail == NUL)
20555 break;
20556 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20557 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558 }
20559 else
20560 {
20561 tail = regmatch.endp[0];
20562 if (*tail == NUL)
20563 break;
20564 }
20565 if (!do_all)
20566 break;
20567 }
20568
20569 if (ga.ga_data != NULL)
20570 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20571
20572 vim_free(regmatch.regprog);
20573 }
20574
20575 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20576 ga_clear(&ga);
20577 p_cpo = save_cpo;
20578
20579 return ret;
20580}
20581
20582#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */