blob: 3044ef8927584db443689e998333926d615d471e [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 Moolenaar33570922005-01-25 22:26:29 +0000403static listitem_T *listitem_alloc __ARGS((void));
404static void listitem_free __ARGS((listitem_T *item));
405static void listitem_remove __ARGS((list_T *l, listitem_T *item));
406static long list_len __ARGS((list_T *l));
407static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
408static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
409static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static listitem_T *list_find __ARGS((list_T *l, long n));
411static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static void list_append __ARGS((list_T *l, listitem_T *item));
413static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000414static int list_append_string __ARGS((list_T *l, char_u *str, int len));
415static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
417static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
418static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000419static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000421static char_u *list2string __ARGS((typval_T *tv, int copyID));
422static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000423static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
424static void set_ref_in_list __ARGS((list_T *l, int copyID));
425static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static void dict_unref __ARGS((dict_T *d));
427static void dict_free __ARGS((dict_T *d));
428static dictitem_T *dictitem_alloc __ARGS((char_u *key));
429static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
430static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
431static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static int dict_add __ARGS((dict_T *d, dictitem_T *item));
434static long dict_len __ARGS((dict_T *d));
435static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
439static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static char_u *string_quote __ARGS((char_u *str, int function));
441static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
442static int find_internal_func __ARGS((char_u *name));
443static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
444static 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));
445static 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 +0000446static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447
448static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000467#if defined(FEAT_INS_EXPAND)
468static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
476static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000502static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000504static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000510static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000518static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000541static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000547static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000564static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000568#ifdef vim_mkdir
569static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
570#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000575static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000577static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000589static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000596static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000597static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000602static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
606#ifdef HAVE_STRFTIME
607static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
608#endif
609static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000621static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000622static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000624static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000639static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
642static int get_env_len __ARGS((char_u **arg));
643static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000644static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000645static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
646#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
647#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
648 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000649static 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 +0000650static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000651static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000652static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
653static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static typval_T *alloc_tv __ARGS((void));
655static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void init_tv __ARGS((typval_T *varp));
657static long get_tv_number __ARGS((typval_T *varp));
658static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000659static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static char_u *get_tv_string __ARGS((typval_T *varp));
661static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000664static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
666static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
667static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
668static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
669static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
670static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
671static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000672static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000674static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
676static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
677static int eval_fname_script __ARGS((char_u *p));
678static int eval_fname_sid __ARGS((char_u *p));
679static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static ufunc_T *find_func __ARGS((char_u *name));
681static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000682static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000683#ifdef FEAT_PROFILE
684static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000685static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
686static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
687static int
688# ifdef __BORLANDC__
689 _RTLENTRYF
690# endif
691 prof_total_cmp __ARGS((const void *s1, const void *s2));
692static int
693# ifdef __BORLANDC__
694 _RTLENTRYF
695# endif
696 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000698static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000699static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000700static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void func_free __ARGS((ufunc_T *fp));
702static void func_unref __ARGS((char_u *name));
703static void func_ref __ARGS((char_u *name));
704static 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));
705static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000706static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000708/* Character used as separated in autoload function/variable names. */
709#define AUTOLOAD_CHAR '#'
710
Bram Moolenaar33570922005-01-25 22:26:29 +0000711/*
712 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000713 */
714 void
715eval_init()
716{
Bram Moolenaar33570922005-01-25 22:26:29 +0000717 int i;
718 struct vimvar *p;
719
720 init_var_dict(&globvardict, &globvars_var);
721 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000722 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000723 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000724
725 for (i = 0; i < VV_LEN; ++i)
726 {
727 p = &vimvars[i];
728 STRCPY(p->vv_di.di_key, p->vv_name);
729 if (p->vv_flags & VV_RO)
730 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
731 else if (p->vv_flags & VV_RO_SBX)
732 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
733 else
734 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000735
736 /* add to v: scope dict, unless the value is not always available */
737 if (p->vv_type != VAR_UNKNOWN)
738 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000739 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000740 /* add to compat scope dict */
741 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000742 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000743}
744
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000745#if defined(EXITFREE) || defined(PROTO)
746 void
747eval_clear()
748{
749 int i;
750 struct vimvar *p;
751
752 for (i = 0; i < VV_LEN; ++i)
753 {
754 p = &vimvars[i];
755 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000756 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000757 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758 p->vv_di.di_tv.vval.v_string = NULL;
759 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000760 }
761 hash_clear(&vimvarht);
762 hash_clear(&compat_hashtab);
763
764 /* script-local variables */
765 for (i = 1; i <= ga_scripts.ga_len; ++i)
766 vars_clear(&SCRIPT_VARS(i));
767 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000768 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000769
770 /* global variables */
771 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000772
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000773 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000774 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000775 hash_clear(&func_hashtab);
776
777 /* unreferenced lists and dicts */
778 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000779}
780#endif
781
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * Return the name of the executed function.
784 */
785 char_u *
786func_name(cookie)
787 void *cookie;
788{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000789 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791
792/*
793 * Return the address holding the next breakpoint line for a funccall cookie.
794 */
795 linenr_T *
796func_breakpoint(cookie)
797 void *cookie;
798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
802/*
803 * Return the address holding the debug tick for a funccall cookie.
804 */
805 int *
806func_dbg_tick(cookie)
807 void *cookie;
808{
Bram Moolenaar33570922005-01-25 22:26:29 +0000809 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
812/*
813 * Return the nesting level for a funccall cookie.
814 */
815 int
816func_level(cookie)
817 void *cookie;
818{
Bram Moolenaar33570922005-01-25 22:26:29 +0000819 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820}
821
822/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000823funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825/*
826 * Return TRUE when a function was ended by a ":return" command.
827 */
828 int
829current_func_returned()
830{
831 return current_funccal->returned;
832}
833
834
835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * Set an internal variable to a string value. Creates the variable if it does
837 * not already exist.
838 */
839 void
840set_internal_string_var(name, value)
841 char_u *name;
842 char_u *value;
843{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000844 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000845 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847 val = vim_strsave(value);
848 if (val != NULL)
849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000851 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000853 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000854 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856 }
857}
858
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000859static lval_T *redir_lval = NULL;
860static char_u *redir_endp = NULL;
861static char_u *redir_varname = NULL;
862
863/*
864 * Start recording command output to a variable
865 * Returns OK if successfully completed the setup. FAIL otherwise.
866 */
867 int
868var_redir_start(name, append)
869 char_u *name;
870 int append; /* append to an existing variable */
871{
872 int save_emsg;
873 int err;
874 typval_T tv;
875
876 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000877 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000878 {
879 EMSG(_(e_invarg));
880 return FAIL;
881 }
882
883 redir_varname = vim_strsave(name);
884 if (redir_varname == NULL)
885 return FAIL;
886
887 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
888 if (redir_lval == NULL)
889 {
890 var_redir_stop();
891 return FAIL;
892 }
893
894 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000895 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
896 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000897 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
898 {
899 if (redir_endp != NULL && *redir_endp != NUL)
900 /* Trailing characters are present after the variable name */
901 EMSG(_(e_trailing));
902 else
903 EMSG(_(e_invarg));
904 var_redir_stop();
905 return FAIL;
906 }
907
908 /* check if we can write to the variable: set it to or append an empty
909 * string */
910 save_emsg = did_emsg;
911 did_emsg = FALSE;
912 tv.v_type = VAR_STRING;
913 tv.vval.v_string = (char_u *)"";
914 if (append)
915 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
916 else
917 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
918 err = did_emsg;
919 did_emsg += save_emsg;
920 if (err)
921 {
922 var_redir_stop();
923 return FAIL;
924 }
925 if (redir_lval->ll_newkey != NULL)
926 {
927 /* Dictionary item was created, don't do it again. */
928 vim_free(redir_lval->ll_newkey);
929 redir_lval->ll_newkey = NULL;
930 }
931
932 return OK;
933}
934
935/*
936 * Append "value[len]" to the variable set by var_redir_start().
937 */
938 void
939var_redir_str(value, len)
940 char_u *value;
941 int len;
942{
943 char_u *val;
944 typval_T tv;
945 int save_emsg;
946 int err;
947
948 if (redir_lval == NULL)
949 return;
950
951 if (len == -1)
952 /* Append the entire string */
953 val = vim_strsave(value);
954 else
955 /* Append only the specified number of characters */
956 val = vim_strnsave(value, len);
957 if (val == NULL)
958 return;
959
960 tv.v_type = VAR_STRING;
961 tv.vval.v_string = val;
962
963 save_emsg = did_emsg;
964 did_emsg = FALSE;
965 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
966 err = did_emsg;
967 did_emsg += save_emsg;
968 if (err)
969 var_redir_stop();
970
971 vim_free(tv.vval.v_string);
972}
973
974/*
975 * Stop redirecting command output to a variable.
976 */
977 void
978var_redir_stop()
979{
980 if (redir_lval != NULL)
981 {
982 clear_lval(redir_lval);
983 vim_free(redir_lval);
984 redir_lval = NULL;
985 }
986 vim_free(redir_varname);
987 redir_varname = NULL;
988}
989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990# if defined(FEAT_MBYTE) || defined(PROTO)
991 int
992eval_charconvert(enc_from, enc_to, fname_from, fname_to)
993 char_u *enc_from;
994 char_u *enc_to;
995 char_u *fname_from;
996 char_u *fname_to;
997{
998 int err = FALSE;
999
1000 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1001 set_vim_var_string(VV_CC_TO, enc_to, -1);
1002 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1003 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1004 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1005 err = TRUE;
1006 set_vim_var_string(VV_CC_FROM, NULL, -1);
1007 set_vim_var_string(VV_CC_TO, NULL, -1);
1008 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1009 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1010
1011 if (err)
1012 return FAIL;
1013 return OK;
1014}
1015# endif
1016
1017# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1018 int
1019eval_printexpr(fname, args)
1020 char_u *fname;
1021 char_u *args;
1022{
1023 int err = FALSE;
1024
1025 set_vim_var_string(VV_FNAME_IN, fname, -1);
1026 set_vim_var_string(VV_CMDARG, args, -1);
1027 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1028 err = TRUE;
1029 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1030 set_vim_var_string(VV_CMDARG, NULL, -1);
1031
1032 if (err)
1033 {
1034 mch_remove(fname);
1035 return FAIL;
1036 }
1037 return OK;
1038}
1039# endif
1040
1041# if defined(FEAT_DIFF) || defined(PROTO)
1042 void
1043eval_diff(origfile, newfile, outfile)
1044 char_u *origfile;
1045 char_u *newfile;
1046 char_u *outfile;
1047{
1048 int err = FALSE;
1049
1050 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1051 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1052 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1053 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1054 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1055 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1056 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1057}
1058
1059 void
1060eval_patch(origfile, difffile, outfile)
1061 char_u *origfile;
1062 char_u *difffile;
1063 char_u *outfile;
1064{
1065 int err;
1066
1067 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1068 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1069 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1070 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1071 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1072 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1073 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1074}
1075# endif
1076
1077/*
1078 * Top level evaluation function, returning a boolean.
1079 * Sets "error" to TRUE if there was an error.
1080 * Return TRUE or FALSE.
1081 */
1082 int
1083eval_to_bool(arg, error, nextcmd, skip)
1084 char_u *arg;
1085 int *error;
1086 char_u **nextcmd;
1087 int skip; /* only parse, don't execute */
1088{
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 int retval = FALSE;
1091
1092 if (skip)
1093 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001094 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 else
1097 {
1098 *error = FALSE;
1099 if (!skip)
1100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001101 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001102 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
1104 }
1105 if (skip)
1106 --emsg_skip;
1107
1108 return retval;
1109}
1110
1111/*
1112 * Top level evaluation function, returning a string. If "skip" is TRUE,
1113 * only parsing to "nextcmd" is done, without reporting errors. Return
1114 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1115 */
1116 char_u *
1117eval_to_string_skip(arg, nextcmd, skip)
1118 char_u *arg;
1119 char_u **nextcmd;
1120 int skip; /* only parse, don't execute */
1121{
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 char_u *retval;
1124
1125 if (skip)
1126 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 retval = NULL;
1129 else
1130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 retval = vim_strsave(get_tv_string(&tv));
1132 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 }
1134 if (skip)
1135 --emsg_skip;
1136
1137 return retval;
1138}
1139
1140/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001141 * Skip over an expression at "*pp".
1142 * Return FAIL for an error, OK otherwise.
1143 */
1144 int
1145skip_expr(pp)
1146 char_u **pp;
1147{
Bram Moolenaar33570922005-01-25 22:26:29 +00001148 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001149
1150 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001152}
1153
1154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 * Top level evaluation function, returning a string.
1156 * Return pointer to allocated memory, or NULL for failure.
1157 */
1158 char_u *
1159eval_to_string(arg, nextcmd)
1160 char_u *arg;
1161 char_u **nextcmd;
1162{
Bram Moolenaar33570922005-01-25 22:26:29 +00001163 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 char_u *retval;
1165
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 retval = NULL;
1168 else
1169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 retval = vim_strsave(get_tv_string(&tv));
1171 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173
1174 return retval;
1175}
1176
1177/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001178 * Call eval_to_string() without using current local variables and using
1179 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 */
1181 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001182eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 char_u *arg;
1184 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001185 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
1187 char_u *retval;
1188 void *save_funccalp;
1189
1190 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001191 if (use_sandbox)
1192 ++sandbox;
1193 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001195 if (use_sandbox)
1196 --sandbox;
1197 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 restore_funccal(save_funccalp);
1199 return retval;
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202/*
1203 * Top level evaluation function, returning a number.
1204 * Evaluates "expr" silently.
1205 * Returns -1 for an error.
1206 */
1207 int
1208eval_to_number(expr)
1209 char_u *expr;
1210{
Bram Moolenaar33570922005-01-25 22:26:29 +00001211 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001213 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215 ++emsg_off;
1216
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 retval = -1;
1219 else
1220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001221 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224 --emsg_off;
1225
1226 return retval;
1227}
1228
Bram Moolenaara40058a2005-07-11 22:42:07 +00001229/*
1230 * Prepare v: variable "idx" to be used.
1231 * Save the current typeval in "save_tv".
1232 * When not used yet add the variable to the v: hashtable.
1233 */
1234 static void
1235prepare_vimvar(idx, save_tv)
1236 int idx;
1237 typval_T *save_tv;
1238{
1239 *save_tv = vimvars[idx].vv_tv;
1240 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1241 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1242}
1243
1244/*
1245 * Restore v: variable "idx" to typeval "save_tv".
1246 * When no longer defined, remove the variable from the v: hashtable.
1247 */
1248 static void
1249restore_vimvar(idx, save_tv)
1250 int idx;
1251 typval_T *save_tv;
1252{
1253 hashitem_T *hi;
1254
1255 clear_tv(&vimvars[idx].vv_tv);
1256 vimvars[idx].vv_tv = *save_tv;
1257 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1258 {
1259 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1260 if (HASHITEM_EMPTY(hi))
1261 EMSG2(_(e_intern2), "restore_vimvar()");
1262 else
1263 hash_remove(&vimvarht, hi);
1264 }
1265}
1266
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001267#if defined(FEAT_SYN_HL) || defined(PROTO)
1268/*
1269 * Evaluate an expression to a list with suggestions.
1270 * For the "expr:" part of 'spellsuggest'.
1271 */
1272 list_T *
1273eval_spell_expr(badword, expr)
1274 char_u *badword;
1275 char_u *expr;
1276{
1277 typval_T save_val;
1278 typval_T rettv;
1279 list_T *list = NULL;
1280 char_u *p = skipwhite(expr);
1281
1282 /* Set "v:val" to the bad word. */
1283 prepare_vimvar(VV_VAL, &save_val);
1284 vimvars[VV_VAL].vv_type = VAR_STRING;
1285 vimvars[VV_VAL].vv_str = badword;
1286 if (p_verbose == 0)
1287 ++emsg_off;
1288
1289 if (eval1(&p, &rettv, TRUE) == OK)
1290 {
1291 if (rettv.v_type != VAR_LIST)
1292 clear_tv(&rettv);
1293 else
1294 list = rettv.vval.v_list;
1295 }
1296
1297 if (p_verbose == 0)
1298 --emsg_off;
1299 vimvars[VV_VAL].vv_str = NULL;
1300 restore_vimvar(VV_VAL, &save_val);
1301
1302 return list;
1303}
1304
1305/*
1306 * "list" is supposed to contain two items: a word and a number. Return the
1307 * word in "pp" and the number as the return value.
1308 * Return -1 if anything isn't right.
1309 * Used to get the good word and score from the eval_spell_expr() result.
1310 */
1311 int
1312get_spellword(list, pp)
1313 list_T *list;
1314 char_u **pp;
1315{
1316 listitem_T *li;
1317
1318 li = list->lv_first;
1319 if (li == NULL)
1320 return -1;
1321 *pp = get_tv_string(&li->li_tv);
1322
1323 li = li->li_next;
1324 if (li == NULL)
1325 return -1;
1326 return get_tv_number(&li->li_tv);
1327}
1328#endif
1329
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001330/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001331 * Top level evaluation function.
1332 * Returns an allocated typval_T with the result.
1333 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001334 */
1335 typval_T *
1336eval_expr(arg, nextcmd)
1337 char_u *arg;
1338 char_u **nextcmd;
1339{
1340 typval_T *tv;
1341
1342 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001343 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001344 {
1345 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001346 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001347 }
1348
1349 return tv;
1350}
1351
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1354/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001355 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001357 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001359 static int
1360call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 char_u *func;
1362 int argc;
1363 char_u **argv;
1364 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
Bram Moolenaar33570922005-01-25 22:26:29 +00001367 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 long n;
1369 int len;
1370 int i;
1371 int doesrange;
1372 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001373 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
Bram Moolenaar33570922005-01-25 22:26:29 +00001375 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379 for (i = 0; i < argc; i++)
1380 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001381 /* Pass a NULL or empty argument as an empty string */
1382 if (argv[i] == NULL || *argv[i] == NUL)
1383 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001384 argvars[i].v_type = VAR_STRING;
1385 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001386 continue;
1387 }
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 /* Recognize a number argument, the others must be strings. */
1390 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1391 if (len != 0 && len == (int)STRLEN(argv[i]))
1392 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001393 argvars[i].v_type = VAR_NUMBER;
1394 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 else
1397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001398 argvars[i].v_type = VAR_STRING;
1399 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401 }
1402
1403 if (safe)
1404 {
1405 save_funccalp = save_funccal();
1406 ++sandbox;
1407 }
1408
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001409 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1410 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001412 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (safe)
1414 {
1415 --sandbox;
1416 restore_funccal(save_funccalp);
1417 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001418 vim_free(argvars);
1419
1420 if (ret == FAIL)
1421 clear_tv(rettv);
1422
1423 return ret;
1424}
1425
1426/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001427 * Call vimL function "func" and return the result as a string.
1428 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001429 * Uses argv[argc] for the function arguments.
1430 */
1431 void *
1432call_func_retstr(func, argc, argv, safe)
1433 char_u *func;
1434 int argc;
1435 char_u **argv;
1436 int safe; /* use the sandbox */
1437{
1438 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001439 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001440
1441 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1442 return NULL;
1443
1444 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001445 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return retval;
1447}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001448
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001449#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001450/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001451 * Call vimL function "func" and return the result as a number.
1452 * Returns -1 when calling the function fails.
1453 * Uses argv[argc] for the function arguments.
1454 */
1455 long
1456call_func_retnr(func, argc, argv, safe)
1457 char_u *func;
1458 int argc;
1459 char_u **argv;
1460 int safe; /* use the sandbox */
1461{
1462 typval_T rettv;
1463 long retval;
1464
1465 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1466 return -1;
1467
1468 retval = get_tv_number_chk(&rettv, NULL);
1469 clear_tv(&rettv);
1470 return retval;
1471}
1472#endif
1473
1474/*
1475 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001476 * Uses argv[argc] for the function arguments.
1477 */
1478 void *
1479call_func_retlist(func, argc, argv, safe)
1480 char_u *func;
1481 int argc;
1482 char_u **argv;
1483 int safe; /* use the sandbox */
1484{
1485 typval_T rettv;
1486
1487 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1488 return NULL;
1489
1490 if (rettv.v_type != VAR_LIST)
1491 {
1492 clear_tv(&rettv);
1493 return NULL;
1494 }
1495
1496 return rettv.vval.v_list;
1497}
1498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499#endif
1500
1501/*
1502 * Save the current function call pointer, and set it to NULL.
1503 * Used when executing autocommands and for ":source".
1504 */
1505 void *
1506save_funccal()
1507{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001508 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 current_funccal = NULL;
1511 return (void *)fc;
1512}
1513
1514 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001515restore_funccal(vfc)
1516 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001518 funccall_T *fc = (funccall_T *)vfc;
1519
1520 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521}
1522
Bram Moolenaar05159a02005-02-26 23:04:13 +00001523#if defined(FEAT_PROFILE) || defined(PROTO)
1524/*
1525 * Prepare profiling for entering a child or something else that is not
1526 * counted for the script/function itself.
1527 * Should always be called in pair with prof_child_exit().
1528 */
1529 void
1530prof_child_enter(tm)
1531 proftime_T *tm; /* place to store waittime */
1532{
1533 funccall_T *fc = current_funccal;
1534
1535 if (fc != NULL && fc->func->uf_profiling)
1536 profile_start(&fc->prof_child);
1537 script_prof_save(tm);
1538}
1539
1540/*
1541 * Take care of time spent in a child.
1542 * Should always be called after prof_child_enter().
1543 */
1544 void
1545prof_child_exit(tm)
1546 proftime_T *tm; /* where waittime was stored */
1547{
1548 funccall_T *fc = current_funccal;
1549
1550 if (fc != NULL && fc->func->uf_profiling)
1551 {
1552 profile_end(&fc->prof_child);
1553 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1554 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1555 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1556 }
1557 script_prof_restore(tm);
1558}
1559#endif
1560
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562#ifdef FEAT_FOLDING
1563/*
1564 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1565 * it in "*cp". Doesn't give error messages.
1566 */
1567 int
1568eval_foldexpr(arg, cp)
1569 char_u *arg;
1570 int *cp;
1571{
Bram Moolenaar33570922005-01-25 22:26:29 +00001572 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 int retval;
1574 char_u *s;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001575 int use_sandbox = was_set_insecurely((char_u *)"foldexpr");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001578 if (use_sandbox)
1579 ++sandbox;
1580 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001582 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 retval = 0;
1584 else
1585 {
1586 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001587 if (tv.v_type == VAR_NUMBER)
1588 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001589 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 retval = 0;
1591 else
1592 {
1593 /* If the result is a string, check if there is a non-digit before
1594 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (!VIM_ISDIGIT(*s) && *s != '-')
1597 *cp = *s++;
1598 retval = atol((char *)s);
1599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001603 if (use_sandbox)
1604 --sandbox;
1605 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607 return retval;
1608}
1609#endif
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 * ":let" list all variable values
1613 * ":let var1 var2" list variable values
1614 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001615 * ":let var += expr" assignment command.
1616 * ":let var -= expr" assignment command.
1617 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 */
1620 void
1621ex_let(eap)
1622 exarg_T *eap;
1623{
1624 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001626 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 int var_count = 0;
1629 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001630 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 expr = skip_var_list(arg, &var_count, &semicolon);
1633 if (expr == NULL)
1634 return;
1635 expr = vim_strchr(expr, '=');
1636 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001638 /*
1639 * ":let" without "=": list variables
1640 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 if (*arg == '[')
1642 EMSG(_(e_invarg));
1643 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 /* ":let var1 var2" */
1645 arg = list_arg_vars(eap, arg);
1646 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001648 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001649 list_glob_vars();
1650 list_buf_vars();
1651 list_win_vars();
1652 list_vim_vars();
1653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 eap->nextcmd = check_nextcmd(arg);
1655 }
1656 else
1657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 op[0] = '=';
1659 op[1] = NUL;
1660 if (expr > arg)
1661 {
1662 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1663 op[0] = expr[-1]; /* +=, -= or .= */
1664 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if (eap->skip)
1668 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (eap->skip)
1671 {
1672 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001673 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 --emsg_skip;
1675 }
1676 else if (i != FAIL)
1677 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
1682 }
1683}
1684
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685/*
1686 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1687 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 * When "nextchars" is not NULL it points to a string with characters that
1689 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1690 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 * Returns OK or FAIL;
1692 */
1693 static int
1694ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1695 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 int copy; /* copy values from "tv", don't move */
1698 int semicolon; /* from skip_var_list() */
1699 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701{
1702 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001703 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001705 listitem_T *item;
1706 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707
1708 if (*arg != '[')
1709 {
1710 /*
1711 * ":let var = expr" or ":for var in list"
1712 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 return FAIL;
1715 return OK;
1716 }
1717
1718 /*
1719 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1720 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001721 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 {
1723 EMSG(_(e_listreq));
1724 return FAIL;
1725 }
1726
1727 i = list_len(l);
1728 if (semicolon == 0 && var_count < i)
1729 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001730 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 return FAIL;
1732 }
1733 if (var_count - semicolon > i)
1734 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001735 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 return FAIL;
1737 }
1738
1739 item = l->lv_first;
1740 while (*arg != ']')
1741 {
1742 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 item = item->li_next;
1745 if (arg == NULL)
1746 return FAIL;
1747
1748 arg = skipwhite(arg);
1749 if (*arg == ';')
1750 {
1751 /* Put the rest of the list (may be empty) in the var after ';'.
1752 * Create a new list for this. */
1753 l = list_alloc();
1754 if (l == NULL)
1755 return FAIL;
1756 while (item != NULL)
1757 {
1758 list_append_tv(l, &item->li_tv);
1759 item = item->li_next;
1760 }
1761
1762 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001763 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 ltv.vval.v_list = l;
1765 l->lv_refcount = 1;
1766
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1768 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 clear_tv(&ltv);
1770 if (arg == NULL)
1771 return FAIL;
1772 break;
1773 }
1774 else if (*arg != ',' && *arg != ']')
1775 {
1776 EMSG2(_(e_intern2), "ex_let_vars()");
1777 return FAIL;
1778 }
1779 }
1780
1781 return OK;
1782}
1783
1784/*
1785 * Skip over assignable variable "var" or list of variables "[var, var]".
1786 * Used for ":let varvar = expr" and ":for varvar in expr".
1787 * For "[var, var]" increment "*var_count" for each variable.
1788 * for "[var, var; var]" set "semicolon".
1789 * Return NULL for an error.
1790 */
1791 static char_u *
1792skip_var_list(arg, var_count, semicolon)
1793 char_u *arg;
1794 int *var_count;
1795 int *semicolon;
1796{
1797 char_u *p, *s;
1798
1799 if (*arg == '[')
1800 {
1801 /* "[var, var]": find the matching ']'. */
1802 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001803 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 {
1805 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1806 s = skip_var_one(p);
1807 if (s == p)
1808 {
1809 EMSG2(_(e_invarg2), p);
1810 return NULL;
1811 }
1812 ++*var_count;
1813
1814 p = skipwhite(s);
1815 if (*p == ']')
1816 break;
1817 else if (*p == ';')
1818 {
1819 if (*semicolon == 1)
1820 {
1821 EMSG(_("Double ; in list of variables"));
1822 return NULL;
1823 }
1824 *semicolon = 1;
1825 }
1826 else if (*p != ',')
1827 {
1828 EMSG2(_(e_invarg2), p);
1829 return NULL;
1830 }
1831 }
1832 return p + 1;
1833 }
1834 else
1835 return skip_var_one(arg);
1836}
1837
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001838/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001839 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1840 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001841 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 static char_u *
1843skip_var_one(arg)
1844 char_u *arg;
1845{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001846 if (*arg == '@' && arg[1] != NUL)
1847 return arg + 2;
1848 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1849 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850}
1851
Bram Moolenaara7043832005-01-21 11:56:39 +00001852/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001853 * List variables for hashtab "ht" with prefix "prefix".
1854 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001855 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001857list_hashtable_vars(ht, prefix, empty)
1858 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001859 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001860 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001861{
Bram Moolenaar33570922005-01-25 22:26:29 +00001862 hashitem_T *hi;
1863 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001864 int todo;
1865
1866 todo = ht->ht_used;
1867 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1868 {
1869 if (!HASHITEM_EMPTY(hi))
1870 {
1871 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 di = HI2DI(hi);
1873 if (empty || di->di_tv.v_type != VAR_STRING
1874 || di->di_tv.vval.v_string != NULL)
1875 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001876 }
1877 }
1878}
1879
1880/*
1881 * List global variables.
1882 */
1883 static void
1884list_glob_vars()
1885{
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001887}
1888
1889/*
1890 * List buffer variables.
1891 */
1892 static void
1893list_buf_vars()
1894{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001895 char_u numbuf[NUMBUFLEN];
1896
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001898
1899 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1900 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001901}
1902
1903/*
1904 * List window variables.
1905 */
1906 static void
1907list_win_vars()
1908{
Bram Moolenaar33570922005-01-25 22:26:29 +00001909 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001910}
1911
1912/*
1913 * List Vim variables.
1914 */
1915 static void
1916list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917{
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919}
1920
1921/*
1922 * List variables in "arg".
1923 */
1924 static char_u *
1925list_arg_vars(eap, arg)
1926 exarg_T *eap;
1927 char_u *arg;
1928{
1929 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001930 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001932 char_u *name_start;
1933 char_u *arg_subsc;
1934 char_u *tofree;
1935 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936
1937 while (!ends_excmd(*arg) && !got_int)
1938 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001939 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001941 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1943 {
1944 emsg_severe = TRUE;
1945 EMSG(_(e_trailing));
1946 break;
1947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001949 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001951 /* get_name_len() takes care of expanding curly braces */
1952 name_start = name = arg;
1953 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1954 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956 /* This is mainly to keep test 49 working: when expanding
1957 * curly braces fails overrule the exception error message. */
1958 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001960 emsg_severe = TRUE;
1961 EMSG2(_(e_invarg2), arg);
1962 break;
1963 }
1964 error = TRUE;
1965 }
1966 else
1967 {
1968 if (tofree != NULL)
1969 name = tofree;
1970 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 else
1973 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001974 /* handle d.key, l[idx], f(expr) */
1975 arg_subsc = arg;
1976 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001977 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001979 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001980 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001981 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001984 case 'g': list_glob_vars(); break;
1985 case 'b': list_buf_vars(); break;
1986 case 'w': list_win_vars(); break;
1987 case 'v': list_vim_vars(); break;
1988 default:
1989 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001990 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001991 }
1992 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001993 {
1994 char_u numbuf[NUMBUFLEN];
1995 char_u *tf;
1996 int c;
1997 char_u *s;
1998
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001999 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002000 c = *arg;
2001 *arg = NUL;
2002 list_one_var_a((char_u *)"",
2003 arg == arg_subsc ? name : name_start,
2004 tv.v_type, s == NULL ? (char_u *)"" : s);
2005 *arg = c;
2006 vim_free(tf);
2007 }
2008 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 }
2011 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002012
2013 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002015
2016 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017 }
2018
2019 return arg;
2020}
2021
2022/*
2023 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2024 * Returns a pointer to the char just after the var name.
2025 * Returns NULL if there is an error.
2026 */
2027 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002029 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002030 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034{
2035 int c1;
2036 char_u *name;
2037 char_u *p;
2038 char_u *arg_end = NULL;
2039 int len;
2040 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002041 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042
2043 /*
2044 * ":let $VAR = expr": Set environment variable.
2045 */
2046 if (*arg == '$')
2047 {
2048 /* Find the end of the name. */
2049 ++arg;
2050 name = arg;
2051 len = get_env_len(&arg);
2052 if (len == 0)
2053 EMSG2(_(e_invarg2), name - 1);
2054 else
2055 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002056 if (op != NULL && (*op == '+' || *op == '-'))
2057 EMSG2(_(e_letwrong), op);
2058 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 EMSG(_(e_letunexp));
2061 else
2062 {
2063 c1 = name[len];
2064 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002065 p = get_tv_string_chk(tv);
2066 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002067 {
2068 int mustfree = FALSE;
2069 char_u *s = vim_getenv(name, &mustfree);
2070
2071 if (s != NULL)
2072 {
2073 p = tofree = concat_str(s, p);
2074 if (mustfree)
2075 vim_free(s);
2076 }
2077 }
2078 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002079 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002080 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002081 if (STRICMP(name, "HOME") == 0)
2082 init_homedir();
2083 else if (didset_vim && STRICMP(name, "VIM") == 0)
2084 didset_vim = FALSE;
2085 else if (didset_vimruntime
2086 && STRICMP(name, "VIMRUNTIME") == 0)
2087 didset_vimruntime = FALSE;
2088 arg_end = arg;
2089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002091 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002092 }
2093 }
2094 }
2095
2096 /*
2097 * ":let &option = expr": Set option value.
2098 * ":let &l:option = expr": Set local option value.
2099 * ":let &g:option = expr": Set global option value.
2100 */
2101 else if (*arg == '&')
2102 {
2103 /* Find the end of the name. */
2104 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002105 if (p == NULL || (endchars != NULL
2106 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 EMSG(_(e_letunexp));
2108 else
2109 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002110 long n;
2111 int opt_type;
2112 long numval;
2113 char_u *stringval = NULL;
2114 char_u *s;
2115
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 c1 = *p;
2117 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002118
2119 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002120 s = get_tv_string_chk(tv); /* != NULL if number or string */
2121 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002122 {
2123 opt_type = get_option_value(arg, &numval,
2124 &stringval, opt_flags);
2125 if ((opt_type == 1 && *op == '.')
2126 || (opt_type == 0 && *op != '.'))
2127 EMSG2(_(e_letwrong), op);
2128 else
2129 {
2130 if (opt_type == 1) /* number */
2131 {
2132 if (*op == '+')
2133 n = numval + n;
2134 else
2135 n = numval - n;
2136 }
2137 else if (opt_type == 0 && stringval != NULL) /* string */
2138 {
2139 s = concat_str(stringval, s);
2140 vim_free(stringval);
2141 stringval = s;
2142 }
2143 }
2144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002145 if (s != NULL)
2146 {
2147 set_option_value(arg, n, s, opt_flags);
2148 arg_end = p;
2149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002151 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 }
2153 }
2154
2155 /*
2156 * ":let @r = expr": Set register contents.
2157 */
2158 else if (*arg == '@')
2159 {
2160 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 if (op != NULL && (*op == '+' || *op == '-'))
2162 EMSG2(_(e_letwrong), op);
2163 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 EMSG(_(e_letunexp));
2166 else
2167 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002168 char_u *tofree = NULL;
2169 char_u *s;
2170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002171 p = get_tv_string_chk(tv);
2172 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002174 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 if (s != NULL)
2176 {
2177 p = tofree = concat_str(s, p);
2178 vim_free(s);
2179 }
2180 }
2181 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002182 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002183 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002184 arg_end = arg + 1;
2185 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 }
2188 }
2189
2190 /*
2191 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002194 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002196 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002198 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2202 EMSG(_(e_letunexp));
2203 else
2204 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 arg_end = p;
2207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
2211
2212 else
2213 EMSG2(_(e_invarg2), arg);
2214
2215 return arg_end;
2216}
2217
2218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2220 */
2221 static int
2222check_changedtick(arg)
2223 char_u *arg;
2224{
2225 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2226 {
2227 EMSG2(_(e_readonlyvar), arg);
2228 return TRUE;
2229 }
2230 return FALSE;
2231}
2232
2233/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 * Get an lval: variable, Dict item or List item that can be assigned a value
2235 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2236 * "name.key", "name.key[expr]" etc.
2237 * Indexing only works if "name" is an existing List or Dictionary.
2238 * "name" points to the start of the name.
2239 * If "rettv" is not NULL it points to the value to be assigned.
2240 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2241 * wrong; must end in space or cmd separator.
2242 *
2243 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 */
2247 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002248get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002250 typval_T *rettv;
2251 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 int unlet;
2253 int skip;
2254 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002255 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 char_u *expr_start, *expr_end;
2259 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002260 dictitem_T *v;
2261 typval_T var1;
2262 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002264 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002266 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002267 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002270 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271
2272 if (skip)
2273 {
2274 /* When skipping just find the end of the name. */
2275 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002276 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 }
2278
2279 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002280 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 if (expr_start != NULL)
2282 {
2283 /* Don't expand the name when we already know there is an error. */
2284 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2285 && *p != '[' && *p != '.')
2286 {
2287 EMSG(_(e_trailing));
2288 return NULL;
2289 }
2290
2291 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2292 if (lp->ll_exp_name == NULL)
2293 {
2294 /* Report an invalid expression in braces, unless the
2295 * expression evaluation has been cancelled due to an
2296 * aborting error, an interrupt, or an exception. */
2297 if (!aborting() && !quiet)
2298 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002299 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 EMSG2(_(e_invarg2), name);
2301 return NULL;
2302 }
2303 }
2304 lp->ll_name = lp->ll_exp_name;
2305 }
2306 else
2307 lp->ll_name = name;
2308
2309 /* Without [idx] or .key we are done. */
2310 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2311 return p;
2312
2313 cc = *p;
2314 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002315 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 if (v == NULL && !quiet)
2317 EMSG2(_(e_undefvar), lp->ll_name);
2318 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 if (v == NULL)
2320 return NULL;
2321
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 /*
2323 * Loop until no more [idx] or .key is following.
2324 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2329 && !(lp->ll_tv->v_type == VAR_DICT
2330 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (!quiet)
2333 EMSG(_("E689: Can only index a List or Dictionary"));
2334 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (!quiet)
2339 EMSG(_("E708: [:] must come last"));
2340 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002342
Bram Moolenaar8c711452005-01-14 21:53:12 +00002343 len = -1;
2344 if (*p == '.')
2345 {
2346 key = p + 1;
2347 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2348 ;
2349 if (len == 0)
2350 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 if (!quiet)
2352 EMSG(_(e_emptykey));
2353 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 }
2355 p = key + len;
2356 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002357 else
2358 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002360 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 if (*p == ':')
2362 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002363 else
2364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365 empty1 = FALSE;
2366 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 if (get_tv_string_chk(&var1) == NULL)
2369 {
2370 /* not a number or string */
2371 clear_tv(&var1);
2372 return NULL;
2373 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374 }
2375
2376 /* Optionally get the second index [ :expr]. */
2377 if (*p == ':')
2378 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002383 if (!empty1)
2384 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002386 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (rettv != NULL && (rettv->v_type != VAR_LIST
2388 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002389 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 if (!quiet)
2391 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 if (!empty1)
2393 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 }
2396 p = skipwhite(p + 1);
2397 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 else
2400 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2403 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 if (!empty1)
2405 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 if (get_tv_string_chk(&var2) == NULL)
2409 {
2410 /* not a number or string */
2411 if (!empty1)
2412 clear_tv(&var1);
2413 clear_tv(&var2);
2414 return NULL;
2415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002421
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (!quiet)
2425 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 if (!empty1)
2427 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 }
2432
2433 /* Skip to past ']'. */
2434 ++p;
2435 }
2436
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 {
2439 if (len == -1)
2440 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 if (*key == NUL)
2444 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (!quiet)
2446 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 }
2450 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 lp->ll_list = NULL;
2452 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002453 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002456 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 if (len == -1)
2462 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 }
2465 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 if (len == -1)
2470 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 p = NULL;
2473 break;
2474 }
2475 if (len == -1)
2476 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 }
2479 else
2480 {
2481 /*
2482 * Get the number and item for the only or first index of the List.
2483 */
2484 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 else
2487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002488 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 clear_tv(&var1);
2490 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 lp->ll_list = lp->ll_tv->vval.v_list;
2493 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2494 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (!quiet)
2497 EMSGN(_(e_listidx), lp->ll_n1);
2498 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 }
2502
2503 /*
2504 * May need to find the item or absolute index for the second
2505 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 * When no index given: "lp->ll_empty2" is TRUE.
2507 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 if (ni == NULL)
2517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (!quiet)
2519 EMSGN(_(e_listidx), lp->ll_n2);
2520 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 }
2524
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2526 if (lp->ll_n1 < 0)
2527 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2528 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002529 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 if (!quiet)
2531 EMSGN(_(e_listidx), lp->ll_n2);
2532 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002533 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002534 }
2535
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 }
2539
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 return p;
2541}
2542
2543/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 */
2546 static void
2547clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002548 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549{
2550 vim_free(lp->ll_exp_name);
2551 vim_free(lp->ll_newkey);
2552}
2553
2554/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002555 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 */
2559 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002560set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566{
2567 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 listitem_T *ri;
2569 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570
2571 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 cc = *endp;
2576 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002577 if (op != NULL && *op != '=')
2578 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002579 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002580
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002581 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002582 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2583 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002584 {
2585 if (tv_op(&tv, rettv, op) == OK)
2586 set_var(lp->ll_name, &tv, FALSE);
2587 clear_tv(&tv);
2588 }
2589 }
2590 else
2591 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002595 else if (tv_check_lock(lp->ll_newkey == NULL
2596 ? lp->ll_tv->v_lock
2597 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2598 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 else if (lp->ll_range)
2600 {
2601 /*
2602 * Assign the List values to the list items.
2603 */
2604 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002605 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002606 if (op != NULL && *op != '=')
2607 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2608 else
2609 {
2610 clear_tv(&lp->ll_li->li_tv);
2611 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2612 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 ri = ri->li_next;
2614 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2615 break;
2616 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002619 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 ri = NULL;
2622 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002624 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 lp->ll_li = lp->ll_li->li_next;
2626 ++lp->ll_n1;
2627 }
2628 if (ri != NULL)
2629 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002630 else if (lp->ll_empty2
2631 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 : lp->ll_n1 != lp->ll_n2)
2633 EMSG(_("E711: List value has not enough items"));
2634 }
2635 else
2636 {
2637 /*
2638 * Assign to a List or Dictionary item.
2639 */
2640 if (lp->ll_newkey != NULL)
2641 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002642 if (op != NULL && *op != '=')
2643 {
2644 EMSG2(_(e_letwrong), op);
2645 return;
2646 }
2647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002649 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (di == NULL)
2651 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002652 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2653 {
2654 vim_free(di);
2655 return;
2656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 else if (op != NULL && *op != '=')
2660 {
2661 tv_op(lp->ll_tv, rettv, op);
2662 return;
2663 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 /*
2668 * Assign the value to the variable or list item.
2669 */
2670 if (copy)
2671 copy_tv(rettv, lp->ll_tv);
2672 else
2673 {
2674 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002675 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002677 }
2678 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002679}
2680
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002681/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002682 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2683 * Returns OK or FAIL.
2684 */
2685 static int
2686tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002687 typval_T *tv1;
2688 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 char_u *op;
2690{
2691 long n;
2692 char_u numbuf[NUMBUFLEN];
2693 char_u *s;
2694
2695 /* Can't do anything with a Funcref or a Dict on the right. */
2696 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2697 {
2698 switch (tv1->v_type)
2699 {
2700 case VAR_DICT:
2701 case VAR_FUNC:
2702 break;
2703
2704 case VAR_LIST:
2705 if (*op != '+' || tv2->v_type != VAR_LIST)
2706 break;
2707 /* List += List */
2708 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2709 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2710 return OK;
2711
2712 case VAR_NUMBER:
2713 case VAR_STRING:
2714 if (tv2->v_type == VAR_LIST)
2715 break;
2716 if (*op == '+' || *op == '-')
2717 {
2718 /* nr += nr or nr -= nr*/
2719 n = get_tv_number(tv1);
2720 if (*op == '+')
2721 n += get_tv_number(tv2);
2722 else
2723 n -= get_tv_number(tv2);
2724 clear_tv(tv1);
2725 tv1->v_type = VAR_NUMBER;
2726 tv1->vval.v_number = n;
2727 }
2728 else
2729 {
2730 /* str .= str */
2731 s = get_tv_string(tv1);
2732 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2733 clear_tv(tv1);
2734 tv1->v_type = VAR_STRING;
2735 tv1->vval.v_string = s;
2736 }
2737 return OK;
2738 }
2739 }
2740
2741 EMSG2(_(e_letwrong), op);
2742 return FAIL;
2743}
2744
2745/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002746 * Add a watcher to a list.
2747 */
2748 static void
2749list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002750 list_T *l;
2751 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002752{
2753 lw->lw_next = l->lv_watch;
2754 l->lv_watch = lw;
2755}
2756
2757/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002758 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002759 * No warning when it isn't found...
2760 */
2761 static void
2762list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002763 list_T *l;
2764 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002765{
Bram Moolenaar33570922005-01-25 22:26:29 +00002766 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002767
2768 lwp = &l->lv_watch;
2769 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2770 {
2771 if (lw == lwrem)
2772 {
2773 *lwp = lw->lw_next;
2774 break;
2775 }
2776 lwp = &lw->lw_next;
2777 }
2778}
2779
2780/*
2781 * Just before removing an item from a list: advance watchers to the next
2782 * item.
2783 */
2784 static void
2785list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002786 list_T *l;
2787 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002788{
Bram Moolenaar33570922005-01-25 22:26:29 +00002789 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002790
2791 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2792 if (lw->lw_item == item)
2793 lw->lw_item = item->li_next;
2794}
2795
2796/*
2797 * Evaluate the expression used in a ":for var in expr" command.
2798 * "arg" points to "var".
2799 * Set "*errp" to TRUE for an error, FALSE otherwise;
2800 * Return a pointer that holds the info. Null when there is an error.
2801 */
2802 void *
2803eval_for_line(arg, errp, nextcmdp, skip)
2804 char_u *arg;
2805 int *errp;
2806 char_u **nextcmdp;
2807 int skip;
2808{
Bram Moolenaar33570922005-01-25 22:26:29 +00002809 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002810 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002811 typval_T tv;
2812 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002813
2814 *errp = TRUE; /* default: there is an error */
2815
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002817 if (fi == NULL)
2818 return NULL;
2819
2820 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2821 if (expr == NULL)
2822 return fi;
2823
2824 expr = skipwhite(expr);
2825 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2826 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002827 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828 return fi;
2829 }
2830
2831 if (skip)
2832 ++emsg_skip;
2833 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2834 {
2835 *errp = FALSE;
2836 if (!skip)
2837 {
2838 l = tv.vval.v_list;
2839 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002840 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002841 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002842 clear_tv(&tv);
2843 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002844 else
2845 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002846 /* No need to increment the refcount, it's already set for the
2847 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848 fi->fi_list = l;
2849 list_add_watch(l, &fi->fi_lw);
2850 fi->fi_lw.lw_item = l->lv_first;
2851 }
2852 }
2853 }
2854 if (skip)
2855 --emsg_skip;
2856
2857 return fi;
2858}
2859
2860/*
2861 * Use the first item in a ":for" list. Advance to the next.
2862 * Assign the values to the variable (list). "arg" points to the first one.
2863 * Return TRUE when a valid item was found, FALSE when at end of list or
2864 * something wrong.
2865 */
2866 int
2867next_for_item(fi_void, arg)
2868 void *fi_void;
2869 char_u *arg;
2870{
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874
2875 item = fi->fi_lw.lw_item;
2876 if (item == NULL)
2877 result = FALSE;
2878 else
2879 {
2880 fi->fi_lw.lw_item = item->li_next;
2881 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2882 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2883 }
2884 return result;
2885}
2886
2887/*
2888 * Free the structure used to store info used by ":for".
2889 */
2890 void
2891free_for_info(fi_void)
2892 void *fi_void;
2893{
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002896 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002897 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002898 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002899 list_unref(fi->fi_list);
2900 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002901 vim_free(fi);
2902}
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2905
2906 void
2907set_context_for_expression(xp, arg, cmdidx)
2908 expand_T *xp;
2909 char_u *arg;
2910 cmdidx_T cmdidx;
2911{
2912 int got_eq = FALSE;
2913 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916 if (cmdidx == CMD_let)
2917 {
2918 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002919 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002920 {
2921 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002922 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 {
2924 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002925 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926 if (vim_iswhite(*p))
2927 break;
2928 }
2929 return;
2930 }
2931 }
2932 else
2933 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2934 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 while ((xp->xp_pattern = vim_strpbrk(arg,
2936 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2937 {
2938 c = *xp->xp_pattern;
2939 if (c == '&')
2940 {
2941 c = xp->xp_pattern[1];
2942 if (c == '&')
2943 {
2944 ++xp->xp_pattern;
2945 xp->xp_context = cmdidx != CMD_let || got_eq
2946 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2947 }
2948 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002951 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2952 xp->xp_pattern += 2;
2953
2954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 }
2956 else if (c == '$')
2957 {
2958 /* environment variable */
2959 xp->xp_context = EXPAND_ENV_VARS;
2960 }
2961 else if (c == '=')
2962 {
2963 got_eq = TRUE;
2964 xp->xp_context = EXPAND_EXPRESSION;
2965 }
2966 else if (c == '<'
2967 && xp->xp_context == EXPAND_FUNCTIONS
2968 && vim_strchr(xp->xp_pattern, '(') == NULL)
2969 {
2970 /* Function name can start with "<SNR>" */
2971 break;
2972 }
2973 else if (cmdidx != CMD_let || got_eq)
2974 {
2975 if (c == '"') /* string */
2976 {
2977 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2978 if (c == '\\' && xp->xp_pattern[1] != NUL)
2979 ++xp->xp_pattern;
2980 xp->xp_context = EXPAND_NOTHING;
2981 }
2982 else if (c == '\'') /* literal string */
2983 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002984 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2986 /* skip */ ;
2987 xp->xp_context = EXPAND_NOTHING;
2988 }
2989 else if (c == '|')
2990 {
2991 if (xp->xp_pattern[1] == '|')
2992 {
2993 ++xp->xp_pattern;
2994 xp->xp_context = EXPAND_EXPRESSION;
2995 }
2996 else
2997 xp->xp_context = EXPAND_COMMANDS;
2998 }
2999 else
3000 xp->xp_context = EXPAND_EXPRESSION;
3001 }
3002 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 /* Doesn't look like something valid, expand as an expression
3004 * anyway. */
3005 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 arg = xp->xp_pattern;
3007 if (*arg != NUL)
3008 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3009 /* skip */ ;
3010 }
3011 xp->xp_pattern = arg;
3012}
3013
3014#endif /* FEAT_CMDL_COMPL */
3015
3016/*
3017 * ":1,25call func(arg1, arg2)" function call.
3018 */
3019 void
3020ex_call(eap)
3021 exarg_T *eap;
3022{
3023 char_u *arg = eap->arg;
3024 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003028 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 linenr_T lnum;
3030 int doesrange;
3031 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003032 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003034 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3035 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (tofree == NULL)
3037 return;
3038
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003039 /* Increase refcount on dictionary, it could get deleted when evaluating
3040 * the arguments. */
3041 if (fudi.fd_dict != NULL)
3042 ++fudi.fd_dict->dv_refcount;
3043
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3045 len = STRLEN(tofree);
3046 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
Bram Moolenaar532c7802005-01-27 14:44:31 +00003048 /* Skip white space to allow ":call func ()". Not good, but required for
3049 * backward compatibility. */
3050 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 if (*startarg != '(')
3054 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003055 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 goto end;
3057 }
3058
3059 /*
3060 * When skipping, evaluate the function once, to find the end of the
3061 * arguments.
3062 * When the function takes a range, this is discovered after the first
3063 * call, and the loop is broken.
3064 */
3065 if (eap->skip)
3066 {
3067 ++emsg_skip;
3068 lnum = eap->line2; /* do it once, also with an invalid range */
3069 }
3070 else
3071 lnum = eap->line1;
3072 for ( ; lnum <= eap->line2; ++lnum)
3073 {
3074 if (!eap->skip && eap->addr_count > 0)
3075 {
3076 curwin->w_cursor.lnum = lnum;
3077 curwin->w_cursor.col = 0;
3078 }
3079 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003081 eap->line1, eap->line2, &doesrange,
3082 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
3084 failed = TRUE;
3085 break;
3086 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003087 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (doesrange || eap->skip)
3089 break;
3090 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003091 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003092 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003093 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (aborting())
3095 break;
3096 }
3097 if (eap->skip)
3098 --emsg_skip;
3099
3100 if (!failed)
3101 {
3102 /* Check for trailing illegal characters and a following command. */
3103 if (!ends_excmd(*arg))
3104 {
3105 emsg_severe = TRUE;
3106 EMSG(_(e_trailing));
3107 }
3108 else
3109 eap->nextcmd = check_nextcmd(arg);
3110 }
3111
3112end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003113 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115}
3116
3117/*
3118 * ":unlet[!] var1 ... " command.
3119 */
3120 void
3121ex_unlet(eap)
3122 exarg_T *eap;
3123{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003124 ex_unletlock(eap, eap->arg, 0);
3125}
3126
3127/*
3128 * ":lockvar" and ":unlockvar" commands
3129 */
3130 void
3131ex_lockvar(eap)
3132 exarg_T *eap;
3133{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003135 int deep = 2;
3136
3137 if (eap->forceit)
3138 deep = -1;
3139 else if (vim_isdigit(*arg))
3140 {
3141 deep = getdigits(&arg);
3142 arg = skipwhite(arg);
3143 }
3144
3145 ex_unletlock(eap, arg, deep);
3146}
3147
3148/*
3149 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3150 */
3151 static void
3152ex_unletlock(eap, argstart, deep)
3153 exarg_T *eap;
3154 char_u *argstart;
3155 int deep;
3156{
3157 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003160 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 do
3163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003164 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003165 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3166 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003167 if (lv.ll_name == NULL)
3168 error = TRUE; /* error but continue parsing */
3169 if (name_end == NULL || (!vim_iswhite(*name_end)
3170 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003172 if (name_end != NULL)
3173 {
3174 emsg_severe = TRUE;
3175 EMSG(_(e_trailing));
3176 }
3177 if (!(eap->skip || error))
3178 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 break;
3180 }
3181
3182 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003183 {
3184 if (eap->cmdidx == CMD_unlet)
3185 {
3186 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3187 error = TRUE;
3188 }
3189 else
3190 {
3191 if (do_lock_var(&lv, name_end, deep,
3192 eap->cmdidx == CMD_lockvar) == FAIL)
3193 error = TRUE;
3194 }
3195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003197 if (!eap->skip)
3198 clear_lval(&lv);
3199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 arg = skipwhite(name_end);
3201 } while (!ends_excmd(*arg));
3202
3203 eap->nextcmd = check_nextcmd(arg);
3204}
3205
Bram Moolenaar8c711452005-01-14 21:53:12 +00003206 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003207do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003208 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003209 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003210 int forceit;
3211{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212 int ret = OK;
3213 int cc;
3214
3215 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003216 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 cc = *name_end;
3218 *name_end = NUL;
3219
3220 /* Normal name or expanded name. */
3221 if (check_changedtick(lp->ll_name))
3222 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003223 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003224 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003226 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003227 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3228 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003229 else if (lp->ll_range)
3230 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232
3233 /* Delete a range of List items. */
3234 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3235 {
3236 li = lp->ll_li->li_next;
3237 listitem_remove(lp->ll_list, lp->ll_li);
3238 lp->ll_li = li;
3239 ++lp->ll_n1;
3240 }
3241 }
3242 else
3243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003244 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003245 /* unlet a List item. */
3246 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003247 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003248 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003249 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003250 }
3251
3252 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003253}
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255/*
3256 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003257 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 */
3259 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003260do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003262 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 hashtab_T *ht;
3265 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003266 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
Bram Moolenaar33570922005-01-25 22:26:29 +00003268 ht = find_var_ht(name, &varname);
3269 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003271 hi = hash_find(ht, varname);
3272 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003273 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003274 if (var_check_ro(HI2DI(hi)->di_flags, name))
3275 return FAIL;
3276 delete_var(ht, hi);
3277 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003280 if (forceit)
3281 return OK;
3282 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 return FAIL;
3284}
3285
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003286/*
3287 * Lock or unlock variable indicated by "lp".
3288 * "deep" is the levels to go (-1 for unlimited);
3289 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3290 */
3291 static int
3292do_lock_var(lp, name_end, deep, lock)
3293 lval_T *lp;
3294 char_u *name_end;
3295 int deep;
3296 int lock;
3297{
3298 int ret = OK;
3299 int cc;
3300 dictitem_T *di;
3301
3302 if (deep == 0) /* nothing to do */
3303 return OK;
3304
3305 if (lp->ll_tv == NULL)
3306 {
3307 cc = *name_end;
3308 *name_end = NUL;
3309
3310 /* Normal name or expanded name. */
3311 if (check_changedtick(lp->ll_name))
3312 ret = FAIL;
3313 else
3314 {
3315 di = find_var(lp->ll_name, NULL);
3316 if (di == NULL)
3317 ret = FAIL;
3318 else
3319 {
3320 if (lock)
3321 di->di_flags |= DI_FLAGS_LOCK;
3322 else
3323 di->di_flags &= ~DI_FLAGS_LOCK;
3324 item_lock(&di->di_tv, deep, lock);
3325 }
3326 }
3327 *name_end = cc;
3328 }
3329 else if (lp->ll_range)
3330 {
3331 listitem_T *li = lp->ll_li;
3332
3333 /* (un)lock a range of List items. */
3334 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3335 {
3336 item_lock(&li->li_tv, deep, lock);
3337 li = li->li_next;
3338 ++lp->ll_n1;
3339 }
3340 }
3341 else if (lp->ll_list != NULL)
3342 /* (un)lock a List item. */
3343 item_lock(&lp->ll_li->li_tv, deep, lock);
3344 else
3345 /* un(lock) a Dictionary item. */
3346 item_lock(&lp->ll_di->di_tv, deep, lock);
3347
3348 return ret;
3349}
3350
3351/*
3352 * Lock or unlock an item. "deep" is nr of levels to go.
3353 */
3354 static void
3355item_lock(tv, deep, lock)
3356 typval_T *tv;
3357 int deep;
3358 int lock;
3359{
3360 static int recurse = 0;
3361 list_T *l;
3362 listitem_T *li;
3363 dict_T *d;
3364 hashitem_T *hi;
3365 int todo;
3366
3367 if (recurse >= DICT_MAXNEST)
3368 {
3369 EMSG(_("E743: variable nested too deep for (un)lock"));
3370 return;
3371 }
3372 if (deep == 0)
3373 return;
3374 ++recurse;
3375
3376 /* lock/unlock the item itself */
3377 if (lock)
3378 tv->v_lock |= VAR_LOCKED;
3379 else
3380 tv->v_lock &= ~VAR_LOCKED;
3381
3382 switch (tv->v_type)
3383 {
3384 case VAR_LIST:
3385 if ((l = tv->vval.v_list) != NULL)
3386 {
3387 if (lock)
3388 l->lv_lock |= VAR_LOCKED;
3389 else
3390 l->lv_lock &= ~VAR_LOCKED;
3391 if (deep < 0 || deep > 1)
3392 /* recursive: lock/unlock the items the List contains */
3393 for (li = l->lv_first; li != NULL; li = li->li_next)
3394 item_lock(&li->li_tv, deep - 1, lock);
3395 }
3396 break;
3397 case VAR_DICT:
3398 if ((d = tv->vval.v_dict) != NULL)
3399 {
3400 if (lock)
3401 d->dv_lock |= VAR_LOCKED;
3402 else
3403 d->dv_lock &= ~VAR_LOCKED;
3404 if (deep < 0 || deep > 1)
3405 {
3406 /* recursive: lock/unlock the items the List contains */
3407 todo = d->dv_hashtab.ht_used;
3408 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3409 {
3410 if (!HASHITEM_EMPTY(hi))
3411 {
3412 --todo;
3413 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3414 }
3415 }
3416 }
3417 }
3418 }
3419 --recurse;
3420}
3421
Bram Moolenaara40058a2005-07-11 22:42:07 +00003422/*
3423 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3424 * it refers to a List or Dictionary that is locked.
3425 */
3426 static int
3427tv_islocked(tv)
3428 typval_T *tv;
3429{
3430 return (tv->v_lock & VAR_LOCKED)
3431 || (tv->v_type == VAR_LIST
3432 && tv->vval.v_list != NULL
3433 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3434 || (tv->v_type == VAR_DICT
3435 && tv->vval.v_dict != NULL
3436 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3437}
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3440/*
3441 * Delete all "menutrans_" variables.
3442 */
3443 void
3444del_menutrans_vars()
3445{
Bram Moolenaar33570922005-01-25 22:26:29 +00003446 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003447 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
Bram Moolenaar33570922005-01-25 22:26:29 +00003449 hash_lock(&globvarht);
3450 todo = globvarht.ht_used;
3451 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003452 {
3453 if (!HASHITEM_EMPTY(hi))
3454 {
3455 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3457 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003458 }
3459 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461}
3462#endif
3463
3464#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3465
3466/*
3467 * Local string buffer for the next two functions to store a variable name
3468 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3469 * get_user_var_name().
3470 */
3471
3472static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3473
3474static char_u *varnamebuf = NULL;
3475static int varnamebuflen = 0;
3476
3477/*
3478 * Function to concatenate a prefix and a variable name.
3479 */
3480 static char_u *
3481cat_prefix_varname(prefix, name)
3482 int prefix;
3483 char_u *name;
3484{
3485 int len;
3486
3487 len = (int)STRLEN(name) + 3;
3488 if (len > varnamebuflen)
3489 {
3490 vim_free(varnamebuf);
3491 len += 10; /* some additional space */
3492 varnamebuf = alloc(len);
3493 if (varnamebuf == NULL)
3494 {
3495 varnamebuflen = 0;
3496 return NULL;
3497 }
3498 varnamebuflen = len;
3499 }
3500 *varnamebuf = prefix;
3501 varnamebuf[1] = ':';
3502 STRCPY(varnamebuf + 2, name);
3503 return varnamebuf;
3504}
3505
3506/*
3507 * Function given to ExpandGeneric() to obtain the list of user defined
3508 * (global/buffer/window/built-in) variable names.
3509 */
3510/*ARGSUSED*/
3511 char_u *
3512get_user_var_name(xp, idx)
3513 expand_T *xp;
3514 int idx;
3515{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003516 static long_u gdone;
3517 static long_u bdone;
3518 static long_u wdone;
3519 static int vidx;
3520 static hashitem_T *hi;
3521 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003524 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003525
3526 /* Global variables */
3527 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003529 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003530 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003531 else
3532 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003533 while (HASHITEM_EMPTY(hi))
3534 ++hi;
3535 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3536 return cat_prefix_varname('g', hi->hi_key);
3537 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003539
3540 /* b: variables */
3541 ht = &curbuf->b_vars.dv_hashtab;
3542 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003544 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003545 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003546 else
3547 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003548 while (HASHITEM_EMPTY(hi))
3549 ++hi;
3550 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 return (char_u *)"b:changedtick";
3556 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003557
3558 /* w: variables */
3559 ht = &curwin->w_vars.dv_hashtab;
3560 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003562 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003563 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003564 else
3565 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003566 while (HASHITEM_EMPTY(hi))
3567 ++hi;
3568 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003570
3571 /* v: variables */
3572 if (vidx < VV_LEN)
3573 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
3575 vim_free(varnamebuf);
3576 varnamebuf = NULL;
3577 varnamebuflen = 0;
3578 return NULL;
3579}
3580
3581#endif /* FEAT_CMDL_COMPL */
3582
3583/*
3584 * types for expressions.
3585 */
3586typedef enum
3587{
3588 TYPE_UNKNOWN = 0
3589 , TYPE_EQUAL /* == */
3590 , TYPE_NEQUAL /* != */
3591 , TYPE_GREATER /* > */
3592 , TYPE_GEQUAL /* >= */
3593 , TYPE_SMALLER /* < */
3594 , TYPE_SEQUAL /* <= */
3595 , TYPE_MATCH /* =~ */
3596 , TYPE_NOMATCH /* !~ */
3597} exptype_T;
3598
3599/*
3600 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003601 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3603 */
3604
3605/*
3606 * Handle zero level expression.
3607 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003609 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 * Return OK or FAIL.
3611 */
3612 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003613eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 char_u **nextcmd;
3617 int evaluate;
3618{
3619 int ret;
3620 char_u *p;
3621
3622 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003623 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (ret == FAIL || !ends_excmd(*p))
3625 {
3626 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003627 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 /*
3629 * Report the invalid expression unless the expression evaluation has
3630 * been cancelled due to an aborting error, an interrupt, or an
3631 * exception.
3632 */
3633 if (!aborting())
3634 EMSG2(_(e_invexpr2), arg);
3635 ret = FAIL;
3636 }
3637 if (nextcmd != NULL)
3638 *nextcmd = check_nextcmd(p);
3639
3640 return ret;
3641}
3642
3643/*
3644 * Handle top level expression:
3645 * expr1 ? expr0 : expr0
3646 *
3647 * "arg" must point to the first non-white of the expression.
3648 * "arg" is advanced to the next non-white after the recognized expression.
3649 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003650 * Note: "rettv.v_lock" is not set.
3651 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 * Return OK or FAIL.
3653 */
3654 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 int evaluate;
3659{
3660 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 /*
3664 * Get the first variable.
3665 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003666 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 return FAIL;
3668
3669 if ((*arg)[0] == '?')
3670 {
3671 result = FALSE;
3672 if (evaluate)
3673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003674 int error = FALSE;
3675
3676 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003679 if (error)
3680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682
3683 /*
3684 * Get the second variable.
3685 */
3686 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003687 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 return FAIL;
3689
3690 /*
3691 * Check for the ":".
3692 */
3693 if ((*arg)[0] != ':')
3694 {
3695 EMSG(_("E109: Missing ':' after '?'"));
3696 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003697 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 return FAIL;
3699 }
3700
3701 /*
3702 * Get the third variable.
3703 */
3704 *arg = skipwhite(*arg + 1);
3705 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3706 {
3707 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003708 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return FAIL;
3710 }
3711 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003712 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
3714
3715 return OK;
3716}
3717
3718/*
3719 * Handle first level expression:
3720 * expr2 || expr2 || expr2 logical OR
3721 *
3722 * "arg" must point to the first non-white of the expression.
3723 * "arg" is advanced to the next non-white after the recognized expression.
3724 *
3725 * Return OK or FAIL.
3726 */
3727 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 int evaluate;
3732{
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 long result;
3735 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 /*
3739 * Get the first variable.
3740 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003741 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return FAIL;
3743
3744 /*
3745 * Repeat until there is no following "||".
3746 */
3747 first = TRUE;
3748 result = FALSE;
3749 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3750 {
3751 if (evaluate && first)
3752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 if (error)
3757 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 first = FALSE;
3759 }
3760
3761 /*
3762 * Get the second variable.
3763 */
3764 *arg = skipwhite(*arg + 2);
3765 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3766 return FAIL;
3767
3768 /*
3769 * Compute the result.
3770 */
3771 if (evaluate && !result)
3772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003773 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003776 if (error)
3777 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779 if (evaluate)
3780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 rettv->v_type = VAR_NUMBER;
3782 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 }
3785
3786 return OK;
3787}
3788
3789/*
3790 * Handle second level expression:
3791 * expr3 && expr3 && expr3 logical AND
3792 *
3793 * "arg" must point to the first non-white of the expression.
3794 * "arg" is advanced to the next non-white after the recognized expression.
3795 *
3796 * Return OK or FAIL.
3797 */
3798 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 int evaluate;
3803{
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 long result;
3806 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003807 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
3809 /*
3810 * Get the first variable.
3811 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 return FAIL;
3814
3815 /*
3816 * Repeat until there is no following "&&".
3817 */
3818 first = TRUE;
3819 result = TRUE;
3820 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3821 {
3822 if (evaluate && first)
3823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003824 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003827 if (error)
3828 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 first = FALSE;
3830 }
3831
3832 /*
3833 * Get the second variable.
3834 */
3835 *arg = skipwhite(*arg + 2);
3836 if (eval4(arg, &var2, evaluate && result) == FAIL)
3837 return FAIL;
3838
3839 /*
3840 * Compute the result.
3841 */
3842 if (evaluate && result)
3843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003844 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003847 if (error)
3848 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
3850 if (evaluate)
3851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003852 rettv->v_type = VAR_NUMBER;
3853 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 }
3856
3857 return OK;
3858}
3859
3860/*
3861 * Handle third level expression:
3862 * var1 == var2
3863 * var1 =~ var2
3864 * var1 != var2
3865 * var1 !~ var2
3866 * var1 > var2
3867 * var1 >= var2
3868 * var1 < var2
3869 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003870 * var1 is var2
3871 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 *
3873 * "arg" must point to the first non-white of the expression.
3874 * "arg" is advanced to the next non-white after the recognized expression.
3875 *
3876 * Return OK or FAIL.
3877 */
3878 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 int evaluate;
3883{
Bram Moolenaar33570922005-01-25 22:26:29 +00003884 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 char_u *p;
3886 int i;
3887 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003888 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 int len = 2;
3890 long n1, n2;
3891 char_u *s1, *s2;
3892 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3893 regmatch_T regmatch;
3894 int ic;
3895 char_u *save_cpo;
3896
3897 /*
3898 * Get the first variable.
3899 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 return FAIL;
3902
3903 p = *arg;
3904 switch (p[0])
3905 {
3906 case '=': if (p[1] == '=')
3907 type = TYPE_EQUAL;
3908 else if (p[1] == '~')
3909 type = TYPE_MATCH;
3910 break;
3911 case '!': if (p[1] == '=')
3912 type = TYPE_NEQUAL;
3913 else if (p[1] == '~')
3914 type = TYPE_NOMATCH;
3915 break;
3916 case '>': if (p[1] != '=')
3917 {
3918 type = TYPE_GREATER;
3919 len = 1;
3920 }
3921 else
3922 type = TYPE_GEQUAL;
3923 break;
3924 case '<': if (p[1] != '=')
3925 {
3926 type = TYPE_SMALLER;
3927 len = 1;
3928 }
3929 else
3930 type = TYPE_SEQUAL;
3931 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003932 case 'i': if (p[1] == 's')
3933 {
3934 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3935 len = 5;
3936 if (!vim_isIDc(p[len]))
3937 {
3938 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3939 type_is = TRUE;
3940 }
3941 }
3942 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944
3945 /*
3946 * If there is a comparitive operator, use it.
3947 */
3948 if (type != TYPE_UNKNOWN)
3949 {
3950 /* extra question mark appended: ignore case */
3951 if (p[len] == '?')
3952 {
3953 ic = TRUE;
3954 ++len;
3955 }
3956 /* extra '#' appended: match case */
3957 else if (p[len] == '#')
3958 {
3959 ic = FALSE;
3960 ++len;
3961 }
3962 /* nothing appened: use 'ignorecase' */
3963 else
3964 ic = p_ic;
3965
3966 /*
3967 * Get the second variable.
3968 */
3969 *arg = skipwhite(p + len);
3970 if (eval5(arg, &var2, evaluate) == FAIL)
3971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 return FAIL;
3974 }
3975
3976 if (evaluate)
3977 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003978 if (type_is && rettv->v_type != var2.v_type)
3979 {
3980 /* For "is" a different type always means FALSE, for "notis"
3981 * it means TRUE. */
3982 n1 = (type == TYPE_NEQUAL);
3983 }
3984 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3985 {
3986 if (type_is)
3987 {
3988 n1 = (rettv->v_type == var2.v_type
3989 && rettv->vval.v_list == var2.vval.v_list);
3990 if (type == TYPE_NEQUAL)
3991 n1 = !n1;
3992 }
3993 else if (rettv->v_type != var2.v_type
3994 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3995 {
3996 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003997 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003998 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003999 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004000 clear_tv(rettv);
4001 clear_tv(&var2);
4002 return FAIL;
4003 }
4004 else
4005 {
4006 /* Compare two Lists for being equal or unequal. */
4007 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4008 if (type == TYPE_NEQUAL)
4009 n1 = !n1;
4010 }
4011 }
4012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004013 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4014 {
4015 if (type_is)
4016 {
4017 n1 = (rettv->v_type == var2.v_type
4018 && rettv->vval.v_dict == var2.vval.v_dict);
4019 if (type == TYPE_NEQUAL)
4020 n1 = !n1;
4021 }
4022 else if (rettv->v_type != var2.v_type
4023 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4024 {
4025 if (rettv->v_type != var2.v_type)
4026 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4027 else
4028 EMSG(_("E736: Invalid operation for Dictionary"));
4029 clear_tv(rettv);
4030 clear_tv(&var2);
4031 return FAIL;
4032 }
4033 else
4034 {
4035 /* Compare two Dictionaries for being equal or unequal. */
4036 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4037 if (type == TYPE_NEQUAL)
4038 n1 = !n1;
4039 }
4040 }
4041
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004042 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4043 {
4044 if (rettv->v_type != var2.v_type
4045 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4046 {
4047 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004048 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004049 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004050 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004051 clear_tv(rettv);
4052 clear_tv(&var2);
4053 return FAIL;
4054 }
4055 else
4056 {
4057 /* Compare two Funcrefs for being equal or unequal. */
4058 if (rettv->vval.v_string == NULL
4059 || var2.vval.v_string == NULL)
4060 n1 = FALSE;
4061 else
4062 n1 = STRCMP(rettv->vval.v_string,
4063 var2.vval.v_string) == 0;
4064 if (type == TYPE_NEQUAL)
4065 n1 = !n1;
4066 }
4067 }
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 /*
4070 * If one of the two variables is a number, compare as a number.
4071 * When using "=~" or "!~", always compare as string.
4072 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004073 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004076 n1 = get_tv_number(rettv);
4077 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 switch (type)
4079 {
4080 case TYPE_EQUAL: n1 = (n1 == n2); break;
4081 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4082 case TYPE_GREATER: n1 = (n1 > n2); break;
4083 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4084 case TYPE_SMALLER: n1 = (n1 < n2); break;
4085 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4086 case TYPE_UNKNOWN:
4087 case TYPE_MATCH:
4088 case TYPE_NOMATCH: break; /* avoid gcc warning */
4089 }
4090 }
4091 else
4092 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 s1 = get_tv_string_buf(rettv, buf1);
4094 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4096 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4097 else
4098 i = 0;
4099 n1 = FALSE;
4100 switch (type)
4101 {
4102 case TYPE_EQUAL: n1 = (i == 0); break;
4103 case TYPE_NEQUAL: n1 = (i != 0); break;
4104 case TYPE_GREATER: n1 = (i > 0); break;
4105 case TYPE_GEQUAL: n1 = (i >= 0); break;
4106 case TYPE_SMALLER: n1 = (i < 0); break;
4107 case TYPE_SEQUAL: n1 = (i <= 0); break;
4108
4109 case TYPE_MATCH:
4110 case TYPE_NOMATCH:
4111 /* avoid 'l' flag in 'cpoptions' */
4112 save_cpo = p_cpo;
4113 p_cpo = (char_u *)"";
4114 regmatch.regprog = vim_regcomp(s2,
4115 RE_MAGIC + RE_STRING);
4116 regmatch.rm_ic = ic;
4117 if (regmatch.regprog != NULL)
4118 {
4119 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4120 vim_free(regmatch.regprog);
4121 if (type == TYPE_NOMATCH)
4122 n1 = !n1;
4123 }
4124 p_cpo = save_cpo;
4125 break;
4126
4127 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4128 }
4129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 clear_tv(rettv);
4131 clear_tv(&var2);
4132 rettv->v_type = VAR_NUMBER;
4133 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 }
4136
4137 return OK;
4138}
4139
4140/*
4141 * Handle fourth level expression:
4142 * + number addition
4143 * - number subtraction
4144 * . string concatenation
4145 *
4146 * "arg" must point to the first non-white of the expression.
4147 * "arg" is advanced to the next non-white after the recognized expression.
4148 *
4149 * Return OK or FAIL.
4150 */
4151 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 int evaluate;
4156{
Bram Moolenaar33570922005-01-25 22:26:29 +00004157 typval_T var2;
4158 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 int op;
4160 long n1, n2;
4161 char_u *s1, *s2;
4162 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4163 char_u *p;
4164
4165 /*
4166 * Get the first variable.
4167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 return FAIL;
4170
4171 /*
4172 * Repeat computing, until no '+', '-' or '.' is following.
4173 */
4174 for (;;)
4175 {
4176 op = **arg;
4177 if (op != '+' && op != '-' && op != '.')
4178 break;
4179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 if (op != '+' || rettv->v_type != VAR_LIST)
4181 {
4182 /* For "list + ...", an illegal use of the first operand as
4183 * a number cannot be determined before evaluating the 2nd
4184 * operand: if this is also a list, all is ok.
4185 * For "something . ...", "something - ..." or "non-list + ...",
4186 * we know that the first operand needs to be a string or number
4187 * without evaluating the 2nd operand. So check before to avoid
4188 * side effects after an error. */
4189 if (evaluate && get_tv_string_chk(rettv) == NULL)
4190 {
4191 clear_tv(rettv);
4192 return FAIL;
4193 }
4194 }
4195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 1);
4200 if (eval6(arg, &var2, evaluate) == FAIL)
4201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return FAIL;
4204 }
4205
4206 if (evaluate)
4207 {
4208 /*
4209 * Compute the result.
4210 */
4211 if (op == '.')
4212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004213 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4214 s2 = get_tv_string_buf_chk(&var2, buf2);
4215 if (s2 == NULL) /* type error ? */
4216 {
4217 clear_tv(rettv);
4218 clear_tv(&var2);
4219 return FAIL;
4220 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004221 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
4223 rettv->v_type = VAR_STRING;
4224 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004226 else if (op == '+' && rettv->v_type == VAR_LIST
4227 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004228 {
4229 /* concatenate Lists */
4230 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4231 &var3) == FAIL)
4232 {
4233 clear_tv(rettv);
4234 clear_tv(&var2);
4235 return FAIL;
4236 }
4237 clear_tv(rettv);
4238 *rettv = var3;
4239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 else
4241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
4243
4244 n1 = get_tv_number_chk(rettv, &error);
4245 if (error)
4246 {
4247 /* This can only happen for "list + non-list".
4248 * For "non-list + ..." or "something - ...", we returned
4249 * before evaluating the 2nd operand. */
4250 clear_tv(rettv);
4251 return FAIL;
4252 }
4253 n2 = get_tv_number_chk(&var2, &error);
4254 if (error)
4255 {
4256 clear_tv(rettv);
4257 clear_tv(&var2);
4258 return FAIL;
4259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (op == '+')
4262 n1 = n1 + n2;
4263 else
4264 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 rettv->v_type = VAR_NUMBER;
4266 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 }
4271 return OK;
4272}
4273
4274/*
4275 * Handle fifth level expression:
4276 * * number multiplication
4277 * / number division
4278 * % number modulo
4279 *
4280 * "arg" must point to the first non-white of the expression.
4281 * "arg" is advanced to the next non-white after the recognized expression.
4282 *
4283 * Return OK or FAIL.
4284 */
4285 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 int evaluate;
4290{
Bram Moolenaar33570922005-01-25 22:26:29 +00004291 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 int op;
4293 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 /*
4297 * Get the first variable.
4298 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 return FAIL;
4301
4302 /*
4303 * Repeat computing, until no '*', '/' or '%' is following.
4304 */
4305 for (;;)
4306 {
4307 op = **arg;
4308 if (op != '*' && op != '/' && op != '%')
4309 break;
4310
4311 if (evaluate)
4312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 if (error)
4316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318 else
4319 n1 = 0;
4320
4321 /*
4322 * Get the second variable.
4323 */
4324 *arg = skipwhite(*arg + 1);
4325 if (eval7(arg, &var2, evaluate) == FAIL)
4326 return FAIL;
4327
4328 if (evaluate)
4329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (error)
4333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 /*
4336 * Compute the result.
4337 */
4338 if (op == '*')
4339 n1 = n1 * n2;
4340 else if (op == '/')
4341 {
4342 if (n2 == 0) /* give an error message? */
4343 n1 = 0x7fffffffL;
4344 else
4345 n1 = n1 / n2;
4346 }
4347 else
4348 {
4349 if (n2 == 0) /* give an error message? */
4350 n1 = 0;
4351 else
4352 n1 = n1 % n2;
4353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 rettv->v_type = VAR_NUMBER;
4355 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 }
4358
4359 return OK;
4360}
4361
4362/*
4363 * Handle sixth level expression:
4364 * number number constant
4365 * "string" string contstant
4366 * 'string' literal string contstant
4367 * &option-name option value
4368 * @r register contents
4369 * identifier variable value
4370 * function() function call
4371 * $VAR environment variable
4372 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004373 * [expr, expr] List
4374 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 *
4376 * Also handle:
4377 * ! in front logical NOT
4378 * - in front unary minus
4379 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 * trailing [] subscript in String or List
4381 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 *
4383 * "arg" must point to the first non-white of the expression.
4384 * "arg" is advanced to the next non-white after the recognized expression.
4385 *
4386 * Return OK or FAIL.
4387 */
4388 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 int evaluate;
4393{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 long n;
4395 int len;
4396 char_u *s;
4397 int val;
4398 char_u *start_leader, *end_leader;
4399 int ret = OK;
4400 char_u *alias;
4401
4402 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
4408 /*
4409 * Skip '!' and '-' characters. They are handled later.
4410 */
4411 start_leader = *arg;
4412 while (**arg == '!' || **arg == '-' || **arg == '+')
4413 *arg = skipwhite(*arg + 1);
4414 end_leader = *arg;
4415
4416 switch (**arg)
4417 {
4418 /*
4419 * Number constant.
4420 */
4421 case '0':
4422 case '1':
4423 case '2':
4424 case '3':
4425 case '4':
4426 case '5':
4427 case '6':
4428 case '7':
4429 case '8':
4430 case '9':
4431 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4432 *arg += len;
4433 if (evaluate)
4434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 rettv->v_type = VAR_NUMBER;
4436 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 break;
4439
4440 /*
4441 * String constant: "string".
4442 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 break;
4445
4446 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004447 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004450 break;
4451
4452 /*
4453 * List: [expr, expr]
4454 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
4457
4458 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 * Dictionary: {key: val, key: val}
4460 */
4461 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4462 break;
4463
4464 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004465 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004467 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 break;
4469
4470 /*
4471 * Environment variable: $VAR.
4472 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 break;
4475
4476 /*
4477 * Register contents: @r.
4478 */
4479 case '@': ++*arg;
4480 if (evaluate)
4481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004483 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 if (**arg != NUL)
4486 ++*arg;
4487 break;
4488
4489 /*
4490 * nested expression: (expression).
4491 */
4492 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004493 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 if (**arg == ')')
4495 ++*arg;
4496 else if (ret == OK)
4497 {
4498 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 ret = FAIL;
4501 }
4502 break;
4503
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507
4508 if (ret == NOTDONE)
4509 {
4510 /*
4511 * Must be a variable or function name.
4512 * Can also be a curly-braces kind of name: {expr}.
4513 */
4514 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004515 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516 if (alias != NULL)
4517 s = alias;
4518
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004519 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 ret = FAIL;
4521 else
4522 {
4523 if (**arg == '(') /* recursive! */
4524 {
4525 /* If "s" is the name of a variable of type VAR_FUNC
4526 * use its contents. */
4527 s = deref_func_name(s, &len);
4528
4529 /* Invoke the function. */
4530 ret = get_func_tv(s, len, rettv, arg,
4531 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004532 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004533 /* Stop the expression evaluation when immediately
4534 * aborting on error, or when an interrupt occurred or
4535 * an exception was thrown but not caught. */
4536 if (aborting())
4537 {
4538 if (ret == OK)
4539 clear_tv(rettv);
4540 ret = FAIL;
4541 }
4542 }
4543 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004544 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004545 else
4546 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 }
4548
4549 if (alias != NULL)
4550 vim_free(alias);
4551 }
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 *arg = skipwhite(*arg);
4554
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004555 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4556 * expr(expr). */
4557 if (ret == OK)
4558 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560 /*
4561 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4562 */
4563 if (ret == OK && evaluate && end_leader > start_leader)
4564 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004565 int error = FALSE;
4566
4567 val = get_tv_number_chk(rettv, &error);
4568 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004570 clear_tv(rettv);
4571 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004573 else
4574 {
4575 while (end_leader > start_leader)
4576 {
4577 --end_leader;
4578 if (*end_leader == '!')
4579 val = !val;
4580 else if (*end_leader == '-')
4581 val = -val;
4582 }
4583 clear_tv(rettv);
4584 rettv->v_type = VAR_NUMBER;
4585 rettv->vval.v_number = val;
4586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588
4589 return ret;
4590}
4591
4592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 * Evaluate an "[expr]" or "[expr:expr]" index.
4594 * "*arg" points to the '['.
4595 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4596 */
4597 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004598eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004600 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004602 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603{
4604 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004605 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004606 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004607 long len = -1;
4608 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004614 if (verbose)
4615 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 return FAIL;
4617 }
4618
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 /*
4622 * dict.name
4623 */
4624 key = *arg + 1;
4625 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4626 ;
4627 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 }
4631 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 /*
4634 * something[idx]
4635 *
4636 * Get the (first) variable from inside the [].
4637 */
4638 *arg = skipwhite(*arg + 1);
4639 if (**arg == ':')
4640 empty1 = TRUE;
4641 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4642 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004643 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4644 {
4645 /* not a number or string */
4646 clear_tv(&var1);
4647 return FAIL;
4648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649
4650 /*
4651 * Get the second variable from inside the [:].
4652 */
4653 if (**arg == ':')
4654 {
4655 range = TRUE;
4656 *arg = skipwhite(*arg + 1);
4657 if (**arg == ']')
4658 empty2 = TRUE;
4659 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004661 if (!empty1)
4662 clear_tv(&var1);
4663 return FAIL;
4664 }
4665 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4666 {
4667 /* not a number or string */
4668 if (!empty1)
4669 clear_tv(&var1);
4670 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 return FAIL;
4672 }
4673 }
4674
4675 /* Check for the ']'. */
4676 if (**arg != ']')
4677 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004678 if (verbose)
4679 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 clear_tv(&var1);
4681 if (range)
4682 clear_tv(&var2);
4683 return FAIL;
4684 }
4685 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 }
4687
4688 if (evaluate)
4689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 n1 = 0;
4691 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 n1 = get_tv_number(&var1);
4694 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 }
4696 if (range)
4697 {
4698 if (empty2)
4699 n2 = -1;
4700 else
4701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 n2 = get_tv_number(&var2);
4703 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 }
4705 }
4706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 {
4709 case VAR_NUMBER:
4710 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004711 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 len = (long)STRLEN(s);
4713 if (range)
4714 {
4715 /* The resulting variable is a substring. If the indexes
4716 * are out of range the result is empty. */
4717 if (n1 < 0)
4718 {
4719 n1 = len + n1;
4720 if (n1 < 0)
4721 n1 = 0;
4722 }
4723 if (n2 < 0)
4724 n2 = len + n2;
4725 else if (n2 >= len)
4726 n2 = len;
4727 if (n1 >= len || n2 < 0 || n1 > n2)
4728 s = NULL;
4729 else
4730 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4731 }
4732 else
4733 {
4734 /* The resulting variable is a string of a single
4735 * character. If the index is too big or negative the
4736 * result is empty. */
4737 if (n1 >= len || n1 < 0)
4738 s = NULL;
4739 else
4740 s = vim_strnsave(s + n1, 1);
4741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004742 clear_tv(rettv);
4743 rettv->v_type = VAR_STRING;
4744 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 break;
4746
4747 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 if (n1 < 0)
4750 n1 = len + n1;
4751 if (!empty1 && (n1 < 0 || n1 >= len))
4752 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004753 if (verbose)
4754 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 return FAIL;
4756 }
4757 if (range)
4758 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004759 list_T *l;
4760 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761
4762 if (n2 < 0)
4763 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004766 if (verbose)
4767 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 return FAIL;
4769 }
4770 l = list_alloc();
4771 if (l == NULL)
4772 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 n1 <= n2; ++n1)
4775 {
4776 if (list_append_tv(l, &item->li_tv) == FAIL)
4777 {
4778 list_free(l);
4779 return FAIL;
4780 }
4781 item = item->li_next;
4782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 clear_tv(rettv);
4784 rettv->v_type = VAR_LIST;
4785 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004786 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 }
4788 else
4789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 clear_tv(rettv);
4793 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004794 }
4795 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796
4797 case VAR_DICT:
4798 if (range)
4799 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004800 if (verbose)
4801 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802 if (len == -1)
4803 clear_tv(&var1);
4804 return FAIL;
4805 }
4806 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004807 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004808
4809 if (len == -1)
4810 {
4811 key = get_tv_string(&var1);
4812 if (*key == NUL)
4813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004814 if (verbose)
4815 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 clear_tv(&var1);
4817 return FAIL;
4818 }
4819 }
4820
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004821 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004823 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004824 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004825 if (len == -1)
4826 clear_tv(&var1);
4827 if (item == NULL)
4828 return FAIL;
4829
4830 copy_tv(&item->di_tv, &var1);
4831 clear_tv(rettv);
4832 *rettv = var1;
4833 }
4834 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835 }
4836 }
4837
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 return OK;
4839}
4840
4841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 * Get an option value.
4843 * "arg" points to the '&' or '+' before the option name.
4844 * "arg" is advanced to character after the option name.
4845 * Return OK or FAIL.
4846 */
4847 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004850 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 int evaluate;
4852{
4853 char_u *option_end;
4854 long numval;
4855 char_u *stringval;
4856 int opt_type;
4857 int c;
4858 int working = (**arg == '+'); /* has("+option") */
4859 int ret = OK;
4860 int opt_flags;
4861
4862 /*
4863 * Isolate the option name and find its value.
4864 */
4865 option_end = find_option_end(arg, &opt_flags);
4866 if (option_end == NULL)
4867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004868 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 EMSG2(_("E112: Option name missing: %s"), *arg);
4870 return FAIL;
4871 }
4872
4873 if (!evaluate)
4874 {
4875 *arg = option_end;
4876 return OK;
4877 }
4878
4879 c = *option_end;
4880 *option_end = NUL;
4881 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 if (opt_type == -3) /* invalid name */
4885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 EMSG2(_("E113: Unknown option: %s"), *arg);
4888 ret = FAIL;
4889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
4892 if (opt_type == -2) /* hidden string option */
4893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004894 rettv->v_type = VAR_STRING;
4895 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 else if (opt_type == -1) /* hidden number option */
4898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 rettv->v_type = VAR_NUMBER;
4900 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 else if (opt_type == 1) /* number option */
4903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 rettv->v_type = VAR_NUMBER;
4905 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 else /* string option */
4908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 rettv->v_type = VAR_STRING;
4910 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 }
4913 else if (working && (opt_type == -2 || opt_type == -1))
4914 ret = FAIL;
4915
4916 *option_end = c; /* put back for error messages */
4917 *arg = option_end;
4918
4919 return ret;
4920}
4921
4922/*
4923 * Allocate a variable for a string constant.
4924 * Return OK or FAIL.
4925 */
4926 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004927get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 int evaluate;
4931{
4932 char_u *p;
4933 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 int extra = 0;
4935
4936 /*
4937 * Find the end of the string, skipping backslashed characters.
4938 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004939 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 if (*p == '\\' && p[1] != NUL)
4942 {
4943 ++p;
4944 /* A "\<x>" form occupies at least 4 characters, and produces up
4945 * to 6 characters: reserve space for 2 extra */
4946 if (*p == '<')
4947 extra += 2;
4948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950
4951 if (*p != '"')
4952 {
4953 EMSG2(_("E114: Missing quote: %s"), *arg);
4954 return FAIL;
4955 }
4956
4957 /* If only parsing, set *arg and return here */
4958 if (!evaluate)
4959 {
4960 *arg = p + 1;
4961 return OK;
4962 }
4963
4964 /*
4965 * Copy the string into allocated memory, handling backslashed
4966 * characters.
4967 */
4968 name = alloc((unsigned)(p - *arg + extra));
4969 if (name == NULL)
4970 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 rettv->v_type = VAR_STRING;
4972 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
4976 if (*p == '\\')
4977 {
4978 switch (*++p)
4979 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 case 'b': *name++ = BS; ++p; break;
4981 case 'e': *name++ = ESC; ++p; break;
4982 case 'f': *name++ = FF; ++p; break;
4983 case 'n': *name++ = NL; ++p; break;
4984 case 'r': *name++ = CAR; ++p; break;
4985 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
4987 case 'X': /* hex: "\x1", "\x12" */
4988 case 'x':
4989 case 'u': /* Unicode: "\u0023" */
4990 case 'U':
4991 if (vim_isxdigit(p[1]))
4992 {
4993 int n, nr;
4994 int c = toupper(*p);
4995
4996 if (c == 'X')
4997 n = 2;
4998 else
4999 n = 4;
5000 nr = 0;
5001 while (--n >= 0 && vim_isxdigit(p[1]))
5002 {
5003 ++p;
5004 nr = (nr << 4) + hex2nr(*p);
5005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#ifdef FEAT_MBYTE
5008 /* For "\u" store the number according to
5009 * 'encoding'. */
5010 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 else
5013#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 break;
5017
5018 /* octal: "\1", "\12", "\123" */
5019 case '0':
5020 case '1':
5021 case '2':
5022 case '3':
5023 case '4':
5024 case '5':
5025 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 case '7': *name = *p++ - '0';
5027 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 *name = (*name << 3) + *p++ - '0';
5030 if (*p >= '0' && *p <= '7')
5031 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 break;
5035
5036 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 if (extra != 0)
5039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 break;
5042 }
5043 /* FALLTHROUGH */
5044
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 break;
5047 }
5048 }
5049 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 *arg = p + 1;
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 return OK;
5057}
5058
5059/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 * Return OK or FAIL.
5062 */
5063 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 int evaluate;
5068{
5069 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005071 int reduce = 0;
5072
5073 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 */
5076 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5077 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 break;
5082 ++reduce;
5083 ++p;
5084 }
5085 }
5086
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005088 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 return FAIL;
5091 }
5092
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005094 if (!evaluate)
5095 {
5096 *arg = p + 1;
5097 return OK;
5098 }
5099
5100 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 */
5103 str = alloc((unsigned)((p - *arg) - reduce));
5104 if (str == NULL)
5105 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 rettv->v_type = VAR_STRING;
5107 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114 break;
5115 ++p;
5116 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 *arg = p + 1;
5121
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122 return OK;
5123}
5124
5125/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126 * Allocate a variable for a List and fill it from "*arg".
5127 * Return OK or FAIL.
5128 */
5129 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005130get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005132 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005133 int evaluate;
5134{
Bram Moolenaar33570922005-01-25 22:26:29 +00005135 list_T *l = NULL;
5136 typval_T tv;
5137 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005138
5139 if (evaluate)
5140 {
5141 l = list_alloc();
5142 if (l == NULL)
5143 return FAIL;
5144 }
5145
5146 *arg = skipwhite(*arg + 1);
5147 while (**arg != ']' && **arg != NUL)
5148 {
5149 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5150 goto failret;
5151 if (evaluate)
5152 {
5153 item = listitem_alloc();
5154 if (item != NULL)
5155 {
5156 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005157 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 list_append(l, item);
5159 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005160 else
5161 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 }
5163
5164 if (**arg == ']')
5165 break;
5166 if (**arg != ',')
5167 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 goto failret;
5170 }
5171 *arg = skipwhite(*arg + 1);
5172 }
5173
5174 if (**arg != ']')
5175 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177failret:
5178 if (evaluate)
5179 list_free(l);
5180 return FAIL;
5181 }
5182
5183 *arg = skipwhite(*arg + 1);
5184 if (evaluate)
5185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 rettv->v_type = VAR_LIST;
5187 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 ++l->lv_refcount;
5189 }
5190
5191 return OK;
5192}
5193
5194/*
5195 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005196 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005198 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199list_alloc()
5200{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005201 list_T *l;
5202
5203 l = (list_T *)alloc_clear(sizeof(list_T));
5204 if (l != NULL)
5205 {
5206 /* Prepend the list to the list of lists for garbage collection. */
5207 if (first_list != NULL)
5208 first_list->lv_used_prev = l;
5209 l->lv_used_prev = NULL;
5210 l->lv_used_next = first_list;
5211 first_list = l;
5212 }
5213 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214}
5215
5216/*
5217 * Unreference a list: decrement the reference count and free it when it
5218 * becomes zero.
5219 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005220 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005222 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005224 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5225 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226}
5227
5228/*
5229 * Free a list, including all items it points to.
5230 * Ignores the reference count.
5231 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005232 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005234 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235{
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237
Bram Moolenaard9fba312005-06-26 22:34:35 +00005238 /* Avoid that recursive reference to the list frees us again. */
5239 l->lv_refcount = DEL_REFCOUNT;
5240
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005241 /* Remove the list from the list of lists for garbage collection. */
5242 if (l->lv_used_prev == NULL)
5243 first_list = l->lv_used_next;
5244 else
5245 l->lv_used_prev->lv_used_next = l->lv_used_next;
5246 if (l->lv_used_next != NULL)
5247 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5248
Bram Moolenaard9fba312005-06-26 22:34:35 +00005249 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005251 /* Remove the item before deleting it. */
5252 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 listitem_free(item);
5254 }
5255 vim_free(l);
5256}
5257
5258/*
5259 * Allocate a list item.
5260 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262listitem_alloc()
5263{
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265}
5266
5267/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005268 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 */
5270 static void
5271listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005272 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 vim_free(item);
5276}
5277
5278/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005279 * Remove a list item from a List and free it. Also clears the value.
5280 */
5281 static void
5282listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005283 list_T *l;
5284 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005285{
5286 list_remove(l, item, item);
5287 listitem_free(item);
5288}
5289
5290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 * Get the number of items in a list.
5292 */
5293 static long
5294list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 if (l == NULL)
5298 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005299 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300}
5301
5302/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005303 * Return TRUE when two lists have exactly the same values.
5304 */
5305 static int
5306list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005307 list_T *l1;
5308 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005309 int ic; /* ignore case for strings */
5310{
Bram Moolenaar33570922005-01-25 22:26:29 +00005311 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005312
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005313 if (list_len(l1) != list_len(l2))
5314 return FALSE;
5315
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005316 for (item1 = l1->lv_first, item2 = l2->lv_first;
5317 item1 != NULL && item2 != NULL;
5318 item1 = item1->li_next, item2 = item2->li_next)
5319 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5320 return FALSE;
5321 return item1 == NULL && item2 == NULL;
5322}
5323
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005324#if defined(FEAT_PYTHON) || defined(PROTO)
5325/*
5326 * Return the dictitem that an entry in a hashtable points to.
5327 */
5328 dictitem_T *
5329dict_lookup(hi)
5330 hashitem_T *hi;
5331{
5332 return HI2DI(hi);
5333}
5334#endif
5335
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005336/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005337 * Return TRUE when two dictionaries have exactly the same key/values.
5338 */
5339 static int
5340dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005341 dict_T *d1;
5342 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005343 int ic; /* ignore case for strings */
5344{
Bram Moolenaar33570922005-01-25 22:26:29 +00005345 hashitem_T *hi;
5346 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005347 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005348
5349 if (dict_len(d1) != dict_len(d2))
5350 return FALSE;
5351
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 todo = d1->dv_hashtab.ht_used;
5353 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005355 if (!HASHITEM_EMPTY(hi))
5356 {
5357 item2 = dict_find(d2, hi->hi_key, -1);
5358 if (item2 == NULL)
5359 return FALSE;
5360 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5361 return FALSE;
5362 --todo;
5363 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005364 }
5365 return TRUE;
5366}
5367
5368/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005369 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005370 * Compares the items just like "==" would compare them, but strings and
5371 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005372 */
5373 static int
5374tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005375 typval_T *tv1;
5376 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005377 int ic; /* ignore case */
5378{
5379 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005380 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005381
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005382 if (tv1->v_type != tv2->v_type)
5383 return FALSE;
5384
5385 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005387 case VAR_LIST:
5388 /* recursive! */
5389 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5390
5391 case VAR_DICT:
5392 /* recursive! */
5393 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5394
5395 case VAR_FUNC:
5396 return (tv1->vval.v_string != NULL
5397 && tv2->vval.v_string != NULL
5398 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5399
5400 case VAR_NUMBER:
5401 return tv1->vval.v_number == tv2->vval.v_number;
5402
5403 case VAR_STRING:
5404 s1 = get_tv_string_buf(tv1, buf1);
5405 s2 = get_tv_string_buf(tv2, buf2);
5406 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005407 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005408
5409 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005410 return TRUE;
5411}
5412
5413/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 * Locate item with index "n" in list "l" and return it.
5415 * A negative index is counted from the end; -1 is the last item.
5416 * Returns NULL when "n" is out of range.
5417 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005418 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 long n;
5422{
Bram Moolenaar33570922005-01-25 22:26:29 +00005423 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 long idx;
5425
5426 if (l == NULL)
5427 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005428
5429 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005431 n = l->lv_len + n;
5432
5433 /* Check for index out of range. */
5434 if (n < 0 || n >= l->lv_len)
5435 return NULL;
5436
5437 /* When there is a cached index may start search from there. */
5438 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005440 if (n < l->lv_idx / 2)
5441 {
5442 /* closest to the start of the list */
5443 item = l->lv_first;
5444 idx = 0;
5445 }
5446 else if (n > (l->lv_idx + l->lv_len) / 2)
5447 {
5448 /* closest to the end of the list */
5449 item = l->lv_last;
5450 idx = l->lv_len - 1;
5451 }
5452 else
5453 {
5454 /* closest to the cached index */
5455 item = l->lv_idx_item;
5456 idx = l->lv_idx;
5457 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 else
5460 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005461 if (n < l->lv_len / 2)
5462 {
5463 /* closest to the start of the list */
5464 item = l->lv_first;
5465 idx = 0;
5466 }
5467 else
5468 {
5469 /* closest to the end of the list */
5470 item = l->lv_last;
5471 idx = l->lv_len - 1;
5472 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005474
5475 while (n > idx)
5476 {
5477 /* search forward */
5478 item = item->li_next;
5479 ++idx;
5480 }
5481 while (n < idx)
5482 {
5483 /* search backward */
5484 item = item->li_prev;
5485 --idx;
5486 }
5487
5488 /* cache the used index */
5489 l->lv_idx = idx;
5490 l->lv_idx_item = item;
5491
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 return item;
5493}
5494
5495/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005496 * Locate "item" list "l" and return its index.
5497 * Returns -1 when "item" is not in the list.
5498 */
5499 static long
5500list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 list_T *l;
5502 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005503{
5504 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005505 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005506
5507 if (l == NULL)
5508 return -1;
5509 idx = 0;
5510 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5511 ++idx;
5512 if (li == NULL)
5513 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005514 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005515}
5516
5517/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 * Append item "item" to the end of list "l".
5519 */
5520 static void
5521list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 list_T *l;
5523 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524{
5525 if (l->lv_last == NULL)
5526 {
5527 /* empty list */
5528 l->lv_first = item;
5529 l->lv_last = item;
5530 item->li_prev = NULL;
5531 }
5532 else
5533 {
5534 l->lv_last->li_next = item;
5535 item->li_prev = l->lv_last;
5536 l->lv_last = item;
5537 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005538 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539 item->li_next = NULL;
5540}
5541
5542/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005543 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005544 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 */
5546 static int
5547list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 list_T *l;
5549 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005551 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552
Bram Moolenaar05159a02005-02-26 23:04:13 +00005553 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005555 copy_tv(tv, &li->li_tv);
5556 list_append(l, li);
5557 return OK;
5558}
5559
5560/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005561 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005562 * Return FAIL when out of memory.
5563 */
5564 int
5565list_append_dict(list, dict)
5566 list_T *list;
5567 dict_T *dict;
5568{
5569 listitem_T *li = listitem_alloc();
5570
5571 if (li == NULL)
5572 return FAIL;
5573 li->li_tv.v_type = VAR_DICT;
5574 li->li_tv.v_lock = 0;
5575 li->li_tv.vval.v_dict = dict;
5576 list_append(list, li);
5577 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 return OK;
5579}
5580
5581/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005582 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005583 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005584 * Returns FAIL when out of memory.
5585 */
5586 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005587list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005588 list_T *l;
5589 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005590 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005591{
5592 listitem_T *li = listitem_alloc();
5593
5594 if (li == NULL)
5595 return FAIL;
5596 list_append(l, li);
5597 li->li_tv.v_type = VAR_STRING;
5598 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005599 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5600 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005601 return FAIL;
5602 return OK;
5603}
5604
5605/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005606 * Append "n" to list "l".
5607 * Returns FAIL when out of memory.
5608 */
5609 static int
5610list_append_number(l, n)
5611 list_T *l;
5612 varnumber_T n;
5613{
5614 listitem_T *li;
5615
5616 li = listitem_alloc();
5617 if (li == NULL)
5618 return FAIL;
5619 li->li_tv.v_type = VAR_NUMBER;
5620 li->li_tv.v_lock = 0;
5621 li->li_tv.vval.v_number = n;
5622 list_append(l, li);
5623 return OK;
5624}
5625
5626/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005628 * If "item" is NULL append at the end.
5629 * Return FAIL when out of memory.
5630 */
5631 static int
5632list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005633 list_T *l;
5634 typval_T *tv;
5635 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005636{
Bram Moolenaar33570922005-01-25 22:26:29 +00005637 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005638
5639 if (ni == NULL)
5640 return FAIL;
5641 copy_tv(tv, &ni->li_tv);
5642 if (item == NULL)
5643 /* Append new item at end of list. */
5644 list_append(l, ni);
5645 else
5646 {
5647 /* Insert new item before existing item. */
5648 ni->li_prev = item->li_prev;
5649 ni->li_next = item;
5650 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005651 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005652 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005653 ++l->lv_idx;
5654 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005655 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005656 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005658 l->lv_idx_item = NULL;
5659 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005660 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005661 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005662 }
5663 return OK;
5664}
5665
5666/*
5667 * Extend "l1" with "l2".
5668 * If "bef" is NULL append at the end, otherwise insert before this item.
5669 * Returns FAIL when out of memory.
5670 */
5671 static int
5672list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005673 list_T *l1;
5674 list_T *l2;
5675 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005676{
Bram Moolenaar33570922005-01-25 22:26:29 +00005677 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005678
5679 for (item = l2->lv_first; item != NULL; item = item->li_next)
5680 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5681 return FAIL;
5682 return OK;
5683}
5684
5685/*
5686 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5687 * Return FAIL when out of memory.
5688 */
5689 static int
5690list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005691 list_T *l1;
5692 list_T *l2;
5693 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005694{
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005696
5697 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005698 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005699 if (l == NULL)
5700 return FAIL;
5701 tv->v_type = VAR_LIST;
5702 tv->vval.v_list = l;
5703
5704 /* append all items from the second list */
5705 return list_extend(l, l2, NULL);
5706}
5707
5708/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005709 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005710 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005711 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 * Returns NULL when out of memory.
5713 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005714 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005715list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005716 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005718 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719{
Bram Moolenaar33570922005-01-25 22:26:29 +00005720 list_T *copy;
5721 listitem_T *item;
5722 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723
5724 if (orig == NULL)
5725 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726
5727 copy = list_alloc();
5728 if (copy != NULL)
5729 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005730 if (copyID != 0)
5731 {
5732 /* Do this before adding the items, because one of the items may
5733 * refer back to this list. */
5734 orig->lv_copyID = copyID;
5735 orig->lv_copylist = copy;
5736 }
5737 for (item = orig->lv_first; item != NULL && !got_int;
5738 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739 {
5740 ni = listitem_alloc();
5741 if (ni == NULL)
5742 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005743 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005744 {
5745 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5746 {
5747 vim_free(ni);
5748 break;
5749 }
5750 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 list_append(copy, ni);
5754 }
5755 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005756 if (item != NULL)
5757 {
5758 list_unref(copy);
5759 copy = NULL;
5760 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 }
5762
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763 return copy;
5764}
5765
5766/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005767 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005768 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005770 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005771list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 list_T *l;
5773 listitem_T *item;
5774 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005775{
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005778 /* notify watchers */
5779 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005781 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005782 list_fix_watch(l, ip);
5783 if (ip == item2)
5784 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005786
5787 if (item2->li_next == NULL)
5788 l->lv_last = item->li_prev;
5789 else
5790 item2->li_next->li_prev = item->li_prev;
5791 if (item->li_prev == NULL)
5792 l->lv_first = item2->li_next;
5793 else
5794 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005795 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796}
5797
5798/*
5799 * Return an allocated string with the string representation of a list.
5800 * May return NULL.
5801 */
5802 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005803list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005804 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005805 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806{
5807 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808
5809 if (tv->vval.v_list == NULL)
5810 return NULL;
5811 ga_init2(&ga, (int)sizeof(char), 80);
5812 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005813 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005814 {
5815 vim_free(ga.ga_data);
5816 return NULL;
5817 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 ga_append(&ga, ']');
5819 ga_append(&ga, NUL);
5820 return (char_u *)ga.ga_data;
5821}
5822
5823/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005824 * Join list "l" into a string in "*gap", using separator "sep".
5825 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005826 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005827 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005828 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005829list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005830 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 char_u *sep;
5833 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005834 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835{
5836 int first = TRUE;
5837 char_u *tofree;
5838 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005839 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 char_u *s;
5841
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005842 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 {
5844 if (first)
5845 first = FALSE;
5846 else
5847 ga_concat(gap, sep);
5848
5849 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005850 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005852 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 if (s != NULL)
5854 ga_concat(gap, s);
5855 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005856 if (s == NULL)
5857 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005859 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860}
5861
5862/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005863 * Garbage collection for lists and dictionaries.
5864 *
5865 * We use reference counts to be able to free most items right away when they
5866 * are no longer used. But for composite items it's possible that it becomes
5867 * unused while the reference count is > 0: When there is a recursive
5868 * reference. Example:
5869 * :let l = [1, 2, 3]
5870 * :let d = {9: l}
5871 * :let l[1] = d
5872 *
5873 * Since this is quite unusual we handle this with garbage collection: every
5874 * once in a while find out which lists and dicts are not referenced from any
5875 * variable.
5876 *
5877 * Here is a good reference text about garbage collection (refers to Python
5878 * but it applies to all reference-counting mechanisms):
5879 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005880 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005881
5882/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005883 * Do garbage collection for lists and dicts.
5884 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005885 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886 int
5887garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005888{
5889 dict_T *dd;
5890 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 int copyID = ++current_copyID;
5892 buf_T *buf;
5893 win_T *wp;
5894 int i;
5895 funccall_T *fc;
5896 int did_free = FALSE;
5897
5898 /*
5899 * 1. Go through all accessible variables and mark all lists and dicts
5900 * with copyID.
5901 */
5902 /* script-local variables */
5903 for (i = 1; i <= ga_scripts.ga_len; ++i)
5904 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5905
5906 /* buffer-local variables */
5907 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5908 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5909
5910 /* window-local variables */
5911 FOR_ALL_WINDOWS(wp)
5912 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5913
5914 /* global variables */
5915 set_ref_in_ht(&globvarht, copyID);
5916
5917 /* function-local variables */
5918 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5919 {
5920 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5921 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5922 }
5923
5924 /*
5925 * 2. Go through the list of dicts and free items without the copyID.
5926 */
5927 for (dd = first_dict; dd != NULL; )
5928 if (dd->dv_copyID != copyID)
5929 {
5930 dict_free(dd);
5931 did_free = TRUE;
5932
5933 /* restart, next dict may also have been freed */
5934 dd = first_dict;
5935 }
5936 else
5937 dd = dd->dv_used_next;
5938
5939 /*
5940 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005941 * But don't free a list that has a watcher (used in a for loop), these
5942 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 */
5944 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005945 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005946 {
5947 list_free(ll);
5948 did_free = TRUE;
5949
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005950 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005951 ll = first_list;
5952 }
5953 else
5954 ll = ll->lv_used_next;
5955
5956 return did_free;
5957}
5958
5959/*
5960 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5961 */
5962 static void
5963set_ref_in_ht(ht, copyID)
5964 hashtab_T *ht;
5965 int copyID;
5966{
5967 int todo;
5968 hashitem_T *hi;
5969
5970 todo = ht->ht_used;
5971 for (hi = ht->ht_array; todo > 0; ++hi)
5972 if (!HASHITEM_EMPTY(hi))
5973 {
5974 --todo;
5975 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5976 }
5977}
5978
5979/*
5980 * Mark all lists and dicts referenced through list "l" with "copyID".
5981 */
5982 static void
5983set_ref_in_list(l, copyID)
5984 list_T *l;
5985 int copyID;
5986{
5987 listitem_T *li;
5988
5989 for (li = l->lv_first; li != NULL; li = li->li_next)
5990 set_ref_in_item(&li->li_tv, copyID);
5991}
5992
5993/*
5994 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5995 */
5996 static void
5997set_ref_in_item(tv, copyID)
5998 typval_T *tv;
5999 int copyID;
6000{
6001 dict_T *dd;
6002 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006003
6004 switch (tv->v_type)
6005 {
6006 case VAR_DICT:
6007 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006008 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006009 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006010 /* Didn't see this dict yet. */
6011 dd->dv_copyID = copyID;
6012 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006013 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006014 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006015
6016 case VAR_LIST:
6017 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006018 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006019 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006020 /* Didn't see this list yet. */
6021 ll->lv_copyID = copyID;
6022 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006023 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006024 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006025 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006027}
6028
6029/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 * Allocate an empty header for a dictionary.
6031 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006032 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006033dict_alloc()
6034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006036
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006038 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006039 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006040 /* Add the list to the hashtable for garbage collection. */
6041 if (first_dict != NULL)
6042 first_dict->dv_used_prev = d;
6043 d->dv_used_next = first_dict;
6044 d->dv_used_prev = NULL;
6045
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006047 d->dv_lock = 0;
6048 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006049 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006051 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006052}
6053
6054/*
6055 * Unreference a Dictionary: decrement the reference count and free it when it
6056 * becomes zero.
6057 */
6058 static void
6059dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006061{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006062 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6063 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006064}
6065
6066/*
6067 * Free a Dictionary, including all items it contains.
6068 * Ignores the reference count.
6069 */
6070 static void
6071dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006073{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006074 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006076 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077
Bram Moolenaard9fba312005-06-26 22:34:35 +00006078 /* Avoid that recursive reference to the dict frees us again. */
6079 d->dv_refcount = DEL_REFCOUNT;
6080
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006081 /* Remove the dict from the list of dicts for garbage collection. */
6082 if (d->dv_used_prev == NULL)
6083 first_dict = d->dv_used_next;
6084 else
6085 d->dv_used_prev->dv_used_next = d->dv_used_next;
6086 if (d->dv_used_next != NULL)
6087 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6088
6089 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006090 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 todo = d->dv_hashtab.ht_used;
6092 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006093 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006094 if (!HASHITEM_EMPTY(hi))
6095 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006096 /* Remove the item before deleting it, just in case there is
6097 * something recursive causing trouble. */
6098 di = HI2DI(hi);
6099 hash_remove(&d->dv_hashtab, hi);
6100 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101 --todo;
6102 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006103 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006105 vim_free(d);
6106}
6107
6108/*
6109 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006110 * The "key" is copied to the new item.
6111 * Note that the value of the item "di_tv" still needs to be initialized!
6112 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006113 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006114 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006115dictitem_alloc(key)
6116 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006117{
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006119
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006121 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006122 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006124 di->di_flags = 0;
6125 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006126 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006127}
6128
6129/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006130 * Make a copy of a Dictionary item.
6131 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006133dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006135{
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006137
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006139 if (di != NULL)
6140 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006141 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006142 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006143 copy_tv(&org->di_tv, &di->di_tv);
6144 }
6145 return di;
6146}
6147
6148/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006149 * Remove item "item" from Dictionary "dict" and free it.
6150 */
6151 static void
6152dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 dict_T *dict;
6154 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155{
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006159 if (HASHITEM_EMPTY(hi))
6160 EMSG2(_(e_intern2), "dictitem_remove()");
6161 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006163 dictitem_free(item);
6164}
6165
6166/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006167 * Free a dict item. Also clears the value.
6168 */
6169 static void
6170dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006172{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173 clear_tv(&item->di_tv);
6174 vim_free(item);
6175}
6176
6177/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006178 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6179 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006180 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006181 * Returns NULL when out of memory.
6182 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006183 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006184dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006185 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006187 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006188{
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 dict_T *copy;
6190 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006191 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006193
6194 if (orig == NULL)
6195 return NULL;
6196
6197 copy = dict_alloc();
6198 if (copy != NULL)
6199 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006200 if (copyID != 0)
6201 {
6202 orig->dv_copyID = copyID;
6203 orig->dv_copydict = copy;
6204 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006206 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006208 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006210 --todo;
6211
6212 di = dictitem_alloc(hi->hi_key);
6213 if (di == NULL)
6214 break;
6215 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006216 {
6217 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6218 copyID) == FAIL)
6219 {
6220 vim_free(di);
6221 break;
6222 }
6223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224 else
6225 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6226 if (dict_add(copy, di) == FAIL)
6227 {
6228 dictitem_free(di);
6229 break;
6230 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006232 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006233
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006235 if (todo > 0)
6236 {
6237 dict_unref(copy);
6238 copy = NULL;
6239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240 }
6241
6242 return copy;
6243}
6244
6245/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006246 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006247 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006248 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006250dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 dict_T *d;
6252 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006253{
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255}
6256
Bram Moolenaar8c711452005-01-14 21:53:12 +00006257/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006258 * Add a number or string entry to dictionary "d".
6259 * When "str" is NULL use number "nr", otherwise use "str".
6260 * Returns FAIL when out of memory and when key already exists.
6261 */
6262 int
6263dict_add_nr_str(d, key, nr, str)
6264 dict_T *d;
6265 char *key;
6266 long nr;
6267 char_u *str;
6268{
6269 dictitem_T *item;
6270
6271 item = dictitem_alloc((char_u *)key);
6272 if (item == NULL)
6273 return FAIL;
6274 item->di_tv.v_lock = 0;
6275 if (str == NULL)
6276 {
6277 item->di_tv.v_type = VAR_NUMBER;
6278 item->di_tv.vval.v_number = nr;
6279 }
6280 else
6281 {
6282 item->di_tv.v_type = VAR_STRING;
6283 item->di_tv.vval.v_string = vim_strsave(str);
6284 }
6285 if (dict_add(d, item) == FAIL)
6286 {
6287 dictitem_free(item);
6288 return FAIL;
6289 }
6290 return OK;
6291}
6292
6293/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294 * Get the number of items in a Dictionary.
6295 */
6296 static long
6297dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006298 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006299{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300 if (d == NULL)
6301 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006302 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303}
6304
6305/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006306 * Find item "key[len]" in Dictionary "d".
6307 * If "len" is negative use strlen(key).
6308 * Returns NULL when not found.
6309 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006311dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313 char_u *key;
6314 int len;
6315{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006316#define AKEYLEN 200
6317 char_u buf[AKEYLEN];
6318 char_u *akey;
6319 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322 if (len < 0)
6323 akey = key;
6324 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006325 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006326 tofree = akey = vim_strnsave(key, len);
6327 if (akey == NULL)
6328 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006329 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006330 else
6331 {
6332 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006333 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334 akey = buf;
6335 }
6336
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006338 vim_free(tofree);
6339 if (HASHITEM_EMPTY(hi))
6340 return NULL;
6341 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342}
6343
6344/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006345 * Get a string item from a dictionary.
6346 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006347 * Returns NULL if the entry doesn't exist or out of memory.
6348 */
6349 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006350get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006351 dict_T *d;
6352 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006353 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006354{
6355 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006356 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006357
6358 di = dict_find(d, key, -1);
6359 if (di == NULL)
6360 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006361 s = get_tv_string(&di->di_tv);
6362 if (save && s != NULL)
6363 s = vim_strsave(s);
6364 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006365}
6366
6367/*
6368 * Get a number item from a dictionary.
6369 * Returns 0 if the entry doesn't exist or out of memory.
6370 */
6371 long
6372get_dict_number(d, key)
6373 dict_T *d;
6374 char_u *key;
6375{
6376 dictitem_T *di;
6377
6378 di = dict_find(d, key, -1);
6379 if (di == NULL)
6380 return 0;
6381 return get_tv_number(&di->di_tv);
6382}
6383
6384/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006385 * Return an allocated string with the string representation of a Dictionary.
6386 * May return NULL.
6387 */
6388 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006389dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006391 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006392{
6393 garray_T ga;
6394 int first = TRUE;
6395 char_u *tofree;
6396 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006400 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006402 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006403 return NULL;
6404 ga_init2(&ga, (int)sizeof(char), 80);
6405 ga_append(&ga, '{');
6406
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006408 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006411 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412 --todo;
6413
6414 if (first)
6415 first = FALSE;
6416 else
6417 ga_concat(&ga, (char_u *)", ");
6418
6419 tofree = string_quote(hi->hi_key, FALSE);
6420 if (tofree != NULL)
6421 {
6422 ga_concat(&ga, tofree);
6423 vim_free(tofree);
6424 }
6425 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006426 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006427 if (s != NULL)
6428 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006430 if (s == NULL)
6431 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006432 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006433 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434 if (todo > 0)
6435 {
6436 vim_free(ga.ga_data);
6437 return NULL;
6438 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006439
6440 ga_append(&ga, '}');
6441 ga_append(&ga, NUL);
6442 return (char_u *)ga.ga_data;
6443}
6444
6445/*
6446 * Allocate a variable for a Dictionary and fill it from "*arg".
6447 * Return OK or FAIL. Returns NOTDONE for {expr}.
6448 */
6449 static int
6450get_dict_tv(arg, rettv, evaluate)
6451 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006452 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006453 int evaluate;
6454{
Bram Moolenaar33570922005-01-25 22:26:29 +00006455 dict_T *d = NULL;
6456 typval_T tvkey;
6457 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006460 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006461 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006462
6463 /*
6464 * First check if it's not a curly-braces thing: {expr}.
6465 * Must do this without evaluating, otherwise a function may be called
6466 * twice. Unfortunately this means we need to call eval1() twice for the
6467 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006469 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006470 if (*start != '}')
6471 {
6472 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6473 return FAIL;
6474 if (*start == '}')
6475 return NOTDONE;
6476 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006477
6478 if (evaluate)
6479 {
6480 d = dict_alloc();
6481 if (d == NULL)
6482 return FAIL;
6483 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006484 tvkey.v_type = VAR_UNKNOWN;
6485 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006486
6487 *arg = skipwhite(*arg + 1);
6488 while (**arg != '}' && **arg != NUL)
6489 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491 goto failret;
6492 if (**arg != ':')
6493 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006494 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006495 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006496 goto failret;
6497 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006498 key = get_tv_string_buf_chk(&tvkey, buf);
6499 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006501 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6502 if (key != NULL)
6503 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006504 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505 goto failret;
6506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507
6508 *arg = skipwhite(*arg + 1);
6509 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006511 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512 goto failret;
6513 }
6514 if (evaluate)
6515 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006516 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006517 if (item != NULL)
6518 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006519 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006520 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006521 clear_tv(&tv);
6522 goto failret;
6523 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006524 item = dictitem_alloc(key);
6525 clear_tv(&tvkey);
6526 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006527 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006528 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006529 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006530 if (dict_add(d, item) == FAIL)
6531 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006532 }
6533 }
6534
6535 if (**arg == '}')
6536 break;
6537 if (**arg != ',')
6538 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006539 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006540 goto failret;
6541 }
6542 *arg = skipwhite(*arg + 1);
6543 }
6544
6545 if (**arg != '}')
6546 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006547 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006548failret:
6549 if (evaluate)
6550 dict_free(d);
6551 return FAIL;
6552 }
6553
6554 *arg = skipwhite(*arg + 1);
6555 if (evaluate)
6556 {
6557 rettv->v_type = VAR_DICT;
6558 rettv->vval.v_dict = d;
6559 ++d->dv_refcount;
6560 }
6561
6562 return OK;
6563}
6564
Bram Moolenaar8c711452005-01-14 21:53:12 +00006565/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566 * Return a string with the string representation of a variable.
6567 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006569 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006570 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571 * May return NULL;
6572 */
6573 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006574echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006578 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580 static int recurse = 0;
6581 char_u *r = NULL;
6582
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006585 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006586 *tofree = NULL;
6587 return NULL;
6588 }
6589 ++recurse;
6590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 switch (tv->v_type)
6592 {
6593 case VAR_FUNC:
6594 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006595 r = tv->vval.v_string;
6596 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006597
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006599 if (tv->vval.v_list == NULL)
6600 {
6601 *tofree = NULL;
6602 r = NULL;
6603 }
6604 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6605 {
6606 *tofree = NULL;
6607 r = (char_u *)"[...]";
6608 }
6609 else
6610 {
6611 tv->vval.v_list->lv_copyID = copyID;
6612 *tofree = list2string(tv, copyID);
6613 r = *tofree;
6614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006616
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006618 if (tv->vval.v_dict == NULL)
6619 {
6620 *tofree = NULL;
6621 r = NULL;
6622 }
6623 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6624 {
6625 *tofree = NULL;
6626 r = (char_u *)"{...}";
6627 }
6628 else
6629 {
6630 tv->vval.v_dict->dv_copyID = copyID;
6631 *tofree = dict2string(tv, copyID);
6632 r = *tofree;
6633 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006634 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006636 case VAR_STRING:
6637 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006638 *tofree = NULL;
6639 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006641
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006642 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006643 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006644 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006646
6647 --recurse;
6648 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006649}
6650
6651/*
6652 * Return a string with the string representation of a variable.
6653 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6654 * "numbuf" is used for a number.
6655 * Puts quotes around strings, so that they can be parsed back by eval().
6656 * May return NULL;
6657 */
6658 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006659tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006660 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006661 char_u **tofree;
6662 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006663 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006664{
6665 switch (tv->v_type)
6666 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006667 case VAR_FUNC:
6668 *tofree = string_quote(tv->vval.v_string, TRUE);
6669 return *tofree;
6670 case VAR_STRING:
6671 *tofree = string_quote(tv->vval.v_string, FALSE);
6672 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006673 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006675 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006676 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006677 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006678 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006680 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681}
6682
6683/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 * Return string "str" in ' quotes, doubling ' characters.
6685 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687 */
6688 static char_u *
6689string_quote(str, function)
6690 char_u *str;
6691 int function;
6692{
Bram Moolenaar33570922005-01-25 22:26:29 +00006693 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006694 char_u *p, *r, *s;
6695
Bram Moolenaar33570922005-01-25 22:26:29 +00006696 len = (function ? 13 : 3);
6697 if (str != NULL)
6698 {
6699 len += STRLEN(str);
6700 for (p = str; *p != NUL; mb_ptr_adv(p))
6701 if (*p == '\'')
6702 ++len;
6703 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704 s = r = alloc(len);
6705 if (r != NULL)
6706 {
6707 if (function)
6708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006709 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 r += 10;
6711 }
6712 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006714 if (str != NULL)
6715 for (p = str; *p != NUL; )
6716 {
6717 if (*p == '\'')
6718 *r++ = '\'';
6719 MB_COPY_CHAR(p, r);
6720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006721 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722 if (function)
6723 *r++ = ')';
6724 *r++ = NUL;
6725 }
6726 return s;
6727}
6728
6729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 * Get the value of an environment variable.
6731 * "arg" is pointing to the '$'. It is advanced to after the name.
6732 * If the environment variable was not set, silently assume it is empty.
6733 * Always return OK.
6734 */
6735 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006736get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 int evaluate;
6740{
6741 char_u *string = NULL;
6742 int len;
6743 int cc;
6744 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006745 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746
6747 ++*arg;
6748 name = *arg;
6749 len = get_env_len(arg);
6750 if (evaluate)
6751 {
6752 if (len != 0)
6753 {
6754 cc = name[len];
6755 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006756 /* first try vim_getenv(), fast for normal environment vars */
6757 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006759 {
6760 if (!mustfree)
6761 string = vim_strsave(string);
6762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 else
6764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006765 if (mustfree)
6766 vim_free(string);
6767
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 /* next try expanding things like $VIM and ${HOME} */
6769 string = expand_env_save(name - 1);
6770 if (string != NULL && *string == '$')
6771 {
6772 vim_free(string);
6773 string = NULL;
6774 }
6775 }
6776 name[len] = cc;
6777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006778 rettv->v_type = VAR_STRING;
6779 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 }
6781
6782 return OK;
6783}
6784
6785/*
6786 * Array with names and number of arguments of all internal functions
6787 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6788 */
6789static struct fst
6790{
6791 char *f_name; /* function name */
6792 char f_min_argc; /* minimal number of arguments */
6793 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006795 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796} functions[] =
6797{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006798 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {"append", 2, 2, f_append},
6800 {"argc", 0, 0, f_argc},
6801 {"argidx", 0, 0, f_argidx},
6802 {"argv", 1, 1, f_argv},
6803 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006804 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {"bufexists", 1, 1, f_bufexists},
6806 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6807 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6808 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6809 {"buflisted", 1, 1, f_buflisted},
6810 {"bufloaded", 1, 1, f_bufloaded},
6811 {"bufname", 1, 1, f_bufname},
6812 {"bufnr", 1, 1, f_bufnr},
6813 {"bufwinnr", 1, 1, f_bufwinnr},
6814 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006815 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006816 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 {"char2nr", 1, 1, f_char2nr},
6818 {"cindent", 1, 1, f_cindent},
6819 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006820#if defined(FEAT_INS_EXPAND)
6821 {"complete_add", 1, 1, f_complete_add},
6822 {"complete_check", 0, 0, f_complete_check},
6823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006826 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {"cscope_connection",0,3, f_cscope_connection},
6828 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006829 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 {"delete", 1, 1, f_delete},
6831 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006832 {"diff_filler", 1, 1, f_diff_filler},
6833 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006834 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006836 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 {"eventhandler", 0, 0, f_eventhandler},
6838 {"executable", 1, 1, f_executable},
6839 {"exists", 1, 1, f_exists},
6840 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006841 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6843 {"filereadable", 1, 1, f_filereadable},
6844 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006845 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006846 {"finddir", 1, 3, f_finddir},
6847 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {"fnamemodify", 2, 2, f_fnamemodify},
6849 {"foldclosed", 1, 1, f_foldclosed},
6850 {"foldclosedend", 1, 1, f_foldclosedend},
6851 {"foldlevel", 1, 1, f_foldlevel},
6852 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006853 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006855 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006857 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006858 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {"getbufvar", 2, 2, f_getbufvar},
6860 {"getchar", 0, 1, f_getchar},
6861 {"getcharmod", 0, 0, f_getcharmod},
6862 {"getcmdline", 0, 0, f_getcmdline},
6863 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006864 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006866 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006867 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 {"getfsize", 1, 1, f_getfsize},
6869 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006870 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006871 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006872 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006873 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006874 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {"getregtype", 0, 1, f_getregtype},
6876 {"getwinposx", 0, 0, f_getwinposx},
6877 {"getwinposy", 0, 0, f_getwinposy},
6878 {"getwinvar", 2, 2, f_getwinvar},
6879 {"glob", 1, 1, f_glob},
6880 {"globpath", 2, 2, f_globpath},
6881 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006882 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {"hasmapto", 1, 2, f_hasmapto},
6884 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6885 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6886 {"histadd", 2, 2, f_histadd},
6887 {"histdel", 1, 2, f_histdel},
6888 {"histget", 1, 2, f_histget},
6889 {"histnr", 1, 1, f_histnr},
6890 {"hlID", 1, 1, f_hlID},
6891 {"hlexists", 1, 1, f_hlexists},
6892 {"hostname", 0, 0, f_hostname},
6893 {"iconv", 3, 3, f_iconv},
6894 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006895 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006896 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006898 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {"inputrestore", 0, 0, f_inputrestore},
6900 {"inputsave", 0, 0, f_inputsave},
6901 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006902 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006904 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006905 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006906 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006907 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006909 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 {"libcall", 3, 3, f_libcall},
6911 {"libcallnr", 3, 3, f_libcallnr},
6912 {"line", 1, 1, f_line},
6913 {"line2byte", 1, 1, f_line2byte},
6914 {"lispindent", 1, 1, f_lispindent},
6915 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006916 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {"maparg", 1, 2, f_maparg},
6918 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006919 {"match", 2, 4, f_match},
6920 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006921 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006922 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006923 {"max", 1, 1, f_max},
6924 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006925#ifdef vim_mkdir
6926 {"mkdir", 1, 3, f_mkdir},
6927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {"mode", 0, 0, f_mode},
6929 {"nextnonblank", 1, 1, f_nextnonblank},
6930 {"nr2char", 1, 1, f_nr2char},
6931 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006932 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006933 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006934 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 {"remote_expr", 2, 3, f_remote_expr},
6936 {"remote_foreground", 1, 1, f_remote_foreground},
6937 {"remote_peek", 1, 2, f_remote_peek},
6938 {"remote_read", 1, 1, f_remote_read},
6939 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006940 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006942 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006944 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006946 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {"searchpair", 3, 5, f_searchpair},
6948 {"server2client", 2, 2, f_server2client},
6949 {"serverlist", 0, 0, f_serverlist},
6950 {"setbufvar", 3, 3, f_setbufvar},
6951 {"setcmdpos", 1, 1, f_setcmdpos},
6952 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006953 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006954 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {"setreg", 2, 3, f_setreg},
6956 {"setwinvar", 3, 3, f_setwinvar},
6957 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006958 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006959 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006960 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006961 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006962 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963#ifdef HAVE_STRFTIME
6964 {"strftime", 1, 2, f_strftime},
6965#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006967 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {"strlen", 1, 1, f_strlen},
6969 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006970 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {"strtrans", 1, 1, f_strtrans},
6972 {"submatch", 1, 1, f_submatch},
6973 {"substitute", 4, 4, f_substitute},
6974 {"synID", 3, 3, f_synID},
6975 {"synIDattr", 2, 3, f_synIDattr},
6976 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006977 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006978 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006979 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006981 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {"tolower", 1, 1, f_tolower},
6983 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006984 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006986 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 {"virtcol", 1, 1, f_virtcol},
6988 {"visualmode", 0, 1, f_visualmode},
6989 {"winbufnr", 1, 1, f_winbufnr},
6990 {"wincol", 0, 0, f_wincol},
6991 {"winheight", 1, 1, f_winheight},
6992 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006993 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 {"winrestcmd", 0, 0, f_winrestcmd},
6995 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006996 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997};
6998
6999#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7000
7001/*
7002 * Function given to ExpandGeneric() to obtain the list of internal
7003 * or user defined function names.
7004 */
7005 char_u *
7006get_function_name(xp, idx)
7007 expand_T *xp;
7008 int idx;
7009{
7010 static int intidx = -1;
7011 char_u *name;
7012
7013 if (idx == 0)
7014 intidx = -1;
7015 if (intidx < 0)
7016 {
7017 name = get_user_func_name(xp, idx);
7018 if (name != NULL)
7019 return name;
7020 }
7021 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7022 {
7023 STRCPY(IObuff, functions[intidx].f_name);
7024 STRCAT(IObuff, "(");
7025 if (functions[intidx].f_max_argc == 0)
7026 STRCAT(IObuff, ")");
7027 return IObuff;
7028 }
7029
7030 return NULL;
7031}
7032
7033/*
7034 * Function given to ExpandGeneric() to obtain the list of internal or
7035 * user defined variable or function names.
7036 */
7037/*ARGSUSED*/
7038 char_u *
7039get_expr_name(xp, idx)
7040 expand_T *xp;
7041 int idx;
7042{
7043 static int intidx = -1;
7044 char_u *name;
7045
7046 if (idx == 0)
7047 intidx = -1;
7048 if (intidx < 0)
7049 {
7050 name = get_function_name(xp, idx);
7051 if (name != NULL)
7052 return name;
7053 }
7054 return get_user_var_name(xp, ++intidx);
7055}
7056
7057#endif /* FEAT_CMDL_COMPL */
7058
7059/*
7060 * Find internal function in table above.
7061 * Return index, or -1 if not found
7062 */
7063 static int
7064find_internal_func(name)
7065 char_u *name; /* name of the function */
7066{
7067 int first = 0;
7068 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7069 int cmp;
7070 int x;
7071
7072 /*
7073 * Find the function name in the table. Binary search.
7074 */
7075 while (first <= last)
7076 {
7077 x = first + ((unsigned)(last - first) >> 1);
7078 cmp = STRCMP(name, functions[x].f_name);
7079 if (cmp < 0)
7080 last = x - 1;
7081 else if (cmp > 0)
7082 first = x + 1;
7083 else
7084 return x;
7085 }
7086 return -1;
7087}
7088
7089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007090 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7091 * name it contains, otherwise return "name".
7092 */
7093 static char_u *
7094deref_func_name(name, lenp)
7095 char_u *name;
7096 int *lenp;
7097{
Bram Moolenaar33570922005-01-25 22:26:29 +00007098 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007099 int cc;
7100
7101 cc = name[*lenp];
7102 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007103 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007104 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007106 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007108 {
7109 *lenp = 0;
7110 return (char_u *)""; /* just in case */
7111 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007112 *lenp = STRLEN(v->di_tv.vval.v_string);
7113 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 }
7115
7116 return name;
7117}
7118
7119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 * Allocate a variable for the result of a function.
7121 * Return OK or FAIL.
7122 */
7123 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007124get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7125 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 char_u *name; /* name of the function */
7127 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 char_u **arg; /* argument, pointing to the '(' */
7130 linenr_T firstline; /* first line of range */
7131 linenr_T lastline; /* last line of range */
7132 int *doesrange; /* return: function handled range */
7133 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
7136 char_u *argp;
7137 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007138 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 int argcount = 0; /* number of arguments found */
7140
7141 /*
7142 * Get the arguments.
7143 */
7144 argp = *arg;
7145 while (argcount < MAX_FUNC_ARGS)
7146 {
7147 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7148 if (*argp == ')' || *argp == ',' || *argp == NUL)
7149 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7151 {
7152 ret = FAIL;
7153 break;
7154 }
7155 ++argcount;
7156 if (*argp != ',')
7157 break;
7158 }
7159 if (*argp == ')')
7160 ++argp;
7161 else
7162 ret = FAIL;
7163
7164 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007165 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007166 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 {
7169 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007170 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
7175 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007176 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
7178 *arg = skipwhite(argp);
7179 return ret;
7180}
7181
7182
7183/*
7184 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007185 * Return OK when the function can't be called, FAIL otherwise.
7186 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 */
7188 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007189call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 char_u *name; /* name of the function */
7192 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 linenr_T firstline; /* first line of range */
7197 linenr_T lastline; /* last line of range */
7198 int *doesrange; /* return: function handled range */
7199 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
7202 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203#define ERROR_UNKNOWN 0
7204#define ERROR_TOOMANY 1
7205#define ERROR_TOOFEW 2
7206#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007207#define ERROR_DICT 4
7208#define ERROR_NONE 5
7209#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 int error = ERROR_NONE;
7211 int i;
7212 int llen;
7213 ufunc_T *fp;
7214 int cc;
7215#define FLEN_FIXED 40
7216 char_u fname_buf[FLEN_FIXED + 1];
7217 char_u *fname;
7218
7219 /*
7220 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7221 * Change <SNR>123_name() to K_SNR 123_name().
7222 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7223 */
7224 cc = name[len];
7225 name[len] = NUL;
7226 llen = eval_fname_script(name);
7227 if (llen > 0)
7228 {
7229 fname_buf[0] = K_SPECIAL;
7230 fname_buf[1] = KS_EXTRA;
7231 fname_buf[2] = (int)KE_SNR;
7232 i = 3;
7233 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7234 {
7235 if (current_SID <= 0)
7236 error = ERROR_SCRIPT;
7237 else
7238 {
7239 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7240 i = (int)STRLEN(fname_buf);
7241 }
7242 }
7243 if (i + STRLEN(name + llen) < FLEN_FIXED)
7244 {
7245 STRCPY(fname_buf + i, name + llen);
7246 fname = fname_buf;
7247 }
7248 else
7249 {
7250 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7251 if (fname == NULL)
7252 error = ERROR_OTHER;
7253 else
7254 {
7255 mch_memmove(fname, fname_buf, (size_t)i);
7256 STRCPY(fname + i, name + llen);
7257 }
7258 }
7259 }
7260 else
7261 fname = name;
7262
7263 *doesrange = FALSE;
7264
7265
7266 /* execute the function if no errors detected and executing */
7267 if (evaluate && error == ERROR_NONE)
7268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007269 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 error = ERROR_UNKNOWN;
7271
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007272 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
7274 /*
7275 * User defined function.
7276 */
7277 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007278
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007280 /* Trigger FuncUndefined event, may load the function. */
7281 if (fp == NULL
7282 && apply_autocmds(EVENT_FUNCUNDEFINED,
7283 fname, fname, TRUE, NULL)
7284 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007286 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 fp = find_func(fname);
7288 }
7289#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007290 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007291 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007292 {
7293 /* loaded a package, search for the function again */
7294 fp = find_func(fname);
7295 }
7296
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 if (fp != NULL)
7298 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007299 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007301 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007303 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007305 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 else
7308 {
7309 /*
7310 * Call the user function.
7311 * Save and restore search patterns, script variables and
7312 * redo buffer.
7313 */
7314 save_search_patterns();
7315 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007316 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007317 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007319 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7320 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7321 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007322 /* Function was unreferenced while being used, free it
7323 * now. */
7324 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 restoreRedobuff();
7326 restore_search_patterns();
7327 error = ERROR_NONE;
7328 }
7329 }
7330 }
7331 else
7332 {
7333 /*
7334 * Find the function name in the table, call its implementation.
7335 */
7336 i = find_internal_func(fname);
7337 if (i >= 0)
7338 {
7339 if (argcount < functions[i].f_min_argc)
7340 error = ERROR_TOOFEW;
7341 else if (argcount > functions[i].f_max_argc)
7342 error = ERROR_TOOMANY;
7343 else
7344 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 error = ERROR_NONE;
7348 }
7349 }
7350 }
7351 /*
7352 * The function call (or "FuncUndefined" autocommand sequence) might
7353 * have been aborted by an error, an interrupt, or an explicitly thrown
7354 * exception that has not been caught so far. This situation can be
7355 * tested for by calling aborting(). For an error in an internal
7356 * function or for the "E132" error in call_user_func(), however, the
7357 * throw point at which the "force_abort" flag (temporarily reset by
7358 * emsg()) is normally updated has not been reached yet. We need to
7359 * update that flag first to make aborting() reliable.
7360 */
7361 update_force_abort();
7362 }
7363 if (error == ERROR_NONE)
7364 ret = OK;
7365
7366 /*
7367 * Report an error unless the argument evaluation or function call has been
7368 * cancelled due to an aborting error, an interrupt, or an exception.
7369 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370 if (!aborting())
7371 {
7372 switch (error)
7373 {
7374 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007375 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376 break;
7377 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379 break;
7380 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 name);
7383 break;
7384 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 name);
7387 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 name);
7391 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392 }
7393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394
7395 name[len] = cc;
7396 if (fname != name && fname != fname_buf)
7397 vim_free(fname);
7398
7399 return ret;
7400}
7401
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007402/*
7403 * Give an error message with a function name. Handle <SNR> things.
7404 */
7405 static void
7406emsg_funcname(msg, name)
7407 char *msg;
7408 char_u *name;
7409{
7410 char_u *p;
7411
7412 if (*name == K_SPECIAL)
7413 p = concat_str((char_u *)"<SNR>", name + 3);
7414 else
7415 p = name;
7416 EMSG2(_(msg), p);
7417 if (p != name)
7418 vim_free(p);
7419}
7420
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421/*********************************************
7422 * Implementation of the built-in functions
7423 */
7424
7425/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007426 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 */
7428 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007429f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 typval_T *argvars;
7431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432{
Bram Moolenaar33570922005-01-25 22:26:29 +00007433 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007435 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007436 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007438 if ((l = argvars[0].vval.v_list) != NULL
7439 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7440 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007442 }
7443 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007444 EMSG(_(e_listreq));
7445}
7446
7447/*
7448 * "append(lnum, string/list)" function
7449 */
7450 static void
7451f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 typval_T *argvars;
7453 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007454{
7455 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007456 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 list_T *l = NULL;
7458 listitem_T *li = NULL;
7459 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007460 long added = 0;
7461
Bram Moolenaar0d660222005-01-07 21:51:51 +00007462 lnum = get_tv_lnum(argvars);
7463 if (lnum >= 0
7464 && lnum <= curbuf->b_ml.ml_line_count
7465 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007466 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007467 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007468 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007469 l = argvars[1].vval.v_list;
7470 if (l == NULL)
7471 return;
7472 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007474 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007475 for (;;)
7476 {
7477 if (l == NULL)
7478 tv = &argvars[1]; /* append a string */
7479 else if (li == NULL)
7480 break; /* end of list */
7481 else
7482 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007483 line = get_tv_string_chk(tv);
7484 if (line == NULL) /* type error */
7485 {
7486 rettv->vval.v_number = 1; /* Failed */
7487 break;
7488 }
7489 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007490 ++added;
7491 if (l == NULL)
7492 break;
7493 li = li->li_next;
7494 }
7495
7496 appended_lines_mark(lnum, added);
7497 if (curwin->w_cursor.lnum > lnum)
7498 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007500 else
7501 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502}
7503
7504/*
7505 * "argc()" function
7506 */
7507/* ARGSUSED */
7508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 typval_T *argvars;
7511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007513 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514}
7515
7516/*
7517 * "argidx()" function
7518 */
7519/* ARGSUSED */
7520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007521f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 typval_T *argvars;
7523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007525 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526}
7527
7528/*
7529 * "argv(nr)" function
7530 */
7531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007532f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007533 typval_T *argvars;
7534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535{
7536 int idx;
7537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007538 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007540 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007542 rettv->vval.v_string = NULL;
7543 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544}
7545
7546/*
7547 * "browse(save, title, initdir, default)" function
7548 */
7549/* ARGSUSED */
7550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 typval_T *argvars;
7553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554{
7555#ifdef FEAT_BROWSE
7556 int save;
7557 char_u *title;
7558 char_u *initdir;
7559 char_u *defname;
7560 char_u buf[NUMBUFLEN];
7561 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007562 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007564 save = get_tv_number_chk(&argvars[0], &error);
7565 title = get_tv_string_chk(&argvars[1]);
7566 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7567 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007569 if (error || title == NULL || initdir == NULL || defname == NULL)
7570 rettv->vval.v_string = NULL;
7571 else
7572 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007573 do_browse(save ? BROWSE_SAVE : 0,
7574 title, defname, NULL, initdir, NULL, curbuf);
7575#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007577#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007578 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007579}
7580
7581/*
7582 * "browsedir(title, initdir)" function
7583 */
7584/* ARGSUSED */
7585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 typval_T *argvars;
7588 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007589{
7590#ifdef FEAT_BROWSE
7591 char_u *title;
7592 char_u *initdir;
7593 char_u buf[NUMBUFLEN];
7594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007595 title = get_tv_string_chk(&argvars[0]);
7596 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007598 if (title == NULL || initdir == NULL)
7599 rettv->vval.v_string = NULL;
7600 else
7601 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007602 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007606 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607}
7608
Bram Moolenaar33570922005-01-25 22:26:29 +00007609static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007610
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611/*
7612 * Find a buffer by number or exact name.
7613 */
7614 static buf_T *
7615find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007620 if (avar->v_type == VAR_NUMBER)
7621 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007622 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007625 if (buf == NULL)
7626 {
7627 /* No full path name match, try a match with a URL or a "nofile"
7628 * buffer, these don't use the full path. */
7629 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7630 if (buf->b_fname != NULL
7631 && (path_with_url(buf->b_fname)
7632#ifdef FEAT_QUICKFIX
7633 || bt_nofile(buf)
7634#endif
7635 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007636 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007637 break;
7638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
7640 return buf;
7641}
7642
7643/*
7644 * "bufexists(expr)" function
7645 */
7646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 typval_T *argvars;
7649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652}
7653
7654/*
7655 * "buflisted(expr)" function
7656 */
7657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007659 typval_T *argvars;
7660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
7662 buf_T *buf;
7663
7664 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666}
7667
7668/*
7669 * "bufloaded(expr)" function
7670 */
7671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007672f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 typval_T *argvars;
7674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 buf_T *buf;
7677
7678 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680}
7681
Bram Moolenaar33570922005-01-25 22:26:29 +00007682static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684/*
7685 * Get buffer by number or pattern.
7686 */
7687 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 int save_magic;
7693 char_u *save_cpo;
7694 buf_T *buf;
7695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007696 if (tv->v_type == VAR_NUMBER)
7697 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007698 if (tv->v_type != VAR_STRING)
7699 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 if (name == NULL || *name == NUL)
7701 return curbuf;
7702 if (name[0] == '$' && name[1] == NUL)
7703 return lastbuf;
7704
7705 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7706 save_magic = p_magic;
7707 p_magic = TRUE;
7708 save_cpo = p_cpo;
7709 p_cpo = (char_u *)"";
7710
7711 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7712 TRUE, FALSE));
7713
7714 p_magic = save_magic;
7715 p_cpo = save_cpo;
7716
7717 /* If not found, try expanding the name, like done for bufexists(). */
7718 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007719 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
7721 return buf;
7722}
7723
7724/*
7725 * "bufname(expr)" function
7726 */
7727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007728f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007729 typval_T *argvars;
7730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
7732 buf_T *buf;
7733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007734 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736 buf = get_buf_tv(&argvars[0]);
7737 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007739 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007741 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 --emsg_off;
7743}
7744
7745/*
7746 * "bufnr(expr)" function
7747 */
7748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007749f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 typval_T *argvars;
7751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 buf_T *buf;
7754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007755 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 --emsg_off;
7763}
7764
7765/*
7766 * "bufwinnr(nr)" function
7767 */
7768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 typval_T *argvars;
7771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773#ifdef FEAT_WINDOWS
7774 win_T *wp;
7775 int winnr = 0;
7776#endif
7777 buf_T *buf;
7778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007779 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782#ifdef FEAT_WINDOWS
7783 for (wp = firstwin; wp; wp = wp->w_next)
7784 {
7785 ++winnr;
7786 if (wp->w_buffer == buf)
7787 break;
7788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007789 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007791 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792#endif
7793 --emsg_off;
7794}
7795
7796/*
7797 * "byte2line(byte)" function
7798 */
7799/*ARGSUSED*/
7800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *argvars;
7803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807#else
7808 long boff = 0;
7809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007810 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007814 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 (linenr_T)0, &boff);
7816#endif
7817}
7818
7819/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007820 * "byteidx()" function
7821 */
7822/*ARGSUSED*/
7823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007824f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 typval_T *argvars;
7826 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007827{
7828#ifdef FEAT_MBYTE
7829 char_u *t;
7830#endif
7831 char_u *str;
7832 long idx;
7833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007834 str = get_tv_string_chk(&argvars[0]);
7835 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007837 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007838 return;
7839
7840#ifdef FEAT_MBYTE
7841 t = str;
7842 for ( ; idx > 0; idx--)
7843 {
7844 if (*t == NUL) /* EOL reached */
7845 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007846 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007849#else
7850 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007852#endif
7853}
7854
7855/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007856 * "call(func, arglist)" function
7857 */
7858 static void
7859f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 typval_T *argvars;
7861 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007862{
7863 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007865 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007867 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007868 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007869
7870 rettv->vval.v_number = 0;
7871 if (argvars[1].v_type != VAR_LIST)
7872 {
7873 EMSG(_(e_listreq));
7874 return;
7875 }
7876 if (argvars[1].vval.v_list == NULL)
7877 return;
7878
7879 if (argvars[0].v_type == VAR_FUNC)
7880 func = argvars[0].vval.v_string;
7881 else
7882 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007883 if (*func == NUL)
7884 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007885
Bram Moolenaare9a41262005-01-15 22:18:47 +00007886 if (argvars[2].v_type != VAR_UNKNOWN)
7887 {
7888 if (argvars[2].v_type != VAR_DICT)
7889 {
7890 EMSG(_(e_dictreq));
7891 return;
7892 }
7893 selfdict = argvars[2].vval.v_dict;
7894 }
7895
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007896 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7897 item = item->li_next)
7898 {
7899 if (argc == MAX_FUNC_ARGS)
7900 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007901 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007902 break;
7903 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007904 /* Make a copy of each argument. This is needed to be able to set
7905 * v_lock to VAR_FIXED in the copy without changing the original list.
7906 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007907 copy_tv(&item->li_tv, &argv[argc++]);
7908 }
7909
7910 if (item == NULL)
7911 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007912 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7913 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007914
7915 /* Free the arguments. */
7916 while (argc > 0)
7917 clear_tv(&argv[--argc]);
7918}
7919
7920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 * "char2nr(string)" function
7922 */
7923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 typval_T *argvars;
7926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
7928#ifdef FEAT_MBYTE
7929 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007930 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 else
7932#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934}
7935
7936/*
7937 * "cindent(lnum)" function
7938 */
7939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 typval_T *argvars;
7942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944#ifdef FEAT_CINDENT
7945 pos_T pos;
7946 linenr_T lnum;
7947
7948 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007949 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7951 {
7952 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 curwin->w_cursor = pos;
7955 }
7956 else
7957#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959}
7960
7961/*
7962 * "col(string)" function
7963 */
7964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 typval_T *argvars;
7967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 colnr_T col = 0;
7970 pos_T *fp;
7971
7972 fp = var2fpos(&argvars[0], FALSE);
7973 if (fp != NULL)
7974 {
7975 if (fp->col == MAXCOL)
7976 {
7977 /* '> can be MAXCOL, get the length of the line then */
7978 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7979 col = STRLEN(ml_get(fp->lnum)) + 1;
7980 else
7981 col = MAXCOL;
7982 }
7983 else
7984 {
7985 col = fp->col + 1;
7986#ifdef FEAT_VIRTUALEDIT
7987 /* col(".") when the cursor is on the NUL at the end of the line
7988 * because of "coladd" can be seen as an extra column. */
7989 if (virtual_active() && fp == &curwin->w_cursor)
7990 {
7991 char_u *p = ml_get_cursor();
7992
7993 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7994 curwin->w_virtcol - curwin->w_cursor.coladd))
7995 {
7996# ifdef FEAT_MBYTE
7997 int l;
7998
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007999 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 col += l;
8001# else
8002 if (*p != NUL && p[1] == NUL)
8003 ++col;
8004# endif
8005 }
8006 }
8007#endif
8008 }
8009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011}
8012
Bram Moolenaar572cb562005-08-05 21:35:02 +00008013#if defined(FEAT_INS_EXPAND)
8014/*
8015 * "complete_add()" function
8016 */
8017/*ARGSUSED*/
8018 static void
8019f_complete_add(argvars, rettv)
8020 typval_T *argvars;
8021 typval_T *rettv;
8022{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008023 char_u *word;
8024 char_u *extra = NULL;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008025
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008026 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8027 {
8028 word = get_dict_string(argvars[0].vval.v_dict,
8029 (char_u *)"word", FALSE);
8030 extra = get_dict_string(argvars[0].vval.v_dict,
8031 (char_u *)"menu", FALSE);
8032 }
8033 else
8034 word = get_tv_string_chk(&argvars[0]);
8035 if (word != NULL)
8036 rettv->vval.v_number = ins_compl_add(word, -1, NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008037}
8038
8039/*
8040 * "complete_check()" function
8041 */
8042/*ARGSUSED*/
8043 static void
8044f_complete_check(argvars, rettv)
8045 typval_T *argvars;
8046 typval_T *rettv;
8047{
8048 int saved = RedrawingDisabled;
8049
8050 RedrawingDisabled = 0;
8051 ins_compl_check_keys(0);
8052 rettv->vval.v_number = compl_interrupted;
8053 RedrawingDisabled = saved;
8054}
8055#endif
8056
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057/*
8058 * "confirm(message, buttons[, default [, type]])" function
8059 */
8060/*ARGSUSED*/
8061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008062f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008063 typval_T *argvars;
8064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
8066#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8067 char_u *message;
8068 char_u *buttons = NULL;
8069 char_u buf[NUMBUFLEN];
8070 char_u buf2[NUMBUFLEN];
8071 int def = 1;
8072 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008073 char_u *typestr;
8074 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008076 message = get_tv_string_chk(&argvars[0]);
8077 if (message == NULL)
8078 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008079 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8082 if (buttons == NULL)
8083 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008086 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008087 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8090 if (typestr == NULL)
8091 error = TRUE;
8092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008094 switch (TOUPPER_ASC(*typestr))
8095 {
8096 case 'E': type = VIM_ERROR; break;
8097 case 'Q': type = VIM_QUESTION; break;
8098 case 'I': type = VIM_INFO; break;
8099 case 'W': type = VIM_WARNING; break;
8100 case 'G': type = VIM_GENERIC; break;
8101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103 }
8104 }
8105 }
8106
8107 if (buttons == NULL || *buttons == NUL)
8108 buttons = (char_u *)_("&Ok");
8109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008110 if (error)
8111 rettv->vval.v_number = 0;
8112 else
8113 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 def, NULL);
8115#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117#endif
8118}
8119
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008120/*
8121 * "copy()" function
8122 */
8123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 typval_T *argvars;
8126 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008127{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008128 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130
8131/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008132 * "count()" function
8133 */
8134 static void
8135f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008136 typval_T *argvars;
8137 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008138{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008139 long n = 0;
8140 int ic = FALSE;
8141
Bram Moolenaare9a41262005-01-15 22:18:47 +00008142 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008143 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008144 listitem_T *li;
8145 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008146 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008147
Bram Moolenaare9a41262005-01-15 22:18:47 +00008148 if ((l = argvars[0].vval.v_list) != NULL)
8149 {
8150 li = l->lv_first;
8151 if (argvars[2].v_type != VAR_UNKNOWN)
8152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008153 int error = FALSE;
8154
8155 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008156 if (argvars[3].v_type != VAR_UNKNOWN)
8157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008158 idx = get_tv_number_chk(&argvars[3], &error);
8159 if (!error)
8160 {
8161 li = list_find(l, idx);
8162 if (li == NULL)
8163 EMSGN(_(e_listidx), idx);
8164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008165 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008166 if (error)
8167 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008168 }
8169
8170 for ( ; li != NULL; li = li->li_next)
8171 if (tv_equal(&li->li_tv, &argvars[1], ic))
8172 ++n;
8173 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008174 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008175 else if (argvars[0].v_type == VAR_DICT)
8176 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008177 int todo;
8178 dict_T *d;
8179 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008180
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008181 if ((d = argvars[0].vval.v_dict) != NULL)
8182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008183 int error = FALSE;
8184
Bram Moolenaare9a41262005-01-15 22:18:47 +00008185 if (argvars[2].v_type != VAR_UNKNOWN)
8186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008187 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008188 if (argvars[3].v_type != VAR_UNKNOWN)
8189 EMSG(_(e_invarg));
8190 }
8191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008192 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008194 {
8195 if (!HASHITEM_EMPTY(hi))
8196 {
8197 --todo;
8198 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8199 ++n;
8200 }
8201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008202 }
8203 }
8204 else
8205 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008206 rettv->vval.v_number = n;
8207}
8208
8209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8211 *
8212 * Checks the existence of a cscope connection.
8213 */
8214/*ARGSUSED*/
8215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008216f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008217 typval_T *argvars;
8218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220#ifdef FEAT_CSCOPE
8221 int num = 0;
8222 char_u *dbpath = NULL;
8223 char_u *prepend = NULL;
8224 char_u buf[NUMBUFLEN];
8225
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008226 if (argvars[0].v_type != VAR_UNKNOWN
8227 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008229 num = (int)get_tv_number(&argvars[0]);
8230 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008231 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008232 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
8234
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238#endif
8239}
8240
8241/*
8242 * "cursor(lnum, col)" function
8243 *
8244 * Moves the cursor to the specified line and column
8245 */
8246/*ARGSUSED*/
8247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008248f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008249 typval_T *argvars;
8250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
8252 long line, col;
8253
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008254 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008255 col = get_tv_number_chk(&argvars[1], NULL);
8256 if (line < 0 || col < 0)
8257 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 if (line > 0)
8259 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 if (col > 0)
8261 curwin->w_cursor.col = col - 1;
8262#ifdef FEAT_VIRTUALEDIT
8263 curwin->w_cursor.coladd = 0;
8264#endif
8265
8266 /* Make sure the cursor is in a valid position. */
8267 check_cursor();
8268#ifdef FEAT_MBYTE
8269 /* Correct cursor for multi-byte character. */
8270 if (has_mbyte)
8271 mb_adjust_cursor();
8272#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008273
8274 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275}
8276
8277/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008278 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 */
8280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008281f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008282 typval_T *argvars;
8283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008285 int noref = 0;
8286
8287 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008288 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008289 if (noref < 0 || noref > 1)
8290 EMSG(_(e_invarg));
8291 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008292 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293}
8294
8295/*
8296 * "delete()" function
8297 */
8298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 typval_T *argvars;
8301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008306 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307}
8308
8309/*
8310 * "did_filetype()" function
8311 */
8312/*ARGSUSED*/
8313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008314f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008315 typval_T *argvars;
8316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317{
8318#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008321 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322#endif
8323}
8324
8325/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008326 * "diff_filler()" function
8327 */
8328/*ARGSUSED*/
8329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008330f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008331 typval_T *argvars;
8332 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008333{
8334#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008335 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008336#endif
8337}
8338
8339/*
8340 * "diff_hlID()" function
8341 */
8342/*ARGSUSED*/
8343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 typval_T *argvars;
8346 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008347{
8348#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008349 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008350 static linenr_T prev_lnum = 0;
8351 static int changedtick = 0;
8352 static int fnum = 0;
8353 static int change_start = 0;
8354 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008355 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008356 int filler_lines;
8357 int col;
8358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008359 if (lnum < 0) /* ignore type error in {lnum} arg */
8360 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008361 if (lnum != prev_lnum
8362 || changedtick != curbuf->b_changedtick
8363 || fnum != curbuf->b_fnum)
8364 {
8365 /* New line, buffer, change: need to get the values. */
8366 filler_lines = diff_check(curwin, lnum);
8367 if (filler_lines < 0)
8368 {
8369 if (filler_lines == -1)
8370 {
8371 change_start = MAXCOL;
8372 change_end = -1;
8373 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8374 hlID = HLF_ADD; /* added line */
8375 else
8376 hlID = HLF_CHD; /* changed line */
8377 }
8378 else
8379 hlID = HLF_ADD; /* added line */
8380 }
8381 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008382 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008383 prev_lnum = lnum;
8384 changedtick = curbuf->b_changedtick;
8385 fnum = curbuf->b_fnum;
8386 }
8387
8388 if (hlID == HLF_CHD || hlID == HLF_TXD)
8389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008390 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008391 if (col >= change_start && col <= change_end)
8392 hlID = HLF_TXD; /* changed text */
8393 else
8394 hlID = HLF_CHD; /* changed line */
8395 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008396 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008397#endif
8398}
8399
8400/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008401 * "empty({expr})" function
8402 */
8403 static void
8404f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008405 typval_T *argvars;
8406 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008407{
8408 int n;
8409
8410 switch (argvars[0].v_type)
8411 {
8412 case VAR_STRING:
8413 case VAR_FUNC:
8414 n = argvars[0].vval.v_string == NULL
8415 || *argvars[0].vval.v_string == NUL;
8416 break;
8417 case VAR_NUMBER:
8418 n = argvars[0].vval.v_number == 0;
8419 break;
8420 case VAR_LIST:
8421 n = argvars[0].vval.v_list == NULL
8422 || argvars[0].vval.v_list->lv_first == NULL;
8423 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008424 case VAR_DICT:
8425 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008426 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008428 default:
8429 EMSG2(_(e_intern2), "f_empty()");
8430 n = 0;
8431 }
8432
8433 rettv->vval.v_number = n;
8434}
8435
8436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 * "escape({string}, {chars})" function
8438 */
8439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008441 typval_T *argvars;
8442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443{
8444 char_u buf[NUMBUFLEN];
8445
Bram Moolenaar758711c2005-02-02 23:11:38 +00008446 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8447 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449}
8450
8451/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008452 * "eval()" function
8453 */
8454/*ARGSUSED*/
8455 static void
8456f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008457 typval_T *argvars;
8458 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008459{
8460 char_u *s;
8461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462 s = get_tv_string_chk(&argvars[0]);
8463 if (s != NULL)
8464 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008466 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8467 {
8468 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008469 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008470 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008471 else if (*s != NUL)
8472 EMSG(_(e_trailing));
8473}
8474
8475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 * "eventhandler()" function
8477 */
8478/*ARGSUSED*/
8479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008480f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008481 typval_T *argvars;
8482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008484 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485}
8486
8487/*
8488 * "executable()" function
8489 */
8490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 typval_T *argvars;
8493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008495 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496}
8497
8498/*
8499 * "exists()" function
8500 */
8501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 typval_T *argvars;
8504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506 char_u *p;
8507 char_u *name;
8508 int n = FALSE;
8509 int len = 0;
8510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 if (*p == '$') /* environment variable */
8513 {
8514 /* first try "normal" environment variables (fast) */
8515 if (mch_getenv(p + 1) != NULL)
8516 n = TRUE;
8517 else
8518 {
8519 /* try expanding things like $VIM and ${HOME} */
8520 p = expand_env_save(p);
8521 if (p != NULL && *p != '$')
8522 n = TRUE;
8523 vim_free(p);
8524 }
8525 }
8526 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008527 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 else if (*p == '*') /* internal or user defined function */
8529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 }
8532 else if (*p == ':')
8533 {
8534 n = cmd_exists(p + 1);
8535 }
8536 else if (*p == '#')
8537 {
8538#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008539 if (p[1] == '#')
8540 n = autocmd_supported(p + 2);
8541 else
8542 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543#endif
8544 }
8545 else /* internal variable */
8546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008547 char_u *tofree;
8548 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008550 /* get_name_len() takes care of expanding curly braces */
8551 name = p;
8552 len = get_name_len(&p, &tofree, TRUE, FALSE);
8553 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008555 if (tofree != NULL)
8556 name = tofree;
8557 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8558 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008560 /* handle d.key, l[idx], f(expr) */
8561 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8562 if (n)
8563 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 }
8565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008567 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 }
8569
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571}
8572
8573/*
8574 * "expand()" function
8575 */
8576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 typval_T *argvars;
8579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581 char_u *s;
8582 int len;
8583 char_u *errormsg;
8584 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8585 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008586 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->v_type = VAR_STRING;
8589 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 if (*s == '%' || *s == '#' || *s == '<')
8591 {
8592 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 --emsg_off;
8595 }
8596 else
8597 {
8598 /* When the optional second argument is non-zero, don't remove matches
8599 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008600 if (argvars[1].v_type != VAR_UNKNOWN
8601 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 if (!error)
8604 {
8605 ExpandInit(&xpc);
8606 xpc.xp_context = EXPAND_FILES;
8607 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8608 ExpandCleanup(&xpc);
8609 }
8610 else
8611 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613}
8614
8615/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008616 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008617 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008618 */
8619 static void
8620f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008621 typval_T *argvars;
8622 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008623{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008624 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008625 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008626 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008627 list_T *l1, *l2;
8628 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008629 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008630 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008631
Bram Moolenaare9a41262005-01-15 22:18:47 +00008632 l1 = argvars[0].vval.v_list;
8633 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008634 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8635 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008636 {
8637 if (argvars[2].v_type != VAR_UNKNOWN)
8638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008639 before = get_tv_number_chk(&argvars[2], &error);
8640 if (error)
8641 return; /* type error; errmsg already given */
8642
Bram Moolenaar758711c2005-02-02 23:11:38 +00008643 if (before == l1->lv_len)
8644 item = NULL;
8645 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008647 item = list_find(l1, before);
8648 if (item == NULL)
8649 {
8650 EMSGN(_(e_listidx), before);
8651 return;
8652 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008653 }
8654 }
8655 else
8656 item = NULL;
8657 list_extend(l1, l2, item);
8658
Bram Moolenaare9a41262005-01-15 22:18:47 +00008659 copy_tv(&argvars[0], rettv);
8660 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008661 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008662 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8663 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 dict_T *d1, *d2;
8665 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008666 char_u *action;
8667 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008668 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008669 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008670
8671 d1 = argvars[0].vval.v_dict;
8672 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008673 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8674 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008675 {
8676 /* Check the third argument. */
8677 if (argvars[2].v_type != VAR_UNKNOWN)
8678 {
8679 static char *(av[]) = {"keep", "force", "error"};
8680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008681 action = get_tv_string_chk(&argvars[2]);
8682 if (action == NULL)
8683 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008684 for (i = 0; i < 3; ++i)
8685 if (STRCMP(action, av[i]) == 0)
8686 break;
8687 if (i == 3)
8688 {
8689 EMSGN(_(e_invarg2), action);
8690 return;
8691 }
8692 }
8693 else
8694 action = (char_u *)"force";
8695
8696 /* Go over all entries in the second dict and add them to the
8697 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008698 todo = d2->dv_hashtab.ht_used;
8699 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008700 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008701 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008702 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008703 --todo;
8704 di1 = dict_find(d1, hi2->hi_key, -1);
8705 if (di1 == NULL)
8706 {
8707 di1 = dictitem_copy(HI2DI(hi2));
8708 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8709 dictitem_free(di1);
8710 }
8711 else if (*action == 'e')
8712 {
8713 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8714 break;
8715 }
8716 else if (*action == 'f')
8717 {
8718 clear_tv(&di1->di_tv);
8719 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8720 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008721 }
8722 }
8723
Bram Moolenaare9a41262005-01-15 22:18:47 +00008724 copy_tv(&argvars[0], rettv);
8725 }
8726 }
8727 else
8728 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008729}
8730
8731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 * "filereadable()" function
8733 */
8734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 typval_T *argvars;
8737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738{
8739 FILE *fd;
8740 char_u *p;
8741 int n;
8742
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8745 {
8746 n = TRUE;
8747 fclose(fd);
8748 }
8749 else
8750 n = FALSE;
8751
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008752 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
8754
8755/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008756 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 * rights to write into.
8758 */
8759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008761 typval_T *argvars;
8762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008764 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765}
8766
Bram Moolenaar33570922005-01-25 22:26:29 +00008767static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008768
8769 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008770findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008773 int dir;
8774{
8775#ifdef FEAT_SEARCHPATH
8776 char_u *fname;
8777 char_u *fresult = NULL;
8778 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8779 char_u *p;
8780 char_u pathbuf[NUMBUFLEN];
8781 int count = 1;
8782 int first = TRUE;
8783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008785
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008786 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008788 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8789 if (p == NULL)
8790 count = -1; /* error */
8791 else
8792 {
8793 if (*p != NUL)
8794 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 if (argvars[2].v_type != VAR_UNKNOWN)
8797 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8798 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008799 }
8800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008801 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008803 do
8804 {
8805 vim_free(fresult);
8806 fresult = find_file_in_path_option(first ? fname : NULL,
8807 first ? (int)STRLEN(fname) : 0,
8808 0, first, path, dir, NULL);
8809 first = FALSE;
8810 } while (--count > 0 && fresult != NULL);
8811 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008814#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008815 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008818}
8819
Bram Moolenaar33570922005-01-25 22:26:29 +00008820static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8821static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008822
8823/*
8824 * Implementation of map() and filter().
8825 */
8826 static void
8827filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008828 typval_T *argvars;
8829 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008830 int map;
8831{
8832 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008833 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008834 listitem_T *li, *nli;
8835 list_T *l = NULL;
8836 dictitem_T *di;
8837 hashtab_T *ht;
8838 hashitem_T *hi;
8839 dict_T *d = NULL;
8840 typval_T save_val;
8841 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008842 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008843 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008844 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008845 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008846
8847 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008849 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008850 if ((l = argvars[0].vval.v_list) == NULL
8851 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 return;
8853 }
8854 else if (argvars[0].v_type == VAR_DICT)
8855 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008856 if ((d = argvars[0].vval.v_dict) == NULL
8857 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008858 return;
8859 }
8860 else
8861 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008862 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008863 return;
8864 }
8865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008866 expr = get_tv_string_buf_chk(&argvars[1], buf);
8867 /* On type errors, the preceding call has already displayed an error
8868 * message. Avoid a misleading error message for an empty string that
8869 * was not passed as argument. */
8870 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008872 prepare_vimvar(VV_VAL, &save_val);
8873 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008874
Bram Moolenaar280f1262006-01-30 00:14:18 +00008875 /* We reset "called_emsg" to be able to detect whether an error
8876 * occurred during evaluation of the expression. "did_emsg" can't be
8877 * used, because it is reset when calling a function. */
8878 save_called_emsg = called_emsg;
8879 called_emsg = FALSE;
8880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008883 prepare_vimvar(VV_KEY, &save_key);
8884 vimvars[VV_KEY].vv_type = VAR_STRING;
8885
8886 ht = &d->dv_hashtab;
8887 hash_lock(ht);
8888 todo = ht->ht_used;
8889 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891 if (!HASHITEM_EMPTY(hi))
8892 {
8893 --todo;
8894 di = HI2DI(hi);
8895 if (tv_check_lock(di->di_tv.v_lock, msg))
8896 break;
8897 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008898 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8899 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008900 break;
8901 if (!map && rem)
8902 dictitem_remove(d, di);
8903 clear_tv(&vimvars[VV_KEY].vv_tv);
8904 }
8905 }
8906 hash_unlock(ht);
8907
8908 restore_vimvar(VV_KEY, &save_key);
8909 }
8910 else
8911 {
8912 for (li = l->lv_first; li != NULL; li = nli)
8913 {
8914 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008915 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008917 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
8918 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008919 break;
8920 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008921 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008922 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008923 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008925 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008926
8927 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008929
8930 copy_tv(&argvars[0], rettv);
8931}
8932
8933 static int
8934filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008936 char_u *expr;
8937 int map;
8938 int *remp;
8939{
Bram Moolenaar33570922005-01-25 22:26:29 +00008940 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008941 char_u *s;
8942
Bram Moolenaar33570922005-01-25 22:26:29 +00008943 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008944 s = expr;
8945 if (eval1(&s, &rettv, TRUE) == FAIL)
8946 return FAIL;
8947 if (*s != NUL) /* check for trailing chars after expr */
8948 {
8949 EMSG2(_(e_invexpr2), s);
8950 return FAIL;
8951 }
8952 if (map)
8953 {
8954 /* map(): replace the list item value */
8955 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008956 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 *tv = rettv;
8958 }
8959 else
8960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008961 int error = FALSE;
8962
Bram Moolenaare9a41262005-01-15 22:18:47 +00008963 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008965 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966 /* On type error, nothing has been removed; return FAIL to stop the
8967 * loop. The error message was given by get_tv_number_chk(). */
8968 if (error)
8969 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008970 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008972 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008973}
8974
8975/*
8976 * "filter()" function
8977 */
8978 static void
8979f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 typval_T *argvars;
8981 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008982{
8983 filter_map(argvars, rettv, FALSE);
8984}
8985
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008987 * "finddir({fname}[, {path}[, {count}]])" function
8988 */
8989 static void
8990f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 typval_T *argvars;
8992 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008993{
8994 findfilendir(argvars, rettv, TRUE);
8995}
8996
8997/*
8998 * "findfile({fname}[, {path}[, {count}]])" function
8999 */
9000 static void
9001f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 typval_T *argvars;
9003 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009004{
9005 findfilendir(argvars, rettv, FALSE);
9006}
9007
9008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 * "fnamemodify({fname}, {mods})" function
9010 */
9011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 typval_T *argvars;
9014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015{
9016 char_u *fname;
9017 char_u *mods;
9018 int usedlen = 0;
9019 int len;
9020 char_u *fbuf = NULL;
9021 char_u buf[NUMBUFLEN];
9022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009023 fname = get_tv_string_chk(&argvars[0]);
9024 mods = get_tv_string_buf_chk(&argvars[1], buf);
9025 if (fname == NULL || mods == NULL)
9026 fname = NULL;
9027 else
9028 {
9029 len = (int)STRLEN(fname);
9030 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009033 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 vim_free(fbuf);
9039}
9040
Bram Moolenaar33570922005-01-25 22:26:29 +00009041static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042
9043/*
9044 * "foldclosed()" function
9045 */
9046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 typval_T *argvars;
9049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 int end;
9051{
9052#ifdef FEAT_FOLDING
9053 linenr_T lnum;
9054 linenr_T first, last;
9055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9058 {
9059 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9060 {
9061 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 return;
9066 }
9067 }
9068#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070}
9071
9072/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009073 * "foldclosed()" function
9074 */
9075 static void
9076f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009077 typval_T *argvars;
9078 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009079{
9080 foldclosed_both(argvars, rettv, FALSE);
9081}
9082
9083/*
9084 * "foldclosedend()" function
9085 */
9086 static void
9087f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 typval_T *argvars;
9089 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009090{
9091 foldclosed_both(argvars, rettv, TRUE);
9092}
9093
9094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 * "foldlevel()" function
9096 */
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102#ifdef FEAT_FOLDING
9103 linenr_T lnum;
9104
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 else
9109#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111}
9112
9113/*
9114 * "foldtext()" function
9115 */
9116/*ARGSUSED*/
9117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009119 typval_T *argvars;
9120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122#ifdef FEAT_FOLDING
9123 linenr_T lnum;
9124 char_u *s;
9125 char_u *r;
9126 int len;
9127 char *txt;
9128#endif
9129
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130 rettv->v_type = VAR_STRING;
9131 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009133 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9134 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9135 <= curbuf->b_ml.ml_line_count
9136 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 {
9138 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009139 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9140 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 {
9142 if (!linewhite(lnum))
9143 break;
9144 ++lnum;
9145 }
9146
9147 /* Find interesting text in this line. */
9148 s = skipwhite(ml_get(lnum));
9149 /* skip C comment-start */
9150 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009153 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009154 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009155 {
9156 s = skipwhite(ml_get(lnum + 1));
9157 if (*s == '*')
9158 s = skipwhite(s + 1);
9159 }
9160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 txt = _("+-%s%3ld lines: ");
9162 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009163 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 + 20 /* for %3ld */
9165 + STRLEN(s))); /* concatenated */
9166 if (r != NULL)
9167 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009168 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9169 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9170 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 len = (int)STRLEN(r);
9172 STRCAT(r, s);
9173 /* remove 'foldmarker' and 'commentstring' */
9174 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 }
9177 }
9178#endif
9179}
9180
9181/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009182 * "foldtextresult(lnum)" function
9183 */
9184/*ARGSUSED*/
9185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009187 typval_T *argvars;
9188 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009189{
9190#ifdef FEAT_FOLDING
9191 linenr_T lnum;
9192 char_u *text;
9193 char_u buf[51];
9194 foldinfo_T foldinfo;
9195 int fold_count;
9196#endif
9197
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->v_type = VAR_STRING;
9199 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009200#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009202 /* treat illegal types and illegal string values for {lnum} the same */
9203 if (lnum < 0)
9204 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009205 fold_count = foldedCount(curwin, lnum, &foldinfo);
9206 if (fold_count > 0)
9207 {
9208 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9209 &foldinfo, buf);
9210 if (text == buf)
9211 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009213 }
9214#endif
9215}
9216
9217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 * "foreground()" function
9219 */
9220/*ARGSUSED*/
9221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009223 typval_T *argvars;
9224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227#ifdef FEAT_GUI
9228 if (gui.in_use)
9229 gui_mch_set_foreground();
9230#else
9231# ifdef WIN32
9232 win32_set_foreground();
9233# endif
9234#endif
9235}
9236
9237/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009238 * "function()" function
9239 */
9240/*ARGSUSED*/
9241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009242f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009243 typval_T *argvars;
9244 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009245{
9246 char_u *s;
9247
Bram Moolenaara7043832005-01-21 11:56:39 +00009248 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009249 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009250 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009251 EMSG2(_(e_invarg2), s);
9252 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009253 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009254 else
9255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256 rettv->vval.v_string = vim_strsave(s);
9257 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009258 }
9259}
9260
9261/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009262 * "garbagecollect()" function
9263 */
9264/*ARGSUSED*/
9265 static void
9266f_garbagecollect(argvars, rettv)
9267 typval_T *argvars;
9268 typval_T *rettv;
9269{
9270 garbage_collect();
9271}
9272
9273/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009274 * "get()" function
9275 */
9276 static void
9277f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 typval_T *argvars;
9279 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009280{
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 listitem_T *li;
9282 list_T *l;
9283 dictitem_T *di;
9284 dict_T *d;
9285 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009286
Bram Moolenaare9a41262005-01-15 22:18:47 +00009287 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009288 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009289 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009291 int error = FALSE;
9292
9293 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9294 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009295 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009296 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009297 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009298 else if (argvars[0].v_type == VAR_DICT)
9299 {
9300 if ((d = argvars[0].vval.v_dict) != NULL)
9301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009302 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 if (di != NULL)
9304 tv = &di->di_tv;
9305 }
9306 }
9307 else
9308 EMSG2(_(e_listdictarg), "get()");
9309
9310 if (tv == NULL)
9311 {
9312 if (argvars[2].v_type == VAR_UNKNOWN)
9313 rettv->vval.v_number = 0;
9314 else
9315 copy_tv(&argvars[2], rettv);
9316 }
9317 else
9318 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009319}
9320
Bram Moolenaar342337a2005-07-21 21:11:17 +00009321static 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 +00009322
9323/*
9324 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009325 * Return a range (from start to end) of lines in rettv from the specified
9326 * buffer.
9327 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009328 */
9329 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009330get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009331 buf_T *buf;
9332 linenr_T start;
9333 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009334 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009335 typval_T *rettv;
9336{
9337 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009338 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009339
Bram Moolenaar342337a2005-07-21 21:11:17 +00009340 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009341 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009342 l = list_alloc();
9343 if (l == NULL)
9344 return;
9345
9346 rettv->vval.v_list = l;
9347 rettv->v_type = VAR_LIST;
9348 ++l->lv_refcount;
9349 }
9350 else
9351 rettv->vval.v_number = 0;
9352
9353 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9354 return;
9355
9356 if (!retlist)
9357 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009358 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9359 p = ml_get_buf(buf, start, FALSE);
9360 else
9361 p = (char_u *)"";
9362
9363 rettv->v_type = VAR_STRING;
9364 rettv->vval.v_string = vim_strsave(p);
9365 }
9366 else
9367 {
9368 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009369 return;
9370
9371 if (start < 1)
9372 start = 1;
9373 if (end > buf->b_ml.ml_line_count)
9374 end = buf->b_ml.ml_line_count;
9375 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009376 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9377 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009378 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009379 }
9380}
9381
9382/*
9383 * "getbufline()" function
9384 */
9385 static void
9386f_getbufline(argvars, rettv)
9387 typval_T *argvars;
9388 typval_T *rettv;
9389{
9390 linenr_T lnum;
9391 linenr_T end;
9392 buf_T *buf;
9393
9394 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9395 ++emsg_off;
9396 buf = get_buf_tv(&argvars[0]);
9397 --emsg_off;
9398
Bram Moolenaar661b1822005-07-28 22:36:45 +00009399 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009400 if (argvars[2].v_type == VAR_UNKNOWN)
9401 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009402 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009403 end = get_tv_lnum_buf(&argvars[2], buf);
9404
Bram Moolenaar342337a2005-07-21 21:11:17 +00009405 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009406}
9407
Bram Moolenaar0d660222005-01-07 21:51:51 +00009408/*
9409 * "getbufvar()" function
9410 */
9411 static void
9412f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009413 typval_T *argvars;
9414 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009415{
9416 buf_T *buf;
9417 buf_T *save_curbuf;
9418 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9422 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009423 ++emsg_off;
9424 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009425
9426 rettv->v_type = VAR_STRING;
9427 rettv->vval.v_string = NULL;
9428
9429 if (buf != NULL && varname != NULL)
9430 {
9431 if (*varname == '&') /* buffer-local-option */
9432 {
9433 /* set curbuf to be our buf, temporarily */
9434 save_curbuf = curbuf;
9435 curbuf = buf;
9436
9437 get_option_tv(&varname, rettv, TRUE);
9438
9439 /* restore previous notion of curbuf */
9440 curbuf = save_curbuf;
9441 }
9442 else
9443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009444 if (*varname == NUL)
9445 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9446 * scope prefix before the NUL byte is required by
9447 * find_var_in_ht(). */
9448 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009449 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009450 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009451 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009452 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009453 }
9454 }
9455
9456 --emsg_off;
9457}
9458
9459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 * "getchar()" function
9461 */
9462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009464 typval_T *argvars;
9465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466{
9467 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009468 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
9470 ++no_mapping;
9471 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009472 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 /* getchar(): blocking wait. */
9474 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009475 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 /* getchar(1): only check if char avail */
9477 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009478 else if (error || vpeekc() == NUL)
9479 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 n = 0;
9481 else
9482 /* getchar(0) and char avail: return char */
9483 n = safe_vgetc();
9484 --no_mapping;
9485 --allow_keys;
9486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 if (IS_SPECIAL(n) || mod_mask != 0)
9489 {
9490 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9491 int i = 0;
9492
9493 /* Turn a special key into three bytes, plus modifier. */
9494 if (mod_mask != 0)
9495 {
9496 temp[i++] = K_SPECIAL;
9497 temp[i++] = KS_MODIFIER;
9498 temp[i++] = mod_mask;
9499 }
9500 if (IS_SPECIAL(n))
9501 {
9502 temp[i++] = K_SPECIAL;
9503 temp[i++] = K_SECOND(n);
9504 temp[i++] = K_THIRD(n);
9505 }
9506#ifdef FEAT_MBYTE
9507 else if (has_mbyte)
9508 i += (*mb_char2bytes)(n, temp + i);
9509#endif
9510 else
9511 temp[i++] = n;
9512 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 rettv->v_type = VAR_STRING;
9514 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 }
9516}
9517
9518/*
9519 * "getcharmod()" function
9520 */
9521/*ARGSUSED*/
9522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009524 typval_T *argvars;
9525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528}
9529
9530/*
9531 * "getcmdline()" function
9532 */
9533/*ARGSUSED*/
9534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009536 typval_T *argvars;
9537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 rettv->v_type = VAR_STRING;
9540 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541}
9542
9543/*
9544 * "getcmdpos()" function
9545 */
9546/*ARGSUSED*/
9547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009549 typval_T *argvars;
9550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553}
9554
9555/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009556 * "getcmdtype()" function
9557 */
9558/*ARGSUSED*/
9559 static void
9560f_getcmdtype(argvars, rettv)
9561 typval_T *argvars;
9562 typval_T *rettv;
9563{
9564 rettv->v_type = VAR_STRING;
9565 rettv->vval.v_string = alloc(2);
9566 if (rettv->vval.v_string != NULL)
9567 {
9568 rettv->vval.v_string[0] = get_cmdline_type();
9569 rettv->vval.v_string[1] = NUL;
9570 }
9571}
9572
9573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 * "getcwd()" function
9575 */
9576/*ARGSUSED*/
9577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009579 typval_T *argvars;
9580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582 char_u cwd[MAXPATHL];
9583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 else
9588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009591 if (rettv->vval.v_string != NULL)
9592 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593#endif
9594 }
9595}
9596
9597/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009598 * "getfontname()" function
9599 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009600/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009603 typval_T *argvars;
9604 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009605{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->v_type = VAR_STRING;
9607 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009608#ifdef FEAT_GUI
9609 if (gui.in_use)
9610 {
9611 GuiFont font;
9612 char_u *name = NULL;
9613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009614 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009615 {
9616 /* Get the "Normal" font. Either the name saved by
9617 * hl_set_font_name() or from the font ID. */
9618 font = gui.norm_font;
9619 name = hl_get_font_name();
9620 }
9621 else
9622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009624 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9625 return;
9626 font = gui_mch_get_font(name, FALSE);
9627 if (font == NOFONT)
9628 return; /* Invalid font name, return empty string. */
9629 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009631 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009632 gui_mch_free_font(font);
9633 }
9634#endif
9635}
9636
9637/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009638 * "getfperm({fname})" function
9639 */
9640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009642 typval_T *argvars;
9643 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009644{
9645 char_u *fname;
9646 struct stat st;
9647 char_u *perm = NULL;
9648 char_u flags[] = "rwx";
9649 int i;
9650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009652
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009654 if (mch_stat((char *)fname, &st) >= 0)
9655 {
9656 perm = vim_strsave((char_u *)"---------");
9657 if (perm != NULL)
9658 {
9659 for (i = 0; i < 9; i++)
9660 {
9661 if (st.st_mode & (1 << (8 - i)))
9662 perm[i] = flags[i % 3];
9663 }
9664 }
9665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009667}
9668
9669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 * "getfsize({fname})" function
9671 */
9672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 typval_T *argvars;
9675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676{
9677 char_u *fname;
9678 struct stat st;
9679
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009680 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683
9684 if (mch_stat((char *)fname, &st) >= 0)
9685 {
9686 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009687 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 }
9691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693}
9694
9695/*
9696 * "getftime({fname})" function
9697 */
9698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009700 typval_T *argvars;
9701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 char_u *fname;
9704 struct stat st;
9705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707
9708 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712}
9713
9714/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009715 * "getftype({fname})" function
9716 */
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009721{
9722 char_u *fname;
9723 struct stat st;
9724 char_u *type = NULL;
9725 char *t;
9726
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009728
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009730 if (mch_lstat((char *)fname, &st) >= 0)
9731 {
9732#ifdef S_ISREG
9733 if (S_ISREG(st.st_mode))
9734 t = "file";
9735 else if (S_ISDIR(st.st_mode))
9736 t = "dir";
9737# ifdef S_ISLNK
9738 else if (S_ISLNK(st.st_mode))
9739 t = "link";
9740# endif
9741# ifdef S_ISBLK
9742 else if (S_ISBLK(st.st_mode))
9743 t = "bdev";
9744# endif
9745# ifdef S_ISCHR
9746 else if (S_ISCHR(st.st_mode))
9747 t = "cdev";
9748# endif
9749# ifdef S_ISFIFO
9750 else if (S_ISFIFO(st.st_mode))
9751 t = "fifo";
9752# endif
9753# ifdef S_ISSOCK
9754 else if (S_ISSOCK(st.st_mode))
9755 t = "fifo";
9756# endif
9757 else
9758 t = "other";
9759#else
9760# ifdef S_IFMT
9761 switch (st.st_mode & S_IFMT)
9762 {
9763 case S_IFREG: t = "file"; break;
9764 case S_IFDIR: t = "dir"; break;
9765# ifdef S_IFLNK
9766 case S_IFLNK: t = "link"; break;
9767# endif
9768# ifdef S_IFBLK
9769 case S_IFBLK: t = "bdev"; break;
9770# endif
9771# ifdef S_IFCHR
9772 case S_IFCHR: t = "cdev"; break;
9773# endif
9774# ifdef S_IFIFO
9775 case S_IFIFO: t = "fifo"; break;
9776# endif
9777# ifdef S_IFSOCK
9778 case S_IFSOCK: t = "socket"; break;
9779# endif
9780 default: t = "other";
9781 }
9782# else
9783 if (mch_isdir(fname))
9784 t = "dir";
9785 else
9786 t = "file";
9787# endif
9788#endif
9789 type = vim_strsave((char_u *)t);
9790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009792}
9793
9794/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009795 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009796 */
9797 static void
9798f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009799 typval_T *argvars;
9800 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009801{
9802 linenr_T lnum;
9803 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009804 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009805
9806 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009807 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009808 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009809 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009810 retlist = FALSE;
9811 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009812 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009813 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009814 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009815 retlist = TRUE;
9816 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009817
Bram Moolenaar342337a2005-07-21 21:11:17 +00009818 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009819}
9820
9821/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009822 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009823 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009824/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009825 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009826f_getqflist(argvars, rettv)
9827 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009828 typval_T *rettv;
9829{
9830#ifdef FEAT_QUICKFIX
9831 list_T *l;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009832 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009833#endif
9834
9835 rettv->vval.v_number = FALSE;
9836#ifdef FEAT_QUICKFIX
9837 l = list_alloc();
9838 if (l != NULL)
9839 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009840 rettv->vval.v_list = l;
9841 rettv->v_type = VAR_LIST;
9842 ++l->lv_refcount;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009843 wp = NULL;
9844 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9845 {
9846 wp = find_win_by_nr(&argvars[0]);
9847 if (wp == NULL)
9848 return;
9849 }
9850
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009851 (void)get_errorlist(wp, l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009852 }
9853#endif
9854}
9855
9856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 * "getreg()" function
9858 */
9859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009861 typval_T *argvars;
9862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863{
9864 char_u *strregname;
9865 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009866 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009867 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009869 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009871 strregname = get_tv_string_chk(&argvars[0]);
9872 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009873 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009877 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 regname = (strregname == NULL ? '"' : *strregname);
9879 if (regname == 0)
9880 regname = '"';
9881
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009883 rettv->vval.v_string = error ? NULL :
9884 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885}
9886
9887/*
9888 * "getregtype()" function
9889 */
9890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 typval_T *argvars;
9893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895 char_u *strregname;
9896 int regname;
9897 char_u buf[NUMBUFLEN + 2];
9898 long reglen = 0;
9899
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009900 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 {
9902 strregname = get_tv_string_chk(&argvars[0]);
9903 if (strregname == NULL) /* type error; errmsg already given */
9904 {
9905 rettv->v_type = VAR_STRING;
9906 rettv->vval.v_string = NULL;
9907 return;
9908 }
9909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 else
9911 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
9914 regname = (strregname == NULL ? '"' : *strregname);
9915 if (regname == 0)
9916 regname = '"';
9917
9918 buf[0] = NUL;
9919 buf[1] = NUL;
9920 switch (get_reg_type(regname, &reglen))
9921 {
9922 case MLINE: buf[0] = 'V'; break;
9923 case MCHAR: buf[0] = 'v'; break;
9924#ifdef FEAT_VISUAL
9925 case MBLOCK:
9926 buf[0] = Ctrl_V;
9927 sprintf((char *)buf + 1, "%ld", reglen + 1);
9928 break;
9929#endif
9930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931 rettv->v_type = VAR_STRING;
9932 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933}
9934
9935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 * "getwinposx()" function
9937 */
9938/*ARGSUSED*/
9939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009941 typval_T *argvars;
9942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945#ifdef FEAT_GUI
9946 if (gui.in_use)
9947 {
9948 int x, y;
9949
9950 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 }
9953#endif
9954}
9955
9956/*
9957 * "getwinposy()" function
9958 */
9959/*ARGSUSED*/
9960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009962 typval_T *argvars;
9963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966#ifdef FEAT_GUI
9967 if (gui.in_use)
9968 {
9969 int x, y;
9970
9971 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 }
9974#endif
9975}
9976
Bram Moolenaara40058a2005-07-11 22:42:07 +00009977 static win_T *
9978find_win_by_nr(vp)
9979 typval_T *vp;
9980{
9981#ifdef FEAT_WINDOWS
9982 win_T *wp;
9983#endif
9984 int nr;
9985
9986 nr = get_tv_number_chk(vp, NULL);
9987
9988#ifdef FEAT_WINDOWS
9989 if (nr < 0)
9990 return NULL;
9991 if (nr == 0)
9992 return curwin;
9993
9994 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9995 if (--nr <= 0)
9996 break;
9997 return wp;
9998#else
9999 if (nr == 0 || nr == 1)
10000 return curwin;
10001 return NULL;
10002#endif
10003}
10004
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005/*
10006 * "getwinvar()" function
10007 */
10008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010010 typval_T *argvars;
10011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012{
10013 win_T *win, *oldcurwin;
10014 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010015 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 varname = get_tv_string_chk(&argvars[1]);
10019 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 rettv->v_type = VAR_STRING;
10022 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023
10024 if (win != NULL && varname != NULL)
10025 {
10026 if (*varname == '&') /* window-local-option */
10027 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010028 /* Set curwin to be our win, temporarily. Also set curbuf, so
10029 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 oldcurwin = curwin;
10031 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010032 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035
10036 /* restore previous notion of curwin */
10037 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010038 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 }
10040 else
10041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010042 if (*varname == NUL)
10043 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10044 * scope prefix before the NUL byte is required by
10045 * find_var_in_ht(). */
10046 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010048 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010050 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 }
10052 }
10053
10054 --emsg_off;
10055}
10056
10057/*
10058 * "glob()" function
10059 */
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
10065 expand_T xpc;
10066
10067 ExpandInit(&xpc);
10068 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069 rettv->v_type = VAR_STRING;
10070 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10072 ExpandCleanup(&xpc);
10073}
10074
10075/*
10076 * "globpath()" function
10077 */
10078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010086 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010087 if (file == NULL)
10088 rettv->vval.v_string = NULL;
10089 else
10090 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091}
10092
10093/*
10094 * "has()" function
10095 */
10096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010098 typval_T *argvars;
10099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 int i;
10102 char_u *name;
10103 int n = FALSE;
10104 static char *(has_list[]) =
10105 {
10106#ifdef AMIGA
10107 "amiga",
10108# ifdef FEAT_ARP
10109 "arp",
10110# endif
10111#endif
10112#ifdef __BEOS__
10113 "beos",
10114#endif
10115#ifdef MSDOS
10116# ifdef DJGPP
10117 "dos32",
10118# else
10119 "dos16",
10120# endif
10121#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010122#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 "mac",
10124#endif
10125#if defined(MACOS_X_UNIX)
10126 "macunix",
10127#endif
10128#ifdef OS2
10129 "os2",
10130#endif
10131#ifdef __QNX__
10132 "qnx",
10133#endif
10134#ifdef RISCOS
10135 "riscos",
10136#endif
10137#ifdef UNIX
10138 "unix",
10139#endif
10140#ifdef VMS
10141 "vms",
10142#endif
10143#ifdef WIN16
10144 "win16",
10145#endif
10146#ifdef WIN32
10147 "win32",
10148#endif
10149#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10150 "win32unix",
10151#endif
10152#ifdef WIN64
10153 "win64",
10154#endif
10155#ifdef EBCDIC
10156 "ebcdic",
10157#endif
10158#ifndef CASE_INSENSITIVE_FILENAME
10159 "fname_case",
10160#endif
10161#ifdef FEAT_ARABIC
10162 "arabic",
10163#endif
10164#ifdef FEAT_AUTOCMD
10165 "autocmd",
10166#endif
10167#ifdef FEAT_BEVAL
10168 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010169# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10170 "balloon_multiline",
10171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172#endif
10173#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10174 "builtin_terms",
10175# ifdef ALL_BUILTIN_TCAPS
10176 "all_builtin_terms",
10177# endif
10178#endif
10179#ifdef FEAT_BYTEOFF
10180 "byte_offset",
10181#endif
10182#ifdef FEAT_CINDENT
10183 "cindent",
10184#endif
10185#ifdef FEAT_CLIENTSERVER
10186 "clientserver",
10187#endif
10188#ifdef FEAT_CLIPBOARD
10189 "clipboard",
10190#endif
10191#ifdef FEAT_CMDL_COMPL
10192 "cmdline_compl",
10193#endif
10194#ifdef FEAT_CMDHIST
10195 "cmdline_hist",
10196#endif
10197#ifdef FEAT_COMMENTS
10198 "comments",
10199#endif
10200#ifdef FEAT_CRYPT
10201 "cryptv",
10202#endif
10203#ifdef FEAT_CSCOPE
10204 "cscope",
10205#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010206#ifdef CURSOR_SHAPE
10207 "cursorshape",
10208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209#ifdef DEBUG
10210 "debug",
10211#endif
10212#ifdef FEAT_CON_DIALOG
10213 "dialog_con",
10214#endif
10215#ifdef FEAT_GUI_DIALOG
10216 "dialog_gui",
10217#endif
10218#ifdef FEAT_DIFF
10219 "diff",
10220#endif
10221#ifdef FEAT_DIGRAPHS
10222 "digraphs",
10223#endif
10224#ifdef FEAT_DND
10225 "dnd",
10226#endif
10227#ifdef FEAT_EMACS_TAGS
10228 "emacs_tags",
10229#endif
10230 "eval", /* always present, of course! */
10231#ifdef FEAT_EX_EXTRA
10232 "ex_extra",
10233#endif
10234#ifdef FEAT_SEARCH_EXTRA
10235 "extra_search",
10236#endif
10237#ifdef FEAT_FKMAP
10238 "farsi",
10239#endif
10240#ifdef FEAT_SEARCHPATH
10241 "file_in_path",
10242#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010243#if defined(UNIX) && !defined(USE_SYSTEM)
10244 "filterpipe",
10245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246#ifdef FEAT_FIND_ID
10247 "find_in_path",
10248#endif
10249#ifdef FEAT_FOLDING
10250 "folding",
10251#endif
10252#ifdef FEAT_FOOTER
10253 "footer",
10254#endif
10255#if !defined(USE_SYSTEM) && defined(UNIX)
10256 "fork",
10257#endif
10258#ifdef FEAT_GETTEXT
10259 "gettext",
10260#endif
10261#ifdef FEAT_GUI
10262 "gui",
10263#endif
10264#ifdef FEAT_GUI_ATHENA
10265# ifdef FEAT_GUI_NEXTAW
10266 "gui_neXtaw",
10267# else
10268 "gui_athena",
10269# endif
10270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271#ifdef FEAT_GUI_GTK
10272 "gui_gtk",
10273# ifdef HAVE_GTK2
10274 "gui_gtk2",
10275# endif
10276#endif
10277#ifdef FEAT_GUI_MAC
10278 "gui_mac",
10279#endif
10280#ifdef FEAT_GUI_MOTIF
10281 "gui_motif",
10282#endif
10283#ifdef FEAT_GUI_PHOTON
10284 "gui_photon",
10285#endif
10286#ifdef FEAT_GUI_W16
10287 "gui_win16",
10288#endif
10289#ifdef FEAT_GUI_W32
10290 "gui_win32",
10291#endif
10292#ifdef FEAT_HANGULIN
10293 "hangul_input",
10294#endif
10295#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10296 "iconv",
10297#endif
10298#ifdef FEAT_INS_EXPAND
10299 "insert_expand",
10300#endif
10301#ifdef FEAT_JUMPLIST
10302 "jumplist",
10303#endif
10304#ifdef FEAT_KEYMAP
10305 "keymap",
10306#endif
10307#ifdef FEAT_LANGMAP
10308 "langmap",
10309#endif
10310#ifdef FEAT_LIBCALL
10311 "libcall",
10312#endif
10313#ifdef FEAT_LINEBREAK
10314 "linebreak",
10315#endif
10316#ifdef FEAT_LISP
10317 "lispindent",
10318#endif
10319#ifdef FEAT_LISTCMDS
10320 "listcmds",
10321#endif
10322#ifdef FEAT_LOCALMAP
10323 "localmap",
10324#endif
10325#ifdef FEAT_MENU
10326 "menu",
10327#endif
10328#ifdef FEAT_SESSION
10329 "mksession",
10330#endif
10331#ifdef FEAT_MODIFY_FNAME
10332 "modify_fname",
10333#endif
10334#ifdef FEAT_MOUSE
10335 "mouse",
10336#endif
10337#ifdef FEAT_MOUSESHAPE
10338 "mouseshape",
10339#endif
10340#if defined(UNIX) || defined(VMS)
10341# ifdef FEAT_MOUSE_DEC
10342 "mouse_dec",
10343# endif
10344# ifdef FEAT_MOUSE_GPM
10345 "mouse_gpm",
10346# endif
10347# ifdef FEAT_MOUSE_JSB
10348 "mouse_jsbterm",
10349# endif
10350# ifdef FEAT_MOUSE_NET
10351 "mouse_netterm",
10352# endif
10353# ifdef FEAT_MOUSE_PTERM
10354 "mouse_pterm",
10355# endif
10356# ifdef FEAT_MOUSE_XTERM
10357 "mouse_xterm",
10358# endif
10359#endif
10360#ifdef FEAT_MBYTE
10361 "multi_byte",
10362#endif
10363#ifdef FEAT_MBYTE_IME
10364 "multi_byte_ime",
10365#endif
10366#ifdef FEAT_MULTI_LANG
10367 "multi_lang",
10368#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010369#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010370#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010371 "mzscheme",
10372#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374#ifdef FEAT_OLE
10375 "ole",
10376#endif
10377#ifdef FEAT_OSFILETYPE
10378 "osfiletype",
10379#endif
10380#ifdef FEAT_PATH_EXTRA
10381 "path_extra",
10382#endif
10383#ifdef FEAT_PERL
10384#ifndef DYNAMIC_PERL
10385 "perl",
10386#endif
10387#endif
10388#ifdef FEAT_PYTHON
10389#ifndef DYNAMIC_PYTHON
10390 "python",
10391#endif
10392#endif
10393#ifdef FEAT_POSTSCRIPT
10394 "postscript",
10395#endif
10396#ifdef FEAT_PRINTER
10397 "printer",
10398#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010399#ifdef FEAT_PROFILE
10400 "profile",
10401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402#ifdef FEAT_QUICKFIX
10403 "quickfix",
10404#endif
10405#ifdef FEAT_RIGHTLEFT
10406 "rightleft",
10407#endif
10408#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10409 "ruby",
10410#endif
10411#ifdef FEAT_SCROLLBIND
10412 "scrollbind",
10413#endif
10414#ifdef FEAT_CMDL_INFO
10415 "showcmd",
10416 "cmdline_info",
10417#endif
10418#ifdef FEAT_SIGNS
10419 "signs",
10420#endif
10421#ifdef FEAT_SMARTINDENT
10422 "smartindent",
10423#endif
10424#ifdef FEAT_SNIFF
10425 "sniff",
10426#endif
10427#ifdef FEAT_STL_OPT
10428 "statusline",
10429#endif
10430#ifdef FEAT_SUN_WORKSHOP
10431 "sun_workshop",
10432#endif
10433#ifdef FEAT_NETBEANS_INTG
10434 "netbeans_intg",
10435#endif
10436#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010437 "spell",
10438#endif
10439#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 "syntax",
10441#endif
10442#if defined(USE_SYSTEM) || !defined(UNIX)
10443 "system",
10444#endif
10445#ifdef FEAT_TAG_BINS
10446 "tag_binary",
10447#endif
10448#ifdef FEAT_TAG_OLDSTATIC
10449 "tag_old_static",
10450#endif
10451#ifdef FEAT_TAG_ANYWHITE
10452 "tag_any_white",
10453#endif
10454#ifdef FEAT_TCL
10455# ifndef DYNAMIC_TCL
10456 "tcl",
10457# endif
10458#endif
10459#ifdef TERMINFO
10460 "terminfo",
10461#endif
10462#ifdef FEAT_TERMRESPONSE
10463 "termresponse",
10464#endif
10465#ifdef FEAT_TEXTOBJ
10466 "textobjects",
10467#endif
10468#ifdef HAVE_TGETENT
10469 "tgetent",
10470#endif
10471#ifdef FEAT_TITLE
10472 "title",
10473#endif
10474#ifdef FEAT_TOOLBAR
10475 "toolbar",
10476#endif
10477#ifdef FEAT_USR_CMDS
10478 "user-commands", /* was accidentally included in 5.4 */
10479 "user_commands",
10480#endif
10481#ifdef FEAT_VIMINFO
10482 "viminfo",
10483#endif
10484#ifdef FEAT_VERTSPLIT
10485 "vertsplit",
10486#endif
10487#ifdef FEAT_VIRTUALEDIT
10488 "virtualedit",
10489#endif
10490#ifdef FEAT_VISUAL
10491 "visual",
10492#endif
10493#ifdef FEAT_VISUALEXTRA
10494 "visualextra",
10495#endif
10496#ifdef FEAT_VREPLACE
10497 "vreplace",
10498#endif
10499#ifdef FEAT_WILDIGN
10500 "wildignore",
10501#endif
10502#ifdef FEAT_WILDMENU
10503 "wildmenu",
10504#endif
10505#ifdef FEAT_WINDOWS
10506 "windows",
10507#endif
10508#ifdef FEAT_WAK
10509 "winaltkeys",
10510#endif
10511#ifdef FEAT_WRITEBACKUP
10512 "writebackup",
10513#endif
10514#ifdef FEAT_XIM
10515 "xim",
10516#endif
10517#ifdef FEAT_XFONTSET
10518 "xfontset",
10519#endif
10520#ifdef USE_XSMP
10521 "xsmp",
10522#endif
10523#ifdef USE_XSMP_INTERACT
10524 "xsmp_interact",
10525#endif
10526#ifdef FEAT_XCLIPBOARD
10527 "xterm_clipboard",
10528#endif
10529#ifdef FEAT_XTERM_SAVE
10530 "xterm_save",
10531#endif
10532#if defined(UNIX) && defined(FEAT_X11)
10533 "X11",
10534#endif
10535 NULL
10536 };
10537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 for (i = 0; has_list[i] != NULL; ++i)
10540 if (STRICMP(name, has_list[i]) == 0)
10541 {
10542 n = TRUE;
10543 break;
10544 }
10545
10546 if (n == FALSE)
10547 {
10548 if (STRNICMP(name, "patch", 5) == 0)
10549 n = has_patch(atoi((char *)name + 5));
10550 else if (STRICMP(name, "vim_starting") == 0)
10551 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010552#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10553 else if (STRICMP(name, "balloon_multiline") == 0)
10554 n = multiline_balloon_available();
10555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556#ifdef DYNAMIC_TCL
10557 else if (STRICMP(name, "tcl") == 0)
10558 n = tcl_enabled(FALSE);
10559#endif
10560#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10561 else if (STRICMP(name, "iconv") == 0)
10562 n = iconv_enabled(FALSE);
10563#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010564#ifdef DYNAMIC_MZSCHEME
10565 else if (STRICMP(name, "mzscheme") == 0)
10566 n = mzscheme_enabled(FALSE);
10567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568#ifdef DYNAMIC_RUBY
10569 else if (STRICMP(name, "ruby") == 0)
10570 n = ruby_enabled(FALSE);
10571#endif
10572#ifdef DYNAMIC_PYTHON
10573 else if (STRICMP(name, "python") == 0)
10574 n = python_enabled(FALSE);
10575#endif
10576#ifdef DYNAMIC_PERL
10577 else if (STRICMP(name, "perl") == 0)
10578 n = perl_enabled(FALSE);
10579#endif
10580#ifdef FEAT_GUI
10581 else if (STRICMP(name, "gui_running") == 0)
10582 n = (gui.in_use || gui.starting);
10583# ifdef FEAT_GUI_W32
10584 else if (STRICMP(name, "gui_win32s") == 0)
10585 n = gui_is_win32s();
10586# endif
10587# ifdef FEAT_BROWSE
10588 else if (STRICMP(name, "browse") == 0)
10589 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10590# endif
10591#endif
10592#ifdef FEAT_SYN_HL
10593 else if (STRICMP(name, "syntax_items") == 0)
10594 n = syntax_present(curbuf);
10595#endif
10596#if defined(WIN3264)
10597 else if (STRICMP(name, "win95") == 0)
10598 n = mch_windows95();
10599#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010600#ifdef FEAT_NETBEANS_INTG
10601 else if (STRICMP(name, "netbeans_enabled") == 0)
10602 n = usingNetbeans;
10603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 }
10605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010606 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607}
10608
10609/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610 * "has_key()" function
10611 */
10612 static void
10613f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010614 typval_T *argvars;
10615 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616{
10617 rettv->vval.v_number = 0;
10618 if (argvars[0].v_type != VAR_DICT)
10619 {
10620 EMSG(_(e_dictreq));
10621 return;
10622 }
10623 if (argvars[0].vval.v_dict == NULL)
10624 return;
10625
10626 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010627 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010628}
10629
10630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 * "hasmapto()" function
10632 */
10633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010635 typval_T *argvars;
10636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
10638 char_u *name;
10639 char_u *mode;
10640 char_u buf[NUMBUFLEN];
10641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010643 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 mode = (char_u *)"nvo";
10645 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010646 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647
10648 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010649 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010651 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652}
10653
10654/*
10655 * "histadd()" function
10656 */
10657/*ARGSUSED*/
10658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010660 typval_T *argvars;
10661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662{
10663#ifdef FEAT_CMDHIST
10664 int histype;
10665 char_u *str;
10666 char_u buf[NUMBUFLEN];
10667#endif
10668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 if (check_restricted() || check_secure())
10671 return;
10672#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010673 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10674 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 if (histype >= 0)
10676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010677 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 if (*str != NUL)
10679 {
10680 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 return;
10683 }
10684 }
10685#endif
10686}
10687
10688/*
10689 * "histdel()" function
10690 */
10691/*ARGSUSED*/
10692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010694 typval_T *argvars;
10695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696{
10697#ifdef FEAT_CMDHIST
10698 int n;
10699 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010702 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10703 if (str == NULL)
10704 n = 0;
10705 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010708 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 else
10713 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010714 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 get_tv_string_buf(&argvars[1], buf));
10716 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719#endif
10720}
10721
10722/*
10723 * "histget()" function
10724 */
10725/*ARGSUSED*/
10726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010728 typval_T *argvars;
10729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730{
10731#ifdef FEAT_CMDHIST
10732 int type;
10733 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10737 if (str == NULL)
10738 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010740 {
10741 type = get_histtype(str);
10742 if (argvars[1].v_type == VAR_UNKNOWN)
10743 idx = get_history_idx(type);
10744 else
10745 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10746 /* -1 on type error */
10747 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753}
10754
10755/*
10756 * "histnr()" function
10757 */
10758/*ARGSUSED*/
10759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010761 typval_T *argvars;
10762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763{
10764 int i;
10765
10766#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010767 char_u *history = get_tv_string_chk(&argvars[0]);
10768
10769 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 if (i >= HIST_CMD && i < HIST_COUNT)
10771 i = get_history_idx(i);
10772 else
10773#endif
10774 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776}
10777
10778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 * "highlightID(name)" function
10780 */
10781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010783 typval_T *argvars;
10784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010786 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787}
10788
10789/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010790 * "highlight_exists()" function
10791 */
10792 static void
10793f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010794 typval_T *argvars;
10795 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010796{
10797 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10798}
10799
10800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 * "hostname()" function
10802 */
10803/*ARGSUSED*/
10804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010806 typval_T *argvars;
10807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808{
10809 char_u hostname[256];
10810
10811 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010812 rettv->v_type = VAR_STRING;
10813 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814}
10815
10816/*
10817 * iconv() function
10818 */
10819/*ARGSUSED*/
10820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821f_iconv(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_MBYTE
10826 char_u buf1[NUMBUFLEN];
10827 char_u buf2[NUMBUFLEN];
10828 char_u *from, *to, *str;
10829 vimconv_T vimconv;
10830#endif
10831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010832 rettv->v_type = VAR_STRING;
10833 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834
10835#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 str = get_tv_string(&argvars[0]);
10837 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10838 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 vimconv.vc_type = CONV_NONE;
10840 convert_setup(&vimconv, from, to);
10841
10842 /* If the encodings are equal, no conversion needed. */
10843 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847
10848 convert_setup(&vimconv, NULL, NULL);
10849 vim_free(from);
10850 vim_free(to);
10851#endif
10852}
10853
10854/*
10855 * "indent()" function
10856 */
10857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010859 typval_T *argvars;
10860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861{
10862 linenr_T lnum;
10863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010864 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869}
10870
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010871/*
10872 * "index()" function
10873 */
10874 static void
10875f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010876 typval_T *argvars;
10877 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010878{
Bram Moolenaar33570922005-01-25 22:26:29 +000010879 list_T *l;
10880 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010881 long idx = 0;
10882 int ic = FALSE;
10883
10884 rettv->vval.v_number = -1;
10885 if (argvars[0].v_type != VAR_LIST)
10886 {
10887 EMSG(_(e_listreq));
10888 return;
10889 }
10890 l = argvars[0].vval.v_list;
10891 if (l != NULL)
10892 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010893 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010894 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896 int error = FALSE;
10897
Bram Moolenaar758711c2005-02-02 23:11:38 +000010898 /* Start at specified item. Use the cached index that list_find()
10899 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010901 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010902 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 ic = get_tv_number_chk(&argvars[3], &error);
10904 if (error)
10905 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010906 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010907
Bram Moolenaar758711c2005-02-02 23:11:38 +000010908 for ( ; item != NULL; item = item->li_next, ++idx)
10909 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010910 {
10911 rettv->vval.v_number = idx;
10912 break;
10913 }
10914 }
10915}
10916
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917static int inputsecret_flag = 0;
10918
10919/*
10920 * "input()" function
10921 * Also handles inputsecret() when inputsecret is set.
10922 */
10923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010925 typval_T *argvars;
10926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010928 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 char_u *p = NULL;
10930 int c;
10931 char_u buf[NUMBUFLEN];
10932 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010934 int xp_type = EXPAND_NOTHING;
10935 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938
10939#ifdef NO_CONSOLE_INPUT
10940 /* While starting up, there is no place to enter text. */
10941 if (no_console_input())
10942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 return;
10945 }
10946#endif
10947
10948 cmd_silent = FALSE; /* Want to see the prompt. */
10949 if (prompt != NULL)
10950 {
10951 /* Only the part of the message after the last NL is considered as
10952 * prompt for the command line */
10953 p = vim_strrchr(prompt, '\n');
10954 if (p == NULL)
10955 p = prompt;
10956 else
10957 {
10958 ++p;
10959 c = *p;
10960 *p = NUL;
10961 msg_start();
10962 msg_clr_eos();
10963 msg_puts_attr(prompt, echo_attr);
10964 msg_didout = FALSE;
10965 msg_starthere();
10966 *p = c;
10967 }
10968 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 if (argvars[1].v_type != VAR_UNKNOWN)
10971 {
10972 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10973 if (defstr != NULL)
10974 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975
Bram Moolenaar4463f292005-09-25 22:20:24 +000010976 if (argvars[2].v_type != VAR_UNKNOWN)
10977 {
10978 char_u *xp_name;
10979 int xp_namelen;
10980 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010981
Bram Moolenaar4463f292005-09-25 22:20:24 +000010982 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010983
Bram Moolenaar4463f292005-09-25 22:20:24 +000010984 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10985 if (xp_name == NULL)
10986 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010987
Bram Moolenaar4463f292005-09-25 22:20:24 +000010988 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010989
Bram Moolenaar4463f292005-09-25 22:20:24 +000010990 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10991 &xp_arg) == FAIL)
10992 return;
10993 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010994 }
10995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 if (defstr != NULL)
10997 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010998 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10999 xp_type, xp_arg);
11000
11001 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011003 /* since the user typed this, no need to wait for return */
11004 need_wait_return = FALSE;
11005 msg_didout = FALSE;
11006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 cmd_silent = cmd_silent_save;
11008}
11009
11010/*
11011 * "inputdialog()" function
11012 */
11013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011015 typval_T *argvars;
11016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
11018#if defined(FEAT_GUI_TEXTDIALOG)
11019 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11020 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11021 {
11022 char_u *message;
11023 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011026 message = get_tv_string_chk(&argvars[0]);
11027 if (argvars[1].v_type != VAR_UNKNOWN
11028 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011029 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 else
11031 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 if (message != NULL && defstr != NULL
11033 && do_dialog(VIM_QUESTION, NULL, message,
11034 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 else
11037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038 if (message != NULL && defstr != NULL
11039 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011040 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->vval.v_string = vim_strsave(
11042 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 }
11048 else
11049#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051}
11052
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011053/*
11054 * "inputlist()" function
11055 */
11056 static void
11057f_inputlist(argvars, rettv)
11058 typval_T *argvars;
11059 typval_T *rettv;
11060{
11061 listitem_T *li;
11062 int selected;
11063 int mouse_used;
11064
11065 rettv->vval.v_number = 0;
11066#ifdef NO_CONSOLE_INPUT
11067 /* While starting up, there is no place to enter text. */
11068 if (no_console_input())
11069 return;
11070#endif
11071 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11072 {
11073 EMSG2(_(e_listarg), "inputlist()");
11074 return;
11075 }
11076
11077 msg_start();
11078 lines_left = Rows; /* avoid more prompt */
11079 msg_scroll = TRUE;
11080 msg_clr_eos();
11081
11082 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11083 {
11084 msg_puts(get_tv_string(&li->li_tv));
11085 msg_putchar('\n');
11086 }
11087
11088 /* Ask for choice. */
11089 selected = prompt_for_number(&mouse_used);
11090 if (mouse_used)
11091 selected -= lines_left;
11092
11093 rettv->vval.v_number = selected;
11094}
11095
11096
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11098
11099/*
11100 * "inputrestore()" function
11101 */
11102/*ARGSUSED*/
11103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011105 typval_T *argvars;
11106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107{
11108 if (ga_userinput.ga_len > 0)
11109 {
11110 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11112 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 }
11115 else if (p_verbose > 1)
11116 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011117 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011118 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 }
11120}
11121
11122/*
11123 * "inputsave()" function
11124 */
11125/*ARGSUSED*/
11126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *argvars;
11129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
11131 /* Add an entry to the stack of typehead storage. */
11132 if (ga_grow(&ga_userinput, 1) == OK)
11133 {
11134 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11135 + ga_userinput.ga_len);
11136 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 }
11139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141}
11142
11143/*
11144 * "inputsecret()" function
11145 */
11146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011148 typval_T *argvars;
11149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150{
11151 ++cmdline_star;
11152 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 --cmdline_star;
11155 --inputsecret_flag;
11156}
11157
11158/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011159 * "insert()" function
11160 */
11161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011163 typval_T *argvars;
11164 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011165{
11166 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 listitem_T *item;
11168 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011170
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011171 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011172 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011173 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011174 else if ((l = argvars[0].vval.v_list) != NULL
11175 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011176 {
11177 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 before = get_tv_number_chk(&argvars[2], &error);
11179 if (error)
11180 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011181
Bram Moolenaar758711c2005-02-02 23:11:38 +000011182 if (before == l->lv_len)
11183 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011184 else
11185 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011186 item = list_find(l, before);
11187 if (item == NULL)
11188 {
11189 EMSGN(_(e_listidx), before);
11190 l = NULL;
11191 }
11192 }
11193 if (l != NULL)
11194 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011195 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011196 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011197 }
11198 }
11199}
11200
11201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 * "isdirectory()" function
11203 */
11204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011206 typval_T *argvars;
11207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210}
11211
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011212/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011213 * "islocked()" function
11214 */
11215 static void
11216f_islocked(argvars, rettv)
11217 typval_T *argvars;
11218 typval_T *rettv;
11219{
11220 lval_T lv;
11221 char_u *end;
11222 dictitem_T *di;
11223
11224 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011225 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11226 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011227 if (end != NULL && lv.ll_name != NULL)
11228 {
11229 if (*end != NUL)
11230 EMSG(_(e_trailing));
11231 else
11232 {
11233 if (lv.ll_tv == NULL)
11234 {
11235 if (check_changedtick(lv.ll_name))
11236 rettv->vval.v_number = 1; /* always locked */
11237 else
11238 {
11239 di = find_var(lv.ll_name, NULL);
11240 if (di != NULL)
11241 {
11242 /* Consider a variable locked when:
11243 * 1. the variable itself is locked
11244 * 2. the value of the variable is locked.
11245 * 3. the List or Dict value is locked.
11246 */
11247 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11248 || tv_islocked(&di->di_tv));
11249 }
11250 }
11251 }
11252 else if (lv.ll_range)
11253 EMSG(_("E745: Range not allowed"));
11254 else if (lv.ll_newkey != NULL)
11255 EMSG2(_(e_dictkey), lv.ll_newkey);
11256 else if (lv.ll_list != NULL)
11257 /* List item. */
11258 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11259 else
11260 /* Dictionary item. */
11261 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11262 }
11263 }
11264
11265 clear_lval(&lv);
11266}
11267
Bram Moolenaar33570922005-01-25 22:26:29 +000011268static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011269
11270/*
11271 * Turn a dict into a list:
11272 * "what" == 0: list of keys
11273 * "what" == 1: list of values
11274 * "what" == 2: list of items
11275 */
11276 static void
11277dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011278 typval_T *argvars;
11279 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011280 int what;
11281{
Bram Moolenaar33570922005-01-25 22:26:29 +000011282 list_T *l;
11283 list_T *l2;
11284 dictitem_T *di;
11285 hashitem_T *hi;
11286 listitem_T *li;
11287 listitem_T *li2;
11288 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011289 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011290
11291 rettv->vval.v_number = 0;
11292 if (argvars[0].v_type != VAR_DICT)
11293 {
11294 EMSG(_(e_dictreq));
11295 return;
11296 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011297 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011298 return;
11299
11300 l = list_alloc();
11301 if (l == NULL)
11302 return;
11303 rettv->v_type = VAR_LIST;
11304 rettv->vval.v_list = l;
11305 ++l->lv_refcount;
11306
Bram Moolenaar33570922005-01-25 22:26:29 +000011307 todo = d->dv_hashtab.ht_used;
11308 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011310 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011312 --todo;
11313 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011314
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011315 li = listitem_alloc();
11316 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011317 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011318 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011319
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011320 if (what == 0)
11321 {
11322 /* keys() */
11323 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011324 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011325 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11326 }
11327 else if (what == 1)
11328 {
11329 /* values() */
11330 copy_tv(&di->di_tv, &li->li_tv);
11331 }
11332 else
11333 {
11334 /* items() */
11335 l2 = list_alloc();
11336 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011337 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011338 li->li_tv.vval.v_list = l2;
11339 if (l2 == NULL)
11340 break;
11341 ++l2->lv_refcount;
11342
11343 li2 = listitem_alloc();
11344 if (li2 == NULL)
11345 break;
11346 list_append(l2, li2);
11347 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011348 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011349 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11350
11351 li2 = listitem_alloc();
11352 if (li2 == NULL)
11353 break;
11354 list_append(l2, li2);
11355 copy_tv(&di->di_tv, &li2->li_tv);
11356 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011357 }
11358 }
11359}
11360
11361/*
11362 * "items(dict)" function
11363 */
11364 static void
11365f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011366 typval_T *argvars;
11367 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011368{
11369 dict_list(argvars, rettv, 2);
11370}
11371
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011373 * "join()" function
11374 */
11375 static void
11376f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 typval_T *argvars;
11378 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011379{
11380 garray_T ga;
11381 char_u *sep;
11382
11383 rettv->vval.v_number = 0;
11384 if (argvars[0].v_type != VAR_LIST)
11385 {
11386 EMSG(_(e_listreq));
11387 return;
11388 }
11389 if (argvars[0].vval.v_list == NULL)
11390 return;
11391 if (argvars[1].v_type == VAR_UNKNOWN)
11392 sep = (char_u *)" ";
11393 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011394 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011395
11396 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397
11398 if (sep != NULL)
11399 {
11400 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011401 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 ga_append(&ga, NUL);
11403 rettv->vval.v_string = (char_u *)ga.ga_data;
11404 }
11405 else
11406 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011407}
11408
11409/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011410 * "keys()" function
11411 */
11412 static void
11413f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 typval_T *argvars;
11415 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011416{
11417 dict_list(argvars, rettv, 0);
11418}
11419
11420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 * "last_buffer_nr()" function.
11422 */
11423/*ARGSUSED*/
11424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011426 typval_T *argvars;
11427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428{
11429 int n = 0;
11430 buf_T *buf;
11431
11432 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11433 if (n < buf->b_fnum)
11434 n = buf->b_fnum;
11435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011437}
11438
11439/*
11440 * "len()" function
11441 */
11442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011444 typval_T *argvars;
11445 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011446{
11447 switch (argvars[0].v_type)
11448 {
11449 case VAR_STRING:
11450 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451 rettv->vval.v_number = (varnumber_T)STRLEN(
11452 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011453 break;
11454 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011456 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011457 case VAR_DICT:
11458 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11459 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011460 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011461 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011462 break;
11463 }
11464}
11465
Bram Moolenaar33570922005-01-25 22:26:29 +000011466static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011467
11468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011472 int type;
11473{
11474#ifdef FEAT_LIBCALL
11475 char_u *string_in;
11476 char_u **string_result;
11477 int nr_result;
11478#endif
11479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011481 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011485
11486 if (check_restricted() || check_secure())
11487 return;
11488
11489#ifdef FEAT_LIBCALL
11490 /* The first two args must be strings, otherwise its meaningless */
11491 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11492 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011493 string_in = NULL;
11494 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011495 string_in = argvars[2].vval.v_string;
11496 if (type == VAR_NUMBER)
11497 string_result = NULL;
11498 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011500 if (mch_libcall(argvars[0].vval.v_string,
11501 argvars[1].vval.v_string,
11502 string_in,
11503 argvars[2].vval.v_number,
11504 string_result,
11505 &nr_result) == OK
11506 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011508 }
11509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510}
11511
11512/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011513 * "libcall()" function
11514 */
11515 static void
11516f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011517 typval_T *argvars;
11518 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011519{
11520 libcall_common(argvars, rettv, VAR_STRING);
11521}
11522
11523/*
11524 * "libcallnr()" function
11525 */
11526 static void
11527f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 typval_T *argvars;
11529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011530{
11531 libcall_common(argvars, rettv, VAR_NUMBER);
11532}
11533
11534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535 * "line(string)" function
11536 */
11537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011539 typval_T *argvars;
11540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541{
11542 linenr_T lnum = 0;
11543 pos_T *fp;
11544
11545 fp = var2fpos(&argvars[0], TRUE);
11546 if (fp != NULL)
11547 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549}
11550
11551/*
11552 * "line2byte(lnum)" function
11553 */
11554/*ARGSUSED*/
11555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011557 typval_T *argvars;
11558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559{
11560#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562#else
11563 linenr_T lnum;
11564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11570 if (rettv->vval.v_number >= 0)
11571 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572#endif
11573}
11574
11575/*
11576 * "lispindent(lnum)" function
11577 */
11578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011580 typval_T *argvars;
11581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582{
11583#ifdef FEAT_LISP
11584 pos_T pos;
11585 linenr_T lnum;
11586
11587 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11590 {
11591 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 curwin->w_cursor = pos;
11594 }
11595 else
11596#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598}
11599
11600/*
11601 * "localtime()" function
11602 */
11603/*ARGSUSED*/
11604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011606 typval_T *argvars;
11607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610}
11611
Bram Moolenaar33570922005-01-25 22:26:29 +000011612static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613
11614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 int exact;
11619{
11620 char_u *keys;
11621 char_u *which;
11622 char_u buf[NUMBUFLEN];
11623 char_u *keys_buf = NULL;
11624 char_u *rhs;
11625 int mode;
11626 garray_T ga;
11627
11628 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629 rettv->v_type = VAR_STRING;
11630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 if (*keys == NUL)
11634 return;
11635
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011636 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011637 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 else
11639 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011640 if (which == NULL)
11641 return;
11642
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 mode = get_map_mode(&which, 0);
11644
11645 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011646 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 vim_free(keys_buf);
11648 if (rhs != NULL)
11649 {
11650 ga_init(&ga);
11651 ga.ga_itemsize = 1;
11652 ga.ga_growsize = 40;
11653
11654 while (*rhs != NUL)
11655 ga_concat(&ga, str2special(&rhs, FALSE));
11656
11657 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 }
11660}
11661
11662/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011663 * "map()" function
11664 */
11665 static void
11666f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011667 typval_T *argvars;
11668 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011669{
11670 filter_map(argvars, rettv, TRUE);
11671}
11672
11673/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011674 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 */
11676 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011677f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 typval_T *argvars;
11679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011681 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682}
11683
11684/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 */
11687 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011688f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011689 typval_T *argvars;
11690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011692 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693}
11694
Bram Moolenaar33570922005-01-25 22:26:29 +000011695static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696
11697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011698find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011699 typval_T *argvars;
11700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 int type;
11702{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011703 char_u *str = NULL;
11704 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 char_u *pat;
11706 regmatch_T regmatch;
11707 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011708 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 char_u *save_cpo;
11710 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011711 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011712 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011713 list_T *l = NULL;
11714 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011715 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011716 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717
11718 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11719 save_cpo = p_cpo;
11720 p_cpo = (char_u *)"";
11721
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011722 rettv->vval.v_number = -1;
11723 if (type == 3)
11724 {
11725 /* return empty list when there are no matches */
11726 if ((rettv->vval.v_list = list_alloc()) == NULL)
11727 goto theend;
11728 rettv->v_type = VAR_LIST;
11729 ++rettv->vval.v_list->lv_refcount;
11730 }
11731 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 rettv->v_type = VAR_STRING;
11734 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011737 if (argvars[0].v_type == VAR_LIST)
11738 {
11739 if ((l = argvars[0].vval.v_list) == NULL)
11740 goto theend;
11741 li = l->lv_first;
11742 }
11743 else
11744 expr = str = get_tv_string(&argvars[0]);
11745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011746 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11747 if (pat == NULL)
11748 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011749
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011750 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011752 int error = FALSE;
11753
11754 start = get_tv_number_chk(&argvars[2], &error);
11755 if (error)
11756 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011757 if (l != NULL)
11758 {
11759 li = list_find(l, start);
11760 if (li == NULL)
11761 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011762 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011763 }
11764 else
11765 {
11766 if (start < 0)
11767 start = 0;
11768 if (start > (long)STRLEN(str))
11769 goto theend;
11770 str += start;
11771 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011772
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011773 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 nth = get_tv_number_chk(&argvars[3], &error);
11775 if (error)
11776 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777 }
11778
11779 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11780 if (regmatch.regprog != NULL)
11781 {
11782 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011783
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011784 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011785 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011786 if (l != NULL)
11787 {
11788 if (li == NULL)
11789 {
11790 match = FALSE;
11791 break;
11792 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011793 vim_free(tofree);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011794 str = echo_string(&li->li_tv, &tofree, strbuf,0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011795 if (str == NULL)
11796 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011797 }
11798
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011799 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011800
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011801 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011802 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011803 if (l == NULL && !match)
11804 break;
11805
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011806 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011807 if (l != NULL)
11808 {
11809 li = li->li_next;
11810 ++idx;
11811 }
11812 else
11813 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011814#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011815 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011816#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011817 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011818#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011819 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011820 }
11821
11822 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011824 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011826 int i;
11827
11828 /* return list with matched string and submatches */
11829 for (i = 0; i < NSUBEXP; ++i)
11830 {
11831 if (regmatch.endp[i] == NULL)
11832 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011833 if (list_append_string(rettv->vval.v_list,
11834 regmatch.startp[i],
11835 (int)(regmatch.endp[i] - regmatch.startp[i]))
11836 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011837 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011838 }
11839 }
11840 else if (type == 2)
11841 {
11842 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011843 if (l != NULL)
11844 copy_tv(&li->li_tv, rettv);
11845 else
11846 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011848 }
11849 else if (l != NULL)
11850 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 else
11852 {
11853 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 (varnumber_T)(regmatch.startp[0] - str);
11856 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 }
11861 }
11862 vim_free(regmatch.regprog);
11863 }
11864
11865theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011866 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867 p_cpo = save_cpo;
11868}
11869
11870/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011871 * "match()" function
11872 */
11873 static void
11874f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011875 typval_T *argvars;
11876 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011877{
11878 find_some_match(argvars, rettv, 1);
11879}
11880
11881/*
11882 * "matchend()" function
11883 */
11884 static void
11885f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011886 typval_T *argvars;
11887 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888{
11889 find_some_match(argvars, rettv, 0);
11890}
11891
11892/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011893 * "matchlist()" function
11894 */
11895 static void
11896f_matchlist(argvars, rettv)
11897 typval_T *argvars;
11898 typval_T *rettv;
11899{
11900 find_some_match(argvars, rettv, 3);
11901}
11902
11903/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904 * "matchstr()" function
11905 */
11906 static void
11907f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 typval_T *argvars;
11909 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011910{
11911 find_some_match(argvars, rettv, 2);
11912}
11913
Bram Moolenaar33570922005-01-25 22:26:29 +000011914static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011915
11916 static void
11917max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011918 typval_T *argvars;
11919 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011920 int domax;
11921{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011922 long n = 0;
11923 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011925
11926 if (argvars[0].v_type == VAR_LIST)
11927 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011928 list_T *l;
11929 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011930
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011931 l = argvars[0].vval.v_list;
11932 if (l != NULL)
11933 {
11934 li = l->lv_first;
11935 if (li != NULL)
11936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011937 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011938 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011939 {
11940 li = li->li_next;
11941 if (li == NULL)
11942 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011943 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011944 if (domax ? i > n : i < n)
11945 n = i;
11946 }
11947 }
11948 }
11949 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011950 else if (argvars[0].v_type == VAR_DICT)
11951 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011953 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011954 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011955 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011956
11957 d = argvars[0].vval.v_dict;
11958 if (d != NULL)
11959 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011960 todo = d->dv_hashtab.ht_used;
11961 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011962 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011963 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011964 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011965 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011967 if (first)
11968 {
11969 n = i;
11970 first = FALSE;
11971 }
11972 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011973 n = i;
11974 }
11975 }
11976 }
11977 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011978 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011979 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011981}
11982
11983/*
11984 * "max()" function
11985 */
11986 static void
11987f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011988 typval_T *argvars;
11989 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011990{
11991 max_min(argvars, rettv, TRUE);
11992}
11993
11994/*
11995 * "min()" function
11996 */
11997 static void
11998f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 typval_T *argvars;
12000 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012001{
12002 max_min(argvars, rettv, FALSE);
12003}
12004
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012005static int mkdir_recurse __ARGS((char_u *dir, int prot));
12006
12007/*
12008 * Create the directory in which "dir" is located, and higher levels when
12009 * needed.
12010 */
12011 static int
12012mkdir_recurse(dir, prot)
12013 char_u *dir;
12014 int prot;
12015{
12016 char_u *p;
12017 char_u *updir;
12018 int r = FAIL;
12019
12020 /* Get end of directory name in "dir".
12021 * We're done when it's "/" or "c:/". */
12022 p = gettail_sep(dir);
12023 if (p <= get_past_head(dir))
12024 return OK;
12025
12026 /* If the directory exists we're done. Otherwise: create it.*/
12027 updir = vim_strnsave(dir, (int)(p - dir));
12028 if (updir == NULL)
12029 return FAIL;
12030 if (mch_isdir(updir))
12031 r = OK;
12032 else if (mkdir_recurse(updir, prot) == OK)
12033 r = vim_mkdir_emsg(updir, prot);
12034 vim_free(updir);
12035 return r;
12036}
12037
12038#ifdef vim_mkdir
12039/*
12040 * "mkdir()" function
12041 */
12042 static void
12043f_mkdir(argvars, rettv)
12044 typval_T *argvars;
12045 typval_T *rettv;
12046{
12047 char_u *dir;
12048 char_u buf[NUMBUFLEN];
12049 int prot = 0755;
12050
12051 rettv->vval.v_number = FAIL;
12052 if (check_restricted() || check_secure())
12053 return;
12054
12055 dir = get_tv_string_buf(&argvars[0], buf);
12056 if (argvars[1].v_type != VAR_UNKNOWN)
12057 {
12058 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 prot = get_tv_number_chk(&argvars[2], NULL);
12060 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012061 mkdir_recurse(dir, prot);
12062 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012063 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012064}
12065#endif
12066
Bram Moolenaar0d660222005-01-07 21:51:51 +000012067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 * "mode()" function
12069 */
12070/*ARGSUSED*/
12071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012073 typval_T *argvars;
12074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075{
12076 char_u buf[2];
12077
12078#ifdef FEAT_VISUAL
12079 if (VIsual_active)
12080 {
12081 if (VIsual_select)
12082 buf[0] = VIsual_mode + 's' - 'v';
12083 else
12084 buf[0] = VIsual_mode;
12085 }
12086 else
12087#endif
12088 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12089 buf[0] = 'r';
12090 else if (State & INSERT)
12091 {
12092 if (State & REPLACE_FLAG)
12093 buf[0] = 'R';
12094 else
12095 buf[0] = 'i';
12096 }
12097 else if (State & CMDLINE)
12098 buf[0] = 'c';
12099 else
12100 buf[0] = 'n';
12101
12102 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 rettv->vval.v_string = vim_strsave(buf);
12104 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105}
12106
12107/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108 * "nextnonblank()" function
12109 */
12110 static void
12111f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012112 typval_T *argvars;
12113 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012114{
12115 linenr_T lnum;
12116
12117 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012119 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012120 {
12121 lnum = 0;
12122 break;
12123 }
12124 if (*skipwhite(ml_get(lnum)) != NUL)
12125 break;
12126 }
12127 rettv->vval.v_number = lnum;
12128}
12129
12130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 * "nr2char()" function
12132 */
12133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012135 typval_T *argvars;
12136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137{
12138 char_u buf[NUMBUFLEN];
12139
12140#ifdef FEAT_MBYTE
12141 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 else
12144#endif
12145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 buf[1] = NUL;
12148 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 rettv->v_type = VAR_STRING;
12150 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012151}
12152
12153/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012154 * "prevnonblank()" function
12155 */
12156 static void
12157f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012158 typval_T *argvars;
12159 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012160{
12161 linenr_T lnum;
12162
12163 lnum = get_tv_lnum(argvars);
12164 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12165 lnum = 0;
12166 else
12167 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12168 --lnum;
12169 rettv->vval.v_number = lnum;
12170}
12171
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012172#ifdef HAVE_STDARG_H
12173/* This dummy va_list is here because:
12174 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12175 * - locally in the function results in a "used before set" warning
12176 * - using va_start() to initialize it gives "function with fixed args" error */
12177static va_list ap;
12178#endif
12179
Bram Moolenaar8c711452005-01-14 21:53:12 +000012180/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012181 * "printf()" function
12182 */
12183 static void
12184f_printf(argvars, rettv)
12185 typval_T *argvars;
12186 typval_T *rettv;
12187{
12188 rettv->v_type = VAR_STRING;
12189 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012190#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012191 {
12192 char_u buf[NUMBUFLEN];
12193 int len;
12194 char_u *s;
12195 int saved_did_emsg = did_emsg;
12196 char *fmt;
12197
12198 /* Get the required length, allocate the buffer and do it for real. */
12199 did_emsg = FALSE;
12200 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012201 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012202 if (!did_emsg)
12203 {
12204 s = alloc(len + 1);
12205 if (s != NULL)
12206 {
12207 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012208 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012209 }
12210 }
12211 did_emsg |= saved_did_emsg;
12212 }
12213#endif
12214}
12215
12216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012217 * "range()" function
12218 */
12219 static void
12220f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012221 typval_T *argvars;
12222 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012223{
12224 long start;
12225 long end;
12226 long stride = 1;
12227 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012228 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012229 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012231 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012232 if (argvars[1].v_type == VAR_UNKNOWN)
12233 {
12234 end = start - 1;
12235 start = 0;
12236 }
12237 else
12238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012239 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012240 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012241 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012242 }
12243
12244 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012245 if (error)
12246 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012247 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012248 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012249 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012250 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012251 else
12252 {
12253 l = list_alloc();
12254 if (l != NULL)
12255 {
12256 rettv->v_type = VAR_LIST;
12257 rettv->vval.v_list = l;
12258 ++l->lv_refcount;
12259
12260 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012261 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012262 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012263 }
12264 }
12265}
12266
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012267/*
12268 * "readfile()" function
12269 */
12270 static void
12271f_readfile(argvars, rettv)
12272 typval_T *argvars;
12273 typval_T *rettv;
12274{
12275 int binary = FALSE;
12276 char_u *fname;
12277 FILE *fd;
12278 list_T *l;
12279 listitem_T *li;
12280#define FREAD_SIZE 200 /* optimized for text lines */
12281 char_u buf[FREAD_SIZE];
12282 int readlen; /* size of last fread() */
12283 int buflen; /* nr of valid chars in buf[] */
12284 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12285 int tolist; /* first byte in buf[] still to be put in list */
12286 int chop; /* how many CR to chop off */
12287 char_u *prev = NULL; /* previously read bytes, if any */
12288 int prevlen = 0; /* length of "prev" if not NULL */
12289 char_u *s;
12290 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012291 long maxline = MAXLNUM;
12292 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012293
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012294 if (argvars[1].v_type != VAR_UNKNOWN)
12295 {
12296 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12297 binary = TRUE;
12298 if (argvars[2].v_type != VAR_UNKNOWN)
12299 maxline = get_tv_number(&argvars[2]);
12300 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012301
12302 l = list_alloc();
12303 if (l == NULL)
12304 return;
12305 rettv->v_type = VAR_LIST;
12306 rettv->vval.v_list = l;
12307 l->lv_refcount = 1;
12308
12309 /* Always open the file in binary mode, library functions have a mind of
12310 * their own about CR-LF conversion. */
12311 fname = get_tv_string(&argvars[0]);
12312 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12313 {
12314 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12315 return;
12316 }
12317
12318 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012319 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012320 {
12321 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12322 buflen = filtd + readlen;
12323 tolist = 0;
12324 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12325 {
12326 if (buf[filtd] == '\n' || readlen <= 0)
12327 {
12328 /* Only when in binary mode add an empty list item when the
12329 * last line ends in a '\n'. */
12330 if (!binary && readlen == 0 && filtd == 0)
12331 break;
12332
12333 /* Found end-of-line or end-of-file: add a text line to the
12334 * list. */
12335 chop = 0;
12336 if (!binary)
12337 while (filtd - chop - 1 >= tolist
12338 && buf[filtd - chop - 1] == '\r')
12339 ++chop;
12340 len = filtd - tolist - chop;
12341 if (prev == NULL)
12342 s = vim_strnsave(buf + tolist, len);
12343 else
12344 {
12345 s = alloc((unsigned)(prevlen + len + 1));
12346 if (s != NULL)
12347 {
12348 mch_memmove(s, prev, prevlen);
12349 vim_free(prev);
12350 prev = NULL;
12351 mch_memmove(s + prevlen, buf + tolist, len);
12352 s[prevlen + len] = NUL;
12353 }
12354 }
12355 tolist = filtd + 1;
12356
12357 li = listitem_alloc();
12358 if (li == NULL)
12359 {
12360 vim_free(s);
12361 break;
12362 }
12363 li->li_tv.v_type = VAR_STRING;
12364 li->li_tv.v_lock = 0;
12365 li->li_tv.vval.v_string = s;
12366 list_append(l, li);
12367
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012368 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012369 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012370 if (readlen <= 0)
12371 break;
12372 }
12373 else if (buf[filtd] == NUL)
12374 buf[filtd] = '\n';
12375 }
12376 if (readlen <= 0)
12377 break;
12378
12379 if (tolist == 0)
12380 {
12381 /* "buf" is full, need to move text to an allocated buffer */
12382 if (prev == NULL)
12383 {
12384 prev = vim_strnsave(buf, buflen);
12385 prevlen = buflen;
12386 }
12387 else
12388 {
12389 s = alloc((unsigned)(prevlen + buflen));
12390 if (s != NULL)
12391 {
12392 mch_memmove(s, prev, prevlen);
12393 mch_memmove(s + prevlen, buf, buflen);
12394 vim_free(prev);
12395 prev = s;
12396 prevlen += buflen;
12397 }
12398 }
12399 filtd = 0;
12400 }
12401 else
12402 {
12403 mch_memmove(buf, buf + tolist, buflen - tolist);
12404 filtd -= tolist;
12405 }
12406 }
12407
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012408 /*
12409 * For a negative line count use only the lines at the end of the file,
12410 * free the rest.
12411 */
12412 if (maxline < 0)
12413 while (cnt > -maxline)
12414 {
12415 listitem_remove(l, l->lv_first);
12416 --cnt;
12417 }
12418
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012419 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012420 fclose(fd);
12421}
12422
12423
Bram Moolenaar0d660222005-01-07 21:51:51 +000012424#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12425static void make_connection __ARGS((void));
12426static int check_connection __ARGS((void));
12427
12428 static void
12429make_connection()
12430{
12431 if (X_DISPLAY == NULL
12432# ifdef FEAT_GUI
12433 && !gui.in_use
12434# endif
12435 )
12436 {
12437 x_force_connect = TRUE;
12438 setup_term_clip();
12439 x_force_connect = FALSE;
12440 }
12441}
12442
12443 static int
12444check_connection()
12445{
12446 make_connection();
12447 if (X_DISPLAY == NULL)
12448 {
12449 EMSG(_("E240: No connection to Vim server"));
12450 return FAIL;
12451 }
12452 return OK;
12453}
12454#endif
12455
12456#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012457static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012458
12459 static void
12460remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012461 typval_T *argvars;
12462 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012463 int expr;
12464{
12465 char_u *server_name;
12466 char_u *keys;
12467 char_u *r = NULL;
12468 char_u buf[NUMBUFLEN];
12469# ifdef WIN32
12470 HWND w;
12471# else
12472 Window w;
12473# endif
12474
12475 if (check_restricted() || check_secure())
12476 return;
12477
12478# ifdef FEAT_X11
12479 if (check_connection() == FAIL)
12480 return;
12481# endif
12482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012483 server_name = get_tv_string_chk(&argvars[0]);
12484 if (server_name == NULL)
12485 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012486 keys = get_tv_string_buf(&argvars[1], buf);
12487# ifdef WIN32
12488 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12489# else
12490 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12491 < 0)
12492# endif
12493 {
12494 if (r != NULL)
12495 EMSG(r); /* sending worked but evaluation failed */
12496 else
12497 EMSG2(_("E241: Unable to send to %s"), server_name);
12498 return;
12499 }
12500
12501 rettv->vval.v_string = r;
12502
12503 if (argvars[2].v_type != VAR_UNKNOWN)
12504 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012505 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012506 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012507 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508
12509 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012510 v.di_tv.v_type = VAR_STRING;
12511 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012512 idvar = get_tv_string_chk(&argvars[2]);
12513 if (idvar != NULL)
12514 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012515 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516 }
12517}
12518#endif
12519
12520/*
12521 * "remote_expr()" function
12522 */
12523/*ARGSUSED*/
12524 static void
12525f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012526 typval_T *argvars;
12527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012528{
12529 rettv->v_type = VAR_STRING;
12530 rettv->vval.v_string = NULL;
12531#ifdef FEAT_CLIENTSERVER
12532 remote_common(argvars, rettv, TRUE);
12533#endif
12534}
12535
12536/*
12537 * "remote_foreground()" function
12538 */
12539/*ARGSUSED*/
12540 static void
12541f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 typval_T *argvars;
12543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012544{
12545 rettv->vval.v_number = 0;
12546#ifdef FEAT_CLIENTSERVER
12547# ifdef WIN32
12548 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549 {
12550 char_u *server_name = get_tv_string_chk(&argvars[0]);
12551
12552 if (server_name != NULL)
12553 serverForeground(server_name);
12554 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012555# else
12556 /* Send a foreground() expression to the server. */
12557 argvars[1].v_type = VAR_STRING;
12558 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12559 argvars[2].v_type = VAR_UNKNOWN;
12560 remote_common(argvars, rettv, TRUE);
12561 vim_free(argvars[1].vval.v_string);
12562# endif
12563#endif
12564}
12565
12566/*ARGSUSED*/
12567 static void
12568f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 typval_T *argvars;
12570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012571{
12572#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012573 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574 char_u *s = NULL;
12575# ifdef WIN32
12576 int n = 0;
12577# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012578 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012579
12580 if (check_restricted() || check_secure())
12581 {
12582 rettv->vval.v_number = -1;
12583 return;
12584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012585 serverid = get_tv_string_chk(&argvars[0]);
12586 if (serverid == NULL)
12587 {
12588 rettv->vval.v_number = -1;
12589 return; /* type error; errmsg already given */
12590 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012591# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012592 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012593 if (n == 0)
12594 rettv->vval.v_number = -1;
12595 else
12596 {
12597 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12598 rettv->vval.v_number = (s != NULL);
12599 }
12600# else
12601 rettv->vval.v_number = 0;
12602 if (check_connection() == FAIL)
12603 return;
12604
12605 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012607# endif
12608
12609 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012611 char_u *retvar;
12612
Bram Moolenaar33570922005-01-25 22:26:29 +000012613 v.di_tv.v_type = VAR_STRING;
12614 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 retvar = get_tv_string_chk(&argvars[1]);
12616 if (retvar != NULL)
12617 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012618 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 }
12620#else
12621 rettv->vval.v_number = -1;
12622#endif
12623}
12624
12625/*ARGSUSED*/
12626 static void
12627f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012628 typval_T *argvars;
12629 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012630{
12631 char_u *r = NULL;
12632
12633#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012634 char_u *serverid = get_tv_string_chk(&argvars[0]);
12635
12636 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012637 {
12638# ifdef WIN32
12639 /* The server's HWND is encoded in the 'id' parameter */
12640 int n = 0;
12641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012642 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012643 if (n != 0)
12644 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12645 if (r == NULL)
12646# else
12647 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012648 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012649# endif
12650 EMSG(_("E277: Unable to read a server reply"));
12651 }
12652#endif
12653 rettv->v_type = VAR_STRING;
12654 rettv->vval.v_string = r;
12655}
12656
12657/*
12658 * "remote_send()" function
12659 */
12660/*ARGSUSED*/
12661 static void
12662f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012663 typval_T *argvars;
12664 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012665{
12666 rettv->v_type = VAR_STRING;
12667 rettv->vval.v_string = NULL;
12668#ifdef FEAT_CLIENTSERVER
12669 remote_common(argvars, rettv, FALSE);
12670#endif
12671}
12672
12673/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012674 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012675 */
12676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012678 typval_T *argvars;
12679 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012680{
Bram Moolenaar33570922005-01-25 22:26:29 +000012681 list_T *l;
12682 listitem_T *item, *item2;
12683 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012684 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012685 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012686 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 dict_T *d;
12688 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012689
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012690 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012691 if (argvars[0].v_type == VAR_DICT)
12692 {
12693 if (argvars[2].v_type != VAR_UNKNOWN)
12694 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012695 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012696 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012698 key = get_tv_string_chk(&argvars[1]);
12699 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012701 di = dict_find(d, key, -1);
12702 if (di == NULL)
12703 EMSG2(_(e_dictkey), key);
12704 else
12705 {
12706 *rettv = di->di_tv;
12707 init_tv(&di->di_tv);
12708 dictitem_remove(d, di);
12709 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012711 }
12712 }
12713 else if (argvars[0].v_type != VAR_LIST)
12714 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012715 else if ((l = argvars[0].vval.v_list) != NULL
12716 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012718 int error = FALSE;
12719
12720 idx = get_tv_number_chk(&argvars[1], &error);
12721 if (error)
12722 ; /* type error: do nothing, errmsg already given */
12723 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012724 EMSGN(_(e_listidx), idx);
12725 else
12726 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012727 if (argvars[2].v_type == VAR_UNKNOWN)
12728 {
12729 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012730 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012731 *rettv = item->li_tv;
12732 vim_free(item);
12733 }
12734 else
12735 {
12736 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 end = get_tv_number_chk(&argvars[2], &error);
12738 if (error)
12739 ; /* type error: do nothing */
12740 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012741 EMSGN(_(e_listidx), end);
12742 else
12743 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012744 int cnt = 0;
12745
12746 for (li = item; li != NULL; li = li->li_next)
12747 {
12748 ++cnt;
12749 if (li == item2)
12750 break;
12751 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012752 if (li == NULL) /* didn't find "item2" after "item" */
12753 EMSG(_(e_invrange));
12754 else
12755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012756 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012757 l = list_alloc();
12758 if (l != NULL)
12759 {
12760 rettv->v_type = VAR_LIST;
12761 rettv->vval.v_list = l;
12762 l->lv_first = item;
12763 l->lv_last = item2;
12764 l->lv_refcount = 1;
12765 item->li_prev = NULL;
12766 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012767 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012768 }
12769 }
12770 }
12771 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012772 }
12773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774}
12775
12776/*
12777 * "rename({from}, {to})" function
12778 */
12779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012781 typval_T *argvars;
12782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783{
12784 char_u buf[NUMBUFLEN];
12785
12786 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12790 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791}
12792
12793/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012794 * "repeat()" function
12795 */
12796/*ARGSUSED*/
12797 static void
12798f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012799 typval_T *argvars;
12800 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012801{
12802 char_u *p;
12803 int n;
12804 int slen;
12805 int len;
12806 char_u *r;
12807 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012808 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012809
12810 n = get_tv_number(&argvars[1]);
12811 if (argvars[0].v_type == VAR_LIST)
12812 {
12813 l = list_alloc();
12814 if (l != NULL && argvars[0].vval.v_list != NULL)
12815 {
12816 l->lv_refcount = 1;
12817 while (n-- > 0)
12818 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12819 break;
12820 }
12821 rettv->v_type = VAR_LIST;
12822 rettv->vval.v_list = l;
12823 }
12824 else
12825 {
12826 p = get_tv_string(&argvars[0]);
12827 rettv->v_type = VAR_STRING;
12828 rettv->vval.v_string = NULL;
12829
12830 slen = (int)STRLEN(p);
12831 len = slen * n;
12832 if (len <= 0)
12833 return;
12834
12835 r = alloc(len + 1);
12836 if (r != NULL)
12837 {
12838 for (i = 0; i < n; i++)
12839 mch_memmove(r + i * slen, p, (size_t)slen);
12840 r[len] = NUL;
12841 }
12842
12843 rettv->vval.v_string = r;
12844 }
12845}
12846
12847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 * "resolve()" function
12849 */
12850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012852 typval_T *argvars;
12853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854{
12855 char_u *p;
12856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858#ifdef FEAT_SHORTCUT
12859 {
12860 char_u *v = NULL;
12861
12862 v = mch_resolve_shortcut(p);
12863 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 }
12868#else
12869# ifdef HAVE_READLINK
12870 {
12871 char_u buf[MAXPATHL + 1];
12872 char_u *cpy;
12873 int len;
12874 char_u *remain = NULL;
12875 char_u *q;
12876 int is_relative_to_current = FALSE;
12877 int has_trailing_pathsep = FALSE;
12878 int limit = 100;
12879
12880 p = vim_strsave(p);
12881
12882 if (p[0] == '.' && (vim_ispathsep(p[1])
12883 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12884 is_relative_to_current = TRUE;
12885
12886 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012887 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888 has_trailing_pathsep = TRUE;
12889
12890 q = getnextcomp(p);
12891 if (*q != NUL)
12892 {
12893 /* Separate the first path component in "p", and keep the
12894 * remainder (beginning with the path separator). */
12895 remain = vim_strsave(q - 1);
12896 q[-1] = NUL;
12897 }
12898
12899 for (;;)
12900 {
12901 for (;;)
12902 {
12903 len = readlink((char *)p, (char *)buf, MAXPATHL);
12904 if (len <= 0)
12905 break;
12906 buf[len] = NUL;
12907
12908 if (limit-- == 0)
12909 {
12910 vim_free(p);
12911 vim_free(remain);
12912 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 goto fail;
12915 }
12916
12917 /* Ensure that the result will have a trailing path separator
12918 * if the argument has one. */
12919 if (remain == NULL && has_trailing_pathsep)
12920 add_pathsep(buf);
12921
12922 /* Separate the first path component in the link value and
12923 * concatenate the remainders. */
12924 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12925 if (*q != NUL)
12926 {
12927 if (remain == NULL)
12928 remain = vim_strsave(q - 1);
12929 else
12930 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012931 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 if (cpy != NULL)
12933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934 vim_free(remain);
12935 remain = cpy;
12936 }
12937 }
12938 q[-1] = NUL;
12939 }
12940
12941 q = gettail(p);
12942 if (q > p && *q == NUL)
12943 {
12944 /* Ignore trailing path separator. */
12945 q[-1] = NUL;
12946 q = gettail(p);
12947 }
12948 if (q > p && !mch_isFullName(buf))
12949 {
12950 /* symlink is relative to directory of argument */
12951 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12952 if (cpy != NULL)
12953 {
12954 STRCPY(cpy, p);
12955 STRCPY(gettail(cpy), buf);
12956 vim_free(p);
12957 p = cpy;
12958 }
12959 }
12960 else
12961 {
12962 vim_free(p);
12963 p = vim_strsave(buf);
12964 }
12965 }
12966
12967 if (remain == NULL)
12968 break;
12969
12970 /* Append the first path component of "remain" to "p". */
12971 q = getnextcomp(remain + 1);
12972 len = q - remain - (*q != NUL);
12973 cpy = vim_strnsave(p, STRLEN(p) + len);
12974 if (cpy != NULL)
12975 {
12976 STRNCAT(cpy, remain, len);
12977 vim_free(p);
12978 p = cpy;
12979 }
12980 /* Shorten "remain". */
12981 if (*q != NUL)
12982 STRCPY(remain, q - 1);
12983 else
12984 {
12985 vim_free(remain);
12986 remain = NULL;
12987 }
12988 }
12989
12990 /* If the result is a relative path name, make it explicitly relative to
12991 * the current directory if and only if the argument had this form. */
12992 if (!vim_ispathsep(*p))
12993 {
12994 if (is_relative_to_current
12995 && *p != NUL
12996 && !(p[0] == '.'
12997 && (p[1] == NUL
12998 || vim_ispathsep(p[1])
12999 || (p[1] == '.'
13000 && (p[2] == NUL
13001 || vim_ispathsep(p[2]))))))
13002 {
13003 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013004 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005 if (cpy != NULL)
13006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 vim_free(p);
13008 p = cpy;
13009 }
13010 }
13011 else if (!is_relative_to_current)
13012 {
13013 /* Strip leading "./". */
13014 q = p;
13015 while (q[0] == '.' && vim_ispathsep(q[1]))
13016 q += 2;
13017 if (q > p)
13018 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13019 }
13020 }
13021
13022 /* Ensure that the result will have no trailing path separator
13023 * if the argument had none. But keep "/" or "//". */
13024 if (!has_trailing_pathsep)
13025 {
13026 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013027 if (after_pathsep(p, q))
13028 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 }
13030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013031 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 }
13033# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013034 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035# endif
13036#endif
13037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013038 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039
13040#ifdef HAVE_READLINK
13041fail:
13042#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044}
13045
13046/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013047 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 */
13049 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013050f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013051 typval_T *argvars;
13052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053{
Bram Moolenaar33570922005-01-25 22:26:29 +000013054 list_T *l;
13055 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056
Bram Moolenaar0d660222005-01-07 21:51:51 +000013057 rettv->vval.v_number = 0;
13058 if (argvars[0].v_type != VAR_LIST)
13059 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013060 else if ((l = argvars[0].vval.v_list) != NULL
13061 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013062 {
13063 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013064 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013065 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013066 while (li != NULL)
13067 {
13068 ni = li->li_prev;
13069 list_append(l, li);
13070 li = ni;
13071 }
13072 rettv->vval.v_list = l;
13073 rettv->v_type = VAR_LIST;
13074 ++l->lv_refcount;
13075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076}
13077
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013078#define SP_NOMOVE 1 /* don't move cursor */
13079#define SP_REPEAT 2 /* repeat to find outer pair */
13080#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013081#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013082
Bram Moolenaar33570922005-01-25 22:26:29 +000013083static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013084
13085/*
13086 * Get flags for a search function.
13087 * Possibly sets "p_ws".
13088 * Returns BACKWARD, FORWARD or zero (for an error).
13089 */
13090 static int
13091get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013092 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013093 int *flagsp;
13094{
13095 int dir = FORWARD;
13096 char_u *flags;
13097 char_u nbuf[NUMBUFLEN];
13098 int mask;
13099
13100 if (varp->v_type != VAR_UNKNOWN)
13101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 flags = get_tv_string_buf_chk(varp, nbuf);
13103 if (flags == NULL)
13104 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013105 while (*flags != NUL)
13106 {
13107 switch (*flags)
13108 {
13109 case 'b': dir = BACKWARD; break;
13110 case 'w': p_ws = TRUE; break;
13111 case 'W': p_ws = FALSE; break;
13112 default: mask = 0;
13113 if (flagsp != NULL)
13114 switch (*flags)
13115 {
13116 case 'n': mask = SP_NOMOVE; break;
13117 case 'r': mask = SP_REPEAT; break;
13118 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013119 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013120 }
13121 if (mask == 0)
13122 {
13123 EMSG2(_(e_invarg2), flags);
13124 dir = 0;
13125 }
13126 else
13127 *flagsp |= mask;
13128 }
13129 if (dir == 0)
13130 break;
13131 ++flags;
13132 }
13133 }
13134 return dir;
13135}
13136
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137/*
13138 * "search()" function
13139 */
13140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013142 typval_T *argvars;
13143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144{
13145 char_u *pat;
13146 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013147 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 int save_p_ws = p_ws;
13149 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013150 int flags = 0;
13151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013154 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013155 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13156 if (dir == 0)
13157 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013158 /*
13159 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13160 * Check to make sure only those flags are set.
13161 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13162 * flags cannot be set. Check for that condition also.
13163 */
13164 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13165 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013168 goto theend;
13169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013171 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13173 SEARCH_KEEP, RE_SEARCH) != FAIL)
13174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013175 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013176 if (flags & SP_SETPCMARK)
13177 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 curwin->w_cursor = pos;
13179 /* "/$" will put the cursor after the end of the line, may need to
13180 * correct that here */
13181 check_cursor();
13182 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013183
13184 /* If 'n' flag is used: restore cursor position. */
13185 if (flags & SP_NOMOVE)
13186 curwin->w_cursor = save_cursor;
13187theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 p_ws = save_p_ws;
13189}
13190
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013192 * "searchdecl()" function
13193 */
13194 static void
13195f_searchdecl(argvars, rettv)
13196 typval_T *argvars;
13197 typval_T *rettv;
13198{
13199 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013200 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013201 int error = FALSE;
13202 char_u *name;
13203
13204 rettv->vval.v_number = 1; /* default: FAIL */
13205
13206 name = get_tv_string_chk(&argvars[0]);
13207 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013208 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013209 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013210 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13211 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13212 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013213 if (!error && name != NULL)
13214 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013215 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013216}
13217
13218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 * "searchpair()" function
13220 */
13221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013222f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013223 typval_T *argvars;
13224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225{
13226 char_u *spat, *mpat, *epat;
13227 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 int dir;
13230 int flags = 0;
13231 char_u nbuf1[NUMBUFLEN];
13232 char_u nbuf2[NUMBUFLEN];
13233 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013238 spat = get_tv_string_chk(&argvars[0]);
13239 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13240 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13241 if (spat == NULL || mpat == NULL || epat == NULL)
13242 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 /* Handle the optional fourth argument: flags */
13245 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013246 if (dir == 0)
13247 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013248 /*
13249 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13250 */
13251 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13252 {
13253 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13254 goto theend;
13255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256
13257 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013258 if (argvars[3].v_type == VAR_UNKNOWN
13259 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260 skip = (char_u *)"";
13261 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13263 if (skip == NULL)
13264 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013266 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13267
13268theend:
13269 p_ws = save_p_ws;
13270}
13271
13272/*
13273 * Search for a start/middle/end thing.
13274 * Used by searchpair(), see its documentation for the details.
13275 * Returns 0 or -1 for no match,
13276 */
13277 long
13278do_searchpair(spat, mpat, epat, dir, skip, flags)
13279 char_u *spat; /* start pattern */
13280 char_u *mpat; /* middle pattern */
13281 char_u *epat; /* end pattern */
13282 int dir; /* BACKWARD or FORWARD */
13283 char_u *skip; /* skip expression */
13284 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13285{
13286 char_u *save_cpo;
13287 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13288 long retval = 0;
13289 pos_T pos;
13290 pos_T firstpos;
13291 pos_T foundpos;
13292 pos_T save_cursor;
13293 pos_T save_pos;
13294 int n;
13295 int r;
13296 int nest = 1;
13297 int err;
13298
13299 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13300 save_cpo = p_cpo;
13301 p_cpo = (char_u *)"";
13302
13303 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13304 * start/middle/end (pat3, for the top pair). */
13305 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13306 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13307 if (pat2 == NULL || pat3 == NULL)
13308 goto theend;
13309 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13310 if (*mpat == NUL)
13311 STRCPY(pat3, pat2);
13312 else
13313 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13314 spat, epat, mpat);
13315
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 save_cursor = curwin->w_cursor;
13317 pos = curwin->w_cursor;
13318 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013319 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320 pat = pat3;
13321 for (;;)
13322 {
13323 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13324 SEARCH_KEEP, RE_SEARCH);
13325 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13326 /* didn't find it or found the first match again: FAIL */
13327 break;
13328
13329 if (firstpos.lnum == 0)
13330 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013331 if (equalpos(pos, foundpos))
13332 {
13333 /* Found the same position again. Can happen with a pattern that
13334 * has "\zs" at the end and searching backwards. Advance one
13335 * character and try again. */
13336 if (dir == BACKWARD)
13337 decl(&pos);
13338 else
13339 incl(&pos);
13340 }
13341 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342
13343 /* If the skip pattern matches, ignore this match. */
13344 if (*skip != NUL)
13345 {
13346 save_pos = curwin->w_cursor;
13347 curwin->w_cursor = pos;
13348 r = eval_to_bool(skip, &err, NULL, FALSE);
13349 curwin->w_cursor = save_pos;
13350 if (err)
13351 {
13352 /* Evaluating {skip} caused an error, break here. */
13353 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013354 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355 break;
13356 }
13357 if (r)
13358 continue;
13359 }
13360
13361 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13362 {
13363 /* Found end when searching backwards or start when searching
13364 * forward: nested pair. */
13365 ++nest;
13366 pat = pat2; /* nested, don't search for middle */
13367 }
13368 else
13369 {
13370 /* Found end when searching forward or start when searching
13371 * backward: end of (nested) pair; or found middle in outer pair. */
13372 if (--nest == 1)
13373 pat = pat3; /* outer level, search for middle */
13374 }
13375
13376 if (nest == 0)
13377 {
13378 /* Found the match: return matchcount or line number. */
13379 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013380 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013382 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013383 if (flags & SP_SETPCMARK)
13384 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 curwin->w_cursor = pos;
13386 if (!(flags & SP_REPEAT))
13387 break;
13388 nest = 1; /* search for next unmatched */
13389 }
13390 }
13391
13392 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013393 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 curwin->w_cursor = save_cursor;
13395
13396theend:
13397 vim_free(pat2);
13398 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013400
13401 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402}
13403
Bram Moolenaar0d660222005-01-07 21:51:51 +000013404/*ARGSUSED*/
13405 static void
13406f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013407 typval_T *argvars;
13408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013410#ifdef FEAT_CLIENTSERVER
13411 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013412 char_u *server = get_tv_string_chk(&argvars[0]);
13413 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414
Bram Moolenaar0d660222005-01-07 21:51:51 +000013415 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013416 if (server == NULL || reply == NULL)
13417 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013418 if (check_restricted() || check_secure())
13419 return;
13420# ifdef FEAT_X11
13421 if (check_connection() == FAIL)
13422 return;
13423# endif
13424
13425 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013427 EMSG(_("E258: Unable to send to client"));
13428 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013430 rettv->vval.v_number = 0;
13431#else
13432 rettv->vval.v_number = -1;
13433#endif
13434}
13435
13436/*ARGSUSED*/
13437 static void
13438f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 typval_T *argvars;
13440 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013441{
13442 char_u *r = NULL;
13443
13444#ifdef FEAT_CLIENTSERVER
13445# ifdef WIN32
13446 r = serverGetVimNames();
13447# else
13448 make_connection();
13449 if (X_DISPLAY != NULL)
13450 r = serverGetVimNames(X_DISPLAY);
13451# endif
13452#endif
13453 rettv->v_type = VAR_STRING;
13454 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455}
13456
13457/*
13458 * "setbufvar()" function
13459 */
13460/*ARGSUSED*/
13461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013462f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013463 typval_T *argvars;
13464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465{
13466 buf_T *buf;
13467#ifdef FEAT_AUTOCMD
13468 aco_save_T aco;
13469#else
13470 buf_T *save_curbuf;
13471#endif
13472 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013473 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 char_u nbuf[NUMBUFLEN];
13475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013476 rettv->vval.v_number = 0;
13477
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 if (check_restricted() || check_secure())
13479 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013480 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13481 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013482 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 varp = &argvars[2];
13484
13485 if (buf != NULL && varname != NULL && varp != NULL)
13486 {
13487 /* set curbuf to be our buf, temporarily */
13488#ifdef FEAT_AUTOCMD
13489 aucmd_prepbuf(&aco, buf);
13490#else
13491 save_curbuf = curbuf;
13492 curbuf = buf;
13493#endif
13494
13495 if (*varname == '&')
13496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 long numval;
13498 char_u *strval;
13499 int error = FALSE;
13500
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013502 numval = get_tv_number_chk(varp, &error);
13503 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013504 if (!error && strval != NULL)
13505 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 }
13507 else
13508 {
13509 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13510 if (bufvarname != NULL)
13511 {
13512 STRCPY(bufvarname, "b:");
13513 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013514 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 vim_free(bufvarname);
13516 }
13517 }
13518
13519 /* reset notion of buffer */
13520#ifdef FEAT_AUTOCMD
13521 aucmd_restbuf(&aco);
13522#else
13523 curbuf = save_curbuf;
13524#endif
13525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526}
13527
13528/*
13529 * "setcmdpos()" function
13530 */
13531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013532f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013533 typval_T *argvars;
13534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536 int pos = (int)get_tv_number(&argvars[0]) - 1;
13537
13538 if (pos >= 0)
13539 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540}
13541
13542/*
13543 * "setline()" function
13544 */
13545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013547 typval_T *argvars;
13548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549{
13550 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013551 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013552 list_T *l = NULL;
13553 listitem_T *li = NULL;
13554 long added = 0;
13555 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013557 lnum = get_tv_lnum(&argvars[0]);
13558 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013560 l = argvars[1].vval.v_list;
13561 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013563 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013564 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013565
13566 rettv->vval.v_number = 0; /* OK */
13567 for (;;)
13568 {
13569 if (l != NULL)
13570 {
13571 /* list argument, get next string */
13572 if (li == NULL)
13573 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013575 li = li->li_next;
13576 }
13577
13578 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013580 break;
13581 if (lnum <= curbuf->b_ml.ml_line_count)
13582 {
13583 /* existing line, replace it */
13584 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13585 {
13586 changed_bytes(lnum, 0);
13587 check_cursor_col();
13588 rettv->vval.v_number = 0; /* OK */
13589 }
13590 }
13591 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13592 {
13593 /* lnum is one past the last line, append the line */
13594 ++added;
13595 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13596 rettv->vval.v_number = 0; /* OK */
13597 }
13598
13599 if (l == NULL) /* only one string argument */
13600 break;
13601 ++lnum;
13602 }
13603
13604 if (added > 0)
13605 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606}
13607
13608/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013609 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013610 */
13611/*ARGSUSED*/
13612 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013613set_qf_ll_list(wp, list_arg, action_arg, rettv)
13614 win_T *wp;
13615 typval_T *list_arg;
13616 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013617 typval_T *rettv;
13618{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013619#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013620 char_u *act;
13621 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013622#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013623
Bram Moolenaar2641f772005-03-25 21:58:17 +000013624 rettv->vval.v_number = -1;
13625
13626#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013627 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013628 EMSG(_(e_listreq));
13629 else
13630 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013631 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013632
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013633 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013634 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013635 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013636 if (act == NULL)
13637 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013638 if (*act == 'a' || *act == 'r')
13639 action = *act;
13640 }
13641
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013642 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013643 rettv->vval.v_number = 0;
13644 }
13645#endif
13646}
13647
13648/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013649 * "setloclist()" function
13650 */
13651/*ARGSUSED*/
13652 static void
13653f_setloclist(argvars, rettv)
13654 typval_T *argvars;
13655 typval_T *rettv;
13656{
13657 win_T *win;
13658
13659 rettv->vval.v_number = -1;
13660
13661 win = find_win_by_nr(&argvars[0]);
13662 if (win != NULL)
13663 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13664}
13665
13666/*
13667 * "setqflist()" function
13668 */
13669/*ARGSUSED*/
13670 static void
13671f_setqflist(argvars, rettv)
13672 typval_T *argvars;
13673 typval_T *rettv;
13674{
13675 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13676}
13677
13678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 * "setreg()" function
13680 */
13681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013683 typval_T *argvars;
13684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685{
13686 int regname;
13687 char_u *strregname;
13688 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013689 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 int append;
13691 char_u yank_type;
13692 long block_len;
13693
13694 block_len = -1;
13695 yank_type = MAUTO;
13696 append = FALSE;
13697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013698 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013699 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701 if (strregname == NULL)
13702 return; /* type error; errmsg already given */
13703 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 if (regname == 0 || regname == '@')
13705 regname = '"';
13706 else if (regname == '=')
13707 return;
13708
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013709 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013711 stropt = get_tv_string_chk(&argvars[2]);
13712 if (stropt == NULL)
13713 return; /* type error */
13714 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 switch (*stropt)
13716 {
13717 case 'a': case 'A': /* append */
13718 append = TRUE;
13719 break;
13720 case 'v': case 'c': /* character-wise selection */
13721 yank_type = MCHAR;
13722 break;
13723 case 'V': case 'l': /* line-wise selection */
13724 yank_type = MLINE;
13725 break;
13726#ifdef FEAT_VISUAL
13727 case 'b': case Ctrl_V: /* block-wise selection */
13728 yank_type = MBLOCK;
13729 if (VIM_ISDIGIT(stropt[1]))
13730 {
13731 ++stropt;
13732 block_len = getdigits(&stropt) - 1;
13733 --stropt;
13734 }
13735 break;
13736#endif
13737 }
13738 }
13739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013740 strval = get_tv_string_chk(&argvars[1]);
13741 if (strval != NULL)
13742 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745}
13746
13747
13748/*
13749 * "setwinvar(expr)" function
13750 */
13751/*ARGSUSED*/
13752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013754 typval_T *argvars;
13755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756{
13757 win_T *win;
13758#ifdef FEAT_WINDOWS
13759 win_T *save_curwin;
13760#endif
13761 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013762 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763 char_u nbuf[NUMBUFLEN];
13764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 rettv->vval.v_number = 0;
13766
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 if (check_restricted() || check_secure())
13768 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 varp = &argvars[2];
13772
13773 if (win != NULL && varname != NULL && varp != NULL)
13774 {
13775#ifdef FEAT_WINDOWS
13776 /* set curwin to be our win, temporarily */
13777 save_curwin = curwin;
13778 curwin = win;
13779 curbuf = curwin->w_buffer;
13780#endif
13781
13782 if (*varname == '&')
13783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 long numval;
13785 char_u *strval;
13786 int error = FALSE;
13787
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789 numval = get_tv_number_chk(varp, &error);
13790 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013791 if (!error && strval != NULL)
13792 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 }
13794 else
13795 {
13796 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13797 if (winvarname != NULL)
13798 {
13799 STRCPY(winvarname, "w:");
13800 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013801 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 vim_free(winvarname);
13803 }
13804 }
13805
13806#ifdef FEAT_WINDOWS
13807 /* Restore current window, if it's still valid (autocomands can make
13808 * it invalid). */
13809 if (win_valid(save_curwin))
13810 {
13811 curwin = save_curwin;
13812 curbuf = curwin->w_buffer;
13813 }
13814#endif
13815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816}
13817
13818/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013819 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 */
13821 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013822f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013823 typval_T *argvars;
13824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013826 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827
Bram Moolenaar0d660222005-01-07 21:51:51 +000013828 p = get_tv_string(&argvars[0]);
13829 rettv->vval.v_string = vim_strsave(p);
13830 simplify_filename(rettv->vval.v_string); /* simplify in place */
13831 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832}
13833
Bram Moolenaar0d660222005-01-07 21:51:51 +000013834static int
13835#ifdef __BORLANDC__
13836 _RTLENTRYF
13837#endif
13838 item_compare __ARGS((const void *s1, const void *s2));
13839static int
13840#ifdef __BORLANDC__
13841 _RTLENTRYF
13842#endif
13843 item_compare2 __ARGS((const void *s1, const void *s2));
13844
13845static int item_compare_ic;
13846static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013848#define ITEM_COMPARE_FAIL 999
13849
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013851 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013853 static int
13854#ifdef __BORLANDC__
13855_RTLENTRYF
13856#endif
13857item_compare(s1, s2)
13858 const void *s1;
13859 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013861 char_u *p1, *p2;
13862 char_u *tofree1, *tofree2;
13863 int res;
13864 char_u numbuf1[NUMBUFLEN];
13865 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013867 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
13868 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013869 if (item_compare_ic)
13870 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013872 res = STRCMP(p1, p2);
13873 vim_free(tofree1);
13874 vim_free(tofree2);
13875 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876}
13877
13878 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013879#ifdef __BORLANDC__
13880_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013882item_compare2(s1, s2)
13883 const void *s1;
13884 const void *s2;
13885{
13886 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013887 typval_T rettv;
13888 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013889 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013891 /* shortcut after failure in previous call; compare all items equal */
13892 if (item_compare_func_err)
13893 return 0;
13894
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013895 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13896 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013897 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13898 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013899
13900 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13901 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013902 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013903 clear_tv(&argv[0]);
13904 clear_tv(&argv[1]);
13905
13906 if (res == FAIL)
13907 res = ITEM_COMPARE_FAIL;
13908 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013909 /* return value has wrong type */
13910 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13911 if (item_compare_func_err)
13912 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013913 clear_tv(&rettv);
13914 return res;
13915}
13916
13917/*
13918 * "sort({list})" function
13919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013921f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013922 typval_T *argvars;
13923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924{
Bram Moolenaar33570922005-01-25 22:26:29 +000013925 list_T *l;
13926 listitem_T *li;
13927 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013928 long len;
13929 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930
Bram Moolenaar0d660222005-01-07 21:51:51 +000013931 rettv->vval.v_number = 0;
13932 if (argvars[0].v_type != VAR_LIST)
13933 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 else
13935 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013936 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013937 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013938 return;
13939 rettv->vval.v_list = l;
13940 rettv->v_type = VAR_LIST;
13941 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942
Bram Moolenaar0d660222005-01-07 21:51:51 +000013943 len = list_len(l);
13944 if (len <= 1)
13945 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947 item_compare_ic = FALSE;
13948 item_compare_func = NULL;
13949 if (argvars[1].v_type != VAR_UNKNOWN)
13950 {
13951 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953 else
13954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 int error = FALSE;
13956
13957 i = get_tv_number_chk(&argvars[1], &error);
13958 if (error)
13959 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013960 if (i == 1)
13961 item_compare_ic = TRUE;
13962 else
13963 item_compare_func = get_tv_string(&argvars[1]);
13964 }
13965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966
Bram Moolenaar0d660222005-01-07 21:51:51 +000013967 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013968 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013969 if (ptrs == NULL)
13970 return;
13971 i = 0;
13972 for (li = l->lv_first; li != NULL; li = li->li_next)
13973 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013976 /* test the compare function */
13977 if (item_compare_func != NULL
13978 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13979 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013980 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013982 {
13983 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013984 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013985 item_compare_func == NULL ? item_compare : item_compare2);
13986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 if (!item_compare_func_err)
13988 {
13989 /* Clear the List and append the items in the sorted order. */
13990 l->lv_first = l->lv_last = NULL;
13991 l->lv_len = 0;
13992 for (i = 0; i < len; ++i)
13993 list_append(l, ptrs[i]);
13994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013995 }
13996
13997 vim_free(ptrs);
13998 }
13999}
14000
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014001/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014002 * "soundfold({word})" function
14003 */
14004 static void
14005f_soundfold(argvars, rettv)
14006 typval_T *argvars;
14007 typval_T *rettv;
14008{
14009 char_u *s;
14010
14011 rettv->v_type = VAR_STRING;
14012 s = get_tv_string(&argvars[0]);
14013#ifdef FEAT_SYN_HL
14014 rettv->vval.v_string = eval_soundfold(s);
14015#else
14016 rettv->vval.v_string = vim_strsave(s);
14017#endif
14018}
14019
14020/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014021 * "spellbadword()" function
14022 */
14023/* ARGSUSED */
14024 static void
14025f_spellbadword(argvars, rettv)
14026 typval_T *argvars;
14027 typval_T *rettv;
14028{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014029 char_u *word = (char_u *)"";
14030#ifdef FEAT_SYN_HL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014031 int len = 0;
14032 hlf_T attr = HLF_COUNT;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014033 list_T *l;
14034#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014035
Bram Moolenaar4463f292005-09-25 22:20:24 +000014036 l = list_alloc();
14037 if (l == NULL)
14038 return;
14039 rettv->v_type = VAR_LIST;
14040 rettv->vval.v_list = l;
14041 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014042
14043#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014044 if (argvars[0].v_type == VAR_UNKNOWN)
14045 {
14046 /* Find the start and length of the badly spelled word. */
14047 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14048 if (len != 0)
14049 word = ml_get_cursor();
14050 }
14051 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14052 {
14053 char_u *str = get_tv_string_chk(&argvars[0]);
14054 int capcol = -1;
14055
14056 if (str != NULL)
14057 {
14058 /* Check the argument for spelling. */
14059 while (*str != NUL)
14060 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014061 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014062 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014063 {
14064 word = str;
14065 break;
14066 }
14067 str += len;
14068 }
14069 }
14070 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014071#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014072
14073 list_append_string(l, word, len);
14074 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014075 attr == HLF_SPB ? "bad" :
14076 attr == HLF_SPR ? "rare" :
14077 attr == HLF_SPL ? "local" :
14078 attr == HLF_SPC ? "caps" :
14079 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014080}
14081
14082/*
14083 * "spellsuggest()" function
14084 */
14085 static void
14086f_spellsuggest(argvars, rettv)
14087 typval_T *argvars;
14088 typval_T *rettv;
14089{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014090 list_T *l;
14091#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014092 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014093 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014094 int maxcount;
14095 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014096 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014097 listitem_T *li;
14098 int need_capital = FALSE;
14099#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014100
14101 l = list_alloc();
14102 if (l == NULL)
14103 return;
14104 rettv->v_type = VAR_LIST;
14105 rettv->vval.v_list = l;
14106 ++l->lv_refcount;
14107
14108#ifdef FEAT_SYN_HL
14109 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14110 {
14111 str = get_tv_string(&argvars[0]);
14112 if (argvars[1].v_type != VAR_UNKNOWN)
14113 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014114 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014115 if (maxcount <= 0)
14116 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014117 if (argvars[2].v_type != VAR_UNKNOWN)
14118 {
14119 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14120 if (typeerr)
14121 return;
14122 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014123 }
14124 else
14125 maxcount = 25;
14126
Bram Moolenaar4770d092006-01-12 23:22:24 +000014127 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014128
14129 for (i = 0; i < ga.ga_len; ++i)
14130 {
14131 str = ((char_u **)ga.ga_data)[i];
14132
14133 li = listitem_alloc();
14134 if (li == NULL)
14135 vim_free(str);
14136 else
14137 {
14138 li->li_tv.v_type = VAR_STRING;
14139 li->li_tv.v_lock = 0;
14140 li->li_tv.vval.v_string = str;
14141 list_append(l, li);
14142 }
14143 }
14144 ga_clear(&ga);
14145 }
14146#endif
14147}
14148
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014150f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014151 typval_T *argvars;
14152 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014153{
14154 char_u *str;
14155 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014156 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014157 regmatch_T regmatch;
14158 char_u patbuf[NUMBUFLEN];
14159 char_u *save_cpo;
14160 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014161 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014163 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165
14166 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14167 save_cpo = p_cpo;
14168 p_cpo = (char_u *)"";
14169
14170 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014171 if (argvars[1].v_type != VAR_UNKNOWN)
14172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14174 if (pat == NULL)
14175 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014176 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014178 }
14179 if (pat == NULL || *pat == NUL)
14180 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014181
14182 l = list_alloc();
14183 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014185 rettv->v_type = VAR_LIST;
14186 rettv->vval.v_list = l;
14187 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188 if (typeerr)
14189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190
Bram Moolenaar0d660222005-01-07 21:51:51 +000014191 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14192 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014194 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014195 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014196 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014197 if (*str == NUL)
14198 match = FALSE; /* empty item at the end */
14199 else
14200 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014201 if (match)
14202 end = regmatch.startp[0];
14203 else
14204 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014205 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14206 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014207 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014208 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014209 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014210 }
14211 if (!match)
14212 break;
14213 /* Advance to just after the match. */
14214 if (regmatch.endp[0] > str)
14215 col = 0;
14216 else
14217 {
14218 /* Don't get stuck at the same match. */
14219#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014220 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014221#else
14222 col = 1;
14223#endif
14224 }
14225 str = regmatch.endp[0];
14226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227
Bram Moolenaar0d660222005-01-07 21:51:51 +000014228 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230
Bram Moolenaar0d660222005-01-07 21:51:51 +000014231 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232}
14233
14234#ifdef HAVE_STRFTIME
14235/*
14236 * "strftime({format}[, {time}])" function
14237 */
14238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014240 typval_T *argvars;
14241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242{
14243 char_u result_buf[256];
14244 struct tm *curtime;
14245 time_t seconds;
14246 char_u *p;
14247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014248 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014250 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014251 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 seconds = time(NULL);
14253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 curtime = localtime(&seconds);
14256 /* MSVC returns NULL for an invalid value of seconds. */
14257 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 else
14260 {
14261# ifdef FEAT_MBYTE
14262 vimconv_T conv;
14263 char_u *enc;
14264
14265 conv.vc_type = CONV_NONE;
14266 enc = enc_locale();
14267 convert_setup(&conv, p_enc, enc);
14268 if (conv.vc_type != CONV_NONE)
14269 p = string_convert(&conv, p, NULL);
14270# endif
14271 if (p != NULL)
14272 (void)strftime((char *)result_buf, sizeof(result_buf),
14273 (char *)p, curtime);
14274 else
14275 result_buf[0] = NUL;
14276
14277# ifdef FEAT_MBYTE
14278 if (conv.vc_type != CONV_NONE)
14279 vim_free(p);
14280 convert_setup(&conv, enc, p_enc);
14281 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014282 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 else
14284# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286
14287# ifdef FEAT_MBYTE
14288 /* Release conversion descriptors */
14289 convert_setup(&conv, NULL, NULL);
14290 vim_free(enc);
14291# endif
14292 }
14293}
14294#endif
14295
14296/*
14297 * "stridx()" function
14298 */
14299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014301 typval_T *argvars;
14302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303{
14304 char_u buf[NUMBUFLEN];
14305 char_u *needle;
14306 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014307 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014309 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 needle = get_tv_string_chk(&argvars[1]);
14312 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014313 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014314 if (needle == NULL || haystack == NULL)
14315 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316
Bram Moolenaar33570922005-01-25 22:26:29 +000014317 if (argvars[2].v_type != VAR_UNKNOWN)
14318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014319 int error = FALSE;
14320
14321 start_idx = get_tv_number_chk(&argvars[2], &error);
14322 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014323 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014324 if (start_idx >= 0)
14325 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014326 }
14327
14328 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14329 if (pos != NULL)
14330 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331}
14332
14333/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014334 * "string()" function
14335 */
14336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014337f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014338 typval_T *argvars;
14339 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014340{
14341 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014342 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014344 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014345 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014346 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348}
14349
14350/*
14351 * "strlen()" function
14352 */
14353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014355 typval_T *argvars;
14356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358 rettv->vval.v_number = (varnumber_T)(STRLEN(
14359 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360}
14361
14362/*
14363 * "strpart()" function
14364 */
14365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014367 typval_T *argvars;
14368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369{
14370 char_u *p;
14371 int n;
14372 int len;
14373 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014374 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 slen = (int)STRLEN(p);
14378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014379 n = get_tv_number_chk(&argvars[1], &error);
14380 if (error)
14381 len = 0;
14382 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014383 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 else
14385 len = slen - n; /* default len: all bytes that are available. */
14386
14387 /*
14388 * Only return the overlap between the specified part and the actual
14389 * string.
14390 */
14391 if (n < 0)
14392 {
14393 len += n;
14394 n = 0;
14395 }
14396 else if (n > slen)
14397 n = slen;
14398 if (len < 0)
14399 len = 0;
14400 else if (n + len > slen)
14401 len = slen - n;
14402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403 rettv->v_type = VAR_STRING;
14404 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405}
14406
14407/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014408 * "strridx()" function
14409 */
14410 static void
14411f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014412 typval_T *argvars;
14413 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014414{
14415 char_u buf[NUMBUFLEN];
14416 char_u *needle;
14417 char_u *haystack;
14418 char_u *rest;
14419 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014420 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 needle = get_tv_string_chk(&argvars[1]);
14423 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014424 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014425
14426 rettv->vval.v_number = -1;
14427 if (needle == NULL || haystack == NULL)
14428 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014429 if (argvars[2].v_type != VAR_UNKNOWN)
14430 {
14431 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014432 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014433 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014434 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014435 }
14436 else
14437 end_idx = haystack_len;
14438
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014440 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014442 lastmatch = haystack + end_idx;
14443 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014444 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014445 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014446 for (rest = haystack; *rest != '\0'; ++rest)
14447 {
14448 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014449 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014450 break;
14451 lastmatch = rest;
14452 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014453 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014454
14455 if (lastmatch == NULL)
14456 rettv->vval.v_number = -1;
14457 else
14458 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14459}
14460
14461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 * "strtrans()" function
14463 */
14464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014465f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014466 typval_T *argvars;
14467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469 rettv->v_type = VAR_STRING;
14470 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471}
14472
14473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014474 * "submatch()" function
14475 */
14476 static void
14477f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014478 typval_T *argvars;
14479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480{
14481 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014482 rettv->vval.v_string =
14483 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014484}
14485
14486/*
14487 * "substitute()" function
14488 */
14489 static void
14490f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014491 typval_T *argvars;
14492 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493{
14494 char_u patbuf[NUMBUFLEN];
14495 char_u subbuf[NUMBUFLEN];
14496 char_u flagsbuf[NUMBUFLEN];
14497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014498 char_u *str = get_tv_string_chk(&argvars[0]);
14499 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14500 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14501 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14502
Bram Moolenaar0d660222005-01-07 21:51:51 +000014503 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014504 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14505 rettv->vval.v_string = NULL;
14506 else
14507 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014508}
14509
14510/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014511 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 */
14513/*ARGSUSED*/
14514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014516 typval_T *argvars;
14517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518{
14519 int id = 0;
14520#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014521 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 long col;
14523 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014524 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 lnum = get_tv_lnum(argvars); /* -1 on type error */
14527 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14528 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014531 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014532 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533#endif
14534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536}
14537
14538/*
14539 * "synIDattr(id, what [, mode])" function
14540 */
14541/*ARGSUSED*/
14542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014543f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014544 typval_T *argvars;
14545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546{
14547 char_u *p = NULL;
14548#ifdef FEAT_SYN_HL
14549 int id;
14550 char_u *what;
14551 char_u *mode;
14552 char_u modebuf[NUMBUFLEN];
14553 int modec;
14554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014555 id = get_tv_number(&argvars[0]);
14556 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014557 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 modec = TOLOWER_ASC(mode[0]);
14561 if (modec != 't' && modec != 'c'
14562#ifdef FEAT_GUI
14563 && modec != 'g'
14564#endif
14565 )
14566 modec = 0; /* replace invalid with current */
14567 }
14568 else
14569 {
14570#ifdef FEAT_GUI
14571 if (gui.in_use)
14572 modec = 'g';
14573 else
14574#endif
14575 if (t_colors > 1)
14576 modec = 'c';
14577 else
14578 modec = 't';
14579 }
14580
14581
14582 switch (TOLOWER_ASC(what[0]))
14583 {
14584 case 'b':
14585 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14586 p = highlight_color(id, what, modec);
14587 else /* bold */
14588 p = highlight_has_attr(id, HL_BOLD, modec);
14589 break;
14590
14591 case 'f': /* fg[#] */
14592 p = highlight_color(id, what, modec);
14593 break;
14594
14595 case 'i':
14596 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14597 p = highlight_has_attr(id, HL_INVERSE, modec);
14598 else /* italic */
14599 p = highlight_has_attr(id, HL_ITALIC, modec);
14600 break;
14601
14602 case 'n': /* name */
14603 p = get_highlight_name(NULL, id - 1);
14604 break;
14605
14606 case 'r': /* reverse */
14607 p = highlight_has_attr(id, HL_INVERSE, modec);
14608 break;
14609
14610 case 's': /* standout */
14611 p = highlight_has_attr(id, HL_STANDOUT, modec);
14612 break;
14613
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014614 case 'u':
14615 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14616 /* underline */
14617 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14618 else
14619 /* undercurl */
14620 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 break;
14622 }
14623
14624 if (p != NULL)
14625 p = vim_strsave(p);
14626#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627 rettv->v_type = VAR_STRING;
14628 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629}
14630
14631/*
14632 * "synIDtrans(id)" function
14633 */
14634/*ARGSUSED*/
14635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014636f_synIDtrans(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 int id;
14641
14642#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644
14645 if (id > 0)
14646 id = syn_get_final_id(id);
14647 else
14648#endif
14649 id = 0;
14650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652}
14653
14654/*
14655 * "system()" function
14656 */
14657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014658f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014659 typval_T *argvars;
14660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014662 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014664 char_u *infile = NULL;
14665 char_u buf[NUMBUFLEN];
14666 int err = FALSE;
14667 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014669 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014670 {
14671 /*
14672 * Write the string to a temp file, to be used for input of the shell
14673 * command.
14674 */
14675 if ((infile = vim_tempname('i')) == NULL)
14676 {
14677 EMSG(_(e_notmp));
14678 return;
14679 }
14680
14681 fd = mch_fopen((char *)infile, WRITEBIN);
14682 if (fd == NULL)
14683 {
14684 EMSG2(_(e_notopen), infile);
14685 goto done;
14686 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014687 p = get_tv_string_buf_chk(&argvars[1], buf);
14688 if (p == NULL)
14689 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014690 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14691 err = TRUE;
14692 if (fclose(fd) != 0)
14693 err = TRUE;
14694 if (err)
14695 {
14696 EMSG(_("E677: Error writing temp file"));
14697 goto done;
14698 }
14699 }
14700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014701 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014702
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703#ifdef USE_CR
14704 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014705 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 {
14707 char_u *s;
14708
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014709 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 {
14711 if (*s == CAR)
14712 *s = NL;
14713 }
14714 }
14715#else
14716# ifdef USE_CRNL
14717 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014718 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 {
14720 char_u *s, *d;
14721
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014722 d = res;
14723 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 {
14725 if (s[0] == CAR && s[1] == NL)
14726 ++s;
14727 *d++ = *s;
14728 }
14729 *d = NUL;
14730 }
14731# endif
14732#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014733
14734done:
14735 if (infile != NULL)
14736 {
14737 mch_remove(infile);
14738 vim_free(infile);
14739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014740 rettv->v_type = VAR_STRING;
14741 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742}
14743
14744/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014745 * "tagfiles()" function
14746 */
14747/*ARGSUSED*/
14748 static void
14749f_tagfiles(argvars, rettv)
14750 typval_T *argvars;
14751 typval_T *rettv;
14752{
14753 char_u fname[MAXPATHL + 1];
14754 list_T *l;
14755
14756 l = list_alloc();
14757 if (l == NULL)
14758 {
14759 rettv->vval.v_number = 0;
14760 return;
14761 }
14762 rettv->vval.v_list = l;
14763 rettv->v_type = VAR_LIST;
14764 ++l->lv_refcount;
14765
14766 get_tagfname(TRUE, NULL);
14767 for (;;)
14768 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014769 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014770 break;
14771}
14772
14773/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014774 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014775 */
14776 static void
14777f_taglist(argvars, rettv)
14778 typval_T *argvars;
14779 typval_T *rettv;
14780{
14781 char_u *tag_pattern;
14782 list_T *l;
14783
14784 tag_pattern = get_tv_string(&argvars[0]);
14785
14786 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014787 if (*tag_pattern == NUL)
14788 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014789
14790 l = list_alloc();
14791 if (l != NULL)
14792 {
14793 if (get_tags(l, tag_pattern) != FAIL)
14794 {
14795 rettv->vval.v_list = l;
14796 rettv->v_type = VAR_LIST;
14797 ++l->lv_refcount;
14798 }
14799 else
14800 list_free(l);
14801 }
14802}
14803
14804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805 * "tempname()" function
14806 */
14807/*ARGSUSED*/
14808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014809f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014810 typval_T *argvars;
14811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812{
14813 static int x = 'A';
14814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014815 rettv->v_type = VAR_STRING;
14816 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817
14818 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14819 * names. Skip 'I' and 'O', they are used for shell redirection. */
14820 do
14821 {
14822 if (x == 'Z')
14823 x = '0';
14824 else if (x == '9')
14825 x = 'A';
14826 else
14827 {
14828#ifdef EBCDIC
14829 if (x == 'I')
14830 x = 'J';
14831 else if (x == 'R')
14832 x = 'S';
14833 else
14834#endif
14835 ++x;
14836 }
14837 } while (x == 'I' || x == 'O');
14838}
14839
14840/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014841 * "test(list)" function: Just checking the walls...
14842 */
14843/*ARGSUSED*/
14844 static void
14845f_test(argvars, rettv)
14846 typval_T *argvars;
14847 typval_T *rettv;
14848{
14849 /* Used for unit testing. Change the code below to your liking. */
14850#if 0
14851 listitem_T *li;
14852 list_T *l;
14853 char_u *bad, *good;
14854
14855 if (argvars[0].v_type != VAR_LIST)
14856 return;
14857 l = argvars[0].vval.v_list;
14858 if (l == NULL)
14859 return;
14860 li = l->lv_first;
14861 if (li == NULL)
14862 return;
14863 bad = get_tv_string(&li->li_tv);
14864 li = li->li_next;
14865 if (li == NULL)
14866 return;
14867 good = get_tv_string(&li->li_tv);
14868 rettv->vval.v_number = test_edit_score(bad, good);
14869#endif
14870}
14871
14872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 * "tolower(string)" function
14874 */
14875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014876f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014877 typval_T *argvars;
14878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879{
14880 char_u *p;
14881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014882 p = vim_strsave(get_tv_string(&argvars[0]));
14883 rettv->v_type = VAR_STRING;
14884 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885
14886 if (p != NULL)
14887 while (*p != NUL)
14888 {
14889#ifdef FEAT_MBYTE
14890 int l;
14891
14892 if (enc_utf8)
14893 {
14894 int c, lc;
14895
14896 c = utf_ptr2char(p);
14897 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014898 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 /* TODO: reallocate string when byte count changes. */
14900 if (utf_char2len(lc) == l)
14901 utf_char2bytes(lc, p);
14902 p += l;
14903 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014904 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 p += l; /* skip multi-byte character */
14906 else
14907#endif
14908 {
14909 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14910 ++p;
14911 }
14912 }
14913}
14914
14915/*
14916 * "toupper(string)" function
14917 */
14918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014919f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014920 typval_T *argvars;
14921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014924 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925}
14926
14927/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014928 * "tr(string, fromstr, tostr)" function
14929 */
14930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014931f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014932 typval_T *argvars;
14933 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014934{
14935 char_u *instr;
14936 char_u *fromstr;
14937 char_u *tostr;
14938 char_u *p;
14939#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014940 int inlen;
14941 int fromlen;
14942 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014943 int idx;
14944 char_u *cpstr;
14945 int cplen;
14946 int first = TRUE;
14947#endif
14948 char_u buf[NUMBUFLEN];
14949 char_u buf2[NUMBUFLEN];
14950 garray_T ga;
14951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014952 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014953 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14954 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014955
14956 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014957 rettv->v_type = VAR_STRING;
14958 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014959 if (fromstr == NULL || tostr == NULL)
14960 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014961 ga_init2(&ga, (int)sizeof(char), 80);
14962
14963#ifdef FEAT_MBYTE
14964 if (!has_mbyte)
14965#endif
14966 /* not multi-byte: fromstr and tostr must be the same length */
14967 if (STRLEN(fromstr) != STRLEN(tostr))
14968 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014969#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014970error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014971#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014972 EMSG2(_(e_invarg2), fromstr);
14973 ga_clear(&ga);
14974 return;
14975 }
14976
14977 /* fromstr and tostr have to contain the same number of chars */
14978 while (*instr != NUL)
14979 {
14980#ifdef FEAT_MBYTE
14981 if (has_mbyte)
14982 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014983 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014984 cpstr = instr;
14985 cplen = inlen;
14986 idx = 0;
14987 for (p = fromstr; *p != NUL; p += fromlen)
14988 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014989 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014990 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14991 {
14992 for (p = tostr; *p != NUL; p += tolen)
14993 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014994 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014995 if (idx-- == 0)
14996 {
14997 cplen = tolen;
14998 cpstr = p;
14999 break;
15000 }
15001 }
15002 if (*p == NUL) /* tostr is shorter than fromstr */
15003 goto error;
15004 break;
15005 }
15006 ++idx;
15007 }
15008
15009 if (first && cpstr == instr)
15010 {
15011 /* Check that fromstr and tostr have the same number of
15012 * (multi-byte) characters. Done only once when a character
15013 * of instr doesn't appear in fromstr. */
15014 first = FALSE;
15015 for (p = tostr; *p != NUL; p += tolen)
15016 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015017 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015018 --idx;
15019 }
15020 if (idx != 0)
15021 goto error;
15022 }
15023
15024 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015025 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015026 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015027
15028 instr += inlen;
15029 }
15030 else
15031#endif
15032 {
15033 /* When not using multi-byte chars we can do it faster. */
15034 p = vim_strchr(fromstr, *instr);
15035 if (p != NULL)
15036 ga_append(&ga, tostr[p - fromstr]);
15037 else
15038 ga_append(&ga, *instr);
15039 ++instr;
15040 }
15041 }
15042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015043 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015044}
15045
15046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 * "type(expr)" function
15048 */
15049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 typval_T *argvars;
15052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015054 int n;
15055
15056 switch (argvars[0].v_type)
15057 {
15058 case VAR_NUMBER: n = 0; break;
15059 case VAR_STRING: n = 1; break;
15060 case VAR_FUNC: n = 2; break;
15061 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015062 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015063 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15064 }
15065 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066}
15067
15068/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015069 * "values(dict)" function
15070 */
15071 static void
15072f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015073 typval_T *argvars;
15074 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015075{
15076 dict_list(argvars, rettv, 1);
15077}
15078
15079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 * "virtcol(string)" function
15081 */
15082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015084 typval_T *argvars;
15085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086{
15087 colnr_T vcol = 0;
15088 pos_T *fp;
15089
15090 fp = var2fpos(&argvars[0], FALSE);
15091 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15092 {
15093 getvvcol(curwin, fp, NULL, NULL, &vcol);
15094 ++vcol;
15095 }
15096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015097 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098}
15099
15100/*
15101 * "visualmode()" function
15102 */
15103/*ARGSUSED*/
15104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015105f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015106 typval_T *argvars;
15107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108{
15109#ifdef FEAT_VISUAL
15110 char_u str[2];
15111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015112 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 str[0] = curbuf->b_visual_mode_eval;
15114 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015115 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116
15117 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015118 if ((argvars[0].v_type == VAR_NUMBER
15119 && argvars[0].vval.v_number != 0)
15120 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015121 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 curbuf->b_visual_mode_eval = NUL;
15123#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125#endif
15126}
15127
15128/*
15129 * "winbufnr(nr)" function
15130 */
15131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015133 typval_T *argvars;
15134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135{
15136 win_T *wp;
15137
15138 wp = find_win_by_nr(&argvars[0]);
15139 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015142 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143}
15144
15145/*
15146 * "wincol()" function
15147 */
15148/*ARGSUSED*/
15149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015150f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015151 typval_T *argvars;
15152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153{
15154 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015155 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156}
15157
15158/*
15159 * "winheight(nr)" function
15160 */
15161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015162f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015163 typval_T *argvars;
15164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165{
15166 win_T *wp;
15167
15168 wp = find_win_by_nr(&argvars[0]);
15169 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015172 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173}
15174
15175/*
15176 * "winline()" function
15177 */
15178/*ARGSUSED*/
15179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015180f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015181 typval_T *argvars;
15182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183{
15184 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015185 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186}
15187
15188/*
15189 * "winnr()" function
15190 */
15191/* ARGSUSED */
15192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015193f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015194 typval_T *argvars;
15195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196{
15197 int nr = 1;
15198#ifdef FEAT_WINDOWS
15199 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015200 win_T *twin = curwin;
15201 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015203 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015205 arg = get_tv_string_chk(&argvars[0]);
15206 if (arg == NULL)
15207 nr = 0; /* type error; errmsg already given */
15208 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015209 twin = lastwin;
15210 else if (STRCMP(arg, "#") == 0)
15211 {
15212 twin = prevwin;
15213 if (prevwin == NULL)
15214 nr = 0;
15215 }
15216 else
15217 {
15218 EMSG2(_(e_invexpr2), arg);
15219 nr = 0;
15220 }
15221 }
15222
15223 if (nr > 0)
15224 for (wp = firstwin; wp != twin; wp = wp->w_next)
15225 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015227 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228}
15229
15230/*
15231 * "winrestcmd()" function
15232 */
15233/* ARGSUSED */
15234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015235f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015236 typval_T *argvars;
15237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238{
15239#ifdef FEAT_WINDOWS
15240 win_T *wp;
15241 int winnr = 1;
15242 garray_T ga;
15243 char_u buf[50];
15244
15245 ga_init2(&ga, (int)sizeof(char), 70);
15246 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15247 {
15248 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15249 ga_concat(&ga, buf);
15250# ifdef FEAT_VERTSPLIT
15251 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15252 ga_concat(&ga, buf);
15253# endif
15254 ++winnr;
15255 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015256 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015258 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015260 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015262 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263}
15264
15265/*
15266 * "winwidth(nr)" function
15267 */
15268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015270 typval_T *argvars;
15271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272{
15273 win_T *wp;
15274
15275 wp = find_win_by_nr(&argvars[0]);
15276 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015277 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 else
15279#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015280 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283#endif
15284}
15285
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015287 * "writefile()" function
15288 */
15289 static void
15290f_writefile(argvars, rettv)
15291 typval_T *argvars;
15292 typval_T *rettv;
15293{
15294 int binary = FALSE;
15295 char_u *fname;
15296 FILE *fd;
15297 listitem_T *li;
15298 char_u *s;
15299 int ret = 0;
15300 int c;
15301
15302 if (argvars[0].v_type != VAR_LIST)
15303 {
15304 EMSG2(_(e_listarg), "writefile()");
15305 return;
15306 }
15307 if (argvars[0].vval.v_list == NULL)
15308 return;
15309
15310 if (argvars[2].v_type != VAR_UNKNOWN
15311 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15312 binary = TRUE;
15313
15314 /* Always open the file in binary mode, library functions have a mind of
15315 * their own about CR-LF conversion. */
15316 fname = get_tv_string(&argvars[1]);
15317 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15318 {
15319 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15320 ret = -1;
15321 }
15322 else
15323 {
15324 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15325 li = li->li_next)
15326 {
15327 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15328 {
15329 if (*s == '\n')
15330 c = putc(NUL, fd);
15331 else
15332 c = putc(*s, fd);
15333 if (c == EOF)
15334 {
15335 ret = -1;
15336 break;
15337 }
15338 }
15339 if (!binary || li->li_next != NULL)
15340 if (putc('\n', fd) == EOF)
15341 {
15342 ret = -1;
15343 break;
15344 }
15345 if (ret < 0)
15346 {
15347 EMSG(_(e_write));
15348 break;
15349 }
15350 }
15351 fclose(fd);
15352 }
15353
15354 rettv->vval.v_number = ret;
15355}
15356
15357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 * Translate a String variable into a position.
15359 */
15360 static pos_T *
15361var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015362 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363 int lnum; /* TRUE when $ is last line */
15364{
15365 char_u *name;
15366 static pos_T pos;
15367 pos_T *pp;
15368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015369 name = get_tv_string_chk(varp);
15370 if (name == NULL)
15371 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372 if (name[0] == '.') /* cursor */
15373 return &curwin->w_cursor;
15374 if (name[0] == '\'') /* mark */
15375 {
15376 pp = getmark(name[1], FALSE);
15377 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15378 return NULL;
15379 return pp;
15380 }
15381 if (name[0] == '$') /* last column or line */
15382 {
15383 if (lnum)
15384 {
15385 pos.lnum = curbuf->b_ml.ml_line_count;
15386 pos.col = 0;
15387 }
15388 else
15389 {
15390 pos.lnum = curwin->w_cursor.lnum;
15391 pos.col = (colnr_T)STRLEN(ml_get_curline());
15392 }
15393 return &pos;
15394 }
15395 return NULL;
15396}
15397
15398/*
15399 * Get the length of an environment variable name.
15400 * Advance "arg" to the first character after the name.
15401 * Return 0 for error.
15402 */
15403 static int
15404get_env_len(arg)
15405 char_u **arg;
15406{
15407 char_u *p;
15408 int len;
15409
15410 for (p = *arg; vim_isIDc(*p); ++p)
15411 ;
15412 if (p == *arg) /* no name found */
15413 return 0;
15414
15415 len = (int)(p - *arg);
15416 *arg = p;
15417 return len;
15418}
15419
15420/*
15421 * Get the length of the name of a function or internal variable.
15422 * "arg" is advanced to the first non-white character after the name.
15423 * Return 0 if something is wrong.
15424 */
15425 static int
15426get_id_len(arg)
15427 char_u **arg;
15428{
15429 char_u *p;
15430 int len;
15431
15432 /* Find the end of the name. */
15433 for (p = *arg; eval_isnamec(*p); ++p)
15434 ;
15435 if (p == *arg) /* no name found */
15436 return 0;
15437
15438 len = (int)(p - *arg);
15439 *arg = skipwhite(p);
15440
15441 return len;
15442}
15443
15444/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015445 * Get the length of the name of a variable or function.
15446 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015448 * Return -1 if curly braces expansion failed.
15449 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450 * If the name contains 'magic' {}'s, expand them and return the
15451 * expanded name in an allocated string via 'alias' - caller must free.
15452 */
15453 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015454get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 char_u **arg;
15456 char_u **alias;
15457 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015458 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459{
15460 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 char_u *p;
15462 char_u *expr_start;
15463 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464
15465 *alias = NULL; /* default to no alias */
15466
15467 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15468 && (*arg)[2] == (int)KE_SNR)
15469 {
15470 /* hard coded <SNR>, already translated */
15471 *arg += 3;
15472 return get_id_len(arg) + 3;
15473 }
15474 len = eval_fname_script(*arg);
15475 if (len > 0)
15476 {
15477 /* literal "<SID>", "s:" or "<SNR>" */
15478 *arg += len;
15479 }
15480
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015482 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015484 p = find_name_end(*arg, &expr_start, &expr_end,
15485 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486 if (expr_start != NULL)
15487 {
15488 char_u *temp_string;
15489
15490 if (!evaluate)
15491 {
15492 len += (int)(p - *arg);
15493 *arg = skipwhite(p);
15494 return len;
15495 }
15496
15497 /*
15498 * Include any <SID> etc in the expanded string:
15499 * Thus the -len here.
15500 */
15501 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15502 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015503 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 *alias = temp_string;
15505 *arg = skipwhite(p);
15506 return (int)STRLEN(temp_string);
15507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508
15509 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015510 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 EMSG2(_(e_invexpr2), *arg);
15512
15513 return len;
15514}
15515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516/*
15517 * Find the end of a variable or function name, taking care of magic braces.
15518 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15519 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015520 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521 * Return a pointer to just after the name. Equal to "arg" if there is no
15522 * valid name.
15523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015525find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 char_u *arg;
15527 char_u **expr_start;
15528 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015529 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531 int mb_nest = 0;
15532 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 char_u *p;
15534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015535 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 *expr_start = NULL;
15538 *expr_end = NULL;
15539 }
15540
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015541 /* Quick check for valid starting character. */
15542 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15543 return arg;
15544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545 for (p = arg; *p != NUL
15546 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015547 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015548 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015550 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015552 if (*p == '\'')
15553 {
15554 /* skip over 'string' to avoid counting [ and ] inside it. */
15555 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15556 ;
15557 if (*p == NUL)
15558 break;
15559 }
15560 else if (*p == '"')
15561 {
15562 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15563 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15564 if (*p == '\\' && p[1] != NUL)
15565 ++p;
15566 if (*p == NUL)
15567 break;
15568 }
15569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015570 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015572 if (*p == '[')
15573 ++br_nest;
15574 else if (*p == ']')
15575 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015578 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015580 if (*p == '{')
15581 {
15582 mb_nest++;
15583 if (expr_start != NULL && *expr_start == NULL)
15584 *expr_start = p;
15585 }
15586 else if (*p == '}')
15587 {
15588 mb_nest--;
15589 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15590 *expr_end = p;
15591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 }
15594
15595 return p;
15596}
15597
15598/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015599 * Expands out the 'magic' {}'s in a variable/function name.
15600 * Note that this can call itself recursively, to deal with
15601 * constructs like foo{bar}{baz}{bam}
15602 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15603 * "in_start" ^
15604 * "expr_start" ^
15605 * "expr_end" ^
15606 * "in_end" ^
15607 *
15608 * Returns a new allocated string, which the caller must free.
15609 * Returns NULL for failure.
15610 */
15611 static char_u *
15612make_expanded_name(in_start, expr_start, expr_end, in_end)
15613 char_u *in_start;
15614 char_u *expr_start;
15615 char_u *expr_end;
15616 char_u *in_end;
15617{
15618 char_u c1;
15619 char_u *retval = NULL;
15620 char_u *temp_result;
15621 char_u *nextcmd = NULL;
15622
15623 if (expr_end == NULL || in_end == NULL)
15624 return NULL;
15625 *expr_start = NUL;
15626 *expr_end = NUL;
15627 c1 = *in_end;
15628 *in_end = NUL;
15629
15630 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15631 if (temp_result != NULL && nextcmd == NULL)
15632 {
15633 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15634 + (in_end - expr_end) + 1));
15635 if (retval != NULL)
15636 {
15637 STRCPY(retval, in_start);
15638 STRCAT(retval, temp_result);
15639 STRCAT(retval, expr_end + 1);
15640 }
15641 }
15642 vim_free(temp_result);
15643
15644 *in_end = c1; /* put char back for error messages */
15645 *expr_start = '{';
15646 *expr_end = '}';
15647
15648 if (retval != NULL)
15649 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015650 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015651 if (expr_start != NULL)
15652 {
15653 /* Further expansion! */
15654 temp_result = make_expanded_name(retval, expr_start,
15655 expr_end, temp_result);
15656 vim_free(retval);
15657 retval = temp_result;
15658 }
15659 }
15660
15661 return retval;
15662}
15663
15664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015666 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 */
15668 static int
15669eval_isnamec(c)
15670 int c;
15671{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015672 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15673}
15674
15675/*
15676 * Return TRUE if character "c" can be used as the first character in a
15677 * variable or function name (excluding '{' and '}').
15678 */
15679 static int
15680eval_isnamec1(c)
15681 int c;
15682{
15683 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684}
15685
15686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 * Set number v: variable to "val".
15688 */
15689 void
15690set_vim_var_nr(idx, val)
15691 int idx;
15692 long val;
15693{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015694 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695}
15696
15697/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015698 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 */
15700 long
15701get_vim_var_nr(idx)
15702 int idx;
15703{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015704 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705}
15706
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015707#if defined(FEAT_AUTOCMD) || defined(PROTO)
15708/*
15709 * Get string v: variable value. Uses a static buffer, can only be used once.
15710 */
15711 char_u *
15712get_vim_var_str(idx)
15713 int idx;
15714{
15715 return get_tv_string(&vimvars[idx].vv_tv);
15716}
15717#endif
15718
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719/*
15720 * Set v:count, v:count1 and v:prevcount.
15721 */
15722 void
15723set_vcount(count, count1)
15724 long count;
15725 long count1;
15726{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015727 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15728 vimvars[VV_COUNT].vv_nr = count;
15729 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730}
15731
15732/*
15733 * Set string v: variable to a copy of "val".
15734 */
15735 void
15736set_vim_var_string(idx, val, len)
15737 int idx;
15738 char_u *val;
15739 int len; /* length of "val" to use or -1 (whole string) */
15740{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015741 /* Need to do this (at least) once, since we can't initialize a union.
15742 * Will always be invoked when "v:progname" is set. */
15743 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15744
Bram Moolenaare9a41262005-01-15 22:18:47 +000015745 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015747 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015749 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015751 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752}
15753
15754/*
15755 * Set v:register if needed.
15756 */
15757 void
15758set_reg_var(c)
15759 int c;
15760{
15761 char_u regname;
15762
15763 if (c == 0 || c == ' ')
15764 regname = '"';
15765 else
15766 regname = c;
15767 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015768 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 set_vim_var_string(VV_REG, &regname, 1);
15770}
15771
15772/*
15773 * Get or set v:exception. If "oldval" == NULL, return the current value.
15774 * Otherwise, restore the value to "oldval" and return NULL.
15775 * Must always be called in pairs to save and restore v:exception! Does not
15776 * take care of memory allocations.
15777 */
15778 char_u *
15779v_exception(oldval)
15780 char_u *oldval;
15781{
15782 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015783 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784
Bram Moolenaare9a41262005-01-15 22:18:47 +000015785 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 return NULL;
15787}
15788
15789/*
15790 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15791 * Otherwise, restore the value to "oldval" and return NULL.
15792 * Must always be called in pairs to save and restore v:throwpoint! Does not
15793 * take care of memory allocations.
15794 */
15795 char_u *
15796v_throwpoint(oldval)
15797 char_u *oldval;
15798{
15799 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015800 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801
Bram Moolenaare9a41262005-01-15 22:18:47 +000015802 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 return NULL;
15804}
15805
15806#if defined(FEAT_AUTOCMD) || defined(PROTO)
15807/*
15808 * Set v:cmdarg.
15809 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15810 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15811 * Must always be called in pairs!
15812 */
15813 char_u *
15814set_cmdarg(eap, oldarg)
15815 exarg_T *eap;
15816 char_u *oldarg;
15817{
15818 char_u *oldval;
15819 char_u *newval;
15820 unsigned len;
15821
Bram Moolenaare9a41262005-01-15 22:18:47 +000015822 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015823 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015825 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015826 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015827 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 }
15829
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015830 if (eap->force_bin == FORCE_BIN)
15831 len = 6;
15832 else if (eap->force_bin == FORCE_NOBIN)
15833 len = 8;
15834 else
15835 len = 0;
15836 if (eap->force_ff != 0)
15837 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15838# ifdef FEAT_MBYTE
15839 if (eap->force_enc != 0)
15840 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015841 if (eap->bad_char != 0)
15842 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015843# endif
15844
15845 newval = alloc(len + 1);
15846 if (newval == NULL)
15847 return NULL;
15848
15849 if (eap->force_bin == FORCE_BIN)
15850 sprintf((char *)newval, " ++bin");
15851 else if (eap->force_bin == FORCE_NOBIN)
15852 sprintf((char *)newval, " ++nobin");
15853 else
15854 *newval = NUL;
15855 if (eap->force_ff != 0)
15856 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15857 eap->cmd + eap->force_ff);
15858# ifdef FEAT_MBYTE
15859 if (eap->force_enc != 0)
15860 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15861 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015862 if (eap->bad_char != 0)
15863 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
15864 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015865# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015866 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015867 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868}
15869#endif
15870
15871/*
15872 * Get the value of internal variable "name".
15873 * Return OK or FAIL.
15874 */
15875 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015876get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 char_u *name;
15878 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015879 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015880 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881{
15882 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015883 typval_T *tv = NULL;
15884 typval_T atv;
15885 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887
15888 /* truncate the name, so that we can use strcmp() */
15889 cc = name[len];
15890 name[len] = NUL;
15891
15892 /*
15893 * Check for "b:changedtick".
15894 */
15895 if (STRCMP(name, "b:changedtick") == 0)
15896 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015897 atv.v_type = VAR_NUMBER;
15898 atv.vval.v_number = curbuf->b_changedtick;
15899 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 }
15901
15902 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 * Check for user-defined variables.
15904 */
15905 else
15906 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015907 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015909 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910 }
15911
Bram Moolenaare9a41262005-01-15 22:18:47 +000015912 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015914 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015915 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 ret = FAIL;
15917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015918 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015919 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015920
15921 name[len] = cc;
15922
15923 return ret;
15924}
15925
15926/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015927 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15928 * Also handle function call with Funcref variable: func(expr)
15929 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15930 */
15931 static int
15932handle_subscript(arg, rettv, evaluate, verbose)
15933 char_u **arg;
15934 typval_T *rettv;
15935 int evaluate; /* do more than finding the end */
15936 int verbose; /* give error messages */
15937{
15938 int ret = OK;
15939 dict_T *selfdict = NULL;
15940 char_u *s;
15941 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015942 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015943
15944 while (ret == OK
15945 && (**arg == '['
15946 || (**arg == '.' && rettv->v_type == VAR_DICT)
15947 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15948 && !vim_iswhite(*(*arg - 1)))
15949 {
15950 if (**arg == '(')
15951 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015952 /* need to copy the funcref so that we can clear rettv */
15953 functv = *rettv;
15954 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015955
15956 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015957 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015958 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015959 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15960 &len, evaluate, selfdict);
15961
15962 /* Clear the funcref afterwards, so that deleting it while
15963 * evaluating the arguments is possible (see test55). */
15964 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015965
15966 /* Stop the expression evaluation when immediately aborting on
15967 * error, or when an interrupt occurred or an exception was thrown
15968 * but not caught. */
15969 if (aborting())
15970 {
15971 if (ret == OK)
15972 clear_tv(rettv);
15973 ret = FAIL;
15974 }
15975 dict_unref(selfdict);
15976 selfdict = NULL;
15977 }
15978 else /* **arg == '[' || **arg == '.' */
15979 {
15980 dict_unref(selfdict);
15981 if (rettv->v_type == VAR_DICT)
15982 {
15983 selfdict = rettv->vval.v_dict;
15984 if (selfdict != NULL)
15985 ++selfdict->dv_refcount;
15986 }
15987 else
15988 selfdict = NULL;
15989 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15990 {
15991 clear_tv(rettv);
15992 ret = FAIL;
15993 }
15994 }
15995 }
15996 dict_unref(selfdict);
15997 return ret;
15998}
15999
16000/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016001 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16002 * value).
16003 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016004 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016006{
Bram Moolenaar33570922005-01-25 22:26:29 +000016007 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016008}
16009
16010/*
16011 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 * The string "s" must have been allocated, it is consumed.
16013 * Return NULL for out of memory, the variable otherwise.
16014 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016015 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016016alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 char_u *s;
16018{
Bram Moolenaar33570922005-01-25 22:26:29 +000016019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016021 rettv = alloc_tv();
16022 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016024 rettv->v_type = VAR_STRING;
16025 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026 }
16027 else
16028 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016029 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030}
16031
16032/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016033 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016035 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016036free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016037 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038{
16039 if (varp != NULL)
16040 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016041 switch (varp->v_type)
16042 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016043 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016044 func_unref(varp->vval.v_string);
16045 /*FALLTHROUGH*/
16046 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016047 vim_free(varp->vval.v_string);
16048 break;
16049 case VAR_LIST:
16050 list_unref(varp->vval.v_list);
16051 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016052 case VAR_DICT:
16053 dict_unref(varp->vval.v_dict);
16054 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016055 case VAR_NUMBER:
16056 case VAR_UNKNOWN:
16057 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016058 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016059 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016060 break;
16061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 vim_free(varp);
16063 }
16064}
16065
16066/*
16067 * Free the memory for a variable value and set the value to NULL or 0.
16068 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016069 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016071 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072{
16073 if (varp != NULL)
16074 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016075 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016077 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016078 func_unref(varp->vval.v_string);
16079 /*FALLTHROUGH*/
16080 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016081 vim_free(varp->vval.v_string);
16082 varp->vval.v_string = NULL;
16083 break;
16084 case VAR_LIST:
16085 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016086 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016087 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016088 case VAR_DICT:
16089 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016090 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016091 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016093 varp->vval.v_number = 0;
16094 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016095 case VAR_UNKNOWN:
16096 break;
16097 default:
16098 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016100 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 }
16102}
16103
16104/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016105 * Set the value of a variable to NULL without freeing items.
16106 */
16107 static void
16108init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016109 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016110{
16111 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016112 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016113}
16114
16115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 * Get the number value of a variable.
16117 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 * For incompatible types, return 0.
16119 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16120 * caller of incompatible types: it sets *denote to TRUE if "denote"
16121 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 */
16123 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016124get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016125 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016127 int error = FALSE;
16128
16129 return get_tv_number_chk(varp, &error); /* return 0L on error */
16130}
16131
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016132 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016133get_tv_number_chk(varp, denote)
16134 typval_T *varp;
16135 int *denote;
16136{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016137 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016139 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016141 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016142 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016143 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016144 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016145 break;
16146 case VAR_STRING:
16147 if (varp->vval.v_string != NULL)
16148 vim_str2nr(varp->vval.v_string, NULL, NULL,
16149 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016150 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016151 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016152 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016153 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016154 case VAR_DICT:
16155 EMSG(_("E728: Using a Dictionary as a number"));
16156 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016157 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016158 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016159 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016161 if (denote == NULL) /* useful for values that must be unsigned */
16162 n = -1;
16163 else
16164 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016165 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166}
16167
16168/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016169 * Get the lnum from the first argument.
16170 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016171 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 */
16173 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016174get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016175 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176{
Bram Moolenaar33570922005-01-25 22:26:29 +000016177 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178 linenr_T lnum;
16179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016180 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181 if (lnum == 0) /* no valid number, try using line() */
16182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183 rettv.v_type = VAR_NUMBER;
16184 f_line(argvars, &rettv);
16185 lnum = rettv.vval.v_number;
16186 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 }
16188 return lnum;
16189}
16190
16191/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016192 * Get the lnum from the first argument.
16193 * Also accepts "$", then "buf" is used.
16194 * Returns 0 on error.
16195 */
16196 static linenr_T
16197get_tv_lnum_buf(argvars, buf)
16198 typval_T *argvars;
16199 buf_T *buf;
16200{
16201 if (argvars[0].v_type == VAR_STRING
16202 && argvars[0].vval.v_string != NULL
16203 && argvars[0].vval.v_string[0] == '$'
16204 && buf != NULL)
16205 return buf->b_ml.ml_line_count;
16206 return get_tv_number_chk(&argvars[0], NULL);
16207}
16208
16209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 * Get the string value of a variable.
16211 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016212 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16213 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214 * If the String variable has never been set, return an empty string.
16215 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016216 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16217 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218 */
16219 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016220get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016221 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222{
16223 static char_u mybuf[NUMBUFLEN];
16224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016225 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226}
16227
16228 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016229get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016230 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016231 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016233 char_u *res = get_tv_string_buf_chk(varp, buf);
16234
16235 return res != NULL ? res : (char_u *)"";
16236}
16237
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016238 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016239get_tv_string_chk(varp)
16240 typval_T *varp;
16241{
16242 static char_u mybuf[NUMBUFLEN];
16243
16244 return get_tv_string_buf_chk(varp, mybuf);
16245}
16246
16247 static char_u *
16248get_tv_string_buf_chk(varp, buf)
16249 typval_T *varp;
16250 char_u *buf;
16251{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016252 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016254 case VAR_NUMBER:
16255 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16256 return buf;
16257 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016258 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016259 break;
16260 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016261 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016262 break;
16263 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016264 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016265 break;
16266 case VAR_STRING:
16267 if (varp->vval.v_string != NULL)
16268 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016269 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016270 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016271 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016274 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275}
16276
16277/*
16278 * Find variable "name" in the list of variables.
16279 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016280 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016281 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016284 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016285find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016287 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016290 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291
Bram Moolenaara7043832005-01-21 11:56:39 +000016292 ht = find_var_ht(name, &varname);
16293 if (htp != NULL)
16294 *htp = ht;
16295 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016297 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298}
16299
16300/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016301 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016302 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016305find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016306 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016307 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016308 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016309{
Bram Moolenaar33570922005-01-25 22:26:29 +000016310 hashitem_T *hi;
16311
16312 if (*varname == NUL)
16313 {
16314 /* Must be something like "s:", otherwise "ht" would be NULL. */
16315 switch (varname[-2])
16316 {
16317 case 's': return &SCRIPT_SV(current_SID).sv_var;
16318 case 'g': return &globvars_var;
16319 case 'v': return &vimvars_var;
16320 case 'b': return &curbuf->b_bufvar;
16321 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016322 case 'l': return current_funccal == NULL
16323 ? NULL : &current_funccal->l_vars_var;
16324 case 'a': return current_funccal == NULL
16325 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016326 }
16327 return NULL;
16328 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016329
16330 hi = hash_find(ht, varname);
16331 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016332 {
16333 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016334 * worked find the variable again. Don't auto-load a script if it was
16335 * loaded already, otherwise it would be loaded every time when
16336 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016337 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016338 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016339 hi = hash_find(ht, varname);
16340 if (HASHITEM_EMPTY(hi))
16341 return NULL;
16342 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016344}
16345
16346/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016347 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016348 * Set "varname" to the start of name without ':'.
16349 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016350 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016351find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 char_u *name;
16353 char_u **varname;
16354{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016355 hashitem_T *hi;
16356
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 if (name[1] != ':')
16358 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016359 /* The name must not start with a colon or #. */
16360 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 return NULL;
16362 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016363
16364 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016365 hi = hash_find(&compat_hashtab, name);
16366 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016367 return &compat_hashtab;
16368
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016370 return &globvarht; /* global variable */
16371 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372 }
16373 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016374 if (*name == 'g') /* global variable */
16375 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016376 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16377 */
16378 if (vim_strchr(name + 2, ':') != NULL
16379 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016380 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016382 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016384 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016385 if (*name == 'v') /* v: variable */
16386 return &vimvarht;
16387 if (*name == 'a' && current_funccal != NULL) /* function argument */
16388 return &current_funccal->l_avars.dv_hashtab;
16389 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16390 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 if (*name == 's' /* script variable */
16392 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16393 return &SCRIPT_VARS(current_SID);
16394 return NULL;
16395}
16396
16397/*
16398 * Get the string value of a (global/local) variable.
16399 * Returns NULL when it doesn't exist.
16400 */
16401 char_u *
16402get_var_value(name)
16403 char_u *name;
16404{
Bram Moolenaar33570922005-01-25 22:26:29 +000016405 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406
Bram Moolenaara7043832005-01-21 11:56:39 +000016407 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408 if (v == NULL)
16409 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016410 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411}
16412
16413/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016414 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 * sourcing this script and when executing functions defined in the script.
16416 */
16417 void
16418new_script_vars(id)
16419 scid_T id;
16420{
Bram Moolenaara7043832005-01-21 11:56:39 +000016421 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016422 hashtab_T *ht;
16423 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016424
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16426 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016427 /* Re-allocating ga_data means that an ht_array pointing to
16428 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016429 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016430 for (i = 1; i <= ga_scripts.ga_len; ++i)
16431 {
16432 ht = &SCRIPT_VARS(i);
16433 if (ht->ht_mask == HT_INIT_SIZE - 1)
16434 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016435 sv = &SCRIPT_SV(i);
16436 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016437 }
16438
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 while (ga_scripts.ga_len < id)
16440 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016441 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16442 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 }
16445 }
16446}
16447
16448/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16450 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 */
16452 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016453init_var_dict(dict, dict_var)
16454 dict_T *dict;
16455 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456{
Bram Moolenaar33570922005-01-25 22:26:29 +000016457 hash_init(&dict->dv_hashtab);
16458 dict->dv_refcount = 99999;
16459 dict_var->di_tv.vval.v_dict = dict;
16460 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016461 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16463 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464}
16465
16466/*
16467 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016468 * Frees all allocated variables and the value they contain.
16469 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470 */
16471 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016472vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016473 hashtab_T *ht;
16474{
16475 vars_clear_ext(ht, TRUE);
16476}
16477
16478/*
16479 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16480 */
16481 static void
16482vars_clear_ext(ht, free_val)
16483 hashtab_T *ht;
16484 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485{
Bram Moolenaara7043832005-01-21 11:56:39 +000016486 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016487 hashitem_T *hi;
16488 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016491 todo = ht->ht_used;
16492 for (hi = ht->ht_array; todo > 0; ++hi)
16493 {
16494 if (!HASHITEM_EMPTY(hi))
16495 {
16496 --todo;
16497
Bram Moolenaar33570922005-01-25 22:26:29 +000016498 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016499 * ht_array might change then. hash_clear() takes care of it
16500 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016501 v = HI2DI(hi);
16502 if (free_val)
16503 clear_tv(&v->di_tv);
16504 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16505 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016506 }
16507 }
16508 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016509 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510}
16511
Bram Moolenaara7043832005-01-21 11:56:39 +000016512/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 * Delete a variable from hashtab "ht" at item "hi".
16514 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016517delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016518 hashtab_T *ht;
16519 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520{
Bram Moolenaar33570922005-01-25 22:26:29 +000016521 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016522
16523 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 clear_tv(&di->di_tv);
16525 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526}
16527
16528/*
16529 * List the value of one internal variable.
16530 */
16531 static void
16532list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016533 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534 char_u *prefix;
16535{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016536 char_u *tofree;
16537 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016538 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016539
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016540 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016541 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016542 s == NULL ? (char_u *)"" : s);
16543 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544}
16545
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 static void
16547list_one_var_a(prefix, name, type, string)
16548 char_u *prefix;
16549 char_u *name;
16550 int type;
16551 char_u *string;
16552{
16553 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16554 if (name != NULL) /* "a:" vars don't have a name stored */
16555 msg_puts(name);
16556 msg_putchar(' ');
16557 msg_advance(22);
16558 if (type == VAR_NUMBER)
16559 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016560 else if (type == VAR_FUNC)
16561 msg_putchar('*');
16562 else if (type == VAR_LIST)
16563 {
16564 msg_putchar('[');
16565 if (*string == '[')
16566 ++string;
16567 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016568 else if (type == VAR_DICT)
16569 {
16570 msg_putchar('{');
16571 if (*string == '{')
16572 ++string;
16573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 else
16575 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016576
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016578
16579 if (type == VAR_FUNC)
16580 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581}
16582
16583/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016584 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585 * If the variable already exists, the value is updated.
16586 * Otherwise the variable is created.
16587 */
16588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016589set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016591 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016592 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593{
Bram Moolenaar33570922005-01-25 22:26:29 +000016594 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016597 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016599 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016600 {
16601 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16602 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16603 ? name[2] : name[0]))
16604 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016605 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016606 return;
16607 }
16608 if (function_exists(name))
16609 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016610 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016611 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016612 return;
16613 }
16614 }
16615
Bram Moolenaara7043832005-01-21 11:56:39 +000016616 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016617 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016618 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016619 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016620 return;
16621 }
16622
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016623 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016624 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016626 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016627 if (var_check_ro(v->di_flags, name)
16628 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016629 return;
16630 if (v->di_tv.v_type != tv->v_type
16631 && !((v->di_tv.v_type == VAR_STRING
16632 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633 && (tv->v_type == VAR_STRING
16634 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016635 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016636 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016637 return;
16638 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016639
16640 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016641 * Handle setting internal v: variables separately: we don't change
16642 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016643 */
16644 if (ht == &vimvarht)
16645 {
16646 if (v->di_tv.v_type == VAR_STRING)
16647 {
16648 vim_free(v->di_tv.vval.v_string);
16649 if (copy || tv->v_type != VAR_STRING)
16650 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16651 else
16652 {
16653 /* Take over the string to avoid an extra alloc/free. */
16654 v->di_tv.vval.v_string = tv->vval.v_string;
16655 tv->vval.v_string = NULL;
16656 }
16657 }
16658 else if (v->di_tv.v_type != VAR_NUMBER)
16659 EMSG2(_(e_intern2), "set_var()");
16660 else
16661 v->di_tv.vval.v_number = get_tv_number(tv);
16662 return;
16663 }
16664
16665 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666 }
16667 else /* add a new variable */
16668 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016669 /* Make sure the variable name is valid. */
16670 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016671 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16672 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016673 {
16674 EMSG2(_(e_illvar), varname);
16675 return;
16676 }
16677
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016678 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16679 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016680 if (v == NULL)
16681 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016682 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016685 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686 return;
16687 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016688 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016691 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016692 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016693 else
16694 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016695 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016696 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016697 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699}
16700
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016701/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16703 * Also give an error message.
16704 */
16705 static int
16706var_check_ro(flags, name)
16707 int flags;
16708 char_u *name;
16709{
16710 if (flags & DI_FLAGS_RO)
16711 {
16712 EMSG2(_(e_readonlyvar), name);
16713 return TRUE;
16714 }
16715 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16716 {
16717 EMSG2(_(e_readonlysbx), name);
16718 return TRUE;
16719 }
16720 return FALSE;
16721}
16722
16723/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016724 * Return TRUE if typeval "tv" is set to be locked (immutable).
16725 * Also give an error message, using "name".
16726 */
16727 static int
16728tv_check_lock(lock, name)
16729 int lock;
16730 char_u *name;
16731{
16732 if (lock & VAR_LOCKED)
16733 {
16734 EMSG2(_("E741: Value is locked: %s"),
16735 name == NULL ? (char_u *)_("Unknown") : name);
16736 return TRUE;
16737 }
16738 if (lock & VAR_FIXED)
16739 {
16740 EMSG2(_("E742: Cannot change value of %s"),
16741 name == NULL ? (char_u *)_("Unknown") : name);
16742 return TRUE;
16743 }
16744 return FALSE;
16745}
16746
16747/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016748 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016749 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016750 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016753copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016754 typval_T *from;
16755 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016757 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016758 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016759 switch (from->v_type)
16760 {
16761 case VAR_NUMBER:
16762 to->vval.v_number = from->vval.v_number;
16763 break;
16764 case VAR_STRING:
16765 case VAR_FUNC:
16766 if (from->vval.v_string == NULL)
16767 to->vval.v_string = NULL;
16768 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016769 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016770 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016771 if (from->v_type == VAR_FUNC)
16772 func_ref(to->vval.v_string);
16773 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016774 break;
16775 case VAR_LIST:
16776 if (from->vval.v_list == NULL)
16777 to->vval.v_list = NULL;
16778 else
16779 {
16780 to->vval.v_list = from->vval.v_list;
16781 ++to->vval.v_list->lv_refcount;
16782 }
16783 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016784 case VAR_DICT:
16785 if (from->vval.v_dict == NULL)
16786 to->vval.v_dict = NULL;
16787 else
16788 {
16789 to->vval.v_dict = from->vval.v_dict;
16790 ++to->vval.v_dict->dv_refcount;
16791 }
16792 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016793 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016794 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016795 break;
16796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797}
16798
16799/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016800 * Make a copy of an item.
16801 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016802 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16803 * reference to an already copied list/dict can be used.
16804 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016805 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016806 static int
16807item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016808 typval_T *from;
16809 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016810 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016811 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016812{
16813 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016814 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016815
Bram Moolenaar33570922005-01-25 22:26:29 +000016816 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016817 {
16818 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016819 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016820 }
16821 ++recurse;
16822
16823 switch (from->v_type)
16824 {
16825 case VAR_NUMBER:
16826 case VAR_STRING:
16827 case VAR_FUNC:
16828 copy_tv(from, to);
16829 break;
16830 case VAR_LIST:
16831 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016832 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016833 if (from->vval.v_list == NULL)
16834 to->vval.v_list = NULL;
16835 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16836 {
16837 /* use the copy made earlier */
16838 to->vval.v_list = from->vval.v_list->lv_copylist;
16839 ++to->vval.v_list->lv_refcount;
16840 }
16841 else
16842 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16843 if (to->vval.v_list == NULL)
16844 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016845 break;
16846 case VAR_DICT:
16847 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016848 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016849 if (from->vval.v_dict == NULL)
16850 to->vval.v_dict = NULL;
16851 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16852 {
16853 /* use the copy made earlier */
16854 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16855 ++to->vval.v_dict->dv_refcount;
16856 }
16857 else
16858 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16859 if (to->vval.v_dict == NULL)
16860 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016861 break;
16862 default:
16863 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016864 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016865 }
16866 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016867 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016868}
16869
16870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 * ":echo expr1 ..." print each argument separated with a space, add a
16872 * newline at the end.
16873 * ":echon expr1 ..." print each argument plain.
16874 */
16875 void
16876ex_echo(eap)
16877 exarg_T *eap;
16878{
16879 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016880 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016881 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 char_u *p;
16883 int needclr = TRUE;
16884 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016885 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886
16887 if (eap->skip)
16888 ++emsg_skip;
16889 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16890 {
16891 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016892 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 {
16894 /*
16895 * Report the invalid expression unless the expression evaluation
16896 * has been cancelled due to an aborting error, an interrupt, or an
16897 * exception.
16898 */
16899 if (!aborting())
16900 EMSG2(_(e_invexpr2), p);
16901 break;
16902 }
16903 if (!eap->skip)
16904 {
16905 if (atstart)
16906 {
16907 atstart = FALSE;
16908 /* Call msg_start() after eval1(), evaluating the expression
16909 * may cause a message to appear. */
16910 if (eap->cmdidx == CMD_echo)
16911 msg_start();
16912 }
16913 else if (eap->cmdidx == CMD_echo)
16914 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016915 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016916 if (p != NULL)
16917 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016919 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016921 if (*p != TAB && needclr)
16922 {
16923 /* remove any text still there from the command */
16924 msg_clr_eos();
16925 needclr = FALSE;
16926 }
16927 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 }
16929 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016930 {
16931#ifdef FEAT_MBYTE
16932 if (has_mbyte)
16933 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016934 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016935
16936 (void)msg_outtrans_len_attr(p, i, echo_attr);
16937 p += i - 1;
16938 }
16939 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016941 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016944 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016946 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 arg = skipwhite(arg);
16948 }
16949 eap->nextcmd = check_nextcmd(arg);
16950
16951 if (eap->skip)
16952 --emsg_skip;
16953 else
16954 {
16955 /* remove text that may still be there from the command */
16956 if (needclr)
16957 msg_clr_eos();
16958 if (eap->cmdidx == CMD_echo)
16959 msg_end();
16960 }
16961}
16962
16963/*
16964 * ":echohl {name}".
16965 */
16966 void
16967ex_echohl(eap)
16968 exarg_T *eap;
16969{
16970 int id;
16971
16972 id = syn_name2id(eap->arg);
16973 if (id == 0)
16974 echo_attr = 0;
16975 else
16976 echo_attr = syn_id2attr(id);
16977}
16978
16979/*
16980 * ":execute expr1 ..." execute the result of an expression.
16981 * ":echomsg expr1 ..." Print a message
16982 * ":echoerr expr1 ..." Print an error
16983 * Each gets spaces around each argument and a newline at the end for
16984 * echo commands
16985 */
16986 void
16987ex_execute(eap)
16988 exarg_T *eap;
16989{
16990 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016991 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 int ret = OK;
16993 char_u *p;
16994 garray_T ga;
16995 int len;
16996 int save_did_emsg;
16997
16998 ga_init2(&ga, 1, 80);
16999
17000 if (eap->skip)
17001 ++emsg_skip;
17002 while (*arg != NUL && *arg != '|' && *arg != '\n')
17003 {
17004 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017005 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 {
17007 /*
17008 * Report the invalid expression unless the expression evaluation
17009 * has been cancelled due to an aborting error, an interrupt, or an
17010 * exception.
17011 */
17012 if (!aborting())
17013 EMSG2(_(e_invexpr2), p);
17014 ret = FAIL;
17015 break;
17016 }
17017
17018 if (!eap->skip)
17019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017020 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 len = (int)STRLEN(p);
17022 if (ga_grow(&ga, len + 2) == FAIL)
17023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017024 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 ret = FAIL;
17026 break;
17027 }
17028 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 ga.ga_len += len;
17032 }
17033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 arg = skipwhite(arg);
17036 }
17037
17038 if (ret != FAIL && ga.ga_data != NULL)
17039 {
17040 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017043 out_flush();
17044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 else if (eap->cmdidx == CMD_echoerr)
17046 {
17047 /* We don't want to abort following commands, restore did_emsg. */
17048 save_did_emsg = did_emsg;
17049 EMSG((char_u *)ga.ga_data);
17050 if (!force_abort)
17051 did_emsg = save_did_emsg;
17052 }
17053 else if (eap->cmdidx == CMD_execute)
17054 do_cmdline((char_u *)ga.ga_data,
17055 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17056 }
17057
17058 ga_clear(&ga);
17059
17060 if (eap->skip)
17061 --emsg_skip;
17062
17063 eap->nextcmd = check_nextcmd(arg);
17064}
17065
17066/*
17067 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17068 * "arg" points to the "&" or '+' when called, to "option" when returning.
17069 * Returns NULL when no option name found. Otherwise pointer to the char
17070 * after the option name.
17071 */
17072 static char_u *
17073find_option_end(arg, opt_flags)
17074 char_u **arg;
17075 int *opt_flags;
17076{
17077 char_u *p = *arg;
17078
17079 ++p;
17080 if (*p == 'g' && p[1] == ':')
17081 {
17082 *opt_flags = OPT_GLOBAL;
17083 p += 2;
17084 }
17085 else if (*p == 'l' && p[1] == ':')
17086 {
17087 *opt_flags = OPT_LOCAL;
17088 p += 2;
17089 }
17090 else
17091 *opt_flags = 0;
17092
17093 if (!ASCII_ISALPHA(*p))
17094 return NULL;
17095 *arg = p;
17096
17097 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17098 p += 4; /* termcap option */
17099 else
17100 while (ASCII_ISALPHA(*p))
17101 ++p;
17102 return p;
17103}
17104
17105/*
17106 * ":function"
17107 */
17108 void
17109ex_function(eap)
17110 exarg_T *eap;
17111{
17112 char_u *theline;
17113 int j;
17114 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 char_u *name = NULL;
17117 char_u *p;
17118 char_u *arg;
17119 garray_T newargs;
17120 garray_T newlines;
17121 int varargs = FALSE;
17122 int mustend = FALSE;
17123 int flags = 0;
17124 ufunc_T *fp;
17125 int indent;
17126 int nesting;
17127 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017128 dictitem_T *v;
17129 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017130 static int func_nr = 0; /* number for nameless function */
17131 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017132 hashtab_T *ht;
17133 int todo;
17134 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135
17136 /*
17137 * ":function" without argument: list functions.
17138 */
17139 if (ends_excmd(*eap->arg))
17140 {
17141 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017142 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017143 todo = func_hashtab.ht_used;
17144 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017145 {
17146 if (!HASHITEM_EMPTY(hi))
17147 {
17148 --todo;
17149 fp = HI2UF(hi);
17150 if (!isdigit(*fp->uf_name))
17151 list_func_head(fp, FALSE);
17152 }
17153 }
17154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 eap->nextcmd = check_nextcmd(eap->arg);
17156 return;
17157 }
17158
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017159 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017160 * ":function /pat": list functions matching pattern.
17161 */
17162 if (*eap->arg == '/')
17163 {
17164 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17165 if (!eap->skip)
17166 {
17167 regmatch_T regmatch;
17168
17169 c = *p;
17170 *p = NUL;
17171 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17172 *p = c;
17173 if (regmatch.regprog != NULL)
17174 {
17175 regmatch.rm_ic = p_ic;
17176
17177 todo = func_hashtab.ht_used;
17178 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17179 {
17180 if (!HASHITEM_EMPTY(hi))
17181 {
17182 --todo;
17183 fp = HI2UF(hi);
17184 if (!isdigit(*fp->uf_name)
17185 && vim_regexec(&regmatch, fp->uf_name, 0))
17186 list_func_head(fp, FALSE);
17187 }
17188 }
17189 }
17190 }
17191 if (*p == '/')
17192 ++p;
17193 eap->nextcmd = check_nextcmd(p);
17194 return;
17195 }
17196
17197 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017198 * Get the function name. There are these situations:
17199 * func normal function name
17200 * "name" == func, "fudi.fd_dict" == NULL
17201 * dict.func new dictionary entry
17202 * "name" == NULL, "fudi.fd_dict" set,
17203 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17204 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017205 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017206 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17207 * dict.func existing dict entry that's not a Funcref
17208 * "name" == NULL, "fudi.fd_dict" set,
17209 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017212 name = trans_function_name(&p, eap->skip, 0, &fudi);
17213 paren = (vim_strchr(p, '(') != NULL);
17214 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 {
17216 /*
17217 * Return on an invalid expression in braces, unless the expression
17218 * evaluation has been cancelled due to an aborting error, an
17219 * interrupt, or an exception.
17220 */
17221 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017222 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017223 if (!eap->skip && fudi.fd_newkey != NULL)
17224 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017225 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 else
17229 eap->skip = TRUE;
17230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 /* An error in a function call during evaluation of an expression in magic
17232 * braces should not cause the function not to be defined. */
17233 saved_did_emsg = did_emsg;
17234 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235
17236 /*
17237 * ":function func" with only function name: list function.
17238 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017239 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 {
17241 if (!ends_excmd(*skipwhite(p)))
17242 {
17243 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017244 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 }
17246 eap->nextcmd = check_nextcmd(p);
17247 if (eap->nextcmd != NULL)
17248 *p = NUL;
17249 if (!eap->skip && !got_int)
17250 {
17251 fp = find_func(name);
17252 if (fp != NULL)
17253 {
17254 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017255 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 {
17257 msg_putchar('\n');
17258 msg_outnum((long)(j + 1));
17259 if (j < 9)
17260 msg_putchar(' ');
17261 if (j < 99)
17262 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017263 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 out_flush(); /* show a line at a time */
17265 ui_breakcheck();
17266 }
17267 if (!got_int)
17268 {
17269 msg_putchar('\n');
17270 msg_puts((char_u *)" endfunction");
17271 }
17272 }
17273 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017274 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017276 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 }
17278
17279 /*
17280 * ":function name(arg1, arg2)" Define function.
17281 */
17282 p = skipwhite(p);
17283 if (*p != '(')
17284 {
17285 if (!eap->skip)
17286 {
17287 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017288 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 }
17290 /* attempt to continue by skipping some text */
17291 if (vim_strchr(p, '(') != NULL)
17292 p = vim_strchr(p, '(');
17293 }
17294 p = skipwhite(p + 1);
17295
17296 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17297 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17298
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017299 if (!eap->skip)
17300 {
17301 /* Check the name of the function. */
17302 if (name != NULL)
17303 arg = name;
17304 else
17305 arg = fudi.fd_newkey;
17306 if (arg != NULL)
17307 {
17308 if (*arg == K_SPECIAL)
17309 j = 3;
17310 else
17311 j = 0;
17312 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17313 : eval_isnamec(arg[j])))
17314 ++j;
17315 if (arg[j] != NUL)
17316 emsg_funcname(_(e_invarg2), arg);
17317 }
17318 }
17319
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 /*
17321 * Isolate the arguments: "arg1, arg2, ...)"
17322 */
17323 while (*p != ')')
17324 {
17325 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17326 {
17327 varargs = TRUE;
17328 p += 3;
17329 mustend = TRUE;
17330 }
17331 else
17332 {
17333 arg = p;
17334 while (ASCII_ISALNUM(*p) || *p == '_')
17335 ++p;
17336 if (arg == p || isdigit(*arg)
17337 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17338 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17339 {
17340 if (!eap->skip)
17341 EMSG2(_("E125: Illegal argument: %s"), arg);
17342 break;
17343 }
17344 if (ga_grow(&newargs, 1) == FAIL)
17345 goto erret;
17346 c = *p;
17347 *p = NUL;
17348 arg = vim_strsave(arg);
17349 if (arg == NULL)
17350 goto erret;
17351 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17352 *p = c;
17353 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 if (*p == ',')
17355 ++p;
17356 else
17357 mustend = TRUE;
17358 }
17359 p = skipwhite(p);
17360 if (mustend && *p != ')')
17361 {
17362 if (!eap->skip)
17363 EMSG2(_(e_invarg2), eap->arg);
17364 break;
17365 }
17366 }
17367 ++p; /* skip the ')' */
17368
Bram Moolenaare9a41262005-01-15 22:18:47 +000017369 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 for (;;)
17371 {
17372 p = skipwhite(p);
17373 if (STRNCMP(p, "range", 5) == 0)
17374 {
17375 flags |= FC_RANGE;
17376 p += 5;
17377 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017378 else if (STRNCMP(p, "dict", 4) == 0)
17379 {
17380 flags |= FC_DICT;
17381 p += 4;
17382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 else if (STRNCMP(p, "abort", 5) == 0)
17384 {
17385 flags |= FC_ABORT;
17386 p += 5;
17387 }
17388 else
17389 break;
17390 }
17391
17392 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17393 EMSG(_(e_trailing));
17394
17395 /*
17396 * Read the body of the function, until ":endfunction" is found.
17397 */
17398 if (KeyTyped)
17399 {
17400 /* Check if the function already exists, don't let the user type the
17401 * whole function before telling him it doesn't work! For a script we
17402 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017403 if (!eap->skip && !eap->forceit)
17404 {
17405 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17406 EMSG(_(e_funcdict));
17407 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017408 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017411 if (!eap->skip && did_emsg)
17412 goto erret;
17413
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414 msg_putchar('\n'); /* don't overwrite the function name */
17415 cmdline_row = msg_row;
17416 }
17417
17418 indent = 2;
17419 nesting = 0;
17420 for (;;)
17421 {
17422 msg_scroll = TRUE;
17423 need_wait_return = FALSE;
17424 if (eap->getline == NULL)
17425 theline = getcmdline(':', 0L, indent);
17426 else
17427 theline = eap->getline(':', eap->cookie, indent);
17428 if (KeyTyped)
17429 lines_left = Rows - 1;
17430 if (theline == NULL)
17431 {
17432 EMSG(_("E126: Missing :endfunction"));
17433 goto erret;
17434 }
17435
17436 if (skip_until != NULL)
17437 {
17438 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17439 * don't check for ":endfunc". */
17440 if (STRCMP(theline, skip_until) == 0)
17441 {
17442 vim_free(skip_until);
17443 skip_until = NULL;
17444 }
17445 }
17446 else
17447 {
17448 /* skip ':' and blanks*/
17449 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17450 ;
17451
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017452 /* Check for "endfunction". */
17453 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 {
17455 vim_free(theline);
17456 break;
17457 }
17458
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017459 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 * at "end". */
17461 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17462 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017463 else if (STRNCMP(p, "if", 2) == 0
17464 || STRNCMP(p, "wh", 2) == 0
17465 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466 || STRNCMP(p, "try", 3) == 0)
17467 indent += 2;
17468
17469 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017470 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017472 if (*p == '!')
17473 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 p += eval_fname_script(p);
17475 if (ASCII_ISALPHA(*p))
17476 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017477 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 if (*skipwhite(p) == '(')
17479 {
17480 ++nesting;
17481 indent += 2;
17482 }
17483 }
17484 }
17485
17486 /* Check for ":append" or ":insert". */
17487 p = skip_range(p, NULL);
17488 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17489 || (p[0] == 'i'
17490 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17491 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17492 skip_until = vim_strsave((char_u *)".");
17493
17494 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17495 arg = skipwhite(skiptowhite(p));
17496 if (arg[0] == '<' && arg[1] =='<'
17497 && ((p[0] == 'p' && p[1] == 'y'
17498 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17499 || (p[0] == 'p' && p[1] == 'e'
17500 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17501 || (p[0] == 't' && p[1] == 'c'
17502 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17503 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17504 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017505 || (p[0] == 'm' && p[1] == 'z'
17506 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 ))
17508 {
17509 /* ":python <<" continues until a dot, like ":append" */
17510 p = skipwhite(arg + 2);
17511 if (*p == NUL)
17512 skip_until = vim_strsave((char_u *)".");
17513 else
17514 skip_until = vim_strsave(p);
17515 }
17516 }
17517
17518 /* Add the line to the function. */
17519 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017520 {
17521 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017523 }
17524
17525 /* Copy the line to newly allocated memory. get_one_sourceline()
17526 * allocates 250 bytes per line, this saves 80% on average. The cost
17527 * is an extra alloc/free. */
17528 p = vim_strsave(theline);
17529 if (p != NULL)
17530 {
17531 vim_free(theline);
17532 theline = p;
17533 }
17534
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17536 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 }
17538
17539 /* Don't define the function when skipping commands or when an error was
17540 * detected. */
17541 if (eap->skip || did_emsg)
17542 goto erret;
17543
17544 /*
17545 * If there are no errors, add the function
17546 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017547 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017548 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017549 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017550 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017551 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017552 emsg_funcname("E707: Function name conflicts with variable: %s",
17553 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017554 goto erret;
17555 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017556
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017557 fp = find_func(name);
17558 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017560 if (!eap->forceit)
17561 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017562 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017563 goto erret;
17564 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017565 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017566 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017567 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017568 name);
17569 goto erret;
17570 }
17571 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017572 ga_clear_strings(&(fp->uf_args));
17573 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017574 vim_free(name);
17575 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 }
17578 else
17579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017580 char numbuf[20];
17581
17582 fp = NULL;
17583 if (fudi.fd_newkey == NULL && !eap->forceit)
17584 {
17585 EMSG(_(e_funcdict));
17586 goto erret;
17587 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017588 if (fudi.fd_di == NULL)
17589 {
17590 /* Can't add a function to a locked dictionary */
17591 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17592 goto erret;
17593 }
17594 /* Can't change an existing function if it is locked */
17595 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17596 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017597
17598 /* Give the function a sequential number. Can only be used with a
17599 * Funcref! */
17600 vim_free(name);
17601 sprintf(numbuf, "%d", ++func_nr);
17602 name = vim_strsave((char_u *)numbuf);
17603 if (name == NULL)
17604 goto erret;
17605 }
17606
17607 if (fp == NULL)
17608 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017609 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017610 {
17611 int slen, plen;
17612 char_u *scriptname;
17613
17614 /* Check that the autoload name matches the script name. */
17615 j = FAIL;
17616 if (sourcing_name != NULL)
17617 {
17618 scriptname = autoload_name(name);
17619 if (scriptname != NULL)
17620 {
17621 p = vim_strchr(scriptname, '/');
17622 plen = STRLEN(p);
17623 slen = STRLEN(sourcing_name);
17624 if (slen > plen && fnamecmp(p,
17625 sourcing_name + slen - plen) == 0)
17626 j = OK;
17627 vim_free(scriptname);
17628 }
17629 }
17630 if (j == FAIL)
17631 {
17632 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17633 goto erret;
17634 }
17635 }
17636
17637 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 if (fp == NULL)
17639 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017640
17641 if (fudi.fd_dict != NULL)
17642 {
17643 if (fudi.fd_di == NULL)
17644 {
17645 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017646 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017647 if (fudi.fd_di == NULL)
17648 {
17649 vim_free(fp);
17650 goto erret;
17651 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017652 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17653 {
17654 vim_free(fudi.fd_di);
17655 goto erret;
17656 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017657 }
17658 else
17659 /* overwrite existing dict entry */
17660 clear_tv(&fudi.fd_di->di_tv);
17661 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017662 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017663 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017664 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017665 }
17666
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017668 STRCPY(fp->uf_name, name);
17669 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017671 fp->uf_args = newargs;
17672 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017673#ifdef FEAT_PROFILE
17674 fp->uf_tml_count = NULL;
17675 fp->uf_tml_total = NULL;
17676 fp->uf_tml_self = NULL;
17677 fp->uf_profiling = FALSE;
17678 if (prof_def_func())
17679 func_do_profile(fp);
17680#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017681 fp->uf_varargs = varargs;
17682 fp->uf_flags = flags;
17683 fp->uf_calls = 0;
17684 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017685 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686
17687erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 ga_clear_strings(&newargs);
17689 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017690ret_free:
17691 vim_free(skip_until);
17692 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695}
17696
17697/*
17698 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017699 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017701 * flags:
17702 * TFN_INT: internal function name OK
17703 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 * Advances "pp" to just after the function name (if no error).
17705 */
17706 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017707trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 char_u **pp;
17709 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017710 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017711 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017713 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 char_u *start;
17715 char_u *end;
17716 int lead;
17717 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017719 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017720
17721 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017722 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017724
17725 /* Check for hard coded <SNR>: already translated function ID (from a user
17726 * command). */
17727 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17728 && (*pp)[2] == (int)KE_SNR)
17729 {
17730 *pp += 3;
17731 len = get_id_len(pp) + 3;
17732 return vim_strnsave(start, len);
17733 }
17734
17735 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17736 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017738 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017740
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017741 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17742 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017743 if (end == start)
17744 {
17745 if (!skip)
17746 EMSG(_("E129: Function name required"));
17747 goto theend;
17748 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017749 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017750 {
17751 /*
17752 * Report an invalid expression in braces, unless the expression
17753 * evaluation has been cancelled due to an aborting error, an
17754 * interrupt, or an exception.
17755 */
17756 if (!aborting())
17757 {
17758 if (end != NULL)
17759 EMSG2(_(e_invarg2), start);
17760 }
17761 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017762 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017763 goto theend;
17764 }
17765
17766 if (lv.ll_tv != NULL)
17767 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017768 if (fdp != NULL)
17769 {
17770 fdp->fd_dict = lv.ll_dict;
17771 fdp->fd_newkey = lv.ll_newkey;
17772 lv.ll_newkey = NULL;
17773 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017774 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017775 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17776 {
17777 name = vim_strsave(lv.ll_tv->vval.v_string);
17778 *pp = end;
17779 }
17780 else
17781 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017782 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17783 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017784 EMSG(_(e_funcref));
17785 else
17786 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017787 name = NULL;
17788 }
17789 goto theend;
17790 }
17791
17792 if (lv.ll_name == NULL)
17793 {
17794 /* Error found, but continue after the function name. */
17795 *pp = end;
17796 goto theend;
17797 }
17798
17799 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000017800 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017801 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000017802 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
17803 && STRNCMP(lv.ll_name, "s:", 2) == 0)
17804 {
17805 /* When there was "s:" already or the name expanded to get a
17806 * leading "s:" then remove it. */
17807 lv.ll_name += 2;
17808 len -= 2;
17809 lead = 2;
17810 }
17811 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017812 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017813 {
17814 if (lead == 2) /* skip over "s:" */
17815 lv.ll_name += 2;
17816 len = (int)(end - lv.ll_name);
17817 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017818
17819 /*
17820 * Copy the function name to allocated memory.
17821 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17822 * Accept <SNR>123_name() outside a script.
17823 */
17824 if (skip)
17825 lead = 0; /* do nothing */
17826 else if (lead > 0)
17827 {
17828 lead = 3;
17829 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17830 {
17831 if (current_SID <= 0)
17832 {
17833 EMSG(_(e_usingsid));
17834 goto theend;
17835 }
17836 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17837 lead += (int)STRLEN(sid_buf);
17838 }
17839 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017840 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017841 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017842 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017843 goto theend;
17844 }
17845 name = alloc((unsigned)(len + lead + 1));
17846 if (name != NULL)
17847 {
17848 if (lead > 0)
17849 {
17850 name[0] = K_SPECIAL;
17851 name[1] = KS_EXTRA;
17852 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017853 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017854 STRCPY(name + 3, sid_buf);
17855 }
17856 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17857 name[len + lead] = NUL;
17858 }
17859 *pp = end;
17860
17861theend:
17862 clear_lval(&lv);
17863 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864}
17865
17866/*
17867 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17868 * Return 2 if "p" starts with "s:".
17869 * Return 0 otherwise.
17870 */
17871 static int
17872eval_fname_script(p)
17873 char_u *p;
17874{
17875 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17876 || STRNICMP(p + 1, "SNR>", 4) == 0))
17877 return 5;
17878 if (p[0] == 's' && p[1] == ':')
17879 return 2;
17880 return 0;
17881}
17882
17883/*
17884 * Return TRUE if "p" starts with "<SID>" or "s:".
17885 * Only works if eval_fname_script() returned non-zero for "p"!
17886 */
17887 static int
17888eval_fname_sid(p)
17889 char_u *p;
17890{
17891 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17892}
17893
17894/*
17895 * List the head of the function: "name(arg1, arg2)".
17896 */
17897 static void
17898list_func_head(fp, indent)
17899 ufunc_T *fp;
17900 int indent;
17901{
17902 int j;
17903
17904 msg_start();
17905 if (indent)
17906 MSG_PUTS(" ");
17907 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017908 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 {
17910 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017911 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 }
17913 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017914 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017916 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 {
17918 if (j)
17919 MSG_PUTS(", ");
17920 msg_puts(FUNCARG(fp, j));
17921 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017922 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 {
17924 if (j)
17925 MSG_PUTS(", ");
17926 MSG_PUTS("...");
17927 }
17928 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017929 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017930 if (p_verbose > 0)
17931 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932}
17933
17934/*
17935 * Find a function by name, return pointer to it in ufuncs.
17936 * Return NULL for unknown function.
17937 */
17938 static ufunc_T *
17939find_func(name)
17940 char_u *name;
17941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017942 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017944 hi = hash_find(&func_hashtab, name);
17945 if (!HASHITEM_EMPTY(hi))
17946 return HI2UF(hi);
17947 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948}
17949
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017950#if defined(EXITFREE) || defined(PROTO)
17951 void
17952free_all_functions()
17953{
17954 hashitem_T *hi;
17955
17956 /* Need to start all over every time, because func_free() may change the
17957 * hash table. */
17958 while (func_hashtab.ht_used > 0)
17959 for (hi = func_hashtab.ht_array; ; ++hi)
17960 if (!HASHITEM_EMPTY(hi))
17961 {
17962 func_free(HI2UF(hi));
17963 break;
17964 }
17965}
17966#endif
17967
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017968/*
17969 * Return TRUE if a function "name" exists.
17970 */
17971 static int
17972function_exists(name)
17973 char_u *name;
17974{
17975 char_u *p = name;
17976 int n = FALSE;
17977
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017978 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017979 if (p != NULL)
17980 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017981 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017982 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017983 else
17984 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017985 vim_free(p);
17986 }
17987 return n;
17988}
17989
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017990/*
17991 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017992 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017993 */
17994 static int
17995builtin_function(name)
17996 char_u *name;
17997{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017998 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17999 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018000}
18001
Bram Moolenaar05159a02005-02-26 23:04:13 +000018002#if defined(FEAT_PROFILE) || defined(PROTO)
18003/*
18004 * Start profiling function "fp".
18005 */
18006 static void
18007func_do_profile(fp)
18008 ufunc_T *fp;
18009{
18010 fp->uf_tm_count = 0;
18011 profile_zero(&fp->uf_tm_self);
18012 profile_zero(&fp->uf_tm_total);
18013 if (fp->uf_tml_count == NULL)
18014 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18015 (sizeof(int) * fp->uf_lines.ga_len));
18016 if (fp->uf_tml_total == NULL)
18017 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18018 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18019 if (fp->uf_tml_self == NULL)
18020 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18021 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18022 fp->uf_tml_idx = -1;
18023 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18024 || fp->uf_tml_self == NULL)
18025 return; /* out of memory */
18026
18027 fp->uf_profiling = TRUE;
18028}
18029
18030/*
18031 * Dump the profiling results for all functions in file "fd".
18032 */
18033 void
18034func_dump_profile(fd)
18035 FILE *fd;
18036{
18037 hashitem_T *hi;
18038 int todo;
18039 ufunc_T *fp;
18040 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018041 ufunc_T **sorttab;
18042 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018043
18044 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018045 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18046
Bram Moolenaar05159a02005-02-26 23:04:13 +000018047 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18048 {
18049 if (!HASHITEM_EMPTY(hi))
18050 {
18051 --todo;
18052 fp = HI2UF(hi);
18053 if (fp->uf_profiling)
18054 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018055 if (sorttab != NULL)
18056 sorttab[st_len++] = fp;
18057
Bram Moolenaar05159a02005-02-26 23:04:13 +000018058 if (fp->uf_name[0] == K_SPECIAL)
18059 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18060 else
18061 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18062 if (fp->uf_tm_count == 1)
18063 fprintf(fd, "Called 1 time\n");
18064 else
18065 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18066 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18067 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18068 fprintf(fd, "\n");
18069 fprintf(fd, "count total (s) self (s)\n");
18070
18071 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18072 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018073 prof_func_line(fd, fp->uf_tml_count[i],
18074 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018075 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18076 }
18077 fprintf(fd, "\n");
18078 }
18079 }
18080 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018081
18082 if (sorttab != NULL && st_len > 0)
18083 {
18084 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18085 prof_total_cmp);
18086 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18087 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18088 prof_self_cmp);
18089 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18090 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018091}
Bram Moolenaar73830342005-02-28 22:48:19 +000018092
18093 static void
18094prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18095 FILE *fd;
18096 ufunc_T **sorttab;
18097 int st_len;
18098 char *title;
18099 int prefer_self; /* when equal print only self time */
18100{
18101 int i;
18102 ufunc_T *fp;
18103
18104 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18105 fprintf(fd, "count total (s) self (s) function\n");
18106 for (i = 0; i < 20 && i < st_len; ++i)
18107 {
18108 fp = sorttab[i];
18109 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18110 prefer_self);
18111 if (fp->uf_name[0] == K_SPECIAL)
18112 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18113 else
18114 fprintf(fd, " %s()\n", fp->uf_name);
18115 }
18116 fprintf(fd, "\n");
18117}
18118
18119/*
18120 * Print the count and times for one function or function line.
18121 */
18122 static void
18123prof_func_line(fd, count, total, self, prefer_self)
18124 FILE *fd;
18125 int count;
18126 proftime_T *total;
18127 proftime_T *self;
18128 int prefer_self; /* when equal print only self time */
18129{
18130 if (count > 0)
18131 {
18132 fprintf(fd, "%5d ", count);
18133 if (prefer_self && profile_equal(total, self))
18134 fprintf(fd, " ");
18135 else
18136 fprintf(fd, "%s ", profile_msg(total));
18137 if (!prefer_self && profile_equal(total, self))
18138 fprintf(fd, " ");
18139 else
18140 fprintf(fd, "%s ", profile_msg(self));
18141 }
18142 else
18143 fprintf(fd, " ");
18144}
18145
18146/*
18147 * Compare function for total time sorting.
18148 */
18149 static int
18150#ifdef __BORLANDC__
18151_RTLENTRYF
18152#endif
18153prof_total_cmp(s1, s2)
18154 const void *s1;
18155 const void *s2;
18156{
18157 ufunc_T *p1, *p2;
18158
18159 p1 = *(ufunc_T **)s1;
18160 p2 = *(ufunc_T **)s2;
18161 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18162}
18163
18164/*
18165 * Compare function for self time sorting.
18166 */
18167 static int
18168#ifdef __BORLANDC__
18169_RTLENTRYF
18170#endif
18171prof_self_cmp(s1, s2)
18172 const void *s1;
18173 const void *s2;
18174{
18175 ufunc_T *p1, *p2;
18176
18177 p1 = *(ufunc_T **)s1;
18178 p2 = *(ufunc_T **)s2;
18179 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18180}
18181
Bram Moolenaar05159a02005-02-26 23:04:13 +000018182#endif
18183
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018184/* The names of packages that once were loaded is remembered. */
18185static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18186
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018187/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018188 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018189 * Return TRUE if a package was loaded.
18190 */
18191 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018192script_autoload(name, reload)
18193 char_u *name;
18194 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018195{
18196 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018197 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018198 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018199 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018200
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018201 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018202 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018203 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018204 return FALSE;
18205
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018206 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018207
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018208 /* Find the name in the list of previously loaded package names. Skip
18209 * "autoload/", it's always the same. */
18210 for (i = 0; i < ga_loaded.ga_len; ++i)
18211 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18212 break;
18213 if (!reload && i < ga_loaded.ga_len)
18214 ret = FALSE; /* was loaded already */
18215 else
18216 {
18217 /* Remember the name if it wasn't loaded already. */
18218 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18219 {
18220 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18221 tofree = NULL;
18222 }
18223
18224 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018225 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018226 ret = TRUE;
18227 }
18228
18229 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018230 return ret;
18231}
18232
18233/*
18234 * Return the autoload script name for a function or variable name.
18235 * Returns NULL when out of memory.
18236 */
18237 static char_u *
18238autoload_name(name)
18239 char_u *name;
18240{
18241 char_u *p;
18242 char_u *scriptname;
18243
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018244 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018245 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18246 if (scriptname == NULL)
18247 return FALSE;
18248 STRCPY(scriptname, "autoload/");
18249 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018250 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018251 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018252 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018253 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018254 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018255}
18256
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18258
18259/*
18260 * Function given to ExpandGeneric() to obtain the list of user defined
18261 * function names.
18262 */
18263 char_u *
18264get_user_func_name(xp, idx)
18265 expand_T *xp;
18266 int idx;
18267{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018268 static long_u done;
18269 static hashitem_T *hi;
18270 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271
18272 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018274 done = 0;
18275 hi = func_hashtab.ht_array;
18276 }
18277 if (done < func_hashtab.ht_used)
18278 {
18279 if (done++ > 0)
18280 ++hi;
18281 while (HASHITEM_EMPTY(hi))
18282 ++hi;
18283 fp = HI2UF(hi);
18284
18285 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18286 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287
18288 cat_func_name(IObuff, fp);
18289 if (xp->xp_context != EXPAND_USER_FUNC)
18290 {
18291 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018292 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 STRCAT(IObuff, ")");
18294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 return IObuff;
18296 }
18297 return NULL;
18298}
18299
18300#endif /* FEAT_CMDL_COMPL */
18301
18302/*
18303 * Copy the function name of "fp" to buffer "buf".
18304 * "buf" must be able to hold the function name plus three bytes.
18305 * Takes care of script-local function names.
18306 */
18307 static void
18308cat_func_name(buf, fp)
18309 char_u *buf;
18310 ufunc_T *fp;
18311{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018312 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 {
18314 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018315 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 }
18317 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018318 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319}
18320
18321/*
18322 * ":delfunction {name}"
18323 */
18324 void
18325ex_delfunction(eap)
18326 exarg_T *eap;
18327{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018328 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 char_u *p;
18330 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018331 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332
18333 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018334 name = trans_function_name(&p, eap->skip, 0, &fudi);
18335 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018337 {
18338 if (fudi.fd_dict != NULL && !eap->skip)
18339 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 if (!ends_excmd(*skipwhite(p)))
18343 {
18344 vim_free(name);
18345 EMSG(_(e_trailing));
18346 return;
18347 }
18348 eap->nextcmd = check_nextcmd(p);
18349 if (eap->nextcmd != NULL)
18350 *p = NUL;
18351
18352 if (!eap->skip)
18353 fp = find_func(name);
18354 vim_free(name);
18355
18356 if (!eap->skip)
18357 {
18358 if (fp == NULL)
18359 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018360 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 return;
18362 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018363 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 {
18365 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18366 return;
18367 }
18368
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018369 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018371 /* Delete the dict item that refers to the function, it will
18372 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018373 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018375 else
18376 func_free(fp);
18377 }
18378}
18379
18380/*
18381 * Free a function and remove it from the list of functions.
18382 */
18383 static void
18384func_free(fp)
18385 ufunc_T *fp;
18386{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018387 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018388
18389 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018390 ga_clear_strings(&(fp->uf_args));
18391 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018392#ifdef FEAT_PROFILE
18393 vim_free(fp->uf_tml_count);
18394 vim_free(fp->uf_tml_total);
18395 vim_free(fp->uf_tml_self);
18396#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018397
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018398 /* remove the function from the function hashtable */
18399 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18400 if (HASHITEM_EMPTY(hi))
18401 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018402 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018403 hash_remove(&func_hashtab, hi);
18404
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018405 vim_free(fp);
18406}
18407
18408/*
18409 * Unreference a Function: decrement the reference count and free it when it
18410 * becomes zero. Only for numbered functions.
18411 */
18412 static void
18413func_unref(name)
18414 char_u *name;
18415{
18416 ufunc_T *fp;
18417
18418 if (name != NULL && isdigit(*name))
18419 {
18420 fp = find_func(name);
18421 if (fp == NULL)
18422 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018423 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018424 {
18425 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018426 * when "uf_calls" becomes zero. */
18427 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018428 func_free(fp);
18429 }
18430 }
18431}
18432
18433/*
18434 * Count a reference to a Function.
18435 */
18436 static void
18437func_ref(name)
18438 char_u *name;
18439{
18440 ufunc_T *fp;
18441
18442 if (name != NULL && isdigit(*name))
18443 {
18444 fp = find_func(name);
18445 if (fp == NULL)
18446 EMSG2(_(e_intern2), "func_ref()");
18447 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018448 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 }
18450}
18451
18452/*
18453 * Call a user function.
18454 */
18455 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018456call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 ufunc_T *fp; /* pointer to function */
18458 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018459 typval_T *argvars; /* arguments */
18460 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 linenr_T firstline; /* first line of range */
18462 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018463 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464{
Bram Moolenaar33570922005-01-25 22:26:29 +000018465 char_u *save_sourcing_name;
18466 linenr_T save_sourcing_lnum;
18467 scid_T save_current_SID;
18468 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018469 int save_did_emsg;
18470 static int depth = 0;
18471 dictitem_T *v;
18472 int fixvar_idx = 0; /* index in fixvar[] */
18473 int i;
18474 int ai;
18475 char_u numbuf[NUMBUFLEN];
18476 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018477#ifdef FEAT_PROFILE
18478 proftime_T wait_start;
18479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480
18481 /* If depth of calling is getting too high, don't execute the function */
18482 if (depth >= p_mfd)
18483 {
18484 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018485 rettv->v_type = VAR_NUMBER;
18486 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 return;
18488 }
18489 ++depth;
18490
18491 line_breakcheck(); /* check for CTRL-C hit */
18492
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018493 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018494 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018496 fc.rettv = rettv;
18497 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 fc.linenr = 0;
18499 fc.returned = FALSE;
18500 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018502 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 fc.dbg_tick = debug_tick;
18504
Bram Moolenaar33570922005-01-25 22:26:29 +000018505 /*
18506 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18507 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18508 * each argument variable and saves a lot of time.
18509 */
18510 /*
18511 * Init l: variables.
18512 */
18513 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018514 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018515 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018516 /* Set l:self to "selfdict". */
18517 v = &fc.fixvar[fixvar_idx++].var;
18518 STRCPY(v->di_key, "self");
18519 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18520 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18521 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018522 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018523 v->di_tv.vval.v_dict = selfdict;
18524 ++selfdict->dv_refcount;
18525 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018526
Bram Moolenaar33570922005-01-25 22:26:29 +000018527 /*
18528 * Init a: variables.
18529 * Set a:0 to "argcount".
18530 * Set a:000 to a list with room for the "..." arguments.
18531 */
18532 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18533 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018534 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018535 v = &fc.fixvar[fixvar_idx++].var;
18536 STRCPY(v->di_key, "000");
18537 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18538 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18539 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018540 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018541 v->di_tv.vval.v_list = &fc.l_varlist;
18542 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18543 fc.l_varlist.lv_refcount = 99999;
18544
18545 /*
18546 * Set a:firstline to "firstline" and a:lastline to "lastline".
18547 * Set a:name to named arguments.
18548 * Set a:N to the "..." arguments.
18549 */
18550 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18551 (varnumber_T)firstline);
18552 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18553 (varnumber_T)lastline);
18554 for (i = 0; i < argcount; ++i)
18555 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018556 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018557 if (ai < 0)
18558 /* named argument a:name */
18559 name = FUNCARG(fp, i);
18560 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018561 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018562 /* "..." argument a:1, a:2, etc. */
18563 sprintf((char *)numbuf, "%d", ai + 1);
18564 name = numbuf;
18565 }
18566 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18567 {
18568 v = &fc.fixvar[fixvar_idx++].var;
18569 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18570 }
18571 else
18572 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018573 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18574 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018575 if (v == NULL)
18576 break;
18577 v->di_flags = DI_FLAGS_RO;
18578 }
18579 STRCPY(v->di_key, name);
18580 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18581
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018582 /* Note: the values are copied directly to avoid alloc/free.
18583 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018584 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018585 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018586
18587 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18588 {
18589 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18590 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018591 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018592 }
18593 }
18594
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 /* Don't redraw while executing the function. */
18596 ++RedrawingDisabled;
18597 save_sourcing_name = sourcing_name;
18598 save_sourcing_lnum = sourcing_lnum;
18599 sourcing_lnum = 1;
18600 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018601 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 if (sourcing_name != NULL)
18603 {
18604 if (save_sourcing_name != NULL
18605 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18606 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18607 else
18608 STRCPY(sourcing_name, "function ");
18609 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18610
18611 if (p_verbose >= 12)
18612 {
18613 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018614 verbose_enter_scroll();
18615
Bram Moolenaar555b2802005-05-19 21:08:39 +000018616 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 if (p_verbose >= 14)
18618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018620 char_u numbuf[NUMBUFLEN];
18621 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622
18623 msg_puts((char_u *)"(");
18624 for (i = 0; i < argcount; ++i)
18625 {
18626 if (i > 0)
18627 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018628 if (argvars[i].v_type == VAR_NUMBER)
18629 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 else
18631 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018632 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018633 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018635 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 }
18637 }
18638 msg_puts((char_u *)")");
18639 }
18640 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018641
18642 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 --no_wait_return;
18644 }
18645 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018646#ifdef FEAT_PROFILE
18647 if (do_profiling)
18648 {
18649 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18650 func_do_profile(fp);
18651 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018652 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018653 {
18654 ++fp->uf_tm_count;
18655 profile_start(&fp->uf_tm_start);
18656 profile_zero(&fp->uf_tm_children);
18657 }
18658 script_prof_save(&wait_start);
18659 }
18660#endif
18661
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018663 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 save_did_emsg = did_emsg;
18665 did_emsg = FALSE;
18666
18667 /* call do_cmdline() to execute the lines */
18668 do_cmdline(NULL, get_func_line, (void *)&fc,
18669 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18670
18671 --RedrawingDisabled;
18672
18673 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018674 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018676 clear_tv(rettv);
18677 rettv->v_type = VAR_NUMBER;
18678 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 }
18680
Bram Moolenaar05159a02005-02-26 23:04:13 +000018681#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018682 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018683 {
18684 profile_end(&fp->uf_tm_start);
18685 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18686 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18687 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18688 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018689 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018690 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018691 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18692 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018693 }
18694 }
18695#endif
18696
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 /* when being verbose, mention the return value */
18698 if (p_verbose >= 12)
18699 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018701 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018704 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018705 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018706 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18707 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018708 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018710 char_u buf[MSG_BUF_LEN];
18711 char_u numbuf[NUMBUFLEN];
18712 char_u *tofree;
18713
Bram Moolenaar555b2802005-05-19 21:08:39 +000018714 /* The value may be very long. Skip the middle part, so that we
18715 * have some idea how it starts and ends. smsg() would always
18716 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018717 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018718 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018719 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018720 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 }
18722 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018723
18724 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 --no_wait_return;
18726 }
18727
18728 vim_free(sourcing_name);
18729 sourcing_name = save_sourcing_name;
18730 sourcing_lnum = save_sourcing_lnum;
18731 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018732#ifdef FEAT_PROFILE
18733 if (do_profiling)
18734 script_prof_restore(&wait_start);
18735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736
18737 if (p_verbose >= 12 && sourcing_name != NULL)
18738 {
18739 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018740 verbose_enter_scroll();
18741
Bram Moolenaar555b2802005-05-19 21:08:39 +000018742 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018744
18745 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 --no_wait_return;
18747 }
18748
18749 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018750 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751
Bram Moolenaar33570922005-01-25 22:26:29 +000018752 /* The a: variables typevals were not alloced, only free the allocated
18753 * variables. */
18754 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18755
18756 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 --depth;
18758}
18759
18760/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 * Add a number variable "name" to dict "dp" with value "nr".
18762 */
18763 static void
18764add_nr_var(dp, v, name, nr)
18765 dict_T *dp;
18766 dictitem_T *v;
18767 char *name;
18768 varnumber_T nr;
18769{
18770 STRCPY(v->di_key, name);
18771 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18772 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18773 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018774 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018775 v->di_tv.vval.v_number = nr;
18776}
18777
18778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 * ":return [expr]"
18780 */
18781 void
18782ex_return(eap)
18783 exarg_T *eap;
18784{
18785 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018786 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 int returning = FALSE;
18788
18789 if (current_funccal == NULL)
18790 {
18791 EMSG(_("E133: :return not inside a function"));
18792 return;
18793 }
18794
18795 if (eap->skip)
18796 ++emsg_skip;
18797
18798 eap->nextcmd = NULL;
18799 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 {
18802 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 }
18807 /* It's safer to return also on error. */
18808 else if (!eap->skip)
18809 {
18810 /*
18811 * Return unless the expression evaluation has been cancelled due to an
18812 * aborting error, an interrupt, or an exception.
18813 */
18814 if (!aborting())
18815 returning = do_return(eap, FALSE, TRUE, NULL);
18816 }
18817
18818 /* When skipping or the return gets pending, advance to the next command
18819 * in this line (!returning). Otherwise, ignore the rest of the line.
18820 * Following lines will be ignored by get_func_line(). */
18821 if (returning)
18822 eap->nextcmd = NULL;
18823 else if (eap->nextcmd == NULL) /* no argument */
18824 eap->nextcmd = check_nextcmd(arg);
18825
18826 if (eap->skip)
18827 --emsg_skip;
18828}
18829
18830/*
18831 * Return from a function. Possibly makes the return pending. Also called
18832 * for a pending return at the ":endtry" or after returning from an extra
18833 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018834 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018835 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 * FALSE when the return gets pending.
18837 */
18838 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018839do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 exarg_T *eap;
18841 int reanimate;
18842 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018843 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844{
18845 int idx;
18846 struct condstack *cstack = eap->cstack;
18847
18848 if (reanimate)
18849 /* Undo the return. */
18850 current_funccal->returned = FALSE;
18851
18852 /*
18853 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18854 * not in its finally clause (which then is to be executed next) is found.
18855 * In this case, make the ":return" pending for execution at the ":endtry".
18856 * Otherwise, return normally.
18857 */
18858 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18859 if (idx >= 0)
18860 {
18861 cstack->cs_pending[idx] = CSTP_RETURN;
18862
18863 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864 /* A pending return again gets pending. "rettv" points to an
18865 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018867 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 else
18869 {
18870 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018871 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018873 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 {
18877 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018878 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018879 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 else
18881 EMSG(_(e_outofmem));
18882 }
18883 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018884 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885
18886 if (reanimate)
18887 {
18888 /* The pending return value could be overwritten by a ":return"
18889 * without argument in a finally clause; reset the default
18890 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018891 current_funccal->rettv->v_type = VAR_NUMBER;
18892 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893 }
18894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018895 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 }
18897 else
18898 {
18899 current_funccal->returned = TRUE;
18900
18901 /* If the return is carried out now, store the return value. For
18902 * a return immediately after reanimation, the value is already
18903 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018904 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018906 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018907 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018909 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 }
18911 }
18912
18913 return idx < 0;
18914}
18915
18916/*
18917 * Free the variable with a pending return value.
18918 */
18919 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018920discard_pending_return(rettv)
18921 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922{
Bram Moolenaar33570922005-01-25 22:26:29 +000018923 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924}
18925
18926/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018927 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 * is an allocated string. Used by report_pending() for verbose messages.
18929 */
18930 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018931get_return_cmd(rettv)
18932 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018934 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018935 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018936 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018938 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018939 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018940 if (s == NULL)
18941 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018942
18943 STRCPY(IObuff, ":return ");
18944 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18945 if (STRLEN(s) + 8 >= IOSIZE)
18946 STRCPY(IObuff + IOSIZE - 4, "...");
18947 vim_free(tofree);
18948 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949}
18950
18951/*
18952 * Get next function line.
18953 * Called by do_cmdline() to get the next line.
18954 * Returns allocated string, or NULL for end of function.
18955 */
18956/* ARGSUSED */
18957 char_u *
18958get_func_line(c, cookie, indent)
18959 int c; /* not used */
18960 void *cookie;
18961 int indent; /* not used */
18962{
Bram Moolenaar33570922005-01-25 22:26:29 +000018963 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018964 ufunc_T *fp = fcp->func;
18965 char_u *retval;
18966 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967
18968 /* If breakpoints have been added/deleted need to check for it. */
18969 if (fcp->dbg_tick != debug_tick)
18970 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018971 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 sourcing_lnum);
18973 fcp->dbg_tick = debug_tick;
18974 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018975#ifdef FEAT_PROFILE
18976 if (do_profiling)
18977 func_line_end(cookie);
18978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979
Bram Moolenaar05159a02005-02-26 23:04:13 +000018980 gap = &fp->uf_lines;
18981 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 retval = NULL;
18983 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18984 retval = NULL;
18985 else
18986 {
18987 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18988 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018989#ifdef FEAT_PROFILE
18990 if (do_profiling)
18991 func_line_start(cookie);
18992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 }
18994
18995 /* Did we encounter a breakpoint? */
18996 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18997 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018998 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019000 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 sourcing_lnum);
19002 fcp->dbg_tick = debug_tick;
19003 }
19004
19005 return retval;
19006}
19007
Bram Moolenaar05159a02005-02-26 23:04:13 +000019008#if defined(FEAT_PROFILE) || defined(PROTO)
19009/*
19010 * Called when starting to read a function line.
19011 * "sourcing_lnum" must be correct!
19012 * When skipping lines it may not actually be executed, but we won't find out
19013 * until later and we need to store the time now.
19014 */
19015 void
19016func_line_start(cookie)
19017 void *cookie;
19018{
19019 funccall_T *fcp = (funccall_T *)cookie;
19020 ufunc_T *fp = fcp->func;
19021
19022 if (fp->uf_profiling && sourcing_lnum >= 1
19023 && sourcing_lnum <= fp->uf_lines.ga_len)
19024 {
19025 fp->uf_tml_idx = sourcing_lnum - 1;
19026 fp->uf_tml_execed = FALSE;
19027 profile_start(&fp->uf_tml_start);
19028 profile_zero(&fp->uf_tml_children);
19029 profile_get_wait(&fp->uf_tml_wait);
19030 }
19031}
19032
19033/*
19034 * Called when actually executing a function line.
19035 */
19036 void
19037func_line_exec(cookie)
19038 void *cookie;
19039{
19040 funccall_T *fcp = (funccall_T *)cookie;
19041 ufunc_T *fp = fcp->func;
19042
19043 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19044 fp->uf_tml_execed = TRUE;
19045}
19046
19047/*
19048 * Called when done with a function line.
19049 */
19050 void
19051func_line_end(cookie)
19052 void *cookie;
19053{
19054 funccall_T *fcp = (funccall_T *)cookie;
19055 ufunc_T *fp = fcp->func;
19056
19057 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19058 {
19059 if (fp->uf_tml_execed)
19060 {
19061 ++fp->uf_tml_count[fp->uf_tml_idx];
19062 profile_end(&fp->uf_tml_start);
19063 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19064 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19065 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19066 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19067 }
19068 fp->uf_tml_idx = -1;
19069 }
19070}
19071#endif
19072
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073/*
19074 * Return TRUE if the currently active function should be ended, because a
19075 * return was encountered or an error occured. Used inside a ":while".
19076 */
19077 int
19078func_has_ended(cookie)
19079 void *cookie;
19080{
Bram Moolenaar33570922005-01-25 22:26:29 +000019081 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082
19083 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19084 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019085 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 || fcp->returned);
19087}
19088
19089/*
19090 * return TRUE if cookie indicates a function which "abort"s on errors.
19091 */
19092 int
19093func_has_abort(cookie)
19094 void *cookie;
19095{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019096 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097}
19098
19099#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19100typedef enum
19101{
19102 VAR_FLAVOUR_DEFAULT,
19103 VAR_FLAVOUR_SESSION,
19104 VAR_FLAVOUR_VIMINFO
19105} var_flavour_T;
19106
19107static var_flavour_T var_flavour __ARGS((char_u *varname));
19108
19109 static var_flavour_T
19110var_flavour(varname)
19111 char_u *varname;
19112{
19113 char_u *p = varname;
19114
19115 if (ASCII_ISUPPER(*p))
19116 {
19117 while (*(++p))
19118 if (ASCII_ISLOWER(*p))
19119 return VAR_FLAVOUR_SESSION;
19120 return VAR_FLAVOUR_VIMINFO;
19121 }
19122 else
19123 return VAR_FLAVOUR_DEFAULT;
19124}
19125#endif
19126
19127#if defined(FEAT_VIMINFO) || defined(PROTO)
19128/*
19129 * Restore global vars that start with a capital from the viminfo file
19130 */
19131 int
19132read_viminfo_varlist(virp, writing)
19133 vir_T *virp;
19134 int writing;
19135{
19136 char_u *tab;
19137 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139
19140 if (!writing && (find_viminfo_parameter('!') != NULL))
19141 {
19142 tab = vim_strchr(virp->vir_line + 1, '\t');
19143 if (tab != NULL)
19144 {
19145 *tab++ = '\0'; /* isolate the variable name */
19146 if (*tab == 'S') /* string var */
19147 is_string = TRUE;
19148
19149 tab = vim_strchr(tab, '\t');
19150 if (tab != NULL)
19151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152 if (is_string)
19153 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019154 tv.v_type = VAR_STRING;
19155 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 }
19158 else
19159 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019160 tv.v_type = VAR_NUMBER;
19161 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019163 set_var(virp->vir_line + 1, &tv, FALSE);
19164 if (is_string)
19165 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 }
19167 }
19168 }
19169
19170 return viminfo_readline(virp);
19171}
19172
19173/*
19174 * Write global vars that start with a capital to the viminfo file
19175 */
19176 void
19177write_viminfo_varlist(fp)
19178 FILE *fp;
19179{
Bram Moolenaar33570922005-01-25 22:26:29 +000019180 hashitem_T *hi;
19181 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019182 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019184 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019186 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187
19188 if (find_viminfo_parameter('!') == NULL)
19189 return;
19190
19191 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019192
Bram Moolenaar33570922005-01-25 22:26:29 +000019193 todo = globvarht.ht_used;
19194 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019196 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019198 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019199 this_var = HI2DI(hi);
19200 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019202 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019203 {
19204 case VAR_STRING: s = "STR"; break;
19205 case VAR_NUMBER: s = "NUM"; break;
19206 default: continue;
19207 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019208 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019209 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019210 if (p != NULL)
19211 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019212 vim_free(tofree);
19213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214 }
19215 }
19216}
19217#endif
19218
19219#if defined(FEAT_SESSION) || defined(PROTO)
19220 int
19221store_session_globals(fd)
19222 FILE *fd;
19223{
Bram Moolenaar33570922005-01-25 22:26:29 +000019224 hashitem_T *hi;
19225 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019226 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 char_u *p, *t;
19228
Bram Moolenaar33570922005-01-25 22:26:29 +000019229 todo = globvarht.ht_used;
19230 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019232 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019234 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019235 this_var = HI2DI(hi);
19236 if ((this_var->di_tv.v_type == VAR_NUMBER
19237 || this_var->di_tv.v_type == VAR_STRING)
19238 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019239 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019240 /* Escape special characters with a backslash. Turn a LF and
19241 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019242 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019243 (char_u *)"\\\"\n\r");
19244 if (p == NULL) /* out of memory */
19245 break;
19246 for (t = p; *t != NUL; ++t)
19247 if (*t == '\n')
19248 *t = 'n';
19249 else if (*t == '\r')
19250 *t = 'r';
19251 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 this_var->di_key,
19253 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19254 : ' ',
19255 p,
19256 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19257 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019258 || put_eol(fd) == FAIL)
19259 {
19260 vim_free(p);
19261 return FAIL;
19262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263 vim_free(p);
19264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 }
19266 }
19267 return OK;
19268}
19269#endif
19270
Bram Moolenaar661b1822005-07-28 22:36:45 +000019271/*
19272 * Display script name where an item was last set.
19273 * Should only be invoked when 'verbose' is non-zero.
19274 */
19275 void
19276last_set_msg(scriptID)
19277 scid_T scriptID;
19278{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019279 char_u *p;
19280
Bram Moolenaar661b1822005-07-28 22:36:45 +000019281 if (scriptID != 0)
19282 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019283 p = home_replace_save(NULL, get_scriptname(scriptID));
19284 if (p != NULL)
19285 {
19286 verbose_enter();
19287 MSG_PUTS(_("\n\tLast set from "));
19288 MSG_PUTS(p);
19289 vim_free(p);
19290 verbose_leave();
19291 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019292 }
19293}
19294
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295#endif /* FEAT_EVAL */
19296
19297#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19298
19299
19300#ifdef WIN3264
19301/*
19302 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19303 */
19304static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19305static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19306static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19307
19308/*
19309 * Get the short pathname of a file.
19310 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19311 */
19312 static int
19313get_short_pathname(fnamep, bufp, fnamelen)
19314 char_u **fnamep;
19315 char_u **bufp;
19316 int *fnamelen;
19317{
19318 int l,len;
19319 char_u *newbuf;
19320
19321 len = *fnamelen;
19322
19323 l = GetShortPathName(*fnamep, *fnamep, len);
19324 if (l > len - 1)
19325 {
19326 /* If that doesn't work (not enough space), then save the string
19327 * and try again with a new buffer big enough
19328 */
19329 newbuf = vim_strnsave(*fnamep, l);
19330 if (newbuf == NULL)
19331 return 0;
19332
19333 vim_free(*bufp);
19334 *fnamep = *bufp = newbuf;
19335
19336 l = GetShortPathName(*fnamep,*fnamep,l+1);
19337
19338 /* Really should always succeed, as the buffer is big enough */
19339 }
19340
19341 *fnamelen = l;
19342 return 1;
19343}
19344
19345/*
19346 * Create a short path name. Returns the length of the buffer it needs.
19347 * Doesn't copy over the end of the buffer passed in.
19348 */
19349 static int
19350shortpath_for_invalid_fname(fname, bufp, fnamelen)
19351 char_u **fname;
19352 char_u **bufp;
19353 int *fnamelen;
19354{
19355 char_u *s, *p, *pbuf2, *pbuf3;
19356 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019357 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358
19359 /* Make a copy */
19360 len2 = *fnamelen;
19361 pbuf2 = vim_strnsave(*fname, len2);
19362 pbuf3 = NULL;
19363
19364 s = pbuf2 + len2 - 1; /* Find the end */
19365 slen = 1;
19366 plen = len2;
19367
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019368 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 {
19370 --s;
19371 ++slen;
19372 --plen;
19373 }
19374
19375 do
19376 {
19377 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019378 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 {
19380 --s;
19381 ++slen;
19382 --plen;
19383 }
19384 if (s <= pbuf2)
19385 break;
19386
19387 /* Remeber the character that is about to be blatted */
19388 ch = *s;
19389 *s = 0; /* get_short_pathname requires a null-terminated string */
19390
19391 /* Try it in situ */
19392 p = pbuf2;
19393 if (!get_short_pathname(&p, &pbuf3, &plen))
19394 {
19395 vim_free(pbuf2);
19396 return -1;
19397 }
19398 *s = ch; /* Preserve the string */
19399 } while (plen == 0);
19400
19401 if (plen > 0)
19402 {
19403 /* Remeber the length of the new string. */
19404 *fnamelen = len = plen + slen;
19405 vim_free(*bufp);
19406 if (len > len2)
19407 {
19408 /* If there's not enough space in the currently allocated string,
19409 * then copy it to a buffer big enough.
19410 */
19411 *fname= *bufp = vim_strnsave(p, len);
19412 if (*fname == NULL)
19413 return -1;
19414 }
19415 else
19416 {
19417 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19418 *fname = *bufp = pbuf2;
19419 if (p != pbuf2)
19420 strncpy(*fname, p, plen);
19421 pbuf2 = NULL;
19422 }
19423 /* Concat the next bit */
19424 strncpy(*fname + plen, s, slen);
19425 (*fname)[len] = '\0';
19426 }
19427 vim_free(pbuf3);
19428 vim_free(pbuf2);
19429 return 0;
19430}
19431
19432/*
19433 * Get a pathname for a partial path.
19434 */
19435 static int
19436shortpath_for_partial(fnamep, bufp, fnamelen)
19437 char_u **fnamep;
19438 char_u **bufp;
19439 int *fnamelen;
19440{
19441 int sepcount, len, tflen;
19442 char_u *p;
19443 char_u *pbuf, *tfname;
19444 int hasTilde;
19445
19446 /* Count up the path seperators from the RHS.. so we know which part
19447 * of the path to return.
19448 */
19449 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019450 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 if (vim_ispathsep(*p))
19452 ++sepcount;
19453
19454 /* Need full path first (use expand_env() to remove a "~/") */
19455 hasTilde = (**fnamep == '~');
19456 if (hasTilde)
19457 pbuf = tfname = expand_env_save(*fnamep);
19458 else
19459 pbuf = tfname = FullName_save(*fnamep, FALSE);
19460
19461 len = tflen = STRLEN(tfname);
19462
19463 if (!get_short_pathname(&tfname, &pbuf, &len))
19464 return -1;
19465
19466 if (len == 0)
19467 {
19468 /* Don't have a valid filename, so shorten the rest of the
19469 * path if we can. This CAN give us invalid 8.3 filenames, but
19470 * there's not a lot of point in guessing what it might be.
19471 */
19472 len = tflen;
19473 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19474 return -1;
19475 }
19476
19477 /* Count the paths backward to find the beginning of the desired string. */
19478 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019479 {
19480#ifdef FEAT_MBYTE
19481 if (has_mbyte)
19482 p -= mb_head_off(tfname, p);
19483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 if (vim_ispathsep(*p))
19485 {
19486 if (sepcount == 0 || (hasTilde && sepcount == 1))
19487 break;
19488 else
19489 sepcount --;
19490 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 if (hasTilde)
19493 {
19494 --p;
19495 if (p >= tfname)
19496 *p = '~';
19497 else
19498 return -1;
19499 }
19500 else
19501 ++p;
19502
19503 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19504 vim_free(*bufp);
19505 *fnamelen = (int)STRLEN(p);
19506 *bufp = pbuf;
19507 *fnamep = p;
19508
19509 return 0;
19510}
19511#endif /* WIN3264 */
19512
19513/*
19514 * Adjust a filename, according to a string of modifiers.
19515 * *fnamep must be NUL terminated when called. When returning, the length is
19516 * determined by *fnamelen.
19517 * Returns valid flags.
19518 * When there is an error, *fnamep is set to NULL.
19519 */
19520 int
19521modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19522 char_u *src; /* string with modifiers */
19523 int *usedlen; /* characters after src that are used */
19524 char_u **fnamep; /* file name so far */
19525 char_u **bufp; /* buffer for allocated file name or NULL */
19526 int *fnamelen; /* length of fnamep */
19527{
19528 int valid = 0;
19529 char_u *tail;
19530 char_u *s, *p, *pbuf;
19531 char_u dirname[MAXPATHL];
19532 int c;
19533 int has_fullname = 0;
19534#ifdef WIN3264
19535 int has_shortname = 0;
19536#endif
19537
19538repeat:
19539 /* ":p" - full path/file_name */
19540 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19541 {
19542 has_fullname = 1;
19543
19544 valid |= VALID_PATH;
19545 *usedlen += 2;
19546
19547 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19548 if ((*fnamep)[0] == '~'
19549#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19550 && ((*fnamep)[1] == '/'
19551# ifdef BACKSLASH_IN_FILENAME
19552 || (*fnamep)[1] == '\\'
19553# endif
19554 || (*fnamep)[1] == NUL)
19555
19556#endif
19557 )
19558 {
19559 *fnamep = expand_env_save(*fnamep);
19560 vim_free(*bufp); /* free any allocated file name */
19561 *bufp = *fnamep;
19562 if (*fnamep == NULL)
19563 return -1;
19564 }
19565
19566 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019567 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 {
19569 if (vim_ispathsep(*p)
19570 && p[1] == '.'
19571 && (p[2] == NUL
19572 || vim_ispathsep(p[2])
19573 || (p[2] == '.'
19574 && (p[3] == NUL || vim_ispathsep(p[3])))))
19575 break;
19576 }
19577
19578 /* FullName_save() is slow, don't use it when not needed. */
19579 if (*p != NUL || !vim_isAbsName(*fnamep))
19580 {
19581 *fnamep = FullName_save(*fnamep, *p != NUL);
19582 vim_free(*bufp); /* free any allocated file name */
19583 *bufp = *fnamep;
19584 if (*fnamep == NULL)
19585 return -1;
19586 }
19587
19588 /* Append a path separator to a directory. */
19589 if (mch_isdir(*fnamep))
19590 {
19591 /* Make room for one or two extra characters. */
19592 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19593 vim_free(*bufp); /* free any allocated file name */
19594 *bufp = *fnamep;
19595 if (*fnamep == NULL)
19596 return -1;
19597 add_pathsep(*fnamep);
19598 }
19599 }
19600
19601 /* ":." - path relative to the current directory */
19602 /* ":~" - path relative to the home directory */
19603 /* ":8" - shortname path - postponed till after */
19604 while (src[*usedlen] == ':'
19605 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19606 {
19607 *usedlen += 2;
19608 if (c == '8')
19609 {
19610#ifdef WIN3264
19611 has_shortname = 1; /* Postpone this. */
19612#endif
19613 continue;
19614 }
19615 pbuf = NULL;
19616 /* Need full path first (use expand_env() to remove a "~/") */
19617 if (!has_fullname)
19618 {
19619 if (c == '.' && **fnamep == '~')
19620 p = pbuf = expand_env_save(*fnamep);
19621 else
19622 p = pbuf = FullName_save(*fnamep, FALSE);
19623 }
19624 else
19625 p = *fnamep;
19626
19627 has_fullname = 0;
19628
19629 if (p != NULL)
19630 {
19631 if (c == '.')
19632 {
19633 mch_dirname(dirname, MAXPATHL);
19634 s = shorten_fname(p, dirname);
19635 if (s != NULL)
19636 {
19637 *fnamep = s;
19638 if (pbuf != NULL)
19639 {
19640 vim_free(*bufp); /* free any allocated file name */
19641 *bufp = pbuf;
19642 pbuf = NULL;
19643 }
19644 }
19645 }
19646 else
19647 {
19648 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19649 /* Only replace it when it starts with '~' */
19650 if (*dirname == '~')
19651 {
19652 s = vim_strsave(dirname);
19653 if (s != NULL)
19654 {
19655 *fnamep = s;
19656 vim_free(*bufp);
19657 *bufp = s;
19658 }
19659 }
19660 }
19661 vim_free(pbuf);
19662 }
19663 }
19664
19665 tail = gettail(*fnamep);
19666 *fnamelen = (int)STRLEN(*fnamep);
19667
19668 /* ":h" - head, remove "/file_name", can be repeated */
19669 /* Don't remove the first "/" or "c:\" */
19670 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19671 {
19672 valid |= VALID_HEAD;
19673 *usedlen += 2;
19674 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019675 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 --tail;
19677 *fnamelen = (int)(tail - *fnamep);
19678#ifdef VMS
19679 if (*fnamelen > 0)
19680 *fnamelen += 1; /* the path separator is part of the path */
19681#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019682 while (tail > s && !after_pathsep(s, tail))
19683 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 }
19685
19686 /* ":8" - shortname */
19687 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19688 {
19689 *usedlen += 2;
19690#ifdef WIN3264
19691 has_shortname = 1;
19692#endif
19693 }
19694
19695#ifdef WIN3264
19696 /* Check shortname after we have done 'heads' and before we do 'tails'
19697 */
19698 if (has_shortname)
19699 {
19700 pbuf = NULL;
19701 /* Copy the string if it is shortened by :h */
19702 if (*fnamelen < (int)STRLEN(*fnamep))
19703 {
19704 p = vim_strnsave(*fnamep, *fnamelen);
19705 if (p == 0)
19706 return -1;
19707 vim_free(*bufp);
19708 *bufp = *fnamep = p;
19709 }
19710
19711 /* Split into two implementations - makes it easier. First is where
19712 * there isn't a full name already, second is where there is.
19713 */
19714 if (!has_fullname && !vim_isAbsName(*fnamep))
19715 {
19716 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19717 return -1;
19718 }
19719 else
19720 {
19721 int l;
19722
19723 /* Simple case, already have the full-name
19724 * Nearly always shorter, so try first time. */
19725 l = *fnamelen;
19726 if (!get_short_pathname(fnamep, bufp, &l))
19727 return -1;
19728
19729 if (l == 0)
19730 {
19731 /* Couldn't find the filename.. search the paths.
19732 */
19733 l = *fnamelen;
19734 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19735 return -1;
19736 }
19737 *fnamelen = l;
19738 }
19739 }
19740#endif /* WIN3264 */
19741
19742 /* ":t" - tail, just the basename */
19743 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19744 {
19745 *usedlen += 2;
19746 *fnamelen -= (int)(tail - *fnamep);
19747 *fnamep = tail;
19748 }
19749
19750 /* ":e" - extension, can be repeated */
19751 /* ":r" - root, without extension, can be repeated */
19752 while (src[*usedlen] == ':'
19753 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19754 {
19755 /* find a '.' in the tail:
19756 * - for second :e: before the current fname
19757 * - otherwise: The last '.'
19758 */
19759 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19760 s = *fnamep - 2;
19761 else
19762 s = *fnamep + *fnamelen - 1;
19763 for ( ; s > tail; --s)
19764 if (s[0] == '.')
19765 break;
19766 if (src[*usedlen + 1] == 'e') /* :e */
19767 {
19768 if (s > tail)
19769 {
19770 *fnamelen += (int)(*fnamep - (s + 1));
19771 *fnamep = s + 1;
19772#ifdef VMS
19773 /* cut version from the extension */
19774 s = *fnamep + *fnamelen - 1;
19775 for ( ; s > *fnamep; --s)
19776 if (s[0] == ';')
19777 break;
19778 if (s > *fnamep)
19779 *fnamelen = s - *fnamep;
19780#endif
19781 }
19782 else if (*fnamep <= tail)
19783 *fnamelen = 0;
19784 }
19785 else /* :r */
19786 {
19787 if (s > tail) /* remove one extension */
19788 *fnamelen = (int)(s - *fnamep);
19789 }
19790 *usedlen += 2;
19791 }
19792
19793 /* ":s?pat?foo?" - substitute */
19794 /* ":gs?pat?foo?" - global substitute */
19795 if (src[*usedlen] == ':'
19796 && (src[*usedlen + 1] == 's'
19797 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19798 {
19799 char_u *str;
19800 char_u *pat;
19801 char_u *sub;
19802 int sep;
19803 char_u *flags;
19804 int didit = FALSE;
19805
19806 flags = (char_u *)"";
19807 s = src + *usedlen + 2;
19808 if (src[*usedlen + 1] == 'g')
19809 {
19810 flags = (char_u *)"g";
19811 ++s;
19812 }
19813
19814 sep = *s++;
19815 if (sep)
19816 {
19817 /* find end of pattern */
19818 p = vim_strchr(s, sep);
19819 if (p != NULL)
19820 {
19821 pat = vim_strnsave(s, (int)(p - s));
19822 if (pat != NULL)
19823 {
19824 s = p + 1;
19825 /* find end of substitution */
19826 p = vim_strchr(s, sep);
19827 if (p != NULL)
19828 {
19829 sub = vim_strnsave(s, (int)(p - s));
19830 str = vim_strnsave(*fnamep, *fnamelen);
19831 if (sub != NULL && str != NULL)
19832 {
19833 *usedlen = (int)(p + 1 - src);
19834 s = do_string_sub(str, pat, sub, flags);
19835 if (s != NULL)
19836 {
19837 *fnamep = s;
19838 *fnamelen = (int)STRLEN(s);
19839 vim_free(*bufp);
19840 *bufp = s;
19841 didit = TRUE;
19842 }
19843 }
19844 vim_free(sub);
19845 vim_free(str);
19846 }
19847 vim_free(pat);
19848 }
19849 }
19850 /* after using ":s", repeat all the modifiers */
19851 if (didit)
19852 goto repeat;
19853 }
19854 }
19855
19856 return valid;
19857}
19858
19859/*
19860 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19861 * "flags" can be "g" to do a global substitute.
19862 * Returns an allocated string, NULL for error.
19863 */
19864 char_u *
19865do_string_sub(str, pat, sub, flags)
19866 char_u *str;
19867 char_u *pat;
19868 char_u *sub;
19869 char_u *flags;
19870{
19871 int sublen;
19872 regmatch_T regmatch;
19873 int i;
19874 int do_all;
19875 char_u *tail;
19876 garray_T ga;
19877 char_u *ret;
19878 char_u *save_cpo;
19879
19880 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19881 save_cpo = p_cpo;
19882 p_cpo = (char_u *)"";
19883
19884 ga_init2(&ga, 1, 200);
19885
19886 do_all = (flags[0] == 'g');
19887
19888 regmatch.rm_ic = p_ic;
19889 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19890 if (regmatch.regprog != NULL)
19891 {
19892 tail = str;
19893 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19894 {
19895 /*
19896 * Get some space for a temporary buffer to do the substitution
19897 * into. It will contain:
19898 * - The text up to where the match is.
19899 * - The substituted text.
19900 * - The text after the match.
19901 */
19902 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19903 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19904 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19905 {
19906 ga_clear(&ga);
19907 break;
19908 }
19909
19910 /* copy the text up to where the match is */
19911 i = (int)(regmatch.startp[0] - tail);
19912 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19913 /* add the substituted text */
19914 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19915 + ga.ga_len + i, TRUE, TRUE, FALSE);
19916 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 /* avoid getting stuck on a match with an empty string */
19918 if (tail == regmatch.endp[0])
19919 {
19920 if (*tail == NUL)
19921 break;
19922 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19923 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 }
19925 else
19926 {
19927 tail = regmatch.endp[0];
19928 if (*tail == NUL)
19929 break;
19930 }
19931 if (!do_all)
19932 break;
19933 }
19934
19935 if (ga.ga_data != NULL)
19936 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19937
19938 vim_free(regmatch.regprog);
19939 }
19940
19941 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19942 ga_clear(&ga);
19943 p_cpo = save_cpo;
19944
19945 return ret;
19946}
19947
19948#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */