blob: 060b5bc39e7c42133811c53228d7c9ae215e6f06 [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));
648static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000649static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000651static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
652static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static int get_env_len __ARGS((char_u **arg));
654static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000656static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
657#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
658#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
659 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000660static 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 +0000661static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000662static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000663static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
664static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static typval_T *alloc_tv __ARGS((void));
666static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void init_tv __ARGS((typval_T *varp));
668static long get_tv_number __ARGS((typval_T *varp));
669static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000670static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static char_u *get_tv_string __ARGS((typval_T *varp));
672static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000673static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000675static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
677static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
678static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
679static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
680static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
681static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
682static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000683static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000685static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
687static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
688static int eval_fname_script __ARGS((char_u *p));
689static int eval_fname_sid __ARGS((char_u *p));
690static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static ufunc_T *find_func __ARGS((char_u *name));
692static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000693static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000694#ifdef FEAT_PROFILE
695static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000696static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
697static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
698static int
699# ifdef __BORLANDC__
700 _RTLENTRYF
701# endif
702 prof_total_cmp __ARGS((const void *s1, const void *s2));
703static int
704# ifdef __BORLANDC__
705 _RTLENTRYF
706# endif
707 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000708#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000709static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000710static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000711static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void func_free __ARGS((ufunc_T *fp));
713static void func_unref __ARGS((char_u *name));
714static void func_ref __ARGS((char_u *name));
715static 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));
716static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000717static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000718static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
719static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000721/* Character used as separated in autoload function/variable names. */
722#define AUTOLOAD_CHAR '#'
723
Bram Moolenaar33570922005-01-25 22:26:29 +0000724/*
725 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000726 */
727 void
728eval_init()
729{
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 int i;
731 struct vimvar *p;
732
733 init_var_dict(&globvardict, &globvars_var);
734 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000735 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000736 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000737
738 for (i = 0; i < VV_LEN; ++i)
739 {
740 p = &vimvars[i];
741 STRCPY(p->vv_di.di_key, p->vv_name);
742 if (p->vv_flags & VV_RO)
743 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
744 else if (p->vv_flags & VV_RO_SBX)
745 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
746 else
747 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000748
749 /* add to v: scope dict, unless the value is not always available */
750 if (p->vv_type != VAR_UNKNOWN)
751 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000752 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000753 /* add to compat scope dict */
754 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000755 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000756}
757
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000758#if defined(EXITFREE) || defined(PROTO)
759 void
760eval_clear()
761{
762 int i;
763 struct vimvar *p;
764
765 for (i = 0; i < VV_LEN; ++i)
766 {
767 p = &vimvars[i];
768 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000769 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000770 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000771 p->vv_di.di_tv.vval.v_string = NULL;
772 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000773 }
774 hash_clear(&vimvarht);
775 hash_clear(&compat_hashtab);
776
777 /* script-local variables */
778 for (i = 1; i <= ga_scripts.ga_len; ++i)
779 vars_clear(&SCRIPT_VARS(i));
780 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000781 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000782
783 /* global variables */
784 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000785
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000786 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000787 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000788 hash_clear(&func_hashtab);
789
790 /* unreferenced lists and dicts */
791 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000792}
793#endif
794
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 * Return the name of the executed function.
797 */
798 char_u *
799func_name(cookie)
800 void *cookie;
801{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000802 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803}
804
805/*
806 * Return the address holding the next breakpoint line for a funccall cookie.
807 */
808 linenr_T *
809func_breakpoint(cookie)
810 void *cookie;
811{
Bram Moolenaar33570922005-01-25 22:26:29 +0000812 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813}
814
815/*
816 * Return the address holding the debug tick for a funccall cookie.
817 */
818 int *
819func_dbg_tick(cookie)
820 void *cookie;
821{
Bram Moolenaar33570922005-01-25 22:26:29 +0000822 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823}
824
825/*
826 * Return the nesting level for a funccall cookie.
827 */
828 int
829func_level(cookie)
830 void *cookie;
831{
Bram Moolenaar33570922005-01-25 22:26:29 +0000832 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833}
834
835/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000836funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
838/*
839 * Return TRUE when a function was ended by a ":return" command.
840 */
841 int
842current_func_returned()
843{
844 return current_funccal->returned;
845}
846
847
848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 * Set an internal variable to a string value. Creates the variable if it does
850 * not already exist.
851 */
852 void
853set_internal_string_var(name, value)
854 char_u *name;
855 char_u *value;
856{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000857 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
860 val = vim_strsave(value);
861 if (val != NULL)
862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000864 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000866 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000867 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869 }
870}
871
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000872static lval_T *redir_lval = NULL;
873static char_u *redir_endp = NULL;
874static char_u *redir_varname = NULL;
875
876/*
877 * Start recording command output to a variable
878 * Returns OK if successfully completed the setup. FAIL otherwise.
879 */
880 int
881var_redir_start(name, append)
882 char_u *name;
883 int append; /* append to an existing variable */
884{
885 int save_emsg;
886 int err;
887 typval_T tv;
888
889 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000890 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000891 {
892 EMSG(_(e_invarg));
893 return FAIL;
894 }
895
896 redir_varname = vim_strsave(name);
897 if (redir_varname == NULL)
898 return FAIL;
899
900 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
901 if (redir_lval == NULL)
902 {
903 var_redir_stop();
904 return FAIL;
905 }
906
907 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000908 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
909 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
911 {
912 if (redir_endp != NULL && *redir_endp != NUL)
913 /* Trailing characters are present after the variable name */
914 EMSG(_(e_trailing));
915 else
916 EMSG(_(e_invarg));
917 var_redir_stop();
918 return FAIL;
919 }
920
921 /* check if we can write to the variable: set it to or append an empty
922 * string */
923 save_emsg = did_emsg;
924 did_emsg = FALSE;
925 tv.v_type = VAR_STRING;
926 tv.vval.v_string = (char_u *)"";
927 if (append)
928 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
929 else
930 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
931 err = did_emsg;
932 did_emsg += save_emsg;
933 if (err)
934 {
935 var_redir_stop();
936 return FAIL;
937 }
938 if (redir_lval->ll_newkey != NULL)
939 {
940 /* Dictionary item was created, don't do it again. */
941 vim_free(redir_lval->ll_newkey);
942 redir_lval->ll_newkey = NULL;
943 }
944
945 return OK;
946}
947
948/*
949 * Append "value[len]" to the variable set by var_redir_start().
950 */
951 void
952var_redir_str(value, len)
953 char_u *value;
954 int len;
955{
956 char_u *val;
957 typval_T tv;
958 int save_emsg;
959 int err;
960
961 if (redir_lval == NULL)
962 return;
963
964 if (len == -1)
965 /* Append the entire string */
966 val = vim_strsave(value);
967 else
968 /* Append only the specified number of characters */
969 val = vim_strnsave(value, len);
970 if (val == NULL)
971 return;
972
973 tv.v_type = VAR_STRING;
974 tv.vval.v_string = val;
975
976 save_emsg = did_emsg;
977 did_emsg = FALSE;
978 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
979 err = did_emsg;
980 did_emsg += save_emsg;
981 if (err)
982 var_redir_stop();
983
984 vim_free(tv.vval.v_string);
985}
986
987/*
988 * Stop redirecting command output to a variable.
989 */
990 void
991var_redir_stop()
992{
993 if (redir_lval != NULL)
994 {
995 clear_lval(redir_lval);
996 vim_free(redir_lval);
997 redir_lval = NULL;
998 }
999 vim_free(redir_varname);
1000 redir_varname = NULL;
1001}
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003# if defined(FEAT_MBYTE) || defined(PROTO)
1004 int
1005eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1006 char_u *enc_from;
1007 char_u *enc_to;
1008 char_u *fname_from;
1009 char_u *fname_to;
1010{
1011 int err = FALSE;
1012
1013 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1014 set_vim_var_string(VV_CC_TO, enc_to, -1);
1015 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1016 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1017 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1018 err = TRUE;
1019 set_vim_var_string(VV_CC_FROM, NULL, -1);
1020 set_vim_var_string(VV_CC_TO, NULL, -1);
1021 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1022 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1023
1024 if (err)
1025 return FAIL;
1026 return OK;
1027}
1028# endif
1029
1030# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1031 int
1032eval_printexpr(fname, args)
1033 char_u *fname;
1034 char_u *args;
1035{
1036 int err = FALSE;
1037
1038 set_vim_var_string(VV_FNAME_IN, fname, -1);
1039 set_vim_var_string(VV_CMDARG, args, -1);
1040 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1041 err = TRUE;
1042 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1043 set_vim_var_string(VV_CMDARG, NULL, -1);
1044
1045 if (err)
1046 {
1047 mch_remove(fname);
1048 return FAIL;
1049 }
1050 return OK;
1051}
1052# endif
1053
1054# if defined(FEAT_DIFF) || defined(PROTO)
1055 void
1056eval_diff(origfile, newfile, outfile)
1057 char_u *origfile;
1058 char_u *newfile;
1059 char_u *outfile;
1060{
1061 int err = FALSE;
1062
1063 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1064 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1065 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1066 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1067 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1068 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1069 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1070}
1071
1072 void
1073eval_patch(origfile, difffile, outfile)
1074 char_u *origfile;
1075 char_u *difffile;
1076 char_u *outfile;
1077{
1078 int err;
1079
1080 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1081 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1082 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1083 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1084 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1085 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1086 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1087}
1088# endif
1089
1090/*
1091 * Top level evaluation function, returning a boolean.
1092 * Sets "error" to TRUE if there was an error.
1093 * Return TRUE or FALSE.
1094 */
1095 int
1096eval_to_bool(arg, error, nextcmd, skip)
1097 char_u *arg;
1098 int *error;
1099 char_u **nextcmd;
1100 int skip; /* only parse, don't execute */
1101{
Bram Moolenaar33570922005-01-25 22:26:29 +00001102 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 int retval = FALSE;
1104
1105 if (skip)
1106 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001107 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 else
1110 {
1111 *error = FALSE;
1112 if (!skip)
1113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001114 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001115 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 }
1117 }
1118 if (skip)
1119 --emsg_skip;
1120
1121 return retval;
1122}
1123
1124/*
1125 * Top level evaluation function, returning a string. If "skip" is TRUE,
1126 * only parsing to "nextcmd" is done, without reporting errors. Return
1127 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1128 */
1129 char_u *
1130eval_to_string_skip(arg, nextcmd, skip)
1131 char_u *arg;
1132 char_u **nextcmd;
1133 int skip; /* only parse, don't execute */
1134{
Bram Moolenaar33570922005-01-25 22:26:29 +00001135 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 char_u *retval;
1137
1138 if (skip)
1139 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001140 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 retval = NULL;
1142 else
1143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001144 retval = vim_strsave(get_tv_string(&tv));
1145 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 }
1147 if (skip)
1148 --emsg_skip;
1149
1150 return retval;
1151}
1152
1153/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001154 * Skip over an expression at "*pp".
1155 * Return FAIL for an error, OK otherwise.
1156 */
1157 int
1158skip_expr(pp)
1159 char_u **pp;
1160{
Bram Moolenaar33570922005-01-25 22:26:29 +00001161 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001162
1163 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001164 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001165}
1166
1167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 * Top level evaluation function, returning a string.
1169 * Return pointer to allocated memory, or NULL for failure.
1170 */
1171 char_u *
1172eval_to_string(arg, nextcmd)
1173 char_u *arg;
1174 char_u **nextcmd;
1175{
Bram Moolenaar33570922005-01-25 22:26:29 +00001176 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 char_u *retval;
1178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001179 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 retval = NULL;
1181 else
1182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 retval = vim_strsave(get_tv_string(&tv));
1184 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 }
1186
1187 return retval;
1188}
1189
1190/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001191 * Call eval_to_string() without using current local variables and using
1192 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 */
1194 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001195eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 char_u *arg;
1197 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001198 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
1200 char_u *retval;
1201 void *save_funccalp;
1202
1203 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001204 if (use_sandbox)
1205 ++sandbox;
1206 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001208 if (use_sandbox)
1209 --sandbox;
1210 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 restore_funccal(save_funccalp);
1212 return retval;
1213}
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215/*
1216 * Top level evaluation function, returning a number.
1217 * Evaluates "expr" silently.
1218 * Returns -1 for an error.
1219 */
1220 int
1221eval_to_number(expr)
1222 char_u *expr;
1223{
Bram Moolenaar33570922005-01-25 22:26:29 +00001224 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001226 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
1228 ++emsg_off;
1229
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 retval = -1;
1232 else
1233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001234 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001235 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 }
1237 --emsg_off;
1238
1239 return retval;
1240}
1241
Bram Moolenaara40058a2005-07-11 22:42:07 +00001242/*
1243 * Prepare v: variable "idx" to be used.
1244 * Save the current typeval in "save_tv".
1245 * When not used yet add the variable to the v: hashtable.
1246 */
1247 static void
1248prepare_vimvar(idx, save_tv)
1249 int idx;
1250 typval_T *save_tv;
1251{
1252 *save_tv = vimvars[idx].vv_tv;
1253 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1254 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1255}
1256
1257/*
1258 * Restore v: variable "idx" to typeval "save_tv".
1259 * When no longer defined, remove the variable from the v: hashtable.
1260 */
1261 static void
1262restore_vimvar(idx, save_tv)
1263 int idx;
1264 typval_T *save_tv;
1265{
1266 hashitem_T *hi;
1267
1268 clear_tv(&vimvars[idx].vv_tv);
1269 vimvars[idx].vv_tv = *save_tv;
1270 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1271 {
1272 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1273 if (HASHITEM_EMPTY(hi))
1274 EMSG2(_(e_intern2), "restore_vimvar()");
1275 else
1276 hash_remove(&vimvarht, hi);
1277 }
1278}
1279
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001280#if defined(FEAT_SYN_HL) || defined(PROTO)
1281/*
1282 * Evaluate an expression to a list with suggestions.
1283 * For the "expr:" part of 'spellsuggest'.
1284 */
1285 list_T *
1286eval_spell_expr(badword, expr)
1287 char_u *badword;
1288 char_u *expr;
1289{
1290 typval_T save_val;
1291 typval_T rettv;
1292 list_T *list = NULL;
1293 char_u *p = skipwhite(expr);
1294
1295 /* Set "v:val" to the bad word. */
1296 prepare_vimvar(VV_VAL, &save_val);
1297 vimvars[VV_VAL].vv_type = VAR_STRING;
1298 vimvars[VV_VAL].vv_str = badword;
1299 if (p_verbose == 0)
1300 ++emsg_off;
1301
1302 if (eval1(&p, &rettv, TRUE) == OK)
1303 {
1304 if (rettv.v_type != VAR_LIST)
1305 clear_tv(&rettv);
1306 else
1307 list = rettv.vval.v_list;
1308 }
1309
1310 if (p_verbose == 0)
1311 --emsg_off;
1312 vimvars[VV_VAL].vv_str = NULL;
1313 restore_vimvar(VV_VAL, &save_val);
1314
1315 return list;
1316}
1317
1318/*
1319 * "list" is supposed to contain two items: a word and a number. Return the
1320 * word in "pp" and the number as the return value.
1321 * Return -1 if anything isn't right.
1322 * Used to get the good word and score from the eval_spell_expr() result.
1323 */
1324 int
1325get_spellword(list, pp)
1326 list_T *list;
1327 char_u **pp;
1328{
1329 listitem_T *li;
1330
1331 li = list->lv_first;
1332 if (li == NULL)
1333 return -1;
1334 *pp = get_tv_string(&li->li_tv);
1335
1336 li = li->li_next;
1337 if (li == NULL)
1338 return -1;
1339 return get_tv_number(&li->li_tv);
1340}
1341#endif
1342
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001343/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001344 * Top level evaluation function.
1345 * Returns an allocated typval_T with the result.
1346 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001347 */
1348 typval_T *
1349eval_expr(arg, nextcmd)
1350 char_u *arg;
1351 char_u **nextcmd;
1352{
1353 typval_T *tv;
1354
1355 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001356 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001357 {
1358 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001359 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001360 }
1361
1362 return tv;
1363}
1364
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1367/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001368 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001370 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001372 static int
1373call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 char_u *func;
1375 int argc;
1376 char_u **argv;
1377 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
Bram Moolenaar33570922005-01-25 22:26:29 +00001380 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 long n;
1382 int len;
1383 int i;
1384 int doesrange;
1385 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001386 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
Bram Moolenaar33570922005-01-25 22:26:29 +00001388 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001390 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 for (i = 0; i < argc; i++)
1393 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001394 /* Pass a NULL or empty argument as an empty string */
1395 if (argv[i] == NULL || *argv[i] == NUL)
1396 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001397 argvars[i].v_type = VAR_STRING;
1398 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001399 continue;
1400 }
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 /* Recognize a number argument, the others must be strings. */
1403 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1404 if (len != 0 && len == (int)STRLEN(argv[i]))
1405 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001406 argvars[i].v_type = VAR_NUMBER;
1407 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 else
1410 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001411 argvars[i].v_type = VAR_STRING;
1412 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414 }
1415
1416 if (safe)
1417 {
1418 save_funccalp = save_funccal();
1419 ++sandbox;
1420 }
1421
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1423 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (safe)
1427 {
1428 --sandbox;
1429 restore_funccal(save_funccalp);
1430 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001431 vim_free(argvars);
1432
1433 if (ret == FAIL)
1434 clear_tv(rettv);
1435
1436 return ret;
1437}
1438
1439/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001440 * Call vimL function "func" and return the result as a string.
1441 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442 * Uses argv[argc] for the function arguments.
1443 */
1444 void *
1445call_func_retstr(func, argc, argv, safe)
1446 char_u *func;
1447 int argc;
1448 char_u **argv;
1449 int safe; /* use the sandbox */
1450{
1451 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001452 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001453
1454 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1455 return NULL;
1456
1457 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001458 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 return retval;
1460}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001462#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001463/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001464 * Call vimL function "func" and return the result as a number.
1465 * Returns -1 when calling the function fails.
1466 * Uses argv[argc] for the function arguments.
1467 */
1468 long
1469call_func_retnr(func, argc, argv, safe)
1470 char_u *func;
1471 int argc;
1472 char_u **argv;
1473 int safe; /* use the sandbox */
1474{
1475 typval_T rettv;
1476 long retval;
1477
1478 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1479 return -1;
1480
1481 retval = get_tv_number_chk(&rettv, NULL);
1482 clear_tv(&rettv);
1483 return retval;
1484}
1485#endif
1486
1487/*
1488 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001489 * Uses argv[argc] for the function arguments.
1490 */
1491 void *
1492call_func_retlist(func, argc, argv, safe)
1493 char_u *func;
1494 int argc;
1495 char_u **argv;
1496 int safe; /* use the sandbox */
1497{
1498 typval_T rettv;
1499
1500 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1501 return NULL;
1502
1503 if (rettv.v_type != VAR_LIST)
1504 {
1505 clear_tv(&rettv);
1506 return NULL;
1507 }
1508
1509 return rettv.vval.v_list;
1510}
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512#endif
1513
1514/*
1515 * Save the current function call pointer, and set it to NULL.
1516 * Used when executing autocommands and for ":source".
1517 */
1518 void *
1519save_funccal()
1520{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001521 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 current_funccal = NULL;
1524 return (void *)fc;
1525}
1526
1527 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001528restore_funccal(vfc)
1529 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001531 funccall_T *fc = (funccall_T *)vfc;
1532
1533 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534}
1535
Bram Moolenaar05159a02005-02-26 23:04:13 +00001536#if defined(FEAT_PROFILE) || defined(PROTO)
1537/*
1538 * Prepare profiling for entering a child or something else that is not
1539 * counted for the script/function itself.
1540 * Should always be called in pair with prof_child_exit().
1541 */
1542 void
1543prof_child_enter(tm)
1544 proftime_T *tm; /* place to store waittime */
1545{
1546 funccall_T *fc = current_funccal;
1547
1548 if (fc != NULL && fc->func->uf_profiling)
1549 profile_start(&fc->prof_child);
1550 script_prof_save(tm);
1551}
1552
1553/*
1554 * Take care of time spent in a child.
1555 * Should always be called after prof_child_enter().
1556 */
1557 void
1558prof_child_exit(tm)
1559 proftime_T *tm; /* where waittime was stored */
1560{
1561 funccall_T *fc = current_funccal;
1562
1563 if (fc != NULL && fc->func->uf_profiling)
1564 {
1565 profile_end(&fc->prof_child);
1566 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1567 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1568 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1569 }
1570 script_prof_restore(tm);
1571}
1572#endif
1573
1574
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575#ifdef FEAT_FOLDING
1576/*
1577 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1578 * it in "*cp". Doesn't give error messages.
1579 */
1580 int
1581eval_foldexpr(arg, cp)
1582 char_u *arg;
1583 int *cp;
1584{
Bram Moolenaar33570922005-01-25 22:26:29 +00001585 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 int retval;
1587 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001588 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1589 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001592 if (use_sandbox)
1593 ++sandbox;
1594 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001596 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 retval = 0;
1598 else
1599 {
1600 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 if (tv.v_type == VAR_NUMBER)
1602 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001603 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 retval = 0;
1605 else
1606 {
1607 /* If the result is a string, check if there is a non-digit before
1608 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (!VIM_ISDIGIT(*s) && *s != '-')
1611 *cp = *s++;
1612 retval = atol((char *)s);
1613 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
1616 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001617 if (use_sandbox)
1618 --sandbox;
1619 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 return retval;
1622}
1623#endif
1624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 * ":let" list all variable values
1627 * ":let var1 var2" list variable values
1628 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 * ":let var += expr" assignment command.
1630 * ":let var -= expr" assignment command.
1631 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 */
1634 void
1635ex_let(eap)
1636 exarg_T *eap;
1637{
1638 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001640 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001642 int var_count = 0;
1643 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646 expr = skip_var_list(arg, &var_count, &semicolon);
1647 if (expr == NULL)
1648 return;
1649 expr = vim_strchr(expr, '=');
1650 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001652 /*
1653 * ":let" without "=": list variables
1654 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655 if (*arg == '[')
1656 EMSG(_(e_invarg));
1657 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 /* ":let var1 var2" */
1659 arg = list_arg_vars(eap, arg);
1660 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001663 list_glob_vars();
1664 list_buf_vars();
1665 list_win_vars();
1666 list_vim_vars();
1667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 eap->nextcmd = check_nextcmd(arg);
1669 }
1670 else
1671 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672 op[0] = '=';
1673 op[1] = NUL;
1674 if (expr > arg)
1675 {
1676 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1677 op[0] = expr[-1]; /* +=, -= or .= */
1678 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (eap->skip)
1682 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (eap->skip)
1685 {
1686 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 --emsg_skip;
1689 }
1690 else if (i != FAIL)
1691 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001693 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001694 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
1696 }
1697}
1698
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699/*
1700 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1701 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 * When "nextchars" is not NULL it points to a string with characters that
1703 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1704 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 * Returns OK or FAIL;
1706 */
1707 static int
1708ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1709 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001710 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 int copy; /* copy values from "tv", don't move */
1712 int semicolon; /* from skip_var_list() */
1713 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001714 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715{
1716 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001717 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001718 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001719 listitem_T *item;
1720 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721
1722 if (*arg != '[')
1723 {
1724 /*
1725 * ":let var = expr" or ":for var in list"
1726 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728 return FAIL;
1729 return OK;
1730 }
1731
1732 /*
1733 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1734 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001735 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 {
1737 EMSG(_(e_listreq));
1738 return FAIL;
1739 }
1740
1741 i = list_len(l);
1742 if (semicolon == 0 && var_count < i)
1743 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001744 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 return FAIL;
1746 }
1747 if (var_count - semicolon > i)
1748 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001749 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 return FAIL;
1751 }
1752
1753 item = l->lv_first;
1754 while (*arg != ']')
1755 {
1756 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 item = item->li_next;
1759 if (arg == NULL)
1760 return FAIL;
1761
1762 arg = skipwhite(arg);
1763 if (*arg == ';')
1764 {
1765 /* Put the rest of the list (may be empty) in the var after ';'.
1766 * Create a new list for this. */
1767 l = list_alloc();
1768 if (l == NULL)
1769 return FAIL;
1770 while (item != NULL)
1771 {
1772 list_append_tv(l, &item->li_tv);
1773 item = item->li_next;
1774 }
1775
1776 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001777 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 ltv.vval.v_list = l;
1779 l->lv_refcount = 1;
1780
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001781 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1782 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 clear_tv(&ltv);
1784 if (arg == NULL)
1785 return FAIL;
1786 break;
1787 }
1788 else if (*arg != ',' && *arg != ']')
1789 {
1790 EMSG2(_(e_intern2), "ex_let_vars()");
1791 return FAIL;
1792 }
1793 }
1794
1795 return OK;
1796}
1797
1798/*
1799 * Skip over assignable variable "var" or list of variables "[var, var]".
1800 * Used for ":let varvar = expr" and ":for varvar in expr".
1801 * For "[var, var]" increment "*var_count" for each variable.
1802 * for "[var, var; var]" set "semicolon".
1803 * Return NULL for an error.
1804 */
1805 static char_u *
1806skip_var_list(arg, var_count, semicolon)
1807 char_u *arg;
1808 int *var_count;
1809 int *semicolon;
1810{
1811 char_u *p, *s;
1812
1813 if (*arg == '[')
1814 {
1815 /* "[var, var]": find the matching ']'. */
1816 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001817 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 {
1819 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1820 s = skip_var_one(p);
1821 if (s == p)
1822 {
1823 EMSG2(_(e_invarg2), p);
1824 return NULL;
1825 }
1826 ++*var_count;
1827
1828 p = skipwhite(s);
1829 if (*p == ']')
1830 break;
1831 else if (*p == ';')
1832 {
1833 if (*semicolon == 1)
1834 {
1835 EMSG(_("Double ; in list of variables"));
1836 return NULL;
1837 }
1838 *semicolon = 1;
1839 }
1840 else if (*p != ',')
1841 {
1842 EMSG2(_(e_invarg2), p);
1843 return NULL;
1844 }
1845 }
1846 return p + 1;
1847 }
1848 else
1849 return skip_var_one(arg);
1850}
1851
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001852/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001853 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1854 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001855 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 static char_u *
1857skip_var_one(arg)
1858 char_u *arg;
1859{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001860 if (*arg == '@' && arg[1] != NUL)
1861 return arg + 2;
1862 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1863 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864}
1865
Bram Moolenaara7043832005-01-21 11:56:39 +00001866/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001867 * List variables for hashtab "ht" with prefix "prefix".
1868 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001871list_hashtable_vars(ht, prefix, empty)
1872 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001873 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001875{
Bram Moolenaar33570922005-01-25 22:26:29 +00001876 hashitem_T *hi;
1877 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001878 int todo;
1879
1880 todo = ht->ht_used;
1881 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1882 {
1883 if (!HASHITEM_EMPTY(hi))
1884 {
1885 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 di = HI2DI(hi);
1887 if (empty || di->di_tv.v_type != VAR_STRING
1888 || di->di_tv.vval.v_string != NULL)
1889 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001890 }
1891 }
1892}
1893
1894/*
1895 * List global variables.
1896 */
1897 static void
1898list_glob_vars()
1899{
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001901}
1902
1903/*
1904 * List buffer variables.
1905 */
1906 static void
1907list_buf_vars()
1908{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001909 char_u numbuf[NUMBUFLEN];
1910
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001912
1913 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1914 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001915}
1916
1917/*
1918 * List window variables.
1919 */
1920 static void
1921list_win_vars()
1922{
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001924}
1925
1926/*
1927 * List Vim variables.
1928 */
1929 static void
1930list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931{
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933}
1934
1935/*
1936 * List variables in "arg".
1937 */
1938 static char_u *
1939list_arg_vars(eap, arg)
1940 exarg_T *eap;
1941 char_u *arg;
1942{
1943 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001944 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001946 char_u *name_start;
1947 char_u *arg_subsc;
1948 char_u *tofree;
1949 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950
1951 while (!ends_excmd(*arg) && !got_int)
1952 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001953 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001955 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1957 {
1958 emsg_severe = TRUE;
1959 EMSG(_(e_trailing));
1960 break;
1961 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 /* get_name_len() takes care of expanding curly braces */
1966 name_start = name = arg;
1967 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1968 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001970 /* This is mainly to keep test 49 working: when expanding
1971 * curly braces fails overrule the exception error message. */
1972 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001974 emsg_severe = TRUE;
1975 EMSG2(_(e_invarg2), arg);
1976 break;
1977 }
1978 error = TRUE;
1979 }
1980 else
1981 {
1982 if (tofree != NULL)
1983 name = tofree;
1984 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001985 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 else
1987 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001988 /* handle d.key, l[idx], f(expr) */
1989 arg_subsc = arg;
1990 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001991 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001994 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001995 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001997 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001998 case 'g': list_glob_vars(); break;
1999 case 'b': list_buf_vars(); break;
2000 case 'w': list_win_vars(); break;
2001 case 'v': list_vim_vars(); break;
2002 default:
2003 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002004 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002005 }
2006 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002007 {
2008 char_u numbuf[NUMBUFLEN];
2009 char_u *tf;
2010 int c;
2011 char_u *s;
2012
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002013 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002014 c = *arg;
2015 *arg = NUL;
2016 list_one_var_a((char_u *)"",
2017 arg == arg_subsc ? name : name_start,
2018 tv.v_type, s == NULL ? (char_u *)"" : s);
2019 *arg = c;
2020 vim_free(tf);
2021 }
2022 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 }
2025 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002026
2027 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002028 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002029
2030 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 }
2032
2033 return arg;
2034}
2035
2036/*
2037 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2038 * Returns a pointer to the char just after the var name.
2039 * Returns NULL if there is an error.
2040 */
2041 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002044 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048{
2049 int c1;
2050 char_u *name;
2051 char_u *p;
2052 char_u *arg_end = NULL;
2053 int len;
2054 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002055 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056
2057 /*
2058 * ":let $VAR = expr": Set environment variable.
2059 */
2060 if (*arg == '$')
2061 {
2062 /* Find the end of the name. */
2063 ++arg;
2064 name = arg;
2065 len = get_env_len(&arg);
2066 if (len == 0)
2067 EMSG2(_(e_invarg2), name - 1);
2068 else
2069 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002070 if (op != NULL && (*op == '+' || *op == '-'))
2071 EMSG2(_(e_letwrong), op);
2072 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 EMSG(_(e_letunexp));
2075 else
2076 {
2077 c1 = name[len];
2078 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002079 p = get_tv_string_chk(tv);
2080 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002081 {
2082 int mustfree = FALSE;
2083 char_u *s = vim_getenv(name, &mustfree);
2084
2085 if (s != NULL)
2086 {
2087 p = tofree = concat_str(s, p);
2088 if (mustfree)
2089 vim_free(s);
2090 }
2091 }
2092 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002093 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002095 if (STRICMP(name, "HOME") == 0)
2096 init_homedir();
2097 else if (didset_vim && STRICMP(name, "VIM") == 0)
2098 didset_vim = FALSE;
2099 else if (didset_vimruntime
2100 && STRICMP(name, "VIMRUNTIME") == 0)
2101 didset_vimruntime = FALSE;
2102 arg_end = arg;
2103 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002105 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 }
2107 }
2108 }
2109
2110 /*
2111 * ":let &option = expr": Set option value.
2112 * ":let &l:option = expr": Set local option value.
2113 * ":let &g:option = expr": Set global option value.
2114 */
2115 else if (*arg == '&')
2116 {
2117 /* Find the end of the name. */
2118 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002119 if (p == NULL || (endchars != NULL
2120 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 EMSG(_(e_letunexp));
2122 else
2123 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002124 long n;
2125 int opt_type;
2126 long numval;
2127 char_u *stringval = NULL;
2128 char_u *s;
2129
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130 c1 = *p;
2131 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002132
2133 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 s = get_tv_string_chk(tv); /* != NULL if number or string */
2135 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 {
2137 opt_type = get_option_value(arg, &numval,
2138 &stringval, opt_flags);
2139 if ((opt_type == 1 && *op == '.')
2140 || (opt_type == 0 && *op != '.'))
2141 EMSG2(_(e_letwrong), op);
2142 else
2143 {
2144 if (opt_type == 1) /* number */
2145 {
2146 if (*op == '+')
2147 n = numval + n;
2148 else
2149 n = numval - n;
2150 }
2151 else if (opt_type == 0 && stringval != NULL) /* string */
2152 {
2153 s = concat_str(stringval, s);
2154 vim_free(stringval);
2155 stringval = s;
2156 }
2157 }
2158 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002159 if (s != NULL)
2160 {
2161 set_option_value(arg, n, s, opt_flags);
2162 arg_end = p;
2163 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002165 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166 }
2167 }
2168
2169 /*
2170 * ":let @r = expr": Set register contents.
2171 */
2172 else if (*arg == '@')
2173 {
2174 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 if (op != NULL && (*op == '+' || *op == '-'))
2176 EMSG2(_(e_letwrong), op);
2177 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002178 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 EMSG(_(e_letunexp));
2180 else
2181 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002182 char_u *tofree = NULL;
2183 char_u *s;
2184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002185 p = get_tv_string_chk(tv);
2186 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002188 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189 if (s != NULL)
2190 {
2191 p = tofree = concat_str(s, p);
2192 vim_free(s);
2193 }
2194 }
2195 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002197 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002198 arg_end = arg + 1;
2199 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002200 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 }
2202 }
2203
2204 /*
2205 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002210 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002212 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2216 EMSG(_(e_letunexp));
2217 else
2218 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002219 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 arg_end = p;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 }
2225
2226 else
2227 EMSG2(_(e_invarg2), arg);
2228
2229 return arg_end;
2230}
2231
2232/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002233 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2234 */
2235 static int
2236check_changedtick(arg)
2237 char_u *arg;
2238{
2239 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2240 {
2241 EMSG2(_(e_readonlyvar), arg);
2242 return TRUE;
2243 }
2244 return FALSE;
2245}
2246
2247/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248 * Get an lval: variable, Dict item or List item that can be assigned a value
2249 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2250 * "name.key", "name.key[expr]" etc.
2251 * Indexing only works if "name" is an existing List or Dictionary.
2252 * "name" points to the start of the name.
2253 * If "rettv" is not NULL it points to the value to be assigned.
2254 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2255 * wrong; must end in space or cmd separator.
2256 *
2257 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 */
2261 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002262get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002264 typval_T *rettv;
2265 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 int unlet;
2267 int skip;
2268 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002269 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 char_u *expr_start, *expr_end;
2273 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002274 dictitem_T *v;
2275 typval_T var1;
2276 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002278 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002279 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002281 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002284 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285
2286 if (skip)
2287 {
2288 /* When skipping just find the end of the name. */
2289 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002290 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 }
2292
2293 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002294 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 if (expr_start != NULL)
2296 {
2297 /* Don't expand the name when we already know there is an error. */
2298 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2299 && *p != '[' && *p != '.')
2300 {
2301 EMSG(_(e_trailing));
2302 return NULL;
2303 }
2304
2305 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2306 if (lp->ll_exp_name == NULL)
2307 {
2308 /* Report an invalid expression in braces, unless the
2309 * expression evaluation has been cancelled due to an
2310 * aborting error, an interrupt, or an exception. */
2311 if (!aborting() && !quiet)
2312 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002313 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 EMSG2(_(e_invarg2), name);
2315 return NULL;
2316 }
2317 }
2318 lp->ll_name = lp->ll_exp_name;
2319 }
2320 else
2321 lp->ll_name = name;
2322
2323 /* Without [idx] or .key we are done. */
2324 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2325 return p;
2326
2327 cc = *p;
2328 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002329 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 if (v == NULL && !quiet)
2331 EMSG2(_(e_undefvar), lp->ll_name);
2332 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 if (v == NULL)
2334 return NULL;
2335
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 /*
2337 * Loop until no more [idx] or .key is following.
2338 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002339 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2343 && !(lp->ll_tv->v_type == VAR_DICT
2344 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 if (!quiet)
2347 EMSG(_("E689: Can only index a List or Dictionary"));
2348 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 if (!quiet)
2353 EMSG(_("E708: [:] must come last"));
2354 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002356
Bram Moolenaar8c711452005-01-14 21:53:12 +00002357 len = -1;
2358 if (*p == '.')
2359 {
2360 key = p + 1;
2361 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2362 ;
2363 if (len == 0)
2364 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 if (!quiet)
2366 EMSG(_(e_emptykey));
2367 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002368 }
2369 p = key + len;
2370 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002371 else
2372 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002373 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002374 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 if (*p == ':')
2376 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002377 else
2378 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002379 empty1 = FALSE;
2380 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 if (get_tv_string_chk(&var1) == NULL)
2383 {
2384 /* not a number or string */
2385 clear_tv(&var1);
2386 return NULL;
2387 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002388 }
2389
2390 /* Optionally get the second index [ :expr]. */
2391 if (*p == ':')
2392 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002394 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002397 if (!empty1)
2398 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002400 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 if (rettv != NULL && (rettv->v_type != VAR_LIST
2402 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002403 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 if (!quiet)
2405 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002406 if (!empty1)
2407 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002409 }
2410 p = skipwhite(p + 1);
2411 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 else
2414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2417 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 if (!empty1)
2419 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002422 if (get_tv_string_chk(&var2) == NULL)
2423 {
2424 /* not a number or string */
2425 if (!empty1)
2426 clear_tv(&var1);
2427 clear_tv(&var2);
2428 return NULL;
2429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002432 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002435
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002437 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (!quiet)
2439 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 if (!empty1)
2441 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 }
2446
2447 /* Skip to past ']'. */
2448 ++p;
2449 }
2450
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 {
2453 if (len == -1)
2454 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 if (*key == NUL)
2458 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (!quiet)
2460 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 }
2464 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 lp->ll_list = NULL;
2466 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002467 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002470 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 if (len == -1)
2476 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 }
2479 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 if (len == -1)
2484 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 p = NULL;
2487 break;
2488 }
2489 if (len == -1)
2490 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 }
2493 else
2494 {
2495 /*
2496 * Get the number and item for the only or first index of the List.
2497 */
2498 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 else
2501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 clear_tv(&var1);
2504 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 lp->ll_list = lp->ll_tv->vval.v_list;
2507 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2508 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (!quiet)
2511 EMSGN(_(e_listidx), lp->ll_n1);
2512 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 }
2516
2517 /*
2518 * May need to find the item or absolute index for the second
2519 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 * When no index given: "lp->ll_empty2" is TRUE.
2521 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002525 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 if (ni == NULL)
2531 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (!quiet)
2533 EMSGN(_(e_listidx), lp->ll_n2);
2534 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 }
2538
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2540 if (lp->ll_n1 < 0)
2541 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2542 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002543 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 if (!quiet)
2545 EMSGN(_(e_listidx), lp->ll_n2);
2546 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002547 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002548 }
2549
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 }
2553
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 return p;
2555}
2556
2557/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 */
2560 static void
2561clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002562 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563{
2564 vim_free(lp->ll_exp_name);
2565 vim_free(lp->ll_newkey);
2566}
2567
2568/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002569 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002571 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 */
2573 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002574set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002579 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580{
2581 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002582 listitem_T *ri;
2583 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584
2585 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 cc = *endp;
2590 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 if (op != NULL && *op != '=')
2592 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002594
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002595 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002596 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2597 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002598 {
2599 if (tv_op(&tv, rettv, op) == OK)
2600 set_var(lp->ll_name, &tv, FALSE);
2601 clear_tv(&tv);
2602 }
2603 }
2604 else
2605 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002607 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002609 else if (tv_check_lock(lp->ll_newkey == NULL
2610 ? lp->ll_tv->v_lock
2611 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2612 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 else if (lp->ll_range)
2614 {
2615 /*
2616 * Assign the List values to the list items.
2617 */
2618 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002619 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002620 if (op != NULL && *op != '=')
2621 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2622 else
2623 {
2624 clear_tv(&lp->ll_li->li_tv);
2625 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2626 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 ri = ri->li_next;
2628 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2629 break;
2630 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002633 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 ri = NULL;
2636 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002638 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 lp->ll_li = lp->ll_li->li_next;
2640 ++lp->ll_n1;
2641 }
2642 if (ri != NULL)
2643 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002644 else if (lp->ll_empty2
2645 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 : lp->ll_n1 != lp->ll_n2)
2647 EMSG(_("E711: List value has not enough items"));
2648 }
2649 else
2650 {
2651 /*
2652 * Assign to a List or Dictionary item.
2653 */
2654 if (lp->ll_newkey != NULL)
2655 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002656 if (op != NULL && *op != '=')
2657 {
2658 EMSG2(_(e_letwrong), op);
2659 return;
2660 }
2661
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002663 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (di == NULL)
2665 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002666 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2667 {
2668 vim_free(di);
2669 return;
2670 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673 else if (op != NULL && *op != '=')
2674 {
2675 tv_op(lp->ll_tv, rettv, op);
2676 return;
2677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002678 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 /*
2682 * Assign the value to the variable or list item.
2683 */
2684 if (copy)
2685 copy_tv(rettv, lp->ll_tv);
2686 else
2687 {
2688 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002689 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691 }
2692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002693}
2694
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002695/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002696 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2697 * Returns OK or FAIL.
2698 */
2699 static int
2700tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002701 typval_T *tv1;
2702 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 char_u *op;
2704{
2705 long n;
2706 char_u numbuf[NUMBUFLEN];
2707 char_u *s;
2708
2709 /* Can't do anything with a Funcref or a Dict on the right. */
2710 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2711 {
2712 switch (tv1->v_type)
2713 {
2714 case VAR_DICT:
2715 case VAR_FUNC:
2716 break;
2717
2718 case VAR_LIST:
2719 if (*op != '+' || tv2->v_type != VAR_LIST)
2720 break;
2721 /* List += List */
2722 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2723 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2724 return OK;
2725
2726 case VAR_NUMBER:
2727 case VAR_STRING:
2728 if (tv2->v_type == VAR_LIST)
2729 break;
2730 if (*op == '+' || *op == '-')
2731 {
2732 /* nr += nr or nr -= nr*/
2733 n = get_tv_number(tv1);
2734 if (*op == '+')
2735 n += get_tv_number(tv2);
2736 else
2737 n -= get_tv_number(tv2);
2738 clear_tv(tv1);
2739 tv1->v_type = VAR_NUMBER;
2740 tv1->vval.v_number = n;
2741 }
2742 else
2743 {
2744 /* str .= str */
2745 s = get_tv_string(tv1);
2746 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2747 clear_tv(tv1);
2748 tv1->v_type = VAR_STRING;
2749 tv1->vval.v_string = s;
2750 }
2751 return OK;
2752 }
2753 }
2754
2755 EMSG2(_(e_letwrong), op);
2756 return FAIL;
2757}
2758
2759/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002760 * Add a watcher to a list.
2761 */
2762 static void
2763list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002764 list_T *l;
2765 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002766{
2767 lw->lw_next = l->lv_watch;
2768 l->lv_watch = lw;
2769}
2770
2771/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002772 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002773 * No warning when it isn't found...
2774 */
2775 static void
2776list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002777 list_T *l;
2778 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002779{
Bram Moolenaar33570922005-01-25 22:26:29 +00002780 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002781
2782 lwp = &l->lv_watch;
2783 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2784 {
2785 if (lw == lwrem)
2786 {
2787 *lwp = lw->lw_next;
2788 break;
2789 }
2790 lwp = &lw->lw_next;
2791 }
2792}
2793
2794/*
2795 * Just before removing an item from a list: advance watchers to the next
2796 * item.
2797 */
2798 static void
2799list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 list_T *l;
2801 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802{
Bram Moolenaar33570922005-01-25 22:26:29 +00002803 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804
2805 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2806 if (lw->lw_item == item)
2807 lw->lw_item = item->li_next;
2808}
2809
2810/*
2811 * Evaluate the expression used in a ":for var in expr" command.
2812 * "arg" points to "var".
2813 * Set "*errp" to TRUE for an error, FALSE otherwise;
2814 * Return a pointer that holds the info. Null when there is an error.
2815 */
2816 void *
2817eval_for_line(arg, errp, nextcmdp, skip)
2818 char_u *arg;
2819 int *errp;
2820 char_u **nextcmdp;
2821 int skip;
2822{
Bram Moolenaar33570922005-01-25 22:26:29 +00002823 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002824 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002825 typval_T tv;
2826 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002827
2828 *errp = TRUE; /* default: there is an error */
2829
Bram Moolenaar33570922005-01-25 22:26:29 +00002830 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002831 if (fi == NULL)
2832 return NULL;
2833
2834 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2835 if (expr == NULL)
2836 return fi;
2837
2838 expr = skipwhite(expr);
2839 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2840 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002841 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002842 return fi;
2843 }
2844
2845 if (skip)
2846 ++emsg_skip;
2847 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2848 {
2849 *errp = FALSE;
2850 if (!skip)
2851 {
2852 l = tv.vval.v_list;
2853 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002854 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002855 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002856 clear_tv(&tv);
2857 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858 else
2859 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002860 /* No need to increment the refcount, it's already set for the
2861 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002862 fi->fi_list = l;
2863 list_add_watch(l, &fi->fi_lw);
2864 fi->fi_lw.lw_item = l->lv_first;
2865 }
2866 }
2867 }
2868 if (skip)
2869 --emsg_skip;
2870
2871 return fi;
2872}
2873
2874/*
2875 * Use the first item in a ":for" list. Advance to the next.
2876 * Assign the values to the variable (list). "arg" points to the first one.
2877 * Return TRUE when a valid item was found, FALSE when at end of list or
2878 * something wrong.
2879 */
2880 int
2881next_for_item(fi_void, arg)
2882 void *fi_void;
2883 char_u *arg;
2884{
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888
2889 item = fi->fi_lw.lw_item;
2890 if (item == NULL)
2891 result = FALSE;
2892 else
2893 {
2894 fi->fi_lw.lw_item = item->li_next;
2895 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2896 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2897 }
2898 return result;
2899}
2900
2901/*
2902 * Free the structure used to store info used by ":for".
2903 */
2904 void
2905free_for_info(fi_void)
2906 void *fi_void;
2907{
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002909
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002910 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002911 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002913 list_unref(fi->fi_list);
2914 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002915 vim_free(fi);
2916}
2917
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2919
2920 void
2921set_context_for_expression(xp, arg, cmdidx)
2922 expand_T *xp;
2923 char_u *arg;
2924 cmdidx_T cmdidx;
2925{
2926 int got_eq = FALSE;
2927 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002930 if (cmdidx == CMD_let)
2931 {
2932 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002933 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002934 {
2935 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002936 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937 {
2938 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002940 if (vim_iswhite(*p))
2941 break;
2942 }
2943 return;
2944 }
2945 }
2946 else
2947 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2948 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 while ((xp->xp_pattern = vim_strpbrk(arg,
2950 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2951 {
2952 c = *xp->xp_pattern;
2953 if (c == '&')
2954 {
2955 c = xp->xp_pattern[1];
2956 if (c == '&')
2957 {
2958 ++xp->xp_pattern;
2959 xp->xp_context = cmdidx != CMD_let || got_eq
2960 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2961 }
2962 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002965 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2966 xp->xp_pattern += 2;
2967
2968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
2970 else if (c == '$')
2971 {
2972 /* environment variable */
2973 xp->xp_context = EXPAND_ENV_VARS;
2974 }
2975 else if (c == '=')
2976 {
2977 got_eq = TRUE;
2978 xp->xp_context = EXPAND_EXPRESSION;
2979 }
2980 else if (c == '<'
2981 && xp->xp_context == EXPAND_FUNCTIONS
2982 && vim_strchr(xp->xp_pattern, '(') == NULL)
2983 {
2984 /* Function name can start with "<SNR>" */
2985 break;
2986 }
2987 else if (cmdidx != CMD_let || got_eq)
2988 {
2989 if (c == '"') /* string */
2990 {
2991 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2992 if (c == '\\' && xp->xp_pattern[1] != NUL)
2993 ++xp->xp_pattern;
2994 xp->xp_context = EXPAND_NOTHING;
2995 }
2996 else if (c == '\'') /* literal string */
2997 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002998 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3000 /* skip */ ;
3001 xp->xp_context = EXPAND_NOTHING;
3002 }
3003 else if (c == '|')
3004 {
3005 if (xp->xp_pattern[1] == '|')
3006 {
3007 ++xp->xp_pattern;
3008 xp->xp_context = EXPAND_EXPRESSION;
3009 }
3010 else
3011 xp->xp_context = EXPAND_COMMANDS;
3012 }
3013 else
3014 xp->xp_context = EXPAND_EXPRESSION;
3015 }
3016 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003017 /* Doesn't look like something valid, expand as an expression
3018 * anyway. */
3019 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 arg = xp->xp_pattern;
3021 if (*arg != NUL)
3022 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3023 /* skip */ ;
3024 }
3025 xp->xp_pattern = arg;
3026}
3027
3028#endif /* FEAT_CMDL_COMPL */
3029
3030/*
3031 * ":1,25call func(arg1, arg2)" function call.
3032 */
3033 void
3034ex_call(eap)
3035 exarg_T *eap;
3036{
3037 char_u *arg = eap->arg;
3038 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003042 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 linenr_T lnum;
3044 int doesrange;
3045 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003046 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003048 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3049 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003050 if (tofree == NULL)
3051 return;
3052
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003053 /* Increase refcount on dictionary, it could get deleted when evaluating
3054 * the arguments. */
3055 if (fudi.fd_dict != NULL)
3056 ++fudi.fd_dict->dv_refcount;
3057
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3059 len = STRLEN(tofree);
3060 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
Bram Moolenaar532c7802005-01-27 14:44:31 +00003062 /* Skip white space to allow ":call func ()". Not good, but required for
3063 * backward compatibility. */
3064 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003065 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
3067 if (*startarg != '(')
3068 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003069 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 goto end;
3071 }
3072
3073 /*
3074 * When skipping, evaluate the function once, to find the end of the
3075 * arguments.
3076 * When the function takes a range, this is discovered after the first
3077 * call, and the loop is broken.
3078 */
3079 if (eap->skip)
3080 {
3081 ++emsg_skip;
3082 lnum = eap->line2; /* do it once, also with an invalid range */
3083 }
3084 else
3085 lnum = eap->line1;
3086 for ( ; lnum <= eap->line2; ++lnum)
3087 {
3088 if (!eap->skip && eap->addr_count > 0)
3089 {
3090 curwin->w_cursor.lnum = lnum;
3091 curwin->w_cursor.col = 0;
3092 }
3093 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003095 eap->line1, eap->line2, &doesrange,
3096 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
3098 failed = TRUE;
3099 break;
3100 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003101 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (doesrange || eap->skip)
3103 break;
3104 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003105 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003107 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (aborting())
3109 break;
3110 }
3111 if (eap->skip)
3112 --emsg_skip;
3113
3114 if (!failed)
3115 {
3116 /* Check for trailing illegal characters and a following command. */
3117 if (!ends_excmd(*arg))
3118 {
3119 emsg_severe = TRUE;
3120 EMSG(_(e_trailing));
3121 }
3122 else
3123 eap->nextcmd = check_nextcmd(arg);
3124 }
3125
3126end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003127 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129}
3130
3131/*
3132 * ":unlet[!] var1 ... " command.
3133 */
3134 void
3135ex_unlet(eap)
3136 exarg_T *eap;
3137{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003138 ex_unletlock(eap, eap->arg, 0);
3139}
3140
3141/*
3142 * ":lockvar" and ":unlockvar" commands
3143 */
3144 void
3145ex_lockvar(eap)
3146 exarg_T *eap;
3147{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003149 int deep = 2;
3150
3151 if (eap->forceit)
3152 deep = -1;
3153 else if (vim_isdigit(*arg))
3154 {
3155 deep = getdigits(&arg);
3156 arg = skipwhite(arg);
3157 }
3158
3159 ex_unletlock(eap, arg, deep);
3160}
3161
3162/*
3163 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3164 */
3165 static void
3166ex_unletlock(eap, argstart, deep)
3167 exarg_T *eap;
3168 char_u *argstart;
3169 int deep;
3170{
3171 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003174 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 do
3177 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003178 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003179 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3180 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003181 if (lv.ll_name == NULL)
3182 error = TRUE; /* error but continue parsing */
3183 if (name_end == NULL || (!vim_iswhite(*name_end)
3184 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003186 if (name_end != NULL)
3187 {
3188 emsg_severe = TRUE;
3189 EMSG(_(e_trailing));
3190 }
3191 if (!(eap->skip || error))
3192 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 break;
3194 }
3195
3196 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003197 {
3198 if (eap->cmdidx == CMD_unlet)
3199 {
3200 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3201 error = TRUE;
3202 }
3203 else
3204 {
3205 if (do_lock_var(&lv, name_end, deep,
3206 eap->cmdidx == CMD_lockvar) == FAIL)
3207 error = TRUE;
3208 }
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003211 if (!eap->skip)
3212 clear_lval(&lv);
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 arg = skipwhite(name_end);
3215 } while (!ends_excmd(*arg));
3216
3217 eap->nextcmd = check_nextcmd(arg);
3218}
3219
Bram Moolenaar8c711452005-01-14 21:53:12 +00003220 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003221do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003224 int forceit;
3225{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003226 int ret = OK;
3227 int cc;
3228
3229 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003230 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003231 cc = *name_end;
3232 *name_end = NUL;
3233
3234 /* Normal name or expanded name. */
3235 if (check_changedtick(lp->ll_name))
3236 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003237 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003240 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003241 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3242 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003243 else if (lp->ll_range)
3244 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003246
3247 /* Delete a range of List items. */
3248 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3249 {
3250 li = lp->ll_li->li_next;
3251 listitem_remove(lp->ll_list, lp->ll_li);
3252 lp->ll_li = li;
3253 ++lp->ll_n1;
3254 }
3255 }
3256 else
3257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259 /* unlet a List item. */
3260 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003261 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003262 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003263 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003264 }
3265
3266 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267}
3268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269/*
3270 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003271 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 */
3273 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003274do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003276 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 hashtab_T *ht;
3279 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003280 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 ht = find_var_ht(name, &varname);
3283 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003285 hi = hash_find(ht, varname);
3286 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003287 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003288 if (var_check_ro(HI2DI(hi)->di_flags, name))
3289 return FAIL;
3290 delete_var(ht, hi);
3291 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003294 if (forceit)
3295 return OK;
3296 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 return FAIL;
3298}
3299
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003300/*
3301 * Lock or unlock variable indicated by "lp".
3302 * "deep" is the levels to go (-1 for unlimited);
3303 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3304 */
3305 static int
3306do_lock_var(lp, name_end, deep, lock)
3307 lval_T *lp;
3308 char_u *name_end;
3309 int deep;
3310 int lock;
3311{
3312 int ret = OK;
3313 int cc;
3314 dictitem_T *di;
3315
3316 if (deep == 0) /* nothing to do */
3317 return OK;
3318
3319 if (lp->ll_tv == NULL)
3320 {
3321 cc = *name_end;
3322 *name_end = NUL;
3323
3324 /* Normal name or expanded name. */
3325 if (check_changedtick(lp->ll_name))
3326 ret = FAIL;
3327 else
3328 {
3329 di = find_var(lp->ll_name, NULL);
3330 if (di == NULL)
3331 ret = FAIL;
3332 else
3333 {
3334 if (lock)
3335 di->di_flags |= DI_FLAGS_LOCK;
3336 else
3337 di->di_flags &= ~DI_FLAGS_LOCK;
3338 item_lock(&di->di_tv, deep, lock);
3339 }
3340 }
3341 *name_end = cc;
3342 }
3343 else if (lp->ll_range)
3344 {
3345 listitem_T *li = lp->ll_li;
3346
3347 /* (un)lock a range of List items. */
3348 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3349 {
3350 item_lock(&li->li_tv, deep, lock);
3351 li = li->li_next;
3352 ++lp->ll_n1;
3353 }
3354 }
3355 else if (lp->ll_list != NULL)
3356 /* (un)lock a List item. */
3357 item_lock(&lp->ll_li->li_tv, deep, lock);
3358 else
3359 /* un(lock) a Dictionary item. */
3360 item_lock(&lp->ll_di->di_tv, deep, lock);
3361
3362 return ret;
3363}
3364
3365/*
3366 * Lock or unlock an item. "deep" is nr of levels to go.
3367 */
3368 static void
3369item_lock(tv, deep, lock)
3370 typval_T *tv;
3371 int deep;
3372 int lock;
3373{
3374 static int recurse = 0;
3375 list_T *l;
3376 listitem_T *li;
3377 dict_T *d;
3378 hashitem_T *hi;
3379 int todo;
3380
3381 if (recurse >= DICT_MAXNEST)
3382 {
3383 EMSG(_("E743: variable nested too deep for (un)lock"));
3384 return;
3385 }
3386 if (deep == 0)
3387 return;
3388 ++recurse;
3389
3390 /* lock/unlock the item itself */
3391 if (lock)
3392 tv->v_lock |= VAR_LOCKED;
3393 else
3394 tv->v_lock &= ~VAR_LOCKED;
3395
3396 switch (tv->v_type)
3397 {
3398 case VAR_LIST:
3399 if ((l = tv->vval.v_list) != NULL)
3400 {
3401 if (lock)
3402 l->lv_lock |= VAR_LOCKED;
3403 else
3404 l->lv_lock &= ~VAR_LOCKED;
3405 if (deep < 0 || deep > 1)
3406 /* recursive: lock/unlock the items the List contains */
3407 for (li = l->lv_first; li != NULL; li = li->li_next)
3408 item_lock(&li->li_tv, deep - 1, lock);
3409 }
3410 break;
3411 case VAR_DICT:
3412 if ((d = tv->vval.v_dict) != NULL)
3413 {
3414 if (lock)
3415 d->dv_lock |= VAR_LOCKED;
3416 else
3417 d->dv_lock &= ~VAR_LOCKED;
3418 if (deep < 0 || deep > 1)
3419 {
3420 /* recursive: lock/unlock the items the List contains */
3421 todo = d->dv_hashtab.ht_used;
3422 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3423 {
3424 if (!HASHITEM_EMPTY(hi))
3425 {
3426 --todo;
3427 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3428 }
3429 }
3430 }
3431 }
3432 }
3433 --recurse;
3434}
3435
Bram Moolenaara40058a2005-07-11 22:42:07 +00003436/*
3437 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3438 * it refers to a List or Dictionary that is locked.
3439 */
3440 static int
3441tv_islocked(tv)
3442 typval_T *tv;
3443{
3444 return (tv->v_lock & VAR_LOCKED)
3445 || (tv->v_type == VAR_LIST
3446 && tv->vval.v_list != NULL
3447 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3448 || (tv->v_type == VAR_DICT
3449 && tv->vval.v_dict != NULL
3450 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3451}
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3454/*
3455 * Delete all "menutrans_" variables.
3456 */
3457 void
3458del_menutrans_vars()
3459{
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003461 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 hash_lock(&globvarht);
3464 todo = globvarht.ht_used;
3465 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003466 {
3467 if (!HASHITEM_EMPTY(hi))
3468 {
3469 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003470 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3471 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003472 }
3473 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003474 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475}
3476#endif
3477
3478#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3479
3480/*
3481 * Local string buffer for the next two functions to store a variable name
3482 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3483 * get_user_var_name().
3484 */
3485
3486static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3487
3488static char_u *varnamebuf = NULL;
3489static int varnamebuflen = 0;
3490
3491/*
3492 * Function to concatenate a prefix and a variable name.
3493 */
3494 static char_u *
3495cat_prefix_varname(prefix, name)
3496 int prefix;
3497 char_u *name;
3498{
3499 int len;
3500
3501 len = (int)STRLEN(name) + 3;
3502 if (len > varnamebuflen)
3503 {
3504 vim_free(varnamebuf);
3505 len += 10; /* some additional space */
3506 varnamebuf = alloc(len);
3507 if (varnamebuf == NULL)
3508 {
3509 varnamebuflen = 0;
3510 return NULL;
3511 }
3512 varnamebuflen = len;
3513 }
3514 *varnamebuf = prefix;
3515 varnamebuf[1] = ':';
3516 STRCPY(varnamebuf + 2, name);
3517 return varnamebuf;
3518}
3519
3520/*
3521 * Function given to ExpandGeneric() to obtain the list of user defined
3522 * (global/buffer/window/built-in) variable names.
3523 */
3524/*ARGSUSED*/
3525 char_u *
3526get_user_var_name(xp, idx)
3527 expand_T *xp;
3528 int idx;
3529{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003530 static long_u gdone;
3531 static long_u bdone;
3532 static long_u wdone;
3533 static int vidx;
3534 static hashitem_T *hi;
3535 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003538 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003539
3540 /* Global variables */
3541 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003543 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003544 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003545 else
3546 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003547 while (HASHITEM_EMPTY(hi))
3548 ++hi;
3549 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3550 return cat_prefix_varname('g', hi->hi_key);
3551 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003553
3554 /* b: variables */
3555 ht = &curbuf->b_vars.dv_hashtab;
3556 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003558 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003559 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003560 else
3561 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003562 while (HASHITEM_EMPTY(hi))
3563 ++hi;
3564 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003566 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003568 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 return (char_u *)"b:changedtick";
3570 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003571
3572 /* w: variables */
3573 ht = &curwin->w_vars.dv_hashtab;
3574 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003576 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003577 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003578 else
3579 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003580 while (HASHITEM_EMPTY(hi))
3581 ++hi;
3582 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003584
3585 /* v: variables */
3586 if (vidx < VV_LEN)
3587 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
3589 vim_free(varnamebuf);
3590 varnamebuf = NULL;
3591 varnamebuflen = 0;
3592 return NULL;
3593}
3594
3595#endif /* FEAT_CMDL_COMPL */
3596
3597/*
3598 * types for expressions.
3599 */
3600typedef enum
3601{
3602 TYPE_UNKNOWN = 0
3603 , TYPE_EQUAL /* == */
3604 , TYPE_NEQUAL /* != */
3605 , TYPE_GREATER /* > */
3606 , TYPE_GEQUAL /* >= */
3607 , TYPE_SMALLER /* < */
3608 , TYPE_SEQUAL /* <= */
3609 , TYPE_MATCH /* =~ */
3610 , TYPE_NOMATCH /* !~ */
3611} exptype_T;
3612
3613/*
3614 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003615 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3617 */
3618
3619/*
3620 * Handle zero level expression.
3621 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003622 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003623 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 * Return OK or FAIL.
3625 */
3626 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003627eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 char_u **nextcmd;
3631 int evaluate;
3632{
3633 int ret;
3634 char_u *p;
3635
3636 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003637 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 if (ret == FAIL || !ends_excmd(*p))
3639 {
3640 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 /*
3643 * Report the invalid expression unless the expression evaluation has
3644 * been cancelled due to an aborting error, an interrupt, or an
3645 * exception.
3646 */
3647 if (!aborting())
3648 EMSG2(_(e_invexpr2), arg);
3649 ret = FAIL;
3650 }
3651 if (nextcmd != NULL)
3652 *nextcmd = check_nextcmd(p);
3653
3654 return ret;
3655}
3656
3657/*
3658 * Handle top level expression:
3659 * expr1 ? expr0 : expr0
3660 *
3661 * "arg" must point to the first non-white of the expression.
3662 * "arg" is advanced to the next non-white after the recognized expression.
3663 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003664 * Note: "rettv.v_lock" is not set.
3665 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 * Return OK or FAIL.
3667 */
3668 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 int evaluate;
3673{
3674 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003675 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
3677 /*
3678 * Get the first variable.
3679 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003680 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 return FAIL;
3682
3683 if ((*arg)[0] == '?')
3684 {
3685 result = FALSE;
3686 if (evaluate)
3687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003688 int error = FALSE;
3689
3690 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003692 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003693 if (error)
3694 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696
3697 /*
3698 * Get the second variable.
3699 */
3700 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003701 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 return FAIL;
3703
3704 /*
3705 * Check for the ":".
3706 */
3707 if ((*arg)[0] != ':')
3708 {
3709 EMSG(_("E109: Missing ':' after '?'"));
3710 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 return FAIL;
3713 }
3714
3715 /*
3716 * Get the third variable.
3717 */
3718 *arg = skipwhite(*arg + 1);
3719 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3720 {
3721 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003722 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 return FAIL;
3724 }
3725 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003726 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
3728
3729 return OK;
3730}
3731
3732/*
3733 * Handle first level expression:
3734 * expr2 || expr2 || expr2 logical OR
3735 *
3736 * "arg" must point to the first non-white of the expression.
3737 * "arg" is advanced to the next non-white after the recognized expression.
3738 *
3739 * Return OK or FAIL.
3740 */
3741 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 int evaluate;
3746{
Bram Moolenaar33570922005-01-25 22:26:29 +00003747 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 long result;
3749 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003750 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
3752 /*
3753 * Get the first variable.
3754 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 return FAIL;
3757
3758 /*
3759 * Repeat until there is no following "||".
3760 */
3761 first = TRUE;
3762 result = FALSE;
3763 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3764 {
3765 if (evaluate && first)
3766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003767 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003770 if (error)
3771 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 first = FALSE;
3773 }
3774
3775 /*
3776 * Get the second variable.
3777 */
3778 *arg = skipwhite(*arg + 2);
3779 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3780 return FAIL;
3781
3782 /*
3783 * Compute the result.
3784 */
3785 if (evaluate && !result)
3786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003787 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003789 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003790 if (error)
3791 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 if (evaluate)
3794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003795 rettv->v_type = VAR_NUMBER;
3796 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798 }
3799
3800 return OK;
3801}
3802
3803/*
3804 * Handle second level expression:
3805 * expr3 && expr3 && expr3 logical AND
3806 *
3807 * "arg" must point to the first non-white of the expression.
3808 * "arg" is advanced to the next non-white after the recognized expression.
3809 *
3810 * Return OK or FAIL.
3811 */
3812 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 int evaluate;
3817{
Bram Moolenaar33570922005-01-25 22:26:29 +00003818 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 long result;
3820 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003821 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823 /*
3824 * Get the first variable.
3825 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 return FAIL;
3828
3829 /*
3830 * Repeat until there is no following "&&".
3831 */
3832 first = TRUE;
3833 result = TRUE;
3834 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3835 {
3836 if (evaluate && first)
3837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003838 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003840 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003841 if (error)
3842 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 first = FALSE;
3844 }
3845
3846 /*
3847 * Get the second variable.
3848 */
3849 *arg = skipwhite(*arg + 2);
3850 if (eval4(arg, &var2, evaluate && result) == FAIL)
3851 return FAIL;
3852
3853 /*
3854 * Compute the result.
3855 */
3856 if (evaluate && result)
3857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003858 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003860 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003861 if (error)
3862 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864 if (evaluate)
3865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003866 rettv->v_type = VAR_NUMBER;
3867 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
3869 }
3870
3871 return OK;
3872}
3873
3874/*
3875 * Handle third level expression:
3876 * var1 == var2
3877 * var1 =~ var2
3878 * var1 != var2
3879 * var1 !~ var2
3880 * var1 > var2
3881 * var1 >= var2
3882 * var1 < var2
3883 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003884 * var1 is var2
3885 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 *
3887 * "arg" must point to the first non-white of the expression.
3888 * "arg" is advanced to the next non-white after the recognized expression.
3889 *
3890 * Return OK or FAIL.
3891 */
3892 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003893eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 int evaluate;
3897{
Bram Moolenaar33570922005-01-25 22:26:29 +00003898 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 char_u *p;
3900 int i;
3901 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003902 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 int len = 2;
3904 long n1, n2;
3905 char_u *s1, *s2;
3906 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3907 regmatch_T regmatch;
3908 int ic;
3909 char_u *save_cpo;
3910
3911 /*
3912 * Get the first variable.
3913 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003914 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 return FAIL;
3916
3917 p = *arg;
3918 switch (p[0])
3919 {
3920 case '=': if (p[1] == '=')
3921 type = TYPE_EQUAL;
3922 else if (p[1] == '~')
3923 type = TYPE_MATCH;
3924 break;
3925 case '!': if (p[1] == '=')
3926 type = TYPE_NEQUAL;
3927 else if (p[1] == '~')
3928 type = TYPE_NOMATCH;
3929 break;
3930 case '>': if (p[1] != '=')
3931 {
3932 type = TYPE_GREATER;
3933 len = 1;
3934 }
3935 else
3936 type = TYPE_GEQUAL;
3937 break;
3938 case '<': if (p[1] != '=')
3939 {
3940 type = TYPE_SMALLER;
3941 len = 1;
3942 }
3943 else
3944 type = TYPE_SEQUAL;
3945 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003946 case 'i': if (p[1] == 's')
3947 {
3948 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3949 len = 5;
3950 if (!vim_isIDc(p[len]))
3951 {
3952 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3953 type_is = TRUE;
3954 }
3955 }
3956 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
3958
3959 /*
3960 * If there is a comparitive operator, use it.
3961 */
3962 if (type != TYPE_UNKNOWN)
3963 {
3964 /* extra question mark appended: ignore case */
3965 if (p[len] == '?')
3966 {
3967 ic = TRUE;
3968 ++len;
3969 }
3970 /* extra '#' appended: match case */
3971 else if (p[len] == '#')
3972 {
3973 ic = FALSE;
3974 ++len;
3975 }
3976 /* nothing appened: use 'ignorecase' */
3977 else
3978 ic = p_ic;
3979
3980 /*
3981 * Get the second variable.
3982 */
3983 *arg = skipwhite(p + len);
3984 if (eval5(arg, &var2, evaluate) == FAIL)
3985 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003986 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 return FAIL;
3988 }
3989
3990 if (evaluate)
3991 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003992 if (type_is && rettv->v_type != var2.v_type)
3993 {
3994 /* For "is" a different type always means FALSE, for "notis"
3995 * it means TRUE. */
3996 n1 = (type == TYPE_NEQUAL);
3997 }
3998 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3999 {
4000 if (type_is)
4001 {
4002 n1 = (rettv->v_type == var2.v_type
4003 && rettv->vval.v_list == var2.vval.v_list);
4004 if (type == TYPE_NEQUAL)
4005 n1 = !n1;
4006 }
4007 else if (rettv->v_type != var2.v_type
4008 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4009 {
4010 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004011 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004012 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004013 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004014 clear_tv(rettv);
4015 clear_tv(&var2);
4016 return FAIL;
4017 }
4018 else
4019 {
4020 /* Compare two Lists for being equal or unequal. */
4021 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4022 if (type == TYPE_NEQUAL)
4023 n1 = !n1;
4024 }
4025 }
4026
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004027 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4028 {
4029 if (type_is)
4030 {
4031 n1 = (rettv->v_type == var2.v_type
4032 && rettv->vval.v_dict == var2.vval.v_dict);
4033 if (type == TYPE_NEQUAL)
4034 n1 = !n1;
4035 }
4036 else if (rettv->v_type != var2.v_type
4037 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4038 {
4039 if (rettv->v_type != var2.v_type)
4040 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4041 else
4042 EMSG(_("E736: Invalid operation for Dictionary"));
4043 clear_tv(rettv);
4044 clear_tv(&var2);
4045 return FAIL;
4046 }
4047 else
4048 {
4049 /* Compare two Dictionaries for being equal or unequal. */
4050 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4051 if (type == TYPE_NEQUAL)
4052 n1 = !n1;
4053 }
4054 }
4055
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004056 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4057 {
4058 if (rettv->v_type != var2.v_type
4059 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4060 {
4061 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004062 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004063 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004064 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004065 clear_tv(rettv);
4066 clear_tv(&var2);
4067 return FAIL;
4068 }
4069 else
4070 {
4071 /* Compare two Funcrefs for being equal or unequal. */
4072 if (rettv->vval.v_string == NULL
4073 || var2.vval.v_string == NULL)
4074 n1 = FALSE;
4075 else
4076 n1 = STRCMP(rettv->vval.v_string,
4077 var2.vval.v_string) == 0;
4078 if (type == TYPE_NEQUAL)
4079 n1 = !n1;
4080 }
4081 }
4082
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 /*
4084 * If one of the two variables is a number, compare as a number.
4085 * When using "=~" or "!~", always compare as string.
4086 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004087 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 n1 = get_tv_number(rettv);
4091 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 switch (type)
4093 {
4094 case TYPE_EQUAL: n1 = (n1 == n2); break;
4095 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4096 case TYPE_GREATER: n1 = (n1 > n2); break;
4097 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4098 case TYPE_SMALLER: n1 = (n1 < n2); break;
4099 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4100 case TYPE_UNKNOWN:
4101 case TYPE_MATCH:
4102 case TYPE_NOMATCH: break; /* avoid gcc warning */
4103 }
4104 }
4105 else
4106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 s1 = get_tv_string_buf(rettv, buf1);
4108 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4110 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4111 else
4112 i = 0;
4113 n1 = FALSE;
4114 switch (type)
4115 {
4116 case TYPE_EQUAL: n1 = (i == 0); break;
4117 case TYPE_NEQUAL: n1 = (i != 0); break;
4118 case TYPE_GREATER: n1 = (i > 0); break;
4119 case TYPE_GEQUAL: n1 = (i >= 0); break;
4120 case TYPE_SMALLER: n1 = (i < 0); break;
4121 case TYPE_SEQUAL: n1 = (i <= 0); break;
4122
4123 case TYPE_MATCH:
4124 case TYPE_NOMATCH:
4125 /* avoid 'l' flag in 'cpoptions' */
4126 save_cpo = p_cpo;
4127 p_cpo = (char_u *)"";
4128 regmatch.regprog = vim_regcomp(s2,
4129 RE_MAGIC + RE_STRING);
4130 regmatch.rm_ic = ic;
4131 if (regmatch.regprog != NULL)
4132 {
4133 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4134 vim_free(regmatch.regprog);
4135 if (type == TYPE_NOMATCH)
4136 n1 = !n1;
4137 }
4138 p_cpo = save_cpo;
4139 break;
4140
4141 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4142 }
4143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 clear_tv(rettv);
4145 clear_tv(&var2);
4146 rettv->v_type = VAR_NUMBER;
4147 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149 }
4150
4151 return OK;
4152}
4153
4154/*
4155 * Handle fourth level expression:
4156 * + number addition
4157 * - number subtraction
4158 * . string concatenation
4159 *
4160 * "arg" must point to the first non-white of the expression.
4161 * "arg" is advanced to the next non-white after the recognized expression.
4162 *
4163 * Return OK or FAIL.
4164 */
4165 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 int evaluate;
4170{
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T var2;
4172 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 int op;
4174 long n1, n2;
4175 char_u *s1, *s2;
4176 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4177 char_u *p;
4178
4179 /*
4180 * Get the first variable.
4181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return FAIL;
4184
4185 /*
4186 * Repeat computing, until no '+', '-' or '.' is following.
4187 */
4188 for (;;)
4189 {
4190 op = **arg;
4191 if (op != '+' && op != '-' && op != '.')
4192 break;
4193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 if (op != '+' || rettv->v_type != VAR_LIST)
4195 {
4196 /* For "list + ...", an illegal use of the first operand as
4197 * a number cannot be determined before evaluating the 2nd
4198 * operand: if this is also a list, all is ok.
4199 * For "something . ...", "something - ..." or "non-list + ...",
4200 * we know that the first operand needs to be a string or number
4201 * without evaluating the 2nd operand. So check before to avoid
4202 * side effects after an error. */
4203 if (evaluate && get_tv_string_chk(rettv) == NULL)
4204 {
4205 clear_tv(rettv);
4206 return FAIL;
4207 }
4208 }
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 /*
4211 * Get the second variable.
4212 */
4213 *arg = skipwhite(*arg + 1);
4214 if (eval6(arg, &var2, evaluate) == FAIL)
4215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218 }
4219
4220 if (evaluate)
4221 {
4222 /*
4223 * Compute the result.
4224 */
4225 if (op == '.')
4226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4228 s2 = get_tv_string_buf_chk(&var2, buf2);
4229 if (s2 == NULL) /* type error ? */
4230 {
4231 clear_tv(rettv);
4232 clear_tv(&var2);
4233 return FAIL;
4234 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004235 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 clear_tv(rettv);
4237 rettv->v_type = VAR_STRING;
4238 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004240 else if (op == '+' && rettv->v_type == VAR_LIST
4241 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004242 {
4243 /* concatenate Lists */
4244 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4245 &var3) == FAIL)
4246 {
4247 clear_tv(rettv);
4248 clear_tv(&var2);
4249 return FAIL;
4250 }
4251 clear_tv(rettv);
4252 *rettv = var3;
4253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 else
4255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 int error = FALSE;
4257
4258 n1 = get_tv_number_chk(rettv, &error);
4259 if (error)
4260 {
4261 /* This can only happen for "list + non-list".
4262 * For "non-list + ..." or "something - ...", we returned
4263 * before evaluating the 2nd operand. */
4264 clear_tv(rettv);
4265 return FAIL;
4266 }
4267 n2 = get_tv_number_chk(&var2, &error);
4268 if (error)
4269 {
4270 clear_tv(rettv);
4271 clear_tv(&var2);
4272 return FAIL;
4273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (op == '+')
4276 n1 = n1 + n2;
4277 else
4278 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 rettv->v_type = VAR_NUMBER;
4280 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 }
4285 return OK;
4286}
4287
4288/*
4289 * Handle fifth level expression:
4290 * * number multiplication
4291 * / number division
4292 * % number modulo
4293 *
4294 * "arg" must point to the first non-white of the expression.
4295 * "arg" is advanced to the next non-white after the recognized expression.
4296 *
4297 * Return OK or FAIL.
4298 */
4299 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int evaluate;
4304{
Bram Moolenaar33570922005-01-25 22:26:29 +00004305 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 int op;
4307 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 /*
4311 * Get the first variable.
4312 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 return FAIL;
4315
4316 /*
4317 * Repeat computing, until no '*', '/' or '%' is following.
4318 */
4319 for (;;)
4320 {
4321 op = **arg;
4322 if (op != '*' && op != '/' && op != '%')
4323 break;
4324
4325 if (evaluate)
4326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (error)
4330 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 else
4333 n1 = 0;
4334
4335 /*
4336 * Get the second variable.
4337 */
4338 *arg = skipwhite(*arg + 1);
4339 if (eval7(arg, &var2, evaluate) == FAIL)
4340 return FAIL;
4341
4342 if (evaluate)
4343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (error)
4347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
4349 /*
4350 * Compute the result.
4351 */
4352 if (op == '*')
4353 n1 = n1 * n2;
4354 else if (op == '/')
4355 {
4356 if (n2 == 0) /* give an error message? */
4357 n1 = 0x7fffffffL;
4358 else
4359 n1 = n1 / n2;
4360 }
4361 else
4362 {
4363 if (n2 == 0) /* give an error message? */
4364 n1 = 0;
4365 else
4366 n1 = n1 % n2;
4367 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 rettv->v_type = VAR_NUMBER;
4369 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 }
4372
4373 return OK;
4374}
4375
4376/*
4377 * Handle sixth level expression:
4378 * number number constant
4379 * "string" string contstant
4380 * 'string' literal string contstant
4381 * &option-name option value
4382 * @r register contents
4383 * identifier variable value
4384 * function() function call
4385 * $VAR environment variable
4386 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004387 * [expr, expr] List
4388 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 *
4390 * Also handle:
4391 * ! in front logical NOT
4392 * - in front unary minus
4393 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004394 * trailing [] subscript in String or List
4395 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 *
4397 * "arg" must point to the first non-white of the expression.
4398 * "arg" is advanced to the next non-white after the recognized expression.
4399 *
4400 * Return OK or FAIL.
4401 */
4402 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 int evaluate;
4407{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 long n;
4409 int len;
4410 char_u *s;
4411 int val;
4412 char_u *start_leader, *end_leader;
4413 int ret = OK;
4414 char_u *alias;
4415
4416 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004417 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004418 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 /*
4423 * Skip '!' and '-' characters. They are handled later.
4424 */
4425 start_leader = *arg;
4426 while (**arg == '!' || **arg == '-' || **arg == '+')
4427 *arg = skipwhite(*arg + 1);
4428 end_leader = *arg;
4429
4430 switch (**arg)
4431 {
4432 /*
4433 * Number constant.
4434 */
4435 case '0':
4436 case '1':
4437 case '2':
4438 case '3':
4439 case '4':
4440 case '5':
4441 case '6':
4442 case '7':
4443 case '8':
4444 case '9':
4445 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4446 *arg += len;
4447 if (evaluate)
4448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 rettv->v_type = VAR_NUMBER;
4450 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452 break;
4453
4454 /*
4455 * String constant: "string".
4456 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 break;
4459
4460 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004463 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 break;
4465
4466 /*
4467 * List: [expr, expr]
4468 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 break;
4471
4472 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004473 * Dictionary: {key: val, key: val}
4474 */
4475 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4476 break;
4477
4478 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004479 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004481 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 break;
4483
4484 /*
4485 * Environment variable: $VAR.
4486 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 break;
4489
4490 /*
4491 * Register contents: @r.
4492 */
4493 case '@': ++*arg;
4494 if (evaluate)
4495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004497 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499 if (**arg != NUL)
4500 ++*arg;
4501 break;
4502
4503 /*
4504 * nested expression: (expression).
4505 */
4506 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004507 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (**arg == ')')
4509 ++*arg;
4510 else if (ret == OK)
4511 {
4512 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004513 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 ret = FAIL;
4515 }
4516 break;
4517
Bram Moolenaar8c711452005-01-14 21:53:12 +00004518 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 break;
4520 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521
4522 if (ret == NOTDONE)
4523 {
4524 /*
4525 * Must be a variable or function name.
4526 * Can also be a curly-braces kind of name: {expr}.
4527 */
4528 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004529 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530 if (alias != NULL)
4531 s = alias;
4532
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004533 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534 ret = FAIL;
4535 else
4536 {
4537 if (**arg == '(') /* recursive! */
4538 {
4539 /* If "s" is the name of a variable of type VAR_FUNC
4540 * use its contents. */
4541 s = deref_func_name(s, &len);
4542
4543 /* Invoke the function. */
4544 ret = get_func_tv(s, len, rettv, arg,
4545 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004546 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 /* Stop the expression evaluation when immediately
4548 * aborting on error, or when an interrupt occurred or
4549 * an exception was thrown but not caught. */
4550 if (aborting())
4551 {
4552 if (ret == OK)
4553 clear_tv(rettv);
4554 ret = FAIL;
4555 }
4556 }
4557 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004558 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004559 else
4560 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 }
4562
4563 if (alias != NULL)
4564 vim_free(alias);
4565 }
4566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 *arg = skipwhite(*arg);
4568
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004569 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4570 * expr(expr). */
4571 if (ret == OK)
4572 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
4574 /*
4575 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4576 */
4577 if (ret == OK && evaluate && end_leader > start_leader)
4578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004579 int error = FALSE;
4580
4581 val = get_tv_number_chk(rettv, &error);
4582 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004584 clear_tv(rettv);
4585 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004587 else
4588 {
4589 while (end_leader > start_leader)
4590 {
4591 --end_leader;
4592 if (*end_leader == '!')
4593 val = !val;
4594 else if (*end_leader == '-')
4595 val = -val;
4596 }
4597 clear_tv(rettv);
4598 rettv->v_type = VAR_NUMBER;
4599 rettv->vval.v_number = val;
4600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602
4603 return ret;
4604}
4605
4606/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004607 * Evaluate an "[expr]" or "[expr:expr]" index.
4608 * "*arg" points to the '['.
4609 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4610 */
4611 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004612eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004614 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004616 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617{
4618 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004619 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 long len = -1;
4622 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004627 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004628 if (verbose)
4629 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 return FAIL;
4631 }
4632
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635 /*
4636 * dict.name
4637 */
4638 key = *arg + 1;
4639 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4640 ;
4641 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004644 }
4645 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004647 /*
4648 * something[idx]
4649 *
4650 * Get the (first) variable from inside the [].
4651 */
4652 *arg = skipwhite(*arg + 1);
4653 if (**arg == ':')
4654 empty1 = TRUE;
4655 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4656 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004657 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4658 {
4659 /* not a number or string */
4660 clear_tv(&var1);
4661 return FAIL;
4662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663
4664 /*
4665 * Get the second variable from inside the [:].
4666 */
4667 if (**arg == ':')
4668 {
4669 range = TRUE;
4670 *arg = skipwhite(*arg + 1);
4671 if (**arg == ']')
4672 empty2 = TRUE;
4673 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004675 if (!empty1)
4676 clear_tv(&var1);
4677 return FAIL;
4678 }
4679 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4680 {
4681 /* not a number or string */
4682 if (!empty1)
4683 clear_tv(&var1);
4684 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 return FAIL;
4686 }
4687 }
4688
4689 /* Check for the ']'. */
4690 if (**arg != ']')
4691 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004692 if (verbose)
4693 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 clear_tv(&var1);
4695 if (range)
4696 clear_tv(&var2);
4697 return FAIL;
4698 }
4699 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700 }
4701
4702 if (evaluate)
4703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 n1 = 0;
4705 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 n1 = get_tv_number(&var1);
4708 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 }
4710 if (range)
4711 {
4712 if (empty2)
4713 n2 = -1;
4714 else
4715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004716 n2 = get_tv_number(&var2);
4717 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 }
4719 }
4720
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004721 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 {
4723 case VAR_NUMBER:
4724 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004725 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 len = (long)STRLEN(s);
4727 if (range)
4728 {
4729 /* The resulting variable is a substring. If the indexes
4730 * are out of range the result is empty. */
4731 if (n1 < 0)
4732 {
4733 n1 = len + n1;
4734 if (n1 < 0)
4735 n1 = 0;
4736 }
4737 if (n2 < 0)
4738 n2 = len + n2;
4739 else if (n2 >= len)
4740 n2 = len;
4741 if (n1 >= len || n2 < 0 || n1 > n2)
4742 s = NULL;
4743 else
4744 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4745 }
4746 else
4747 {
4748 /* The resulting variable is a string of a single
4749 * character. If the index is too big or negative the
4750 * result is empty. */
4751 if (n1 >= len || n1 < 0)
4752 s = NULL;
4753 else
4754 s = vim_strnsave(s + n1, 1);
4755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
4757 rettv->v_type = VAR_STRING;
4758 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 break;
4760
4761 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763 if (n1 < 0)
4764 n1 = len + n1;
4765 if (!empty1 && (n1 < 0 || n1 >= len))
4766 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767 if (verbose)
4768 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 return FAIL;
4770 }
4771 if (range)
4772 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004773 list_T *l;
4774 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775
4776 if (n2 < 0)
4777 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004778 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004780 if (verbose)
4781 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 return FAIL;
4783 }
4784 l = list_alloc();
4785 if (l == NULL)
4786 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788 n1 <= n2; ++n1)
4789 {
4790 if (list_append_tv(l, &item->li_tv) == FAIL)
4791 {
4792 list_free(l);
4793 return FAIL;
4794 }
4795 item = item->li_next;
4796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 clear_tv(rettv);
4798 rettv->v_type = VAR_LIST;
4799 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004800 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 }
4802 else
4803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004804 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004806 clear_tv(rettv);
4807 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 }
4809 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810
4811 case VAR_DICT:
4812 if (range)
4813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004814 if (verbose)
4815 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 if (len == -1)
4817 clear_tv(&var1);
4818 return FAIL;
4819 }
4820 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004821 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822
4823 if (len == -1)
4824 {
4825 key = get_tv_string(&var1);
4826 if (*key == NUL)
4827 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004828 if (verbose)
4829 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004830 clear_tv(&var1);
4831 return FAIL;
4832 }
4833 }
4834
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004835 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004836
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004837 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004838 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004839 if (len == -1)
4840 clear_tv(&var1);
4841 if (item == NULL)
4842 return FAIL;
4843
4844 copy_tv(&item->di_tv, &var1);
4845 clear_tv(rettv);
4846 *rettv = var1;
4847 }
4848 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849 }
4850 }
4851
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852 return OK;
4853}
4854
4855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 * Get an option value.
4857 * "arg" points to the '&' or '+' before the option name.
4858 * "arg" is advanced to character after the option name.
4859 * Return OK or FAIL.
4860 */
4861 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004864 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 int evaluate;
4866{
4867 char_u *option_end;
4868 long numval;
4869 char_u *stringval;
4870 int opt_type;
4871 int c;
4872 int working = (**arg == '+'); /* has("+option") */
4873 int ret = OK;
4874 int opt_flags;
4875
4876 /*
4877 * Isolate the option name and find its value.
4878 */
4879 option_end = find_option_end(arg, &opt_flags);
4880 if (option_end == NULL)
4881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 EMSG2(_("E112: Option name missing: %s"), *arg);
4884 return FAIL;
4885 }
4886
4887 if (!evaluate)
4888 {
4889 *arg = option_end;
4890 return OK;
4891 }
4892
4893 c = *option_end;
4894 *option_end = NUL;
4895 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897
4898 if (opt_type == -3) /* invalid name */
4899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 EMSG2(_("E113: Unknown option: %s"), *arg);
4902 ret = FAIL;
4903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
4906 if (opt_type == -2) /* hidden string option */
4907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004908 rettv->v_type = VAR_STRING;
4909 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 else if (opt_type == -1) /* hidden number option */
4912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 rettv->v_type = VAR_NUMBER;
4914 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 else if (opt_type == 1) /* number option */
4917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 rettv->v_type = VAR_NUMBER;
4919 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
4921 else /* string option */
4922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 rettv->v_type = VAR_STRING;
4924 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 }
4927 else if (working && (opt_type == -2 || opt_type == -1))
4928 ret = FAIL;
4929
4930 *option_end = c; /* put back for error messages */
4931 *arg = option_end;
4932
4933 return ret;
4934}
4935
4936/*
4937 * Allocate a variable for a string constant.
4938 * Return OK or FAIL.
4939 */
4940 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004941get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 int evaluate;
4945{
4946 char_u *p;
4947 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 int extra = 0;
4949
4950 /*
4951 * Find the end of the string, skipping backslashed characters.
4952 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004953 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
4955 if (*p == '\\' && p[1] != NUL)
4956 {
4957 ++p;
4958 /* A "\<x>" form occupies at least 4 characters, and produces up
4959 * to 6 characters: reserve space for 2 extra */
4960 if (*p == '<')
4961 extra += 2;
4962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964
4965 if (*p != '"')
4966 {
4967 EMSG2(_("E114: Missing quote: %s"), *arg);
4968 return FAIL;
4969 }
4970
4971 /* If only parsing, set *arg and return here */
4972 if (!evaluate)
4973 {
4974 *arg = p + 1;
4975 return OK;
4976 }
4977
4978 /*
4979 * Copy the string into allocated memory, handling backslashed
4980 * characters.
4981 */
4982 name = alloc((unsigned)(p - *arg + extra));
4983 if (name == NULL)
4984 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004985 rettv->v_type = VAR_STRING;
4986 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 if (*p == '\\')
4991 {
4992 switch (*++p)
4993 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 case 'b': *name++ = BS; ++p; break;
4995 case 'e': *name++ = ESC; ++p; break;
4996 case 'f': *name++ = FF; ++p; break;
4997 case 'n': *name++ = NL; ++p; break;
4998 case 'r': *name++ = CAR; ++p; break;
4999 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
5001 case 'X': /* hex: "\x1", "\x12" */
5002 case 'x':
5003 case 'u': /* Unicode: "\u0023" */
5004 case 'U':
5005 if (vim_isxdigit(p[1]))
5006 {
5007 int n, nr;
5008 int c = toupper(*p);
5009
5010 if (c == 'X')
5011 n = 2;
5012 else
5013 n = 4;
5014 nr = 0;
5015 while (--n >= 0 && vim_isxdigit(p[1]))
5016 {
5017 ++p;
5018 nr = (nr << 4) + hex2nr(*p);
5019 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021#ifdef FEAT_MBYTE
5022 /* For "\u" store the number according to
5023 * 'encoding'. */
5024 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 else
5027#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 break;
5031
5032 /* octal: "\1", "\12", "\123" */
5033 case '0':
5034 case '1':
5035 case '2':
5036 case '3':
5037 case '4':
5038 case '5':
5039 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 case '7': *name = *p++ - '0';
5041 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 *name = (*name << 3) + *p++ - '0';
5044 if (*p >= '0' && *p <= '7')
5045 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 break;
5049
5050 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (extra != 0)
5053 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 break;
5056 }
5057 /* FALLTHROUGH */
5058
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061 }
5062 }
5063 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 *arg = p + 1;
5069
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 return OK;
5071}
5072
5073/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 * Return OK or FAIL.
5076 */
5077 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 int evaluate;
5082{
5083 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005084 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005085 int reduce = 0;
5086
5087 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 */
5090 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5091 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005095 break;
5096 ++reduce;
5097 ++p;
5098 }
5099 }
5100
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 return FAIL;
5105 }
5106
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108 if (!evaluate)
5109 {
5110 *arg = p + 1;
5111 return OK;
5112 }
5113
5114 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005116 */
5117 str = alloc((unsigned)((p - *arg) - reduce));
5118 if (str == NULL)
5119 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 rettv->v_type = VAR_STRING;
5121 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005124 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005126 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005128 break;
5129 ++p;
5130 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005132 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005134 *arg = p + 1;
5135
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136 return OK;
5137}
5138
5139/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005140 * Allocate a variable for a List and fill it from "*arg".
5141 * Return OK or FAIL.
5142 */
5143 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005145 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005146 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 int evaluate;
5148{
Bram Moolenaar33570922005-01-25 22:26:29 +00005149 list_T *l = NULL;
5150 typval_T tv;
5151 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152
5153 if (evaluate)
5154 {
5155 l = list_alloc();
5156 if (l == NULL)
5157 return FAIL;
5158 }
5159
5160 *arg = skipwhite(*arg + 1);
5161 while (**arg != ']' && **arg != NUL)
5162 {
5163 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5164 goto failret;
5165 if (evaluate)
5166 {
5167 item = listitem_alloc();
5168 if (item != NULL)
5169 {
5170 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005171 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 list_append(l, item);
5173 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005174 else
5175 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 }
5177
5178 if (**arg == ']')
5179 break;
5180 if (**arg != ',')
5181 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 goto failret;
5184 }
5185 *arg = skipwhite(*arg + 1);
5186 }
5187
5188 if (**arg != ']')
5189 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005190 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191failret:
5192 if (evaluate)
5193 list_free(l);
5194 return FAIL;
5195 }
5196
5197 *arg = skipwhite(*arg + 1);
5198 if (evaluate)
5199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005200 rettv->v_type = VAR_LIST;
5201 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202 ++l->lv_refcount;
5203 }
5204
5205 return OK;
5206}
5207
5208/*
5209 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005210 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005211 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005212 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213list_alloc()
5214{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005215 list_T *l;
5216
5217 l = (list_T *)alloc_clear(sizeof(list_T));
5218 if (l != NULL)
5219 {
5220 /* Prepend the list to the list of lists for garbage collection. */
5221 if (first_list != NULL)
5222 first_list->lv_used_prev = l;
5223 l->lv_used_prev = NULL;
5224 l->lv_used_next = first_list;
5225 first_list = l;
5226 }
5227 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228}
5229
5230/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005231 * Allocate an empty list for a return value.
5232 * Returns OK or FAIL.
5233 */
5234 static int
5235rettv_list_alloc(rettv)
5236 typval_T *rettv;
5237{
5238 list_T *l = list_alloc();
5239
5240 if (l == NULL)
5241 return FAIL;
5242
5243 rettv->vval.v_list = l;
5244 rettv->v_type = VAR_LIST;
5245 ++l->lv_refcount;
5246 return OK;
5247}
5248
5249/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 * Unreference a list: decrement the reference count and free it when it
5251 * becomes zero.
5252 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005253 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005255 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5258 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259}
5260
5261/*
5262 * Free a list, including all items it points to.
5263 * Ignores the reference count.
5264 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005265 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268{
Bram Moolenaar33570922005-01-25 22:26:29 +00005269 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270
Bram Moolenaard9fba312005-06-26 22:34:35 +00005271 /* Avoid that recursive reference to the list frees us again. */
5272 l->lv_refcount = DEL_REFCOUNT;
5273
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005274 /* Remove the list from the list of lists for garbage collection. */
5275 if (l->lv_used_prev == NULL)
5276 first_list = l->lv_used_next;
5277 else
5278 l->lv_used_prev->lv_used_next = l->lv_used_next;
5279 if (l->lv_used_next != NULL)
5280 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5281
Bram Moolenaard9fba312005-06-26 22:34:35 +00005282 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005284 /* Remove the item before deleting it. */
5285 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 listitem_free(item);
5287 }
5288 vim_free(l);
5289}
5290
5291/*
5292 * Allocate a list item.
5293 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005294 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295listitem_alloc()
5296{
Bram Moolenaar33570922005-01-25 22:26:29 +00005297 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298}
5299
5300/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005301 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 */
5303 static void
5304listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005305 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005307 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 vim_free(item);
5309}
5310
5311/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005312 * Remove a list item from a List and free it. Also clears the value.
5313 */
5314 static void
5315listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 list_T *l;
5317 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005318{
5319 list_remove(l, item, item);
5320 listitem_free(item);
5321}
5322
5323/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 * Get the number of items in a list.
5325 */
5326 static long
5327list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005328 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 if (l == NULL)
5331 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005332 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333}
5334
5335/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005336 * Return TRUE when two lists have exactly the same values.
5337 */
5338 static int
5339list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005340 list_T *l1;
5341 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005342 int ic; /* ignore case for strings */
5343{
Bram Moolenaar33570922005-01-25 22:26:29 +00005344 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005346 if (list_len(l1) != list_len(l2))
5347 return FALSE;
5348
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005349 for (item1 = l1->lv_first, item2 = l2->lv_first;
5350 item1 != NULL && item2 != NULL;
5351 item1 = item1->li_next, item2 = item2->li_next)
5352 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5353 return FALSE;
5354 return item1 == NULL && item2 == NULL;
5355}
5356
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005357#if defined(FEAT_PYTHON) || defined(PROTO)
5358/*
5359 * Return the dictitem that an entry in a hashtable points to.
5360 */
5361 dictitem_T *
5362dict_lookup(hi)
5363 hashitem_T *hi;
5364{
5365 return HI2DI(hi);
5366}
5367#endif
5368
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005369/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005370 * Return TRUE when two dictionaries have exactly the same key/values.
5371 */
5372 static int
5373dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 dict_T *d1;
5375 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005376 int ic; /* ignore case for strings */
5377{
Bram Moolenaar33570922005-01-25 22:26:29 +00005378 hashitem_T *hi;
5379 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005380 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005381
5382 if (dict_len(d1) != dict_len(d2))
5383 return FALSE;
5384
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 todo = d1->dv_hashtab.ht_used;
5386 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005388 if (!HASHITEM_EMPTY(hi))
5389 {
5390 item2 = dict_find(d2, hi->hi_key, -1);
5391 if (item2 == NULL)
5392 return FALSE;
5393 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5394 return FALSE;
5395 --todo;
5396 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005397 }
5398 return TRUE;
5399}
5400
5401/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005402 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005403 * Compares the items just like "==" would compare them, but strings and
5404 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005405 */
5406 static int
5407tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005408 typval_T *tv1;
5409 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005410 int ic; /* ignore case */
5411{
5412 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005413 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005414
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005415 if (tv1->v_type != tv2->v_type)
5416 return FALSE;
5417
5418 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005419 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005420 case VAR_LIST:
5421 /* recursive! */
5422 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5423
5424 case VAR_DICT:
5425 /* recursive! */
5426 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5427
5428 case VAR_FUNC:
5429 return (tv1->vval.v_string != NULL
5430 && tv2->vval.v_string != NULL
5431 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5432
5433 case VAR_NUMBER:
5434 return tv1->vval.v_number == tv2->vval.v_number;
5435
5436 case VAR_STRING:
5437 s1 = get_tv_string_buf(tv1, buf1);
5438 s2 = get_tv_string_buf(tv2, buf2);
5439 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005440 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005441
5442 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005443 return TRUE;
5444}
5445
5446/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 * Locate item with index "n" in list "l" and return it.
5448 * A negative index is counted from the end; -1 is the last item.
5449 * Returns NULL when "n" is out of range.
5450 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005451 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 long n;
5455{
Bram Moolenaar33570922005-01-25 22:26:29 +00005456 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 long idx;
5458
5459 if (l == NULL)
5460 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005461
5462 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005464 n = l->lv_len + n;
5465
5466 /* Check for index out of range. */
5467 if (n < 0 || n >= l->lv_len)
5468 return NULL;
5469
5470 /* When there is a cached index may start search from there. */
5471 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005473 if (n < l->lv_idx / 2)
5474 {
5475 /* closest to the start of the list */
5476 item = l->lv_first;
5477 idx = 0;
5478 }
5479 else if (n > (l->lv_idx + l->lv_len) / 2)
5480 {
5481 /* closest to the end of the list */
5482 item = l->lv_last;
5483 idx = l->lv_len - 1;
5484 }
5485 else
5486 {
5487 /* closest to the cached index */
5488 item = l->lv_idx_item;
5489 idx = l->lv_idx;
5490 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 }
5492 else
5493 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005494 if (n < l->lv_len / 2)
5495 {
5496 /* closest to the start of the list */
5497 item = l->lv_first;
5498 idx = 0;
5499 }
5500 else
5501 {
5502 /* closest to the end of the list */
5503 item = l->lv_last;
5504 idx = l->lv_len - 1;
5505 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005507
5508 while (n > idx)
5509 {
5510 /* search forward */
5511 item = item->li_next;
5512 ++idx;
5513 }
5514 while (n < idx)
5515 {
5516 /* search backward */
5517 item = item->li_prev;
5518 --idx;
5519 }
5520
5521 /* cache the used index */
5522 l->lv_idx = idx;
5523 l->lv_idx_item = item;
5524
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 return item;
5526}
5527
5528/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005529 * Get list item "l[idx]" as a number.
5530 */
5531 static long
5532list_find_nr(l, idx, errorp)
5533 list_T *l;
5534 long idx;
5535 int *errorp; /* set to TRUE when something wrong */
5536{
5537 listitem_T *li;
5538
5539 li = list_find(l, idx);
5540 if (li == NULL)
5541 {
5542 if (errorp != NULL)
5543 *errorp = TRUE;
5544 return -1L;
5545 }
5546 return get_tv_number_chk(&li->li_tv, errorp);
5547}
5548
5549/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005550 * Locate "item" list "l" and return its index.
5551 * Returns -1 when "item" is not in the list.
5552 */
5553 static long
5554list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005555 list_T *l;
5556 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005557{
5558 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005559 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005560
5561 if (l == NULL)
5562 return -1;
5563 idx = 0;
5564 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5565 ++idx;
5566 if (li == NULL)
5567 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005568 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005569}
5570
5571/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 * Append item "item" to the end of list "l".
5573 */
5574 static void
5575list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005576 list_T *l;
5577 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578{
5579 if (l->lv_last == NULL)
5580 {
5581 /* empty list */
5582 l->lv_first = item;
5583 l->lv_last = item;
5584 item->li_prev = NULL;
5585 }
5586 else
5587 {
5588 l->lv_last->li_next = item;
5589 item->li_prev = l->lv_last;
5590 l->lv_last = item;
5591 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005592 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 item->li_next = NULL;
5594}
5595
5596/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005597 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005598 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 */
5600 static int
5601list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005602 list_T *l;
5603 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005605 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606
Bram Moolenaar05159a02005-02-26 23:04:13 +00005607 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005609 copy_tv(tv, &li->li_tv);
5610 list_append(l, li);
5611 return OK;
5612}
5613
5614/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005615 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005616 * Return FAIL when out of memory.
5617 */
5618 int
5619list_append_dict(list, dict)
5620 list_T *list;
5621 dict_T *dict;
5622{
5623 listitem_T *li = listitem_alloc();
5624
5625 if (li == NULL)
5626 return FAIL;
5627 li->li_tv.v_type = VAR_DICT;
5628 li->li_tv.v_lock = 0;
5629 li->li_tv.vval.v_dict = dict;
5630 list_append(list, li);
5631 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 return OK;
5633}
5634
5635/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005636 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005637 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005638 * Returns FAIL when out of memory.
5639 */
5640 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005641list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005642 list_T *l;
5643 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005644 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005645{
5646 listitem_T *li = listitem_alloc();
5647
5648 if (li == NULL)
5649 return FAIL;
5650 list_append(l, li);
5651 li->li_tv.v_type = VAR_STRING;
5652 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005653 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5654 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005655 return FAIL;
5656 return OK;
5657}
5658
5659/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005660 * Append "n" to list "l".
5661 * Returns FAIL when out of memory.
5662 */
5663 static int
5664list_append_number(l, n)
5665 list_T *l;
5666 varnumber_T n;
5667{
5668 listitem_T *li;
5669
5670 li = listitem_alloc();
5671 if (li == NULL)
5672 return FAIL;
5673 li->li_tv.v_type = VAR_NUMBER;
5674 li->li_tv.v_lock = 0;
5675 li->li_tv.vval.v_number = n;
5676 list_append(l, li);
5677 return OK;
5678}
5679
5680/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005681 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005682 * If "item" is NULL append at the end.
5683 * Return FAIL when out of memory.
5684 */
5685 static int
5686list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005687 list_T *l;
5688 typval_T *tv;
5689 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005690{
Bram Moolenaar33570922005-01-25 22:26:29 +00005691 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005692
5693 if (ni == NULL)
5694 return FAIL;
5695 copy_tv(tv, &ni->li_tv);
5696 if (item == NULL)
5697 /* Append new item at end of list. */
5698 list_append(l, ni);
5699 else
5700 {
5701 /* Insert new item before existing item. */
5702 ni->li_prev = item->li_prev;
5703 ni->li_next = item;
5704 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005705 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005706 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005707 ++l->lv_idx;
5708 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005709 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005710 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005711 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005712 l->lv_idx_item = NULL;
5713 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005714 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005715 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005716 }
5717 return OK;
5718}
5719
5720/*
5721 * Extend "l1" with "l2".
5722 * If "bef" is NULL append at the end, otherwise insert before this item.
5723 * Returns FAIL when out of memory.
5724 */
5725 static int
5726list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005727 list_T *l1;
5728 list_T *l2;
5729 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005730{
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005732
5733 for (item = l2->lv_first; item != NULL; item = item->li_next)
5734 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5735 return FAIL;
5736 return OK;
5737}
5738
5739/*
5740 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5741 * Return FAIL when out of memory.
5742 */
5743 static int
5744list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005745 list_T *l1;
5746 list_T *l2;
5747 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005748{
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005750
5751 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005752 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005753 if (l == NULL)
5754 return FAIL;
5755 tv->v_type = VAR_LIST;
5756 tv->vval.v_list = l;
5757
5758 /* append all items from the second list */
5759 return list_extend(l, l2, NULL);
5760}
5761
5762/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005763 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005765 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 * Returns NULL when out of memory.
5767 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005769list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005770 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005772 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773{
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 list_T *copy;
5775 listitem_T *item;
5776 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777
5778 if (orig == NULL)
5779 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780
5781 copy = list_alloc();
5782 if (copy != NULL)
5783 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005784 if (copyID != 0)
5785 {
5786 /* Do this before adding the items, because one of the items may
5787 * refer back to this list. */
5788 orig->lv_copyID = copyID;
5789 orig->lv_copylist = copy;
5790 }
5791 for (item = orig->lv_first; item != NULL && !got_int;
5792 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 {
5794 ni = listitem_alloc();
5795 if (ni == NULL)
5796 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005797 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005798 {
5799 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5800 {
5801 vim_free(ni);
5802 break;
5803 }
5804 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005806 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 list_append(copy, ni);
5808 }
5809 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005810 if (item != NULL)
5811 {
5812 list_unref(copy);
5813 copy = NULL;
5814 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005815 }
5816
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 return copy;
5818}
5819
5820/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005821 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005822 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005824 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005825list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005826 list_T *l;
5827 listitem_T *item;
5828 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005829{
Bram Moolenaar33570922005-01-25 22:26:29 +00005830 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005832 /* notify watchers */
5833 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005835 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005836 list_fix_watch(l, ip);
5837 if (ip == item2)
5838 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840
5841 if (item2->li_next == NULL)
5842 l->lv_last = item->li_prev;
5843 else
5844 item2->li_next->li_prev = item->li_prev;
5845 if (item->li_prev == NULL)
5846 l->lv_first = item2->li_next;
5847 else
5848 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005849 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850}
5851
5852/*
5853 * Return an allocated string with the string representation of a list.
5854 * May return NULL.
5855 */
5856 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005857list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005858 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005859 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860{
5861 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862
5863 if (tv->vval.v_list == NULL)
5864 return NULL;
5865 ga_init2(&ga, (int)sizeof(char), 80);
5866 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005867 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005868 {
5869 vim_free(ga.ga_data);
5870 return NULL;
5871 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872 ga_append(&ga, ']');
5873 ga_append(&ga, NUL);
5874 return (char_u *)ga.ga_data;
5875}
5876
5877/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 * Join list "l" into a string in "*gap", using separator "sep".
5879 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005880 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005882 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005883list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 char_u *sep;
5887 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005888 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889{
5890 int first = TRUE;
5891 char_u *tofree;
5892 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 char_u *s;
5895
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005896 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 {
5898 if (first)
5899 first = FALSE;
5900 else
5901 ga_concat(gap, sep);
5902
5903 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005906 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 if (s != NULL)
5908 ga_concat(gap, s);
5909 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005910 if (s == NULL)
5911 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005913 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914}
5915
5916/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005917 * Garbage collection for lists and dictionaries.
5918 *
5919 * We use reference counts to be able to free most items right away when they
5920 * are no longer used. But for composite items it's possible that it becomes
5921 * unused while the reference count is > 0: When there is a recursive
5922 * reference. Example:
5923 * :let l = [1, 2, 3]
5924 * :let d = {9: l}
5925 * :let l[1] = d
5926 *
5927 * Since this is quite unusual we handle this with garbage collection: every
5928 * once in a while find out which lists and dicts are not referenced from any
5929 * variable.
5930 *
5931 * Here is a good reference text about garbage collection (refers to Python
5932 * but it applies to all reference-counting mechanisms):
5933 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005934 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005935
5936/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005937 * Do garbage collection for lists and dicts.
5938 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005939 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005940 int
5941garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005942{
5943 dict_T *dd;
5944 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005945 int copyID = ++current_copyID;
5946 buf_T *buf;
5947 win_T *wp;
5948 int i;
5949 funccall_T *fc;
5950 int did_free = FALSE;
5951
5952 /*
5953 * 1. Go through all accessible variables and mark all lists and dicts
5954 * with copyID.
5955 */
5956 /* script-local variables */
5957 for (i = 1; i <= ga_scripts.ga_len; ++i)
5958 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5959
5960 /* buffer-local variables */
5961 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5962 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5963
5964 /* window-local variables */
5965 FOR_ALL_WINDOWS(wp)
5966 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5967
5968 /* global variables */
5969 set_ref_in_ht(&globvarht, copyID);
5970
5971 /* function-local variables */
5972 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5973 {
5974 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5975 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5976 }
5977
5978 /*
5979 * 2. Go through the list of dicts and free items without the copyID.
5980 */
5981 for (dd = first_dict; dd != NULL; )
5982 if (dd->dv_copyID != copyID)
5983 {
5984 dict_free(dd);
5985 did_free = TRUE;
5986
5987 /* restart, next dict may also have been freed */
5988 dd = first_dict;
5989 }
5990 else
5991 dd = dd->dv_used_next;
5992
5993 /*
5994 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005995 * But don't free a list that has a watcher (used in a for loop), these
5996 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005997 */
5998 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005999 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006000 {
6001 list_free(ll);
6002 did_free = TRUE;
6003
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006004 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006005 ll = first_list;
6006 }
6007 else
6008 ll = ll->lv_used_next;
6009
6010 return did_free;
6011}
6012
6013/*
6014 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6015 */
6016 static void
6017set_ref_in_ht(ht, copyID)
6018 hashtab_T *ht;
6019 int copyID;
6020{
6021 int todo;
6022 hashitem_T *hi;
6023
6024 todo = ht->ht_used;
6025 for (hi = ht->ht_array; todo > 0; ++hi)
6026 if (!HASHITEM_EMPTY(hi))
6027 {
6028 --todo;
6029 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6030 }
6031}
6032
6033/*
6034 * Mark all lists and dicts referenced through list "l" with "copyID".
6035 */
6036 static void
6037set_ref_in_list(l, copyID)
6038 list_T *l;
6039 int copyID;
6040{
6041 listitem_T *li;
6042
6043 for (li = l->lv_first; li != NULL; li = li->li_next)
6044 set_ref_in_item(&li->li_tv, copyID);
6045}
6046
6047/*
6048 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6049 */
6050 static void
6051set_ref_in_item(tv, copyID)
6052 typval_T *tv;
6053 int copyID;
6054{
6055 dict_T *dd;
6056 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006057
6058 switch (tv->v_type)
6059 {
6060 case VAR_DICT:
6061 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006062 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006063 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064 /* Didn't see this dict yet. */
6065 dd->dv_copyID = copyID;
6066 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006067 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006068 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006069
6070 case VAR_LIST:
6071 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006072 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006073 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006074 /* Didn't see this list yet. */
6075 ll->lv_copyID = copyID;
6076 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006077 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006078 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006079 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006080 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006081}
6082
6083/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006084 * Allocate an empty header for a dictionary.
6085 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006086 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006087dict_alloc()
6088{
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006090
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006092 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006093 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006094 /* Add the list to the hashtable for garbage collection. */
6095 if (first_dict != NULL)
6096 first_dict->dv_used_prev = d;
6097 d->dv_used_next = first_dict;
6098 d->dv_used_prev = NULL;
6099
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006101 d->dv_lock = 0;
6102 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006103 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006104 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006105 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006106}
6107
6108/*
6109 * Unreference a Dictionary: decrement the reference count and free it when it
6110 * becomes zero.
6111 */
6112 static void
6113dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006114 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006115{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006116 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6117 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006118}
6119
6120/*
6121 * Free a Dictionary, including all items it contains.
6122 * Ignores the reference count.
6123 */
6124 static void
6125dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006127{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006128 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006129 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006130 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006131
Bram Moolenaard9fba312005-06-26 22:34:35 +00006132 /* Avoid that recursive reference to the dict frees us again. */
6133 d->dv_refcount = DEL_REFCOUNT;
6134
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006135 /* Remove the dict from the list of dicts for garbage collection. */
6136 if (d->dv_used_prev == NULL)
6137 first_dict = d->dv_used_next;
6138 else
6139 d->dv_used_prev->dv_used_next = d->dv_used_next;
6140 if (d->dv_used_next != NULL)
6141 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6142
6143 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006144 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 todo = d->dv_hashtab.ht_used;
6146 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006147 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006148 if (!HASHITEM_EMPTY(hi))
6149 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006150 /* Remove the item before deleting it, just in case there is
6151 * something recursive causing trouble. */
6152 di = HI2DI(hi);
6153 hash_remove(&d->dv_hashtab, hi);
6154 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 --todo;
6156 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006157 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006159 vim_free(d);
6160}
6161
6162/*
6163 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006164 * The "key" is copied to the new item.
6165 * Note that the value of the item "di_tv" still needs to be initialized!
6166 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006167 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006168 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006169dictitem_alloc(key)
6170 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006171{
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006173
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006175 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006176 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006177 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006178 di->di_flags = 0;
6179 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006180 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006181}
6182
6183/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006184 * Make a copy of a Dictionary item.
6185 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006186 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006187dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006189{
Bram Moolenaar33570922005-01-25 22:26:29 +00006190 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006193 if (di != NULL)
6194 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006195 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006196 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006197 copy_tv(&org->di_tv, &di->di_tv);
6198 }
6199 return di;
6200}
6201
6202/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006203 * Remove item "item" from Dictionary "dict" and free it.
6204 */
6205 static void
6206dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006207 dict_T *dict;
6208 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006209{
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211
Bram Moolenaar33570922005-01-25 22:26:29 +00006212 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006213 if (HASHITEM_EMPTY(hi))
6214 EMSG2(_(e_intern2), "dictitem_remove()");
6215 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006216 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006217 dictitem_free(item);
6218}
6219
6220/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006221 * Free a dict item. Also clears the value.
6222 */
6223 static void
6224dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006226{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006227 clear_tv(&item->di_tv);
6228 vim_free(item);
6229}
6230
6231/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006232 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6233 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006234 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006235 * Returns NULL when out of memory.
6236 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006238dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006241 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006242{
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 dict_T *copy;
6244 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006245 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006246 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006247
6248 if (orig == NULL)
6249 return NULL;
6250
6251 copy = dict_alloc();
6252 if (copy != NULL)
6253 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006254 if (copyID != 0)
6255 {
6256 orig->dv_copyID = copyID;
6257 orig->dv_copydict = copy;
6258 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006260 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006261 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006262 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006263 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006264 --todo;
6265
6266 di = dictitem_alloc(hi->hi_key);
6267 if (di == NULL)
6268 break;
6269 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006270 {
6271 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6272 copyID) == FAIL)
6273 {
6274 vim_free(di);
6275 break;
6276 }
6277 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278 else
6279 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6280 if (dict_add(copy, di) == FAIL)
6281 {
6282 dictitem_free(di);
6283 break;
6284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006285 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006286 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287
Bram Moolenaare9a41262005-01-15 22:18:47 +00006288 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006289 if (todo > 0)
6290 {
6291 dict_unref(copy);
6292 copy = NULL;
6293 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294 }
6295
6296 return copy;
6297}
6298
6299/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006300 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006301 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006302 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006303 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006304dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 dict_T *d;
6306 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006307{
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309}
6310
Bram Moolenaar8c711452005-01-14 21:53:12 +00006311/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006312 * Add a number or string entry to dictionary "d".
6313 * When "str" is NULL use number "nr", otherwise use "str".
6314 * Returns FAIL when out of memory and when key already exists.
6315 */
6316 int
6317dict_add_nr_str(d, key, nr, str)
6318 dict_T *d;
6319 char *key;
6320 long nr;
6321 char_u *str;
6322{
6323 dictitem_T *item;
6324
6325 item = dictitem_alloc((char_u *)key);
6326 if (item == NULL)
6327 return FAIL;
6328 item->di_tv.v_lock = 0;
6329 if (str == NULL)
6330 {
6331 item->di_tv.v_type = VAR_NUMBER;
6332 item->di_tv.vval.v_number = nr;
6333 }
6334 else
6335 {
6336 item->di_tv.v_type = VAR_STRING;
6337 item->di_tv.vval.v_string = vim_strsave(str);
6338 }
6339 if (dict_add(d, item) == FAIL)
6340 {
6341 dictitem_free(item);
6342 return FAIL;
6343 }
6344 return OK;
6345}
6346
6347/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006348 * Get the number of items in a Dictionary.
6349 */
6350 static long
6351dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006353{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006354 if (d == NULL)
6355 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006357}
6358
6359/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006360 * Find item "key[len]" in Dictionary "d".
6361 * If "len" is negative use strlen(key).
6362 * Returns NULL when not found.
6363 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006365dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006367 char_u *key;
6368 int len;
6369{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006370#define AKEYLEN 200
6371 char_u buf[AKEYLEN];
6372 char_u *akey;
6373 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006376 if (len < 0)
6377 akey = key;
6378 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006379 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006380 tofree = akey = vim_strnsave(key, len);
6381 if (akey == NULL)
6382 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006383 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006384 else
6385 {
6386 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006387 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006388 akey = buf;
6389 }
6390
Bram Moolenaar33570922005-01-25 22:26:29 +00006391 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006392 vim_free(tofree);
6393 if (HASHITEM_EMPTY(hi))
6394 return NULL;
6395 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006396}
6397
6398/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006399 * Get a string item from a dictionary.
6400 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006401 * Returns NULL if the entry doesn't exist or out of memory.
6402 */
6403 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006404get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006405 dict_T *d;
6406 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006407 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006408{
6409 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006410 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006411
6412 di = dict_find(d, key, -1);
6413 if (di == NULL)
6414 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006415 s = get_tv_string(&di->di_tv);
6416 if (save && s != NULL)
6417 s = vim_strsave(s);
6418 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006419}
6420
6421/*
6422 * Get a number item from a dictionary.
6423 * Returns 0 if the entry doesn't exist or out of memory.
6424 */
6425 long
6426get_dict_number(d, key)
6427 dict_T *d;
6428 char_u *key;
6429{
6430 dictitem_T *di;
6431
6432 di = dict_find(d, key, -1);
6433 if (di == NULL)
6434 return 0;
6435 return get_tv_number(&di->di_tv);
6436}
6437
6438/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006439 * Return an allocated string with the string representation of a Dictionary.
6440 * May return NULL.
6441 */
6442 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006443dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006445 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006446{
6447 garray_T ga;
6448 int first = TRUE;
6449 char_u *tofree;
6450 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006452 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006453 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006454 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006455
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006456 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006457 return NULL;
6458 ga_init2(&ga, (int)sizeof(char), 80);
6459 ga_append(&ga, '{');
6460
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006462 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006463 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006464 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006465 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006466 --todo;
6467
6468 if (first)
6469 first = FALSE;
6470 else
6471 ga_concat(&ga, (char_u *)", ");
6472
6473 tofree = string_quote(hi->hi_key, FALSE);
6474 if (tofree != NULL)
6475 {
6476 ga_concat(&ga, tofree);
6477 vim_free(tofree);
6478 }
6479 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006480 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006481 if (s != NULL)
6482 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 if (s == NULL)
6485 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006486 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488 if (todo > 0)
6489 {
6490 vim_free(ga.ga_data);
6491 return NULL;
6492 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006493
6494 ga_append(&ga, '}');
6495 ga_append(&ga, NUL);
6496 return (char_u *)ga.ga_data;
6497}
6498
6499/*
6500 * Allocate a variable for a Dictionary and fill it from "*arg".
6501 * Return OK or FAIL. Returns NOTDONE for {expr}.
6502 */
6503 static int
6504get_dict_tv(arg, rettv, evaluate)
6505 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507 int evaluate;
6508{
Bram Moolenaar33570922005-01-25 22:26:29 +00006509 dict_T *d = NULL;
6510 typval_T tvkey;
6511 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006514 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006515 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006516
6517 /*
6518 * First check if it's not a curly-braces thing: {expr}.
6519 * Must do this without evaluating, otherwise a function may be called
6520 * twice. Unfortunately this means we need to call eval1() twice for the
6521 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006522 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006523 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 if (*start != '}')
6525 {
6526 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6527 return FAIL;
6528 if (*start == '}')
6529 return NOTDONE;
6530 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006531
6532 if (evaluate)
6533 {
6534 d = dict_alloc();
6535 if (d == NULL)
6536 return FAIL;
6537 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006538 tvkey.v_type = VAR_UNKNOWN;
6539 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006540
6541 *arg = skipwhite(*arg + 1);
6542 while (**arg != '}' && **arg != NUL)
6543 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006544 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006545 goto failret;
6546 if (**arg != ':')
6547 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006548 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006549 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006550 goto failret;
6551 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006552 key = get_tv_string_buf_chk(&tvkey, buf);
6553 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006555 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6556 if (key != NULL)
6557 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006558 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559 goto failret;
6560 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006561
6562 *arg = skipwhite(*arg + 1);
6563 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6564 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006565 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006566 goto failret;
6567 }
6568 if (evaluate)
6569 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006570 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006571 if (item != NULL)
6572 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006573 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006574 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006575 clear_tv(&tv);
6576 goto failret;
6577 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006578 item = dictitem_alloc(key);
6579 clear_tv(&tvkey);
6580 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006581 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006582 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006583 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006584 if (dict_add(d, item) == FAIL)
6585 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006586 }
6587 }
6588
6589 if (**arg == '}')
6590 break;
6591 if (**arg != ',')
6592 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006593 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594 goto failret;
6595 }
6596 *arg = skipwhite(*arg + 1);
6597 }
6598
6599 if (**arg != '}')
6600 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006601 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006602failret:
6603 if (evaluate)
6604 dict_free(d);
6605 return FAIL;
6606 }
6607
6608 *arg = skipwhite(*arg + 1);
6609 if (evaluate)
6610 {
6611 rettv->v_type = VAR_DICT;
6612 rettv->vval.v_dict = d;
6613 ++d->dv_refcount;
6614 }
6615
6616 return OK;
6617}
6618
Bram Moolenaar8c711452005-01-14 21:53:12 +00006619/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 * Return a string with the string representation of a variable.
6621 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006622 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006623 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006624 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 * May return NULL;
6626 */
6627 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006628echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006629 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006632 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006634 static int recurse = 0;
6635 char_u *r = NULL;
6636
Bram Moolenaar33570922005-01-25 22:26:29 +00006637 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006638 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006639 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006640 *tofree = NULL;
6641 return NULL;
6642 }
6643 ++recurse;
6644
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 switch (tv->v_type)
6646 {
6647 case VAR_FUNC:
6648 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006649 r = tv->vval.v_string;
6650 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006651
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006652 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006653 if (tv->vval.v_list == NULL)
6654 {
6655 *tofree = NULL;
6656 r = NULL;
6657 }
6658 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6659 {
6660 *tofree = NULL;
6661 r = (char_u *)"[...]";
6662 }
6663 else
6664 {
6665 tv->vval.v_list->lv_copyID = copyID;
6666 *tofree = list2string(tv, copyID);
6667 r = *tofree;
6668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006670
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006672 if (tv->vval.v_dict == NULL)
6673 {
6674 *tofree = NULL;
6675 r = NULL;
6676 }
6677 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6678 {
6679 *tofree = NULL;
6680 r = (char_u *)"{...}";
6681 }
6682 else
6683 {
6684 tv->vval.v_dict->dv_copyID = copyID;
6685 *tofree = dict2string(tv, copyID);
6686 r = *tofree;
6687 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006688 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006690 case VAR_STRING:
6691 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006692 *tofree = NULL;
6693 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006696 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006697 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006698 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006700
6701 --recurse;
6702 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703}
6704
6705/*
6706 * Return a string with the string representation of a variable.
6707 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6708 * "numbuf" is used for a number.
6709 * Puts quotes around strings, so that they can be parsed back by eval().
6710 * May return NULL;
6711 */
6712 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006713tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006714 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715 char_u **tofree;
6716 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006717 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006718{
6719 switch (tv->v_type)
6720 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006721 case VAR_FUNC:
6722 *tofree = string_quote(tv->vval.v_string, TRUE);
6723 return *tofree;
6724 case VAR_STRING:
6725 *tofree = string_quote(tv->vval.v_string, FALSE);
6726 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006727 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006728 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006729 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006730 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006732 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006733 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006734 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735}
6736
6737/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006738 * Return string "str" in ' quotes, doubling ' characters.
6739 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006740 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741 */
6742 static char_u *
6743string_quote(str, function)
6744 char_u *str;
6745 int function;
6746{
Bram Moolenaar33570922005-01-25 22:26:29 +00006747 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006748 char_u *p, *r, *s;
6749
Bram Moolenaar33570922005-01-25 22:26:29 +00006750 len = (function ? 13 : 3);
6751 if (str != NULL)
6752 {
6753 len += STRLEN(str);
6754 for (p = str; *p != NUL; mb_ptr_adv(p))
6755 if (*p == '\'')
6756 ++len;
6757 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 s = r = alloc(len);
6759 if (r != NULL)
6760 {
6761 if (function)
6762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006763 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006764 r += 10;
6765 }
6766 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006767 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006768 if (str != NULL)
6769 for (p = str; *p != NUL; )
6770 {
6771 if (*p == '\'')
6772 *r++ = '\'';
6773 MB_COPY_CHAR(p, r);
6774 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006775 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 if (function)
6777 *r++ = ')';
6778 *r++ = NUL;
6779 }
6780 return s;
6781}
6782
6783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 * Get the value of an environment variable.
6785 * "arg" is pointing to the '$'. It is advanced to after the name.
6786 * If the environment variable was not set, silently assume it is empty.
6787 * Always return OK.
6788 */
6789 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006790get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 int evaluate;
6794{
6795 char_u *string = NULL;
6796 int len;
6797 int cc;
6798 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006799 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
6801 ++*arg;
6802 name = *arg;
6803 len = get_env_len(arg);
6804 if (evaluate)
6805 {
6806 if (len != 0)
6807 {
6808 cc = name[len];
6809 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006810 /* first try vim_getenv(), fast for normal environment vars */
6811 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006813 {
6814 if (!mustfree)
6815 string = vim_strsave(string);
6816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 else
6818 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006819 if (mustfree)
6820 vim_free(string);
6821
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 /* next try expanding things like $VIM and ${HOME} */
6823 string = expand_env_save(name - 1);
6824 if (string != NULL && *string == '$')
6825 {
6826 vim_free(string);
6827 string = NULL;
6828 }
6829 }
6830 name[len] = cc;
6831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006832 rettv->v_type = VAR_STRING;
6833 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 }
6835
6836 return OK;
6837}
6838
6839/*
6840 * Array with names and number of arguments of all internal functions
6841 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6842 */
6843static struct fst
6844{
6845 char *f_name; /* function name */
6846 char f_min_argc; /* minimal number of arguments */
6847 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006848 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850} functions[] =
6851{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006852 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {"append", 2, 2, f_append},
6854 {"argc", 0, 0, f_argc},
6855 {"argidx", 0, 0, f_argidx},
6856 {"argv", 1, 1, f_argv},
6857 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006858 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {"bufexists", 1, 1, f_bufexists},
6860 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6861 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6862 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6863 {"buflisted", 1, 1, f_buflisted},
6864 {"bufloaded", 1, 1, f_bufloaded},
6865 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006866 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 {"bufwinnr", 1, 1, f_bufwinnr},
6868 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006869 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {"char2nr", 1, 1, f_char2nr},
6872 {"cindent", 1, 1, f_cindent},
6873 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006874#if defined(FEAT_INS_EXPAND)
6875 {"complete_add", 1, 1, f_complete_add},
6876 {"complete_check", 0, 0, f_complete_check},
6877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006879 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006880 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006882 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006883 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {"delete", 1, 1, f_delete},
6885 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006886 {"diff_filler", 1, 1, f_diff_filler},
6887 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006888 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006890 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {"eventhandler", 0, 0, f_eventhandler},
6892 {"executable", 1, 1, f_executable},
6893 {"exists", 1, 1, f_exists},
6894 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006895 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6897 {"filereadable", 1, 1, f_filereadable},
6898 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006899 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006900 {"finddir", 1, 3, f_finddir},
6901 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {"fnamemodify", 2, 2, f_fnamemodify},
6903 {"foldclosed", 1, 1, f_foldclosed},
6904 {"foldclosedend", 1, 1, f_foldclosedend},
6905 {"foldlevel", 1, 1, f_foldlevel},
6906 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006907 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006909 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006911 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006912 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {"getbufvar", 2, 2, f_getbufvar},
6914 {"getchar", 0, 1, f_getchar},
6915 {"getcharmod", 0, 0, f_getcharmod},
6916 {"getcmdline", 0, 0, f_getcmdline},
6917 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006918 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006920 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006921 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {"getfsize", 1, 1, f_getfsize},
6923 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006924 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006925 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006926 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006927 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006928 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006929 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {"getregtype", 0, 1, f_getregtype},
6931 {"getwinposx", 0, 0, f_getwinposx},
6932 {"getwinposy", 0, 0, f_getwinposy},
6933 {"getwinvar", 2, 2, f_getwinvar},
6934 {"glob", 1, 1, f_glob},
6935 {"globpath", 2, 2, f_globpath},
6936 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {"hasmapto", 1, 2, f_hasmapto},
6939 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6940 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6941 {"histadd", 2, 2, f_histadd},
6942 {"histdel", 1, 2, f_histdel},
6943 {"histget", 1, 2, f_histget},
6944 {"histnr", 1, 1, f_histnr},
6945 {"hlID", 1, 1, f_hlID},
6946 {"hlexists", 1, 1, f_hlexists},
6947 {"hostname", 0, 0, f_hostname},
6948 {"iconv", 3, 3, f_iconv},
6949 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006950 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006951 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006953 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {"inputrestore", 0, 0, f_inputrestore},
6955 {"inputsave", 0, 0, f_inputsave},
6956 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006957 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006959 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006960 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006961 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006962 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006964 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {"libcall", 3, 3, f_libcall},
6966 {"libcallnr", 3, 3, f_libcallnr},
6967 {"line", 1, 1, f_line},
6968 {"line2byte", 1, 1, f_line2byte},
6969 {"lispindent", 1, 1, f_lispindent},
6970 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006971 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 {"maparg", 1, 2, f_maparg},
6973 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006974 {"match", 2, 4, f_match},
6975 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006976 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006977 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006978 {"max", 1, 1, f_max},
6979 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006980#ifdef vim_mkdir
6981 {"mkdir", 1, 3, f_mkdir},
6982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {"mode", 0, 0, f_mode},
6984 {"nextnonblank", 1, 1, f_nextnonblank},
6985 {"nr2char", 1, 1, f_nr2char},
6986 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006987 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006988 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006989 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006990 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {"remote_expr", 2, 3, f_remote_expr},
6992 {"remote_foreground", 1, 1, f_remote_foreground},
6993 {"remote_peek", 1, 2, f_remote_peek},
6994 {"remote_read", 1, 1, f_remote_read},
6995 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006996 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006998 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007000 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007001 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007002 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007003 {"searchpair", 3, 6, f_searchpair},
7004 {"searchpairpos", 3, 6, f_searchpairpos},
7005 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {"server2client", 2, 2, f_server2client},
7007 {"serverlist", 0, 0, f_serverlist},
7008 {"setbufvar", 3, 3, f_setbufvar},
7009 {"setcmdpos", 1, 1, f_setcmdpos},
7010 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007011 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007012 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007013 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {"setreg", 2, 3, f_setreg},
7015 {"setwinvar", 3, 3, f_setwinvar},
7016 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007017 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007018 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007019 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007020 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007021 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022#ifdef HAVE_STRFTIME
7023 {"strftime", 1, 2, f_strftime},
7024#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007025 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007026 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {"strlen", 1, 1, f_strlen},
7028 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007029 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 {"strtrans", 1, 1, f_strtrans},
7031 {"submatch", 1, 1, f_submatch},
7032 {"substitute", 4, 4, f_substitute},
7033 {"synID", 3, 3, f_synID},
7034 {"synIDattr", 2, 3, f_synIDattr},
7035 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007036 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007037 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007038 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007039 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007040 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007041 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007043 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"tolower", 1, 1, f_tolower},
7045 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007046 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007048 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {"virtcol", 1, 1, f_virtcol},
7050 {"visualmode", 0, 1, f_visualmode},
7051 {"winbufnr", 1, 1, f_winbufnr},
7052 {"wincol", 0, 0, f_wincol},
7053 {"winheight", 1, 1, f_winheight},
7054 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007055 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {"winrestcmd", 0, 0, f_winrestcmd},
7057 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007058 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059};
7060
7061#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7062
7063/*
7064 * Function given to ExpandGeneric() to obtain the list of internal
7065 * or user defined function names.
7066 */
7067 char_u *
7068get_function_name(xp, idx)
7069 expand_T *xp;
7070 int idx;
7071{
7072 static int intidx = -1;
7073 char_u *name;
7074
7075 if (idx == 0)
7076 intidx = -1;
7077 if (intidx < 0)
7078 {
7079 name = get_user_func_name(xp, idx);
7080 if (name != NULL)
7081 return name;
7082 }
7083 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7084 {
7085 STRCPY(IObuff, functions[intidx].f_name);
7086 STRCAT(IObuff, "(");
7087 if (functions[intidx].f_max_argc == 0)
7088 STRCAT(IObuff, ")");
7089 return IObuff;
7090 }
7091
7092 return NULL;
7093}
7094
7095/*
7096 * Function given to ExpandGeneric() to obtain the list of internal or
7097 * user defined variable or function names.
7098 */
7099/*ARGSUSED*/
7100 char_u *
7101get_expr_name(xp, idx)
7102 expand_T *xp;
7103 int idx;
7104{
7105 static int intidx = -1;
7106 char_u *name;
7107
7108 if (idx == 0)
7109 intidx = -1;
7110 if (intidx < 0)
7111 {
7112 name = get_function_name(xp, idx);
7113 if (name != NULL)
7114 return name;
7115 }
7116 return get_user_var_name(xp, ++intidx);
7117}
7118
7119#endif /* FEAT_CMDL_COMPL */
7120
7121/*
7122 * Find internal function in table above.
7123 * Return index, or -1 if not found
7124 */
7125 static int
7126find_internal_func(name)
7127 char_u *name; /* name of the function */
7128{
7129 int first = 0;
7130 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7131 int cmp;
7132 int x;
7133
7134 /*
7135 * Find the function name in the table. Binary search.
7136 */
7137 while (first <= last)
7138 {
7139 x = first + ((unsigned)(last - first) >> 1);
7140 cmp = STRCMP(name, functions[x].f_name);
7141 if (cmp < 0)
7142 last = x - 1;
7143 else if (cmp > 0)
7144 first = x + 1;
7145 else
7146 return x;
7147 }
7148 return -1;
7149}
7150
7151/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007152 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7153 * name it contains, otherwise return "name".
7154 */
7155 static char_u *
7156deref_func_name(name, lenp)
7157 char_u *name;
7158 int *lenp;
7159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007161 int cc;
7162
7163 cc = name[*lenp];
7164 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007165 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007166 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007167 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007170 {
7171 *lenp = 0;
7172 return (char_u *)""; /* just in case */
7173 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 *lenp = STRLEN(v->di_tv.vval.v_string);
7175 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007176 }
7177
7178 return name;
7179}
7180
7181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 * Allocate a variable for the result of a function.
7183 * Return OK or FAIL.
7184 */
7185 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7187 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 char_u *name; /* name of the function */
7189 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 char_u **arg; /* argument, pointing to the '(' */
7192 linenr_T firstline; /* first line of range */
7193 linenr_T lastline; /* last line of range */
7194 int *doesrange; /* return: function handled range */
7195 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 char_u *argp;
7199 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 int argcount = 0; /* number of arguments found */
7202
7203 /*
7204 * Get the arguments.
7205 */
7206 argp = *arg;
7207 while (argcount < MAX_FUNC_ARGS)
7208 {
7209 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7210 if (*argp == ')' || *argp == ',' || *argp == NUL)
7211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7213 {
7214 ret = FAIL;
7215 break;
7216 }
7217 ++argcount;
7218 if (*argp != ',')
7219 break;
7220 }
7221 if (*argp == ')')
7222 ++argp;
7223 else
7224 ret = FAIL;
7225
7226 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007227 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007228 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 {
7231 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007232 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007233 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007234 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
7237 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007238 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 *arg = skipwhite(argp);
7241 return ret;
7242}
7243
7244
7245/*
7246 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007247 * Return OK when the function can't be called, FAIL otherwise.
7248 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 */
7250 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007251call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007252 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 char_u *name; /* name of the function */
7254 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 linenr_T firstline; /* first line of range */
7259 linenr_T lastline; /* last line of range */
7260 int *doesrange; /* return: function handled range */
7261 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263{
7264 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265#define ERROR_UNKNOWN 0
7266#define ERROR_TOOMANY 1
7267#define ERROR_TOOFEW 2
7268#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007269#define ERROR_DICT 4
7270#define ERROR_NONE 5
7271#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 int error = ERROR_NONE;
7273 int i;
7274 int llen;
7275 ufunc_T *fp;
7276 int cc;
7277#define FLEN_FIXED 40
7278 char_u fname_buf[FLEN_FIXED + 1];
7279 char_u *fname;
7280
7281 /*
7282 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7283 * Change <SNR>123_name() to K_SNR 123_name().
7284 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7285 */
7286 cc = name[len];
7287 name[len] = NUL;
7288 llen = eval_fname_script(name);
7289 if (llen > 0)
7290 {
7291 fname_buf[0] = K_SPECIAL;
7292 fname_buf[1] = KS_EXTRA;
7293 fname_buf[2] = (int)KE_SNR;
7294 i = 3;
7295 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7296 {
7297 if (current_SID <= 0)
7298 error = ERROR_SCRIPT;
7299 else
7300 {
7301 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7302 i = (int)STRLEN(fname_buf);
7303 }
7304 }
7305 if (i + STRLEN(name + llen) < FLEN_FIXED)
7306 {
7307 STRCPY(fname_buf + i, name + llen);
7308 fname = fname_buf;
7309 }
7310 else
7311 {
7312 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7313 if (fname == NULL)
7314 error = ERROR_OTHER;
7315 else
7316 {
7317 mch_memmove(fname, fname_buf, (size_t)i);
7318 STRCPY(fname + i, name + llen);
7319 }
7320 }
7321 }
7322 else
7323 fname = name;
7324
7325 *doesrange = FALSE;
7326
7327
7328 /* execute the function if no errors detected and executing */
7329 if (evaluate && error == ERROR_NONE)
7330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007331 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 error = ERROR_UNKNOWN;
7333
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007334 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 /*
7337 * User defined function.
7338 */
7339 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007340
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007342 /* Trigger FuncUndefined event, may load the function. */
7343 if (fp == NULL
7344 && apply_autocmds(EVENT_FUNCUNDEFINED,
7345 fname, fname, TRUE, NULL)
7346 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007348 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 fp = find_func(fname);
7350 }
7351#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007352 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007353 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007354 {
7355 /* loaded a package, search for the function again */
7356 fp = find_func(fname);
7357 }
7358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (fp != NULL)
7360 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007361 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007363 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007365 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007367 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 else
7370 {
7371 /*
7372 * Call the user function.
7373 * Save and restore search patterns, script variables and
7374 * redo buffer.
7375 */
7376 save_search_patterns();
7377 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007378 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007379 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007381 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7382 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7383 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007384 /* Function was unreferenced while being used, free it
7385 * now. */
7386 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 restoreRedobuff();
7388 restore_search_patterns();
7389 error = ERROR_NONE;
7390 }
7391 }
7392 }
7393 else
7394 {
7395 /*
7396 * Find the function name in the table, call its implementation.
7397 */
7398 i = find_internal_func(fname);
7399 if (i >= 0)
7400 {
7401 if (argcount < functions[i].f_min_argc)
7402 error = ERROR_TOOFEW;
7403 else if (argcount > functions[i].f_max_argc)
7404 error = ERROR_TOOMANY;
7405 else
7406 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007407 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007408 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 error = ERROR_NONE;
7410 }
7411 }
7412 }
7413 /*
7414 * The function call (or "FuncUndefined" autocommand sequence) might
7415 * have been aborted by an error, an interrupt, or an explicitly thrown
7416 * exception that has not been caught so far. This situation can be
7417 * tested for by calling aborting(). For an error in an internal
7418 * function or for the "E132" error in call_user_func(), however, the
7419 * throw point at which the "force_abort" flag (temporarily reset by
7420 * emsg()) is normally updated has not been reached yet. We need to
7421 * update that flag first to make aborting() reliable.
7422 */
7423 update_force_abort();
7424 }
7425 if (error == ERROR_NONE)
7426 ret = OK;
7427
7428 /*
7429 * Report an error unless the argument evaluation or function call has been
7430 * cancelled due to an aborting error, an interrupt, or an exception.
7431 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 if (!aborting())
7433 {
7434 switch (error)
7435 {
7436 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007437 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 break;
7439 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007440 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441 break;
7442 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007443 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444 name);
7445 break;
7446 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007447 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 name);
7449 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007450 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007451 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007452 name);
7453 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 }
7455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
7457 name[len] = cc;
7458 if (fname != name && fname != fname_buf)
7459 vim_free(fname);
7460
7461 return ret;
7462}
7463
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007464/*
7465 * Give an error message with a function name. Handle <SNR> things.
7466 */
7467 static void
7468emsg_funcname(msg, name)
7469 char *msg;
7470 char_u *name;
7471{
7472 char_u *p;
7473
7474 if (*name == K_SPECIAL)
7475 p = concat_str((char_u *)"<SNR>", name + 3);
7476 else
7477 p = name;
7478 EMSG2(_(msg), p);
7479 if (p != name)
7480 vim_free(p);
7481}
7482
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483/*********************************************
7484 * Implementation of the built-in functions
7485 */
7486
7487/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007488 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 */
7490 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007491f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 typval_T *argvars;
7493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007497 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007498 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007500 if ((l = argvars[0].vval.v_list) != NULL
7501 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7502 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007503 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007504 }
7505 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007506 EMSG(_(e_listreq));
7507}
7508
7509/*
7510 * "append(lnum, string/list)" function
7511 */
7512 static void
7513f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 typval_T *argvars;
7515 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007516{
7517 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007518 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 list_T *l = NULL;
7520 listitem_T *li = NULL;
7521 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007522 long added = 0;
7523
Bram Moolenaar0d660222005-01-07 21:51:51 +00007524 lnum = get_tv_lnum(argvars);
7525 if (lnum >= 0
7526 && lnum <= curbuf->b_ml.ml_line_count
7527 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007528 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007529 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007530 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007531 l = argvars[1].vval.v_list;
7532 if (l == NULL)
7533 return;
7534 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007536 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007537 for (;;)
7538 {
7539 if (l == NULL)
7540 tv = &argvars[1]; /* append a string */
7541 else if (li == NULL)
7542 break; /* end of list */
7543 else
7544 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007545 line = get_tv_string_chk(tv);
7546 if (line == NULL) /* type error */
7547 {
7548 rettv->vval.v_number = 1; /* Failed */
7549 break;
7550 }
7551 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007552 ++added;
7553 if (l == NULL)
7554 break;
7555 li = li->li_next;
7556 }
7557
7558 appended_lines_mark(lnum, added);
7559 if (curwin->w_cursor.lnum > lnum)
7560 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007562 else
7563 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564}
7565
7566/*
7567 * "argc()" function
7568 */
7569/* ARGSUSED */
7570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007571f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 typval_T *argvars;
7573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007575 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576}
7577
7578/*
7579 * "argidx()" function
7580 */
7581/* ARGSUSED */
7582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007583f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 typval_T *argvars;
7585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007587 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588}
7589
7590/*
7591 * "argv(nr)" function
7592 */
7593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 typval_T *argvars;
7596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598 int idx;
7599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007600 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604 rettv->vval.v_string = NULL;
7605 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606}
7607
7608/*
7609 * "browse(save, title, initdir, default)" function
7610 */
7611/* ARGSUSED */
7612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 typval_T *argvars;
7615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
7617#ifdef FEAT_BROWSE
7618 int save;
7619 char_u *title;
7620 char_u *initdir;
7621 char_u *defname;
7622 char_u buf[NUMBUFLEN];
7623 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007624 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007626 save = get_tv_number_chk(&argvars[0], &error);
7627 title = get_tv_string_chk(&argvars[1]);
7628 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7629 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007631 if (error || title == NULL || initdir == NULL || defname == NULL)
7632 rettv->vval.v_string = NULL;
7633 else
7634 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007635 do_browse(save ? BROWSE_SAVE : 0,
7636 title, defname, NULL, initdir, NULL, curbuf);
7637#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007639#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007640 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007641}
7642
7643/*
7644 * "browsedir(title, initdir)" function
7645 */
7646/* ARGSUSED */
7647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007648f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 typval_T *argvars;
7650 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007651{
7652#ifdef FEAT_BROWSE
7653 char_u *title;
7654 char_u *initdir;
7655 char_u buf[NUMBUFLEN];
7656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007657 title = get_tv_string_chk(&argvars[0]);
7658 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007660 if (title == NULL || initdir == NULL)
7661 rettv->vval.v_string = NULL;
7662 else
7663 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007664 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669}
7670
Bram Moolenaar33570922005-01-25 22:26:29 +00007671static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007672
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673/*
7674 * Find a buffer by number or exact name.
7675 */
7676 static buf_T *
7677find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
7680 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007682 if (avar->v_type == VAR_NUMBER)
7683 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007684 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007687 if (buf == NULL)
7688 {
7689 /* No full path name match, try a match with a URL or a "nofile"
7690 * buffer, these don't use the full path. */
7691 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7692 if (buf->b_fname != NULL
7693 && (path_with_url(buf->b_fname)
7694#ifdef FEAT_QUICKFIX
7695 || bt_nofile(buf)
7696#endif
7697 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007699 break;
7700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
7702 return buf;
7703}
7704
7705/*
7706 * "bufexists(expr)" function
7707 */
7708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007709f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007710 typval_T *argvars;
7711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714}
7715
7716/*
7717 * "buflisted(expr)" function
7718 */
7719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 typval_T *argvars;
7722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723{
7724 buf_T *buf;
7725
7726 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007727 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728}
7729
7730/*
7731 * "bufloaded(expr)" function
7732 */
7733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007734f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 typval_T *argvars;
7736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
7738 buf_T *buf;
7739
7740 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007741 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742}
7743
Bram Moolenaar33570922005-01-25 22:26:29 +00007744static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007745
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746/*
7747 * Get buffer by number or pattern.
7748 */
7749 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 int save_magic;
7755 char_u *save_cpo;
7756 buf_T *buf;
7757
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007758 if (tv->v_type == VAR_NUMBER)
7759 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007760 if (tv->v_type != VAR_STRING)
7761 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 if (name == NULL || *name == NUL)
7763 return curbuf;
7764 if (name[0] == '$' && name[1] == NUL)
7765 return lastbuf;
7766
7767 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7768 save_magic = p_magic;
7769 p_magic = TRUE;
7770 save_cpo = p_cpo;
7771 p_cpo = (char_u *)"";
7772
7773 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7774 TRUE, FALSE));
7775
7776 p_magic = save_magic;
7777 p_cpo = save_cpo;
7778
7779 /* If not found, try expanding the name, like done for bufexists(). */
7780 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782
7783 return buf;
7784}
7785
7786/*
7787 * "bufname(expr)" function
7788 */
7789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007790f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007791 typval_T *argvars;
7792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 buf_T *buf;
7795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007796 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007798 buf = get_buf_tv(&argvars[0]);
7799 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007803 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 --emsg_off;
7805}
7806
7807/*
7808 * "bufnr(expr)" function
7809 */
7810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 typval_T *argvars;
7813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
7815 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007816 int error = FALSE;
7817 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007819 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007821 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007822 --emsg_off;
7823
7824 /* If the buffer isn't found and the second argument is not zero create a
7825 * new buffer. */
7826 if (buf == NULL
7827 && argvars[1].v_type != VAR_UNKNOWN
7828 && get_tv_number_chk(&argvars[1], &error) != 0
7829 && !error
7830 && (name = get_tv_string_chk(&argvars[0])) != NULL
7831 && !error)
7832 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7833
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007837 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838}
7839
7840/*
7841 * "bufwinnr(nr)" function
7842 */
7843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 typval_T *argvars;
7846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848#ifdef FEAT_WINDOWS
7849 win_T *wp;
7850 int winnr = 0;
7851#endif
7852 buf_T *buf;
7853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857#ifdef FEAT_WINDOWS
7858 for (wp = firstwin; wp; wp = wp->w_next)
7859 {
7860 ++winnr;
7861 if (wp->w_buffer == buf)
7862 break;
7863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867#endif
7868 --emsg_off;
7869}
7870
7871/*
7872 * "byte2line(byte)" function
7873 */
7874/*ARGSUSED*/
7875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007876f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007877 typval_T *argvars;
7878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882#else
7883 long boff = 0;
7884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007885 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007889 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 (linenr_T)0, &boff);
7891#endif
7892}
7893
7894/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007895 * "byteidx()" function
7896 */
7897/*ARGSUSED*/
7898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007899f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 typval_T *argvars;
7901 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007902{
7903#ifdef FEAT_MBYTE
7904 char_u *t;
7905#endif
7906 char_u *str;
7907 long idx;
7908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007909 str = get_tv_string_chk(&argvars[0]);
7910 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007911 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007912 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007913 return;
7914
7915#ifdef FEAT_MBYTE
7916 t = str;
7917 for ( ; idx > 0; idx--)
7918 {
7919 if (*t == NUL) /* EOL reached */
7920 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007921 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007923 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007924#else
7925 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007926 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007927#endif
7928}
7929
7930/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007931 * "call(func, arglist)" function
7932 */
7933 static void
7934f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007935 typval_T *argvars;
7936 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007937{
7938 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007940 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007942 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007944
7945 rettv->vval.v_number = 0;
7946 if (argvars[1].v_type != VAR_LIST)
7947 {
7948 EMSG(_(e_listreq));
7949 return;
7950 }
7951 if (argvars[1].vval.v_list == NULL)
7952 return;
7953
7954 if (argvars[0].v_type == VAR_FUNC)
7955 func = argvars[0].vval.v_string;
7956 else
7957 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007958 if (*func == NUL)
7959 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007960
Bram Moolenaare9a41262005-01-15 22:18:47 +00007961 if (argvars[2].v_type != VAR_UNKNOWN)
7962 {
7963 if (argvars[2].v_type != VAR_DICT)
7964 {
7965 EMSG(_(e_dictreq));
7966 return;
7967 }
7968 selfdict = argvars[2].vval.v_dict;
7969 }
7970
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007971 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7972 item = item->li_next)
7973 {
7974 if (argc == MAX_FUNC_ARGS)
7975 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007976 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007977 break;
7978 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007979 /* Make a copy of each argument. This is needed to be able to set
7980 * v_lock to VAR_FIXED in the copy without changing the original list.
7981 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007982 copy_tv(&item->li_tv, &argv[argc++]);
7983 }
7984
7985 if (item == NULL)
7986 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7988 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007989
7990 /* Free the arguments. */
7991 while (argc > 0)
7992 clear_tv(&argv[--argc]);
7993}
7994
7995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 * "char2nr(string)" function
7997 */
7998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007999f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008000 typval_T *argvars;
8001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002{
8003#ifdef FEAT_MBYTE
8004 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008005 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 else
8007#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009}
8010
8011/*
8012 * "cindent(lnum)" function
8013 */
8014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008015f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 typval_T *argvars;
8017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019#ifdef FEAT_CINDENT
8020 pos_T pos;
8021 linenr_T lnum;
8022
8023 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8026 {
8027 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 curwin->w_cursor = pos;
8030 }
8031 else
8032#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008033 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034}
8035
8036/*
8037 * "col(string)" function
8038 */
8039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008040f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 typval_T *argvars;
8042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
8044 colnr_T col = 0;
8045 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008046 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008048 fp = var2fpos(&argvars[0], FALSE, &fnum);
8049 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {
8051 if (fp->col == MAXCOL)
8052 {
8053 /* '> can be MAXCOL, get the length of the line then */
8054 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8055 col = STRLEN(ml_get(fp->lnum)) + 1;
8056 else
8057 col = MAXCOL;
8058 }
8059 else
8060 {
8061 col = fp->col + 1;
8062#ifdef FEAT_VIRTUALEDIT
8063 /* col(".") when the cursor is on the NUL at the end of the line
8064 * because of "coladd" can be seen as an extra column. */
8065 if (virtual_active() && fp == &curwin->w_cursor)
8066 {
8067 char_u *p = ml_get_cursor();
8068
8069 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8070 curwin->w_virtcol - curwin->w_cursor.coladd))
8071 {
8072# ifdef FEAT_MBYTE
8073 int l;
8074
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008075 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 col += l;
8077# else
8078 if (*p != NUL && p[1] == NUL)
8079 ++col;
8080# endif
8081 }
8082 }
8083#endif
8084 }
8085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008086 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087}
8088
Bram Moolenaar572cb562005-08-05 21:35:02 +00008089#if defined(FEAT_INS_EXPAND)
8090/*
8091 * "complete_add()" function
8092 */
8093/*ARGSUSED*/
8094 static void
8095f_complete_add(argvars, rettv)
8096 typval_T *argvars;
8097 typval_T *rettv;
8098{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008099 char_u *word;
8100 char_u *extra = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008101 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008102
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008103 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8104 {
8105 word = get_dict_string(argvars[0].vval.v_dict,
8106 (char_u *)"word", FALSE);
8107 extra = get_dict_string(argvars[0].vval.v_dict,
8108 (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008109 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008110 }
8111 else
8112 word = get_tv_string_chk(&argvars[0]);
8113 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008114 rettv->vval.v_number = ins_compl_add(word, -1, icase,
8115 NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008116}
8117
8118/*
8119 * "complete_check()" function
8120 */
8121/*ARGSUSED*/
8122 static void
8123f_complete_check(argvars, rettv)
8124 typval_T *argvars;
8125 typval_T *rettv;
8126{
8127 int saved = RedrawingDisabled;
8128
8129 RedrawingDisabled = 0;
8130 ins_compl_check_keys(0);
8131 rettv->vval.v_number = compl_interrupted;
8132 RedrawingDisabled = saved;
8133}
8134#endif
8135
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136/*
8137 * "confirm(message, buttons[, default [, type]])" function
8138 */
8139/*ARGSUSED*/
8140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 typval_T *argvars;
8143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
8145#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8146 char_u *message;
8147 char_u *buttons = NULL;
8148 char_u buf[NUMBUFLEN];
8149 char_u buf2[NUMBUFLEN];
8150 int def = 1;
8151 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008152 char_u *typestr;
8153 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008155 message = get_tv_string_chk(&argvars[0]);
8156 if (message == NULL)
8157 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008158 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008160 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8161 if (buttons == NULL)
8162 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008163 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008165 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008166 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008168 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8169 if (typestr == NULL)
8170 error = TRUE;
8171 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008173 switch (TOUPPER_ASC(*typestr))
8174 {
8175 case 'E': type = VIM_ERROR; break;
8176 case 'Q': type = VIM_QUESTION; break;
8177 case 'I': type = VIM_INFO; break;
8178 case 'W': type = VIM_WARNING; break;
8179 case 'G': type = VIM_GENERIC; break;
8180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 }
8182 }
8183 }
8184 }
8185
8186 if (buttons == NULL || *buttons == NUL)
8187 buttons = (char_u *)_("&Ok");
8188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008189 if (error)
8190 rettv->vval.v_number = 0;
8191 else
8192 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 def, NULL);
8194#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008195 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196#endif
8197}
8198
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008199/*
8200 * "copy()" function
8201 */
8202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 typval_T *argvars;
8205 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008206{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008207 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008208}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209
8210/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008211 * "count()" function
8212 */
8213 static void
8214f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008215 typval_T *argvars;
8216 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008217{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008218 long n = 0;
8219 int ic = FALSE;
8220
Bram Moolenaare9a41262005-01-15 22:18:47 +00008221 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008222 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008223 listitem_T *li;
8224 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008225 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008226
Bram Moolenaare9a41262005-01-15 22:18:47 +00008227 if ((l = argvars[0].vval.v_list) != NULL)
8228 {
8229 li = l->lv_first;
8230 if (argvars[2].v_type != VAR_UNKNOWN)
8231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008232 int error = FALSE;
8233
8234 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008235 if (argvars[3].v_type != VAR_UNKNOWN)
8236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008237 idx = get_tv_number_chk(&argvars[3], &error);
8238 if (!error)
8239 {
8240 li = list_find(l, idx);
8241 if (li == NULL)
8242 EMSGN(_(e_listidx), idx);
8243 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008245 if (error)
8246 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008247 }
8248
8249 for ( ; li != NULL; li = li->li_next)
8250 if (tv_equal(&li->li_tv, &argvars[1], ic))
8251 ++n;
8252 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008253 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008254 else if (argvars[0].v_type == VAR_DICT)
8255 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 int todo;
8257 dict_T *d;
8258 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008259
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008260 if ((d = argvars[0].vval.v_dict) != NULL)
8261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008262 int error = FALSE;
8263
Bram Moolenaare9a41262005-01-15 22:18:47 +00008264 if (argvars[2].v_type != VAR_UNKNOWN)
8265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008266 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008267 if (argvars[3].v_type != VAR_UNKNOWN)
8268 EMSG(_(e_invarg));
8269 }
8270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008271 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008273 {
8274 if (!HASHITEM_EMPTY(hi))
8275 {
8276 --todo;
8277 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8278 ++n;
8279 }
8280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008281 }
8282 }
8283 else
8284 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008285 rettv->vval.v_number = n;
8286}
8287
8288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8290 *
8291 * Checks the existence of a cscope connection.
8292 */
8293/*ARGSUSED*/
8294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008295f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 typval_T *argvars;
8297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
8299#ifdef FEAT_CSCOPE
8300 int num = 0;
8301 char_u *dbpath = NULL;
8302 char_u *prepend = NULL;
8303 char_u buf[NUMBUFLEN];
8304
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008305 if (argvars[0].v_type != VAR_UNKNOWN
8306 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308 num = (int)get_tv_number(&argvars[0]);
8309 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008311 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 }
8313
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008314 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317#endif
8318}
8319
8320/*
8321 * "cursor(lnum, col)" function
8322 *
8323 * Moves the cursor to the specified line and column
8324 */
8325/*ARGSUSED*/
8326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008327f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 typval_T *argvars;
8329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
8331 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008332#ifdef FEAT_VIRTUALEDIT
8333 long coladd = 0;
8334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
Bram Moolenaara5525202006-03-02 22:52:09 +00008336 if (argvars[1].v_type == VAR_UNKNOWN)
8337 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008338 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008339
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008340 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008341 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008342 line = pos.lnum;
8343 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008344#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008345 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008346#endif
8347 }
8348 else
8349 {
8350 line = get_tv_lnum(argvars);
8351 col = get_tv_number_chk(&argvars[1], NULL);
8352#ifdef FEAT_VIRTUALEDIT
8353 if (argvars[2].v_type != VAR_UNKNOWN)
8354 coladd = get_tv_number_chk(&argvars[2], NULL);
8355#endif
8356 }
8357 if (line < 0 || col < 0
8358#ifdef FEAT_VIRTUALEDIT
8359 || coladd < 0
8360#endif
8361 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008362 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 if (line > 0)
8364 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 if (col > 0)
8366 curwin->w_cursor.col = col - 1;
8367#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008368 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369#endif
8370
8371 /* Make sure the cursor is in a valid position. */
8372 check_cursor();
8373#ifdef FEAT_MBYTE
8374 /* Correct cursor for multi-byte character. */
8375 if (has_mbyte)
8376 mb_adjust_cursor();
8377#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008378
8379 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380}
8381
8382/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008383 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 */
8385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008386f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008387 typval_T *argvars;
8388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008390 int noref = 0;
8391
8392 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008393 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008394 if (noref < 0 || noref > 1)
8395 EMSG(_(e_invarg));
8396 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008397 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398}
8399
8400/*
8401 * "delete()" function
8402 */
8403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008405 typval_T *argvars;
8406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
8408 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008409 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008411 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412}
8413
8414/*
8415 * "did_filetype()" function
8416 */
8417/*ARGSUSED*/
8418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008419f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008420 typval_T *argvars;
8421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
8423#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008424 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008426 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427#endif
8428}
8429
8430/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008431 * "diff_filler()" function
8432 */
8433/*ARGSUSED*/
8434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 typval_T *argvars;
8437 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008438{
8439#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008441#endif
8442}
8443
8444/*
8445 * "diff_hlID()" function
8446 */
8447/*ARGSUSED*/
8448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008449f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008450 typval_T *argvars;
8451 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008452{
8453#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008455 static linenr_T prev_lnum = 0;
8456 static int changedtick = 0;
8457 static int fnum = 0;
8458 static int change_start = 0;
8459 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008460 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008461 int filler_lines;
8462 int col;
8463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008464 if (lnum < 0) /* ignore type error in {lnum} arg */
8465 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008466 if (lnum != prev_lnum
8467 || changedtick != curbuf->b_changedtick
8468 || fnum != curbuf->b_fnum)
8469 {
8470 /* New line, buffer, change: need to get the values. */
8471 filler_lines = diff_check(curwin, lnum);
8472 if (filler_lines < 0)
8473 {
8474 if (filler_lines == -1)
8475 {
8476 change_start = MAXCOL;
8477 change_end = -1;
8478 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8479 hlID = HLF_ADD; /* added line */
8480 else
8481 hlID = HLF_CHD; /* changed line */
8482 }
8483 else
8484 hlID = HLF_ADD; /* added line */
8485 }
8486 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008487 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008488 prev_lnum = lnum;
8489 changedtick = curbuf->b_changedtick;
8490 fnum = curbuf->b_fnum;
8491 }
8492
8493 if (hlID == HLF_CHD || hlID == HLF_TXD)
8494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008495 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008496 if (col >= change_start && col <= change_end)
8497 hlID = HLF_TXD; /* changed text */
8498 else
8499 hlID = HLF_CHD; /* changed line */
8500 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008501 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008502#endif
8503}
8504
8505/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008506 * "empty({expr})" function
8507 */
8508 static void
8509f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 typval_T *argvars;
8511 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008512{
8513 int n;
8514
8515 switch (argvars[0].v_type)
8516 {
8517 case VAR_STRING:
8518 case VAR_FUNC:
8519 n = argvars[0].vval.v_string == NULL
8520 || *argvars[0].vval.v_string == NUL;
8521 break;
8522 case VAR_NUMBER:
8523 n = argvars[0].vval.v_number == 0;
8524 break;
8525 case VAR_LIST:
8526 n = argvars[0].vval.v_list == NULL
8527 || argvars[0].vval.v_list->lv_first == NULL;
8528 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008529 case VAR_DICT:
8530 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008531 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008533 default:
8534 EMSG2(_(e_intern2), "f_empty()");
8535 n = 0;
8536 }
8537
8538 rettv->vval.v_number = n;
8539}
8540
8541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 * "escape({string}, {chars})" function
8543 */
8544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008545f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 typval_T *argvars;
8547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 char_u buf[NUMBUFLEN];
8550
Bram Moolenaar758711c2005-02-02 23:11:38 +00008551 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8552 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008553 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554}
8555
8556/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008557 * "eval()" function
8558 */
8559/*ARGSUSED*/
8560 static void
8561f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 typval_T *argvars;
8563 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008564{
8565 char_u *s;
8566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008567 s = get_tv_string_chk(&argvars[0]);
8568 if (s != NULL)
8569 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008571 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8572 {
8573 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008574 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008575 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008576 else if (*s != NUL)
8577 EMSG(_(e_trailing));
8578}
8579
8580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 * "eventhandler()" function
8582 */
8583/*ARGSUSED*/
8584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 typval_T *argvars;
8587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590}
8591
8592/*
8593 * "executable()" function
8594 */
8595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008597 typval_T *argvars;
8598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601}
8602
8603/*
8604 * "exists()" function
8605 */
8606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008608 typval_T *argvars;
8609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
8611 char_u *p;
8612 char_u *name;
8613 int n = FALSE;
8614 int len = 0;
8615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008616 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 if (*p == '$') /* environment variable */
8618 {
8619 /* first try "normal" environment variables (fast) */
8620 if (mch_getenv(p + 1) != NULL)
8621 n = TRUE;
8622 else
8623 {
8624 /* try expanding things like $VIM and ${HOME} */
8625 p = expand_env_save(p);
8626 if (p != NULL && *p != '$')
8627 n = TRUE;
8628 vim_free(p);
8629 }
8630 }
8631 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 else if (*p == '*') /* internal or user defined function */
8634 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008635 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 }
8637 else if (*p == ':')
8638 {
8639 n = cmd_exists(p + 1);
8640 }
8641 else if (*p == '#')
8642 {
8643#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008644 if (p[1] == '#')
8645 n = autocmd_supported(p + 2);
8646 else
8647 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648#endif
8649 }
8650 else /* internal variable */
8651 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008652 char_u *tofree;
8653 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008655 /* get_name_len() takes care of expanding curly braces */
8656 name = p;
8657 len = get_name_len(&p, &tofree, TRUE, FALSE);
8658 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008660 if (tofree != NULL)
8661 name = tofree;
8662 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8663 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008665 /* handle d.key, l[idx], f(expr) */
8666 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8667 if (n)
8668 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 }
8670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008672 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 }
8674
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676}
8677
8678/*
8679 * "expand()" function
8680 */
8681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 typval_T *argvars;
8684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686 char_u *s;
8687 int len;
8688 char_u *errormsg;
8689 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8690 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008691 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693 rettv->v_type = VAR_STRING;
8694 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 if (*s == '%' || *s == '#' || *s == '<')
8696 {
8697 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 --emsg_off;
8700 }
8701 else
8702 {
8703 /* When the optional second argument is non-zero, don't remove matches
8704 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008705 if (argvars[1].v_type != VAR_UNKNOWN
8706 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008708 if (!error)
8709 {
8710 ExpandInit(&xpc);
8711 xpc.xp_context = EXPAND_FILES;
8712 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8713 ExpandCleanup(&xpc);
8714 }
8715 else
8716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 }
8718}
8719
8720/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008721 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008722 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008723 */
8724 static void
8725f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *argvars;
8727 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008728{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008729 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008730 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008731 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 list_T *l1, *l2;
8733 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008734 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008735 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008736
Bram Moolenaare9a41262005-01-15 22:18:47 +00008737 l1 = argvars[0].vval.v_list;
8738 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008739 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8740 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008741 {
8742 if (argvars[2].v_type != VAR_UNKNOWN)
8743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008744 before = get_tv_number_chk(&argvars[2], &error);
8745 if (error)
8746 return; /* type error; errmsg already given */
8747
Bram Moolenaar758711c2005-02-02 23:11:38 +00008748 if (before == l1->lv_len)
8749 item = NULL;
8750 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008751 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008752 item = list_find(l1, before);
8753 if (item == NULL)
8754 {
8755 EMSGN(_(e_listidx), before);
8756 return;
8757 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008758 }
8759 }
8760 else
8761 item = NULL;
8762 list_extend(l1, l2, item);
8763
Bram Moolenaare9a41262005-01-15 22:18:47 +00008764 copy_tv(&argvars[0], rettv);
8765 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008766 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008767 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8768 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 dict_T *d1, *d2;
8770 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008771 char_u *action;
8772 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008773 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008774 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008775
8776 d1 = argvars[0].vval.v_dict;
8777 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008778 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8779 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008780 {
8781 /* Check the third argument. */
8782 if (argvars[2].v_type != VAR_UNKNOWN)
8783 {
8784 static char *(av[]) = {"keep", "force", "error"};
8785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008786 action = get_tv_string_chk(&argvars[2]);
8787 if (action == NULL)
8788 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008789 for (i = 0; i < 3; ++i)
8790 if (STRCMP(action, av[i]) == 0)
8791 break;
8792 if (i == 3)
8793 {
8794 EMSGN(_(e_invarg2), action);
8795 return;
8796 }
8797 }
8798 else
8799 action = (char_u *)"force";
8800
8801 /* Go over all entries in the second dict and add them to the
8802 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 todo = d2->dv_hashtab.ht_used;
8804 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008805 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008806 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008807 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008808 --todo;
8809 di1 = dict_find(d1, hi2->hi_key, -1);
8810 if (di1 == NULL)
8811 {
8812 di1 = dictitem_copy(HI2DI(hi2));
8813 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8814 dictitem_free(di1);
8815 }
8816 else if (*action == 'e')
8817 {
8818 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8819 break;
8820 }
8821 else if (*action == 'f')
8822 {
8823 clear_tv(&di1->di_tv);
8824 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8825 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008826 }
8827 }
8828
Bram Moolenaare9a41262005-01-15 22:18:47 +00008829 copy_tv(&argvars[0], rettv);
8830 }
8831 }
8832 else
8833 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008834}
8835
8836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 * "filereadable()" function
8838 */
8839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008841 typval_T *argvars;
8842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
8844 FILE *fd;
8845 char_u *p;
8846 int n;
8847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8850 {
8851 n = TRUE;
8852 fclose(fd);
8853 }
8854 else
8855 n = FALSE;
8856
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008857 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858}
8859
8860/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008861 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 * rights to write into.
8863 */
8864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008869 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870}
8871
Bram Moolenaar33570922005-01-25 22:26:29 +00008872static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008873
8874 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008875findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008876 typval_T *argvars;
8877 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008878 int dir;
8879{
8880#ifdef FEAT_SEARCHPATH
8881 char_u *fname;
8882 char_u *fresult = NULL;
8883 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8884 char_u *p;
8885 char_u pathbuf[NUMBUFLEN];
8886 int count = 1;
8887 int first = TRUE;
8888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008890
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008891 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8894 if (p == NULL)
8895 count = -1; /* error */
8896 else
8897 {
8898 if (*p != NUL)
8899 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008901 if (argvars[2].v_type != VAR_UNKNOWN)
8902 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8903 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008904 }
8905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008906 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008908 do
8909 {
8910 vim_free(fresult);
8911 fresult = find_file_in_path_option(first ? fname : NULL,
8912 first ? (int)STRLEN(fname) : 0,
8913 0, first, path, dir, NULL);
8914 first = FALSE;
8915 } while (--count > 0 && fresult != NULL);
8916 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008917
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008919#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008921#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008923}
8924
Bram Moolenaar33570922005-01-25 22:26:29 +00008925static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8926static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008927
8928/*
8929 * Implementation of map() and filter().
8930 */
8931 static void
8932filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008933 typval_T *argvars;
8934 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008935 int map;
8936{
8937 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008938 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008939 listitem_T *li, *nli;
8940 list_T *l = NULL;
8941 dictitem_T *di;
8942 hashtab_T *ht;
8943 hashitem_T *hi;
8944 dict_T *d = NULL;
8945 typval_T save_val;
8946 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008948 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008949 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008950 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008951
8952 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008953 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008954 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008955 if ((l = argvars[0].vval.v_list) == NULL
8956 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 return;
8958 }
8959 else if (argvars[0].v_type == VAR_DICT)
8960 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008961 if ((d = argvars[0].vval.v_dict) == NULL
8962 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008963 return;
8964 }
8965 else
8966 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008967 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008968 return;
8969 }
8970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008971 expr = get_tv_string_buf_chk(&argvars[1], buf);
8972 /* On type errors, the preceding call has already displayed an error
8973 * message. Avoid a misleading error message for an empty string that
8974 * was not passed as argument. */
8975 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008977 prepare_vimvar(VV_VAL, &save_val);
8978 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008979
Bram Moolenaar280f1262006-01-30 00:14:18 +00008980 /* We reset "called_emsg" to be able to detect whether an error
8981 * occurred during evaluation of the expression. "did_emsg" can't be
8982 * used, because it is reset when calling a function. */
8983 save_called_emsg = called_emsg;
8984 called_emsg = FALSE;
8985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008986 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 prepare_vimvar(VV_KEY, &save_key);
8989 vimvars[VV_KEY].vv_type = VAR_STRING;
8990
8991 ht = &d->dv_hashtab;
8992 hash_lock(ht);
8993 todo = ht->ht_used;
8994 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996 if (!HASHITEM_EMPTY(hi))
8997 {
8998 --todo;
8999 di = HI2DI(hi);
9000 if (tv_check_lock(di->di_tv.v_lock, msg))
9001 break;
9002 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009003 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
9004 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009005 break;
9006 if (!map && rem)
9007 dictitem_remove(d, di);
9008 clear_tv(&vimvars[VV_KEY].vv_tv);
9009 }
9010 }
9011 hash_unlock(ht);
9012
9013 restore_vimvar(VV_KEY, &save_key);
9014 }
9015 else
9016 {
9017 for (li = l->lv_first; li != NULL; li = nli)
9018 {
9019 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009020 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009022 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
9023 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009024 break;
9025 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009026 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009027 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009028 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009030 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009031
9032 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009034
9035 copy_tv(&argvars[0], rettv);
9036}
9037
9038 static int
9039filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 char_u *expr;
9042 int map;
9043 int *remp;
9044{
Bram Moolenaar33570922005-01-25 22:26:29 +00009045 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009046 char_u *s;
9047
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049 s = expr;
9050 if (eval1(&s, &rettv, TRUE) == FAIL)
9051 return FAIL;
9052 if (*s != NUL) /* check for trailing chars after expr */
9053 {
9054 EMSG2(_(e_invexpr2), s);
9055 return FAIL;
9056 }
9057 if (map)
9058 {
9059 /* map(): replace the list item value */
9060 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009061 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009062 *tv = rettv;
9063 }
9064 else
9065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009066 int error = FALSE;
9067
Bram Moolenaare9a41262005-01-15 22:18:47 +00009068 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009070 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009071 /* On type error, nothing has been removed; return FAIL to stop the
9072 * loop. The error message was given by get_tv_number_chk(). */
9073 if (error)
9074 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009075 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009077 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009078}
9079
9080/*
9081 * "filter()" function
9082 */
9083 static void
9084f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009085 typval_T *argvars;
9086 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009087{
9088 filter_map(argvars, rettv, FALSE);
9089}
9090
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009091/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009092 * "finddir({fname}[, {path}[, {count}]])" function
9093 */
9094 static void
9095f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *argvars;
9097 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009098{
9099 findfilendir(argvars, rettv, TRUE);
9100}
9101
9102/*
9103 * "findfile({fname}[, {path}[, {count}]])" function
9104 */
9105 static void
9106f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009109{
9110 findfilendir(argvars, rettv, FALSE);
9111}
9112
9113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 * "fnamemodify({fname}, {mods})" function
9115 */
9116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009118 typval_T *argvars;
9119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 char_u *fname;
9122 char_u *mods;
9123 int usedlen = 0;
9124 int len;
9125 char_u *fbuf = NULL;
9126 char_u buf[NUMBUFLEN];
9127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 fname = get_tv_string_chk(&argvars[0]);
9129 mods = get_tv_string_buf_chk(&argvars[1], buf);
9130 if (fname == NULL || mods == NULL)
9131 fname = NULL;
9132 else
9133 {
9134 len = (int)STRLEN(fname);
9135 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 vim_free(fbuf);
9144}
9145
Bram Moolenaar33570922005-01-25 22:26:29 +00009146static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147
9148/*
9149 * "foldclosed()" function
9150 */
9151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 typval_T *argvars;
9154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 int end;
9156{
9157#ifdef FEAT_FOLDING
9158 linenr_T lnum;
9159 linenr_T first, last;
9160
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9163 {
9164 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9165 {
9166 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 return;
9171 }
9172 }
9173#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175}
9176
9177/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009178 * "foldclosed()" function
9179 */
9180 static void
9181f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009182 typval_T *argvars;
9183 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009184{
9185 foldclosed_both(argvars, rettv, FALSE);
9186}
9187
9188/*
9189 * "foldclosedend()" function
9190 */
9191 static void
9192f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009193 typval_T *argvars;
9194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009195{
9196 foldclosed_both(argvars, rettv, TRUE);
9197}
9198
9199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 * "foldlevel()" function
9201 */
9202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009204 typval_T *argvars;
9205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206{
9207#ifdef FEAT_FOLDING
9208 linenr_T lnum;
9209
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 else
9214#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216}
9217
9218/*
9219 * "foldtext()" function
9220 */
9221/*ARGSUSED*/
9222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009223f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009224 typval_T *argvars;
9225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226{
9227#ifdef FEAT_FOLDING
9228 linenr_T lnum;
9229 char_u *s;
9230 char_u *r;
9231 int len;
9232 char *txt;
9233#endif
9234
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009235 rettv->v_type = VAR_STRING;
9236 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009238 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9239 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9240 <= curbuf->b_ml.ml_line_count
9241 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 {
9243 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009244 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9245 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 {
9247 if (!linewhite(lnum))
9248 break;
9249 ++lnum;
9250 }
9251
9252 /* Find interesting text in this line. */
9253 s = skipwhite(ml_get(lnum));
9254 /* skip C comment-start */
9255 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009258 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009259 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009260 {
9261 s = skipwhite(ml_get(lnum + 1));
9262 if (*s == '*')
9263 s = skipwhite(s + 1);
9264 }
9265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 txt = _("+-%s%3ld lines: ");
9267 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009268 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 + 20 /* for %3ld */
9270 + STRLEN(s))); /* concatenated */
9271 if (r != NULL)
9272 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009273 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9274 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9275 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 len = (int)STRLEN(r);
9277 STRCAT(r, s);
9278 /* remove 'foldmarker' and 'commentstring' */
9279 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 }
9282 }
9283#endif
9284}
9285
9286/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009287 * "foldtextresult(lnum)" function
9288 */
9289/*ARGSUSED*/
9290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009292 typval_T *argvars;
9293 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009294{
9295#ifdef FEAT_FOLDING
9296 linenr_T lnum;
9297 char_u *text;
9298 char_u buf[51];
9299 foldinfo_T foldinfo;
9300 int fold_count;
9301#endif
9302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->v_type = VAR_STRING;
9304 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009305#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009307 /* treat illegal types and illegal string values for {lnum} the same */
9308 if (lnum < 0)
9309 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009310 fold_count = foldedCount(curwin, lnum, &foldinfo);
9311 if (fold_count > 0)
9312 {
9313 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9314 &foldinfo, buf);
9315 if (text == buf)
9316 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009318 }
9319#endif
9320}
9321
9322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 * "foreground()" function
9324 */
9325/*ARGSUSED*/
9326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009328 typval_T *argvars;
9329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332#ifdef FEAT_GUI
9333 if (gui.in_use)
9334 gui_mch_set_foreground();
9335#else
9336# ifdef WIN32
9337 win32_set_foreground();
9338# endif
9339#endif
9340}
9341
9342/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009343 * "function()" function
9344 */
9345/*ARGSUSED*/
9346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009347f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009348 typval_T *argvars;
9349 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009350{
9351 char_u *s;
9352
Bram Moolenaara7043832005-01-21 11:56:39 +00009353 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009354 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009355 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009356 EMSG2(_(e_invarg2), s);
9357 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009358 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009359 else
9360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 rettv->vval.v_string = vim_strsave(s);
9362 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009363 }
9364}
9365
9366/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009367 * "garbagecollect()" function
9368 */
9369/*ARGSUSED*/
9370 static void
9371f_garbagecollect(argvars, rettv)
9372 typval_T *argvars;
9373 typval_T *rettv;
9374{
9375 garbage_collect();
9376}
9377
9378/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009379 * "get()" function
9380 */
9381 static void
9382f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *argvars;
9384 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009385{
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 listitem_T *li;
9387 list_T *l;
9388 dictitem_T *di;
9389 dict_T *d;
9390 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009391
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009393 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009394 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009396 int error = FALSE;
9397
9398 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9399 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009400 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009401 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009403 else if (argvars[0].v_type == VAR_DICT)
9404 {
9405 if ((d = argvars[0].vval.v_dict) != NULL)
9406 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009407 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009408 if (di != NULL)
9409 tv = &di->di_tv;
9410 }
9411 }
9412 else
9413 EMSG2(_(e_listdictarg), "get()");
9414
9415 if (tv == NULL)
9416 {
9417 if (argvars[2].v_type == VAR_UNKNOWN)
9418 rettv->vval.v_number = 0;
9419 else
9420 copy_tv(&argvars[2], rettv);
9421 }
9422 else
9423 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009424}
9425
Bram Moolenaar342337a2005-07-21 21:11:17 +00009426static 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 +00009427
9428/*
9429 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009430 * Return a range (from start to end) of lines in rettv from the specified
9431 * buffer.
9432 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009433 */
9434 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009435get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009436 buf_T *buf;
9437 linenr_T start;
9438 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009439 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009440 typval_T *rettv;
9441{
9442 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009443
Bram Moolenaar342337a2005-07-21 21:11:17 +00009444 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009445 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009446 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009447 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009448 }
9449 else
9450 rettv->vval.v_number = 0;
9451
9452 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9453 return;
9454
9455 if (!retlist)
9456 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009457 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9458 p = ml_get_buf(buf, start, FALSE);
9459 else
9460 p = (char_u *)"";
9461
9462 rettv->v_type = VAR_STRING;
9463 rettv->vval.v_string = vim_strsave(p);
9464 }
9465 else
9466 {
9467 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009468 return;
9469
9470 if (start < 1)
9471 start = 1;
9472 if (end > buf->b_ml.ml_line_count)
9473 end = buf->b_ml.ml_line_count;
9474 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009475 if (list_append_string(rettv->vval.v_list,
9476 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009477 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009478 }
9479}
9480
9481/*
9482 * "getbufline()" function
9483 */
9484 static void
9485f_getbufline(argvars, rettv)
9486 typval_T *argvars;
9487 typval_T *rettv;
9488{
9489 linenr_T lnum;
9490 linenr_T end;
9491 buf_T *buf;
9492
9493 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9494 ++emsg_off;
9495 buf = get_buf_tv(&argvars[0]);
9496 --emsg_off;
9497
Bram Moolenaar661b1822005-07-28 22:36:45 +00009498 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009499 if (argvars[2].v_type == VAR_UNKNOWN)
9500 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009501 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009502 end = get_tv_lnum_buf(&argvars[2], buf);
9503
Bram Moolenaar342337a2005-07-21 21:11:17 +00009504 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009505}
9506
Bram Moolenaar0d660222005-01-07 21:51:51 +00009507/*
9508 * "getbufvar()" function
9509 */
9510 static void
9511f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009512 typval_T *argvars;
9513 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009514{
9515 buf_T *buf;
9516 buf_T *save_curbuf;
9517 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009518 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009520 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9521 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009522 ++emsg_off;
9523 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009524
9525 rettv->v_type = VAR_STRING;
9526 rettv->vval.v_string = NULL;
9527
9528 if (buf != NULL && varname != NULL)
9529 {
9530 if (*varname == '&') /* buffer-local-option */
9531 {
9532 /* set curbuf to be our buf, temporarily */
9533 save_curbuf = curbuf;
9534 curbuf = buf;
9535
9536 get_option_tv(&varname, rettv, TRUE);
9537
9538 /* restore previous notion of curbuf */
9539 curbuf = save_curbuf;
9540 }
9541 else
9542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009543 if (*varname == NUL)
9544 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9545 * scope prefix before the NUL byte is required by
9546 * find_var_in_ht(). */
9547 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009548 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009549 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009550 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009551 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009552 }
9553 }
9554
9555 --emsg_off;
9556}
9557
9558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 * "getchar()" function
9560 */
9561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009563 typval_T *argvars;
9564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568
9569 ++no_mapping;
9570 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009571 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 /* getchar(): blocking wait. */
9573 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 /* getchar(1): only check if char avail */
9576 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009577 else if (error || vpeekc() == NUL)
9578 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 n = 0;
9580 else
9581 /* getchar(0) and char avail: return char */
9582 n = safe_vgetc();
9583 --no_mapping;
9584 --allow_keys;
9585
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 if (IS_SPECIAL(n) || mod_mask != 0)
9588 {
9589 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9590 int i = 0;
9591
9592 /* Turn a special key into three bytes, plus modifier. */
9593 if (mod_mask != 0)
9594 {
9595 temp[i++] = K_SPECIAL;
9596 temp[i++] = KS_MODIFIER;
9597 temp[i++] = mod_mask;
9598 }
9599 if (IS_SPECIAL(n))
9600 {
9601 temp[i++] = K_SPECIAL;
9602 temp[i++] = K_SECOND(n);
9603 temp[i++] = K_THIRD(n);
9604 }
9605#ifdef FEAT_MBYTE
9606 else if (has_mbyte)
9607 i += (*mb_char2bytes)(n, temp + i);
9608#endif
9609 else
9610 temp[i++] = n;
9611 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 rettv->v_type = VAR_STRING;
9613 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 }
9615}
9616
9617/*
9618 * "getcharmod()" function
9619 */
9620/*ARGSUSED*/
9621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009623 typval_T *argvars;
9624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627}
9628
9629/*
9630 * "getcmdline()" function
9631 */
9632/*ARGSUSED*/
9633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 typval_T *argvars;
9636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->v_type = VAR_STRING;
9639 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640}
9641
9642/*
9643 * "getcmdpos()" function
9644 */
9645/*ARGSUSED*/
9646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009648 typval_T *argvars;
9649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652}
9653
9654/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009655 * "getcmdtype()" function
9656 */
9657/*ARGSUSED*/
9658 static void
9659f_getcmdtype(argvars, rettv)
9660 typval_T *argvars;
9661 typval_T *rettv;
9662{
9663 rettv->v_type = VAR_STRING;
9664 rettv->vval.v_string = alloc(2);
9665 if (rettv->vval.v_string != NULL)
9666 {
9667 rettv->vval.v_string[0] = get_cmdline_type();
9668 rettv->vval.v_string[1] = NUL;
9669 }
9670}
9671
9672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 * "getcwd()" function
9674 */
9675/*ARGSUSED*/
9676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009678 typval_T *argvars;
9679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 char_u cwd[MAXPATHL];
9682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 else
9687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009690 if (rettv->vval.v_string != NULL)
9691 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692#endif
9693 }
9694}
9695
9696/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009697 * "getfontname()" function
9698 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009699/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009702 typval_T *argvars;
9703 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009704{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->v_type = VAR_STRING;
9706 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009707#ifdef FEAT_GUI
9708 if (gui.in_use)
9709 {
9710 GuiFont font;
9711 char_u *name = NULL;
9712
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009713 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009714 {
9715 /* Get the "Normal" font. Either the name saved by
9716 * hl_set_font_name() or from the font ID. */
9717 font = gui.norm_font;
9718 name = hl_get_font_name();
9719 }
9720 else
9721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009723 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9724 return;
9725 font = gui_mch_get_font(name, FALSE);
9726 if (font == NOFONT)
9727 return; /* Invalid font name, return empty string. */
9728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009730 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009731 gui_mch_free_font(font);
9732 }
9733#endif
9734}
9735
9736/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009737 * "getfperm({fname})" function
9738 */
9739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009741 typval_T *argvars;
9742 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009743{
9744 char_u *fname;
9745 struct stat st;
9746 char_u *perm = NULL;
9747 char_u flags[] = "rwx";
9748 int i;
9749
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009751
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009753 if (mch_stat((char *)fname, &st) >= 0)
9754 {
9755 perm = vim_strsave((char_u *)"---------");
9756 if (perm != NULL)
9757 {
9758 for (i = 0; i < 9; i++)
9759 {
9760 if (st.st_mode & (1 << (8 - i)))
9761 perm[i] = flags[i % 3];
9762 }
9763 }
9764 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009766}
9767
9768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 * "getfsize({fname})" function
9770 */
9771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009773 typval_T *argvars;
9774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
9776 char_u *fname;
9777 struct stat st;
9778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782
9783 if (mch_stat((char *)fname, &st) >= 0)
9784 {
9785 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 }
9790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792}
9793
9794/*
9795 * "getftime({fname})" function
9796 */
9797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009799 typval_T *argvars;
9800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
9802 char_u *fname;
9803 struct stat st;
9804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
9807 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811}
9812
9813/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009814 * "getftype({fname})" function
9815 */
9816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009818 typval_T *argvars;
9819 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009820{
9821 char_u *fname;
9822 struct stat st;
9823 char_u *type = NULL;
9824 char *t;
9825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009827
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009829 if (mch_lstat((char *)fname, &st) >= 0)
9830 {
9831#ifdef S_ISREG
9832 if (S_ISREG(st.st_mode))
9833 t = "file";
9834 else if (S_ISDIR(st.st_mode))
9835 t = "dir";
9836# ifdef S_ISLNK
9837 else if (S_ISLNK(st.st_mode))
9838 t = "link";
9839# endif
9840# ifdef S_ISBLK
9841 else if (S_ISBLK(st.st_mode))
9842 t = "bdev";
9843# endif
9844# ifdef S_ISCHR
9845 else if (S_ISCHR(st.st_mode))
9846 t = "cdev";
9847# endif
9848# ifdef S_ISFIFO
9849 else if (S_ISFIFO(st.st_mode))
9850 t = "fifo";
9851# endif
9852# ifdef S_ISSOCK
9853 else if (S_ISSOCK(st.st_mode))
9854 t = "fifo";
9855# endif
9856 else
9857 t = "other";
9858#else
9859# ifdef S_IFMT
9860 switch (st.st_mode & S_IFMT)
9861 {
9862 case S_IFREG: t = "file"; break;
9863 case S_IFDIR: t = "dir"; break;
9864# ifdef S_IFLNK
9865 case S_IFLNK: t = "link"; break;
9866# endif
9867# ifdef S_IFBLK
9868 case S_IFBLK: t = "bdev"; break;
9869# endif
9870# ifdef S_IFCHR
9871 case S_IFCHR: t = "cdev"; break;
9872# endif
9873# ifdef S_IFIFO
9874 case S_IFIFO: t = "fifo"; break;
9875# endif
9876# ifdef S_IFSOCK
9877 case S_IFSOCK: t = "socket"; break;
9878# endif
9879 default: t = "other";
9880 }
9881# else
9882 if (mch_isdir(fname))
9883 t = "dir";
9884 else
9885 t = "file";
9886# endif
9887#endif
9888 type = vim_strsave((char_u *)t);
9889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009891}
9892
9893/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009894 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009895 */
9896 static void
9897f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 typval_T *argvars;
9899 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009900{
9901 linenr_T lnum;
9902 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009903 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009904
9905 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009906 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009907 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009908 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009909 retlist = FALSE;
9910 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009911 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009912 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009913 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009914 retlist = TRUE;
9915 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009916
Bram Moolenaar342337a2005-07-21 21:11:17 +00009917 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009918}
9919
9920/*
Bram Moolenaara5525202006-03-02 22:52:09 +00009921 * "getpos(string)" function
9922 */
9923 static void
9924f_getpos(argvars, rettv)
9925 typval_T *argvars;
9926 typval_T *rettv;
9927{
9928 pos_T *fp;
9929 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009930 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009931
9932 if (rettv_list_alloc(rettv) == OK)
9933 {
9934 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009935 fp = var2fpos(&argvars[0], TRUE, &fnum);
9936 if (fnum != -1)
9937 list_append_number(l, (varnumber_T)fnum);
9938 else
9939 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +00009940 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
9941 : (varnumber_T)0);
9942 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
9943 : (varnumber_T)0);
9944 list_append_number(l,
9945#ifdef FEAT_VIRTUALEDIT
9946 (fp != NULL) ? (varnumber_T)fp->coladd :
9947#endif
9948 (varnumber_T)0);
9949 }
9950 else
9951 rettv->vval.v_number = FALSE;
9952}
9953
9954/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009955 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009956 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009957/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009958 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009959f_getqflist(argvars, rettv)
9960 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009961 typval_T *rettv;
9962{
9963#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +00009964 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009965#endif
9966
9967 rettv->vval.v_number = FALSE;
9968#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009969 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +00009970 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00009971 wp = NULL;
9972 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9973 {
9974 wp = find_win_by_nr(&argvars[0]);
9975 if (wp == NULL)
9976 return;
9977 }
9978
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009979 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009980 }
9981#endif
9982}
9983
9984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 * "getreg()" function
9986 */
9987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009988f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009989 typval_T *argvars;
9990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991{
9992 char_u *strregname;
9993 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009994 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009995 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009997 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009999 strregname = get_tv_string_chk(&argvars[0]);
10000 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010001 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010002 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010005 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 regname = (strregname == NULL ? '"' : *strregname);
10007 if (regname == 0)
10008 regname = '"';
10009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010011 rettv->vval.v_string = error ? NULL :
10012 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013}
10014
10015/*
10016 * "getregtype()" function
10017 */
10018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010020 typval_T *argvars;
10021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022{
10023 char_u *strregname;
10024 int regname;
10025 char_u buf[NUMBUFLEN + 2];
10026 long reglen = 0;
10027
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010028 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010029 {
10030 strregname = get_tv_string_chk(&argvars[0]);
10031 if (strregname == NULL) /* type error; errmsg already given */
10032 {
10033 rettv->v_type = VAR_STRING;
10034 rettv->vval.v_string = NULL;
10035 return;
10036 }
10037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 else
10039 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010040 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
10042 regname = (strregname == NULL ? '"' : *strregname);
10043 if (regname == 0)
10044 regname = '"';
10045
10046 buf[0] = NUL;
10047 buf[1] = NUL;
10048 switch (get_reg_type(regname, &reglen))
10049 {
10050 case MLINE: buf[0] = 'V'; break;
10051 case MCHAR: buf[0] = 'v'; break;
10052#ifdef FEAT_VISUAL
10053 case MBLOCK:
10054 buf[0] = Ctrl_V;
10055 sprintf((char *)buf + 1, "%ld", reglen + 1);
10056 break;
10057#endif
10058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 rettv->v_type = VAR_STRING;
10060 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061}
10062
10063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 * "getwinposx()" function
10065 */
10066/*ARGSUSED*/
10067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010069 typval_T *argvars;
10070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073#ifdef FEAT_GUI
10074 if (gui.in_use)
10075 {
10076 int x, y;
10077
10078 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 }
10081#endif
10082}
10083
10084/*
10085 * "getwinposy()" function
10086 */
10087/*ARGSUSED*/
10088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010090 typval_T *argvars;
10091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094#ifdef FEAT_GUI
10095 if (gui.in_use)
10096 {
10097 int x, y;
10098
10099 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 }
10102#endif
10103}
10104
Bram Moolenaara40058a2005-07-11 22:42:07 +000010105 static win_T *
10106find_win_by_nr(vp)
10107 typval_T *vp;
10108{
10109#ifdef FEAT_WINDOWS
10110 win_T *wp;
10111#endif
10112 int nr;
10113
10114 nr = get_tv_number_chk(vp, NULL);
10115
10116#ifdef FEAT_WINDOWS
10117 if (nr < 0)
10118 return NULL;
10119 if (nr == 0)
10120 return curwin;
10121
10122 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10123 if (--nr <= 0)
10124 break;
10125 return wp;
10126#else
10127 if (nr == 0 || nr == 1)
10128 return curwin;
10129 return NULL;
10130#endif
10131}
10132
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133/*
10134 * "getwinvar()" function
10135 */
10136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010138 typval_T *argvars;
10139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140{
10141 win_T *win, *oldcurwin;
10142 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010143 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010146 varname = get_tv_string_chk(&argvars[1]);
10147 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 rettv->v_type = VAR_STRING;
10150 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151
10152 if (win != NULL && varname != NULL)
10153 {
10154 if (*varname == '&') /* window-local-option */
10155 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010156 /* Set curwin to be our win, temporarily. Also set curbuf, so
10157 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 oldcurwin = curwin;
10159 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010160 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163
10164 /* restore previous notion of curwin */
10165 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010166 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 }
10168 else
10169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010170 if (*varname == NUL)
10171 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10172 * scope prefix before the NUL byte is required by
10173 * find_var_in_ht(). */
10174 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010176 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010178 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 }
10180 }
10181
10182 --emsg_off;
10183}
10184
10185/*
10186 * "glob()" function
10187 */
10188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010190 typval_T *argvars;
10191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192{
10193 expand_T xpc;
10194
10195 ExpandInit(&xpc);
10196 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010197 rettv->v_type = VAR_STRING;
10198 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10200 ExpandCleanup(&xpc);
10201}
10202
10203/*
10204 * "globpath()" function
10205 */
10206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010208 typval_T *argvars;
10209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210{
10211 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010214 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 if (file == NULL)
10216 rettv->vval.v_string = NULL;
10217 else
10218 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219}
10220
10221/*
10222 * "has()" function
10223 */
10224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010225f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010226 typval_T *argvars;
10227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228{
10229 int i;
10230 char_u *name;
10231 int n = FALSE;
10232 static char *(has_list[]) =
10233 {
10234#ifdef AMIGA
10235 "amiga",
10236# ifdef FEAT_ARP
10237 "arp",
10238# endif
10239#endif
10240#ifdef __BEOS__
10241 "beos",
10242#endif
10243#ifdef MSDOS
10244# ifdef DJGPP
10245 "dos32",
10246# else
10247 "dos16",
10248# endif
10249#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010250#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 "mac",
10252#endif
10253#if defined(MACOS_X_UNIX)
10254 "macunix",
10255#endif
10256#ifdef OS2
10257 "os2",
10258#endif
10259#ifdef __QNX__
10260 "qnx",
10261#endif
10262#ifdef RISCOS
10263 "riscos",
10264#endif
10265#ifdef UNIX
10266 "unix",
10267#endif
10268#ifdef VMS
10269 "vms",
10270#endif
10271#ifdef WIN16
10272 "win16",
10273#endif
10274#ifdef WIN32
10275 "win32",
10276#endif
10277#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10278 "win32unix",
10279#endif
10280#ifdef WIN64
10281 "win64",
10282#endif
10283#ifdef EBCDIC
10284 "ebcdic",
10285#endif
10286#ifndef CASE_INSENSITIVE_FILENAME
10287 "fname_case",
10288#endif
10289#ifdef FEAT_ARABIC
10290 "arabic",
10291#endif
10292#ifdef FEAT_AUTOCMD
10293 "autocmd",
10294#endif
10295#ifdef FEAT_BEVAL
10296 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010297# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10298 "balloon_multiline",
10299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300#endif
10301#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10302 "builtin_terms",
10303# ifdef ALL_BUILTIN_TCAPS
10304 "all_builtin_terms",
10305# endif
10306#endif
10307#ifdef FEAT_BYTEOFF
10308 "byte_offset",
10309#endif
10310#ifdef FEAT_CINDENT
10311 "cindent",
10312#endif
10313#ifdef FEAT_CLIENTSERVER
10314 "clientserver",
10315#endif
10316#ifdef FEAT_CLIPBOARD
10317 "clipboard",
10318#endif
10319#ifdef FEAT_CMDL_COMPL
10320 "cmdline_compl",
10321#endif
10322#ifdef FEAT_CMDHIST
10323 "cmdline_hist",
10324#endif
10325#ifdef FEAT_COMMENTS
10326 "comments",
10327#endif
10328#ifdef FEAT_CRYPT
10329 "cryptv",
10330#endif
10331#ifdef FEAT_CSCOPE
10332 "cscope",
10333#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010334#ifdef CURSOR_SHAPE
10335 "cursorshape",
10336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337#ifdef DEBUG
10338 "debug",
10339#endif
10340#ifdef FEAT_CON_DIALOG
10341 "dialog_con",
10342#endif
10343#ifdef FEAT_GUI_DIALOG
10344 "dialog_gui",
10345#endif
10346#ifdef FEAT_DIFF
10347 "diff",
10348#endif
10349#ifdef FEAT_DIGRAPHS
10350 "digraphs",
10351#endif
10352#ifdef FEAT_DND
10353 "dnd",
10354#endif
10355#ifdef FEAT_EMACS_TAGS
10356 "emacs_tags",
10357#endif
10358 "eval", /* always present, of course! */
10359#ifdef FEAT_EX_EXTRA
10360 "ex_extra",
10361#endif
10362#ifdef FEAT_SEARCH_EXTRA
10363 "extra_search",
10364#endif
10365#ifdef FEAT_FKMAP
10366 "farsi",
10367#endif
10368#ifdef FEAT_SEARCHPATH
10369 "file_in_path",
10370#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010371#if defined(UNIX) && !defined(USE_SYSTEM)
10372 "filterpipe",
10373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374#ifdef FEAT_FIND_ID
10375 "find_in_path",
10376#endif
10377#ifdef FEAT_FOLDING
10378 "folding",
10379#endif
10380#ifdef FEAT_FOOTER
10381 "footer",
10382#endif
10383#if !defined(USE_SYSTEM) && defined(UNIX)
10384 "fork",
10385#endif
10386#ifdef FEAT_GETTEXT
10387 "gettext",
10388#endif
10389#ifdef FEAT_GUI
10390 "gui",
10391#endif
10392#ifdef FEAT_GUI_ATHENA
10393# ifdef FEAT_GUI_NEXTAW
10394 "gui_neXtaw",
10395# else
10396 "gui_athena",
10397# endif
10398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399#ifdef FEAT_GUI_GTK
10400 "gui_gtk",
10401# ifdef HAVE_GTK2
10402 "gui_gtk2",
10403# endif
10404#endif
10405#ifdef FEAT_GUI_MAC
10406 "gui_mac",
10407#endif
10408#ifdef FEAT_GUI_MOTIF
10409 "gui_motif",
10410#endif
10411#ifdef FEAT_GUI_PHOTON
10412 "gui_photon",
10413#endif
10414#ifdef FEAT_GUI_W16
10415 "gui_win16",
10416#endif
10417#ifdef FEAT_GUI_W32
10418 "gui_win32",
10419#endif
10420#ifdef FEAT_HANGULIN
10421 "hangul_input",
10422#endif
10423#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10424 "iconv",
10425#endif
10426#ifdef FEAT_INS_EXPAND
10427 "insert_expand",
10428#endif
10429#ifdef FEAT_JUMPLIST
10430 "jumplist",
10431#endif
10432#ifdef FEAT_KEYMAP
10433 "keymap",
10434#endif
10435#ifdef FEAT_LANGMAP
10436 "langmap",
10437#endif
10438#ifdef FEAT_LIBCALL
10439 "libcall",
10440#endif
10441#ifdef FEAT_LINEBREAK
10442 "linebreak",
10443#endif
10444#ifdef FEAT_LISP
10445 "lispindent",
10446#endif
10447#ifdef FEAT_LISTCMDS
10448 "listcmds",
10449#endif
10450#ifdef FEAT_LOCALMAP
10451 "localmap",
10452#endif
10453#ifdef FEAT_MENU
10454 "menu",
10455#endif
10456#ifdef FEAT_SESSION
10457 "mksession",
10458#endif
10459#ifdef FEAT_MODIFY_FNAME
10460 "modify_fname",
10461#endif
10462#ifdef FEAT_MOUSE
10463 "mouse",
10464#endif
10465#ifdef FEAT_MOUSESHAPE
10466 "mouseshape",
10467#endif
10468#if defined(UNIX) || defined(VMS)
10469# ifdef FEAT_MOUSE_DEC
10470 "mouse_dec",
10471# endif
10472# ifdef FEAT_MOUSE_GPM
10473 "mouse_gpm",
10474# endif
10475# ifdef FEAT_MOUSE_JSB
10476 "mouse_jsbterm",
10477# endif
10478# ifdef FEAT_MOUSE_NET
10479 "mouse_netterm",
10480# endif
10481# ifdef FEAT_MOUSE_PTERM
10482 "mouse_pterm",
10483# endif
10484# ifdef FEAT_MOUSE_XTERM
10485 "mouse_xterm",
10486# endif
10487#endif
10488#ifdef FEAT_MBYTE
10489 "multi_byte",
10490#endif
10491#ifdef FEAT_MBYTE_IME
10492 "multi_byte_ime",
10493#endif
10494#ifdef FEAT_MULTI_LANG
10495 "multi_lang",
10496#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010497#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010498#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010499 "mzscheme",
10500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502#ifdef FEAT_OLE
10503 "ole",
10504#endif
10505#ifdef FEAT_OSFILETYPE
10506 "osfiletype",
10507#endif
10508#ifdef FEAT_PATH_EXTRA
10509 "path_extra",
10510#endif
10511#ifdef FEAT_PERL
10512#ifndef DYNAMIC_PERL
10513 "perl",
10514#endif
10515#endif
10516#ifdef FEAT_PYTHON
10517#ifndef DYNAMIC_PYTHON
10518 "python",
10519#endif
10520#endif
10521#ifdef FEAT_POSTSCRIPT
10522 "postscript",
10523#endif
10524#ifdef FEAT_PRINTER
10525 "printer",
10526#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010527#ifdef FEAT_PROFILE
10528 "profile",
10529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530#ifdef FEAT_QUICKFIX
10531 "quickfix",
10532#endif
10533#ifdef FEAT_RIGHTLEFT
10534 "rightleft",
10535#endif
10536#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10537 "ruby",
10538#endif
10539#ifdef FEAT_SCROLLBIND
10540 "scrollbind",
10541#endif
10542#ifdef FEAT_CMDL_INFO
10543 "showcmd",
10544 "cmdline_info",
10545#endif
10546#ifdef FEAT_SIGNS
10547 "signs",
10548#endif
10549#ifdef FEAT_SMARTINDENT
10550 "smartindent",
10551#endif
10552#ifdef FEAT_SNIFF
10553 "sniff",
10554#endif
10555#ifdef FEAT_STL_OPT
10556 "statusline",
10557#endif
10558#ifdef FEAT_SUN_WORKSHOP
10559 "sun_workshop",
10560#endif
10561#ifdef FEAT_NETBEANS_INTG
10562 "netbeans_intg",
10563#endif
10564#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010565 "spell",
10566#endif
10567#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 "syntax",
10569#endif
10570#if defined(USE_SYSTEM) || !defined(UNIX)
10571 "system",
10572#endif
10573#ifdef FEAT_TAG_BINS
10574 "tag_binary",
10575#endif
10576#ifdef FEAT_TAG_OLDSTATIC
10577 "tag_old_static",
10578#endif
10579#ifdef FEAT_TAG_ANYWHITE
10580 "tag_any_white",
10581#endif
10582#ifdef FEAT_TCL
10583# ifndef DYNAMIC_TCL
10584 "tcl",
10585# endif
10586#endif
10587#ifdef TERMINFO
10588 "terminfo",
10589#endif
10590#ifdef FEAT_TERMRESPONSE
10591 "termresponse",
10592#endif
10593#ifdef FEAT_TEXTOBJ
10594 "textobjects",
10595#endif
10596#ifdef HAVE_TGETENT
10597 "tgetent",
10598#endif
10599#ifdef FEAT_TITLE
10600 "title",
10601#endif
10602#ifdef FEAT_TOOLBAR
10603 "toolbar",
10604#endif
10605#ifdef FEAT_USR_CMDS
10606 "user-commands", /* was accidentally included in 5.4 */
10607 "user_commands",
10608#endif
10609#ifdef FEAT_VIMINFO
10610 "viminfo",
10611#endif
10612#ifdef FEAT_VERTSPLIT
10613 "vertsplit",
10614#endif
10615#ifdef FEAT_VIRTUALEDIT
10616 "virtualedit",
10617#endif
10618#ifdef FEAT_VISUAL
10619 "visual",
10620#endif
10621#ifdef FEAT_VISUALEXTRA
10622 "visualextra",
10623#endif
10624#ifdef FEAT_VREPLACE
10625 "vreplace",
10626#endif
10627#ifdef FEAT_WILDIGN
10628 "wildignore",
10629#endif
10630#ifdef FEAT_WILDMENU
10631 "wildmenu",
10632#endif
10633#ifdef FEAT_WINDOWS
10634 "windows",
10635#endif
10636#ifdef FEAT_WAK
10637 "winaltkeys",
10638#endif
10639#ifdef FEAT_WRITEBACKUP
10640 "writebackup",
10641#endif
10642#ifdef FEAT_XIM
10643 "xim",
10644#endif
10645#ifdef FEAT_XFONTSET
10646 "xfontset",
10647#endif
10648#ifdef USE_XSMP
10649 "xsmp",
10650#endif
10651#ifdef USE_XSMP_INTERACT
10652 "xsmp_interact",
10653#endif
10654#ifdef FEAT_XCLIPBOARD
10655 "xterm_clipboard",
10656#endif
10657#ifdef FEAT_XTERM_SAVE
10658 "xterm_save",
10659#endif
10660#if defined(UNIX) && defined(FEAT_X11)
10661 "X11",
10662#endif
10663 NULL
10664 };
10665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010666 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 for (i = 0; has_list[i] != NULL; ++i)
10668 if (STRICMP(name, has_list[i]) == 0)
10669 {
10670 n = TRUE;
10671 break;
10672 }
10673
10674 if (n == FALSE)
10675 {
10676 if (STRNICMP(name, "patch", 5) == 0)
10677 n = has_patch(atoi((char *)name + 5));
10678 else if (STRICMP(name, "vim_starting") == 0)
10679 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010680#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10681 else if (STRICMP(name, "balloon_multiline") == 0)
10682 n = multiline_balloon_available();
10683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684#ifdef DYNAMIC_TCL
10685 else if (STRICMP(name, "tcl") == 0)
10686 n = tcl_enabled(FALSE);
10687#endif
10688#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10689 else if (STRICMP(name, "iconv") == 0)
10690 n = iconv_enabled(FALSE);
10691#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010692#ifdef DYNAMIC_MZSCHEME
10693 else if (STRICMP(name, "mzscheme") == 0)
10694 n = mzscheme_enabled(FALSE);
10695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696#ifdef DYNAMIC_RUBY
10697 else if (STRICMP(name, "ruby") == 0)
10698 n = ruby_enabled(FALSE);
10699#endif
10700#ifdef DYNAMIC_PYTHON
10701 else if (STRICMP(name, "python") == 0)
10702 n = python_enabled(FALSE);
10703#endif
10704#ifdef DYNAMIC_PERL
10705 else if (STRICMP(name, "perl") == 0)
10706 n = perl_enabled(FALSE);
10707#endif
10708#ifdef FEAT_GUI
10709 else if (STRICMP(name, "gui_running") == 0)
10710 n = (gui.in_use || gui.starting);
10711# ifdef FEAT_GUI_W32
10712 else if (STRICMP(name, "gui_win32s") == 0)
10713 n = gui_is_win32s();
10714# endif
10715# ifdef FEAT_BROWSE
10716 else if (STRICMP(name, "browse") == 0)
10717 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10718# endif
10719#endif
10720#ifdef FEAT_SYN_HL
10721 else if (STRICMP(name, "syntax_items") == 0)
10722 n = syntax_present(curbuf);
10723#endif
10724#if defined(WIN3264)
10725 else if (STRICMP(name, "win95") == 0)
10726 n = mch_windows95();
10727#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010728#ifdef FEAT_NETBEANS_INTG
10729 else if (STRICMP(name, "netbeans_enabled") == 0)
10730 n = usingNetbeans;
10731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 }
10733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010734 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735}
10736
10737/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010738 * "has_key()" function
10739 */
10740 static void
10741f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010742 typval_T *argvars;
10743 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010744{
10745 rettv->vval.v_number = 0;
10746 if (argvars[0].v_type != VAR_DICT)
10747 {
10748 EMSG(_(e_dictreq));
10749 return;
10750 }
10751 if (argvars[0].vval.v_dict == NULL)
10752 return;
10753
10754 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010755 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010756}
10757
10758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 * "hasmapto()" function
10760 */
10761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010763 typval_T *argvars;
10764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765{
10766 char_u *name;
10767 char_u *mode;
10768 char_u buf[NUMBUFLEN];
10769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010771 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 mode = (char_u *)"nvo";
10773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775
10776 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780}
10781
10782/*
10783 * "histadd()" function
10784 */
10785/*ARGSUSED*/
10786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010788 typval_T *argvars;
10789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790{
10791#ifdef FEAT_CMDHIST
10792 int histype;
10793 char_u *str;
10794 char_u buf[NUMBUFLEN];
10795#endif
10796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 if (check_restricted() || check_secure())
10799 return;
10800#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010801 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10802 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 if (histype >= 0)
10804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (*str != NUL)
10807 {
10808 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 return;
10811 }
10812 }
10813#endif
10814}
10815
10816/*
10817 * "histdel()" function
10818 */
10819/*ARGSUSED*/
10820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010822 typval_T *argvars;
10823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824{
10825#ifdef FEAT_CMDHIST
10826 int n;
10827 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010830 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10831 if (str == NULL)
10832 n = 0;
10833 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010835 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010836 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 else
10841 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 get_tv_string_buf(&argvars[1], buf));
10844 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847#endif
10848}
10849
10850/*
10851 * "histget()" function
10852 */
10853/*ARGSUSED*/
10854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010856 typval_T *argvars;
10857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858{
10859#ifdef FEAT_CMDHIST
10860 int type;
10861 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010862 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010864 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10865 if (str == NULL)
10866 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 {
10869 type = get_histtype(str);
10870 if (argvars[1].v_type == VAR_UNKNOWN)
10871 idx = get_history_idx(type);
10872 else
10873 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10874 /* -1 on type error */
10875 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881}
10882
10883/*
10884 * "histnr()" function
10885 */
10886/*ARGSUSED*/
10887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010889 typval_T *argvars;
10890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891{
10892 int i;
10893
10894#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010895 char_u *history = get_tv_string_chk(&argvars[0]);
10896
10897 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 if (i >= HIST_CMD && i < HIST_COUNT)
10899 i = get_history_idx(i);
10900 else
10901#endif
10902 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904}
10905
10906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 * "highlightID(name)" function
10908 */
10909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010911 typval_T *argvars;
10912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915}
10916
10917/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010918 * "highlight_exists()" function
10919 */
10920 static void
10921f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010922 typval_T *argvars;
10923 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010924{
10925 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10926}
10927
10928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 * "hostname()" function
10930 */
10931/*ARGSUSED*/
10932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010934 typval_T *argvars;
10935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936{
10937 char_u hostname[256];
10938
10939 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940 rettv->v_type = VAR_STRING;
10941 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942}
10943
10944/*
10945 * iconv() function
10946 */
10947/*ARGSUSED*/
10948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010950 typval_T *argvars;
10951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
10953#ifdef FEAT_MBYTE
10954 char_u buf1[NUMBUFLEN];
10955 char_u buf2[NUMBUFLEN];
10956 char_u *from, *to, *str;
10957 vimconv_T vimconv;
10958#endif
10959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960 rettv->v_type = VAR_STRING;
10961 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962
10963#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 str = get_tv_string(&argvars[0]);
10965 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10966 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 vimconv.vc_type = CONV_NONE;
10968 convert_setup(&vimconv, from, to);
10969
10970 /* If the encodings are equal, no conversion needed. */
10971 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010972 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975
10976 convert_setup(&vimconv, NULL, NULL);
10977 vim_free(from);
10978 vim_free(to);
10979#endif
10980}
10981
10982/*
10983 * "indent()" function
10984 */
10985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010987 typval_T *argvars;
10988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989{
10990 linenr_T lnum;
10991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010994 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997}
10998
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010999/*
11000 * "index()" function
11001 */
11002 static void
11003f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011004 typval_T *argvars;
11005 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011006{
Bram Moolenaar33570922005-01-25 22:26:29 +000011007 list_T *l;
11008 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011009 long idx = 0;
11010 int ic = FALSE;
11011
11012 rettv->vval.v_number = -1;
11013 if (argvars[0].v_type != VAR_LIST)
11014 {
11015 EMSG(_(e_listreq));
11016 return;
11017 }
11018 l = argvars[0].vval.v_list;
11019 if (l != NULL)
11020 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011021 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011022 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 int error = FALSE;
11025
Bram Moolenaar758711c2005-02-02 23:11:38 +000011026 /* Start at specified item. Use the cached index that list_find()
11027 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011028 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011029 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011030 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011031 ic = get_tv_number_chk(&argvars[3], &error);
11032 if (error)
11033 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011034 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011035
Bram Moolenaar758711c2005-02-02 23:11:38 +000011036 for ( ; item != NULL; item = item->li_next, ++idx)
11037 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011038 {
11039 rettv->vval.v_number = idx;
11040 break;
11041 }
11042 }
11043}
11044
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045static int inputsecret_flag = 0;
11046
11047/*
11048 * "input()" function
11049 * Also handles inputsecret() when inputsecret is set.
11050 */
11051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011053 typval_T *argvars;
11054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 char_u *p = NULL;
11058 int c;
11059 char_u buf[NUMBUFLEN];
11060 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011062 int xp_type = EXPAND_NOTHING;
11063 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066
11067#ifdef NO_CONSOLE_INPUT
11068 /* While starting up, there is no place to enter text. */
11069 if (no_console_input())
11070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 return;
11073 }
11074#endif
11075
11076 cmd_silent = FALSE; /* Want to see the prompt. */
11077 if (prompt != NULL)
11078 {
11079 /* Only the part of the message after the last NL is considered as
11080 * prompt for the command line */
11081 p = vim_strrchr(prompt, '\n');
11082 if (p == NULL)
11083 p = prompt;
11084 else
11085 {
11086 ++p;
11087 c = *p;
11088 *p = NUL;
11089 msg_start();
11090 msg_clr_eos();
11091 msg_puts_attr(prompt, echo_attr);
11092 msg_didout = FALSE;
11093 msg_starthere();
11094 *p = c;
11095 }
11096 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011098 if (argvars[1].v_type != VAR_UNKNOWN)
11099 {
11100 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11101 if (defstr != NULL)
11102 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
Bram Moolenaar4463f292005-09-25 22:20:24 +000011104 if (argvars[2].v_type != VAR_UNKNOWN)
11105 {
11106 char_u *xp_name;
11107 int xp_namelen;
11108 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011109
Bram Moolenaar4463f292005-09-25 22:20:24 +000011110 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011111
Bram Moolenaar4463f292005-09-25 22:20:24 +000011112 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11113 if (xp_name == NULL)
11114 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011115
Bram Moolenaar4463f292005-09-25 22:20:24 +000011116 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011117
Bram Moolenaar4463f292005-09-25 22:20:24 +000011118 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11119 &xp_arg) == FAIL)
11120 return;
11121 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011122 }
11123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 if (defstr != NULL)
11125 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011126 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11127 xp_type, xp_arg);
11128
11129 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 /* since the user typed this, no need to wait for return */
11132 need_wait_return = FALSE;
11133 msg_didout = FALSE;
11134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 cmd_silent = cmd_silent_save;
11136}
11137
11138/*
11139 * "inputdialog()" function
11140 */
11141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011143 typval_T *argvars;
11144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145{
11146#if defined(FEAT_GUI_TEXTDIALOG)
11147 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11148 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11149 {
11150 char_u *message;
11151 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 message = get_tv_string_chk(&argvars[0]);
11155 if (argvars[1].v_type != VAR_UNKNOWN
11156 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011157 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 else
11159 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011160 if (message != NULL && defstr != NULL
11161 && do_dialog(VIM_QUESTION, NULL, message,
11162 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 else
11165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 if (message != NULL && defstr != NULL
11167 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011168 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011169 rettv->vval.v_string = vim_strsave(
11170 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 }
11176 else
11177#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179}
11180
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011181/*
11182 * "inputlist()" function
11183 */
11184 static void
11185f_inputlist(argvars, rettv)
11186 typval_T *argvars;
11187 typval_T *rettv;
11188{
11189 listitem_T *li;
11190 int selected;
11191 int mouse_used;
11192
11193 rettv->vval.v_number = 0;
11194#ifdef NO_CONSOLE_INPUT
11195 /* While starting up, there is no place to enter text. */
11196 if (no_console_input())
11197 return;
11198#endif
11199 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11200 {
11201 EMSG2(_(e_listarg), "inputlist()");
11202 return;
11203 }
11204
11205 msg_start();
11206 lines_left = Rows; /* avoid more prompt */
11207 msg_scroll = TRUE;
11208 msg_clr_eos();
11209
11210 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11211 {
11212 msg_puts(get_tv_string(&li->li_tv));
11213 msg_putchar('\n');
11214 }
11215
11216 /* Ask for choice. */
11217 selected = prompt_for_number(&mouse_used);
11218 if (mouse_used)
11219 selected -= lines_left;
11220
11221 rettv->vval.v_number = selected;
11222}
11223
11224
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11226
11227/*
11228 * "inputrestore()" function
11229 */
11230/*ARGSUSED*/
11231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011233 typval_T *argvars;
11234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235{
11236 if (ga_userinput.ga_len > 0)
11237 {
11238 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11240 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 }
11243 else if (p_verbose > 1)
11244 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011245 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 }
11248}
11249
11250/*
11251 * "inputsave()" function
11252 */
11253/*ARGSUSED*/
11254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011256 typval_T *argvars;
11257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258{
11259 /* Add an entry to the stack of typehead storage. */
11260 if (ga_grow(&ga_userinput, 1) == OK)
11261 {
11262 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11263 + ga_userinput.ga_len);
11264 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 }
11267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269}
11270
11271/*
11272 * "inputsecret()" function
11273 */
11274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011276 typval_T *argvars;
11277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278{
11279 ++cmdline_star;
11280 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 --cmdline_star;
11283 --inputsecret_flag;
11284}
11285
11286/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011287 * "insert()" function
11288 */
11289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011291 typval_T *argvars;
11292 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011293{
11294 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011295 listitem_T *item;
11296 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011297 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011298
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011299 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011300 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011301 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011302 else if ((l = argvars[0].vval.v_list) != NULL
11303 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011304 {
11305 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306 before = get_tv_number_chk(&argvars[2], &error);
11307 if (error)
11308 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011309
Bram Moolenaar758711c2005-02-02 23:11:38 +000011310 if (before == l->lv_len)
11311 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011312 else
11313 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011314 item = list_find(l, before);
11315 if (item == NULL)
11316 {
11317 EMSGN(_(e_listidx), before);
11318 l = NULL;
11319 }
11320 }
11321 if (l != NULL)
11322 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011323 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011324 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011325 }
11326 }
11327}
11328
11329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 * "isdirectory()" function
11331 */
11332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011334 typval_T *argvars;
11335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338}
11339
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011340/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011341 * "islocked()" function
11342 */
11343 static void
11344f_islocked(argvars, rettv)
11345 typval_T *argvars;
11346 typval_T *rettv;
11347{
11348 lval_T lv;
11349 char_u *end;
11350 dictitem_T *di;
11351
11352 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011353 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11354 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011355 if (end != NULL && lv.ll_name != NULL)
11356 {
11357 if (*end != NUL)
11358 EMSG(_(e_trailing));
11359 else
11360 {
11361 if (lv.ll_tv == NULL)
11362 {
11363 if (check_changedtick(lv.ll_name))
11364 rettv->vval.v_number = 1; /* always locked */
11365 else
11366 {
11367 di = find_var(lv.ll_name, NULL);
11368 if (di != NULL)
11369 {
11370 /* Consider a variable locked when:
11371 * 1. the variable itself is locked
11372 * 2. the value of the variable is locked.
11373 * 3. the List or Dict value is locked.
11374 */
11375 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11376 || tv_islocked(&di->di_tv));
11377 }
11378 }
11379 }
11380 else if (lv.ll_range)
11381 EMSG(_("E745: Range not allowed"));
11382 else if (lv.ll_newkey != NULL)
11383 EMSG2(_(e_dictkey), lv.ll_newkey);
11384 else if (lv.ll_list != NULL)
11385 /* List item. */
11386 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11387 else
11388 /* Dictionary item. */
11389 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11390 }
11391 }
11392
11393 clear_lval(&lv);
11394}
11395
Bram Moolenaar33570922005-01-25 22:26:29 +000011396static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011397
11398/*
11399 * Turn a dict into a list:
11400 * "what" == 0: list of keys
11401 * "what" == 1: list of values
11402 * "what" == 2: list of items
11403 */
11404 static void
11405dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011406 typval_T *argvars;
11407 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011408 int what;
11409{
Bram Moolenaar33570922005-01-25 22:26:29 +000011410 list_T *l2;
11411 dictitem_T *di;
11412 hashitem_T *hi;
11413 listitem_T *li;
11414 listitem_T *li2;
11415 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011416 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011417
11418 rettv->vval.v_number = 0;
11419 if (argvars[0].v_type != VAR_DICT)
11420 {
11421 EMSG(_(e_dictreq));
11422 return;
11423 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011424 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011425 return;
11426
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011427 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011428 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011429
Bram Moolenaar33570922005-01-25 22:26:29 +000011430 todo = d->dv_hashtab.ht_used;
11431 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011433 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011435 --todo;
11436 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011437
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011438 li = listitem_alloc();
11439 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011440 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011441 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011442
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011443 if (what == 0)
11444 {
11445 /* keys() */
11446 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011447 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011448 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11449 }
11450 else if (what == 1)
11451 {
11452 /* values() */
11453 copy_tv(&di->di_tv, &li->li_tv);
11454 }
11455 else
11456 {
11457 /* items() */
11458 l2 = list_alloc();
11459 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011460 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011461 li->li_tv.vval.v_list = l2;
11462 if (l2 == NULL)
11463 break;
11464 ++l2->lv_refcount;
11465
11466 li2 = listitem_alloc();
11467 if (li2 == NULL)
11468 break;
11469 list_append(l2, li2);
11470 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011471 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011472 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11473
11474 li2 = listitem_alloc();
11475 if (li2 == NULL)
11476 break;
11477 list_append(l2, li2);
11478 copy_tv(&di->di_tv, &li2->li_tv);
11479 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011480 }
11481 }
11482}
11483
11484/*
11485 * "items(dict)" function
11486 */
11487 static void
11488f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011489 typval_T *argvars;
11490 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011491{
11492 dict_list(argvars, rettv, 2);
11493}
11494
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011496 * "join()" function
11497 */
11498 static void
11499f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011500 typval_T *argvars;
11501 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011502{
11503 garray_T ga;
11504 char_u *sep;
11505
11506 rettv->vval.v_number = 0;
11507 if (argvars[0].v_type != VAR_LIST)
11508 {
11509 EMSG(_(e_listreq));
11510 return;
11511 }
11512 if (argvars[0].vval.v_list == NULL)
11513 return;
11514 if (argvars[1].v_type == VAR_UNKNOWN)
11515 sep = (char_u *)" ";
11516 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011518
11519 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011520
11521 if (sep != NULL)
11522 {
11523 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011524 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525 ga_append(&ga, NUL);
11526 rettv->vval.v_string = (char_u *)ga.ga_data;
11527 }
11528 else
11529 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011530}
11531
11532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011533 * "keys()" function
11534 */
11535 static void
11536f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011537 typval_T *argvars;
11538 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011539{
11540 dict_list(argvars, rettv, 0);
11541}
11542
11543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 * "last_buffer_nr()" function.
11545 */
11546/*ARGSUSED*/
11547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 typval_T *argvars;
11550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551{
11552 int n = 0;
11553 buf_T *buf;
11554
11555 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11556 if (n < buf->b_fnum)
11557 n = buf->b_fnum;
11558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011559 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011560}
11561
11562/*
11563 * "len()" function
11564 */
11565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011567 typval_T *argvars;
11568 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011569{
11570 switch (argvars[0].v_type)
11571 {
11572 case VAR_STRING:
11573 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 rettv->vval.v_number = (varnumber_T)STRLEN(
11575 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011576 break;
11577 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011579 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011580 case VAR_DICT:
11581 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11582 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011583 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011584 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011585 break;
11586 }
11587}
11588
Bram Moolenaar33570922005-01-25 22:26:29 +000011589static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011590
11591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011593 typval_T *argvars;
11594 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011595 int type;
11596{
11597#ifdef FEAT_LIBCALL
11598 char_u *string_in;
11599 char_u **string_result;
11600 int nr_result;
11601#endif
11602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011603 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011606 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011608
11609 if (check_restricted() || check_secure())
11610 return;
11611
11612#ifdef FEAT_LIBCALL
11613 /* The first two args must be strings, otherwise its meaningless */
11614 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11615 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011616 string_in = NULL;
11617 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011618 string_in = argvars[2].vval.v_string;
11619 if (type == VAR_NUMBER)
11620 string_result = NULL;
11621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011623 if (mch_libcall(argvars[0].vval.v_string,
11624 argvars[1].vval.v_string,
11625 string_in,
11626 argvars[2].vval.v_number,
11627 string_result,
11628 &nr_result) == OK
11629 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011631 }
11632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633}
11634
11635/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011636 * "libcall()" function
11637 */
11638 static void
11639f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011640 typval_T *argvars;
11641 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011642{
11643 libcall_common(argvars, rettv, VAR_STRING);
11644}
11645
11646/*
11647 * "libcallnr()" function
11648 */
11649 static void
11650f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011651 typval_T *argvars;
11652 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011653{
11654 libcall_common(argvars, rettv, VAR_NUMBER);
11655}
11656
11657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 * "line(string)" function
11659 */
11660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011662 typval_T *argvars;
11663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664{
11665 linenr_T lnum = 0;
11666 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011667 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011669 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 if (fp != NULL)
11671 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011672 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673}
11674
11675/*
11676 * "line2byte(lnum)" function
11677 */
11678/*ARGSUSED*/
11679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 typval_T *argvars;
11682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683{
11684#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686#else
11687 linenr_T lnum;
11688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011693 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11694 if (rettv->vval.v_number >= 0)
11695 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696#endif
11697}
11698
11699/*
11700 * "lispindent(lnum)" function
11701 */
11702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011703f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011704 typval_T *argvars;
11705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706{
11707#ifdef FEAT_LISP
11708 pos_T pos;
11709 linenr_T lnum;
11710
11711 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011712 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11714 {
11715 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 curwin->w_cursor = pos;
11718 }
11719 else
11720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011721 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722}
11723
11724/*
11725 * "localtime()" function
11726 */
11727/*ARGSUSED*/
11728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011730 typval_T *argvars;
11731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734}
11735
Bram Moolenaar33570922005-01-25 22:26:29 +000011736static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737
11738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011740 typval_T *argvars;
11741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742 int exact;
11743{
11744 char_u *keys;
11745 char_u *which;
11746 char_u buf[NUMBUFLEN];
11747 char_u *keys_buf = NULL;
11748 char_u *rhs;
11749 int mode;
11750 garray_T ga;
11751
11752 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->v_type = VAR_STRING;
11754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011756 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 if (*keys == NUL)
11758 return;
11759
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011760 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011761 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 else
11763 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 if (which == NULL)
11765 return;
11766
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 mode = get_map_mode(&which, 0);
11768
11769 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011770 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771 vim_free(keys_buf);
11772 if (rhs != NULL)
11773 {
11774 ga_init(&ga);
11775 ga.ga_itemsize = 1;
11776 ga.ga_growsize = 40;
11777
11778 while (*rhs != NUL)
11779 ga_concat(&ga, str2special(&rhs, FALSE));
11780
11781 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 }
11784}
11785
11786/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011787 * "map()" function
11788 */
11789 static void
11790f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011791 typval_T *argvars;
11792 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011793{
11794 filter_map(argvars, rettv, TRUE);
11795}
11796
11797/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011798 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 */
11800 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011801f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011802 typval_T *argvars;
11803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011805 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806}
11807
11808/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011809 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 */
11811 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011812f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011813 typval_T *argvars;
11814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011816 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817}
11818
Bram Moolenaar33570922005-01-25 22:26:29 +000011819static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820
11821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011823 typval_T *argvars;
11824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 int type;
11826{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011827 char_u *str = NULL;
11828 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 char_u *pat;
11830 regmatch_T regmatch;
11831 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011832 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 char_u *save_cpo;
11834 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011835 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011836 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011837 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011838 list_T *l = NULL;
11839 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011840 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011841 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842
11843 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11844 save_cpo = p_cpo;
11845 p_cpo = (char_u *)"";
11846
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011847 rettv->vval.v_number = -1;
11848 if (type == 3)
11849 {
11850 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011851 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011852 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011853 }
11854 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 rettv->v_type = VAR_STRING;
11857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011860 if (argvars[0].v_type == VAR_LIST)
11861 {
11862 if ((l = argvars[0].vval.v_list) == NULL)
11863 goto theend;
11864 li = l->lv_first;
11865 }
11866 else
11867 expr = str = get_tv_string(&argvars[0]);
11868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011869 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11870 if (pat == NULL)
11871 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011872
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011875 int error = FALSE;
11876
11877 start = get_tv_number_chk(&argvars[2], &error);
11878 if (error)
11879 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011880 if (l != NULL)
11881 {
11882 li = list_find(l, start);
11883 if (li == NULL)
11884 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011885 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011886 }
11887 else
11888 {
11889 if (start < 0)
11890 start = 0;
11891 if (start > (long)STRLEN(str))
11892 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011893 /* When "count" argument is there ignore matches before "start",
11894 * otherwise skip part of the string. Differs when pattern is "^"
11895 * or "\<". */
11896 if (argvars[3].v_type != VAR_UNKNOWN)
11897 startcol = start;
11898 else
11899 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011900 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011901
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011902 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 nth = get_tv_number_chk(&argvars[3], &error);
11904 if (error)
11905 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 }
11907
11908 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11909 if (regmatch.regprog != NULL)
11910 {
11911 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011912
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011913 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011914 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011915 if (l != NULL)
11916 {
11917 if (li == NULL)
11918 {
11919 match = FALSE;
11920 break;
11921 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011922 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011923 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011924 if (str == NULL)
11925 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011926 }
11927
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011928 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011929
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011930 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011931 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011932 if (l == NULL && !match)
11933 break;
11934
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011935 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011936 if (l != NULL)
11937 {
11938 li = li->li_next;
11939 ++idx;
11940 }
11941 else
11942 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011943#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011944 startcol = regmatch.startp[0]
11945 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011946#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011947 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011948#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011949 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011950 }
11951
11952 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011954 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011955 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011956 int i;
11957
11958 /* return list with matched string and submatches */
11959 for (i = 0; i < NSUBEXP; ++i)
11960 {
11961 if (regmatch.endp[i] == NULL)
11962 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011963 if (list_append_string(rettv->vval.v_list,
11964 regmatch.startp[i],
11965 (int)(regmatch.endp[i] - regmatch.startp[i]))
11966 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011967 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011968 }
11969 }
11970 else if (type == 2)
11971 {
11972 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011973 if (l != NULL)
11974 copy_tv(&li->li_tv, rettv);
11975 else
11976 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011978 }
11979 else if (l != NULL)
11980 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 else
11982 {
11983 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 (varnumber_T)(regmatch.startp[0] - str);
11986 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 }
11991 }
11992 vim_free(regmatch.regprog);
11993 }
11994
11995theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011996 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 p_cpo = save_cpo;
11998}
11999
12000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012001 * "match()" function
12002 */
12003 static void
12004f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012005 typval_T *argvars;
12006 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012007{
12008 find_some_match(argvars, rettv, 1);
12009}
12010
12011/*
12012 * "matchend()" function
12013 */
12014 static void
12015f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012016 typval_T *argvars;
12017 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012018{
12019 find_some_match(argvars, rettv, 0);
12020}
12021
12022/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012023 * "matchlist()" function
12024 */
12025 static void
12026f_matchlist(argvars, rettv)
12027 typval_T *argvars;
12028 typval_T *rettv;
12029{
12030 find_some_match(argvars, rettv, 3);
12031}
12032
12033/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012034 * "matchstr()" function
12035 */
12036 static void
12037f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012038 typval_T *argvars;
12039 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012040{
12041 find_some_match(argvars, rettv, 2);
12042}
12043
Bram Moolenaar33570922005-01-25 22:26:29 +000012044static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012045
12046 static void
12047max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012048 typval_T *argvars;
12049 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012050 int domax;
12051{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012052 long n = 0;
12053 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012054 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012055
12056 if (argvars[0].v_type == VAR_LIST)
12057 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012058 list_T *l;
12059 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012060
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012061 l = argvars[0].vval.v_list;
12062 if (l != NULL)
12063 {
12064 li = l->lv_first;
12065 if (li != NULL)
12066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012068 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012069 {
12070 li = li->li_next;
12071 if (li == NULL)
12072 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012074 if (domax ? i > n : i < n)
12075 n = i;
12076 }
12077 }
12078 }
12079 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012080 else if (argvars[0].v_type == VAR_DICT)
12081 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012082 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012083 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012084 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012085 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012086
12087 d = argvars[0].vval.v_dict;
12088 if (d != NULL)
12089 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012090 todo = d->dv_hashtab.ht_used;
12091 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012092 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012093 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012094 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012095 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012096 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012097 if (first)
12098 {
12099 n = i;
12100 first = FALSE;
12101 }
12102 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012103 n = i;
12104 }
12105 }
12106 }
12107 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012108 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012109 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012110 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012111}
12112
12113/*
12114 * "max()" function
12115 */
12116 static void
12117f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012118 typval_T *argvars;
12119 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012120{
12121 max_min(argvars, rettv, TRUE);
12122}
12123
12124/*
12125 * "min()" function
12126 */
12127 static void
12128f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012129 typval_T *argvars;
12130 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012131{
12132 max_min(argvars, rettv, FALSE);
12133}
12134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012135static int mkdir_recurse __ARGS((char_u *dir, int prot));
12136
12137/*
12138 * Create the directory in which "dir" is located, and higher levels when
12139 * needed.
12140 */
12141 static int
12142mkdir_recurse(dir, prot)
12143 char_u *dir;
12144 int prot;
12145{
12146 char_u *p;
12147 char_u *updir;
12148 int r = FAIL;
12149
12150 /* Get end of directory name in "dir".
12151 * We're done when it's "/" or "c:/". */
12152 p = gettail_sep(dir);
12153 if (p <= get_past_head(dir))
12154 return OK;
12155
12156 /* If the directory exists we're done. Otherwise: create it.*/
12157 updir = vim_strnsave(dir, (int)(p - dir));
12158 if (updir == NULL)
12159 return FAIL;
12160 if (mch_isdir(updir))
12161 r = OK;
12162 else if (mkdir_recurse(updir, prot) == OK)
12163 r = vim_mkdir_emsg(updir, prot);
12164 vim_free(updir);
12165 return r;
12166}
12167
12168#ifdef vim_mkdir
12169/*
12170 * "mkdir()" function
12171 */
12172 static void
12173f_mkdir(argvars, rettv)
12174 typval_T *argvars;
12175 typval_T *rettv;
12176{
12177 char_u *dir;
12178 char_u buf[NUMBUFLEN];
12179 int prot = 0755;
12180
12181 rettv->vval.v_number = FAIL;
12182 if (check_restricted() || check_secure())
12183 return;
12184
12185 dir = get_tv_string_buf(&argvars[0], buf);
12186 if (argvars[1].v_type != VAR_UNKNOWN)
12187 {
12188 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012189 prot = get_tv_number_chk(&argvars[2], NULL);
12190 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012191 mkdir_recurse(dir, prot);
12192 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012193 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012194}
12195#endif
12196
Bram Moolenaar0d660222005-01-07 21:51:51 +000012197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 * "mode()" function
12199 */
12200/*ARGSUSED*/
12201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012203 typval_T *argvars;
12204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205{
12206 char_u buf[2];
12207
12208#ifdef FEAT_VISUAL
12209 if (VIsual_active)
12210 {
12211 if (VIsual_select)
12212 buf[0] = VIsual_mode + 's' - 'v';
12213 else
12214 buf[0] = VIsual_mode;
12215 }
12216 else
12217#endif
12218 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12219 buf[0] = 'r';
12220 else if (State & INSERT)
12221 {
12222 if (State & REPLACE_FLAG)
12223 buf[0] = 'R';
12224 else
12225 buf[0] = 'i';
12226 }
12227 else if (State & CMDLINE)
12228 buf[0] = 'c';
12229 else
12230 buf[0] = 'n';
12231
12232 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->vval.v_string = vim_strsave(buf);
12234 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235}
12236
12237/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012238 * "nextnonblank()" function
12239 */
12240 static void
12241f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012242 typval_T *argvars;
12243 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012244{
12245 linenr_T lnum;
12246
12247 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012249 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012250 {
12251 lnum = 0;
12252 break;
12253 }
12254 if (*skipwhite(ml_get(lnum)) != NUL)
12255 break;
12256 }
12257 rettv->vval.v_number = lnum;
12258}
12259
12260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 * "nr2char()" function
12262 */
12263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012265 typval_T *argvars;
12266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267{
12268 char_u buf[NUMBUFLEN];
12269
12270#ifdef FEAT_MBYTE
12271 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 else
12274#endif
12275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012276 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 buf[1] = NUL;
12278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 rettv->v_type = VAR_STRING;
12280 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012281}
12282
12283/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012284 * "prevnonblank()" function
12285 */
12286 static void
12287f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012288 typval_T *argvars;
12289 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012290{
12291 linenr_T lnum;
12292
12293 lnum = get_tv_lnum(argvars);
12294 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12295 lnum = 0;
12296 else
12297 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12298 --lnum;
12299 rettv->vval.v_number = lnum;
12300}
12301
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012302#ifdef HAVE_STDARG_H
12303/* This dummy va_list is here because:
12304 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12305 * - locally in the function results in a "used before set" warning
12306 * - using va_start() to initialize it gives "function with fixed args" error */
12307static va_list ap;
12308#endif
12309
Bram Moolenaar8c711452005-01-14 21:53:12 +000012310/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012311 * "printf()" function
12312 */
12313 static void
12314f_printf(argvars, rettv)
12315 typval_T *argvars;
12316 typval_T *rettv;
12317{
12318 rettv->v_type = VAR_STRING;
12319 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012320#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012321 {
12322 char_u buf[NUMBUFLEN];
12323 int len;
12324 char_u *s;
12325 int saved_did_emsg = did_emsg;
12326 char *fmt;
12327
12328 /* Get the required length, allocate the buffer and do it for real. */
12329 did_emsg = FALSE;
12330 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012331 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012332 if (!did_emsg)
12333 {
12334 s = alloc(len + 1);
12335 if (s != NULL)
12336 {
12337 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012338 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012339 }
12340 }
12341 did_emsg |= saved_did_emsg;
12342 }
12343#endif
12344}
12345
12346/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012347 * "pumvisible()" function
12348 */
12349/*ARGSUSED*/
12350 static void
12351f_pumvisible(argvars, rettv)
12352 typval_T *argvars;
12353 typval_T *rettv;
12354{
12355 rettv->vval.v_number = 0;
12356#ifdef FEAT_INS_EXPAND
12357 if (pum_visible())
12358 rettv->vval.v_number = 1;
12359#endif
12360}
12361
12362/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012363 * "range()" function
12364 */
12365 static void
12366f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012367 typval_T *argvars;
12368 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012369{
12370 long start;
12371 long end;
12372 long stride = 1;
12373 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012374 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012376 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012377 if (argvars[1].v_type == VAR_UNKNOWN)
12378 {
12379 end = start - 1;
12380 start = 0;
12381 }
12382 else
12383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012385 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012386 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012387 }
12388
12389 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 if (error)
12391 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012392 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012393 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012394 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012395 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012396 else
12397 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012398 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012399 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012400 if (list_append_number(rettv->vval.v_list,
12401 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012402 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012403 }
12404}
12405
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012406/*
12407 * "readfile()" function
12408 */
12409 static void
12410f_readfile(argvars, rettv)
12411 typval_T *argvars;
12412 typval_T *rettv;
12413{
12414 int binary = FALSE;
12415 char_u *fname;
12416 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012417 listitem_T *li;
12418#define FREAD_SIZE 200 /* optimized for text lines */
12419 char_u buf[FREAD_SIZE];
12420 int readlen; /* size of last fread() */
12421 int buflen; /* nr of valid chars in buf[] */
12422 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12423 int tolist; /* first byte in buf[] still to be put in list */
12424 int chop; /* how many CR to chop off */
12425 char_u *prev = NULL; /* previously read bytes, if any */
12426 int prevlen = 0; /* length of "prev" if not NULL */
12427 char_u *s;
12428 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012429 long maxline = MAXLNUM;
12430 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012431
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012432 if (argvars[1].v_type != VAR_UNKNOWN)
12433 {
12434 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12435 binary = TRUE;
12436 if (argvars[2].v_type != VAR_UNKNOWN)
12437 maxline = get_tv_number(&argvars[2]);
12438 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012439
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012440 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012441 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012442
12443 /* Always open the file in binary mode, library functions have a mind of
12444 * their own about CR-LF conversion. */
12445 fname = get_tv_string(&argvars[0]);
12446 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12447 {
12448 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12449 return;
12450 }
12451
12452 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012453 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012454 {
12455 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12456 buflen = filtd + readlen;
12457 tolist = 0;
12458 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12459 {
12460 if (buf[filtd] == '\n' || readlen <= 0)
12461 {
12462 /* Only when in binary mode add an empty list item when the
12463 * last line ends in a '\n'. */
12464 if (!binary && readlen == 0 && filtd == 0)
12465 break;
12466
12467 /* Found end-of-line or end-of-file: add a text line to the
12468 * list. */
12469 chop = 0;
12470 if (!binary)
12471 while (filtd - chop - 1 >= tolist
12472 && buf[filtd - chop - 1] == '\r')
12473 ++chop;
12474 len = filtd - tolist - chop;
12475 if (prev == NULL)
12476 s = vim_strnsave(buf + tolist, len);
12477 else
12478 {
12479 s = alloc((unsigned)(prevlen + len + 1));
12480 if (s != NULL)
12481 {
12482 mch_memmove(s, prev, prevlen);
12483 vim_free(prev);
12484 prev = NULL;
12485 mch_memmove(s + prevlen, buf + tolist, len);
12486 s[prevlen + len] = NUL;
12487 }
12488 }
12489 tolist = filtd + 1;
12490
12491 li = listitem_alloc();
12492 if (li == NULL)
12493 {
12494 vim_free(s);
12495 break;
12496 }
12497 li->li_tv.v_type = VAR_STRING;
12498 li->li_tv.v_lock = 0;
12499 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012500 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012501
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012502 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012503 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012504 if (readlen <= 0)
12505 break;
12506 }
12507 else if (buf[filtd] == NUL)
12508 buf[filtd] = '\n';
12509 }
12510 if (readlen <= 0)
12511 break;
12512
12513 if (tolist == 0)
12514 {
12515 /* "buf" is full, need to move text to an allocated buffer */
12516 if (prev == NULL)
12517 {
12518 prev = vim_strnsave(buf, buflen);
12519 prevlen = buflen;
12520 }
12521 else
12522 {
12523 s = alloc((unsigned)(prevlen + buflen));
12524 if (s != NULL)
12525 {
12526 mch_memmove(s, prev, prevlen);
12527 mch_memmove(s + prevlen, buf, buflen);
12528 vim_free(prev);
12529 prev = s;
12530 prevlen += buflen;
12531 }
12532 }
12533 filtd = 0;
12534 }
12535 else
12536 {
12537 mch_memmove(buf, buf + tolist, buflen - tolist);
12538 filtd -= tolist;
12539 }
12540 }
12541
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012542 /*
12543 * For a negative line count use only the lines at the end of the file,
12544 * free the rest.
12545 */
12546 if (maxline < 0)
12547 while (cnt > -maxline)
12548 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012549 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012550 --cnt;
12551 }
12552
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012553 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012554 fclose(fd);
12555}
12556
12557
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12559static void make_connection __ARGS((void));
12560static int check_connection __ARGS((void));
12561
12562 static void
12563make_connection()
12564{
12565 if (X_DISPLAY == NULL
12566# ifdef FEAT_GUI
12567 && !gui.in_use
12568# endif
12569 )
12570 {
12571 x_force_connect = TRUE;
12572 setup_term_clip();
12573 x_force_connect = FALSE;
12574 }
12575}
12576
12577 static int
12578check_connection()
12579{
12580 make_connection();
12581 if (X_DISPLAY == NULL)
12582 {
12583 EMSG(_("E240: No connection to Vim server"));
12584 return FAIL;
12585 }
12586 return OK;
12587}
12588#endif
12589
12590#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012591static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012592
12593 static void
12594remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012595 typval_T *argvars;
12596 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012597 int expr;
12598{
12599 char_u *server_name;
12600 char_u *keys;
12601 char_u *r = NULL;
12602 char_u buf[NUMBUFLEN];
12603# ifdef WIN32
12604 HWND w;
12605# else
12606 Window w;
12607# endif
12608
12609 if (check_restricted() || check_secure())
12610 return;
12611
12612# ifdef FEAT_X11
12613 if (check_connection() == FAIL)
12614 return;
12615# endif
12616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012617 server_name = get_tv_string_chk(&argvars[0]);
12618 if (server_name == NULL)
12619 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012620 keys = get_tv_string_buf(&argvars[1], buf);
12621# ifdef WIN32
12622 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12623# else
12624 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12625 < 0)
12626# endif
12627 {
12628 if (r != NULL)
12629 EMSG(r); /* sending worked but evaluation failed */
12630 else
12631 EMSG2(_("E241: Unable to send to %s"), server_name);
12632 return;
12633 }
12634
12635 rettv->vval.v_string = r;
12636
12637 if (argvars[2].v_type != VAR_UNKNOWN)
12638 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012640 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012642
12643 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 v.di_tv.v_type = VAR_STRING;
12645 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646 idvar = get_tv_string_chk(&argvars[2]);
12647 if (idvar != NULL)
12648 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012649 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012650 }
12651}
12652#endif
12653
12654/*
12655 * "remote_expr()" function
12656 */
12657/*ARGSUSED*/
12658 static void
12659f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012660 typval_T *argvars;
12661 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012662{
12663 rettv->v_type = VAR_STRING;
12664 rettv->vval.v_string = NULL;
12665#ifdef FEAT_CLIENTSERVER
12666 remote_common(argvars, rettv, TRUE);
12667#endif
12668}
12669
12670/*
12671 * "remote_foreground()" function
12672 */
12673/*ARGSUSED*/
12674 static void
12675f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *argvars;
12677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012678{
12679 rettv->vval.v_number = 0;
12680#ifdef FEAT_CLIENTSERVER
12681# ifdef WIN32
12682 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 {
12684 char_u *server_name = get_tv_string_chk(&argvars[0]);
12685
12686 if (server_name != NULL)
12687 serverForeground(server_name);
12688 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012689# else
12690 /* Send a foreground() expression to the server. */
12691 argvars[1].v_type = VAR_STRING;
12692 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12693 argvars[2].v_type = VAR_UNKNOWN;
12694 remote_common(argvars, rettv, TRUE);
12695 vim_free(argvars[1].vval.v_string);
12696# endif
12697#endif
12698}
12699
12700/*ARGSUSED*/
12701 static void
12702f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012703 typval_T *argvars;
12704 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012705{
12706#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012707 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012708 char_u *s = NULL;
12709# ifdef WIN32
12710 int n = 0;
12711# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012712 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012713
12714 if (check_restricted() || check_secure())
12715 {
12716 rettv->vval.v_number = -1;
12717 return;
12718 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 serverid = get_tv_string_chk(&argvars[0]);
12720 if (serverid == NULL)
12721 {
12722 rettv->vval.v_number = -1;
12723 return; /* type error; errmsg already given */
12724 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012725# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012726 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012727 if (n == 0)
12728 rettv->vval.v_number = -1;
12729 else
12730 {
12731 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12732 rettv->vval.v_number = (s != NULL);
12733 }
12734# else
12735 rettv->vval.v_number = 0;
12736 if (check_connection() == FAIL)
12737 return;
12738
12739 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012740 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012741# endif
12742
12743 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 char_u *retvar;
12746
Bram Moolenaar33570922005-01-25 22:26:29 +000012747 v.di_tv.v_type = VAR_STRING;
12748 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012749 retvar = get_tv_string_chk(&argvars[1]);
12750 if (retvar != NULL)
12751 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012752 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012753 }
12754#else
12755 rettv->vval.v_number = -1;
12756#endif
12757}
12758
12759/*ARGSUSED*/
12760 static void
12761f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 typval_T *argvars;
12763 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012764{
12765 char_u *r = NULL;
12766
12767#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012768 char_u *serverid = get_tv_string_chk(&argvars[0]);
12769
12770 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012771 {
12772# ifdef WIN32
12773 /* The server's HWND is encoded in the 'id' parameter */
12774 int n = 0;
12775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012777 if (n != 0)
12778 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12779 if (r == NULL)
12780# else
12781 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012782 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012783# endif
12784 EMSG(_("E277: Unable to read a server reply"));
12785 }
12786#endif
12787 rettv->v_type = VAR_STRING;
12788 rettv->vval.v_string = r;
12789}
12790
12791/*
12792 * "remote_send()" function
12793 */
12794/*ARGSUSED*/
12795 static void
12796f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012797 typval_T *argvars;
12798 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012799{
12800 rettv->v_type = VAR_STRING;
12801 rettv->vval.v_string = NULL;
12802#ifdef FEAT_CLIENTSERVER
12803 remote_common(argvars, rettv, FALSE);
12804#endif
12805}
12806
12807/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012808 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012809 */
12810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012812 typval_T *argvars;
12813 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012814{
Bram Moolenaar33570922005-01-25 22:26:29 +000012815 list_T *l;
12816 listitem_T *item, *item2;
12817 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012818 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012819 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012820 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012821 dict_T *d;
12822 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012823
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012824 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012825 if (argvars[0].v_type == VAR_DICT)
12826 {
12827 if (argvars[2].v_type != VAR_UNKNOWN)
12828 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012829 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012830 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012832 key = get_tv_string_chk(&argvars[1]);
12833 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012835 di = dict_find(d, key, -1);
12836 if (di == NULL)
12837 EMSG2(_(e_dictkey), key);
12838 else
12839 {
12840 *rettv = di->di_tv;
12841 init_tv(&di->di_tv);
12842 dictitem_remove(d, di);
12843 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012844 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012845 }
12846 }
12847 else if (argvars[0].v_type != VAR_LIST)
12848 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012849 else if ((l = argvars[0].vval.v_list) != NULL
12850 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012852 int error = FALSE;
12853
12854 idx = get_tv_number_chk(&argvars[1], &error);
12855 if (error)
12856 ; /* type error: do nothing, errmsg already given */
12857 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012858 EMSGN(_(e_listidx), idx);
12859 else
12860 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012861 if (argvars[2].v_type == VAR_UNKNOWN)
12862 {
12863 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012864 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012865 *rettv = item->li_tv;
12866 vim_free(item);
12867 }
12868 else
12869 {
12870 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 end = get_tv_number_chk(&argvars[2], &error);
12872 if (error)
12873 ; /* type error: do nothing */
12874 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012875 EMSGN(_(e_listidx), end);
12876 else
12877 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012878 int cnt = 0;
12879
12880 for (li = item; li != NULL; li = li->li_next)
12881 {
12882 ++cnt;
12883 if (li == item2)
12884 break;
12885 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012886 if (li == NULL) /* didn't find "item2" after "item" */
12887 EMSG(_(e_invrange));
12888 else
12889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012890 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012891 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012892 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012893 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012894 l->lv_first = item;
12895 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012896 item->li_prev = NULL;
12897 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012898 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012899 }
12900 }
12901 }
12902 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012903 }
12904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905}
12906
12907/*
12908 * "rename({from}, {to})" function
12909 */
12910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012912 typval_T *argvars;
12913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914{
12915 char_u buf[NUMBUFLEN];
12916
12917 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12921 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922}
12923
12924/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012925 * "repeat()" function
12926 */
12927/*ARGSUSED*/
12928 static void
12929f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 typval_T *argvars;
12931 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012932{
12933 char_u *p;
12934 int n;
12935 int slen;
12936 int len;
12937 char_u *r;
12938 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012939
12940 n = get_tv_number(&argvars[1]);
12941 if (argvars[0].v_type == VAR_LIST)
12942 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012943 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012944 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012945 if (list_extend(rettv->vval.v_list,
12946 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012947 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012948 }
12949 else
12950 {
12951 p = get_tv_string(&argvars[0]);
12952 rettv->v_type = VAR_STRING;
12953 rettv->vval.v_string = NULL;
12954
12955 slen = (int)STRLEN(p);
12956 len = slen * n;
12957 if (len <= 0)
12958 return;
12959
12960 r = alloc(len + 1);
12961 if (r != NULL)
12962 {
12963 for (i = 0; i < n; i++)
12964 mch_memmove(r + i * slen, p, (size_t)slen);
12965 r[len] = NUL;
12966 }
12967
12968 rettv->vval.v_string = r;
12969 }
12970}
12971
12972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973 * "resolve()" function
12974 */
12975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012976f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012977 typval_T *argvars;
12978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979{
12980 char_u *p;
12981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983#ifdef FEAT_SHORTCUT
12984 {
12985 char_u *v = NULL;
12986
12987 v = mch_resolve_shortcut(p);
12988 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012989 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012991 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 }
12993#else
12994# ifdef HAVE_READLINK
12995 {
12996 char_u buf[MAXPATHL + 1];
12997 char_u *cpy;
12998 int len;
12999 char_u *remain = NULL;
13000 char_u *q;
13001 int is_relative_to_current = FALSE;
13002 int has_trailing_pathsep = FALSE;
13003 int limit = 100;
13004
13005 p = vim_strsave(p);
13006
13007 if (p[0] == '.' && (vim_ispathsep(p[1])
13008 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13009 is_relative_to_current = TRUE;
13010
13011 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013012 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 has_trailing_pathsep = TRUE;
13014
13015 q = getnextcomp(p);
13016 if (*q != NUL)
13017 {
13018 /* Separate the first path component in "p", and keep the
13019 * remainder (beginning with the path separator). */
13020 remain = vim_strsave(q - 1);
13021 q[-1] = NUL;
13022 }
13023
13024 for (;;)
13025 {
13026 for (;;)
13027 {
13028 len = readlink((char *)p, (char *)buf, MAXPATHL);
13029 if (len <= 0)
13030 break;
13031 buf[len] = NUL;
13032
13033 if (limit-- == 0)
13034 {
13035 vim_free(p);
13036 vim_free(remain);
13037 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013038 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 goto fail;
13040 }
13041
13042 /* Ensure that the result will have a trailing path separator
13043 * if the argument has one. */
13044 if (remain == NULL && has_trailing_pathsep)
13045 add_pathsep(buf);
13046
13047 /* Separate the first path component in the link value and
13048 * concatenate the remainders. */
13049 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13050 if (*q != NUL)
13051 {
13052 if (remain == NULL)
13053 remain = vim_strsave(q - 1);
13054 else
13055 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013056 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057 if (cpy != NULL)
13058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 vim_free(remain);
13060 remain = cpy;
13061 }
13062 }
13063 q[-1] = NUL;
13064 }
13065
13066 q = gettail(p);
13067 if (q > p && *q == NUL)
13068 {
13069 /* Ignore trailing path separator. */
13070 q[-1] = NUL;
13071 q = gettail(p);
13072 }
13073 if (q > p && !mch_isFullName(buf))
13074 {
13075 /* symlink is relative to directory of argument */
13076 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13077 if (cpy != NULL)
13078 {
13079 STRCPY(cpy, p);
13080 STRCPY(gettail(cpy), buf);
13081 vim_free(p);
13082 p = cpy;
13083 }
13084 }
13085 else
13086 {
13087 vim_free(p);
13088 p = vim_strsave(buf);
13089 }
13090 }
13091
13092 if (remain == NULL)
13093 break;
13094
13095 /* Append the first path component of "remain" to "p". */
13096 q = getnextcomp(remain + 1);
13097 len = q - remain - (*q != NUL);
13098 cpy = vim_strnsave(p, STRLEN(p) + len);
13099 if (cpy != NULL)
13100 {
13101 STRNCAT(cpy, remain, len);
13102 vim_free(p);
13103 p = cpy;
13104 }
13105 /* Shorten "remain". */
13106 if (*q != NUL)
13107 STRCPY(remain, q - 1);
13108 else
13109 {
13110 vim_free(remain);
13111 remain = NULL;
13112 }
13113 }
13114
13115 /* If the result is a relative path name, make it explicitly relative to
13116 * the current directory if and only if the argument had this form. */
13117 if (!vim_ispathsep(*p))
13118 {
13119 if (is_relative_to_current
13120 && *p != NUL
13121 && !(p[0] == '.'
13122 && (p[1] == NUL
13123 || vim_ispathsep(p[1])
13124 || (p[1] == '.'
13125 && (p[2] == NUL
13126 || vim_ispathsep(p[2]))))))
13127 {
13128 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013129 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130 if (cpy != NULL)
13131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 vim_free(p);
13133 p = cpy;
13134 }
13135 }
13136 else if (!is_relative_to_current)
13137 {
13138 /* Strip leading "./". */
13139 q = p;
13140 while (q[0] == '.' && vim_ispathsep(q[1]))
13141 q += 2;
13142 if (q > p)
13143 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13144 }
13145 }
13146
13147 /* Ensure that the result will have no trailing path separator
13148 * if the argument had none. But keep "/" or "//". */
13149 if (!has_trailing_pathsep)
13150 {
13151 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013152 if (after_pathsep(p, q))
13153 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 }
13155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 }
13158# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160# endif
13161#endif
13162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164
13165#ifdef HAVE_READLINK
13166fail:
13167#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169}
13170
13171/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013172 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 */
13174 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013175f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013176 typval_T *argvars;
13177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178{
Bram Moolenaar33570922005-01-25 22:26:29 +000013179 list_T *l;
13180 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181
Bram Moolenaar0d660222005-01-07 21:51:51 +000013182 rettv->vval.v_number = 0;
13183 if (argvars[0].v_type != VAR_LIST)
13184 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013185 else if ((l = argvars[0].vval.v_list) != NULL
13186 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013187 {
13188 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013189 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013190 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013191 while (li != NULL)
13192 {
13193 ni = li->li_prev;
13194 list_append(l, li);
13195 li = ni;
13196 }
13197 rettv->vval.v_list = l;
13198 rettv->v_type = VAR_LIST;
13199 ++l->lv_refcount;
13200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201}
13202
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013203#define SP_NOMOVE 1 /* don't move cursor */
13204#define SP_REPEAT 2 /* repeat to find outer pair */
13205#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013206#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013207#define SP_START 16 /* accept match at start position */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013208
Bram Moolenaar33570922005-01-25 22:26:29 +000013209static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013210
13211/*
13212 * Get flags for a search function.
13213 * Possibly sets "p_ws".
13214 * Returns BACKWARD, FORWARD or zero (for an error).
13215 */
13216 static int
13217get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013218 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013219 int *flagsp;
13220{
13221 int dir = FORWARD;
13222 char_u *flags;
13223 char_u nbuf[NUMBUFLEN];
13224 int mask;
13225
13226 if (varp->v_type != VAR_UNKNOWN)
13227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013228 flags = get_tv_string_buf_chk(varp, nbuf);
13229 if (flags == NULL)
13230 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013231 while (*flags != NUL)
13232 {
13233 switch (*flags)
13234 {
13235 case 'b': dir = BACKWARD; break;
13236 case 'w': p_ws = TRUE; break;
13237 case 'W': p_ws = FALSE; break;
13238 default: mask = 0;
13239 if (flagsp != NULL)
13240 switch (*flags)
13241 {
13242 case 'n': mask = SP_NOMOVE; break;
13243 case 'r': mask = SP_REPEAT; break;
13244 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013245 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013246 case 'c': mask = SP_START; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013247 }
13248 if (mask == 0)
13249 {
13250 EMSG2(_(e_invarg2), flags);
13251 dir = 0;
13252 }
13253 else
13254 *flagsp |= mask;
13255 }
13256 if (dir == 0)
13257 break;
13258 ++flags;
13259 }
13260 }
13261 return dir;
13262}
13263
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013265 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013267 static int
13268search_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013269 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013270 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271{
13272 char_u *pat;
13273 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013274 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 int save_p_ws = p_ws;
13276 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013277 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013278 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013279 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013280 int options = SEARCH_KEEP;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013283 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13284 if (dir == 0)
13285 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013286 if (flags & SP_START)
13287 options |= SEARCH_START;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013288
13289 /* Optional extra argument: line number to stop searching. */
13290 if (argvars[1].v_type != VAR_UNKNOWN
13291 && argvars[2].v_type != VAR_UNKNOWN)
13292 {
13293 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13294 if (lnum_stop < 0)
13295 goto theend;
13296 }
13297
Bram Moolenaar231334e2005-07-25 20:46:57 +000013298 /*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013299 * This function accepts only SP_NOMOVE, SP_START and SP_SETPCMARK flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013300 * Check to make sure only those flags are set.
13301 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13302 * flags cannot be set. Check for that condition also.
13303 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013304 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK | SP_START)) != 0)
13305 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013308 goto theend;
13309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013311 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013313 options, RE_SEARCH, (linenr_T)lnum_stop) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013315 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013316 if (flags & SP_SETPCMARK)
13317 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013319 if (match_pos != NULL)
13320 {
13321 /* Store the match cursor position */
13322 match_pos->lnum = pos.lnum;
13323 match_pos->col = pos.col + 1;
13324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 /* "/$" will put the cursor after the end of the line, may need to
13326 * correct that here */
13327 check_cursor();
13328 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013329
13330 /* If 'n' flag is used: restore cursor position. */
13331 if (flags & SP_NOMOVE)
13332 curwin->w_cursor = save_cursor;
13333theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013335
13336 return retval;
13337}
13338
13339/*
13340 * "search()" function
13341 */
13342 static void
13343f_search(argvars, rettv)
13344 typval_T *argvars;
13345 typval_T *rettv;
13346{
13347 rettv->vval.v_number = search_cmn(argvars, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348}
13349
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013351 * "searchdecl()" function
13352 */
13353 static void
13354f_searchdecl(argvars, rettv)
13355 typval_T *argvars;
13356 typval_T *rettv;
13357{
13358 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013359 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013360 int error = FALSE;
13361 char_u *name;
13362
13363 rettv->vval.v_number = 1; /* default: FAIL */
13364
13365 name = get_tv_string_chk(&argvars[0]);
13366 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013367 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013368 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013369 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13370 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13371 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013372 if (!error && name != NULL)
13373 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013374 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013375}
13376
13377/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013378 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013380 static int
13381searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013383 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384{
13385 char_u *spat, *mpat, *epat;
13386 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388 int dir;
13389 int flags = 0;
13390 char_u nbuf1[NUMBUFLEN];
13391 char_u nbuf2[NUMBUFLEN];
13392 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013393 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013394 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013397 spat = get_tv_string_chk(&argvars[0]);
13398 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13399 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13400 if (spat == NULL || mpat == NULL || epat == NULL)
13401 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 /* Handle the optional fourth argument: flags */
13404 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013405 if (dir == 0)
13406 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013407 /*
13408 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13409 */
13410 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13411 {
13412 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13413 goto theend;
13414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013416 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013417 if (argvars[3].v_type == VAR_UNKNOWN
13418 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 skip = (char_u *)"";
13420 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013423 if (argvars[5].v_type != VAR_UNKNOWN)
13424 {
13425 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13426 if (lnum_stop < 0)
13427 goto theend;
13428 }
13429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013430 if (skip == NULL)
13431 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013433 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13434 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013435
13436theend:
13437 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013438
13439 return retval;
13440}
13441
13442/*
13443 * "searchpair()" function
13444 */
13445 static void
13446f_searchpair(argvars, rettv)
13447 typval_T *argvars;
13448 typval_T *rettv;
13449{
13450 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13451}
13452
13453/*
13454 * "searchpairpos()" function
13455 */
13456 static void
13457f_searchpairpos(argvars, rettv)
13458 typval_T *argvars;
13459 typval_T *rettv;
13460{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013461 pos_T match_pos;
13462 int lnum = 0;
13463 int col = 0;
13464
13465 rettv->vval.v_number = 0;
13466
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013467 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013468 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013469
13470 if (searchpair_cmn(argvars, &match_pos) > 0)
13471 {
13472 lnum = match_pos.lnum;
13473 col = match_pos.col;
13474 }
13475
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013476 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13477 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013478}
13479
13480/*
13481 * Search for a start/middle/end thing.
13482 * Used by searchpair(), see its documentation for the details.
13483 * Returns 0 or -1 for no match,
13484 */
13485 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013486do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013487 char_u *spat; /* start pattern */
13488 char_u *mpat; /* middle pattern */
13489 char_u *epat; /* end pattern */
13490 int dir; /* BACKWARD or FORWARD */
13491 char_u *skip; /* skip expression */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013492 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE, SP_START */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013493 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013494 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013495{
13496 char_u *save_cpo;
13497 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13498 long retval = 0;
13499 pos_T pos;
13500 pos_T firstpos;
13501 pos_T foundpos;
13502 pos_T save_cursor;
13503 pos_T save_pos;
13504 int n;
13505 int r;
13506 int nest = 1;
13507 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013508 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013509
13510 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13511 save_cpo = p_cpo;
13512 p_cpo = (char_u *)"";
13513
13514 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13515 * start/middle/end (pat3, for the top pair). */
13516 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13517 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13518 if (pat2 == NULL || pat3 == NULL)
13519 goto theend;
13520 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13521 if (*mpat == NUL)
13522 STRCPY(pat3, pat2);
13523 else
13524 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13525 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013526 if (flags & SP_START)
13527 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013528
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 save_cursor = curwin->w_cursor;
13530 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013531 clearpos(&firstpos);
13532 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 pat = pat3;
13534 for (;;)
13535 {
13536 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013537 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13539 /* didn't find it or found the first match again: FAIL */
13540 break;
13541
13542 if (firstpos.lnum == 0)
13543 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013544 if (equalpos(pos, foundpos))
13545 {
13546 /* Found the same position again. Can happen with a pattern that
13547 * has "\zs" at the end and searching backwards. Advance one
13548 * character and try again. */
13549 if (dir == BACKWARD)
13550 decl(&pos);
13551 else
13552 incl(&pos);
13553 }
13554 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555
13556 /* If the skip pattern matches, ignore this match. */
13557 if (*skip != NUL)
13558 {
13559 save_pos = curwin->w_cursor;
13560 curwin->w_cursor = pos;
13561 r = eval_to_bool(skip, &err, NULL, FALSE);
13562 curwin->w_cursor = save_pos;
13563 if (err)
13564 {
13565 /* Evaluating {skip} caused an error, break here. */
13566 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013567 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568 break;
13569 }
13570 if (r)
13571 continue;
13572 }
13573
13574 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13575 {
13576 /* Found end when searching backwards or start when searching
13577 * forward: nested pair. */
13578 ++nest;
13579 pat = pat2; /* nested, don't search for middle */
13580 }
13581 else
13582 {
13583 /* Found end when searching forward or start when searching
13584 * backward: end of (nested) pair; or found middle in outer pair. */
13585 if (--nest == 1)
13586 pat = pat3; /* outer level, search for middle */
13587 }
13588
13589 if (nest == 0)
13590 {
13591 /* Found the match: return matchcount or line number. */
13592 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013593 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013595 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013596 if (flags & SP_SETPCMARK)
13597 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 curwin->w_cursor = pos;
13599 if (!(flags & SP_REPEAT))
13600 break;
13601 nest = 1; /* search for next unmatched */
13602 }
13603 }
13604
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013605 if (match_pos != NULL)
13606 {
13607 /* Store the match cursor position */
13608 match_pos->lnum = curwin->w_cursor.lnum;
13609 match_pos->col = curwin->w_cursor.col + 1;
13610 }
13611
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013613 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614 curwin->w_cursor = save_cursor;
13615
13616theend:
13617 vim_free(pat2);
13618 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013620
13621 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622}
13623
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013624/*
13625 * "searchpos()" function
13626 */
13627 static void
13628f_searchpos(argvars, rettv)
13629 typval_T *argvars;
13630 typval_T *rettv;
13631{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013632 pos_T match_pos;
13633 int lnum = 0;
13634 int col = 0;
13635
13636 rettv->vval.v_number = 0;
13637
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013638 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013639 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013640
13641 if (search_cmn(argvars, &match_pos) > 0)
13642 {
13643 lnum = match_pos.lnum;
13644 col = match_pos.col;
13645 }
13646
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013647 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13648 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013649
13650}
13651
13652
Bram Moolenaar0d660222005-01-07 21:51:51 +000013653/*ARGSUSED*/
13654 static void
13655f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013656 typval_T *argvars;
13657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013659#ifdef FEAT_CLIENTSERVER
13660 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013661 char_u *server = get_tv_string_chk(&argvars[0]);
13662 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663
Bram Moolenaar0d660222005-01-07 21:51:51 +000013664 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 if (server == NULL || reply == NULL)
13666 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013667 if (check_restricted() || check_secure())
13668 return;
13669# ifdef FEAT_X11
13670 if (check_connection() == FAIL)
13671 return;
13672# endif
13673
13674 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676 EMSG(_("E258: Unable to send to client"));
13677 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013679 rettv->vval.v_number = 0;
13680#else
13681 rettv->vval.v_number = -1;
13682#endif
13683}
13684
13685/*ARGSUSED*/
13686 static void
13687f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *argvars;
13689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013690{
13691 char_u *r = NULL;
13692
13693#ifdef FEAT_CLIENTSERVER
13694# ifdef WIN32
13695 r = serverGetVimNames();
13696# else
13697 make_connection();
13698 if (X_DISPLAY != NULL)
13699 r = serverGetVimNames(X_DISPLAY);
13700# endif
13701#endif
13702 rettv->v_type = VAR_STRING;
13703 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704}
13705
13706/*
13707 * "setbufvar()" function
13708 */
13709/*ARGSUSED*/
13710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013712 typval_T *argvars;
13713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714{
13715 buf_T *buf;
13716#ifdef FEAT_AUTOCMD
13717 aco_save_T aco;
13718#else
13719 buf_T *save_curbuf;
13720#endif
13721 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013722 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 char_u nbuf[NUMBUFLEN];
13724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013725 rettv->vval.v_number = 0;
13726
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 if (check_restricted() || check_secure())
13728 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13730 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 varp = &argvars[2];
13733
13734 if (buf != NULL && varname != NULL && varp != NULL)
13735 {
13736 /* set curbuf to be our buf, temporarily */
13737#ifdef FEAT_AUTOCMD
13738 aucmd_prepbuf(&aco, buf);
13739#else
13740 save_curbuf = curbuf;
13741 curbuf = buf;
13742#endif
13743
13744 if (*varname == '&')
13745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 long numval;
13747 char_u *strval;
13748 int error = FALSE;
13749
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013751 numval = get_tv_number_chk(varp, &error);
13752 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013753 if (!error && strval != NULL)
13754 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 }
13756 else
13757 {
13758 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13759 if (bufvarname != NULL)
13760 {
13761 STRCPY(bufvarname, "b:");
13762 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013763 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 vim_free(bufvarname);
13765 }
13766 }
13767
13768 /* reset notion of buffer */
13769#ifdef FEAT_AUTOCMD
13770 aucmd_restbuf(&aco);
13771#else
13772 curbuf = save_curbuf;
13773#endif
13774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775}
13776
13777/*
13778 * "setcmdpos()" function
13779 */
13780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013782 typval_T *argvars;
13783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013785 int pos = (int)get_tv_number(&argvars[0]) - 1;
13786
13787 if (pos >= 0)
13788 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789}
13790
13791/*
13792 * "setline()" function
13793 */
13794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013796 typval_T *argvars;
13797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798{
13799 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013800 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013801 list_T *l = NULL;
13802 listitem_T *li = NULL;
13803 long added = 0;
13804 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013806 lnum = get_tv_lnum(&argvars[0]);
13807 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013809 l = argvars[1].vval.v_list;
13810 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013812 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013814
13815 rettv->vval.v_number = 0; /* OK */
13816 for (;;)
13817 {
13818 if (l != NULL)
13819 {
13820 /* list argument, get next string */
13821 if (li == NULL)
13822 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013824 li = li->li_next;
13825 }
13826
13827 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013828 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013829 break;
13830 if (lnum <= curbuf->b_ml.ml_line_count)
13831 {
13832 /* existing line, replace it */
13833 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13834 {
13835 changed_bytes(lnum, 0);
13836 check_cursor_col();
13837 rettv->vval.v_number = 0; /* OK */
13838 }
13839 }
13840 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13841 {
13842 /* lnum is one past the last line, append the line */
13843 ++added;
13844 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13845 rettv->vval.v_number = 0; /* OK */
13846 }
13847
13848 if (l == NULL) /* only one string argument */
13849 break;
13850 ++lnum;
13851 }
13852
13853 if (added > 0)
13854 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855}
13856
13857/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013858 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013859 */
13860/*ARGSUSED*/
13861 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013862set_qf_ll_list(wp, list_arg, action_arg, rettv)
13863 win_T *wp;
13864 typval_T *list_arg;
13865 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013866 typval_T *rettv;
13867{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013868#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013869 char_u *act;
13870 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013871#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013872
Bram Moolenaar2641f772005-03-25 21:58:17 +000013873 rettv->vval.v_number = -1;
13874
13875#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013876 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013877 EMSG(_(e_listreq));
13878 else
13879 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013880 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013881
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013882 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013883 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013884 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013885 if (act == NULL)
13886 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013887 if (*act == 'a' || *act == 'r')
13888 action = *act;
13889 }
13890
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013891 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013892 rettv->vval.v_number = 0;
13893 }
13894#endif
13895}
13896
13897/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013898 * "setloclist()" function
13899 */
13900/*ARGSUSED*/
13901 static void
13902f_setloclist(argvars, rettv)
13903 typval_T *argvars;
13904 typval_T *rettv;
13905{
13906 win_T *win;
13907
13908 rettv->vval.v_number = -1;
13909
13910 win = find_win_by_nr(&argvars[0]);
13911 if (win != NULL)
13912 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13913}
13914
13915/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013916 * "setpos()" function
13917 */
13918/*ARGSUSED*/
13919 static void
13920f_setpos(argvars, rettv)
13921 typval_T *argvars;
13922 typval_T *rettv;
13923{
13924 pos_T pos;
13925 int fnum;
13926 char_u *name;
13927
13928 name = get_tv_string_chk(argvars);
13929 if (name != NULL)
13930 {
13931 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
13932 {
13933 --pos.col;
13934 if (name[0] == '.') /* cursor */
13935 {
13936 if (fnum == curbuf->b_fnum)
13937 {
13938 curwin->w_cursor = pos;
13939 check_cursor();
13940 }
13941 else
13942 EMSG(_(e_invarg));
13943 }
13944 else if (name[0] == '\'') /* mark */
13945 (void)setmark_pos(name[1], &pos, fnum);
13946 else
13947 EMSG(_(e_invarg));
13948 }
13949 }
13950}
13951
13952/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013953 * "setqflist()" function
13954 */
13955/*ARGSUSED*/
13956 static void
13957f_setqflist(argvars, rettv)
13958 typval_T *argvars;
13959 typval_T *rettv;
13960{
13961 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13962}
13963
13964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 * "setreg()" function
13966 */
13967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013968f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971{
13972 int regname;
13973 char_u *strregname;
13974 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 int append;
13977 char_u yank_type;
13978 long block_len;
13979
13980 block_len = -1;
13981 yank_type = MAUTO;
13982 append = FALSE;
13983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013984 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 if (strregname == NULL)
13988 return; /* type error; errmsg already given */
13989 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 if (regname == 0 || regname == '@')
13991 regname = '"';
13992 else if (regname == '=')
13993 return;
13994
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013995 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997 stropt = get_tv_string_chk(&argvars[2]);
13998 if (stropt == NULL)
13999 return; /* type error */
14000 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 switch (*stropt)
14002 {
14003 case 'a': case 'A': /* append */
14004 append = TRUE;
14005 break;
14006 case 'v': case 'c': /* character-wise selection */
14007 yank_type = MCHAR;
14008 break;
14009 case 'V': case 'l': /* line-wise selection */
14010 yank_type = MLINE;
14011 break;
14012#ifdef FEAT_VISUAL
14013 case 'b': case Ctrl_V: /* block-wise selection */
14014 yank_type = MBLOCK;
14015 if (VIM_ISDIGIT(stropt[1]))
14016 {
14017 ++stropt;
14018 block_len = getdigits(&stropt) - 1;
14019 --stropt;
14020 }
14021 break;
14022#endif
14023 }
14024 }
14025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026 strval = get_tv_string_chk(&argvars[1]);
14027 if (strval != NULL)
14028 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014030 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031}
14032
14033
14034/*
14035 * "setwinvar(expr)" function
14036 */
14037/*ARGSUSED*/
14038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014040 typval_T *argvars;
14041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042{
14043 win_T *win;
14044#ifdef FEAT_WINDOWS
14045 win_T *save_curwin;
14046#endif
14047 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014048 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 char_u nbuf[NUMBUFLEN];
14050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051 rettv->vval.v_number = 0;
14052
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 if (check_restricted() || check_secure())
14054 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014056 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 varp = &argvars[2];
14058
14059 if (win != NULL && varname != NULL && varp != NULL)
14060 {
14061#ifdef FEAT_WINDOWS
14062 /* set curwin to be our win, temporarily */
14063 save_curwin = curwin;
14064 curwin = win;
14065 curbuf = curwin->w_buffer;
14066#endif
14067
14068 if (*varname == '&')
14069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014070 long numval;
14071 char_u *strval;
14072 int error = FALSE;
14073
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014075 numval = get_tv_number_chk(varp, &error);
14076 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077 if (!error && strval != NULL)
14078 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079 }
14080 else
14081 {
14082 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14083 if (winvarname != NULL)
14084 {
14085 STRCPY(winvarname, "w:");
14086 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014087 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 vim_free(winvarname);
14089 }
14090 }
14091
14092#ifdef FEAT_WINDOWS
14093 /* Restore current window, if it's still valid (autocomands can make
14094 * it invalid). */
14095 if (win_valid(save_curwin))
14096 {
14097 curwin = save_curwin;
14098 curbuf = curwin->w_buffer;
14099 }
14100#endif
14101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102}
14103
14104/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014105 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 */
14107 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 typval_T *argvars;
14110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114 p = get_tv_string(&argvars[0]);
14115 rettv->vval.v_string = vim_strsave(p);
14116 simplify_filename(rettv->vval.v_string); /* simplify in place */
14117 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118}
14119
Bram Moolenaar0d660222005-01-07 21:51:51 +000014120static int
14121#ifdef __BORLANDC__
14122 _RTLENTRYF
14123#endif
14124 item_compare __ARGS((const void *s1, const void *s2));
14125static int
14126#ifdef __BORLANDC__
14127 _RTLENTRYF
14128#endif
14129 item_compare2 __ARGS((const void *s1, const void *s2));
14130
14131static int item_compare_ic;
14132static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014133static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014134#define ITEM_COMPARE_FAIL 999
14135
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014137 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014139 static int
14140#ifdef __BORLANDC__
14141_RTLENTRYF
14142#endif
14143item_compare(s1, s2)
14144 const void *s1;
14145 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014147 char_u *p1, *p2;
14148 char_u *tofree1, *tofree2;
14149 int res;
14150 char_u numbuf1[NUMBUFLEN];
14151 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014153 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14154 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155 if (item_compare_ic)
14156 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014158 res = STRCMP(p1, p2);
14159 vim_free(tofree1);
14160 vim_free(tofree2);
14161 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162}
14163
14164 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165#ifdef __BORLANDC__
14166_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014168item_compare2(s1, s2)
14169 const void *s1;
14170 const void *s2;
14171{
14172 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 typval_T rettv;
14174 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014175 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 /* shortcut after failure in previous call; compare all items equal */
14178 if (item_compare_func_err)
14179 return 0;
14180
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014181 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14182 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14184 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014185
14186 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14187 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014188 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014189 clear_tv(&argv[0]);
14190 clear_tv(&argv[1]);
14191
14192 if (res == FAIL)
14193 res = ITEM_COMPARE_FAIL;
14194 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 /* return value has wrong type */
14196 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14197 if (item_compare_func_err)
14198 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014199 clear_tv(&rettv);
14200 return res;
14201}
14202
14203/*
14204 * "sort({list})" function
14205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014207f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014208 typval_T *argvars;
14209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210{
Bram Moolenaar33570922005-01-25 22:26:29 +000014211 list_T *l;
14212 listitem_T *li;
14213 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014214 long len;
14215 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216
Bram Moolenaar0d660222005-01-07 21:51:51 +000014217 rettv->vval.v_number = 0;
14218 if (argvars[0].v_type != VAR_LIST)
14219 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 else
14221 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014222 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014223 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014224 return;
14225 rettv->vval.v_list = l;
14226 rettv->v_type = VAR_LIST;
14227 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228
Bram Moolenaar0d660222005-01-07 21:51:51 +000014229 len = list_len(l);
14230 if (len <= 1)
14231 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232
Bram Moolenaar0d660222005-01-07 21:51:51 +000014233 item_compare_ic = FALSE;
14234 item_compare_func = NULL;
14235 if (argvars[1].v_type != VAR_UNKNOWN)
14236 {
14237 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014239 else
14240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241 int error = FALSE;
14242
14243 i = get_tv_number_chk(&argvars[1], &error);
14244 if (error)
14245 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014246 if (i == 1)
14247 item_compare_ic = TRUE;
14248 else
14249 item_compare_func = get_tv_string(&argvars[1]);
14250 }
14251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252
Bram Moolenaar0d660222005-01-07 21:51:51 +000014253 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014254 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014255 if (ptrs == NULL)
14256 return;
14257 i = 0;
14258 for (li = l->lv_first; li != NULL; li = li->li_next)
14259 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014262 /* test the compare function */
14263 if (item_compare_func != NULL
14264 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14265 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014266 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014268 {
14269 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014270 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014271 item_compare_func == NULL ? item_compare : item_compare2);
14272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 if (!item_compare_func_err)
14274 {
14275 /* Clear the List and append the items in the sorted order. */
14276 l->lv_first = l->lv_last = NULL;
14277 l->lv_len = 0;
14278 for (i = 0; i < len; ++i)
14279 list_append(l, ptrs[i]);
14280 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014281 }
14282
14283 vim_free(ptrs);
14284 }
14285}
14286
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014287/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014288 * "soundfold({word})" function
14289 */
14290 static void
14291f_soundfold(argvars, rettv)
14292 typval_T *argvars;
14293 typval_T *rettv;
14294{
14295 char_u *s;
14296
14297 rettv->v_type = VAR_STRING;
14298 s = get_tv_string(&argvars[0]);
14299#ifdef FEAT_SYN_HL
14300 rettv->vval.v_string = eval_soundfold(s);
14301#else
14302 rettv->vval.v_string = vim_strsave(s);
14303#endif
14304}
14305
14306/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014307 * "spellbadword()" function
14308 */
14309/* ARGSUSED */
14310 static void
14311f_spellbadword(argvars, rettv)
14312 typval_T *argvars;
14313 typval_T *rettv;
14314{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014315 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014316 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014317 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014318
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014319 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014320 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014321
14322#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014323 if (argvars[0].v_type == VAR_UNKNOWN)
14324 {
14325 /* Find the start and length of the badly spelled word. */
14326 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14327 if (len != 0)
14328 word = ml_get_cursor();
14329 }
14330 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14331 {
14332 char_u *str = get_tv_string_chk(&argvars[0]);
14333 int capcol = -1;
14334
14335 if (str != NULL)
14336 {
14337 /* Check the argument for spelling. */
14338 while (*str != NUL)
14339 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014340 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014341 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014342 {
14343 word = str;
14344 break;
14345 }
14346 str += len;
14347 }
14348 }
14349 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014350#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014351
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014352 list_append_string(rettv->vval.v_list, word, len);
14353 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014354 attr == HLF_SPB ? "bad" :
14355 attr == HLF_SPR ? "rare" :
14356 attr == HLF_SPL ? "local" :
14357 attr == HLF_SPC ? "caps" :
14358 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014359}
14360
14361/*
14362 * "spellsuggest()" function
14363 */
14364 static void
14365f_spellsuggest(argvars, rettv)
14366 typval_T *argvars;
14367 typval_T *rettv;
14368{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014369#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014370 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014371 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014372 int maxcount;
14373 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014374 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014375 listitem_T *li;
14376 int need_capital = FALSE;
14377#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014378
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014379 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014380 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014381
14382#ifdef FEAT_SYN_HL
14383 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14384 {
14385 str = get_tv_string(&argvars[0]);
14386 if (argvars[1].v_type != VAR_UNKNOWN)
14387 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014388 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014389 if (maxcount <= 0)
14390 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014391 if (argvars[2].v_type != VAR_UNKNOWN)
14392 {
14393 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14394 if (typeerr)
14395 return;
14396 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014397 }
14398 else
14399 maxcount = 25;
14400
Bram Moolenaar4770d092006-01-12 23:22:24 +000014401 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014402
14403 for (i = 0; i < ga.ga_len; ++i)
14404 {
14405 str = ((char_u **)ga.ga_data)[i];
14406
14407 li = listitem_alloc();
14408 if (li == NULL)
14409 vim_free(str);
14410 else
14411 {
14412 li->li_tv.v_type = VAR_STRING;
14413 li->li_tv.v_lock = 0;
14414 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014415 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014416 }
14417 }
14418 ga_clear(&ga);
14419 }
14420#endif
14421}
14422
Bram Moolenaar0d660222005-01-07 21:51:51 +000014423 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014424f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014425 typval_T *argvars;
14426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014427{
14428 char_u *str;
14429 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014430 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431 regmatch_T regmatch;
14432 char_u patbuf[NUMBUFLEN];
14433 char_u *save_cpo;
14434 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014435 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014436 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014437 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014438
14439 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14440 save_cpo = p_cpo;
14441 p_cpo = (char_u *)"";
14442
14443 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014444 if (argvars[1].v_type != VAR_UNKNOWN)
14445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14447 if (pat == NULL)
14448 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014450 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014451 }
14452 if (pat == NULL || *pat == NUL)
14453 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014454
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014455 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014457 if (typeerr)
14458 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459
Bram Moolenaar0d660222005-01-07 21:51:51 +000014460 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14461 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014463 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014464 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014466 if (*str == NUL)
14467 match = FALSE; /* empty item at the end */
14468 else
14469 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014470 if (match)
14471 end = regmatch.startp[0];
14472 else
14473 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014474 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14475 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014477 if (list_append_string(rettv->vval.v_list, str,
14478 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014479 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480 }
14481 if (!match)
14482 break;
14483 /* Advance to just after the match. */
14484 if (regmatch.endp[0] > str)
14485 col = 0;
14486 else
14487 {
14488 /* Don't get stuck at the same match. */
14489#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014490 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491#else
14492 col = 1;
14493#endif
14494 }
14495 str = regmatch.endp[0];
14496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497
Bram Moolenaar0d660222005-01-07 21:51:51 +000014498 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500
Bram Moolenaar0d660222005-01-07 21:51:51 +000014501 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502}
14503
14504#ifdef HAVE_STRFTIME
14505/*
14506 * "strftime({format}[, {time}])" function
14507 */
14508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014510 typval_T *argvars;
14511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512{
14513 char_u result_buf[256];
14514 struct tm *curtime;
14515 time_t seconds;
14516 char_u *p;
14517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014521 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 seconds = time(NULL);
14523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 curtime = localtime(&seconds);
14526 /* MSVC returns NULL for an invalid value of seconds. */
14527 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 else
14530 {
14531# ifdef FEAT_MBYTE
14532 vimconv_T conv;
14533 char_u *enc;
14534
14535 conv.vc_type = CONV_NONE;
14536 enc = enc_locale();
14537 convert_setup(&conv, p_enc, enc);
14538 if (conv.vc_type != CONV_NONE)
14539 p = string_convert(&conv, p, NULL);
14540# endif
14541 if (p != NULL)
14542 (void)strftime((char *)result_buf, sizeof(result_buf),
14543 (char *)p, curtime);
14544 else
14545 result_buf[0] = NUL;
14546
14547# ifdef FEAT_MBYTE
14548 if (conv.vc_type != CONV_NONE)
14549 vim_free(p);
14550 convert_setup(&conv, enc, p_enc);
14551 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014552 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 else
14554# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014555 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556
14557# ifdef FEAT_MBYTE
14558 /* Release conversion descriptors */
14559 convert_setup(&conv, NULL, NULL);
14560 vim_free(enc);
14561# endif
14562 }
14563}
14564#endif
14565
14566/*
14567 * "stridx()" function
14568 */
14569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014570f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014571 typval_T *argvars;
14572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573{
14574 char_u buf[NUMBUFLEN];
14575 char_u *needle;
14576 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014579 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014581 needle = get_tv_string_chk(&argvars[1]);
14582 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014584 if (needle == NULL || haystack == NULL)
14585 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586
Bram Moolenaar33570922005-01-25 22:26:29 +000014587 if (argvars[2].v_type != VAR_UNKNOWN)
14588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014589 int error = FALSE;
14590
14591 start_idx = get_tv_number_chk(&argvars[2], &error);
14592 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014594 if (start_idx >= 0)
14595 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 }
14597
14598 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14599 if (pos != NULL)
14600 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601}
14602
14603/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014604 * "string()" function
14605 */
14606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014608 typval_T *argvars;
14609 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014610{
14611 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014612 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014614 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014615 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014616 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014617 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618}
14619
14620/*
14621 * "strlen()" function
14622 */
14623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014625 typval_T *argvars;
14626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014628 rettv->vval.v_number = (varnumber_T)(STRLEN(
14629 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630}
14631
14632/*
14633 * "strpart()" function
14634 */
14635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014636f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014637 typval_T *argvars;
14638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639{
14640 char_u *p;
14641 int n;
14642 int len;
14643 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014644 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014646 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647 slen = (int)STRLEN(p);
14648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014649 n = get_tv_number_chk(&argvars[1], &error);
14650 if (error)
14651 len = 0;
14652 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014653 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 else
14655 len = slen - n; /* default len: all bytes that are available. */
14656
14657 /*
14658 * Only return the overlap between the specified part and the actual
14659 * string.
14660 */
14661 if (n < 0)
14662 {
14663 len += n;
14664 n = 0;
14665 }
14666 else if (n > slen)
14667 n = slen;
14668 if (len < 0)
14669 len = 0;
14670 else if (n + len > slen)
14671 len = slen - n;
14672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673 rettv->v_type = VAR_STRING;
14674 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675}
14676
14677/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014678 * "strridx()" function
14679 */
14680 static void
14681f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014682 typval_T *argvars;
14683 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014684{
14685 char_u buf[NUMBUFLEN];
14686 char_u *needle;
14687 char_u *haystack;
14688 char_u *rest;
14689 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014690 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 needle = get_tv_string_chk(&argvars[1]);
14693 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014694 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014695
14696 rettv->vval.v_number = -1;
14697 if (needle == NULL || haystack == NULL)
14698 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014699 if (argvars[2].v_type != VAR_UNKNOWN)
14700 {
14701 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014703 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014704 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014705 }
14706 else
14707 end_idx = haystack_len;
14708
Bram Moolenaar0d660222005-01-07 21:51:51 +000014709 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014710 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014712 lastmatch = haystack + end_idx;
14713 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014714 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014715 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716 for (rest = haystack; *rest != '\0'; ++rest)
14717 {
14718 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014719 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720 break;
14721 lastmatch = rest;
14722 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014723 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014724
14725 if (lastmatch == NULL)
14726 rettv->vval.v_number = -1;
14727 else
14728 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14729}
14730
14731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732 * "strtrans()" function
14733 */
14734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 typval_T *argvars;
14737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014739 rettv->v_type = VAR_STRING;
14740 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741}
14742
14743/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014744 * "submatch()" function
14745 */
14746 static void
14747f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014748 typval_T *argvars;
14749 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750{
14751 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014752 rettv->vval.v_string =
14753 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014754}
14755
14756/*
14757 * "substitute()" function
14758 */
14759 static void
14760f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014761 typval_T *argvars;
14762 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014763{
14764 char_u patbuf[NUMBUFLEN];
14765 char_u subbuf[NUMBUFLEN];
14766 char_u flagsbuf[NUMBUFLEN];
14767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014768 char_u *str = get_tv_string_chk(&argvars[0]);
14769 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14770 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14771 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14772
Bram Moolenaar0d660222005-01-07 21:51:51 +000014773 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14775 rettv->vval.v_string = NULL;
14776 else
14777 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778}
14779
14780/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014781 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782 */
14783/*ARGSUSED*/
14784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014785f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014786 typval_T *argvars;
14787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788{
14789 int id = 0;
14790#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014791 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792 long col;
14793 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014794 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 lnum = get_tv_lnum(argvars); /* -1 on type error */
14797 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14798 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014800 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014801 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014802 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803#endif
14804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014805 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806}
14807
14808/*
14809 * "synIDattr(id, what [, mode])" function
14810 */
14811/*ARGSUSED*/
14812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014813f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014814 typval_T *argvars;
14815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816{
14817 char_u *p = NULL;
14818#ifdef FEAT_SYN_HL
14819 int id;
14820 char_u *what;
14821 char_u *mode;
14822 char_u modebuf[NUMBUFLEN];
14823 int modec;
14824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014825 id = get_tv_number(&argvars[0]);
14826 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830 modec = TOLOWER_ASC(mode[0]);
14831 if (modec != 't' && modec != 'c'
14832#ifdef FEAT_GUI
14833 && modec != 'g'
14834#endif
14835 )
14836 modec = 0; /* replace invalid with current */
14837 }
14838 else
14839 {
14840#ifdef FEAT_GUI
14841 if (gui.in_use)
14842 modec = 'g';
14843 else
14844#endif
14845 if (t_colors > 1)
14846 modec = 'c';
14847 else
14848 modec = 't';
14849 }
14850
14851
14852 switch (TOLOWER_ASC(what[0]))
14853 {
14854 case 'b':
14855 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14856 p = highlight_color(id, what, modec);
14857 else /* bold */
14858 p = highlight_has_attr(id, HL_BOLD, modec);
14859 break;
14860
14861 case 'f': /* fg[#] */
14862 p = highlight_color(id, what, modec);
14863 break;
14864
14865 case 'i':
14866 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14867 p = highlight_has_attr(id, HL_INVERSE, modec);
14868 else /* italic */
14869 p = highlight_has_attr(id, HL_ITALIC, modec);
14870 break;
14871
14872 case 'n': /* name */
14873 p = get_highlight_name(NULL, id - 1);
14874 break;
14875
14876 case 'r': /* reverse */
14877 p = highlight_has_attr(id, HL_INVERSE, modec);
14878 break;
14879
14880 case 's': /* standout */
14881 p = highlight_has_attr(id, HL_STANDOUT, modec);
14882 break;
14883
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014884 case 'u':
14885 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14886 /* underline */
14887 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14888 else
14889 /* undercurl */
14890 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891 break;
14892 }
14893
14894 if (p != NULL)
14895 p = vim_strsave(p);
14896#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014897 rettv->v_type = VAR_STRING;
14898 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899}
14900
14901/*
14902 * "synIDtrans(id)" function
14903 */
14904/*ARGSUSED*/
14905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014906f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014907 typval_T *argvars;
14908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909{
14910 int id;
14911
14912#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914
14915 if (id > 0)
14916 id = syn_get_final_id(id);
14917 else
14918#endif
14919 id = 0;
14920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014921 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922}
14923
14924/*
14925 * "system()" function
14926 */
14927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014928f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014929 typval_T *argvars;
14930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014932 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014934 char_u *infile = NULL;
14935 char_u buf[NUMBUFLEN];
14936 int err = FALSE;
14937 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014939 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014940 {
14941 /*
14942 * Write the string to a temp file, to be used for input of the shell
14943 * command.
14944 */
14945 if ((infile = vim_tempname('i')) == NULL)
14946 {
14947 EMSG(_(e_notmp));
14948 return;
14949 }
14950
14951 fd = mch_fopen((char *)infile, WRITEBIN);
14952 if (fd == NULL)
14953 {
14954 EMSG2(_(e_notopen), infile);
14955 goto done;
14956 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014957 p = get_tv_string_buf_chk(&argvars[1], buf);
14958 if (p == NULL)
14959 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014960 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14961 err = TRUE;
14962 if (fclose(fd) != 0)
14963 err = TRUE;
14964 if (err)
14965 {
14966 EMSG(_("E677: Error writing temp file"));
14967 goto done;
14968 }
14969 }
14970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014972
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973#ifdef USE_CR
14974 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014975 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 {
14977 char_u *s;
14978
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014979 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 {
14981 if (*s == CAR)
14982 *s = NL;
14983 }
14984 }
14985#else
14986# ifdef USE_CRNL
14987 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014988 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 {
14990 char_u *s, *d;
14991
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014992 d = res;
14993 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 {
14995 if (s[0] == CAR && s[1] == NL)
14996 ++s;
14997 *d++ = *s;
14998 }
14999 *d = NUL;
15000 }
15001# endif
15002#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015003
15004done:
15005 if (infile != NULL)
15006 {
15007 mch_remove(infile);
15008 vim_free(infile);
15009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 rettv->v_type = VAR_STRING;
15011 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012}
15013
15014/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015015 * "tabpagebuflist()" function
15016 */
15017/* ARGSUSED */
15018 static void
15019f_tabpagebuflist(argvars, rettv)
15020 typval_T *argvars;
15021 typval_T *rettv;
15022{
15023#ifndef FEAT_WINDOWS
15024 rettv->vval.v_number = 0;
15025#else
15026 tabpage_T *tp;
15027 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015028
15029 if (argvars[0].v_type == VAR_UNKNOWN)
15030 wp = firstwin;
15031 else
15032 {
15033 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15034 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015035 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015036 }
15037 if (wp == NULL)
15038 rettv->vval.v_number = 0;
15039 else
15040 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015041 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015042 rettv->vval.v_number = 0;
15043 else
15044 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015045 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015046 if (list_append_number(rettv->vval.v_list,
15047 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015048 break;
15049 }
15050 }
15051#endif
15052}
15053
15054
15055/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015056 * "tabpagenr()" function
15057 */
15058/* ARGSUSED */
15059 static void
15060f_tabpagenr(argvars, rettv)
15061 typval_T *argvars;
15062 typval_T *rettv;
15063{
15064 int nr = 1;
15065#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015066 char_u *arg;
15067
15068 if (argvars[0].v_type != VAR_UNKNOWN)
15069 {
15070 arg = get_tv_string_chk(&argvars[0]);
15071 nr = 0;
15072 if (arg != NULL)
15073 {
15074 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015075 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015076 else
15077 EMSG2(_(e_invexpr2), arg);
15078 }
15079 }
15080 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015081 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015082#endif
15083 rettv->vval.v_number = nr;
15084}
15085
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015086
15087#ifdef FEAT_WINDOWS
15088static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15089
15090/*
15091 * Common code for tabpagewinnr() and winnr().
15092 */
15093 static int
15094get_winnr(tp, argvar)
15095 tabpage_T *tp;
15096 typval_T *argvar;
15097{
15098 win_T *twin;
15099 int nr = 1;
15100 win_T *wp;
15101 char_u *arg;
15102
15103 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15104 if (argvar->v_type != VAR_UNKNOWN)
15105 {
15106 arg = get_tv_string_chk(argvar);
15107 if (arg == NULL)
15108 nr = 0; /* type error; errmsg already given */
15109 else if (STRCMP(arg, "$") == 0)
15110 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15111 else if (STRCMP(arg, "#") == 0)
15112 {
15113 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15114 if (twin == NULL)
15115 nr = 0;
15116 }
15117 else
15118 {
15119 EMSG2(_(e_invexpr2), arg);
15120 nr = 0;
15121 }
15122 }
15123
15124 if (nr > 0)
15125 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15126 wp != twin; wp = wp->w_next)
15127 ++nr;
15128 return nr;
15129}
15130#endif
15131
15132/*
15133 * "tabpagewinnr()" function
15134 */
15135/* ARGSUSED */
15136 static void
15137f_tabpagewinnr(argvars, rettv)
15138 typval_T *argvars;
15139 typval_T *rettv;
15140{
15141 int nr = 1;
15142#ifdef FEAT_WINDOWS
15143 tabpage_T *tp;
15144
15145 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15146 if (tp == NULL)
15147 nr = 0;
15148 else
15149 nr = get_winnr(tp, &argvars[1]);
15150#endif
15151 rettv->vval.v_number = nr;
15152}
15153
15154
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015155/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015156 * "tagfiles()" function
15157 */
15158/*ARGSUSED*/
15159 static void
15160f_tagfiles(argvars, rettv)
15161 typval_T *argvars;
15162 typval_T *rettv;
15163{
15164 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015165 tagname_T tn;
15166 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015167
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015168 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015169 {
15170 rettv->vval.v_number = 0;
15171 return;
15172 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015173
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015174 for (first = TRUE; ; first = FALSE)
15175 if (get_tagfname(&tn, first, fname) == FAIL
15176 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015177 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015178 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015179}
15180
15181/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015182 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015183 */
15184 static void
15185f_taglist(argvars, rettv)
15186 typval_T *argvars;
15187 typval_T *rettv;
15188{
15189 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015190
15191 tag_pattern = get_tv_string(&argvars[0]);
15192
15193 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015194 if (*tag_pattern == NUL)
15195 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015196
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015197 if (rettv_list_alloc(rettv) == OK)
15198 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015199}
15200
15201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 * "tempname()" function
15203 */
15204/*ARGSUSED*/
15205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015206f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015207 typval_T *argvars;
15208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209{
15210 static int x = 'A';
15211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212 rettv->v_type = VAR_STRING;
15213 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214
15215 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15216 * names. Skip 'I' and 'O', they are used for shell redirection. */
15217 do
15218 {
15219 if (x == 'Z')
15220 x = '0';
15221 else if (x == '9')
15222 x = 'A';
15223 else
15224 {
15225#ifdef EBCDIC
15226 if (x == 'I')
15227 x = 'J';
15228 else if (x == 'R')
15229 x = 'S';
15230 else
15231#endif
15232 ++x;
15233 }
15234 } while (x == 'I' || x == 'O');
15235}
15236
15237/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015238 * "test(list)" function: Just checking the walls...
15239 */
15240/*ARGSUSED*/
15241 static void
15242f_test(argvars, rettv)
15243 typval_T *argvars;
15244 typval_T *rettv;
15245{
15246 /* Used for unit testing. Change the code below to your liking. */
15247#if 0
15248 listitem_T *li;
15249 list_T *l;
15250 char_u *bad, *good;
15251
15252 if (argvars[0].v_type != VAR_LIST)
15253 return;
15254 l = argvars[0].vval.v_list;
15255 if (l == NULL)
15256 return;
15257 li = l->lv_first;
15258 if (li == NULL)
15259 return;
15260 bad = get_tv_string(&li->li_tv);
15261 li = li->li_next;
15262 if (li == NULL)
15263 return;
15264 good = get_tv_string(&li->li_tv);
15265 rettv->vval.v_number = test_edit_score(bad, good);
15266#endif
15267}
15268
15269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 * "tolower(string)" function
15271 */
15272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015273f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015274 typval_T *argvars;
15275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276{
15277 char_u *p;
15278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015279 p = vim_strsave(get_tv_string(&argvars[0]));
15280 rettv->v_type = VAR_STRING;
15281 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282
15283 if (p != NULL)
15284 while (*p != NUL)
15285 {
15286#ifdef FEAT_MBYTE
15287 int l;
15288
15289 if (enc_utf8)
15290 {
15291 int c, lc;
15292
15293 c = utf_ptr2char(p);
15294 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015295 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296 /* TODO: reallocate string when byte count changes. */
15297 if (utf_char2len(lc) == l)
15298 utf_char2bytes(lc, p);
15299 p += l;
15300 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015301 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 p += l; /* skip multi-byte character */
15303 else
15304#endif
15305 {
15306 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15307 ++p;
15308 }
15309 }
15310}
15311
15312/*
15313 * "toupper(string)" function
15314 */
15315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015317 typval_T *argvars;
15318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015320 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015321 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322}
15323
15324/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015325 * "tr(string, fromstr, tostr)" function
15326 */
15327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015328f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015329 typval_T *argvars;
15330 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015331{
15332 char_u *instr;
15333 char_u *fromstr;
15334 char_u *tostr;
15335 char_u *p;
15336#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015337 int inlen;
15338 int fromlen;
15339 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015340 int idx;
15341 char_u *cpstr;
15342 int cplen;
15343 int first = TRUE;
15344#endif
15345 char_u buf[NUMBUFLEN];
15346 char_u buf2[NUMBUFLEN];
15347 garray_T ga;
15348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015350 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15351 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015352
15353 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015354 rettv->v_type = VAR_STRING;
15355 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015356 if (fromstr == NULL || tostr == NULL)
15357 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015358 ga_init2(&ga, (int)sizeof(char), 80);
15359
15360#ifdef FEAT_MBYTE
15361 if (!has_mbyte)
15362#endif
15363 /* not multi-byte: fromstr and tostr must be the same length */
15364 if (STRLEN(fromstr) != STRLEN(tostr))
15365 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015366#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015367error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015368#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015369 EMSG2(_(e_invarg2), fromstr);
15370 ga_clear(&ga);
15371 return;
15372 }
15373
15374 /* fromstr and tostr have to contain the same number of chars */
15375 while (*instr != NUL)
15376 {
15377#ifdef FEAT_MBYTE
15378 if (has_mbyte)
15379 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015380 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015381 cpstr = instr;
15382 cplen = inlen;
15383 idx = 0;
15384 for (p = fromstr; *p != NUL; p += fromlen)
15385 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015386 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015387 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15388 {
15389 for (p = tostr; *p != NUL; p += tolen)
15390 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015391 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015392 if (idx-- == 0)
15393 {
15394 cplen = tolen;
15395 cpstr = p;
15396 break;
15397 }
15398 }
15399 if (*p == NUL) /* tostr is shorter than fromstr */
15400 goto error;
15401 break;
15402 }
15403 ++idx;
15404 }
15405
15406 if (first && cpstr == instr)
15407 {
15408 /* Check that fromstr and tostr have the same number of
15409 * (multi-byte) characters. Done only once when a character
15410 * of instr doesn't appear in fromstr. */
15411 first = FALSE;
15412 for (p = tostr; *p != NUL; p += tolen)
15413 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015414 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015415 --idx;
15416 }
15417 if (idx != 0)
15418 goto error;
15419 }
15420
15421 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015422 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015423 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015424
15425 instr += inlen;
15426 }
15427 else
15428#endif
15429 {
15430 /* When not using multi-byte chars we can do it faster. */
15431 p = vim_strchr(fromstr, *instr);
15432 if (p != NULL)
15433 ga_append(&ga, tostr[p - fromstr]);
15434 else
15435 ga_append(&ga, *instr);
15436 ++instr;
15437 }
15438 }
15439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015441}
15442
15443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 * "type(expr)" function
15445 */
15446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015448 typval_T *argvars;
15449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015451 int n;
15452
15453 switch (argvars[0].v_type)
15454 {
15455 case VAR_NUMBER: n = 0; break;
15456 case VAR_STRING: n = 1; break;
15457 case VAR_FUNC: n = 2; break;
15458 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015459 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015460 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15461 }
15462 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463}
15464
15465/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015466 * "values(dict)" function
15467 */
15468 static void
15469f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015470 typval_T *argvars;
15471 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015472{
15473 dict_list(argvars, rettv, 1);
15474}
15475
15476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477 * "virtcol(string)" function
15478 */
15479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015480f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015481 typval_T *argvars;
15482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483{
15484 colnr_T vcol = 0;
15485 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015486 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015488 fp = var2fpos(&argvars[0], FALSE, &fnum);
15489 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15490 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 {
15492 getvvcol(curwin, fp, NULL, NULL, &vcol);
15493 ++vcol;
15494 }
15495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497}
15498
15499/*
15500 * "visualmode()" function
15501 */
15502/*ARGSUSED*/
15503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015504f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015505 typval_T *argvars;
15506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507{
15508#ifdef FEAT_VISUAL
15509 char_u str[2];
15510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 str[0] = curbuf->b_visual_mode_eval;
15513 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515
15516 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015517 if ((argvars[0].v_type == VAR_NUMBER
15518 && argvars[0].vval.v_number != 0)
15519 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 curbuf->b_visual_mode_eval = NUL;
15522#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015523 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524#endif
15525}
15526
15527/*
15528 * "winbufnr(nr)" function
15529 */
15530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015532 typval_T *argvars;
15533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534{
15535 win_T *wp;
15536
15537 wp = find_win_by_nr(&argvars[0]);
15538 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015539 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015541 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542}
15543
15544/*
15545 * "wincol()" function
15546 */
15547/*ARGSUSED*/
15548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015550 typval_T *argvars;
15551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552{
15553 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555}
15556
15557/*
15558 * "winheight(nr)" function
15559 */
15560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015561f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015562 typval_T *argvars;
15563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564{
15565 win_T *wp;
15566
15567 wp = find_win_by_nr(&argvars[0]);
15568 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015571 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572}
15573
15574/*
15575 * "winline()" function
15576 */
15577/*ARGSUSED*/
15578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015580 typval_T *argvars;
15581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582{
15583 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015584 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585}
15586
15587/*
15588 * "winnr()" function
15589 */
15590/* ARGSUSED */
15591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015593 typval_T *argvars;
15594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595{
15596 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015597
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015599 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015601 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602}
15603
15604/*
15605 * "winrestcmd()" function
15606 */
15607/* ARGSUSED */
15608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015609f_winrestcmd(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#ifdef FEAT_WINDOWS
15614 win_T *wp;
15615 int winnr = 1;
15616 garray_T ga;
15617 char_u buf[50];
15618
15619 ga_init2(&ga, (int)sizeof(char), 70);
15620 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15621 {
15622 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15623 ga_concat(&ga, buf);
15624# ifdef FEAT_VERTSPLIT
15625 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15626 ga_concat(&ga, buf);
15627# endif
15628 ++winnr;
15629 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015630 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015632 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015634 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015636 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637}
15638
15639/*
15640 * "winwidth(nr)" function
15641 */
15642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015643f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015644 typval_T *argvars;
15645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646{
15647 win_T *wp;
15648
15649 wp = find_win_by_nr(&argvars[0]);
15650 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015651 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 else
15653#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015654 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015656 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657#endif
15658}
15659
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015661 * "writefile()" function
15662 */
15663 static void
15664f_writefile(argvars, rettv)
15665 typval_T *argvars;
15666 typval_T *rettv;
15667{
15668 int binary = FALSE;
15669 char_u *fname;
15670 FILE *fd;
15671 listitem_T *li;
15672 char_u *s;
15673 int ret = 0;
15674 int c;
15675
15676 if (argvars[0].v_type != VAR_LIST)
15677 {
15678 EMSG2(_(e_listarg), "writefile()");
15679 return;
15680 }
15681 if (argvars[0].vval.v_list == NULL)
15682 return;
15683
15684 if (argvars[2].v_type != VAR_UNKNOWN
15685 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15686 binary = TRUE;
15687
15688 /* Always open the file in binary mode, library functions have a mind of
15689 * their own about CR-LF conversion. */
15690 fname = get_tv_string(&argvars[1]);
15691 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15692 {
15693 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15694 ret = -1;
15695 }
15696 else
15697 {
15698 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15699 li = li->li_next)
15700 {
15701 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15702 {
15703 if (*s == '\n')
15704 c = putc(NUL, fd);
15705 else
15706 c = putc(*s, fd);
15707 if (c == EOF)
15708 {
15709 ret = -1;
15710 break;
15711 }
15712 }
15713 if (!binary || li->li_next != NULL)
15714 if (putc('\n', fd) == EOF)
15715 {
15716 ret = -1;
15717 break;
15718 }
15719 if (ret < 0)
15720 {
15721 EMSG(_(e_write));
15722 break;
15723 }
15724 }
15725 fclose(fd);
15726 }
15727
15728 rettv->vval.v_number = ret;
15729}
15730
15731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015733 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 */
15735 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015736var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015737 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015739 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015741 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015743 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744
Bram Moolenaara5525202006-03-02 22:52:09 +000015745 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015746 if (varp->v_type == VAR_LIST)
15747 {
15748 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015749 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000015750 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015751
15752 l = varp->vval.v_list;
15753 if (l == NULL)
15754 return NULL;
15755
15756 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015757 pos.lnum = list_find_nr(l, 0L, &error);
15758 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015759 return NULL; /* invalid line number */
15760
15761 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015762 pos.col = list_find_nr(l, 1L, &error);
15763 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015764 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015765 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000015766 /* Accept a position up to the NUL after the line. */
15767 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015768 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015769 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015770
Bram Moolenaara5525202006-03-02 22:52:09 +000015771#ifdef FEAT_VIRTUALEDIT
15772 /* Get the virtual offset. Defaults to zero. */
15773 pos.coladd = list_find_nr(l, 2L, &error);
15774 if (error)
15775 pos.coladd = 0;
15776#endif
15777
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015778 return &pos;
15779 }
15780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015781 name = get_tv_string_chk(varp);
15782 if (name == NULL)
15783 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 if (name[0] == '.') /* cursor */
15785 return &curwin->w_cursor;
15786 if (name[0] == '\'') /* mark */
15787 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015788 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15790 return NULL;
15791 return pp;
15792 }
Bram Moolenaara5525202006-03-02 22:52:09 +000015793
15794#ifdef FEAT_VIRTUALEDIT
15795 pos.coladd = 0;
15796#endif
15797
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015798 if (name[0] == 'w' && lnum)
15799 {
15800 pos.col = 0;
15801 if (name[1] == '0') /* "w0": first visible line */
15802 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015803 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015804 pos.lnum = curwin->w_topline;
15805 return &pos;
15806 }
15807 else if (name[1] == '$') /* "w$": last visible line */
15808 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015809 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015810 pos.lnum = curwin->w_botline - 1;
15811 return &pos;
15812 }
15813 }
15814 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 {
15816 if (lnum)
15817 {
15818 pos.lnum = curbuf->b_ml.ml_line_count;
15819 pos.col = 0;
15820 }
15821 else
15822 {
15823 pos.lnum = curwin->w_cursor.lnum;
15824 pos.col = (colnr_T)STRLEN(ml_get_curline());
15825 }
15826 return &pos;
15827 }
15828 return NULL;
15829}
15830
15831/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015832 * Convert list in "arg" into a position and optional file number.
15833 * When "fnump" is NULL there is no file number, only 3 items.
15834 * Note that the column is passed on as-is, the caller may want to decrement
15835 * it to use 1 for the first column.
15836 * Return FAIL when conversion is not possible, doesn't check the position for
15837 * validity.
15838 */
15839 static int
15840list2fpos(arg, posp, fnump)
15841 typval_T *arg;
15842 pos_T *posp;
15843 int *fnump;
15844{
15845 list_T *l = arg->vval.v_list;
15846 long i = 0;
15847 long n;
15848
15849 /* List must be: [fnum, lnum, col, coladd] */
15850 if (arg->v_type != VAR_LIST || l == NULL
15851 || l->lv_len != (fnump == NULL ? 3 : 4))
15852 return FAIL;
15853
15854 if (fnump != NULL)
15855 {
15856 n = list_find_nr(l, i++, NULL); /* fnum */
15857 if (n < 0)
15858 return FAIL;
15859 if (n == 0)
15860 n = curbuf->b_fnum; /* current buffer */
15861 *fnump = n;
15862 }
15863
15864 n = list_find_nr(l, i++, NULL); /* lnum */
15865 if (n < 0)
15866 return FAIL;
15867 posp->lnum = n;
15868
15869 n = list_find_nr(l, i++, NULL); /* col */
15870 if (n < 0)
15871 return FAIL;
15872 posp->col = n;
15873
15874#ifdef FEAT_VIRTUALEDIT
15875 n = list_find_nr(l, i, NULL);
15876 if (n < 0)
15877 return FAIL;
15878 posp->coladd = n;
15879#endif
15880
15881 return OK;
15882}
15883
15884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 * Get the length of an environment variable name.
15886 * Advance "arg" to the first character after the name.
15887 * Return 0 for error.
15888 */
15889 static int
15890get_env_len(arg)
15891 char_u **arg;
15892{
15893 char_u *p;
15894 int len;
15895
15896 for (p = *arg; vim_isIDc(*p); ++p)
15897 ;
15898 if (p == *arg) /* no name found */
15899 return 0;
15900
15901 len = (int)(p - *arg);
15902 *arg = p;
15903 return len;
15904}
15905
15906/*
15907 * Get the length of the name of a function or internal variable.
15908 * "arg" is advanced to the first non-white character after the name.
15909 * Return 0 if something is wrong.
15910 */
15911 static int
15912get_id_len(arg)
15913 char_u **arg;
15914{
15915 char_u *p;
15916 int len;
15917
15918 /* Find the end of the name. */
15919 for (p = *arg; eval_isnamec(*p); ++p)
15920 ;
15921 if (p == *arg) /* no name found */
15922 return 0;
15923
15924 len = (int)(p - *arg);
15925 *arg = skipwhite(p);
15926
15927 return len;
15928}
15929
15930/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015931 * Get the length of the name of a variable or function.
15932 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015934 * Return -1 if curly braces expansion failed.
15935 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 * If the name contains 'magic' {}'s, expand them and return the
15937 * expanded name in an allocated string via 'alias' - caller must free.
15938 */
15939 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015940get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941 char_u **arg;
15942 char_u **alias;
15943 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015944 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945{
15946 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 char_u *p;
15948 char_u *expr_start;
15949 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950
15951 *alias = NULL; /* default to no alias */
15952
15953 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15954 && (*arg)[2] == (int)KE_SNR)
15955 {
15956 /* hard coded <SNR>, already translated */
15957 *arg += 3;
15958 return get_id_len(arg) + 3;
15959 }
15960 len = eval_fname_script(*arg);
15961 if (len > 0)
15962 {
15963 /* literal "<SID>", "s:" or "<SNR>" */
15964 *arg += len;
15965 }
15966
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015968 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015970 p = find_name_end(*arg, &expr_start, &expr_end,
15971 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 if (expr_start != NULL)
15973 {
15974 char_u *temp_string;
15975
15976 if (!evaluate)
15977 {
15978 len += (int)(p - *arg);
15979 *arg = skipwhite(p);
15980 return len;
15981 }
15982
15983 /*
15984 * Include any <SID> etc in the expanded string:
15985 * Thus the -len here.
15986 */
15987 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15988 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015989 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 *alias = temp_string;
15991 *arg = skipwhite(p);
15992 return (int)STRLEN(temp_string);
15993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994
15995 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015996 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 EMSG2(_(e_invexpr2), *arg);
15998
15999 return len;
16000}
16001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016002/*
16003 * Find the end of a variable or function name, taking care of magic braces.
16004 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16005 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016006 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016007 * Return a pointer to just after the name. Equal to "arg" if there is no
16008 * valid name.
16009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016011find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 char_u *arg;
16013 char_u **expr_start;
16014 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016015 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016017 int mb_nest = 0;
16018 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 char_u *p;
16020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016021 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016023 *expr_start = NULL;
16024 *expr_end = NULL;
16025 }
16026
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016027 /* Quick check for valid starting character. */
16028 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16029 return arg;
16030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016031 for (p = arg; *p != NUL
16032 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016033 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016034 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016035 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016036 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016037 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016038 if (*p == '\'')
16039 {
16040 /* skip over 'string' to avoid counting [ and ] inside it. */
16041 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16042 ;
16043 if (*p == NUL)
16044 break;
16045 }
16046 else if (*p == '"')
16047 {
16048 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16049 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16050 if (*p == '\\' && p[1] != NUL)
16051 ++p;
16052 if (*p == NUL)
16053 break;
16054 }
16055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016056 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016058 if (*p == '[')
16059 ++br_nest;
16060 else if (*p == ']')
16061 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016064 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016066 if (*p == '{')
16067 {
16068 mb_nest++;
16069 if (expr_start != NULL && *expr_start == NULL)
16070 *expr_start = p;
16071 }
16072 else if (*p == '}')
16073 {
16074 mb_nest--;
16075 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16076 *expr_end = p;
16077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079 }
16080
16081 return p;
16082}
16083
16084/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016085 * Expands out the 'magic' {}'s in a variable/function name.
16086 * Note that this can call itself recursively, to deal with
16087 * constructs like foo{bar}{baz}{bam}
16088 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16089 * "in_start" ^
16090 * "expr_start" ^
16091 * "expr_end" ^
16092 * "in_end" ^
16093 *
16094 * Returns a new allocated string, which the caller must free.
16095 * Returns NULL for failure.
16096 */
16097 static char_u *
16098make_expanded_name(in_start, expr_start, expr_end, in_end)
16099 char_u *in_start;
16100 char_u *expr_start;
16101 char_u *expr_end;
16102 char_u *in_end;
16103{
16104 char_u c1;
16105 char_u *retval = NULL;
16106 char_u *temp_result;
16107 char_u *nextcmd = NULL;
16108
16109 if (expr_end == NULL || in_end == NULL)
16110 return NULL;
16111 *expr_start = NUL;
16112 *expr_end = NUL;
16113 c1 = *in_end;
16114 *in_end = NUL;
16115
16116 temp_result = eval_to_string(expr_start + 1, &nextcmd);
16117 if (temp_result != NULL && nextcmd == NULL)
16118 {
16119 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16120 + (in_end - expr_end) + 1));
16121 if (retval != NULL)
16122 {
16123 STRCPY(retval, in_start);
16124 STRCAT(retval, temp_result);
16125 STRCAT(retval, expr_end + 1);
16126 }
16127 }
16128 vim_free(temp_result);
16129
16130 *in_end = c1; /* put char back for error messages */
16131 *expr_start = '{';
16132 *expr_end = '}';
16133
16134 if (retval != NULL)
16135 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016136 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016137 if (expr_start != NULL)
16138 {
16139 /* Further expansion! */
16140 temp_result = make_expanded_name(retval, expr_start,
16141 expr_end, temp_result);
16142 vim_free(retval);
16143 retval = temp_result;
16144 }
16145 }
16146
16147 return retval;
16148}
16149
16150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016152 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 */
16154 static int
16155eval_isnamec(c)
16156 int c;
16157{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016158 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16159}
16160
16161/*
16162 * Return TRUE if character "c" can be used as the first character in a
16163 * variable or function name (excluding '{' and '}').
16164 */
16165 static int
16166eval_isnamec1(c)
16167 int c;
16168{
16169 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170}
16171
16172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 * Set number v: variable to "val".
16174 */
16175 void
16176set_vim_var_nr(idx, val)
16177 int idx;
16178 long val;
16179{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016180 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181}
16182
16183/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016184 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 */
16186 long
16187get_vim_var_nr(idx)
16188 int idx;
16189{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016190 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191}
16192
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016193#if defined(FEAT_AUTOCMD) || defined(PROTO)
16194/*
16195 * Get string v: variable value. Uses a static buffer, can only be used once.
16196 */
16197 char_u *
16198get_vim_var_str(idx)
16199 int idx;
16200{
16201 return get_tv_string(&vimvars[idx].vv_tv);
16202}
16203#endif
16204
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205/*
16206 * Set v:count, v:count1 and v:prevcount.
16207 */
16208 void
16209set_vcount(count, count1)
16210 long count;
16211 long count1;
16212{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016213 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16214 vimvars[VV_COUNT].vv_nr = count;
16215 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216}
16217
16218/*
16219 * Set string v: variable to a copy of "val".
16220 */
16221 void
16222set_vim_var_string(idx, val, len)
16223 int idx;
16224 char_u *val;
16225 int len; /* length of "val" to use or -1 (whole string) */
16226{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016227 /* Need to do this (at least) once, since we can't initialize a union.
16228 * Will always be invoked when "v:progname" is set. */
16229 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16230
Bram Moolenaare9a41262005-01-15 22:18:47 +000016231 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016233 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016235 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016237 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238}
16239
16240/*
16241 * Set v:register if needed.
16242 */
16243 void
16244set_reg_var(c)
16245 int c;
16246{
16247 char_u regname;
16248
16249 if (c == 0 || c == ' ')
16250 regname = '"';
16251 else
16252 regname = c;
16253 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016254 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 set_vim_var_string(VV_REG, &regname, 1);
16256}
16257
16258/*
16259 * Get or set v:exception. If "oldval" == NULL, return the current value.
16260 * Otherwise, restore the value to "oldval" and return NULL.
16261 * Must always be called in pairs to save and restore v:exception! Does not
16262 * take care of memory allocations.
16263 */
16264 char_u *
16265v_exception(oldval)
16266 char_u *oldval;
16267{
16268 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016269 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270
Bram Moolenaare9a41262005-01-15 22:18:47 +000016271 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 return NULL;
16273}
16274
16275/*
16276 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16277 * Otherwise, restore the value to "oldval" and return NULL.
16278 * Must always be called in pairs to save and restore v:throwpoint! Does not
16279 * take care of memory allocations.
16280 */
16281 char_u *
16282v_throwpoint(oldval)
16283 char_u *oldval;
16284{
16285 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016286 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287
Bram Moolenaare9a41262005-01-15 22:18:47 +000016288 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 return NULL;
16290}
16291
16292#if defined(FEAT_AUTOCMD) || defined(PROTO)
16293/*
16294 * Set v:cmdarg.
16295 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16296 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16297 * Must always be called in pairs!
16298 */
16299 char_u *
16300set_cmdarg(eap, oldarg)
16301 exarg_T *eap;
16302 char_u *oldarg;
16303{
16304 char_u *oldval;
16305 char_u *newval;
16306 unsigned len;
16307
Bram Moolenaare9a41262005-01-15 22:18:47 +000016308 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016309 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016311 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016312 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016313 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 }
16315
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016316 if (eap->force_bin == FORCE_BIN)
16317 len = 6;
16318 else if (eap->force_bin == FORCE_NOBIN)
16319 len = 8;
16320 else
16321 len = 0;
16322 if (eap->force_ff != 0)
16323 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16324# ifdef FEAT_MBYTE
16325 if (eap->force_enc != 0)
16326 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016327 if (eap->bad_char != 0)
16328 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016329# endif
16330
16331 newval = alloc(len + 1);
16332 if (newval == NULL)
16333 return NULL;
16334
16335 if (eap->force_bin == FORCE_BIN)
16336 sprintf((char *)newval, " ++bin");
16337 else if (eap->force_bin == FORCE_NOBIN)
16338 sprintf((char *)newval, " ++nobin");
16339 else
16340 *newval = NUL;
16341 if (eap->force_ff != 0)
16342 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16343 eap->cmd + eap->force_ff);
16344# ifdef FEAT_MBYTE
16345 if (eap->force_enc != 0)
16346 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16347 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016348 if (eap->bad_char != 0)
16349 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16350 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016351# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016352 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016353 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354}
16355#endif
16356
16357/*
16358 * Get the value of internal variable "name".
16359 * Return OK or FAIL.
16360 */
16361 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016362get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363 char_u *name;
16364 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016365 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016366 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367{
16368 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016369 typval_T *tv = NULL;
16370 typval_T atv;
16371 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373
16374 /* truncate the name, so that we can use strcmp() */
16375 cc = name[len];
16376 name[len] = NUL;
16377
16378 /*
16379 * Check for "b:changedtick".
16380 */
16381 if (STRCMP(name, "b:changedtick") == 0)
16382 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016383 atv.v_type = VAR_NUMBER;
16384 atv.vval.v_number = curbuf->b_changedtick;
16385 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 }
16387
16388 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 * Check for user-defined variables.
16390 */
16391 else
16392 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016393 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016395 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 }
16397
Bram Moolenaare9a41262005-01-15 22:18:47 +000016398 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016400 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016401 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 ret = FAIL;
16403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016404 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016405 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406
16407 name[len] = cc;
16408
16409 return ret;
16410}
16411
16412/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016413 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16414 * Also handle function call with Funcref variable: func(expr)
16415 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16416 */
16417 static int
16418handle_subscript(arg, rettv, evaluate, verbose)
16419 char_u **arg;
16420 typval_T *rettv;
16421 int evaluate; /* do more than finding the end */
16422 int verbose; /* give error messages */
16423{
16424 int ret = OK;
16425 dict_T *selfdict = NULL;
16426 char_u *s;
16427 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016428 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016429
16430 while (ret == OK
16431 && (**arg == '['
16432 || (**arg == '.' && rettv->v_type == VAR_DICT)
16433 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16434 && !vim_iswhite(*(*arg - 1)))
16435 {
16436 if (**arg == '(')
16437 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016438 /* need to copy the funcref so that we can clear rettv */
16439 functv = *rettv;
16440 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016441
16442 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016443 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016444 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016445 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16446 &len, evaluate, selfdict);
16447
16448 /* Clear the funcref afterwards, so that deleting it while
16449 * evaluating the arguments is possible (see test55). */
16450 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016451
16452 /* Stop the expression evaluation when immediately aborting on
16453 * error, or when an interrupt occurred or an exception was thrown
16454 * but not caught. */
16455 if (aborting())
16456 {
16457 if (ret == OK)
16458 clear_tv(rettv);
16459 ret = FAIL;
16460 }
16461 dict_unref(selfdict);
16462 selfdict = NULL;
16463 }
16464 else /* **arg == '[' || **arg == '.' */
16465 {
16466 dict_unref(selfdict);
16467 if (rettv->v_type == VAR_DICT)
16468 {
16469 selfdict = rettv->vval.v_dict;
16470 if (selfdict != NULL)
16471 ++selfdict->dv_refcount;
16472 }
16473 else
16474 selfdict = NULL;
16475 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16476 {
16477 clear_tv(rettv);
16478 ret = FAIL;
16479 }
16480 }
16481 }
16482 dict_unref(selfdict);
16483 return ret;
16484}
16485
16486/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016487 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16488 * value).
16489 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016491alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016492{
Bram Moolenaar33570922005-01-25 22:26:29 +000016493 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016494}
16495
16496/*
16497 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 * The string "s" must have been allocated, it is consumed.
16499 * Return NULL for out of memory, the variable otherwise.
16500 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016501 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 char_u *s;
16504{
Bram Moolenaar33570922005-01-25 22:26:29 +000016505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016507 rettv = alloc_tv();
16508 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510 rettv->v_type = VAR_STRING;
16511 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512 }
16513 else
16514 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016515 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516}
16517
16518/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016519 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016521 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016522free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016523 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524{
16525 if (varp != NULL)
16526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016527 switch (varp->v_type)
16528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016529 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016530 func_unref(varp->vval.v_string);
16531 /*FALLTHROUGH*/
16532 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016533 vim_free(varp->vval.v_string);
16534 break;
16535 case VAR_LIST:
16536 list_unref(varp->vval.v_list);
16537 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016538 case VAR_DICT:
16539 dict_unref(varp->vval.v_dict);
16540 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016541 case VAR_NUMBER:
16542 case VAR_UNKNOWN:
16543 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016544 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016545 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016546 break;
16547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 vim_free(varp);
16549 }
16550}
16551
16552/*
16553 * Free the memory for a variable value and set the value to NULL or 0.
16554 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016555 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016556clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016557 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558{
16559 if (varp != NULL)
16560 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016561 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016563 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016564 func_unref(varp->vval.v_string);
16565 /*FALLTHROUGH*/
16566 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016567 vim_free(varp->vval.v_string);
16568 varp->vval.v_string = NULL;
16569 break;
16570 case VAR_LIST:
16571 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016572 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016573 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016574 case VAR_DICT:
16575 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016576 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016577 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016578 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016579 varp->vval.v_number = 0;
16580 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016581 case VAR_UNKNOWN:
16582 break;
16583 default:
16584 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016586 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 }
16588}
16589
16590/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016591 * Set the value of a variable to NULL without freeing items.
16592 */
16593 static void
16594init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016595 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016596{
16597 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016598 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016599}
16600
16601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 * Get the number value of a variable.
16603 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016604 * For incompatible types, return 0.
16605 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16606 * caller of incompatible types: it sets *denote to TRUE if "denote"
16607 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 */
16609 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016610get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016611 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016613 int error = FALSE;
16614
16615 return get_tv_number_chk(varp, &error); /* return 0L on error */
16616}
16617
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016618 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016619get_tv_number_chk(varp, denote)
16620 typval_T *varp;
16621 int *denote;
16622{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016623 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016625 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016627 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016628 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016629 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016630 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016631 break;
16632 case VAR_STRING:
16633 if (varp->vval.v_string != NULL)
16634 vim_str2nr(varp->vval.v_string, NULL, NULL,
16635 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016636 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016637 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016638 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016639 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016640 case VAR_DICT:
16641 EMSG(_("E728: Using a Dictionary as a number"));
16642 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016643 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016644 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016645 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016647 if (denote == NULL) /* useful for values that must be unsigned */
16648 n = -1;
16649 else
16650 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016651 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652}
16653
16654/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016655 * Get the lnum from the first argument.
16656 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016657 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 */
16659 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016660get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016661 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662{
Bram Moolenaar33570922005-01-25 22:26:29 +000016663 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 linenr_T lnum;
16665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016666 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 if (lnum == 0) /* no valid number, try using line() */
16668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016669 rettv.v_type = VAR_NUMBER;
16670 f_line(argvars, &rettv);
16671 lnum = rettv.vval.v_number;
16672 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 }
16674 return lnum;
16675}
16676
16677/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016678 * Get the lnum from the first argument.
16679 * Also accepts "$", then "buf" is used.
16680 * Returns 0 on error.
16681 */
16682 static linenr_T
16683get_tv_lnum_buf(argvars, buf)
16684 typval_T *argvars;
16685 buf_T *buf;
16686{
16687 if (argvars[0].v_type == VAR_STRING
16688 && argvars[0].vval.v_string != NULL
16689 && argvars[0].vval.v_string[0] == '$'
16690 && buf != NULL)
16691 return buf->b_ml.ml_line_count;
16692 return get_tv_number_chk(&argvars[0], NULL);
16693}
16694
16695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 * Get the string value of a variable.
16697 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016698 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16699 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 * If the String variable has never been set, return an empty string.
16701 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016702 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16703 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 */
16705 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016706get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016707 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708{
16709 static char_u mybuf[NUMBUFLEN];
16710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016711 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712}
16713
16714 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016715get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016716 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016717 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016719 char_u *res = get_tv_string_buf_chk(varp, buf);
16720
16721 return res != NULL ? res : (char_u *)"";
16722}
16723
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016724 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016725get_tv_string_chk(varp)
16726 typval_T *varp;
16727{
16728 static char_u mybuf[NUMBUFLEN];
16729
16730 return get_tv_string_buf_chk(varp, mybuf);
16731}
16732
16733 static char_u *
16734get_tv_string_buf_chk(varp, buf)
16735 typval_T *varp;
16736 char_u *buf;
16737{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016738 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016740 case VAR_NUMBER:
16741 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16742 return buf;
16743 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016744 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016745 break;
16746 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016747 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016748 break;
16749 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016750 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016751 break;
16752 case VAR_STRING:
16753 if (varp->vval.v_string != NULL)
16754 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016755 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016756 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016757 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016758 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016760 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761}
16762
16763/*
16764 * Find variable "name" in the list of variables.
16765 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016766 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016767 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016768 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016770 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016771find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016773 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016776 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777
Bram Moolenaara7043832005-01-21 11:56:39 +000016778 ht = find_var_ht(name, &varname);
16779 if (htp != NULL)
16780 *htp = ht;
16781 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016783 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784}
16785
16786/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016787 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016788 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016790 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016791find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016792 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016793 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016794 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016795{
Bram Moolenaar33570922005-01-25 22:26:29 +000016796 hashitem_T *hi;
16797
16798 if (*varname == NUL)
16799 {
16800 /* Must be something like "s:", otherwise "ht" would be NULL. */
16801 switch (varname[-2])
16802 {
16803 case 's': return &SCRIPT_SV(current_SID).sv_var;
16804 case 'g': return &globvars_var;
16805 case 'v': return &vimvars_var;
16806 case 'b': return &curbuf->b_bufvar;
16807 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016808 case 'l': return current_funccal == NULL
16809 ? NULL : &current_funccal->l_vars_var;
16810 case 'a': return current_funccal == NULL
16811 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016812 }
16813 return NULL;
16814 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016815
16816 hi = hash_find(ht, varname);
16817 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016818 {
16819 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016820 * worked find the variable again. Don't auto-load a script if it was
16821 * loaded already, otherwise it would be loaded every time when
16822 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016823 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016824 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016825 hi = hash_find(ht, varname);
16826 if (HASHITEM_EMPTY(hi))
16827 return NULL;
16828 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016829 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016830}
16831
16832/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016833 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016834 * Set "varname" to the start of name without ':'.
16835 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016836 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016837find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 char_u *name;
16839 char_u **varname;
16840{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016841 hashitem_T *hi;
16842
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 if (name[1] != ':')
16844 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016845 /* The name must not start with a colon or #. */
16846 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847 return NULL;
16848 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016849
16850 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016851 hi = hash_find(&compat_hashtab, name);
16852 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016853 return &compat_hashtab;
16854
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016856 return &globvarht; /* global variable */
16857 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 }
16859 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016860 if (*name == 'g') /* global variable */
16861 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016862 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16863 */
16864 if (vim_strchr(name + 2, ':') != NULL
16865 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016866 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016868 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016870 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016871 if (*name == 'v') /* v: variable */
16872 return &vimvarht;
16873 if (*name == 'a' && current_funccal != NULL) /* function argument */
16874 return &current_funccal->l_avars.dv_hashtab;
16875 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16876 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 if (*name == 's' /* script variable */
16878 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16879 return &SCRIPT_VARS(current_SID);
16880 return NULL;
16881}
16882
16883/*
16884 * Get the string value of a (global/local) variable.
16885 * Returns NULL when it doesn't exist.
16886 */
16887 char_u *
16888get_var_value(name)
16889 char_u *name;
16890{
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892
Bram Moolenaara7043832005-01-21 11:56:39 +000016893 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 if (v == NULL)
16895 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016896 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897}
16898
16899/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 * sourcing this script and when executing functions defined in the script.
16902 */
16903 void
16904new_script_vars(id)
16905 scid_T id;
16906{
Bram Moolenaara7043832005-01-21 11:56:39 +000016907 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016908 hashtab_T *ht;
16909 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016910
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16912 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016913 /* Re-allocating ga_data means that an ht_array pointing to
16914 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016915 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016916 for (i = 1; i <= ga_scripts.ga_len; ++i)
16917 {
16918 ht = &SCRIPT_VARS(i);
16919 if (ht->ht_mask == HT_INIT_SIZE - 1)
16920 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016921 sv = &SCRIPT_SV(i);
16922 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016923 }
16924
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 while (ga_scripts.ga_len < id)
16926 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16928 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 }
16931 }
16932}
16933
16934/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016935 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16936 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 */
16938 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016939init_var_dict(dict, dict_var)
16940 dict_T *dict;
16941 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942{
Bram Moolenaar33570922005-01-25 22:26:29 +000016943 hash_init(&dict->dv_hashtab);
16944 dict->dv_refcount = 99999;
16945 dict_var->di_tv.vval.v_dict = dict;
16946 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016947 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016948 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16949 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950}
16951
16952/*
16953 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016954 * Frees all allocated variables and the value they contain.
16955 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 */
16957 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016958vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 hashtab_T *ht;
16960{
16961 vars_clear_ext(ht, TRUE);
16962}
16963
16964/*
16965 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16966 */
16967 static void
16968vars_clear_ext(ht, free_val)
16969 hashtab_T *ht;
16970 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971{
Bram Moolenaara7043832005-01-21 11:56:39 +000016972 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 hashitem_T *hi;
16974 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016977 todo = ht->ht_used;
16978 for (hi = ht->ht_array; todo > 0; ++hi)
16979 {
16980 if (!HASHITEM_EMPTY(hi))
16981 {
16982 --todo;
16983
Bram Moolenaar33570922005-01-25 22:26:29 +000016984 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016985 * ht_array might change then. hash_clear() takes care of it
16986 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016987 v = HI2DI(hi);
16988 if (free_val)
16989 clear_tv(&v->di_tv);
16990 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16991 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016992 }
16993 }
16994 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016995 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996}
16997
Bram Moolenaara7043832005-01-21 11:56:39 +000016998/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016999 * Delete a variable from hashtab "ht" at item "hi".
17000 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017003delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017004 hashtab_T *ht;
17005 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006{
Bram Moolenaar33570922005-01-25 22:26:29 +000017007 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017008
17009 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017010 clear_tv(&di->di_tv);
17011 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012}
17013
17014/*
17015 * List the value of one internal variable.
17016 */
17017 static void
17018list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017019 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 char_u *prefix;
17021{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017022 char_u *tofree;
17023 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017024 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017025
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017026 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017027 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017028 s == NULL ? (char_u *)"" : s);
17029 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030}
17031
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032 static void
17033list_one_var_a(prefix, name, type, string)
17034 char_u *prefix;
17035 char_u *name;
17036 int type;
17037 char_u *string;
17038{
17039 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17040 if (name != NULL) /* "a:" vars don't have a name stored */
17041 msg_puts(name);
17042 msg_putchar(' ');
17043 msg_advance(22);
17044 if (type == VAR_NUMBER)
17045 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017046 else if (type == VAR_FUNC)
17047 msg_putchar('*');
17048 else if (type == VAR_LIST)
17049 {
17050 msg_putchar('[');
17051 if (*string == '[')
17052 ++string;
17053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017054 else if (type == VAR_DICT)
17055 {
17056 msg_putchar('{');
17057 if (*string == '{')
17058 ++string;
17059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 else
17061 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017062
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017064
17065 if (type == VAR_FUNC)
17066 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067}
17068
17069/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017070 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071 * If the variable already exists, the value is updated.
17072 * Otherwise the variable is created.
17073 */
17074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017077 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017078 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079{
Bram Moolenaar33570922005-01-25 22:26:29 +000017080 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017082 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017083 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017085 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017086 {
17087 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17088 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17089 ? name[2] : name[0]))
17090 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017091 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017092 return;
17093 }
17094 if (function_exists(name))
17095 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017096 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017097 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017098 return;
17099 }
17100 }
17101
Bram Moolenaara7043832005-01-21 11:56:39 +000017102 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017103 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017104 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017105 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017106 return;
17107 }
17108
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017109 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017110 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017112 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017113 if (var_check_ro(v->di_flags, name)
17114 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017115 return;
17116 if (v->di_tv.v_type != tv->v_type
17117 && !((v->di_tv.v_type == VAR_STRING
17118 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119 && (tv->v_type == VAR_STRING
17120 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017121 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017122 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017123 return;
17124 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017125
17126 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017127 * Handle setting internal v: variables separately: we don't change
17128 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 */
17130 if (ht == &vimvarht)
17131 {
17132 if (v->di_tv.v_type == VAR_STRING)
17133 {
17134 vim_free(v->di_tv.vval.v_string);
17135 if (copy || tv->v_type != VAR_STRING)
17136 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17137 else
17138 {
17139 /* Take over the string to avoid an extra alloc/free. */
17140 v->di_tv.vval.v_string = tv->vval.v_string;
17141 tv->vval.v_string = NULL;
17142 }
17143 }
17144 else if (v->di_tv.v_type != VAR_NUMBER)
17145 EMSG2(_(e_intern2), "set_var()");
17146 else
17147 v->di_tv.vval.v_number = get_tv_number(tv);
17148 return;
17149 }
17150
17151 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 }
17153 else /* add a new variable */
17154 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017155 /* Make sure the variable name is valid. */
17156 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017157 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17158 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017159 {
17160 EMSG2(_(e_illvar), varname);
17161 return;
17162 }
17163
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017164 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17165 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017166 if (v == NULL)
17167 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017168 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017169 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017171 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 return;
17173 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017174 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017177 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017178 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017179 else
17180 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017181 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017182 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017183 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185}
17186
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017187/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017188 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17189 * Also give an error message.
17190 */
17191 static int
17192var_check_ro(flags, name)
17193 int flags;
17194 char_u *name;
17195{
17196 if (flags & DI_FLAGS_RO)
17197 {
17198 EMSG2(_(e_readonlyvar), name);
17199 return TRUE;
17200 }
17201 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17202 {
17203 EMSG2(_(e_readonlysbx), name);
17204 return TRUE;
17205 }
17206 return FALSE;
17207}
17208
17209/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017210 * Return TRUE if typeval "tv" is set to be locked (immutable).
17211 * Also give an error message, using "name".
17212 */
17213 static int
17214tv_check_lock(lock, name)
17215 int lock;
17216 char_u *name;
17217{
17218 if (lock & VAR_LOCKED)
17219 {
17220 EMSG2(_("E741: Value is locked: %s"),
17221 name == NULL ? (char_u *)_("Unknown") : name);
17222 return TRUE;
17223 }
17224 if (lock & VAR_FIXED)
17225 {
17226 EMSG2(_("E742: Cannot change value of %s"),
17227 name == NULL ? (char_u *)_("Unknown") : name);
17228 return TRUE;
17229 }
17230 return FALSE;
17231}
17232
17233/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017234 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017235 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017236 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017239copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017240 typval_T *from;
17241 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017243 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017244 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017245 switch (from->v_type)
17246 {
17247 case VAR_NUMBER:
17248 to->vval.v_number = from->vval.v_number;
17249 break;
17250 case VAR_STRING:
17251 case VAR_FUNC:
17252 if (from->vval.v_string == NULL)
17253 to->vval.v_string = NULL;
17254 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017255 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017256 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017257 if (from->v_type == VAR_FUNC)
17258 func_ref(to->vval.v_string);
17259 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017260 break;
17261 case VAR_LIST:
17262 if (from->vval.v_list == NULL)
17263 to->vval.v_list = NULL;
17264 else
17265 {
17266 to->vval.v_list = from->vval.v_list;
17267 ++to->vval.v_list->lv_refcount;
17268 }
17269 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017270 case VAR_DICT:
17271 if (from->vval.v_dict == NULL)
17272 to->vval.v_dict = NULL;
17273 else
17274 {
17275 to->vval.v_dict = from->vval.v_dict;
17276 ++to->vval.v_dict->dv_refcount;
17277 }
17278 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017279 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017280 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017281 break;
17282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283}
17284
17285/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017286 * Make a copy of an item.
17287 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017288 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17289 * reference to an already copied list/dict can be used.
17290 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017291 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017292 static int
17293item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017294 typval_T *from;
17295 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017296 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017297 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017298{
17299 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017300 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017301
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017303 {
17304 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017305 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017306 }
17307 ++recurse;
17308
17309 switch (from->v_type)
17310 {
17311 case VAR_NUMBER:
17312 case VAR_STRING:
17313 case VAR_FUNC:
17314 copy_tv(from, to);
17315 break;
17316 case VAR_LIST:
17317 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017318 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017319 if (from->vval.v_list == NULL)
17320 to->vval.v_list = NULL;
17321 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17322 {
17323 /* use the copy made earlier */
17324 to->vval.v_list = from->vval.v_list->lv_copylist;
17325 ++to->vval.v_list->lv_refcount;
17326 }
17327 else
17328 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17329 if (to->vval.v_list == NULL)
17330 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017331 break;
17332 case VAR_DICT:
17333 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017334 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017335 if (from->vval.v_dict == NULL)
17336 to->vval.v_dict = NULL;
17337 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17338 {
17339 /* use the copy made earlier */
17340 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17341 ++to->vval.v_dict->dv_refcount;
17342 }
17343 else
17344 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17345 if (to->vval.v_dict == NULL)
17346 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017347 break;
17348 default:
17349 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017350 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017351 }
17352 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017353 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017354}
17355
17356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 * ":echo expr1 ..." print each argument separated with a space, add a
17358 * newline at the end.
17359 * ":echon expr1 ..." print each argument plain.
17360 */
17361 void
17362ex_echo(eap)
17363 exarg_T *eap;
17364{
17365 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017366 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017367 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368 char_u *p;
17369 int needclr = TRUE;
17370 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017371 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372
17373 if (eap->skip)
17374 ++emsg_skip;
17375 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17376 {
17377 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 {
17380 /*
17381 * Report the invalid expression unless the expression evaluation
17382 * has been cancelled due to an aborting error, an interrupt, or an
17383 * exception.
17384 */
17385 if (!aborting())
17386 EMSG2(_(e_invexpr2), p);
17387 break;
17388 }
17389 if (!eap->skip)
17390 {
17391 if (atstart)
17392 {
17393 atstart = FALSE;
17394 /* Call msg_start() after eval1(), evaluating the expression
17395 * may cause a message to appear. */
17396 if (eap->cmdidx == CMD_echo)
17397 msg_start();
17398 }
17399 else if (eap->cmdidx == CMD_echo)
17400 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017401 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017402 if (p != NULL)
17403 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017405 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017407 if (*p != TAB && needclr)
17408 {
17409 /* remove any text still there from the command */
17410 msg_clr_eos();
17411 needclr = FALSE;
17412 }
17413 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414 }
17415 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017416 {
17417#ifdef FEAT_MBYTE
17418 if (has_mbyte)
17419 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017420 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017421
17422 (void)msg_outtrans_len_attr(p, i, echo_attr);
17423 p += i - 1;
17424 }
17425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017427 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017430 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433 arg = skipwhite(arg);
17434 }
17435 eap->nextcmd = check_nextcmd(arg);
17436
17437 if (eap->skip)
17438 --emsg_skip;
17439 else
17440 {
17441 /* remove text that may still be there from the command */
17442 if (needclr)
17443 msg_clr_eos();
17444 if (eap->cmdidx == CMD_echo)
17445 msg_end();
17446 }
17447}
17448
17449/*
17450 * ":echohl {name}".
17451 */
17452 void
17453ex_echohl(eap)
17454 exarg_T *eap;
17455{
17456 int id;
17457
17458 id = syn_name2id(eap->arg);
17459 if (id == 0)
17460 echo_attr = 0;
17461 else
17462 echo_attr = syn_id2attr(id);
17463}
17464
17465/*
17466 * ":execute expr1 ..." execute the result of an expression.
17467 * ":echomsg expr1 ..." Print a message
17468 * ":echoerr expr1 ..." Print an error
17469 * Each gets spaces around each argument and a newline at the end for
17470 * echo commands
17471 */
17472 void
17473ex_execute(eap)
17474 exarg_T *eap;
17475{
17476 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017477 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 int ret = OK;
17479 char_u *p;
17480 garray_T ga;
17481 int len;
17482 int save_did_emsg;
17483
17484 ga_init2(&ga, 1, 80);
17485
17486 if (eap->skip)
17487 ++emsg_skip;
17488 while (*arg != NUL && *arg != '|' && *arg != '\n')
17489 {
17490 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017491 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 {
17493 /*
17494 * Report the invalid expression unless the expression evaluation
17495 * has been cancelled due to an aborting error, an interrupt, or an
17496 * exception.
17497 */
17498 if (!aborting())
17499 EMSG2(_(e_invexpr2), p);
17500 ret = FAIL;
17501 break;
17502 }
17503
17504 if (!eap->skip)
17505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017506 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 len = (int)STRLEN(p);
17508 if (ga_grow(&ga, len + 2) == FAIL)
17509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017510 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 ret = FAIL;
17512 break;
17513 }
17514 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 ga.ga_len += len;
17518 }
17519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 arg = skipwhite(arg);
17522 }
17523
17524 if (ret != FAIL && ga.ga_data != NULL)
17525 {
17526 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017529 out_flush();
17530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 else if (eap->cmdidx == CMD_echoerr)
17532 {
17533 /* We don't want to abort following commands, restore did_emsg. */
17534 save_did_emsg = did_emsg;
17535 EMSG((char_u *)ga.ga_data);
17536 if (!force_abort)
17537 did_emsg = save_did_emsg;
17538 }
17539 else if (eap->cmdidx == CMD_execute)
17540 do_cmdline((char_u *)ga.ga_data,
17541 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17542 }
17543
17544 ga_clear(&ga);
17545
17546 if (eap->skip)
17547 --emsg_skip;
17548
17549 eap->nextcmd = check_nextcmd(arg);
17550}
17551
17552/*
17553 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17554 * "arg" points to the "&" or '+' when called, to "option" when returning.
17555 * Returns NULL when no option name found. Otherwise pointer to the char
17556 * after the option name.
17557 */
17558 static char_u *
17559find_option_end(arg, opt_flags)
17560 char_u **arg;
17561 int *opt_flags;
17562{
17563 char_u *p = *arg;
17564
17565 ++p;
17566 if (*p == 'g' && p[1] == ':')
17567 {
17568 *opt_flags = OPT_GLOBAL;
17569 p += 2;
17570 }
17571 else if (*p == 'l' && p[1] == ':')
17572 {
17573 *opt_flags = OPT_LOCAL;
17574 p += 2;
17575 }
17576 else
17577 *opt_flags = 0;
17578
17579 if (!ASCII_ISALPHA(*p))
17580 return NULL;
17581 *arg = p;
17582
17583 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17584 p += 4; /* termcap option */
17585 else
17586 while (ASCII_ISALPHA(*p))
17587 ++p;
17588 return p;
17589}
17590
17591/*
17592 * ":function"
17593 */
17594 void
17595ex_function(eap)
17596 exarg_T *eap;
17597{
17598 char_u *theline;
17599 int j;
17600 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 char_u *name = NULL;
17603 char_u *p;
17604 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017605 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 garray_T newargs;
17607 garray_T newlines;
17608 int varargs = FALSE;
17609 int mustend = FALSE;
17610 int flags = 0;
17611 ufunc_T *fp;
17612 int indent;
17613 int nesting;
17614 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017615 dictitem_T *v;
17616 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017617 static int func_nr = 0; /* number for nameless function */
17618 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017619 hashtab_T *ht;
17620 int todo;
17621 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017622 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623
17624 /*
17625 * ":function" without argument: list functions.
17626 */
17627 if (ends_excmd(*eap->arg))
17628 {
17629 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017630 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017631 todo = func_hashtab.ht_used;
17632 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017633 {
17634 if (!HASHITEM_EMPTY(hi))
17635 {
17636 --todo;
17637 fp = HI2UF(hi);
17638 if (!isdigit(*fp->uf_name))
17639 list_func_head(fp, FALSE);
17640 }
17641 }
17642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 eap->nextcmd = check_nextcmd(eap->arg);
17644 return;
17645 }
17646
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017647 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017648 * ":function /pat": list functions matching pattern.
17649 */
17650 if (*eap->arg == '/')
17651 {
17652 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17653 if (!eap->skip)
17654 {
17655 regmatch_T regmatch;
17656
17657 c = *p;
17658 *p = NUL;
17659 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17660 *p = c;
17661 if (regmatch.regprog != NULL)
17662 {
17663 regmatch.rm_ic = p_ic;
17664
17665 todo = func_hashtab.ht_used;
17666 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17667 {
17668 if (!HASHITEM_EMPTY(hi))
17669 {
17670 --todo;
17671 fp = HI2UF(hi);
17672 if (!isdigit(*fp->uf_name)
17673 && vim_regexec(&regmatch, fp->uf_name, 0))
17674 list_func_head(fp, FALSE);
17675 }
17676 }
17677 }
17678 }
17679 if (*p == '/')
17680 ++p;
17681 eap->nextcmd = check_nextcmd(p);
17682 return;
17683 }
17684
17685 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017686 * Get the function name. There are these situations:
17687 * func normal function name
17688 * "name" == func, "fudi.fd_dict" == NULL
17689 * dict.func new dictionary entry
17690 * "name" == NULL, "fudi.fd_dict" set,
17691 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17692 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017693 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017694 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17695 * dict.func existing dict entry that's not a Funcref
17696 * "name" == NULL, "fudi.fd_dict" set,
17697 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017700 name = trans_function_name(&p, eap->skip, 0, &fudi);
17701 paren = (vim_strchr(p, '(') != NULL);
17702 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 {
17704 /*
17705 * Return on an invalid expression in braces, unless the expression
17706 * evaluation has been cancelled due to an aborting error, an
17707 * interrupt, or an exception.
17708 */
17709 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017710 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017711 if (!eap->skip && fudi.fd_newkey != NULL)
17712 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017713 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 else
17717 eap->skip = TRUE;
17718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 /* An error in a function call during evaluation of an expression in magic
17720 * braces should not cause the function not to be defined. */
17721 saved_did_emsg = did_emsg;
17722 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723
17724 /*
17725 * ":function func" with only function name: list function.
17726 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017727 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 {
17729 if (!ends_excmd(*skipwhite(p)))
17730 {
17731 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017732 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 }
17734 eap->nextcmd = check_nextcmd(p);
17735 if (eap->nextcmd != NULL)
17736 *p = NUL;
17737 if (!eap->skip && !got_int)
17738 {
17739 fp = find_func(name);
17740 if (fp != NULL)
17741 {
17742 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017743 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017745 if (FUNCLINE(fp, j) == NULL)
17746 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 msg_putchar('\n');
17748 msg_outnum((long)(j + 1));
17749 if (j < 9)
17750 msg_putchar(' ');
17751 if (j < 99)
17752 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017753 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 out_flush(); /* show a line at a time */
17755 ui_breakcheck();
17756 }
17757 if (!got_int)
17758 {
17759 msg_putchar('\n');
17760 msg_puts((char_u *)" endfunction");
17761 }
17762 }
17763 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017764 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017766 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 }
17768
17769 /*
17770 * ":function name(arg1, arg2)" Define function.
17771 */
17772 p = skipwhite(p);
17773 if (*p != '(')
17774 {
17775 if (!eap->skip)
17776 {
17777 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017778 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 }
17780 /* attempt to continue by skipping some text */
17781 if (vim_strchr(p, '(') != NULL)
17782 p = vim_strchr(p, '(');
17783 }
17784 p = skipwhite(p + 1);
17785
17786 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17787 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17788
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017789 if (!eap->skip)
17790 {
17791 /* Check the name of the function. */
17792 if (name != NULL)
17793 arg = name;
17794 else
17795 arg = fudi.fd_newkey;
17796 if (arg != NULL)
17797 {
17798 if (*arg == K_SPECIAL)
17799 j = 3;
17800 else
17801 j = 0;
17802 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17803 : eval_isnamec(arg[j])))
17804 ++j;
17805 if (arg[j] != NUL)
17806 emsg_funcname(_(e_invarg2), arg);
17807 }
17808 }
17809
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 /*
17811 * Isolate the arguments: "arg1, arg2, ...)"
17812 */
17813 while (*p != ')')
17814 {
17815 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17816 {
17817 varargs = TRUE;
17818 p += 3;
17819 mustend = TRUE;
17820 }
17821 else
17822 {
17823 arg = p;
17824 while (ASCII_ISALNUM(*p) || *p == '_')
17825 ++p;
17826 if (arg == p || isdigit(*arg)
17827 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17828 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17829 {
17830 if (!eap->skip)
17831 EMSG2(_("E125: Illegal argument: %s"), arg);
17832 break;
17833 }
17834 if (ga_grow(&newargs, 1) == FAIL)
17835 goto erret;
17836 c = *p;
17837 *p = NUL;
17838 arg = vim_strsave(arg);
17839 if (arg == NULL)
17840 goto erret;
17841 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17842 *p = c;
17843 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 if (*p == ',')
17845 ++p;
17846 else
17847 mustend = TRUE;
17848 }
17849 p = skipwhite(p);
17850 if (mustend && *p != ')')
17851 {
17852 if (!eap->skip)
17853 EMSG2(_(e_invarg2), eap->arg);
17854 break;
17855 }
17856 }
17857 ++p; /* skip the ')' */
17858
Bram Moolenaare9a41262005-01-15 22:18:47 +000017859 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 for (;;)
17861 {
17862 p = skipwhite(p);
17863 if (STRNCMP(p, "range", 5) == 0)
17864 {
17865 flags |= FC_RANGE;
17866 p += 5;
17867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017868 else if (STRNCMP(p, "dict", 4) == 0)
17869 {
17870 flags |= FC_DICT;
17871 p += 4;
17872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 else if (STRNCMP(p, "abort", 5) == 0)
17874 {
17875 flags |= FC_ABORT;
17876 p += 5;
17877 }
17878 else
17879 break;
17880 }
17881
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017882 /* When there is a line break use what follows for the function body.
17883 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17884 if (*p == '\n')
17885 line_arg = p + 1;
17886 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887 EMSG(_(e_trailing));
17888
17889 /*
17890 * Read the body of the function, until ":endfunction" is found.
17891 */
17892 if (KeyTyped)
17893 {
17894 /* Check if the function already exists, don't let the user type the
17895 * whole function before telling him it doesn't work! For a script we
17896 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017897 if (!eap->skip && !eap->forceit)
17898 {
17899 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17900 EMSG(_(e_funcdict));
17901 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017902 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017905 if (!eap->skip && did_emsg)
17906 goto erret;
17907
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 msg_putchar('\n'); /* don't overwrite the function name */
17909 cmdline_row = msg_row;
17910 }
17911
17912 indent = 2;
17913 nesting = 0;
17914 for (;;)
17915 {
17916 msg_scroll = TRUE;
17917 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017918 sourcing_lnum_off = sourcing_lnum;
17919
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017920 if (line_arg != NULL)
17921 {
17922 /* Use eap->arg, split up in parts by line breaks. */
17923 theline = line_arg;
17924 p = vim_strchr(theline, '\n');
17925 if (p == NULL)
17926 line_arg += STRLEN(line_arg);
17927 else
17928 {
17929 *p = NUL;
17930 line_arg = p + 1;
17931 }
17932 }
17933 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 theline = getcmdline(':', 0L, indent);
17935 else
17936 theline = eap->getline(':', eap->cookie, indent);
17937 if (KeyTyped)
17938 lines_left = Rows - 1;
17939 if (theline == NULL)
17940 {
17941 EMSG(_("E126: Missing :endfunction"));
17942 goto erret;
17943 }
17944
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017945 /* Detect line continuation: sourcing_lnum increased more than one. */
17946 if (sourcing_lnum > sourcing_lnum_off + 1)
17947 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
17948 else
17949 sourcing_lnum_off = 0;
17950
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 if (skip_until != NULL)
17952 {
17953 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17954 * don't check for ":endfunc". */
17955 if (STRCMP(theline, skip_until) == 0)
17956 {
17957 vim_free(skip_until);
17958 skip_until = NULL;
17959 }
17960 }
17961 else
17962 {
17963 /* skip ':' and blanks*/
17964 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17965 ;
17966
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017967 /* Check for "endfunction". */
17968 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017970 if (line_arg == NULL)
17971 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 break;
17973 }
17974
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017975 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 * at "end". */
17977 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17978 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017979 else if (STRNCMP(p, "if", 2) == 0
17980 || STRNCMP(p, "wh", 2) == 0
17981 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 || STRNCMP(p, "try", 3) == 0)
17983 indent += 2;
17984
17985 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017986 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017988 if (*p == '!')
17989 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 p += eval_fname_script(p);
17991 if (ASCII_ISALPHA(*p))
17992 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017993 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 if (*skipwhite(p) == '(')
17995 {
17996 ++nesting;
17997 indent += 2;
17998 }
17999 }
18000 }
18001
18002 /* Check for ":append" or ":insert". */
18003 p = skip_range(p, NULL);
18004 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18005 || (p[0] == 'i'
18006 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18007 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18008 skip_until = vim_strsave((char_u *)".");
18009
18010 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18011 arg = skipwhite(skiptowhite(p));
18012 if (arg[0] == '<' && arg[1] =='<'
18013 && ((p[0] == 'p' && p[1] == 'y'
18014 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18015 || (p[0] == 'p' && p[1] == 'e'
18016 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18017 || (p[0] == 't' && p[1] == 'c'
18018 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18019 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18020 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018021 || (p[0] == 'm' && p[1] == 'z'
18022 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 ))
18024 {
18025 /* ":python <<" continues until a dot, like ":append" */
18026 p = skipwhite(arg + 2);
18027 if (*p == NUL)
18028 skip_until = vim_strsave((char_u *)".");
18029 else
18030 skip_until = vim_strsave(p);
18031 }
18032 }
18033
18034 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018035 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018036 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018037 if (line_arg == NULL)
18038 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018040 }
18041
18042 /* Copy the line to newly allocated memory. get_one_sourceline()
18043 * allocates 250 bytes per line, this saves 80% on average. The cost
18044 * is an extra alloc/free. */
18045 p = vim_strsave(theline);
18046 if (p != NULL)
18047 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018048 if (line_arg == NULL)
18049 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018050 theline = p;
18051 }
18052
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018053 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18054
18055 /* Add NULL lines for continuation lines, so that the line count is
18056 * equal to the index in the growarray. */
18057 while (sourcing_lnum_off-- > 0)
18058 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018059
18060 /* Check for end of eap->arg. */
18061 if (line_arg != NULL && *line_arg == NUL)
18062 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 }
18064
18065 /* Don't define the function when skipping commands or when an error was
18066 * detected. */
18067 if (eap->skip || did_emsg)
18068 goto erret;
18069
18070 /*
18071 * If there are no errors, add the function
18072 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018073 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018074 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018075 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018076 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018077 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018078 emsg_funcname("E707: Function name conflicts with variable: %s",
18079 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018080 goto erret;
18081 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018082
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018083 fp = find_func(name);
18084 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018086 if (!eap->forceit)
18087 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018088 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018089 goto erret;
18090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018091 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018092 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018093 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018094 name);
18095 goto erret;
18096 }
18097 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018098 ga_clear_strings(&(fp->uf_args));
18099 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018100 vim_free(name);
18101 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 }
18104 else
18105 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018106 char numbuf[20];
18107
18108 fp = NULL;
18109 if (fudi.fd_newkey == NULL && !eap->forceit)
18110 {
18111 EMSG(_(e_funcdict));
18112 goto erret;
18113 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018114 if (fudi.fd_di == NULL)
18115 {
18116 /* Can't add a function to a locked dictionary */
18117 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18118 goto erret;
18119 }
18120 /* Can't change an existing function if it is locked */
18121 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18122 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018123
18124 /* Give the function a sequential number. Can only be used with a
18125 * Funcref! */
18126 vim_free(name);
18127 sprintf(numbuf, "%d", ++func_nr);
18128 name = vim_strsave((char_u *)numbuf);
18129 if (name == NULL)
18130 goto erret;
18131 }
18132
18133 if (fp == NULL)
18134 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018135 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018136 {
18137 int slen, plen;
18138 char_u *scriptname;
18139
18140 /* Check that the autoload name matches the script name. */
18141 j = FAIL;
18142 if (sourcing_name != NULL)
18143 {
18144 scriptname = autoload_name(name);
18145 if (scriptname != NULL)
18146 {
18147 p = vim_strchr(scriptname, '/');
18148 plen = STRLEN(p);
18149 slen = STRLEN(sourcing_name);
18150 if (slen > plen && fnamecmp(p,
18151 sourcing_name + slen - plen) == 0)
18152 j = OK;
18153 vim_free(scriptname);
18154 }
18155 }
18156 if (j == FAIL)
18157 {
18158 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18159 goto erret;
18160 }
18161 }
18162
18163 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 if (fp == NULL)
18165 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018166
18167 if (fudi.fd_dict != NULL)
18168 {
18169 if (fudi.fd_di == NULL)
18170 {
18171 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018172 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018173 if (fudi.fd_di == NULL)
18174 {
18175 vim_free(fp);
18176 goto erret;
18177 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018178 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18179 {
18180 vim_free(fudi.fd_di);
18181 goto erret;
18182 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018183 }
18184 else
18185 /* overwrite existing dict entry */
18186 clear_tv(&fudi.fd_di->di_tv);
18187 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018188 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018189 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018190 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018191 }
18192
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018194 STRCPY(fp->uf_name, name);
18195 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018197 fp->uf_args = newargs;
18198 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018199#ifdef FEAT_PROFILE
18200 fp->uf_tml_count = NULL;
18201 fp->uf_tml_total = NULL;
18202 fp->uf_tml_self = NULL;
18203 fp->uf_profiling = FALSE;
18204 if (prof_def_func())
18205 func_do_profile(fp);
18206#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018207 fp->uf_varargs = varargs;
18208 fp->uf_flags = flags;
18209 fp->uf_calls = 0;
18210 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018211 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212
18213erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 ga_clear_strings(&newargs);
18215 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018216ret_free:
18217 vim_free(skip_until);
18218 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221}
18222
18223/*
18224 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018225 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018227 * flags:
18228 * TFN_INT: internal function name OK
18229 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 * Advances "pp" to just after the function name (if no error).
18231 */
18232 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018233trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 char_u **pp;
18235 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018236 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018237 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018239 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 char_u *start;
18241 char_u *end;
18242 int lead;
18243 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018245 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018246
18247 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018248 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018250
18251 /* Check for hard coded <SNR>: already translated function ID (from a user
18252 * command). */
18253 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18254 && (*pp)[2] == (int)KE_SNR)
18255 {
18256 *pp += 3;
18257 len = get_id_len(pp) + 3;
18258 return vim_strnsave(start, len);
18259 }
18260
18261 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18262 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018264 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018266
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018267 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18268 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018269 if (end == start)
18270 {
18271 if (!skip)
18272 EMSG(_("E129: Function name required"));
18273 goto theend;
18274 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018275 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018276 {
18277 /*
18278 * Report an invalid expression in braces, unless the expression
18279 * evaluation has been cancelled due to an aborting error, an
18280 * interrupt, or an exception.
18281 */
18282 if (!aborting())
18283 {
18284 if (end != NULL)
18285 EMSG2(_(e_invarg2), start);
18286 }
18287 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018288 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018289 goto theend;
18290 }
18291
18292 if (lv.ll_tv != NULL)
18293 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018294 if (fdp != NULL)
18295 {
18296 fdp->fd_dict = lv.ll_dict;
18297 fdp->fd_newkey = lv.ll_newkey;
18298 lv.ll_newkey = NULL;
18299 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018300 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018301 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18302 {
18303 name = vim_strsave(lv.ll_tv->vval.v_string);
18304 *pp = end;
18305 }
18306 else
18307 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018308 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18309 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018310 EMSG(_(e_funcref));
18311 else
18312 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018313 name = NULL;
18314 }
18315 goto theend;
18316 }
18317
18318 if (lv.ll_name == NULL)
18319 {
18320 /* Error found, but continue after the function name. */
18321 *pp = end;
18322 goto theend;
18323 }
18324
18325 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018326 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018327 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018328 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18329 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18330 {
18331 /* When there was "s:" already or the name expanded to get a
18332 * leading "s:" then remove it. */
18333 lv.ll_name += 2;
18334 len -= 2;
18335 lead = 2;
18336 }
18337 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018338 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018339 {
18340 if (lead == 2) /* skip over "s:" */
18341 lv.ll_name += 2;
18342 len = (int)(end - lv.ll_name);
18343 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018344
18345 /*
18346 * Copy the function name to allocated memory.
18347 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18348 * Accept <SNR>123_name() outside a script.
18349 */
18350 if (skip)
18351 lead = 0; /* do nothing */
18352 else if (lead > 0)
18353 {
18354 lead = 3;
18355 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18356 {
18357 if (current_SID <= 0)
18358 {
18359 EMSG(_(e_usingsid));
18360 goto theend;
18361 }
18362 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18363 lead += (int)STRLEN(sid_buf);
18364 }
18365 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018366 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018367 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018368 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018369 goto theend;
18370 }
18371 name = alloc((unsigned)(len + lead + 1));
18372 if (name != NULL)
18373 {
18374 if (lead > 0)
18375 {
18376 name[0] = K_SPECIAL;
18377 name[1] = KS_EXTRA;
18378 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018379 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018380 STRCPY(name + 3, sid_buf);
18381 }
18382 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18383 name[len + lead] = NUL;
18384 }
18385 *pp = end;
18386
18387theend:
18388 clear_lval(&lv);
18389 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390}
18391
18392/*
18393 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18394 * Return 2 if "p" starts with "s:".
18395 * Return 0 otherwise.
18396 */
18397 static int
18398eval_fname_script(p)
18399 char_u *p;
18400{
18401 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18402 || STRNICMP(p + 1, "SNR>", 4) == 0))
18403 return 5;
18404 if (p[0] == 's' && p[1] == ':')
18405 return 2;
18406 return 0;
18407}
18408
18409/*
18410 * Return TRUE if "p" starts with "<SID>" or "s:".
18411 * Only works if eval_fname_script() returned non-zero for "p"!
18412 */
18413 static int
18414eval_fname_sid(p)
18415 char_u *p;
18416{
18417 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18418}
18419
18420/*
18421 * List the head of the function: "name(arg1, arg2)".
18422 */
18423 static void
18424list_func_head(fp, indent)
18425 ufunc_T *fp;
18426 int indent;
18427{
18428 int j;
18429
18430 msg_start();
18431 if (indent)
18432 MSG_PUTS(" ");
18433 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018434 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 {
18436 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018437 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 }
18439 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018440 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018442 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443 {
18444 if (j)
18445 MSG_PUTS(", ");
18446 msg_puts(FUNCARG(fp, j));
18447 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018448 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 {
18450 if (j)
18451 MSG_PUTS(", ");
18452 MSG_PUTS("...");
18453 }
18454 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018455 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018456 if (p_verbose > 0)
18457 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458}
18459
18460/*
18461 * Find a function by name, return pointer to it in ufuncs.
18462 * Return NULL for unknown function.
18463 */
18464 static ufunc_T *
18465find_func(name)
18466 char_u *name;
18467{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018468 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018470 hi = hash_find(&func_hashtab, name);
18471 if (!HASHITEM_EMPTY(hi))
18472 return HI2UF(hi);
18473 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474}
18475
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018476#if defined(EXITFREE) || defined(PROTO)
18477 void
18478free_all_functions()
18479{
18480 hashitem_T *hi;
18481
18482 /* Need to start all over every time, because func_free() may change the
18483 * hash table. */
18484 while (func_hashtab.ht_used > 0)
18485 for (hi = func_hashtab.ht_array; ; ++hi)
18486 if (!HASHITEM_EMPTY(hi))
18487 {
18488 func_free(HI2UF(hi));
18489 break;
18490 }
18491}
18492#endif
18493
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018494/*
18495 * Return TRUE if a function "name" exists.
18496 */
18497 static int
18498function_exists(name)
18499 char_u *name;
18500{
18501 char_u *p = name;
18502 int n = FALSE;
18503
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018504 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018505 if (p != NULL)
18506 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018507 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018508 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018509 else
18510 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018511 vim_free(p);
18512 }
18513 return n;
18514}
18515
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018516/*
18517 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018518 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018519 */
18520 static int
18521builtin_function(name)
18522 char_u *name;
18523{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018524 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18525 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018526}
18527
Bram Moolenaar05159a02005-02-26 23:04:13 +000018528#if defined(FEAT_PROFILE) || defined(PROTO)
18529/*
18530 * Start profiling function "fp".
18531 */
18532 static void
18533func_do_profile(fp)
18534 ufunc_T *fp;
18535{
18536 fp->uf_tm_count = 0;
18537 profile_zero(&fp->uf_tm_self);
18538 profile_zero(&fp->uf_tm_total);
18539 if (fp->uf_tml_count == NULL)
18540 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18541 (sizeof(int) * fp->uf_lines.ga_len));
18542 if (fp->uf_tml_total == NULL)
18543 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18544 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18545 if (fp->uf_tml_self == NULL)
18546 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18547 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18548 fp->uf_tml_idx = -1;
18549 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18550 || fp->uf_tml_self == NULL)
18551 return; /* out of memory */
18552
18553 fp->uf_profiling = TRUE;
18554}
18555
18556/*
18557 * Dump the profiling results for all functions in file "fd".
18558 */
18559 void
18560func_dump_profile(fd)
18561 FILE *fd;
18562{
18563 hashitem_T *hi;
18564 int todo;
18565 ufunc_T *fp;
18566 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018567 ufunc_T **sorttab;
18568 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018569
18570 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018571 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18572
Bram Moolenaar05159a02005-02-26 23:04:13 +000018573 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18574 {
18575 if (!HASHITEM_EMPTY(hi))
18576 {
18577 --todo;
18578 fp = HI2UF(hi);
18579 if (fp->uf_profiling)
18580 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018581 if (sorttab != NULL)
18582 sorttab[st_len++] = fp;
18583
Bram Moolenaar05159a02005-02-26 23:04:13 +000018584 if (fp->uf_name[0] == K_SPECIAL)
18585 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18586 else
18587 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18588 if (fp->uf_tm_count == 1)
18589 fprintf(fd, "Called 1 time\n");
18590 else
18591 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18592 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18593 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18594 fprintf(fd, "\n");
18595 fprintf(fd, "count total (s) self (s)\n");
18596
18597 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18598 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018599 if (FUNCLINE(fp, i) == NULL)
18600 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018601 prof_func_line(fd, fp->uf_tml_count[i],
18602 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018603 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18604 }
18605 fprintf(fd, "\n");
18606 }
18607 }
18608 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018609
18610 if (sorttab != NULL && st_len > 0)
18611 {
18612 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18613 prof_total_cmp);
18614 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18615 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18616 prof_self_cmp);
18617 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18618 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018619}
Bram Moolenaar73830342005-02-28 22:48:19 +000018620
18621 static void
18622prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18623 FILE *fd;
18624 ufunc_T **sorttab;
18625 int st_len;
18626 char *title;
18627 int prefer_self; /* when equal print only self time */
18628{
18629 int i;
18630 ufunc_T *fp;
18631
18632 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18633 fprintf(fd, "count total (s) self (s) function\n");
18634 for (i = 0; i < 20 && i < st_len; ++i)
18635 {
18636 fp = sorttab[i];
18637 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18638 prefer_self);
18639 if (fp->uf_name[0] == K_SPECIAL)
18640 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18641 else
18642 fprintf(fd, " %s()\n", fp->uf_name);
18643 }
18644 fprintf(fd, "\n");
18645}
18646
18647/*
18648 * Print the count and times for one function or function line.
18649 */
18650 static void
18651prof_func_line(fd, count, total, self, prefer_self)
18652 FILE *fd;
18653 int count;
18654 proftime_T *total;
18655 proftime_T *self;
18656 int prefer_self; /* when equal print only self time */
18657{
18658 if (count > 0)
18659 {
18660 fprintf(fd, "%5d ", count);
18661 if (prefer_self && profile_equal(total, self))
18662 fprintf(fd, " ");
18663 else
18664 fprintf(fd, "%s ", profile_msg(total));
18665 if (!prefer_self && profile_equal(total, self))
18666 fprintf(fd, " ");
18667 else
18668 fprintf(fd, "%s ", profile_msg(self));
18669 }
18670 else
18671 fprintf(fd, " ");
18672}
18673
18674/*
18675 * Compare function for total time sorting.
18676 */
18677 static int
18678#ifdef __BORLANDC__
18679_RTLENTRYF
18680#endif
18681prof_total_cmp(s1, s2)
18682 const void *s1;
18683 const void *s2;
18684{
18685 ufunc_T *p1, *p2;
18686
18687 p1 = *(ufunc_T **)s1;
18688 p2 = *(ufunc_T **)s2;
18689 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18690}
18691
18692/*
18693 * Compare function for self time sorting.
18694 */
18695 static int
18696#ifdef __BORLANDC__
18697_RTLENTRYF
18698#endif
18699prof_self_cmp(s1, s2)
18700 const void *s1;
18701 const void *s2;
18702{
18703 ufunc_T *p1, *p2;
18704
18705 p1 = *(ufunc_T **)s1;
18706 p2 = *(ufunc_T **)s2;
18707 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18708}
18709
Bram Moolenaar05159a02005-02-26 23:04:13 +000018710#endif
18711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018712/* The names of packages that once were loaded is remembered. */
18713static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18714
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018715/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018716 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018717 * Return TRUE if a package was loaded.
18718 */
18719 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018720script_autoload(name, reload)
18721 char_u *name;
18722 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018723{
18724 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018725 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018726 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018727 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018728
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018729 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018730 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018731 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018732 return FALSE;
18733
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018734 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018735
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018736 /* Find the name in the list of previously loaded package names. Skip
18737 * "autoload/", it's always the same. */
18738 for (i = 0; i < ga_loaded.ga_len; ++i)
18739 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18740 break;
18741 if (!reload && i < ga_loaded.ga_len)
18742 ret = FALSE; /* was loaded already */
18743 else
18744 {
18745 /* Remember the name if it wasn't loaded already. */
18746 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18747 {
18748 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18749 tofree = NULL;
18750 }
18751
18752 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018753 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018754 ret = TRUE;
18755 }
18756
18757 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018758 return ret;
18759}
18760
18761/*
18762 * Return the autoload script name for a function or variable name.
18763 * Returns NULL when out of memory.
18764 */
18765 static char_u *
18766autoload_name(name)
18767 char_u *name;
18768{
18769 char_u *p;
18770 char_u *scriptname;
18771
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018772 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018773 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18774 if (scriptname == NULL)
18775 return FALSE;
18776 STRCPY(scriptname, "autoload/");
18777 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018778 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018779 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018780 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018781 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018782 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018783}
18784
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18786
18787/*
18788 * Function given to ExpandGeneric() to obtain the list of user defined
18789 * function names.
18790 */
18791 char_u *
18792get_user_func_name(xp, idx)
18793 expand_T *xp;
18794 int idx;
18795{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018796 static long_u done;
18797 static hashitem_T *hi;
18798 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799
18800 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018802 done = 0;
18803 hi = func_hashtab.ht_array;
18804 }
18805 if (done < func_hashtab.ht_used)
18806 {
18807 if (done++ > 0)
18808 ++hi;
18809 while (HASHITEM_EMPTY(hi))
18810 ++hi;
18811 fp = HI2UF(hi);
18812
18813 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18814 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815
18816 cat_func_name(IObuff, fp);
18817 if (xp->xp_context != EXPAND_USER_FUNC)
18818 {
18819 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018820 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 STRCAT(IObuff, ")");
18822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 return IObuff;
18824 }
18825 return NULL;
18826}
18827
18828#endif /* FEAT_CMDL_COMPL */
18829
18830/*
18831 * Copy the function name of "fp" to buffer "buf".
18832 * "buf" must be able to hold the function name plus three bytes.
18833 * Takes care of script-local function names.
18834 */
18835 static void
18836cat_func_name(buf, fp)
18837 char_u *buf;
18838 ufunc_T *fp;
18839{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018840 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 {
18842 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018843 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844 }
18845 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018846 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847}
18848
18849/*
18850 * ":delfunction {name}"
18851 */
18852 void
18853ex_delfunction(eap)
18854 exarg_T *eap;
18855{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018856 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 char_u *p;
18858 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018859 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860
18861 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018862 name = trans_function_name(&p, eap->skip, 0, &fudi);
18863 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018865 {
18866 if (fudi.fd_dict != NULL && !eap->skip)
18867 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 if (!ends_excmd(*skipwhite(p)))
18871 {
18872 vim_free(name);
18873 EMSG(_(e_trailing));
18874 return;
18875 }
18876 eap->nextcmd = check_nextcmd(p);
18877 if (eap->nextcmd != NULL)
18878 *p = NUL;
18879
18880 if (!eap->skip)
18881 fp = find_func(name);
18882 vim_free(name);
18883
18884 if (!eap->skip)
18885 {
18886 if (fp == NULL)
18887 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018888 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889 return;
18890 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018891 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 {
18893 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18894 return;
18895 }
18896
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018897 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018899 /* Delete the dict item that refers to the function, it will
18900 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018901 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018903 else
18904 func_free(fp);
18905 }
18906}
18907
18908/*
18909 * Free a function and remove it from the list of functions.
18910 */
18911 static void
18912func_free(fp)
18913 ufunc_T *fp;
18914{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018915 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018916
18917 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018918 ga_clear_strings(&(fp->uf_args));
18919 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018920#ifdef FEAT_PROFILE
18921 vim_free(fp->uf_tml_count);
18922 vim_free(fp->uf_tml_total);
18923 vim_free(fp->uf_tml_self);
18924#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018925
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018926 /* remove the function from the function hashtable */
18927 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18928 if (HASHITEM_EMPTY(hi))
18929 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018930 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018931 hash_remove(&func_hashtab, hi);
18932
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018933 vim_free(fp);
18934}
18935
18936/*
18937 * Unreference a Function: decrement the reference count and free it when it
18938 * becomes zero. Only for numbered functions.
18939 */
18940 static void
18941func_unref(name)
18942 char_u *name;
18943{
18944 ufunc_T *fp;
18945
18946 if (name != NULL && isdigit(*name))
18947 {
18948 fp = find_func(name);
18949 if (fp == NULL)
18950 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018951 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018952 {
18953 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018954 * when "uf_calls" becomes zero. */
18955 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018956 func_free(fp);
18957 }
18958 }
18959}
18960
18961/*
18962 * Count a reference to a Function.
18963 */
18964 static void
18965func_ref(name)
18966 char_u *name;
18967{
18968 ufunc_T *fp;
18969
18970 if (name != NULL && isdigit(*name))
18971 {
18972 fp = find_func(name);
18973 if (fp == NULL)
18974 EMSG2(_(e_intern2), "func_ref()");
18975 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018976 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 }
18978}
18979
18980/*
18981 * Call a user function.
18982 */
18983 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018984call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 ufunc_T *fp; /* pointer to function */
18986 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018987 typval_T *argvars; /* arguments */
18988 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 linenr_T firstline; /* first line of range */
18990 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018991 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992{
Bram Moolenaar33570922005-01-25 22:26:29 +000018993 char_u *save_sourcing_name;
18994 linenr_T save_sourcing_lnum;
18995 scid_T save_current_SID;
18996 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018997 int save_did_emsg;
18998 static int depth = 0;
18999 dictitem_T *v;
19000 int fixvar_idx = 0; /* index in fixvar[] */
19001 int i;
19002 int ai;
19003 char_u numbuf[NUMBUFLEN];
19004 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019005#ifdef FEAT_PROFILE
19006 proftime_T wait_start;
19007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008
19009 /* If depth of calling is getting too high, don't execute the function */
19010 if (depth >= p_mfd)
19011 {
19012 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019013 rettv->v_type = VAR_NUMBER;
19014 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 return;
19016 }
19017 ++depth;
19018
19019 line_breakcheck(); /* check for CTRL-C hit */
19020
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019021 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019022 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019024 fc.rettv = rettv;
19025 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 fc.linenr = 0;
19027 fc.returned = FALSE;
19028 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019030 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 fc.dbg_tick = debug_tick;
19032
Bram Moolenaar33570922005-01-25 22:26:29 +000019033 /*
19034 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19035 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19036 * each argument variable and saves a lot of time.
19037 */
19038 /*
19039 * Init l: variables.
19040 */
19041 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019042 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019043 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019044 /* Set l:self to "selfdict". */
19045 v = &fc.fixvar[fixvar_idx++].var;
19046 STRCPY(v->di_key, "self");
19047 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19048 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19049 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019050 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019051 v->di_tv.vval.v_dict = selfdict;
19052 ++selfdict->dv_refcount;
19053 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019054
Bram Moolenaar33570922005-01-25 22:26:29 +000019055 /*
19056 * Init a: variables.
19057 * Set a:0 to "argcount".
19058 * Set a:000 to a list with room for the "..." arguments.
19059 */
19060 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19061 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019062 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019063 v = &fc.fixvar[fixvar_idx++].var;
19064 STRCPY(v->di_key, "000");
19065 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19066 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19067 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019068 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019069 v->di_tv.vval.v_list = &fc.l_varlist;
19070 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19071 fc.l_varlist.lv_refcount = 99999;
19072
19073 /*
19074 * Set a:firstline to "firstline" and a:lastline to "lastline".
19075 * Set a:name to named arguments.
19076 * Set a:N to the "..." arguments.
19077 */
19078 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19079 (varnumber_T)firstline);
19080 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19081 (varnumber_T)lastline);
19082 for (i = 0; i < argcount; ++i)
19083 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019084 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019085 if (ai < 0)
19086 /* named argument a:name */
19087 name = FUNCARG(fp, i);
19088 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019089 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019090 /* "..." argument a:1, a:2, etc. */
19091 sprintf((char *)numbuf, "%d", ai + 1);
19092 name = numbuf;
19093 }
19094 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19095 {
19096 v = &fc.fixvar[fixvar_idx++].var;
19097 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19098 }
19099 else
19100 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019101 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19102 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019103 if (v == NULL)
19104 break;
19105 v->di_flags = DI_FLAGS_RO;
19106 }
19107 STRCPY(v->di_key, name);
19108 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19109
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019110 /* Note: the values are copied directly to avoid alloc/free.
19111 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019112 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019113 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019114
19115 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19116 {
19117 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19118 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019119 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019120 }
19121 }
19122
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 /* Don't redraw while executing the function. */
19124 ++RedrawingDisabled;
19125 save_sourcing_name = sourcing_name;
19126 save_sourcing_lnum = sourcing_lnum;
19127 sourcing_lnum = 1;
19128 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019129 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 if (sourcing_name != NULL)
19131 {
19132 if (save_sourcing_name != NULL
19133 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19134 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19135 else
19136 STRCPY(sourcing_name, "function ");
19137 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19138
19139 if (p_verbose >= 12)
19140 {
19141 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019142 verbose_enter_scroll();
19143
Bram Moolenaar555b2802005-05-19 21:08:39 +000019144 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 if (p_verbose >= 14)
19146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019148 char_u numbuf[NUMBUFLEN];
19149 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150
19151 msg_puts((char_u *)"(");
19152 for (i = 0; i < argcount; ++i)
19153 {
19154 if (i > 0)
19155 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019156 if (argvars[i].v_type == VAR_NUMBER)
19157 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 else
19159 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019160 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019161 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019163 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 }
19165 }
19166 msg_puts((char_u *)")");
19167 }
19168 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019169
19170 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171 --no_wait_return;
19172 }
19173 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019174#ifdef FEAT_PROFILE
19175 if (do_profiling)
19176 {
19177 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19178 func_do_profile(fp);
19179 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019180 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019181 {
19182 ++fp->uf_tm_count;
19183 profile_start(&fp->uf_tm_start);
19184 profile_zero(&fp->uf_tm_children);
19185 }
19186 script_prof_save(&wait_start);
19187 }
19188#endif
19189
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019191 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192 save_did_emsg = did_emsg;
19193 did_emsg = FALSE;
19194
19195 /* call do_cmdline() to execute the lines */
19196 do_cmdline(NULL, get_func_line, (void *)&fc,
19197 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19198
19199 --RedrawingDisabled;
19200
19201 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019202 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 clear_tv(rettv);
19205 rettv->v_type = VAR_NUMBER;
19206 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 }
19208
Bram Moolenaar05159a02005-02-26 23:04:13 +000019209#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019210 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019211 {
19212 profile_end(&fp->uf_tm_start);
19213 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19214 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
19215 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
19216 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019217 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019218 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019219 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19220 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019221 }
19222 }
19223#endif
19224
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 /* when being verbose, mention the return value */
19226 if (p_verbose >= 12)
19227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019229 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019232 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019233 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019234 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19235 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019238 char_u buf[MSG_BUF_LEN];
19239 char_u numbuf[NUMBUFLEN];
19240 char_u *tofree;
19241
Bram Moolenaar555b2802005-05-19 21:08:39 +000019242 /* The value may be very long. Skip the middle part, so that we
19243 * have some idea how it starts and ends. smsg() would always
19244 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019245 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019246 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019247 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019248 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 }
19250 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019251
19252 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 --no_wait_return;
19254 }
19255
19256 vim_free(sourcing_name);
19257 sourcing_name = save_sourcing_name;
19258 sourcing_lnum = save_sourcing_lnum;
19259 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019260#ifdef FEAT_PROFILE
19261 if (do_profiling)
19262 script_prof_restore(&wait_start);
19263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264
19265 if (p_verbose >= 12 && sourcing_name != NULL)
19266 {
19267 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019268 verbose_enter_scroll();
19269
Bram Moolenaar555b2802005-05-19 21:08:39 +000019270 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019272
19273 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 --no_wait_return;
19275 }
19276
19277 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019278 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279
Bram Moolenaar33570922005-01-25 22:26:29 +000019280 /* The a: variables typevals were not alloced, only free the allocated
19281 * variables. */
19282 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19283
19284 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 --depth;
19286}
19287
19288/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019289 * Add a number variable "name" to dict "dp" with value "nr".
19290 */
19291 static void
19292add_nr_var(dp, v, name, nr)
19293 dict_T *dp;
19294 dictitem_T *v;
19295 char *name;
19296 varnumber_T nr;
19297{
19298 STRCPY(v->di_key, name);
19299 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19300 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19301 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019302 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019303 v->di_tv.vval.v_number = nr;
19304}
19305
19306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 * ":return [expr]"
19308 */
19309 void
19310ex_return(eap)
19311 exarg_T *eap;
19312{
19313 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019314 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 int returning = FALSE;
19316
19317 if (current_funccal == NULL)
19318 {
19319 EMSG(_("E133: :return not inside a function"));
19320 return;
19321 }
19322
19323 if (eap->skip)
19324 ++emsg_skip;
19325
19326 eap->nextcmd = NULL;
19327 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019328 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 {
19330 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019331 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019333 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334 }
19335 /* It's safer to return also on error. */
19336 else if (!eap->skip)
19337 {
19338 /*
19339 * Return unless the expression evaluation has been cancelled due to an
19340 * aborting error, an interrupt, or an exception.
19341 */
19342 if (!aborting())
19343 returning = do_return(eap, FALSE, TRUE, NULL);
19344 }
19345
19346 /* When skipping or the return gets pending, advance to the next command
19347 * in this line (!returning). Otherwise, ignore the rest of the line.
19348 * Following lines will be ignored by get_func_line(). */
19349 if (returning)
19350 eap->nextcmd = NULL;
19351 else if (eap->nextcmd == NULL) /* no argument */
19352 eap->nextcmd = check_nextcmd(arg);
19353
19354 if (eap->skip)
19355 --emsg_skip;
19356}
19357
19358/*
19359 * Return from a function. Possibly makes the return pending. Also called
19360 * for a pending return at the ":endtry" or after returning from an extra
19361 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019362 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019363 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 * FALSE when the return gets pending.
19365 */
19366 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019367do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 exarg_T *eap;
19369 int reanimate;
19370 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019371 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372{
19373 int idx;
19374 struct condstack *cstack = eap->cstack;
19375
19376 if (reanimate)
19377 /* Undo the return. */
19378 current_funccal->returned = FALSE;
19379
19380 /*
19381 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19382 * not in its finally clause (which then is to be executed next) is found.
19383 * In this case, make the ":return" pending for execution at the ":endtry".
19384 * Otherwise, return normally.
19385 */
19386 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19387 if (idx >= 0)
19388 {
19389 cstack->cs_pending[idx] = CSTP_RETURN;
19390
19391 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019392 /* A pending return again gets pending. "rettv" points to an
19393 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019395 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 else
19397 {
19398 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019399 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019401 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019403 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 {
19405 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019406 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019407 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 else
19409 EMSG(_(e_outofmem));
19410 }
19411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019412 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413
19414 if (reanimate)
19415 {
19416 /* The pending return value could be overwritten by a ":return"
19417 * without argument in a finally clause; reset the default
19418 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019419 current_funccal->rettv->v_type = VAR_NUMBER;
19420 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 }
19422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019423 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424 }
19425 else
19426 {
19427 current_funccal->returned = TRUE;
19428
19429 /* If the return is carried out now, store the return value. For
19430 * a return immediately after reanimation, the value is already
19431 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019432 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019434 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019435 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019437 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 }
19439 }
19440
19441 return idx < 0;
19442}
19443
19444/*
19445 * Free the variable with a pending return value.
19446 */
19447 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019448discard_pending_return(rettv)
19449 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450{
Bram Moolenaar33570922005-01-25 22:26:29 +000019451 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452}
19453
19454/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019455 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456 * is an allocated string. Used by report_pending() for verbose messages.
19457 */
19458 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019459get_return_cmd(rettv)
19460 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019462 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019463 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019464 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019466 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019467 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019468 if (s == NULL)
19469 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019470
19471 STRCPY(IObuff, ":return ");
19472 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19473 if (STRLEN(s) + 8 >= IOSIZE)
19474 STRCPY(IObuff + IOSIZE - 4, "...");
19475 vim_free(tofree);
19476 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477}
19478
19479/*
19480 * Get next function line.
19481 * Called by do_cmdline() to get the next line.
19482 * Returns allocated string, or NULL for end of function.
19483 */
19484/* ARGSUSED */
19485 char_u *
19486get_func_line(c, cookie, indent)
19487 int c; /* not used */
19488 void *cookie;
19489 int indent; /* not used */
19490{
Bram Moolenaar33570922005-01-25 22:26:29 +000019491 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019492 ufunc_T *fp = fcp->func;
19493 char_u *retval;
19494 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495
19496 /* If breakpoints have been added/deleted need to check for it. */
19497 if (fcp->dbg_tick != debug_tick)
19498 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019499 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500 sourcing_lnum);
19501 fcp->dbg_tick = debug_tick;
19502 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019503#ifdef FEAT_PROFILE
19504 if (do_profiling)
19505 func_line_end(cookie);
19506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507
Bram Moolenaar05159a02005-02-26 23:04:13 +000019508 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019509 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19510 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 retval = NULL;
19512 else
19513 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019514 /* Skip NULL lines (continuation lines). */
19515 while (fcp->linenr < gap->ga_len
19516 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19517 ++fcp->linenr;
19518 if (fcp->linenr >= gap->ga_len)
19519 retval = NULL;
19520 else
19521 {
19522 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19523 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019524#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019525 if (do_profiling)
19526 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019527#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 }
19530
19531 /* Did we encounter a breakpoint? */
19532 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19533 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019534 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019536 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537 sourcing_lnum);
19538 fcp->dbg_tick = debug_tick;
19539 }
19540
19541 return retval;
19542}
19543
Bram Moolenaar05159a02005-02-26 23:04:13 +000019544#if defined(FEAT_PROFILE) || defined(PROTO)
19545/*
19546 * Called when starting to read a function line.
19547 * "sourcing_lnum" must be correct!
19548 * When skipping lines it may not actually be executed, but we won't find out
19549 * until later and we need to store the time now.
19550 */
19551 void
19552func_line_start(cookie)
19553 void *cookie;
19554{
19555 funccall_T *fcp = (funccall_T *)cookie;
19556 ufunc_T *fp = fcp->func;
19557
19558 if (fp->uf_profiling && sourcing_lnum >= 1
19559 && sourcing_lnum <= fp->uf_lines.ga_len)
19560 {
19561 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019562 /* Skip continuation lines. */
19563 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19564 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019565 fp->uf_tml_execed = FALSE;
19566 profile_start(&fp->uf_tml_start);
19567 profile_zero(&fp->uf_tml_children);
19568 profile_get_wait(&fp->uf_tml_wait);
19569 }
19570}
19571
19572/*
19573 * Called when actually executing a function line.
19574 */
19575 void
19576func_line_exec(cookie)
19577 void *cookie;
19578{
19579 funccall_T *fcp = (funccall_T *)cookie;
19580 ufunc_T *fp = fcp->func;
19581
19582 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19583 fp->uf_tml_execed = TRUE;
19584}
19585
19586/*
19587 * Called when done with a function line.
19588 */
19589 void
19590func_line_end(cookie)
19591 void *cookie;
19592{
19593 funccall_T *fcp = (funccall_T *)cookie;
19594 ufunc_T *fp = fcp->func;
19595
19596 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19597 {
19598 if (fp->uf_tml_execed)
19599 {
19600 ++fp->uf_tml_count[fp->uf_tml_idx];
19601 profile_end(&fp->uf_tml_start);
19602 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19603 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19604 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19605 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19606 }
19607 fp->uf_tml_idx = -1;
19608 }
19609}
19610#endif
19611
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612/*
19613 * Return TRUE if the currently active function should be ended, because a
19614 * return was encountered or an error occured. Used inside a ":while".
19615 */
19616 int
19617func_has_ended(cookie)
19618 void *cookie;
19619{
Bram Moolenaar33570922005-01-25 22:26:29 +000019620 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621
19622 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19623 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019624 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 || fcp->returned);
19626}
19627
19628/*
19629 * return TRUE if cookie indicates a function which "abort"s on errors.
19630 */
19631 int
19632func_has_abort(cookie)
19633 void *cookie;
19634{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019635 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636}
19637
19638#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19639typedef enum
19640{
19641 VAR_FLAVOUR_DEFAULT,
19642 VAR_FLAVOUR_SESSION,
19643 VAR_FLAVOUR_VIMINFO
19644} var_flavour_T;
19645
19646static var_flavour_T var_flavour __ARGS((char_u *varname));
19647
19648 static var_flavour_T
19649var_flavour(varname)
19650 char_u *varname;
19651{
19652 char_u *p = varname;
19653
19654 if (ASCII_ISUPPER(*p))
19655 {
19656 while (*(++p))
19657 if (ASCII_ISLOWER(*p))
19658 return VAR_FLAVOUR_SESSION;
19659 return VAR_FLAVOUR_VIMINFO;
19660 }
19661 else
19662 return VAR_FLAVOUR_DEFAULT;
19663}
19664#endif
19665
19666#if defined(FEAT_VIMINFO) || defined(PROTO)
19667/*
19668 * Restore global vars that start with a capital from the viminfo file
19669 */
19670 int
19671read_viminfo_varlist(virp, writing)
19672 vir_T *virp;
19673 int writing;
19674{
19675 char_u *tab;
19676 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678
19679 if (!writing && (find_viminfo_parameter('!') != NULL))
19680 {
19681 tab = vim_strchr(virp->vir_line + 1, '\t');
19682 if (tab != NULL)
19683 {
19684 *tab++ = '\0'; /* isolate the variable name */
19685 if (*tab == 'S') /* string var */
19686 is_string = TRUE;
19687
19688 tab = vim_strchr(tab, '\t');
19689 if (tab != NULL)
19690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 if (is_string)
19692 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019693 tv.v_type = VAR_STRING;
19694 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 }
19697 else
19698 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019699 tv.v_type = VAR_NUMBER;
19700 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019702 set_var(virp->vir_line + 1, &tv, FALSE);
19703 if (is_string)
19704 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 }
19706 }
19707 }
19708
19709 return viminfo_readline(virp);
19710}
19711
19712/*
19713 * Write global vars that start with a capital to the viminfo file
19714 */
19715 void
19716write_viminfo_varlist(fp)
19717 FILE *fp;
19718{
Bram Moolenaar33570922005-01-25 22:26:29 +000019719 hashitem_T *hi;
19720 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019721 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019723 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019724 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019725 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726
19727 if (find_viminfo_parameter('!') == NULL)
19728 return;
19729
19730 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019731
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 todo = globvarht.ht_used;
19733 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019735 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019737 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019738 this_var = HI2DI(hi);
19739 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019740 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019741 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019742 {
19743 case VAR_STRING: s = "STR"; break;
19744 case VAR_NUMBER: s = "NUM"; break;
19745 default: continue;
19746 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019748 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019749 if (p != NULL)
19750 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019751 vim_free(tofree);
19752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 }
19754 }
19755}
19756#endif
19757
19758#if defined(FEAT_SESSION) || defined(PROTO)
19759 int
19760store_session_globals(fd)
19761 FILE *fd;
19762{
Bram Moolenaar33570922005-01-25 22:26:29 +000019763 hashitem_T *hi;
19764 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019765 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 char_u *p, *t;
19767
Bram Moolenaar33570922005-01-25 22:26:29 +000019768 todo = globvarht.ht_used;
19769 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019771 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019773 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019774 this_var = HI2DI(hi);
19775 if ((this_var->di_tv.v_type == VAR_NUMBER
19776 || this_var->di_tv.v_type == VAR_STRING)
19777 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019778 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019779 /* Escape special characters with a backslash. Turn a LF and
19780 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019781 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019782 (char_u *)"\\\"\n\r");
19783 if (p == NULL) /* out of memory */
19784 break;
19785 for (t = p; *t != NUL; ++t)
19786 if (*t == '\n')
19787 *t = 'n';
19788 else if (*t == '\r')
19789 *t = 'r';
19790 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019791 this_var->di_key,
19792 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19793 : ' ',
19794 p,
19795 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19796 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019797 || put_eol(fd) == FAIL)
19798 {
19799 vim_free(p);
19800 return FAIL;
19801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 vim_free(p);
19803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 }
19805 }
19806 return OK;
19807}
19808#endif
19809
Bram Moolenaar661b1822005-07-28 22:36:45 +000019810/*
19811 * Display script name where an item was last set.
19812 * Should only be invoked when 'verbose' is non-zero.
19813 */
19814 void
19815last_set_msg(scriptID)
19816 scid_T scriptID;
19817{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019818 char_u *p;
19819
Bram Moolenaar661b1822005-07-28 22:36:45 +000019820 if (scriptID != 0)
19821 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019822 p = home_replace_save(NULL, get_scriptname(scriptID));
19823 if (p != NULL)
19824 {
19825 verbose_enter();
19826 MSG_PUTS(_("\n\tLast set from "));
19827 MSG_PUTS(p);
19828 vim_free(p);
19829 verbose_leave();
19830 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019831 }
19832}
19833
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834#endif /* FEAT_EVAL */
19835
19836#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19837
19838
19839#ifdef WIN3264
19840/*
19841 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19842 */
19843static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19844static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19845static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19846
19847/*
19848 * Get the short pathname of a file.
19849 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19850 */
19851 static int
19852get_short_pathname(fnamep, bufp, fnamelen)
19853 char_u **fnamep;
19854 char_u **bufp;
19855 int *fnamelen;
19856{
19857 int l,len;
19858 char_u *newbuf;
19859
19860 len = *fnamelen;
19861
19862 l = GetShortPathName(*fnamep, *fnamep, len);
19863 if (l > len - 1)
19864 {
19865 /* If that doesn't work (not enough space), then save the string
19866 * and try again with a new buffer big enough
19867 */
19868 newbuf = vim_strnsave(*fnamep, l);
19869 if (newbuf == NULL)
19870 return 0;
19871
19872 vim_free(*bufp);
19873 *fnamep = *bufp = newbuf;
19874
19875 l = GetShortPathName(*fnamep,*fnamep,l+1);
19876
19877 /* Really should always succeed, as the buffer is big enough */
19878 }
19879
19880 *fnamelen = l;
19881 return 1;
19882}
19883
19884/*
19885 * Create a short path name. Returns the length of the buffer it needs.
19886 * Doesn't copy over the end of the buffer passed in.
19887 */
19888 static int
19889shortpath_for_invalid_fname(fname, bufp, fnamelen)
19890 char_u **fname;
19891 char_u **bufp;
19892 int *fnamelen;
19893{
19894 char_u *s, *p, *pbuf2, *pbuf3;
19895 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019896 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897
19898 /* Make a copy */
19899 len2 = *fnamelen;
19900 pbuf2 = vim_strnsave(*fname, len2);
19901 pbuf3 = NULL;
19902
19903 s = pbuf2 + len2 - 1; /* Find the end */
19904 slen = 1;
19905 plen = len2;
19906
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019907 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 {
19909 --s;
19910 ++slen;
19911 --plen;
19912 }
19913
19914 do
19915 {
19916 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019917 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 {
19919 --s;
19920 ++slen;
19921 --plen;
19922 }
19923 if (s <= pbuf2)
19924 break;
19925
19926 /* Remeber the character that is about to be blatted */
19927 ch = *s;
19928 *s = 0; /* get_short_pathname requires a null-terminated string */
19929
19930 /* Try it in situ */
19931 p = pbuf2;
19932 if (!get_short_pathname(&p, &pbuf3, &plen))
19933 {
19934 vim_free(pbuf2);
19935 return -1;
19936 }
19937 *s = ch; /* Preserve the string */
19938 } while (plen == 0);
19939
19940 if (plen > 0)
19941 {
19942 /* Remeber the length of the new string. */
19943 *fnamelen = len = plen + slen;
19944 vim_free(*bufp);
19945 if (len > len2)
19946 {
19947 /* If there's not enough space in the currently allocated string,
19948 * then copy it to a buffer big enough.
19949 */
19950 *fname= *bufp = vim_strnsave(p, len);
19951 if (*fname == NULL)
19952 return -1;
19953 }
19954 else
19955 {
19956 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19957 *fname = *bufp = pbuf2;
19958 if (p != pbuf2)
19959 strncpy(*fname, p, plen);
19960 pbuf2 = NULL;
19961 }
19962 /* Concat the next bit */
19963 strncpy(*fname + plen, s, slen);
19964 (*fname)[len] = '\0';
19965 }
19966 vim_free(pbuf3);
19967 vim_free(pbuf2);
19968 return 0;
19969}
19970
19971/*
19972 * Get a pathname for a partial path.
19973 */
19974 static int
19975shortpath_for_partial(fnamep, bufp, fnamelen)
19976 char_u **fnamep;
19977 char_u **bufp;
19978 int *fnamelen;
19979{
19980 int sepcount, len, tflen;
19981 char_u *p;
19982 char_u *pbuf, *tfname;
19983 int hasTilde;
19984
19985 /* Count up the path seperators from the RHS.. so we know which part
19986 * of the path to return.
19987 */
19988 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019989 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 if (vim_ispathsep(*p))
19991 ++sepcount;
19992
19993 /* Need full path first (use expand_env() to remove a "~/") */
19994 hasTilde = (**fnamep == '~');
19995 if (hasTilde)
19996 pbuf = tfname = expand_env_save(*fnamep);
19997 else
19998 pbuf = tfname = FullName_save(*fnamep, FALSE);
19999
20000 len = tflen = STRLEN(tfname);
20001
20002 if (!get_short_pathname(&tfname, &pbuf, &len))
20003 return -1;
20004
20005 if (len == 0)
20006 {
20007 /* Don't have a valid filename, so shorten the rest of the
20008 * path if we can. This CAN give us invalid 8.3 filenames, but
20009 * there's not a lot of point in guessing what it might be.
20010 */
20011 len = tflen;
20012 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20013 return -1;
20014 }
20015
20016 /* Count the paths backward to find the beginning of the desired string. */
20017 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020018 {
20019#ifdef FEAT_MBYTE
20020 if (has_mbyte)
20021 p -= mb_head_off(tfname, p);
20022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 if (vim_ispathsep(*p))
20024 {
20025 if (sepcount == 0 || (hasTilde && sepcount == 1))
20026 break;
20027 else
20028 sepcount --;
20029 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 if (hasTilde)
20032 {
20033 --p;
20034 if (p >= tfname)
20035 *p = '~';
20036 else
20037 return -1;
20038 }
20039 else
20040 ++p;
20041
20042 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20043 vim_free(*bufp);
20044 *fnamelen = (int)STRLEN(p);
20045 *bufp = pbuf;
20046 *fnamep = p;
20047
20048 return 0;
20049}
20050#endif /* WIN3264 */
20051
20052/*
20053 * Adjust a filename, according to a string of modifiers.
20054 * *fnamep must be NUL terminated when called. When returning, the length is
20055 * determined by *fnamelen.
20056 * Returns valid flags.
20057 * When there is an error, *fnamep is set to NULL.
20058 */
20059 int
20060modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20061 char_u *src; /* string with modifiers */
20062 int *usedlen; /* characters after src that are used */
20063 char_u **fnamep; /* file name so far */
20064 char_u **bufp; /* buffer for allocated file name or NULL */
20065 int *fnamelen; /* length of fnamep */
20066{
20067 int valid = 0;
20068 char_u *tail;
20069 char_u *s, *p, *pbuf;
20070 char_u dirname[MAXPATHL];
20071 int c;
20072 int has_fullname = 0;
20073#ifdef WIN3264
20074 int has_shortname = 0;
20075#endif
20076
20077repeat:
20078 /* ":p" - full path/file_name */
20079 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20080 {
20081 has_fullname = 1;
20082
20083 valid |= VALID_PATH;
20084 *usedlen += 2;
20085
20086 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20087 if ((*fnamep)[0] == '~'
20088#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20089 && ((*fnamep)[1] == '/'
20090# ifdef BACKSLASH_IN_FILENAME
20091 || (*fnamep)[1] == '\\'
20092# endif
20093 || (*fnamep)[1] == NUL)
20094
20095#endif
20096 )
20097 {
20098 *fnamep = expand_env_save(*fnamep);
20099 vim_free(*bufp); /* free any allocated file name */
20100 *bufp = *fnamep;
20101 if (*fnamep == NULL)
20102 return -1;
20103 }
20104
20105 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020106 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 {
20108 if (vim_ispathsep(*p)
20109 && p[1] == '.'
20110 && (p[2] == NUL
20111 || vim_ispathsep(p[2])
20112 || (p[2] == '.'
20113 && (p[3] == NUL || vim_ispathsep(p[3])))))
20114 break;
20115 }
20116
20117 /* FullName_save() is slow, don't use it when not needed. */
20118 if (*p != NUL || !vim_isAbsName(*fnamep))
20119 {
20120 *fnamep = FullName_save(*fnamep, *p != NUL);
20121 vim_free(*bufp); /* free any allocated file name */
20122 *bufp = *fnamep;
20123 if (*fnamep == NULL)
20124 return -1;
20125 }
20126
20127 /* Append a path separator to a directory. */
20128 if (mch_isdir(*fnamep))
20129 {
20130 /* Make room for one or two extra characters. */
20131 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20132 vim_free(*bufp); /* free any allocated file name */
20133 *bufp = *fnamep;
20134 if (*fnamep == NULL)
20135 return -1;
20136 add_pathsep(*fnamep);
20137 }
20138 }
20139
20140 /* ":." - path relative to the current directory */
20141 /* ":~" - path relative to the home directory */
20142 /* ":8" - shortname path - postponed till after */
20143 while (src[*usedlen] == ':'
20144 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20145 {
20146 *usedlen += 2;
20147 if (c == '8')
20148 {
20149#ifdef WIN3264
20150 has_shortname = 1; /* Postpone this. */
20151#endif
20152 continue;
20153 }
20154 pbuf = NULL;
20155 /* Need full path first (use expand_env() to remove a "~/") */
20156 if (!has_fullname)
20157 {
20158 if (c == '.' && **fnamep == '~')
20159 p = pbuf = expand_env_save(*fnamep);
20160 else
20161 p = pbuf = FullName_save(*fnamep, FALSE);
20162 }
20163 else
20164 p = *fnamep;
20165
20166 has_fullname = 0;
20167
20168 if (p != NULL)
20169 {
20170 if (c == '.')
20171 {
20172 mch_dirname(dirname, MAXPATHL);
20173 s = shorten_fname(p, dirname);
20174 if (s != NULL)
20175 {
20176 *fnamep = s;
20177 if (pbuf != NULL)
20178 {
20179 vim_free(*bufp); /* free any allocated file name */
20180 *bufp = pbuf;
20181 pbuf = NULL;
20182 }
20183 }
20184 }
20185 else
20186 {
20187 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20188 /* Only replace it when it starts with '~' */
20189 if (*dirname == '~')
20190 {
20191 s = vim_strsave(dirname);
20192 if (s != NULL)
20193 {
20194 *fnamep = s;
20195 vim_free(*bufp);
20196 *bufp = s;
20197 }
20198 }
20199 }
20200 vim_free(pbuf);
20201 }
20202 }
20203
20204 tail = gettail(*fnamep);
20205 *fnamelen = (int)STRLEN(*fnamep);
20206
20207 /* ":h" - head, remove "/file_name", can be repeated */
20208 /* Don't remove the first "/" or "c:\" */
20209 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20210 {
20211 valid |= VALID_HEAD;
20212 *usedlen += 2;
20213 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020214 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 --tail;
20216 *fnamelen = (int)(tail - *fnamep);
20217#ifdef VMS
20218 if (*fnamelen > 0)
20219 *fnamelen += 1; /* the path separator is part of the path */
20220#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020221 while (tail > s && !after_pathsep(s, tail))
20222 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 }
20224
20225 /* ":8" - shortname */
20226 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20227 {
20228 *usedlen += 2;
20229#ifdef WIN3264
20230 has_shortname = 1;
20231#endif
20232 }
20233
20234#ifdef WIN3264
20235 /* Check shortname after we have done 'heads' and before we do 'tails'
20236 */
20237 if (has_shortname)
20238 {
20239 pbuf = NULL;
20240 /* Copy the string if it is shortened by :h */
20241 if (*fnamelen < (int)STRLEN(*fnamep))
20242 {
20243 p = vim_strnsave(*fnamep, *fnamelen);
20244 if (p == 0)
20245 return -1;
20246 vim_free(*bufp);
20247 *bufp = *fnamep = p;
20248 }
20249
20250 /* Split into two implementations - makes it easier. First is where
20251 * there isn't a full name already, second is where there is.
20252 */
20253 if (!has_fullname && !vim_isAbsName(*fnamep))
20254 {
20255 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20256 return -1;
20257 }
20258 else
20259 {
20260 int l;
20261
20262 /* Simple case, already have the full-name
20263 * Nearly always shorter, so try first time. */
20264 l = *fnamelen;
20265 if (!get_short_pathname(fnamep, bufp, &l))
20266 return -1;
20267
20268 if (l == 0)
20269 {
20270 /* Couldn't find the filename.. search the paths.
20271 */
20272 l = *fnamelen;
20273 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20274 return -1;
20275 }
20276 *fnamelen = l;
20277 }
20278 }
20279#endif /* WIN3264 */
20280
20281 /* ":t" - tail, just the basename */
20282 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20283 {
20284 *usedlen += 2;
20285 *fnamelen -= (int)(tail - *fnamep);
20286 *fnamep = tail;
20287 }
20288
20289 /* ":e" - extension, can be repeated */
20290 /* ":r" - root, without extension, can be repeated */
20291 while (src[*usedlen] == ':'
20292 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20293 {
20294 /* find a '.' in the tail:
20295 * - for second :e: before the current fname
20296 * - otherwise: The last '.'
20297 */
20298 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20299 s = *fnamep - 2;
20300 else
20301 s = *fnamep + *fnamelen - 1;
20302 for ( ; s > tail; --s)
20303 if (s[0] == '.')
20304 break;
20305 if (src[*usedlen + 1] == 'e') /* :e */
20306 {
20307 if (s > tail)
20308 {
20309 *fnamelen += (int)(*fnamep - (s + 1));
20310 *fnamep = s + 1;
20311#ifdef VMS
20312 /* cut version from the extension */
20313 s = *fnamep + *fnamelen - 1;
20314 for ( ; s > *fnamep; --s)
20315 if (s[0] == ';')
20316 break;
20317 if (s > *fnamep)
20318 *fnamelen = s - *fnamep;
20319#endif
20320 }
20321 else if (*fnamep <= tail)
20322 *fnamelen = 0;
20323 }
20324 else /* :r */
20325 {
20326 if (s > tail) /* remove one extension */
20327 *fnamelen = (int)(s - *fnamep);
20328 }
20329 *usedlen += 2;
20330 }
20331
20332 /* ":s?pat?foo?" - substitute */
20333 /* ":gs?pat?foo?" - global substitute */
20334 if (src[*usedlen] == ':'
20335 && (src[*usedlen + 1] == 's'
20336 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20337 {
20338 char_u *str;
20339 char_u *pat;
20340 char_u *sub;
20341 int sep;
20342 char_u *flags;
20343 int didit = FALSE;
20344
20345 flags = (char_u *)"";
20346 s = src + *usedlen + 2;
20347 if (src[*usedlen + 1] == 'g')
20348 {
20349 flags = (char_u *)"g";
20350 ++s;
20351 }
20352
20353 sep = *s++;
20354 if (sep)
20355 {
20356 /* find end of pattern */
20357 p = vim_strchr(s, sep);
20358 if (p != NULL)
20359 {
20360 pat = vim_strnsave(s, (int)(p - s));
20361 if (pat != NULL)
20362 {
20363 s = p + 1;
20364 /* find end of substitution */
20365 p = vim_strchr(s, sep);
20366 if (p != NULL)
20367 {
20368 sub = vim_strnsave(s, (int)(p - s));
20369 str = vim_strnsave(*fnamep, *fnamelen);
20370 if (sub != NULL && str != NULL)
20371 {
20372 *usedlen = (int)(p + 1 - src);
20373 s = do_string_sub(str, pat, sub, flags);
20374 if (s != NULL)
20375 {
20376 *fnamep = s;
20377 *fnamelen = (int)STRLEN(s);
20378 vim_free(*bufp);
20379 *bufp = s;
20380 didit = TRUE;
20381 }
20382 }
20383 vim_free(sub);
20384 vim_free(str);
20385 }
20386 vim_free(pat);
20387 }
20388 }
20389 /* after using ":s", repeat all the modifiers */
20390 if (didit)
20391 goto repeat;
20392 }
20393 }
20394
20395 return valid;
20396}
20397
20398/*
20399 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20400 * "flags" can be "g" to do a global substitute.
20401 * Returns an allocated string, NULL for error.
20402 */
20403 char_u *
20404do_string_sub(str, pat, sub, flags)
20405 char_u *str;
20406 char_u *pat;
20407 char_u *sub;
20408 char_u *flags;
20409{
20410 int sublen;
20411 regmatch_T regmatch;
20412 int i;
20413 int do_all;
20414 char_u *tail;
20415 garray_T ga;
20416 char_u *ret;
20417 char_u *save_cpo;
20418
20419 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20420 save_cpo = p_cpo;
20421 p_cpo = (char_u *)"";
20422
20423 ga_init2(&ga, 1, 200);
20424
20425 do_all = (flags[0] == 'g');
20426
20427 regmatch.rm_ic = p_ic;
20428 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20429 if (regmatch.regprog != NULL)
20430 {
20431 tail = str;
20432 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20433 {
20434 /*
20435 * Get some space for a temporary buffer to do the substitution
20436 * into. It will contain:
20437 * - The text up to where the match is.
20438 * - The substituted text.
20439 * - The text after the match.
20440 */
20441 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20442 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20443 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20444 {
20445 ga_clear(&ga);
20446 break;
20447 }
20448
20449 /* copy the text up to where the match is */
20450 i = (int)(regmatch.startp[0] - tail);
20451 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20452 /* add the substituted text */
20453 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20454 + ga.ga_len + i, TRUE, TRUE, FALSE);
20455 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 /* avoid getting stuck on a match with an empty string */
20457 if (tail == regmatch.endp[0])
20458 {
20459 if (*tail == NUL)
20460 break;
20461 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20462 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 }
20464 else
20465 {
20466 tail = regmatch.endp[0];
20467 if (*tail == NUL)
20468 break;
20469 }
20470 if (!do_all)
20471 break;
20472 }
20473
20474 if (ga.ga_data != NULL)
20475 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20476
20477 vim_free(regmatch.regprog);
20478 }
20479
20480 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20481 ga_clear(&ga);
20482 p_cpo = save_cpo;
20483
20484 return ret;
20485}
20486
20487#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */