blob: 47fccbca9faa7d619cacf06e73edd22b9b12913f [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));
403static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static void list_free __ARGS((list_T *l));
405static listitem_T *listitem_alloc __ARGS((void));
406static void listitem_free __ARGS((listitem_T *item));
407static void listitem_remove __ARGS((list_T *l, listitem_T *item));
408static long list_len __ARGS((list_T *l));
409static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
410static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
411static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static listitem_T *list_find __ARGS((list_T *l, long n));
413static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static void list_append __ARGS((list_T *l, listitem_T *item));
415static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000416static int list_append_string __ARGS((list_T *l, char_u *str, int len));
417static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
419static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
420static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000423static char_u *list2string __ARGS((typval_T *tv, int copyID));
424static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000425static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
426static void set_ref_in_list __ARGS((list_T *l, int copyID));
427static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static void dict_unref __ARGS((dict_T *d));
429static void dict_free __ARGS((dict_T *d));
430static dictitem_T *dictitem_alloc __ARGS((char_u *key));
431static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
432static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
433static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int dict_add __ARGS((dict_T *d, dictitem_T *item));
436static long dict_len __ARGS((dict_T *d));
437static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static char_u *string_quote __ARGS((char_u *str, int function));
443static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
444static int find_internal_func __ARGS((char_u *name));
445static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
446static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
447static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000448static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
450static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000469#if defined(FEAT_INS_EXPAND)
470static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
472#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
478static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000504static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000506static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000512static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000520static void f_getloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000521static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000544static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000550static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000567static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571#ifdef vim_mkdir
572static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
573#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000578static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000580static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000592static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000599static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000600static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000605static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
609#ifdef HAVE_STRFTIME
610static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
612static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000624static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000625static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000627static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000642static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000643
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
645static int get_env_len __ARGS((char_u **arg));
646static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000647static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000648static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
649#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
650#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
651 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000652static 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 +0000653static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000654static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
656static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static typval_T *alloc_tv __ARGS((void));
658static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void init_tv __ARGS((typval_T *varp));
660static long get_tv_number __ARGS((typval_T *varp));
661static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000662static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static char_u *get_tv_string __ARGS((typval_T *varp));
664static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000665static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000667static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
669static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
670static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
671static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
672static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
673static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
674static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000675static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000677static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
679static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
680static int eval_fname_script __ARGS((char_u *p));
681static int eval_fname_sid __ARGS((char_u *p));
682static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static ufunc_T *find_func __ARGS((char_u *name));
684static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000685static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000686#ifdef FEAT_PROFILE
687static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000688static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
689static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
690static int
691# ifdef __BORLANDC__
692 _RTLENTRYF
693# endif
694 prof_total_cmp __ARGS((const void *s1, const void *s2));
695static int
696# ifdef __BORLANDC__
697 _RTLENTRYF
698# endif
699 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000701static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000702static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000703static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static void func_free __ARGS((ufunc_T *fp));
705static void func_unref __ARGS((char_u *name));
706static void func_ref __ARGS((char_u *name));
707static 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));
708static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000709static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000711/* Character used as separated in autoload function/variable names. */
712#define AUTOLOAD_CHAR '#'
713
Bram Moolenaar33570922005-01-25 22:26:29 +0000714/*
715 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000716 */
717 void
718eval_init()
719{
Bram Moolenaar33570922005-01-25 22:26:29 +0000720 int i;
721 struct vimvar *p;
722
723 init_var_dict(&globvardict, &globvars_var);
724 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000725 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000726 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000727
728 for (i = 0; i < VV_LEN; ++i)
729 {
730 p = &vimvars[i];
731 STRCPY(p->vv_di.di_key, p->vv_name);
732 if (p->vv_flags & VV_RO)
733 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
734 else if (p->vv_flags & VV_RO_SBX)
735 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
736 else
737 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000738
739 /* add to v: scope dict, unless the value is not always available */
740 if (p->vv_type != VAR_UNKNOWN)
741 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000742 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000743 /* add to compat scope dict */
744 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000745 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000746}
747
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000748#if defined(EXITFREE) || defined(PROTO)
749 void
750eval_clear()
751{
752 int i;
753 struct vimvar *p;
754
755 for (i = 0; i < VV_LEN; ++i)
756 {
757 p = &vimvars[i];
758 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000759 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000760 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000761 p->vv_di.di_tv.vval.v_string = NULL;
762 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000763 }
764 hash_clear(&vimvarht);
765 hash_clear(&compat_hashtab);
766
767 /* script-local variables */
768 for (i = 1; i <= ga_scripts.ga_len; ++i)
769 vars_clear(&SCRIPT_VARS(i));
770 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000771 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000772
773 /* global variables */
774 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000775
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000776 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000777 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000778 hash_clear(&func_hashtab);
779
780 /* unreferenced lists and dicts */
781 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000782}
783#endif
784
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 * Return the name of the executed function.
787 */
788 char_u *
789func_name(cookie)
790 void *cookie;
791{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000792 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793}
794
795/*
796 * Return the address holding the next breakpoint line for a funccall cookie.
797 */
798 linenr_T *
799func_breakpoint(cookie)
800 void *cookie;
801{
Bram Moolenaar33570922005-01-25 22:26:29 +0000802 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803}
804
805/*
806 * Return the address holding the debug tick for a funccall cookie.
807 */
808 int *
809func_dbg_tick(cookie)
810 void *cookie;
811{
Bram Moolenaar33570922005-01-25 22:26:29 +0000812 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813}
814
815/*
816 * Return the nesting level for a funccall cookie.
817 */
818 int
819func_level(cookie)
820 void *cookie;
821{
Bram Moolenaar33570922005-01-25 22:26:29 +0000822 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823}
824
825/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000826funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828/*
829 * Return TRUE when a function was ended by a ":return" command.
830 */
831 int
832current_func_returned()
833{
834 return current_funccal->returned;
835}
836
837
838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 * Set an internal variable to a string value. Creates the variable if it does
840 * not already exist.
841 */
842 void
843set_internal_string_var(name, value)
844 char_u *name;
845 char_u *value;
846{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000847 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000848 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
850 val = vim_strsave(value);
851 if (val != NULL)
852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000853 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000854 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000856 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000857 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 }
860}
861
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000862static lval_T *redir_lval = NULL;
863static char_u *redir_endp = NULL;
864static char_u *redir_varname = NULL;
865
866/*
867 * Start recording command output to a variable
868 * Returns OK if successfully completed the setup. FAIL otherwise.
869 */
870 int
871var_redir_start(name, append)
872 char_u *name;
873 int append; /* append to an existing variable */
874{
875 int save_emsg;
876 int err;
877 typval_T tv;
878
879 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000880 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000881 {
882 EMSG(_(e_invarg));
883 return FAIL;
884 }
885
886 redir_varname = vim_strsave(name);
887 if (redir_varname == NULL)
888 return FAIL;
889
890 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
891 if (redir_lval == NULL)
892 {
893 var_redir_stop();
894 return FAIL;
895 }
896
897 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000898 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
899 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
901 {
902 if (redir_endp != NULL && *redir_endp != NUL)
903 /* Trailing characters are present after the variable name */
904 EMSG(_(e_trailing));
905 else
906 EMSG(_(e_invarg));
907 var_redir_stop();
908 return FAIL;
909 }
910
911 /* check if we can write to the variable: set it to or append an empty
912 * string */
913 save_emsg = did_emsg;
914 did_emsg = FALSE;
915 tv.v_type = VAR_STRING;
916 tv.vval.v_string = (char_u *)"";
917 if (append)
918 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
919 else
920 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
921 err = did_emsg;
922 did_emsg += save_emsg;
923 if (err)
924 {
925 var_redir_stop();
926 return FAIL;
927 }
928 if (redir_lval->ll_newkey != NULL)
929 {
930 /* Dictionary item was created, don't do it again. */
931 vim_free(redir_lval->ll_newkey);
932 redir_lval->ll_newkey = NULL;
933 }
934
935 return OK;
936}
937
938/*
939 * Append "value[len]" to the variable set by var_redir_start().
940 */
941 void
942var_redir_str(value, len)
943 char_u *value;
944 int len;
945{
946 char_u *val;
947 typval_T tv;
948 int save_emsg;
949 int err;
950
951 if (redir_lval == NULL)
952 return;
953
954 if (len == -1)
955 /* Append the entire string */
956 val = vim_strsave(value);
957 else
958 /* Append only the specified number of characters */
959 val = vim_strnsave(value, len);
960 if (val == NULL)
961 return;
962
963 tv.v_type = VAR_STRING;
964 tv.vval.v_string = val;
965
966 save_emsg = did_emsg;
967 did_emsg = FALSE;
968 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
969 err = did_emsg;
970 did_emsg += save_emsg;
971 if (err)
972 var_redir_stop();
973
974 vim_free(tv.vval.v_string);
975}
976
977/*
978 * Stop redirecting command output to a variable.
979 */
980 void
981var_redir_stop()
982{
983 if (redir_lval != NULL)
984 {
985 clear_lval(redir_lval);
986 vim_free(redir_lval);
987 redir_lval = NULL;
988 }
989 vim_free(redir_varname);
990 redir_varname = NULL;
991}
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993# if defined(FEAT_MBYTE) || defined(PROTO)
994 int
995eval_charconvert(enc_from, enc_to, fname_from, fname_to)
996 char_u *enc_from;
997 char_u *enc_to;
998 char_u *fname_from;
999 char_u *fname_to;
1000{
1001 int err = FALSE;
1002
1003 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1004 set_vim_var_string(VV_CC_TO, enc_to, -1);
1005 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1006 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1007 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1008 err = TRUE;
1009 set_vim_var_string(VV_CC_FROM, NULL, -1);
1010 set_vim_var_string(VV_CC_TO, NULL, -1);
1011 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1012 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1013
1014 if (err)
1015 return FAIL;
1016 return OK;
1017}
1018# endif
1019
1020# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1021 int
1022eval_printexpr(fname, args)
1023 char_u *fname;
1024 char_u *args;
1025{
1026 int err = FALSE;
1027
1028 set_vim_var_string(VV_FNAME_IN, fname, -1);
1029 set_vim_var_string(VV_CMDARG, args, -1);
1030 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1031 err = TRUE;
1032 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1033 set_vim_var_string(VV_CMDARG, NULL, -1);
1034
1035 if (err)
1036 {
1037 mch_remove(fname);
1038 return FAIL;
1039 }
1040 return OK;
1041}
1042# endif
1043
1044# if defined(FEAT_DIFF) || defined(PROTO)
1045 void
1046eval_diff(origfile, newfile, outfile)
1047 char_u *origfile;
1048 char_u *newfile;
1049 char_u *outfile;
1050{
1051 int err = FALSE;
1052
1053 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1054 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1055 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1056 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1057 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1058 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1059 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1060}
1061
1062 void
1063eval_patch(origfile, difffile, outfile)
1064 char_u *origfile;
1065 char_u *difffile;
1066 char_u *outfile;
1067{
1068 int err;
1069
1070 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1071 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1072 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1073 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1074 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1075 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1076 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1077}
1078# endif
1079
1080/*
1081 * Top level evaluation function, returning a boolean.
1082 * Sets "error" to TRUE if there was an error.
1083 * Return TRUE or FALSE.
1084 */
1085 int
1086eval_to_bool(arg, error, nextcmd, skip)
1087 char_u *arg;
1088 int *error;
1089 char_u **nextcmd;
1090 int skip; /* only parse, don't execute */
1091{
Bram Moolenaar33570922005-01-25 22:26:29 +00001092 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 int retval = FALSE;
1094
1095 if (skip)
1096 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001097 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 else
1100 {
1101 *error = FALSE;
1102 if (!skip)
1103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001104 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001105 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 }
1108 if (skip)
1109 --emsg_skip;
1110
1111 return retval;
1112}
1113
1114/*
1115 * Top level evaluation function, returning a string. If "skip" is TRUE,
1116 * only parsing to "nextcmd" is done, without reporting errors. Return
1117 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1118 */
1119 char_u *
1120eval_to_string_skip(arg, nextcmd, skip)
1121 char_u *arg;
1122 char_u **nextcmd;
1123 int skip; /* only parse, don't execute */
1124{
Bram Moolenaar33570922005-01-25 22:26:29 +00001125 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 char_u *retval;
1127
1128 if (skip)
1129 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 retval = NULL;
1132 else
1133 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001134 retval = vim_strsave(get_tv_string(&tv));
1135 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137 if (skip)
1138 --emsg_skip;
1139
1140 return retval;
1141}
1142
1143/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001144 * Skip over an expression at "*pp".
1145 * Return FAIL for an error, OK otherwise.
1146 */
1147 int
1148skip_expr(pp)
1149 char_u **pp;
1150{
Bram Moolenaar33570922005-01-25 22:26:29 +00001151 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001152
1153 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001155}
1156
1157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 * Top level evaluation function, returning a string.
1159 * Return pointer to allocated memory, or NULL for failure.
1160 */
1161 char_u *
1162eval_to_string(arg, nextcmd)
1163 char_u *arg;
1164 char_u **nextcmd;
1165{
Bram Moolenaar33570922005-01-25 22:26:29 +00001166 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 char_u *retval;
1168
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001169 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 retval = NULL;
1171 else
1172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001173 retval = vim_strsave(get_tv_string(&tv));
1174 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176
1177 return retval;
1178}
1179
1180/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001181 * Call eval_to_string() without using current local variables and using
1182 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 */
1184 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001185eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 char_u *arg;
1187 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001188 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
1190 char_u *retval;
1191 void *save_funccalp;
1192
1193 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001194 if (use_sandbox)
1195 ++sandbox;
1196 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001198 if (use_sandbox)
1199 --sandbox;
1200 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 restore_funccal(save_funccalp);
1202 return retval;
1203}
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205/*
1206 * Top level evaluation function, returning a number.
1207 * Evaluates "expr" silently.
1208 * Returns -1 for an error.
1209 */
1210 int
1211eval_to_number(expr)
1212 char_u *expr;
1213{
Bram Moolenaar33570922005-01-25 22:26:29 +00001214 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001216 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
1218 ++emsg_off;
1219
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 retval = -1;
1222 else
1223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001224 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 --emsg_off;
1228
1229 return retval;
1230}
1231
Bram Moolenaara40058a2005-07-11 22:42:07 +00001232/*
1233 * Prepare v: variable "idx" to be used.
1234 * Save the current typeval in "save_tv".
1235 * When not used yet add the variable to the v: hashtable.
1236 */
1237 static void
1238prepare_vimvar(idx, save_tv)
1239 int idx;
1240 typval_T *save_tv;
1241{
1242 *save_tv = vimvars[idx].vv_tv;
1243 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1244 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1245}
1246
1247/*
1248 * Restore v: variable "idx" to typeval "save_tv".
1249 * When no longer defined, remove the variable from the v: hashtable.
1250 */
1251 static void
1252restore_vimvar(idx, save_tv)
1253 int idx;
1254 typval_T *save_tv;
1255{
1256 hashitem_T *hi;
1257
1258 clear_tv(&vimvars[idx].vv_tv);
1259 vimvars[idx].vv_tv = *save_tv;
1260 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1261 {
1262 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1263 if (HASHITEM_EMPTY(hi))
1264 EMSG2(_(e_intern2), "restore_vimvar()");
1265 else
1266 hash_remove(&vimvarht, hi);
1267 }
1268}
1269
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001270#if defined(FEAT_SYN_HL) || defined(PROTO)
1271/*
1272 * Evaluate an expression to a list with suggestions.
1273 * For the "expr:" part of 'spellsuggest'.
1274 */
1275 list_T *
1276eval_spell_expr(badword, expr)
1277 char_u *badword;
1278 char_u *expr;
1279{
1280 typval_T save_val;
1281 typval_T rettv;
1282 list_T *list = NULL;
1283 char_u *p = skipwhite(expr);
1284
1285 /* Set "v:val" to the bad word. */
1286 prepare_vimvar(VV_VAL, &save_val);
1287 vimvars[VV_VAL].vv_type = VAR_STRING;
1288 vimvars[VV_VAL].vv_str = badword;
1289 if (p_verbose == 0)
1290 ++emsg_off;
1291
1292 if (eval1(&p, &rettv, TRUE) == OK)
1293 {
1294 if (rettv.v_type != VAR_LIST)
1295 clear_tv(&rettv);
1296 else
1297 list = rettv.vval.v_list;
1298 }
1299
1300 if (p_verbose == 0)
1301 --emsg_off;
1302 vimvars[VV_VAL].vv_str = NULL;
1303 restore_vimvar(VV_VAL, &save_val);
1304
1305 return list;
1306}
1307
1308/*
1309 * "list" is supposed to contain two items: a word and a number. Return the
1310 * word in "pp" and the number as the return value.
1311 * Return -1 if anything isn't right.
1312 * Used to get the good word and score from the eval_spell_expr() result.
1313 */
1314 int
1315get_spellword(list, pp)
1316 list_T *list;
1317 char_u **pp;
1318{
1319 listitem_T *li;
1320
1321 li = list->lv_first;
1322 if (li == NULL)
1323 return -1;
1324 *pp = get_tv_string(&li->li_tv);
1325
1326 li = li->li_next;
1327 if (li == NULL)
1328 return -1;
1329 return get_tv_number(&li->li_tv);
1330}
1331#endif
1332
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001333/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001334 * Top level evaluation function.
1335 * Returns an allocated typval_T with the result.
1336 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001337 */
1338 typval_T *
1339eval_expr(arg, nextcmd)
1340 char_u *arg;
1341 char_u **nextcmd;
1342{
1343 typval_T *tv;
1344
1345 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001346 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001347 {
1348 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001349 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001350 }
1351
1352 return tv;
1353}
1354
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1357/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001358 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001360 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001362 static int
1363call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 char_u *func;
1365 int argc;
1366 char_u **argv;
1367 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
Bram Moolenaar33570922005-01-25 22:26:29 +00001370 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 long n;
1372 int len;
1373 int i;
1374 int doesrange;
1375 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001376 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
Bram Moolenaar33570922005-01-25 22:26:29 +00001378 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001380 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 for (i = 0; i < argc; i++)
1383 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001384 /* Pass a NULL or empty argument as an empty string */
1385 if (argv[i] == NULL || *argv[i] == NUL)
1386 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001387 argvars[i].v_type = VAR_STRING;
1388 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001389 continue;
1390 }
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 /* Recognize a number argument, the others must be strings. */
1393 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1394 if (len != 0 && len == (int)STRLEN(argv[i]))
1395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001396 argvars[i].v_type = VAR_NUMBER;
1397 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 else
1400 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001401 argvars[i].v_type = VAR_STRING;
1402 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404 }
1405
1406 if (safe)
1407 {
1408 save_funccalp = save_funccal();
1409 ++sandbox;
1410 }
1411
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001412 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1413 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001415 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (safe)
1417 {
1418 --sandbox;
1419 restore_funccal(save_funccalp);
1420 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001421 vim_free(argvars);
1422
1423 if (ret == FAIL)
1424 clear_tv(rettv);
1425
1426 return ret;
1427}
1428
1429/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001430 * Call vimL function "func" and return the result as a string.
1431 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001432 * Uses argv[argc] for the function arguments.
1433 */
1434 void *
1435call_func_retstr(func, argc, argv, safe)
1436 char_u *func;
1437 int argc;
1438 char_u **argv;
1439 int safe; /* use the sandbox */
1440{
1441 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001442 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001443
1444 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1445 return NULL;
1446
1447 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001448 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 return retval;
1450}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001451
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001452#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001453/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001454 * Call vimL function "func" and return the result as a number.
1455 * Returns -1 when calling the function fails.
1456 * Uses argv[argc] for the function arguments.
1457 */
1458 long
1459call_func_retnr(func, argc, argv, safe)
1460 char_u *func;
1461 int argc;
1462 char_u **argv;
1463 int safe; /* use the sandbox */
1464{
1465 typval_T rettv;
1466 long retval;
1467
1468 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1469 return -1;
1470
1471 retval = get_tv_number_chk(&rettv, NULL);
1472 clear_tv(&rettv);
1473 return retval;
1474}
1475#endif
1476
1477/*
1478 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479 * Uses argv[argc] for the function arguments.
1480 */
1481 void *
1482call_func_retlist(func, argc, argv, safe)
1483 char_u *func;
1484 int argc;
1485 char_u **argv;
1486 int safe; /* use the sandbox */
1487{
1488 typval_T rettv;
1489
1490 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1491 return NULL;
1492
1493 if (rettv.v_type != VAR_LIST)
1494 {
1495 clear_tv(&rettv);
1496 return NULL;
1497 }
1498
1499 return rettv.vval.v_list;
1500}
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502#endif
1503
1504/*
1505 * Save the current function call pointer, and set it to NULL.
1506 * Used when executing autocommands and for ":source".
1507 */
1508 void *
1509save_funccal()
1510{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001511 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 current_funccal = NULL;
1514 return (void *)fc;
1515}
1516
1517 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001518restore_funccal(vfc)
1519 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001521 funccall_T *fc = (funccall_T *)vfc;
1522
1523 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524}
1525
Bram Moolenaar05159a02005-02-26 23:04:13 +00001526#if defined(FEAT_PROFILE) || defined(PROTO)
1527/*
1528 * Prepare profiling for entering a child or something else that is not
1529 * counted for the script/function itself.
1530 * Should always be called in pair with prof_child_exit().
1531 */
1532 void
1533prof_child_enter(tm)
1534 proftime_T *tm; /* place to store waittime */
1535{
1536 funccall_T *fc = current_funccal;
1537
1538 if (fc != NULL && fc->func->uf_profiling)
1539 profile_start(&fc->prof_child);
1540 script_prof_save(tm);
1541}
1542
1543/*
1544 * Take care of time spent in a child.
1545 * Should always be called after prof_child_enter().
1546 */
1547 void
1548prof_child_exit(tm)
1549 proftime_T *tm; /* where waittime was stored */
1550{
1551 funccall_T *fc = current_funccal;
1552
1553 if (fc != NULL && fc->func->uf_profiling)
1554 {
1555 profile_end(&fc->prof_child);
1556 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1557 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1558 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1559 }
1560 script_prof_restore(tm);
1561}
1562#endif
1563
1564
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#ifdef FEAT_FOLDING
1566/*
1567 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1568 * it in "*cp". Doesn't give error messages.
1569 */
1570 int
1571eval_foldexpr(arg, cp)
1572 char_u *arg;
1573 int *cp;
1574{
Bram Moolenaar33570922005-01-25 22:26:29 +00001575 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 int retval;
1577 char_u *s;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001578 int use_sandbox = was_set_insecurely((char_u *)"foldexpr");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001581 if (use_sandbox)
1582 ++sandbox;
1583 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 retval = 0;
1587 else
1588 {
1589 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590 if (tv.v_type == VAR_NUMBER)
1591 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001592 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 retval = 0;
1594 else
1595 {
1596 /* If the result is a string, check if there is a non-digit before
1597 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (!VIM_ISDIGIT(*s) && *s != '-')
1600 *cp = *s++;
1601 retval = atol((char *)s);
1602 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001603 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
1605 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001606 if (use_sandbox)
1607 --sandbox;
1608 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
1610 return retval;
1611}
1612#endif
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615 * ":let" list all variable values
1616 * ":let var1 var2" list variable values
1617 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001618 * ":let var += expr" assignment command.
1619 * ":let var -= expr" assignment command.
1620 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001621 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 */
1623 void
1624ex_let(eap)
1625 exarg_T *eap;
1626{
1627 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001629 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 int var_count = 0;
1632 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 expr = skip_var_list(arg, &var_count, &semicolon);
1636 if (expr == NULL)
1637 return;
1638 expr = vim_strchr(expr, '=');
1639 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001641 /*
1642 * ":let" without "=": list variables
1643 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001644 if (*arg == '[')
1645 EMSG(_(e_invarg));
1646 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647 /* ":let var1 var2" */
1648 arg = list_arg_vars(eap, arg);
1649 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001652 list_glob_vars();
1653 list_buf_vars();
1654 list_win_vars();
1655 list_vim_vars();
1656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 eap->nextcmd = check_nextcmd(arg);
1658 }
1659 else
1660 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001661 op[0] = '=';
1662 op[1] = NUL;
1663 if (expr > arg)
1664 {
1665 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1666 op[0] = expr[-1]; /* +=, -= or .= */
1667 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (eap->skip)
1671 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (eap->skip)
1674 {
1675 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 --emsg_skip;
1678 }
1679 else if (i != FAIL)
1680 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001682 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 }
1685 }
1686}
1687
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688/*
1689 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1690 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 * When "nextchars" is not NULL it points to a string with characters that
1692 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1693 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 * Returns OK or FAIL;
1695 */
1696 static int
1697ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1698 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001699 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700 int copy; /* copy values from "tv", don't move */
1701 int semicolon; /* from skip_var_list() */
1702 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001703 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704{
1705 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001706 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001708 listitem_T *item;
1709 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710
1711 if (*arg != '[')
1712 {
1713 /*
1714 * ":let var = expr" or ":for var in list"
1715 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001716 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 return FAIL;
1718 return OK;
1719 }
1720
1721 /*
1722 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1723 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001724 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 {
1726 EMSG(_(e_listreq));
1727 return FAIL;
1728 }
1729
1730 i = list_len(l);
1731 if (semicolon == 0 && var_count < i)
1732 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001733 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734 return FAIL;
1735 }
1736 if (var_count - semicolon > i)
1737 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001738 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739 return FAIL;
1740 }
1741
1742 item = l->lv_first;
1743 while (*arg != ']')
1744 {
1745 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 item = item->li_next;
1748 if (arg == NULL)
1749 return FAIL;
1750
1751 arg = skipwhite(arg);
1752 if (*arg == ';')
1753 {
1754 /* Put the rest of the list (may be empty) in the var after ';'.
1755 * Create a new list for this. */
1756 l = list_alloc();
1757 if (l == NULL)
1758 return FAIL;
1759 while (item != NULL)
1760 {
1761 list_append_tv(l, &item->li_tv);
1762 item = item->li_next;
1763 }
1764
1765 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001766 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767 ltv.vval.v_list = l;
1768 l->lv_refcount = 1;
1769
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001770 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1771 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 clear_tv(&ltv);
1773 if (arg == NULL)
1774 return FAIL;
1775 break;
1776 }
1777 else if (*arg != ',' && *arg != ']')
1778 {
1779 EMSG2(_(e_intern2), "ex_let_vars()");
1780 return FAIL;
1781 }
1782 }
1783
1784 return OK;
1785}
1786
1787/*
1788 * Skip over assignable variable "var" or list of variables "[var, var]".
1789 * Used for ":let varvar = expr" and ":for varvar in expr".
1790 * For "[var, var]" increment "*var_count" for each variable.
1791 * for "[var, var; var]" set "semicolon".
1792 * Return NULL for an error.
1793 */
1794 static char_u *
1795skip_var_list(arg, var_count, semicolon)
1796 char_u *arg;
1797 int *var_count;
1798 int *semicolon;
1799{
1800 char_u *p, *s;
1801
1802 if (*arg == '[')
1803 {
1804 /* "[var, var]": find the matching ']'. */
1805 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001806 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001807 {
1808 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1809 s = skip_var_one(p);
1810 if (s == p)
1811 {
1812 EMSG2(_(e_invarg2), p);
1813 return NULL;
1814 }
1815 ++*var_count;
1816
1817 p = skipwhite(s);
1818 if (*p == ']')
1819 break;
1820 else if (*p == ';')
1821 {
1822 if (*semicolon == 1)
1823 {
1824 EMSG(_("Double ; in list of variables"));
1825 return NULL;
1826 }
1827 *semicolon = 1;
1828 }
1829 else if (*p != ',')
1830 {
1831 EMSG2(_(e_invarg2), p);
1832 return NULL;
1833 }
1834 }
1835 return p + 1;
1836 }
1837 else
1838 return skip_var_one(arg);
1839}
1840
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001841/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001842 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1843 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001844 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 static char_u *
1846skip_var_one(arg)
1847 char_u *arg;
1848{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001849 if (*arg == '@' && arg[1] != NUL)
1850 return arg + 2;
1851 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1852 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853}
1854
Bram Moolenaara7043832005-01-21 11:56:39 +00001855/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001856 * List variables for hashtab "ht" with prefix "prefix".
1857 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001860list_hashtable_vars(ht, prefix, empty)
1861 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001863 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001864{
Bram Moolenaar33570922005-01-25 22:26:29 +00001865 hashitem_T *hi;
1866 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001867 int todo;
1868
1869 todo = ht->ht_used;
1870 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1871 {
1872 if (!HASHITEM_EMPTY(hi))
1873 {
1874 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001875 di = HI2DI(hi);
1876 if (empty || di->di_tv.v_type != VAR_STRING
1877 || di->di_tv.vval.v_string != NULL)
1878 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001879 }
1880 }
1881}
1882
1883/*
1884 * List global variables.
1885 */
1886 static void
1887list_glob_vars()
1888{
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001890}
1891
1892/*
1893 * List buffer variables.
1894 */
1895 static void
1896list_buf_vars()
1897{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001898 char_u numbuf[NUMBUFLEN];
1899
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001901
1902 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1903 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001904}
1905
1906/*
1907 * List window variables.
1908 */
1909 static void
1910list_win_vars()
1911{
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001913}
1914
1915/*
1916 * List Vim variables.
1917 */
1918 static void
1919list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920{
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922}
1923
1924/*
1925 * List variables in "arg".
1926 */
1927 static char_u *
1928list_arg_vars(eap, arg)
1929 exarg_T *eap;
1930 char_u *arg;
1931{
1932 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001933 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001935 char_u *name_start;
1936 char_u *arg_subsc;
1937 char_u *tofree;
1938 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939
1940 while (!ends_excmd(*arg) && !got_int)
1941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001944 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001945 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1946 {
1947 emsg_severe = TRUE;
1948 EMSG(_(e_trailing));
1949 break;
1950 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001952 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001954 /* get_name_len() takes care of expanding curly braces */
1955 name_start = name = arg;
1956 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1957 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001959 /* This is mainly to keep test 49 working: when expanding
1960 * curly braces fails overrule the exception error message. */
1961 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001963 emsg_severe = TRUE;
1964 EMSG2(_(e_invarg2), arg);
1965 break;
1966 }
1967 error = TRUE;
1968 }
1969 else
1970 {
1971 if (tofree != NULL)
1972 name = tofree;
1973 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 else
1976 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001977 /* handle d.key, l[idx], f(expr) */
1978 arg_subsc = arg;
1979 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001980 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001982 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001983 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001984 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001985 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001986 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001987 case 'g': list_glob_vars(); break;
1988 case 'b': list_buf_vars(); break;
1989 case 'w': list_win_vars(); break;
1990 case 'v': list_vim_vars(); break;
1991 default:
1992 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001994 }
1995 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996 {
1997 char_u numbuf[NUMBUFLEN];
1998 char_u *tf;
1999 int c;
2000 char_u *s;
2001
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002002 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002003 c = *arg;
2004 *arg = NUL;
2005 list_one_var_a((char_u *)"",
2006 arg == arg_subsc ? name : name_start,
2007 tv.v_type, s == NULL ? (char_u *)"" : s);
2008 *arg = c;
2009 vim_free(tf);
2010 }
2011 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002012 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 }
2014 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002015
2016 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002018
2019 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 }
2021
2022 return arg;
2023}
2024
2025/*
2026 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2027 * Returns a pointer to the char just after the var name.
2028 * Returns NULL if there is an error.
2029 */
2030 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002033 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002036 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037{
2038 int c1;
2039 char_u *name;
2040 char_u *p;
2041 char_u *arg_end = NULL;
2042 int len;
2043 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045
2046 /*
2047 * ":let $VAR = expr": Set environment variable.
2048 */
2049 if (*arg == '$')
2050 {
2051 /* Find the end of the name. */
2052 ++arg;
2053 name = arg;
2054 len = get_env_len(&arg);
2055 if (len == 0)
2056 EMSG2(_(e_invarg2), name - 1);
2057 else
2058 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002059 if (op != NULL && (*op == '+' || *op == '-'))
2060 EMSG2(_(e_letwrong), op);
2061 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 EMSG(_(e_letunexp));
2064 else
2065 {
2066 c1 = name[len];
2067 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002068 p = get_tv_string_chk(tv);
2069 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002070 {
2071 int mustfree = FALSE;
2072 char_u *s = vim_getenv(name, &mustfree);
2073
2074 if (s != NULL)
2075 {
2076 p = tofree = concat_str(s, p);
2077 if (mustfree)
2078 vim_free(s);
2079 }
2080 }
2081 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002082 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002083 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002084 if (STRICMP(name, "HOME") == 0)
2085 init_homedir();
2086 else if (didset_vim && STRICMP(name, "VIM") == 0)
2087 didset_vim = FALSE;
2088 else if (didset_vimruntime
2089 && STRICMP(name, "VIMRUNTIME") == 0)
2090 didset_vimruntime = FALSE;
2091 arg_end = arg;
2092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 }
2096 }
2097 }
2098
2099 /*
2100 * ":let &option = expr": Set option value.
2101 * ":let &l:option = expr": Set local option value.
2102 * ":let &g:option = expr": Set global option value.
2103 */
2104 else if (*arg == '&')
2105 {
2106 /* Find the end of the name. */
2107 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002108 if (p == NULL || (endchars != NULL
2109 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 EMSG(_(e_letunexp));
2111 else
2112 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002113 long n;
2114 int opt_type;
2115 long numval;
2116 char_u *stringval = NULL;
2117 char_u *s;
2118
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 c1 = *p;
2120 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002121
2122 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002123 s = get_tv_string_chk(tv); /* != NULL if number or string */
2124 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002125 {
2126 opt_type = get_option_value(arg, &numval,
2127 &stringval, opt_flags);
2128 if ((opt_type == 1 && *op == '.')
2129 || (opt_type == 0 && *op != '.'))
2130 EMSG2(_(e_letwrong), op);
2131 else
2132 {
2133 if (opt_type == 1) /* number */
2134 {
2135 if (*op == '+')
2136 n = numval + n;
2137 else
2138 n = numval - n;
2139 }
2140 else if (opt_type == 0 && stringval != NULL) /* string */
2141 {
2142 s = concat_str(stringval, s);
2143 vim_free(stringval);
2144 stringval = s;
2145 }
2146 }
2147 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002148 if (s != NULL)
2149 {
2150 set_option_value(arg, n, s, opt_flags);
2151 arg_end = p;
2152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002154 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 }
2156 }
2157
2158 /*
2159 * ":let @r = expr": Set register contents.
2160 */
2161 else if (*arg == '@')
2162 {
2163 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 if (op != NULL && (*op == '+' || *op == '-'))
2165 EMSG2(_(e_letwrong), op);
2166 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002167 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 EMSG(_(e_letunexp));
2169 else
2170 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 char_u *tofree = NULL;
2172 char_u *s;
2173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002174 p = get_tv_string_chk(tv);
2175 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002176 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002177 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 if (s != NULL)
2179 {
2180 p = tofree = concat_str(s, p);
2181 vim_free(s);
2182 }
2183 }
2184 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002185 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002187 arg_end = arg + 1;
2188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 }
2191 }
2192
2193 /*
2194 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002197 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002199 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002201 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2205 EMSG(_(e_letunexp));
2206 else
2207 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002208 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 arg_end = p;
2210 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 }
2214
2215 else
2216 EMSG2(_(e_invarg2), arg);
2217
2218 return arg_end;
2219}
2220
2221/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002222 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2223 */
2224 static int
2225check_changedtick(arg)
2226 char_u *arg;
2227{
2228 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2229 {
2230 EMSG2(_(e_readonlyvar), arg);
2231 return TRUE;
2232 }
2233 return FALSE;
2234}
2235
2236/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 * Get an lval: variable, Dict item or List item that can be assigned a value
2238 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2239 * "name.key", "name.key[expr]" etc.
2240 * Indexing only works if "name" is an existing List or Dictionary.
2241 * "name" points to the start of the name.
2242 * If "rettv" is not NULL it points to the value to be assigned.
2243 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2244 * wrong; must end in space or cmd separator.
2245 *
2246 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 */
2250 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002251get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002253 typval_T *rettv;
2254 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 int unlet;
2256 int skip;
2257 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002258 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 char_u *expr_start, *expr_end;
2262 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002263 dictitem_T *v;
2264 typval_T var1;
2265 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002267 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002268 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002269 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002270 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002273 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274
2275 if (skip)
2276 {
2277 /* When skipping just find the end of the name. */
2278 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002279 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 }
2281
2282 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002283 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 if (expr_start != NULL)
2285 {
2286 /* Don't expand the name when we already know there is an error. */
2287 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2288 && *p != '[' && *p != '.')
2289 {
2290 EMSG(_(e_trailing));
2291 return NULL;
2292 }
2293
2294 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2295 if (lp->ll_exp_name == NULL)
2296 {
2297 /* Report an invalid expression in braces, unless the
2298 * expression evaluation has been cancelled due to an
2299 * aborting error, an interrupt, or an exception. */
2300 if (!aborting() && !quiet)
2301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002302 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 EMSG2(_(e_invarg2), name);
2304 return NULL;
2305 }
2306 }
2307 lp->ll_name = lp->ll_exp_name;
2308 }
2309 else
2310 lp->ll_name = name;
2311
2312 /* Without [idx] or .key we are done. */
2313 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2314 return p;
2315
2316 cc = *p;
2317 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 if (v == NULL && !quiet)
2320 EMSG2(_(e_undefvar), lp->ll_name);
2321 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 if (v == NULL)
2323 return NULL;
2324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 /*
2326 * Loop until no more [idx] or .key is following.
2327 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002328 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2332 && !(lp->ll_tv->v_type == VAR_DICT
2333 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002335 if (!quiet)
2336 EMSG(_("E689: Can only index a List or Dictionary"));
2337 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 if (!quiet)
2342 EMSG(_("E708: [:] must come last"));
2343 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002345
Bram Moolenaar8c711452005-01-14 21:53:12 +00002346 len = -1;
2347 if (*p == '.')
2348 {
2349 key = p + 1;
2350 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2351 ;
2352 if (len == 0)
2353 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 if (!quiet)
2355 EMSG(_(e_emptykey));
2356 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002357 }
2358 p = key + len;
2359 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002360 else
2361 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002363 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002364 if (*p == ':')
2365 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002366 else
2367 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002368 empty1 = FALSE;
2369 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 if (get_tv_string_chk(&var1) == NULL)
2372 {
2373 /* not a number or string */
2374 clear_tv(&var1);
2375 return NULL;
2376 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 }
2378
2379 /* Optionally get the second index [ :expr]. */
2380 if (*p == ':')
2381 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002386 if (!empty1)
2387 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002389 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 if (rettv != NULL && (rettv->v_type != VAR_LIST
2391 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 if (!quiet)
2394 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 if (!empty1)
2396 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 }
2399 p = skipwhite(p + 1);
2400 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 else
2403 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002405 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 if (!empty1)
2408 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002411 if (get_tv_string_chk(&var2) == NULL)
2412 {
2413 /* not a number or string */
2414 if (!empty1)
2415 clear_tv(&var1);
2416 clear_tv(&var2);
2417 return NULL;
2418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002424
Bram Moolenaar8c711452005-01-14 21:53:12 +00002425 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002426 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 if (!quiet)
2428 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 if (!empty1)
2430 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 }
2435
2436 /* Skip to past ']'. */
2437 ++p;
2438 }
2439
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 {
2442 if (len == -1)
2443 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 if (*key == NUL)
2447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (!quiet)
2449 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 }
2453 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 lp->ll_list = NULL;
2455 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002456 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002459 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 if (len == -1)
2465 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 }
2468 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002470 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 if (len == -1)
2473 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 p = NULL;
2476 break;
2477 }
2478 if (len == -1)
2479 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 }
2482 else
2483 {
2484 /*
2485 * Get the number and item for the only or first index of the List.
2486 */
2487 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 else
2490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002491 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 clear_tv(&var1);
2493 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 lp->ll_list = lp->ll_tv->vval.v_list;
2496 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2497 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 if (!quiet)
2500 EMSGN(_(e_listidx), lp->ll_n1);
2501 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
2505
2506 /*
2507 * May need to find the item or absolute index for the second
2508 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 * When no index given: "lp->ll_empty2" is TRUE.
2510 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 if (ni == NULL)
2520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (!quiet)
2522 EMSGN(_(e_listidx), lp->ll_n2);
2523 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 }
2527
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2529 if (lp->ll_n1 < 0)
2530 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2531 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002532 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 if (!quiet)
2534 EMSGN(_(e_listidx), lp->ll_n2);
2535 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002536 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002537 }
2538
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 }
2542
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 return p;
2544}
2545
2546/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 */
2549 static void
2550clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552{
2553 vim_free(lp->ll_exp_name);
2554 vim_free(lp->ll_newkey);
2555}
2556
2557/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002558 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002560 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 */
2562 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002563set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002566 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569{
2570 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 listitem_T *ri;
2572 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573
2574 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002575 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 cc = *endp;
2579 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002580 if (op != NULL && *op != '=')
2581 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002582 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002584 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002585 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2586 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002587 {
2588 if (tv_op(&tv, rettv, op) == OK)
2589 set_var(lp->ll_name, &tv, FALSE);
2590 clear_tv(&tv);
2591 }
2592 }
2593 else
2594 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002598 else if (tv_check_lock(lp->ll_newkey == NULL
2599 ? lp->ll_tv->v_lock
2600 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2601 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 else if (lp->ll_range)
2603 {
2604 /*
2605 * Assign the List values to the list items.
2606 */
2607 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002608 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002609 if (op != NULL && *op != '=')
2610 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2611 else
2612 {
2613 clear_tv(&lp->ll_li->li_tv);
2614 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2615 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 ri = ri->li_next;
2617 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2618 break;
2619 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002622 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 ri = NULL;
2625 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 lp->ll_li = lp->ll_li->li_next;
2629 ++lp->ll_n1;
2630 }
2631 if (ri != NULL)
2632 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002633 else if (lp->ll_empty2
2634 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 : lp->ll_n1 != lp->ll_n2)
2636 EMSG(_("E711: List value has not enough items"));
2637 }
2638 else
2639 {
2640 /*
2641 * Assign to a List or Dictionary item.
2642 */
2643 if (lp->ll_newkey != NULL)
2644 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002645 if (op != NULL && *op != '=')
2646 {
2647 EMSG2(_(e_letwrong), op);
2648 return;
2649 }
2650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002652 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (di == NULL)
2654 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002655 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2656 {
2657 vim_free(di);
2658 return;
2659 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002662 else if (op != NULL && *op != '=')
2663 {
2664 tv_op(lp->ll_tv, rettv, op);
2665 return;
2666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 /*
2671 * Assign the value to the variable or list item.
2672 */
2673 if (copy)
2674 copy_tv(rettv, lp->ll_tv);
2675 else
2676 {
2677 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002678 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002680 }
2681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682}
2683
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002684/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2686 * Returns OK or FAIL.
2687 */
2688 static int
2689tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002690 typval_T *tv1;
2691 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 char_u *op;
2693{
2694 long n;
2695 char_u numbuf[NUMBUFLEN];
2696 char_u *s;
2697
2698 /* Can't do anything with a Funcref or a Dict on the right. */
2699 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2700 {
2701 switch (tv1->v_type)
2702 {
2703 case VAR_DICT:
2704 case VAR_FUNC:
2705 break;
2706
2707 case VAR_LIST:
2708 if (*op != '+' || tv2->v_type != VAR_LIST)
2709 break;
2710 /* List += List */
2711 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2712 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2713 return OK;
2714
2715 case VAR_NUMBER:
2716 case VAR_STRING:
2717 if (tv2->v_type == VAR_LIST)
2718 break;
2719 if (*op == '+' || *op == '-')
2720 {
2721 /* nr += nr or nr -= nr*/
2722 n = get_tv_number(tv1);
2723 if (*op == '+')
2724 n += get_tv_number(tv2);
2725 else
2726 n -= get_tv_number(tv2);
2727 clear_tv(tv1);
2728 tv1->v_type = VAR_NUMBER;
2729 tv1->vval.v_number = n;
2730 }
2731 else
2732 {
2733 /* str .= str */
2734 s = get_tv_string(tv1);
2735 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2736 clear_tv(tv1);
2737 tv1->v_type = VAR_STRING;
2738 tv1->vval.v_string = s;
2739 }
2740 return OK;
2741 }
2742 }
2743
2744 EMSG2(_(e_letwrong), op);
2745 return FAIL;
2746}
2747
2748/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002749 * Add a watcher to a list.
2750 */
2751 static void
2752list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002753 list_T *l;
2754 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002755{
2756 lw->lw_next = l->lv_watch;
2757 l->lv_watch = lw;
2758}
2759
2760/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002761 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002762 * No warning when it isn't found...
2763 */
2764 static void
2765list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002766 list_T *l;
2767 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768{
Bram Moolenaar33570922005-01-25 22:26:29 +00002769 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002770
2771 lwp = &l->lv_watch;
2772 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2773 {
2774 if (lw == lwrem)
2775 {
2776 *lwp = lw->lw_next;
2777 break;
2778 }
2779 lwp = &lw->lw_next;
2780 }
2781}
2782
2783/*
2784 * Just before removing an item from a list: advance watchers to the next
2785 * item.
2786 */
2787 static void
2788list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002789 list_T *l;
2790 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002791{
Bram Moolenaar33570922005-01-25 22:26:29 +00002792 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002793
2794 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2795 if (lw->lw_item == item)
2796 lw->lw_item = item->li_next;
2797}
2798
2799/*
2800 * Evaluate the expression used in a ":for var in expr" command.
2801 * "arg" points to "var".
2802 * Set "*errp" to TRUE for an error, FALSE otherwise;
2803 * Return a pointer that holds the info. Null when there is an error.
2804 */
2805 void *
2806eval_for_line(arg, errp, nextcmdp, skip)
2807 char_u *arg;
2808 int *errp;
2809 char_u **nextcmdp;
2810 int skip;
2811{
Bram Moolenaar33570922005-01-25 22:26:29 +00002812 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002813 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002814 typval_T tv;
2815 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002816
2817 *errp = TRUE; /* default: there is an error */
2818
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002820 if (fi == NULL)
2821 return NULL;
2822
2823 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2824 if (expr == NULL)
2825 return fi;
2826
2827 expr = skipwhite(expr);
2828 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2829 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002830 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002831 return fi;
2832 }
2833
2834 if (skip)
2835 ++emsg_skip;
2836 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2837 {
2838 *errp = FALSE;
2839 if (!skip)
2840 {
2841 l = tv.vval.v_list;
2842 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002843 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002844 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002845 clear_tv(&tv);
2846 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847 else
2848 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002849 /* No need to increment the refcount, it's already set for the
2850 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851 fi->fi_list = l;
2852 list_add_watch(l, &fi->fi_lw);
2853 fi->fi_lw.lw_item = l->lv_first;
2854 }
2855 }
2856 }
2857 if (skip)
2858 --emsg_skip;
2859
2860 return fi;
2861}
2862
2863/*
2864 * Use the first item in a ":for" list. Advance to the next.
2865 * Assign the values to the variable (list). "arg" points to the first one.
2866 * Return TRUE when a valid item was found, FALSE when at end of list or
2867 * something wrong.
2868 */
2869 int
2870next_for_item(fi_void, arg)
2871 void *fi_void;
2872 char_u *arg;
2873{
Bram Moolenaar33570922005-01-25 22:26:29 +00002874 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002875 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002876 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002877
2878 item = fi->fi_lw.lw_item;
2879 if (item == NULL)
2880 result = FALSE;
2881 else
2882 {
2883 fi->fi_lw.lw_item = item->li_next;
2884 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2885 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2886 }
2887 return result;
2888}
2889
2890/*
2891 * Free the structure used to store info used by ":for".
2892 */
2893 void
2894free_for_info(fi_void)
2895 void *fi_void;
2896{
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002898
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002899 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002900 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002901 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002902 list_unref(fi->fi_list);
2903 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 vim_free(fi);
2905}
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2908
2909 void
2910set_context_for_expression(xp, arg, cmdidx)
2911 expand_T *xp;
2912 char_u *arg;
2913 cmdidx_T cmdidx;
2914{
2915 int got_eq = FALSE;
2916 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002917 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919 if (cmdidx == CMD_let)
2920 {
2921 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002922 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 {
2924 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002925 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926 {
2927 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002928 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002929 if (vim_iswhite(*p))
2930 break;
2931 }
2932 return;
2933 }
2934 }
2935 else
2936 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2937 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 while ((xp->xp_pattern = vim_strpbrk(arg,
2939 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2940 {
2941 c = *xp->xp_pattern;
2942 if (c == '&')
2943 {
2944 c = xp->xp_pattern[1];
2945 if (c == '&')
2946 {
2947 ++xp->xp_pattern;
2948 xp->xp_context = cmdidx != CMD_let || got_eq
2949 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2950 }
2951 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002954 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2955 xp->xp_pattern += 2;
2956
2957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959 else if (c == '$')
2960 {
2961 /* environment variable */
2962 xp->xp_context = EXPAND_ENV_VARS;
2963 }
2964 else if (c == '=')
2965 {
2966 got_eq = TRUE;
2967 xp->xp_context = EXPAND_EXPRESSION;
2968 }
2969 else if (c == '<'
2970 && xp->xp_context == EXPAND_FUNCTIONS
2971 && vim_strchr(xp->xp_pattern, '(') == NULL)
2972 {
2973 /* Function name can start with "<SNR>" */
2974 break;
2975 }
2976 else if (cmdidx != CMD_let || got_eq)
2977 {
2978 if (c == '"') /* string */
2979 {
2980 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2981 if (c == '\\' && xp->xp_pattern[1] != NUL)
2982 ++xp->xp_pattern;
2983 xp->xp_context = EXPAND_NOTHING;
2984 }
2985 else if (c == '\'') /* literal string */
2986 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002987 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2989 /* skip */ ;
2990 xp->xp_context = EXPAND_NOTHING;
2991 }
2992 else if (c == '|')
2993 {
2994 if (xp->xp_pattern[1] == '|')
2995 {
2996 ++xp->xp_pattern;
2997 xp->xp_context = EXPAND_EXPRESSION;
2998 }
2999 else
3000 xp->xp_context = EXPAND_COMMANDS;
3001 }
3002 else
3003 xp->xp_context = EXPAND_EXPRESSION;
3004 }
3005 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006 /* Doesn't look like something valid, expand as an expression
3007 * anyway. */
3008 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 arg = xp->xp_pattern;
3010 if (*arg != NUL)
3011 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3012 /* skip */ ;
3013 }
3014 xp->xp_pattern = arg;
3015}
3016
3017#endif /* FEAT_CMDL_COMPL */
3018
3019/*
3020 * ":1,25call func(arg1, arg2)" function call.
3021 */
3022 void
3023ex_call(eap)
3024 exarg_T *eap;
3025{
3026 char_u *arg = eap->arg;
3027 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003031 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 linenr_T lnum;
3033 int doesrange;
3034 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003035 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3038 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 if (tofree == NULL)
3040 return;
3041
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003042 /* Increase refcount on dictionary, it could get deleted when evaluating
3043 * the arguments. */
3044 if (fudi.fd_dict != NULL)
3045 ++fudi.fd_dict->dv_refcount;
3046
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003047 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3048 len = STRLEN(tofree);
3049 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
Bram Moolenaar532c7802005-01-27 14:44:31 +00003051 /* Skip white space to allow ":call func ()". Not good, but required for
3052 * backward compatibility. */
3053 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
3056 if (*startarg != '(')
3057 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003058 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 goto end;
3060 }
3061
3062 /*
3063 * When skipping, evaluate the function once, to find the end of the
3064 * arguments.
3065 * When the function takes a range, this is discovered after the first
3066 * call, and the loop is broken.
3067 */
3068 if (eap->skip)
3069 {
3070 ++emsg_skip;
3071 lnum = eap->line2; /* do it once, also with an invalid range */
3072 }
3073 else
3074 lnum = eap->line1;
3075 for ( ; lnum <= eap->line2; ++lnum)
3076 {
3077 if (!eap->skip && eap->addr_count > 0)
3078 {
3079 curwin->w_cursor.lnum = lnum;
3080 curwin->w_cursor.col = 0;
3081 }
3082 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003083 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003084 eap->line1, eap->line2, &doesrange,
3085 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 {
3087 failed = TRUE;
3088 break;
3089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003090 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (doesrange || eap->skip)
3092 break;
3093 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003094 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003096 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (aborting())
3098 break;
3099 }
3100 if (eap->skip)
3101 --emsg_skip;
3102
3103 if (!failed)
3104 {
3105 /* Check for trailing illegal characters and a following command. */
3106 if (!ends_excmd(*arg))
3107 {
3108 emsg_severe = TRUE;
3109 EMSG(_(e_trailing));
3110 }
3111 else
3112 eap->nextcmd = check_nextcmd(arg);
3113 }
3114
3115end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003116 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003117 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118}
3119
3120/*
3121 * ":unlet[!] var1 ... " command.
3122 */
3123 void
3124ex_unlet(eap)
3125 exarg_T *eap;
3126{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003127 ex_unletlock(eap, eap->arg, 0);
3128}
3129
3130/*
3131 * ":lockvar" and ":unlockvar" commands
3132 */
3133 void
3134ex_lockvar(eap)
3135 exarg_T *eap;
3136{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003138 int deep = 2;
3139
3140 if (eap->forceit)
3141 deep = -1;
3142 else if (vim_isdigit(*arg))
3143 {
3144 deep = getdigits(&arg);
3145 arg = skipwhite(arg);
3146 }
3147
3148 ex_unletlock(eap, arg, deep);
3149}
3150
3151/*
3152 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3153 */
3154 static void
3155ex_unletlock(eap, argstart, deep)
3156 exarg_T *eap;
3157 char_u *argstart;
3158 int deep;
3159{
3160 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003163 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165 do
3166 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003167 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003168 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3169 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003170 if (lv.ll_name == NULL)
3171 error = TRUE; /* error but continue parsing */
3172 if (name_end == NULL || (!vim_iswhite(*name_end)
3173 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003175 if (name_end != NULL)
3176 {
3177 emsg_severe = TRUE;
3178 EMSG(_(e_trailing));
3179 }
3180 if (!(eap->skip || error))
3181 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 break;
3183 }
3184
3185 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003186 {
3187 if (eap->cmdidx == CMD_unlet)
3188 {
3189 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3190 error = TRUE;
3191 }
3192 else
3193 {
3194 if (do_lock_var(&lv, name_end, deep,
3195 eap->cmdidx == CMD_lockvar) == FAIL)
3196 error = TRUE;
3197 }
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 if (!eap->skip)
3201 clear_lval(&lv);
3202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 arg = skipwhite(name_end);
3204 } while (!ends_excmd(*arg));
3205
3206 eap->nextcmd = check_nextcmd(arg);
3207}
3208
Bram Moolenaar8c711452005-01-14 21:53:12 +00003209 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003210do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003213 int forceit;
3214{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215 int ret = OK;
3216 int cc;
3217
3218 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003219 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 cc = *name_end;
3221 *name_end = NUL;
3222
3223 /* Normal name or expanded name. */
3224 if (check_changedtick(lp->ll_name))
3225 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003226 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003227 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003229 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003230 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3231 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 else if (lp->ll_range)
3233 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235
3236 /* Delete a range of List items. */
3237 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3238 {
3239 li = lp->ll_li->li_next;
3240 listitem_remove(lp->ll_list, lp->ll_li);
3241 lp->ll_li = li;
3242 ++lp->ll_n1;
3243 }
3244 }
3245 else
3246 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003247 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003248 /* unlet a List item. */
3249 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003250 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003251 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003252 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003253 }
3254
3255 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003256}
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258/*
3259 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003260 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 */
3262 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003263do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003265 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266{
Bram Moolenaar33570922005-01-25 22:26:29 +00003267 hashtab_T *ht;
3268 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003269 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
Bram Moolenaar33570922005-01-25 22:26:29 +00003271 ht = find_var_ht(name, &varname);
3272 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 hi = hash_find(ht, varname);
3275 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003276 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277 if (var_check_ro(HI2DI(hi)->di_flags, name))
3278 return FAIL;
3279 delete_var(ht, hi);
3280 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003283 if (forceit)
3284 return OK;
3285 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 return FAIL;
3287}
3288
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003289/*
3290 * Lock or unlock variable indicated by "lp".
3291 * "deep" is the levels to go (-1 for unlimited);
3292 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3293 */
3294 static int
3295do_lock_var(lp, name_end, deep, lock)
3296 lval_T *lp;
3297 char_u *name_end;
3298 int deep;
3299 int lock;
3300{
3301 int ret = OK;
3302 int cc;
3303 dictitem_T *di;
3304
3305 if (deep == 0) /* nothing to do */
3306 return OK;
3307
3308 if (lp->ll_tv == NULL)
3309 {
3310 cc = *name_end;
3311 *name_end = NUL;
3312
3313 /* Normal name or expanded name. */
3314 if (check_changedtick(lp->ll_name))
3315 ret = FAIL;
3316 else
3317 {
3318 di = find_var(lp->ll_name, NULL);
3319 if (di == NULL)
3320 ret = FAIL;
3321 else
3322 {
3323 if (lock)
3324 di->di_flags |= DI_FLAGS_LOCK;
3325 else
3326 di->di_flags &= ~DI_FLAGS_LOCK;
3327 item_lock(&di->di_tv, deep, lock);
3328 }
3329 }
3330 *name_end = cc;
3331 }
3332 else if (lp->ll_range)
3333 {
3334 listitem_T *li = lp->ll_li;
3335
3336 /* (un)lock a range of List items. */
3337 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3338 {
3339 item_lock(&li->li_tv, deep, lock);
3340 li = li->li_next;
3341 ++lp->ll_n1;
3342 }
3343 }
3344 else if (lp->ll_list != NULL)
3345 /* (un)lock a List item. */
3346 item_lock(&lp->ll_li->li_tv, deep, lock);
3347 else
3348 /* un(lock) a Dictionary item. */
3349 item_lock(&lp->ll_di->di_tv, deep, lock);
3350
3351 return ret;
3352}
3353
3354/*
3355 * Lock or unlock an item. "deep" is nr of levels to go.
3356 */
3357 static void
3358item_lock(tv, deep, lock)
3359 typval_T *tv;
3360 int deep;
3361 int lock;
3362{
3363 static int recurse = 0;
3364 list_T *l;
3365 listitem_T *li;
3366 dict_T *d;
3367 hashitem_T *hi;
3368 int todo;
3369
3370 if (recurse >= DICT_MAXNEST)
3371 {
3372 EMSG(_("E743: variable nested too deep for (un)lock"));
3373 return;
3374 }
3375 if (deep == 0)
3376 return;
3377 ++recurse;
3378
3379 /* lock/unlock the item itself */
3380 if (lock)
3381 tv->v_lock |= VAR_LOCKED;
3382 else
3383 tv->v_lock &= ~VAR_LOCKED;
3384
3385 switch (tv->v_type)
3386 {
3387 case VAR_LIST:
3388 if ((l = tv->vval.v_list) != NULL)
3389 {
3390 if (lock)
3391 l->lv_lock |= VAR_LOCKED;
3392 else
3393 l->lv_lock &= ~VAR_LOCKED;
3394 if (deep < 0 || deep > 1)
3395 /* recursive: lock/unlock the items the List contains */
3396 for (li = l->lv_first; li != NULL; li = li->li_next)
3397 item_lock(&li->li_tv, deep - 1, lock);
3398 }
3399 break;
3400 case VAR_DICT:
3401 if ((d = tv->vval.v_dict) != NULL)
3402 {
3403 if (lock)
3404 d->dv_lock |= VAR_LOCKED;
3405 else
3406 d->dv_lock &= ~VAR_LOCKED;
3407 if (deep < 0 || deep > 1)
3408 {
3409 /* recursive: lock/unlock the items the List contains */
3410 todo = d->dv_hashtab.ht_used;
3411 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3412 {
3413 if (!HASHITEM_EMPTY(hi))
3414 {
3415 --todo;
3416 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3417 }
3418 }
3419 }
3420 }
3421 }
3422 --recurse;
3423}
3424
Bram Moolenaara40058a2005-07-11 22:42:07 +00003425/*
3426 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3427 * it refers to a List or Dictionary that is locked.
3428 */
3429 static int
3430tv_islocked(tv)
3431 typval_T *tv;
3432{
3433 return (tv->v_lock & VAR_LOCKED)
3434 || (tv->v_type == VAR_LIST
3435 && tv->vval.v_list != NULL
3436 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3437 || (tv->v_type == VAR_DICT
3438 && tv->vval.v_dict != NULL
3439 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3440}
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3443/*
3444 * Delete all "menutrans_" variables.
3445 */
3446 void
3447del_menutrans_vars()
3448{
Bram Moolenaar33570922005-01-25 22:26:29 +00003449 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003450 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 hash_lock(&globvarht);
3453 todo = globvarht.ht_used;
3454 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003455 {
3456 if (!HASHITEM_EMPTY(hi))
3457 {
3458 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3460 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003461 }
3462 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464}
3465#endif
3466
3467#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3468
3469/*
3470 * Local string buffer for the next two functions to store a variable name
3471 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3472 * get_user_var_name().
3473 */
3474
3475static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3476
3477static char_u *varnamebuf = NULL;
3478static int varnamebuflen = 0;
3479
3480/*
3481 * Function to concatenate a prefix and a variable name.
3482 */
3483 static char_u *
3484cat_prefix_varname(prefix, name)
3485 int prefix;
3486 char_u *name;
3487{
3488 int len;
3489
3490 len = (int)STRLEN(name) + 3;
3491 if (len > varnamebuflen)
3492 {
3493 vim_free(varnamebuf);
3494 len += 10; /* some additional space */
3495 varnamebuf = alloc(len);
3496 if (varnamebuf == NULL)
3497 {
3498 varnamebuflen = 0;
3499 return NULL;
3500 }
3501 varnamebuflen = len;
3502 }
3503 *varnamebuf = prefix;
3504 varnamebuf[1] = ':';
3505 STRCPY(varnamebuf + 2, name);
3506 return varnamebuf;
3507}
3508
3509/*
3510 * Function given to ExpandGeneric() to obtain the list of user defined
3511 * (global/buffer/window/built-in) variable names.
3512 */
3513/*ARGSUSED*/
3514 char_u *
3515get_user_var_name(xp, idx)
3516 expand_T *xp;
3517 int idx;
3518{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003519 static long_u gdone;
3520 static long_u bdone;
3521 static long_u wdone;
3522 static int vidx;
3523 static hashitem_T *hi;
3524 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003527 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003528
3529 /* Global variables */
3530 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003532 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003533 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003534 else
3535 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003536 while (HASHITEM_EMPTY(hi))
3537 ++hi;
3538 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3539 return cat_prefix_varname('g', hi->hi_key);
3540 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003542
3543 /* b: variables */
3544 ht = &curbuf->b_vars.dv_hashtab;
3545 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003547 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003548 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003549 else
3550 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003551 while (HASHITEM_EMPTY(hi))
3552 ++hi;
3553 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003555 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003557 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 return (char_u *)"b:changedtick";
3559 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003560
3561 /* w: variables */
3562 ht = &curwin->w_vars.dv_hashtab;
3563 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003565 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003566 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003567 else
3568 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003569 while (HASHITEM_EMPTY(hi))
3570 ++hi;
3571 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003573
3574 /* v: variables */
3575 if (vidx < VV_LEN)
3576 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
3578 vim_free(varnamebuf);
3579 varnamebuf = NULL;
3580 varnamebuflen = 0;
3581 return NULL;
3582}
3583
3584#endif /* FEAT_CMDL_COMPL */
3585
3586/*
3587 * types for expressions.
3588 */
3589typedef enum
3590{
3591 TYPE_UNKNOWN = 0
3592 , TYPE_EQUAL /* == */
3593 , TYPE_NEQUAL /* != */
3594 , TYPE_GREATER /* > */
3595 , TYPE_GEQUAL /* >= */
3596 , TYPE_SMALLER /* < */
3597 , TYPE_SEQUAL /* <= */
3598 , TYPE_MATCH /* =~ */
3599 , TYPE_NOMATCH /* !~ */
3600} exptype_T;
3601
3602/*
3603 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003604 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3606 */
3607
3608/*
3609 * Handle zero level expression.
3610 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003611 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003612 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 * Return OK or FAIL.
3614 */
3615 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 char_u **nextcmd;
3620 int evaluate;
3621{
3622 int ret;
3623 char_u *p;
3624
3625 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003626 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 if (ret == FAIL || !ends_excmd(*p))
3628 {
3629 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003630 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 /*
3632 * Report the invalid expression unless the expression evaluation has
3633 * been cancelled due to an aborting error, an interrupt, or an
3634 * exception.
3635 */
3636 if (!aborting())
3637 EMSG2(_(e_invexpr2), arg);
3638 ret = FAIL;
3639 }
3640 if (nextcmd != NULL)
3641 *nextcmd = check_nextcmd(p);
3642
3643 return ret;
3644}
3645
3646/*
3647 * Handle top level expression:
3648 * expr1 ? expr0 : expr0
3649 *
3650 * "arg" must point to the first non-white of the expression.
3651 * "arg" is advanced to the next non-white after the recognized expression.
3652 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003653 * Note: "rettv.v_lock" is not set.
3654 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 * Return OK or FAIL.
3656 */
3657 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003658eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 int evaluate;
3662{
3663 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 /*
3667 * Get the first variable.
3668 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 return FAIL;
3671
3672 if ((*arg)[0] == '?')
3673 {
3674 result = FALSE;
3675 if (evaluate)
3676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003677 int error = FALSE;
3678
3679 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003681 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682 if (error)
3683 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685
3686 /*
3687 * Get the second variable.
3688 */
3689 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003690 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 return FAIL;
3692
3693 /*
3694 * Check for the ":".
3695 */
3696 if ((*arg)[0] != ':')
3697 {
3698 EMSG(_("E109: Missing ':' after '?'"));
3699 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003700 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 return FAIL;
3702 }
3703
3704 /*
3705 * Get the third variable.
3706 */
3707 *arg = skipwhite(*arg + 1);
3708 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3709 {
3710 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 return FAIL;
3713 }
3714 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003715 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717
3718 return OK;
3719}
3720
3721/*
3722 * Handle first level expression:
3723 * expr2 || expr2 || expr2 logical OR
3724 *
3725 * "arg" must point to the first non-white of the expression.
3726 * "arg" is advanced to the next non-white after the recognized expression.
3727 *
3728 * Return OK or FAIL.
3729 */
3730 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003731eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 int evaluate;
3735{
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 long result;
3738 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003739 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
3741 /*
3742 * Get the first variable.
3743 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003744 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 return FAIL;
3746
3747 /*
3748 * Repeat until there is no following "||".
3749 */
3750 first = TRUE;
3751 result = FALSE;
3752 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3753 {
3754 if (evaluate && first)
3755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003758 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003759 if (error)
3760 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 first = FALSE;
3762 }
3763
3764 /*
3765 * Get the second variable.
3766 */
3767 *arg = skipwhite(*arg + 2);
3768 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3769 return FAIL;
3770
3771 /*
3772 * Compute the result.
3773 */
3774 if (evaluate && !result)
3775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003776 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003778 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 if (error)
3780 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
3782 if (evaluate)
3783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784 rettv->v_type = VAR_NUMBER;
3785 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787 }
3788
3789 return OK;
3790}
3791
3792/*
3793 * Handle second level expression:
3794 * expr3 && expr3 && expr3 logical AND
3795 *
3796 * "arg" must point to the first non-white of the expression.
3797 * "arg" is advanced to the next non-white after the recognized expression.
3798 *
3799 * Return OK or FAIL.
3800 */
3801 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003802eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 int evaluate;
3806{
Bram Moolenaar33570922005-01-25 22:26:29 +00003807 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 long result;
3809 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003810 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
3812 /*
3813 * Get the first variable.
3814 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003815 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 return FAIL;
3817
3818 /*
3819 * Repeat until there is no following "&&".
3820 */
3821 first = TRUE;
3822 result = TRUE;
3823 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3824 {
3825 if (evaluate && first)
3826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003827 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003829 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003830 if (error)
3831 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 first = FALSE;
3833 }
3834
3835 /*
3836 * Get the second variable.
3837 */
3838 *arg = skipwhite(*arg + 2);
3839 if (eval4(arg, &var2, evaluate && result) == FAIL)
3840 return FAIL;
3841
3842 /*
3843 * Compute the result.
3844 */
3845 if (evaluate && result)
3846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003847 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003849 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003850 if (error)
3851 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
3853 if (evaluate)
3854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855 rettv->v_type = VAR_NUMBER;
3856 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858 }
3859
3860 return OK;
3861}
3862
3863/*
3864 * Handle third level expression:
3865 * var1 == var2
3866 * var1 =~ var2
3867 * var1 != var2
3868 * var1 !~ var2
3869 * var1 > var2
3870 * var1 >= var2
3871 * var1 < var2
3872 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003873 * var1 is var2
3874 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 *
3876 * "arg" must point to the first non-white of the expression.
3877 * "arg" is advanced to the next non-white after the recognized expression.
3878 *
3879 * Return OK or FAIL.
3880 */
3881 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 int evaluate;
3886{
Bram Moolenaar33570922005-01-25 22:26:29 +00003887 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 char_u *p;
3889 int i;
3890 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003891 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 int len = 2;
3893 long n1, n2;
3894 char_u *s1, *s2;
3895 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3896 regmatch_T regmatch;
3897 int ic;
3898 char_u *save_cpo;
3899
3900 /*
3901 * Get the first variable.
3902 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003903 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 return FAIL;
3905
3906 p = *arg;
3907 switch (p[0])
3908 {
3909 case '=': if (p[1] == '=')
3910 type = TYPE_EQUAL;
3911 else if (p[1] == '~')
3912 type = TYPE_MATCH;
3913 break;
3914 case '!': if (p[1] == '=')
3915 type = TYPE_NEQUAL;
3916 else if (p[1] == '~')
3917 type = TYPE_NOMATCH;
3918 break;
3919 case '>': if (p[1] != '=')
3920 {
3921 type = TYPE_GREATER;
3922 len = 1;
3923 }
3924 else
3925 type = TYPE_GEQUAL;
3926 break;
3927 case '<': if (p[1] != '=')
3928 {
3929 type = TYPE_SMALLER;
3930 len = 1;
3931 }
3932 else
3933 type = TYPE_SEQUAL;
3934 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003935 case 'i': if (p[1] == 's')
3936 {
3937 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3938 len = 5;
3939 if (!vim_isIDc(p[len]))
3940 {
3941 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3942 type_is = TRUE;
3943 }
3944 }
3945 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947
3948 /*
3949 * If there is a comparitive operator, use it.
3950 */
3951 if (type != TYPE_UNKNOWN)
3952 {
3953 /* extra question mark appended: ignore case */
3954 if (p[len] == '?')
3955 {
3956 ic = TRUE;
3957 ++len;
3958 }
3959 /* extra '#' appended: match case */
3960 else if (p[len] == '#')
3961 {
3962 ic = FALSE;
3963 ++len;
3964 }
3965 /* nothing appened: use 'ignorecase' */
3966 else
3967 ic = p_ic;
3968
3969 /*
3970 * Get the second variable.
3971 */
3972 *arg = skipwhite(p + len);
3973 if (eval5(arg, &var2, evaluate) == FAIL)
3974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003975 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 return FAIL;
3977 }
3978
3979 if (evaluate)
3980 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003981 if (type_is && rettv->v_type != var2.v_type)
3982 {
3983 /* For "is" a different type always means FALSE, for "notis"
3984 * it means TRUE. */
3985 n1 = (type == TYPE_NEQUAL);
3986 }
3987 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3988 {
3989 if (type_is)
3990 {
3991 n1 = (rettv->v_type == var2.v_type
3992 && rettv->vval.v_list == var2.vval.v_list);
3993 if (type == TYPE_NEQUAL)
3994 n1 = !n1;
3995 }
3996 else if (rettv->v_type != var2.v_type
3997 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3998 {
3999 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004000 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004001 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004002 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004003 clear_tv(rettv);
4004 clear_tv(&var2);
4005 return FAIL;
4006 }
4007 else
4008 {
4009 /* Compare two Lists for being equal or unequal. */
4010 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4011 if (type == TYPE_NEQUAL)
4012 n1 = !n1;
4013 }
4014 }
4015
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004016 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4017 {
4018 if (type_is)
4019 {
4020 n1 = (rettv->v_type == var2.v_type
4021 && rettv->vval.v_dict == var2.vval.v_dict);
4022 if (type == TYPE_NEQUAL)
4023 n1 = !n1;
4024 }
4025 else if (rettv->v_type != var2.v_type
4026 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4027 {
4028 if (rettv->v_type != var2.v_type)
4029 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4030 else
4031 EMSG(_("E736: Invalid operation for Dictionary"));
4032 clear_tv(rettv);
4033 clear_tv(&var2);
4034 return FAIL;
4035 }
4036 else
4037 {
4038 /* Compare two Dictionaries for being equal or unequal. */
4039 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4040 if (type == TYPE_NEQUAL)
4041 n1 = !n1;
4042 }
4043 }
4044
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004045 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4046 {
4047 if (rettv->v_type != var2.v_type
4048 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4049 {
4050 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004051 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004052 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004053 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004054 clear_tv(rettv);
4055 clear_tv(&var2);
4056 return FAIL;
4057 }
4058 else
4059 {
4060 /* Compare two Funcrefs for being equal or unequal. */
4061 if (rettv->vval.v_string == NULL
4062 || var2.vval.v_string == NULL)
4063 n1 = FALSE;
4064 else
4065 n1 = STRCMP(rettv->vval.v_string,
4066 var2.vval.v_string) == 0;
4067 if (type == TYPE_NEQUAL)
4068 n1 = !n1;
4069 }
4070 }
4071
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 /*
4073 * If one of the two variables is a number, compare as a number.
4074 * When using "=~" or "!~", always compare as string.
4075 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004076 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004079 n1 = get_tv_number(rettv);
4080 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 switch (type)
4082 {
4083 case TYPE_EQUAL: n1 = (n1 == n2); break;
4084 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4085 case TYPE_GREATER: n1 = (n1 > n2); break;
4086 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4087 case TYPE_SMALLER: n1 = (n1 < n2); break;
4088 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4089 case TYPE_UNKNOWN:
4090 case TYPE_MATCH:
4091 case TYPE_NOMATCH: break; /* avoid gcc warning */
4092 }
4093 }
4094 else
4095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 s1 = get_tv_string_buf(rettv, buf1);
4097 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4099 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4100 else
4101 i = 0;
4102 n1 = FALSE;
4103 switch (type)
4104 {
4105 case TYPE_EQUAL: n1 = (i == 0); break;
4106 case TYPE_NEQUAL: n1 = (i != 0); break;
4107 case TYPE_GREATER: n1 = (i > 0); break;
4108 case TYPE_GEQUAL: n1 = (i >= 0); break;
4109 case TYPE_SMALLER: n1 = (i < 0); break;
4110 case TYPE_SEQUAL: n1 = (i <= 0); break;
4111
4112 case TYPE_MATCH:
4113 case TYPE_NOMATCH:
4114 /* avoid 'l' flag in 'cpoptions' */
4115 save_cpo = p_cpo;
4116 p_cpo = (char_u *)"";
4117 regmatch.regprog = vim_regcomp(s2,
4118 RE_MAGIC + RE_STRING);
4119 regmatch.rm_ic = ic;
4120 if (regmatch.regprog != NULL)
4121 {
4122 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4123 vim_free(regmatch.regprog);
4124 if (type == TYPE_NOMATCH)
4125 n1 = !n1;
4126 }
4127 p_cpo = save_cpo;
4128 break;
4129
4130 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4131 }
4132 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
4134 clear_tv(&var2);
4135 rettv->v_type = VAR_NUMBER;
4136 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138 }
4139
4140 return OK;
4141}
4142
4143/*
4144 * Handle fourth level expression:
4145 * + number addition
4146 * - number subtraction
4147 * . string concatenation
4148 *
4149 * "arg" must point to the first non-white of the expression.
4150 * "arg" is advanced to the next non-white after the recognized expression.
4151 *
4152 * Return OK or FAIL.
4153 */
4154 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 int evaluate;
4159{
Bram Moolenaar33570922005-01-25 22:26:29 +00004160 typval_T var2;
4161 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int op;
4163 long n1, n2;
4164 char_u *s1, *s2;
4165 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4166 char_u *p;
4167
4168 /*
4169 * Get the first variable.
4170 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 return FAIL;
4173
4174 /*
4175 * Repeat computing, until no '+', '-' or '.' is following.
4176 */
4177 for (;;)
4178 {
4179 op = **arg;
4180 if (op != '+' && op != '-' && op != '.')
4181 break;
4182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 if (op != '+' || rettv->v_type != VAR_LIST)
4184 {
4185 /* For "list + ...", an illegal use of the first operand as
4186 * a number cannot be determined before evaluating the 2nd
4187 * operand: if this is also a list, all is ok.
4188 * For "something . ...", "something - ..." or "non-list + ...",
4189 * we know that the first operand needs to be a string or number
4190 * without evaluating the 2nd operand. So check before to avoid
4191 * side effects after an error. */
4192 if (evaluate && get_tv_string_chk(rettv) == NULL)
4193 {
4194 clear_tv(rettv);
4195 return FAIL;
4196 }
4197 }
4198
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 /*
4200 * Get the second variable.
4201 */
4202 *arg = skipwhite(*arg + 1);
4203 if (eval6(arg, &var2, evaluate) == FAIL)
4204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return FAIL;
4207 }
4208
4209 if (evaluate)
4210 {
4211 /*
4212 * Compute the result.
4213 */
4214 if (op == '.')
4215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4217 s2 = get_tv_string_buf_chk(&var2, buf2);
4218 if (s2 == NULL) /* type error ? */
4219 {
4220 clear_tv(rettv);
4221 clear_tv(&var2);
4222 return FAIL;
4223 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004224 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 clear_tv(rettv);
4226 rettv->v_type = VAR_STRING;
4227 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004229 else if (op == '+' && rettv->v_type == VAR_LIST
4230 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004231 {
4232 /* concatenate Lists */
4233 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4234 &var3) == FAIL)
4235 {
4236 clear_tv(rettv);
4237 clear_tv(&var2);
4238 return FAIL;
4239 }
4240 clear_tv(rettv);
4241 *rettv = var3;
4242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 else
4244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 int error = FALSE;
4246
4247 n1 = get_tv_number_chk(rettv, &error);
4248 if (error)
4249 {
4250 /* This can only happen for "list + non-list".
4251 * For "non-list + ..." or "something - ...", we returned
4252 * before evaluating the 2nd operand. */
4253 clear_tv(rettv);
4254 return FAIL;
4255 }
4256 n2 = get_tv_number_chk(&var2, &error);
4257 if (error)
4258 {
4259 clear_tv(rettv);
4260 clear_tv(&var2);
4261 return FAIL;
4262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (op == '+')
4265 n1 = n1 + n2;
4266 else
4267 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 rettv->v_type = VAR_NUMBER;
4269 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273 }
4274 return OK;
4275}
4276
4277/*
4278 * Handle fifth level expression:
4279 * * number multiplication
4280 * / number division
4281 * % number modulo
4282 *
4283 * "arg" must point to the first non-white of the expression.
4284 * "arg" is advanced to the next non-white after the recognized expression.
4285 *
4286 * Return OK or FAIL.
4287 */
4288 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 int evaluate;
4293{
Bram Moolenaar33570922005-01-25 22:26:29 +00004294 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 int op;
4296 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 /*
4300 * Get the first variable.
4301 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 return FAIL;
4304
4305 /*
4306 * Repeat computing, until no '*', '/' or '%' is following.
4307 */
4308 for (;;)
4309 {
4310 op = **arg;
4311 if (op != '*' && op != '/' && op != '%')
4312 break;
4313
4314 if (evaluate)
4315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004316 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004317 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 if (error)
4319 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321 else
4322 n1 = 0;
4323
4324 /*
4325 * Get the second variable.
4326 */
4327 *arg = skipwhite(*arg + 1);
4328 if (eval7(arg, &var2, evaluate) == FAIL)
4329 return FAIL;
4330
4331 if (evaluate)
4332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004334 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (error)
4336 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 /*
4339 * Compute the result.
4340 */
4341 if (op == '*')
4342 n1 = n1 * n2;
4343 else if (op == '/')
4344 {
4345 if (n2 == 0) /* give an error message? */
4346 n1 = 0x7fffffffL;
4347 else
4348 n1 = n1 / n2;
4349 }
4350 else
4351 {
4352 if (n2 == 0) /* give an error message? */
4353 n1 = 0;
4354 else
4355 n1 = n1 % n2;
4356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 rettv->v_type = VAR_NUMBER;
4358 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 }
4361
4362 return OK;
4363}
4364
4365/*
4366 * Handle sixth level expression:
4367 * number number constant
4368 * "string" string contstant
4369 * 'string' literal string contstant
4370 * &option-name option value
4371 * @r register contents
4372 * identifier variable value
4373 * function() function call
4374 * $VAR environment variable
4375 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004376 * [expr, expr] List
4377 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 *
4379 * Also handle:
4380 * ! in front logical NOT
4381 * - in front unary minus
4382 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004383 * trailing [] subscript in String or List
4384 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 *
4386 * "arg" must point to the first non-white of the expression.
4387 * "arg" is advanced to the next non-white after the recognized expression.
4388 *
4389 * Return OK or FAIL.
4390 */
4391 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 int evaluate;
4396{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 long n;
4398 int len;
4399 char_u *s;
4400 int val;
4401 char_u *start_leader, *end_leader;
4402 int ret = OK;
4403 char_u *alias;
4404
4405 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004407 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
4411 /*
4412 * Skip '!' and '-' characters. They are handled later.
4413 */
4414 start_leader = *arg;
4415 while (**arg == '!' || **arg == '-' || **arg == '+')
4416 *arg = skipwhite(*arg + 1);
4417 end_leader = *arg;
4418
4419 switch (**arg)
4420 {
4421 /*
4422 * Number constant.
4423 */
4424 case '0':
4425 case '1':
4426 case '2':
4427 case '3':
4428 case '4':
4429 case '5':
4430 case '6':
4431 case '7':
4432 case '8':
4433 case '9':
4434 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4435 *arg += len;
4436 if (evaluate)
4437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438 rettv->v_type = VAR_NUMBER;
4439 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 break;
4442
4443 /*
4444 * String constant: "string".
4445 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 break;
4448
4449 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004452 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004453 break;
4454
4455 /*
4456 * List: [expr, expr]
4457 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 break;
4460
4461 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 * Dictionary: {key: val, key: val}
4463 */
4464 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4465 break;
4466
4467 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004468 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004470 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 break;
4472
4473 /*
4474 * Environment variable: $VAR.
4475 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 break;
4478
4479 /*
4480 * Register contents: @r.
4481 */
4482 case '@': ++*arg;
4483 if (evaluate)
4484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004485 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004486 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 if (**arg != NUL)
4489 ++*arg;
4490 break;
4491
4492 /*
4493 * nested expression: (expression).
4494 */
4495 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (**arg == ')')
4498 ++*arg;
4499 else if (ret == OK)
4500 {
4501 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004502 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 ret = FAIL;
4504 }
4505 break;
4506
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 break;
4509 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510
4511 if (ret == NOTDONE)
4512 {
4513 /*
4514 * Must be a variable or function name.
4515 * Can also be a curly-braces kind of name: {expr}.
4516 */
4517 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004518 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004519 if (alias != NULL)
4520 s = alias;
4521
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004522 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004523 ret = FAIL;
4524 else
4525 {
4526 if (**arg == '(') /* recursive! */
4527 {
4528 /* If "s" is the name of a variable of type VAR_FUNC
4529 * use its contents. */
4530 s = deref_func_name(s, &len);
4531
4532 /* Invoke the function. */
4533 ret = get_func_tv(s, len, rettv, arg,
4534 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004535 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 /* Stop the expression evaluation when immediately
4537 * aborting on error, or when an interrupt occurred or
4538 * an exception was thrown but not caught. */
4539 if (aborting())
4540 {
4541 if (ret == OK)
4542 clear_tv(rettv);
4543 ret = FAIL;
4544 }
4545 }
4546 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004547 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004548 else
4549 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 }
4551
4552 if (alias != NULL)
4553 vim_free(alias);
4554 }
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 *arg = skipwhite(*arg);
4557
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004558 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4559 * expr(expr). */
4560 if (ret == OK)
4561 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 /*
4564 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4565 */
4566 if (ret == OK && evaluate && end_leader > start_leader)
4567 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004568 int error = FALSE;
4569
4570 val = get_tv_number_chk(rettv, &error);
4571 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004573 clear_tv(rettv);
4574 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004576 else
4577 {
4578 while (end_leader > start_leader)
4579 {
4580 --end_leader;
4581 if (*end_leader == '!')
4582 val = !val;
4583 else if (*end_leader == '-')
4584 val = -val;
4585 }
4586 clear_tv(rettv);
4587 rettv->v_type = VAR_NUMBER;
4588 rettv->vval.v_number = val;
4589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591
4592 return ret;
4593}
4594
4595/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 * Evaluate an "[expr]" or "[expr:expr]" index.
4597 * "*arg" points to the '['.
4598 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4599 */
4600 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004601eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004603 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004605 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004606{
4607 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004608 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 long len = -1;
4611 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004617 if (verbose)
4618 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 return FAIL;
4620 }
4621
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 /*
4625 * dict.name
4626 */
4627 key = *arg + 1;
4628 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4629 ;
4630 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004631 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004632 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 }
4634 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636 /*
4637 * something[idx]
4638 *
4639 * Get the (first) variable from inside the [].
4640 */
4641 *arg = skipwhite(*arg + 1);
4642 if (**arg == ':')
4643 empty1 = TRUE;
4644 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4645 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004646 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4647 {
4648 /* not a number or string */
4649 clear_tv(&var1);
4650 return FAIL;
4651 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004652
4653 /*
4654 * Get the second variable from inside the [:].
4655 */
4656 if (**arg == ':')
4657 {
4658 range = TRUE;
4659 *arg = skipwhite(*arg + 1);
4660 if (**arg == ']')
4661 empty2 = TRUE;
4662 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004664 if (!empty1)
4665 clear_tv(&var1);
4666 return FAIL;
4667 }
4668 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4669 {
4670 /* not a number or string */
4671 if (!empty1)
4672 clear_tv(&var1);
4673 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674 return FAIL;
4675 }
4676 }
4677
4678 /* Check for the ']'. */
4679 if (**arg != ']')
4680 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004681 if (verbose)
4682 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 clear_tv(&var1);
4684 if (range)
4685 clear_tv(&var2);
4686 return FAIL;
4687 }
4688 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 }
4690
4691 if (evaluate)
4692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 n1 = 0;
4694 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 n1 = get_tv_number(&var1);
4697 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004698 }
4699 if (range)
4700 {
4701 if (empty2)
4702 n2 = -1;
4703 else
4704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004705 n2 = get_tv_number(&var2);
4706 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004707 }
4708 }
4709
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004710 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 {
4712 case VAR_NUMBER:
4713 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004714 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 len = (long)STRLEN(s);
4716 if (range)
4717 {
4718 /* The resulting variable is a substring. If the indexes
4719 * are out of range the result is empty. */
4720 if (n1 < 0)
4721 {
4722 n1 = len + n1;
4723 if (n1 < 0)
4724 n1 = 0;
4725 }
4726 if (n2 < 0)
4727 n2 = len + n2;
4728 else if (n2 >= len)
4729 n2 = len;
4730 if (n1 >= len || n2 < 0 || n1 > n2)
4731 s = NULL;
4732 else
4733 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4734 }
4735 else
4736 {
4737 /* The resulting variable is a string of a single
4738 * character. If the index is too big or negative the
4739 * result is empty. */
4740 if (n1 >= len || n1 < 0)
4741 s = NULL;
4742 else
4743 s = vim_strnsave(s + n1, 1);
4744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
4746 rettv->v_type = VAR_STRING;
4747 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 break;
4749
4750 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004751 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752 if (n1 < 0)
4753 n1 = len + n1;
4754 if (!empty1 && (n1 < 0 || n1 >= len))
4755 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004756 if (verbose)
4757 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004758 return FAIL;
4759 }
4760 if (range)
4761 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004762 list_T *l;
4763 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764
4765 if (n2 < 0)
4766 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004767 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004769 if (verbose)
4770 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 return FAIL;
4772 }
4773 l = list_alloc();
4774 if (l == NULL)
4775 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004776 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777 n1 <= n2; ++n1)
4778 {
4779 if (list_append_tv(l, &item->li_tv) == FAIL)
4780 {
4781 list_free(l);
4782 return FAIL;
4783 }
4784 item = item->li_next;
4785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004786 clear_tv(rettv);
4787 rettv->v_type = VAR_LIST;
4788 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004789 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 }
4791 else
4792 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004794 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004795 clear_tv(rettv);
4796 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 }
4798 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799
4800 case VAR_DICT:
4801 if (range)
4802 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004803 if (verbose)
4804 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 if (len == -1)
4806 clear_tv(&var1);
4807 return FAIL;
4808 }
4809 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004811
4812 if (len == -1)
4813 {
4814 key = get_tv_string(&var1);
4815 if (*key == NUL)
4816 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004817 if (verbose)
4818 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004819 clear_tv(&var1);
4820 return FAIL;
4821 }
4822 }
4823
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004824 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004825
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004826 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004827 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828 if (len == -1)
4829 clear_tv(&var1);
4830 if (item == NULL)
4831 return FAIL;
4832
4833 copy_tv(&item->di_tv, &var1);
4834 clear_tv(rettv);
4835 *rettv = var1;
4836 }
4837 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 }
4839 }
4840
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004841 return OK;
4842}
4843
4844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 * Get an option value.
4846 * "arg" points to the '&' or '+' before the option name.
4847 * "arg" is advanced to character after the option name.
4848 * Return OK or FAIL.
4849 */
4850 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004853 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 int evaluate;
4855{
4856 char_u *option_end;
4857 long numval;
4858 char_u *stringval;
4859 int opt_type;
4860 int c;
4861 int working = (**arg == '+'); /* has("+option") */
4862 int ret = OK;
4863 int opt_flags;
4864
4865 /*
4866 * Isolate the option name and find its value.
4867 */
4868 option_end = find_option_end(arg, &opt_flags);
4869 if (option_end == NULL)
4870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 EMSG2(_("E112: Option name missing: %s"), *arg);
4873 return FAIL;
4874 }
4875
4876 if (!evaluate)
4877 {
4878 *arg = option_end;
4879 return OK;
4880 }
4881
4882 c = *option_end;
4883 *option_end = NUL;
4884 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 if (opt_type == -3) /* invalid name */
4888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004889 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 EMSG2(_("E113: Unknown option: %s"), *arg);
4891 ret = FAIL;
4892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
4895 if (opt_type == -2) /* hidden string option */
4896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004897 rettv->v_type = VAR_STRING;
4898 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 else if (opt_type == -1) /* hidden number option */
4901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 rettv->v_type = VAR_NUMBER;
4903 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905 else if (opt_type == 1) /* number option */
4906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004907 rettv->v_type = VAR_NUMBER;
4908 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
4910 else /* string option */
4911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 rettv->v_type = VAR_STRING;
4913 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 }
4916 else if (working && (opt_type == -2 || opt_type == -1))
4917 ret = FAIL;
4918
4919 *option_end = c; /* put back for error messages */
4920 *arg = option_end;
4921
4922 return ret;
4923}
4924
4925/*
4926 * Allocate a variable for a string constant.
4927 * Return OK or FAIL.
4928 */
4929 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 int evaluate;
4934{
4935 char_u *p;
4936 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 int extra = 0;
4938
4939 /*
4940 * Find the end of the string, skipping backslashed characters.
4941 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004942 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
4944 if (*p == '\\' && p[1] != NUL)
4945 {
4946 ++p;
4947 /* A "\<x>" form occupies at least 4 characters, and produces up
4948 * to 6 characters: reserve space for 2 extra */
4949 if (*p == '<')
4950 extra += 2;
4951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953
4954 if (*p != '"')
4955 {
4956 EMSG2(_("E114: Missing quote: %s"), *arg);
4957 return FAIL;
4958 }
4959
4960 /* If only parsing, set *arg and return here */
4961 if (!evaluate)
4962 {
4963 *arg = p + 1;
4964 return OK;
4965 }
4966
4967 /*
4968 * Copy the string into allocated memory, handling backslashed
4969 * characters.
4970 */
4971 name = alloc((unsigned)(p - *arg + extra));
4972 if (name == NULL)
4973 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 rettv->v_type = VAR_STRING;
4975 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 if (*p == '\\')
4980 {
4981 switch (*++p)
4982 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 case 'b': *name++ = BS; ++p; break;
4984 case 'e': *name++ = ESC; ++p; break;
4985 case 'f': *name++ = FF; ++p; break;
4986 case 'n': *name++ = NL; ++p; break;
4987 case 'r': *name++ = CAR; ++p; break;
4988 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
4990 case 'X': /* hex: "\x1", "\x12" */
4991 case 'x':
4992 case 'u': /* Unicode: "\u0023" */
4993 case 'U':
4994 if (vim_isxdigit(p[1]))
4995 {
4996 int n, nr;
4997 int c = toupper(*p);
4998
4999 if (c == 'X')
5000 n = 2;
5001 else
5002 n = 4;
5003 nr = 0;
5004 while (--n >= 0 && vim_isxdigit(p[1]))
5005 {
5006 ++p;
5007 nr = (nr << 4) + hex2nr(*p);
5008 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010#ifdef FEAT_MBYTE
5011 /* For "\u" store the number according to
5012 * 'encoding'. */
5013 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 else
5016#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 break;
5020
5021 /* octal: "\1", "\12", "\123" */
5022 case '0':
5023 case '1':
5024 case '2':
5025 case '3':
5026 case '4':
5027 case '5':
5028 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 case '7': *name = *p++ - '0';
5030 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 *name = (*name << 3) + *p++ - '0';
5033 if (*p >= '0' && *p <= '7')
5034 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 break;
5038
5039 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (extra != 0)
5042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 break;
5045 }
5046 /* FALLTHROUGH */
5047
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 break;
5050 }
5051 }
5052 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 *arg = p + 1;
5058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 return OK;
5060}
5061
5062/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 * Return OK or FAIL.
5065 */
5066 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 int evaluate;
5071{
5072 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005073 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005074 int reduce = 0;
5075
5076 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 */
5079 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5080 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005084 break;
5085 ++reduce;
5086 ++p;
5087 }
5088 }
5089
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093 return FAIL;
5094 }
5095
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 if (!evaluate)
5098 {
5099 *arg = p + 1;
5100 return OK;
5101 }
5102
5103 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 */
5106 str = alloc((unsigned)((p - *arg) - reduce));
5107 if (str == NULL)
5108 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 rettv->v_type = VAR_STRING;
5110 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005117 break;
5118 ++p;
5119 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005121 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005123 *arg = p + 1;
5124
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005125 return OK;
5126}
5127
5128/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 * Allocate a variable for a List and fill it from "*arg".
5130 * Return OK or FAIL.
5131 */
5132 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005134 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005135 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005136 int evaluate;
5137{
Bram Moolenaar33570922005-01-25 22:26:29 +00005138 list_T *l = NULL;
5139 typval_T tv;
5140 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141
5142 if (evaluate)
5143 {
5144 l = list_alloc();
5145 if (l == NULL)
5146 return FAIL;
5147 }
5148
5149 *arg = skipwhite(*arg + 1);
5150 while (**arg != ']' && **arg != NUL)
5151 {
5152 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5153 goto failret;
5154 if (evaluate)
5155 {
5156 item = listitem_alloc();
5157 if (item != NULL)
5158 {
5159 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005160 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161 list_append(l, item);
5162 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005163 else
5164 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 }
5166
5167 if (**arg == ']')
5168 break;
5169 if (**arg != ',')
5170 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 goto failret;
5173 }
5174 *arg = skipwhite(*arg + 1);
5175 }
5176
5177 if (**arg != ']')
5178 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180failret:
5181 if (evaluate)
5182 list_free(l);
5183 return FAIL;
5184 }
5185
5186 *arg = skipwhite(*arg + 1);
5187 if (evaluate)
5188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 rettv->v_type = VAR_LIST;
5190 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 ++l->lv_refcount;
5192 }
5193
5194 return OK;
5195}
5196
5197/*
5198 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005199 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005200 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005201 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202list_alloc()
5203{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005204 list_T *l;
5205
5206 l = (list_T *)alloc_clear(sizeof(list_T));
5207 if (l != NULL)
5208 {
5209 /* Prepend the list to the list of lists for garbage collection. */
5210 if (first_list != NULL)
5211 first_list->lv_used_prev = l;
5212 l->lv_used_prev = NULL;
5213 l->lv_used_next = first_list;
5214 first_list = l;
5215 }
5216 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217}
5218
5219/*
5220 * Unreference a list: decrement the reference count and free it when it
5221 * becomes zero.
5222 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005223 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005225 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005227 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5228 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229}
5230
5231/*
5232 * Free a list, including all items it points to.
5233 * Ignores the reference count.
5234 */
5235 static void
5236list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005237 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238{
Bram Moolenaar33570922005-01-25 22:26:29 +00005239 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240
Bram Moolenaard9fba312005-06-26 22:34:35 +00005241 /* Avoid that recursive reference to the list frees us again. */
5242 l->lv_refcount = DEL_REFCOUNT;
5243
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005244 /* Remove the list from the list of lists for garbage collection. */
5245 if (l->lv_used_prev == NULL)
5246 first_list = l->lv_used_next;
5247 else
5248 l->lv_used_prev->lv_used_next = l->lv_used_next;
5249 if (l->lv_used_next != NULL)
5250 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5251
Bram Moolenaard9fba312005-06-26 22:34:35 +00005252 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005254 /* Remove the item before deleting it. */
5255 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 listitem_free(item);
5257 }
5258 vim_free(l);
5259}
5260
5261/*
5262 * Allocate a list item.
5263 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265listitem_alloc()
5266{
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268}
5269
5270/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005271 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 */
5273 static void
5274listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005275 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005277 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 vim_free(item);
5279}
5280
5281/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005282 * Remove a list item from a List and free it. Also clears the value.
5283 */
5284 static void
5285listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005286 list_T *l;
5287 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005288{
5289 list_remove(l, item, item);
5290 listitem_free(item);
5291}
5292
5293/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 * Get the number of items in a list.
5295 */
5296 static long
5297list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005298 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 if (l == NULL)
5301 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005302 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303}
5304
5305/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005306 * Return TRUE when two lists have exactly the same values.
5307 */
5308 static int
5309list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005310 list_T *l1;
5311 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005312 int ic; /* ignore case for strings */
5313{
Bram Moolenaar33570922005-01-25 22:26:29 +00005314 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005315
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005316 if (list_len(l1) != list_len(l2))
5317 return FALSE;
5318
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005319 for (item1 = l1->lv_first, item2 = l2->lv_first;
5320 item1 != NULL && item2 != NULL;
5321 item1 = item1->li_next, item2 = item2->li_next)
5322 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5323 return FALSE;
5324 return item1 == NULL && item2 == NULL;
5325}
5326
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005327#if defined(FEAT_PYTHON) || defined(PROTO)
5328/*
5329 * Return the dictitem that an entry in a hashtable points to.
5330 */
5331 dictitem_T *
5332dict_lookup(hi)
5333 hashitem_T *hi;
5334{
5335 return HI2DI(hi);
5336}
5337#endif
5338
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005339/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005340 * Return TRUE when two dictionaries have exactly the same key/values.
5341 */
5342 static int
5343dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005344 dict_T *d1;
5345 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005346 int ic; /* ignore case for strings */
5347{
Bram Moolenaar33570922005-01-25 22:26:29 +00005348 hashitem_T *hi;
5349 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005350 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005351
5352 if (dict_len(d1) != dict_len(d2))
5353 return FALSE;
5354
Bram Moolenaar33570922005-01-25 22:26:29 +00005355 todo = d1->dv_hashtab.ht_used;
5356 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005357 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005358 if (!HASHITEM_EMPTY(hi))
5359 {
5360 item2 = dict_find(d2, hi->hi_key, -1);
5361 if (item2 == NULL)
5362 return FALSE;
5363 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5364 return FALSE;
5365 --todo;
5366 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005367 }
5368 return TRUE;
5369}
5370
5371/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005372 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005373 * Compares the items just like "==" would compare them, but strings and
5374 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005375 */
5376 static int
5377tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005378 typval_T *tv1;
5379 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005380 int ic; /* ignore case */
5381{
5382 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005383 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005384
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005385 if (tv1->v_type != tv2->v_type)
5386 return FALSE;
5387
5388 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005389 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005390 case VAR_LIST:
5391 /* recursive! */
5392 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5393
5394 case VAR_DICT:
5395 /* recursive! */
5396 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5397
5398 case VAR_FUNC:
5399 return (tv1->vval.v_string != NULL
5400 && tv2->vval.v_string != NULL
5401 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5402
5403 case VAR_NUMBER:
5404 return tv1->vval.v_number == tv2->vval.v_number;
5405
5406 case VAR_STRING:
5407 s1 = get_tv_string_buf(tv1, buf1);
5408 s2 = get_tv_string_buf(tv2, buf2);
5409 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005410 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005411
5412 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005413 return TRUE;
5414}
5415
5416/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 * Locate item with index "n" in list "l" and return it.
5418 * A negative index is counted from the end; -1 is the last item.
5419 * Returns NULL when "n" is out of range.
5420 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005423 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 long n;
5425{
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 long idx;
5428
5429 if (l == NULL)
5430 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005431
5432 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005434 n = l->lv_len + n;
5435
5436 /* Check for index out of range. */
5437 if (n < 0 || n >= l->lv_len)
5438 return NULL;
5439
5440 /* When there is a cached index may start search from there. */
5441 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005443 if (n < l->lv_idx / 2)
5444 {
5445 /* closest to the start of the list */
5446 item = l->lv_first;
5447 idx = 0;
5448 }
5449 else if (n > (l->lv_idx + l->lv_len) / 2)
5450 {
5451 /* closest to the end of the list */
5452 item = l->lv_last;
5453 idx = l->lv_len - 1;
5454 }
5455 else
5456 {
5457 /* closest to the cached index */
5458 item = l->lv_idx_item;
5459 idx = l->lv_idx;
5460 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 else
5463 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005464 if (n < l->lv_len / 2)
5465 {
5466 /* closest to the start of the list */
5467 item = l->lv_first;
5468 idx = 0;
5469 }
5470 else
5471 {
5472 /* closest to the end of the list */
5473 item = l->lv_last;
5474 idx = l->lv_len - 1;
5475 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005477
5478 while (n > idx)
5479 {
5480 /* search forward */
5481 item = item->li_next;
5482 ++idx;
5483 }
5484 while (n < idx)
5485 {
5486 /* search backward */
5487 item = item->li_prev;
5488 --idx;
5489 }
5490
5491 /* cache the used index */
5492 l->lv_idx = idx;
5493 l->lv_idx_item = item;
5494
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 return item;
5496}
5497
5498/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005499 * Locate "item" list "l" and return its index.
5500 * Returns -1 when "item" is not in the list.
5501 */
5502 static long
5503list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005504 list_T *l;
5505 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005506{
5507 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005508 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005509
5510 if (l == NULL)
5511 return -1;
5512 idx = 0;
5513 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5514 ++idx;
5515 if (li == NULL)
5516 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005517 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005518}
5519
5520/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 * Append item "item" to the end of list "l".
5522 */
5523 static void
5524list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005525 list_T *l;
5526 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527{
5528 if (l->lv_last == NULL)
5529 {
5530 /* empty list */
5531 l->lv_first = item;
5532 l->lv_last = item;
5533 item->li_prev = NULL;
5534 }
5535 else
5536 {
5537 l->lv_last->li_next = item;
5538 item->li_prev = l->lv_last;
5539 l->lv_last = item;
5540 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005541 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 item->li_next = NULL;
5543}
5544
5545/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005546 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005547 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 */
5549 static int
5550list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 list_T *l;
5552 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005554 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555
Bram Moolenaar05159a02005-02-26 23:04:13 +00005556 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005558 copy_tv(tv, &li->li_tv);
5559 list_append(l, li);
5560 return OK;
5561}
5562
5563/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005564 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005565 * Return FAIL when out of memory.
5566 */
5567 int
5568list_append_dict(list, dict)
5569 list_T *list;
5570 dict_T *dict;
5571{
5572 listitem_T *li = listitem_alloc();
5573
5574 if (li == NULL)
5575 return FAIL;
5576 li->li_tv.v_type = VAR_DICT;
5577 li->li_tv.v_lock = 0;
5578 li->li_tv.vval.v_dict = dict;
5579 list_append(list, li);
5580 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 return OK;
5582}
5583
5584/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005585 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005586 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005587 * Returns FAIL when out of memory.
5588 */
5589 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005590list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005591 list_T *l;
5592 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005593 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005594{
5595 listitem_T *li = listitem_alloc();
5596
5597 if (li == NULL)
5598 return FAIL;
5599 list_append(l, li);
5600 li->li_tv.v_type = VAR_STRING;
5601 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005602 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5603 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005604 return FAIL;
5605 return OK;
5606}
5607
5608/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005609 * Append "n" to list "l".
5610 * Returns FAIL when out of memory.
5611 */
5612 static int
5613list_append_number(l, n)
5614 list_T *l;
5615 varnumber_T n;
5616{
5617 listitem_T *li;
5618
5619 li = listitem_alloc();
5620 if (li == NULL)
5621 return FAIL;
5622 li->li_tv.v_type = VAR_NUMBER;
5623 li->li_tv.v_lock = 0;
5624 li->li_tv.vval.v_number = n;
5625 list_append(l, li);
5626 return OK;
5627}
5628
5629/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005630 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005631 * If "item" is NULL append at the end.
5632 * Return FAIL when out of memory.
5633 */
5634 static int
5635list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005636 list_T *l;
5637 typval_T *tv;
5638 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005639{
Bram Moolenaar33570922005-01-25 22:26:29 +00005640 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005641
5642 if (ni == NULL)
5643 return FAIL;
5644 copy_tv(tv, &ni->li_tv);
5645 if (item == NULL)
5646 /* Append new item at end of list. */
5647 list_append(l, ni);
5648 else
5649 {
5650 /* Insert new item before existing item. */
5651 ni->li_prev = item->li_prev;
5652 ni->li_next = item;
5653 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005654 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005655 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005656 ++l->lv_idx;
5657 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005658 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005659 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005660 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005661 l->lv_idx_item = NULL;
5662 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005663 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005664 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005665 }
5666 return OK;
5667}
5668
5669/*
5670 * Extend "l1" with "l2".
5671 * If "bef" is NULL append at the end, otherwise insert before this item.
5672 * Returns FAIL when out of memory.
5673 */
5674 static int
5675list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005676 list_T *l1;
5677 list_T *l2;
5678 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005679{
Bram Moolenaar33570922005-01-25 22:26:29 +00005680 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005681
5682 for (item = l2->lv_first; item != NULL; item = item->li_next)
5683 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5684 return FAIL;
5685 return OK;
5686}
5687
5688/*
5689 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5690 * Return FAIL when out of memory.
5691 */
5692 static int
5693list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005694 list_T *l1;
5695 list_T *l2;
5696 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005697{
Bram Moolenaar33570922005-01-25 22:26:29 +00005698 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005699
5700 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005701 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005702 if (l == NULL)
5703 return FAIL;
5704 tv->v_type = VAR_LIST;
5705 tv->vval.v_list = l;
5706
5707 /* append all items from the second list */
5708 return list_extend(l, l2, NULL);
5709}
5710
5711/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005712 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005714 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715 * Returns NULL when out of memory.
5716 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005717 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005718list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005719 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005721 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722{
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 list_T *copy;
5724 listitem_T *item;
5725 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726
5727 if (orig == NULL)
5728 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729
5730 copy = list_alloc();
5731 if (copy != NULL)
5732 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005733 if (copyID != 0)
5734 {
5735 /* Do this before adding the items, because one of the items may
5736 * refer back to this list. */
5737 orig->lv_copyID = copyID;
5738 orig->lv_copylist = copy;
5739 }
5740 for (item = orig->lv_first; item != NULL && !got_int;
5741 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742 {
5743 ni = listitem_alloc();
5744 if (ni == NULL)
5745 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005746 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005747 {
5748 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5749 {
5750 vim_free(ni);
5751 break;
5752 }
5753 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005755 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 list_append(copy, ni);
5757 }
5758 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005759 if (item != NULL)
5760 {
5761 list_unref(copy);
5762 copy = NULL;
5763 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 }
5765
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 return copy;
5767}
5768
5769/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005770 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005771 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005773 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005774list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005775 list_T *l;
5776 listitem_T *item;
5777 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005778{
Bram Moolenaar33570922005-01-25 22:26:29 +00005779 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005781 /* notify watchers */
5782 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005784 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005785 list_fix_watch(l, ip);
5786 if (ip == item2)
5787 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005788 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005789
5790 if (item2->li_next == NULL)
5791 l->lv_last = item->li_prev;
5792 else
5793 item2->li_next->li_prev = item->li_prev;
5794 if (item->li_prev == NULL)
5795 l->lv_first = item2->li_next;
5796 else
5797 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005798 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799}
5800
5801/*
5802 * Return an allocated string with the string representation of a list.
5803 * May return NULL.
5804 */
5805 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005806list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005807 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005808 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809{
5810 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811
5812 if (tv->vval.v_list == NULL)
5813 return NULL;
5814 ga_init2(&ga, (int)sizeof(char), 80);
5815 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005816 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005817 {
5818 vim_free(ga.ga_data);
5819 return NULL;
5820 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 ga_append(&ga, ']');
5822 ga_append(&ga, NUL);
5823 return (char_u *)ga.ga_data;
5824}
5825
5826/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005827 * Join list "l" into a string in "*gap", using separator "sep".
5828 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005829 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005830 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005831 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005832list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 char_u *sep;
5836 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005837 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838{
5839 int first = TRUE;
5840 char_u *tofree;
5841 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005842 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 char_u *s;
5844
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005845 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 {
5847 if (first)
5848 first = FALSE;
5849 else
5850 ga_concat(gap, sep);
5851
5852 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005853 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005855 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 if (s != NULL)
5857 ga_concat(gap, s);
5858 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005859 if (s == NULL)
5860 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005862 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863}
5864
5865/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005866 * Garbage collection for lists and dictionaries.
5867 *
5868 * We use reference counts to be able to free most items right away when they
5869 * are no longer used. But for composite items it's possible that it becomes
5870 * unused while the reference count is > 0: When there is a recursive
5871 * reference. Example:
5872 * :let l = [1, 2, 3]
5873 * :let d = {9: l}
5874 * :let l[1] = d
5875 *
5876 * Since this is quite unusual we handle this with garbage collection: every
5877 * once in a while find out which lists and dicts are not referenced from any
5878 * variable.
5879 *
5880 * Here is a good reference text about garbage collection (refers to Python
5881 * but it applies to all reference-counting mechanisms):
5882 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005883 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005884
5885/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886 * Do garbage collection for lists and dicts.
5887 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005888 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005889 int
5890garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005891{
5892 dict_T *dd;
5893 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005894 int copyID = ++current_copyID;
5895 buf_T *buf;
5896 win_T *wp;
5897 int i;
5898 funccall_T *fc;
5899 int did_free = FALSE;
5900
5901 /*
5902 * 1. Go through all accessible variables and mark all lists and dicts
5903 * with copyID.
5904 */
5905 /* script-local variables */
5906 for (i = 1; i <= ga_scripts.ga_len; ++i)
5907 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5908
5909 /* buffer-local variables */
5910 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5911 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5912
5913 /* window-local variables */
5914 FOR_ALL_WINDOWS(wp)
5915 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5916
5917 /* global variables */
5918 set_ref_in_ht(&globvarht, copyID);
5919
5920 /* function-local variables */
5921 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5922 {
5923 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5924 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5925 }
5926
5927 /*
5928 * 2. Go through the list of dicts and free items without the copyID.
5929 */
5930 for (dd = first_dict; dd != NULL; )
5931 if (dd->dv_copyID != copyID)
5932 {
5933 dict_free(dd);
5934 did_free = TRUE;
5935
5936 /* restart, next dict may also have been freed */
5937 dd = first_dict;
5938 }
5939 else
5940 dd = dd->dv_used_next;
5941
5942 /*
5943 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005944 * But don't free a list that has a watcher (used in a for loop), these
5945 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005946 */
5947 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005948 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005949 {
5950 list_free(ll);
5951 did_free = TRUE;
5952
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005953 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005954 ll = first_list;
5955 }
5956 else
5957 ll = ll->lv_used_next;
5958
5959 return did_free;
5960}
5961
5962/*
5963 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5964 */
5965 static void
5966set_ref_in_ht(ht, copyID)
5967 hashtab_T *ht;
5968 int copyID;
5969{
5970 int todo;
5971 hashitem_T *hi;
5972
5973 todo = ht->ht_used;
5974 for (hi = ht->ht_array; todo > 0; ++hi)
5975 if (!HASHITEM_EMPTY(hi))
5976 {
5977 --todo;
5978 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5979 }
5980}
5981
5982/*
5983 * Mark all lists and dicts referenced through list "l" with "copyID".
5984 */
5985 static void
5986set_ref_in_list(l, copyID)
5987 list_T *l;
5988 int copyID;
5989{
5990 listitem_T *li;
5991
5992 for (li = l->lv_first; li != NULL; li = li->li_next)
5993 set_ref_in_item(&li->li_tv, copyID);
5994}
5995
5996/*
5997 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5998 */
5999 static void
6000set_ref_in_item(tv, copyID)
6001 typval_T *tv;
6002 int copyID;
6003{
6004 dict_T *dd;
6005 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006006
6007 switch (tv->v_type)
6008 {
6009 case VAR_DICT:
6010 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006011 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006012 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006013 /* Didn't see this dict yet. */
6014 dd->dv_copyID = copyID;
6015 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006016 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006017 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006018
6019 case VAR_LIST:
6020 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006021 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006022 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006023 /* Didn't see this list yet. */
6024 ll->lv_copyID = copyID;
6025 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006026 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006027 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006028 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006029 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006030}
6031
6032/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006033 * Allocate an empty header for a dictionary.
6034 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006035 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006036dict_alloc()
6037{
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006039
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006041 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006042 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006043 /* Add the list to the hashtable for garbage collection. */
6044 if (first_dict != NULL)
6045 first_dict->dv_used_prev = d;
6046 d->dv_used_next = first_dict;
6047 d->dv_used_prev = NULL;
6048
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006050 d->dv_lock = 0;
6051 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006052 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006053 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006054 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006055}
6056
6057/*
6058 * Unreference a Dictionary: decrement the reference count and free it when it
6059 * becomes zero.
6060 */
6061 static void
6062dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006064{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006065 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6066 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006067}
6068
6069/*
6070 * Free a Dictionary, including all items it contains.
6071 * Ignores the reference count.
6072 */
6073 static void
6074dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006076{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006079 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006080
Bram Moolenaard9fba312005-06-26 22:34:35 +00006081 /* Avoid that recursive reference to the dict frees us again. */
6082 d->dv_refcount = DEL_REFCOUNT;
6083
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006084 /* Remove the dict from the list of dicts for garbage collection. */
6085 if (d->dv_used_prev == NULL)
6086 first_dict = d->dv_used_next;
6087 else
6088 d->dv_used_prev->dv_used_next = d->dv_used_next;
6089 if (d->dv_used_next != NULL)
6090 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6091
6092 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006093 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006094 todo = d->dv_hashtab.ht_used;
6095 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006096 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006097 if (!HASHITEM_EMPTY(hi))
6098 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006099 /* Remove the item before deleting it, just in case there is
6100 * something recursive causing trouble. */
6101 di = HI2DI(hi);
6102 hash_remove(&d->dv_hashtab, hi);
6103 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006104 --todo;
6105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006106 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006108 vim_free(d);
6109}
6110
6111/*
6112 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006113 * The "key" is copied to the new item.
6114 * Note that the value of the item "di_tv" still needs to be initialized!
6115 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006116 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006117 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006118dictitem_alloc(key)
6119 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006120{
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006122
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006124 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006125 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006126 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006127 di->di_flags = 0;
6128 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006129 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006130}
6131
6132/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006133 * Make a copy of a Dictionary item.
6134 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006136dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006138{
Bram Moolenaar33570922005-01-25 22:26:29 +00006139 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006140
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006142 if (di != NULL)
6143 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006144 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006145 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006146 copy_tv(&org->di_tv, &di->di_tv);
6147 }
6148 return di;
6149}
6150
6151/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152 * Remove item "item" from Dictionary "dict" and free it.
6153 */
6154 static void
6155dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 dict_T *dict;
6157 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006158{
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160
Bram Moolenaar33570922005-01-25 22:26:29 +00006161 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006162 if (HASHITEM_EMPTY(hi))
6163 EMSG2(_(e_intern2), "dictitem_remove()");
6164 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 dictitem_free(item);
6167}
6168
6169/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006170 * Free a dict item. Also clears the value.
6171 */
6172 static void
6173dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006175{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006176 clear_tv(&item->di_tv);
6177 vim_free(item);
6178}
6179
6180/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006181 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6182 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006183 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006184 * Returns NULL when out of memory.
6185 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006186 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006187dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006189 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006190 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191{
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 dict_T *copy;
6193 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006194 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006195 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006196
6197 if (orig == NULL)
6198 return NULL;
6199
6200 copy = dict_alloc();
6201 if (copy != NULL)
6202 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006203 if (copyID != 0)
6204 {
6205 orig->dv_copyID = copyID;
6206 orig->dv_copydict = copy;
6207 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006208 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006209 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006210 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006212 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006213 --todo;
6214
6215 di = dictitem_alloc(hi->hi_key);
6216 if (di == NULL)
6217 break;
6218 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006219 {
6220 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6221 copyID) == FAIL)
6222 {
6223 vim_free(di);
6224 break;
6225 }
6226 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006227 else
6228 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6229 if (dict_add(copy, di) == FAIL)
6230 {
6231 dictitem_free(di);
6232 break;
6233 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006235 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006236
Bram Moolenaare9a41262005-01-15 22:18:47 +00006237 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006238 if (todo > 0)
6239 {
6240 dict_unref(copy);
6241 copy = NULL;
6242 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006243 }
6244
6245 return copy;
6246}
6247
6248/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006249 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006250 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006251 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006252 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006253dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 dict_T *d;
6255 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006256{
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006258}
6259
Bram Moolenaar8c711452005-01-14 21:53:12 +00006260/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006261 * Add a number or string entry to dictionary "d".
6262 * When "str" is NULL use number "nr", otherwise use "str".
6263 * Returns FAIL when out of memory and when key already exists.
6264 */
6265 int
6266dict_add_nr_str(d, key, nr, str)
6267 dict_T *d;
6268 char *key;
6269 long nr;
6270 char_u *str;
6271{
6272 dictitem_T *item;
6273
6274 item = dictitem_alloc((char_u *)key);
6275 if (item == NULL)
6276 return FAIL;
6277 item->di_tv.v_lock = 0;
6278 if (str == NULL)
6279 {
6280 item->di_tv.v_type = VAR_NUMBER;
6281 item->di_tv.vval.v_number = nr;
6282 }
6283 else
6284 {
6285 item->di_tv.v_type = VAR_STRING;
6286 item->di_tv.vval.v_string = vim_strsave(str);
6287 }
6288 if (dict_add(d, item) == FAIL)
6289 {
6290 dictitem_free(item);
6291 return FAIL;
6292 }
6293 return OK;
6294}
6295
6296/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006297 * Get the number of items in a Dictionary.
6298 */
6299 static long
6300dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006301 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006302{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303 if (d == NULL)
6304 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006306}
6307
6308/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309 * Find item "key[len]" in Dictionary "d".
6310 * If "len" is negative use strlen(key).
6311 * Returns NULL when not found.
6312 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006314dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316 char_u *key;
6317 int len;
6318{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006319#define AKEYLEN 200
6320 char_u buf[AKEYLEN];
6321 char_u *akey;
6322 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006324
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325 if (len < 0)
6326 akey = key;
6327 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006328 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006329 tofree = akey = vim_strnsave(key, len);
6330 if (akey == NULL)
6331 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006332 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006333 else
6334 {
6335 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006336 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006337 akey = buf;
6338 }
6339
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006341 vim_free(tofree);
6342 if (HASHITEM_EMPTY(hi))
6343 return NULL;
6344 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006345}
6346
6347/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006348 * Get a string item from a dictionary in allocated memory.
6349 * Returns NULL if the entry doesn't exist or out of memory.
6350 */
6351 char_u *
6352get_dict_string(d, key)
6353 dict_T *d;
6354 char_u *key;
6355{
6356 dictitem_T *di;
6357
6358 di = dict_find(d, key, -1);
6359 if (di == NULL)
6360 return NULL;
6361 return vim_strsave(get_tv_string(&di->di_tv));
6362}
6363
6364/*
6365 * Get a number item from a dictionary.
6366 * Returns 0 if the entry doesn't exist or out of memory.
6367 */
6368 long
6369get_dict_number(d, key)
6370 dict_T *d;
6371 char_u *key;
6372{
6373 dictitem_T *di;
6374
6375 di = dict_find(d, key, -1);
6376 if (di == NULL)
6377 return 0;
6378 return get_tv_number(&di->di_tv);
6379}
6380
6381/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006382 * Return an allocated string with the string representation of a Dictionary.
6383 * May return NULL.
6384 */
6385 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006386dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006388 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006389{
6390 garray_T ga;
6391 int first = TRUE;
6392 char_u *tofree;
6393 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006395 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006397 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006399 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006400 return NULL;
6401 ga_init2(&ga, (int)sizeof(char), 80);
6402 ga_append(&ga, '{');
6403
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006405 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006407 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006408 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006409 --todo;
6410
6411 if (first)
6412 first = FALSE;
6413 else
6414 ga_concat(&ga, (char_u *)", ");
6415
6416 tofree = string_quote(hi->hi_key, FALSE);
6417 if (tofree != NULL)
6418 {
6419 ga_concat(&ga, tofree);
6420 vim_free(tofree);
6421 }
6422 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006423 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006424 if (s != NULL)
6425 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006427 if (s == NULL)
6428 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006430 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006431 if (todo > 0)
6432 {
6433 vim_free(ga.ga_data);
6434 return NULL;
6435 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006436
6437 ga_append(&ga, '}');
6438 ga_append(&ga, NUL);
6439 return (char_u *)ga.ga_data;
6440}
6441
6442/*
6443 * Allocate a variable for a Dictionary and fill it from "*arg".
6444 * Return OK or FAIL. Returns NOTDONE for {expr}.
6445 */
6446 static int
6447get_dict_tv(arg, rettv, evaluate)
6448 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006449 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006450 int evaluate;
6451{
Bram Moolenaar33570922005-01-25 22:26:29 +00006452 dict_T *d = NULL;
6453 typval_T tvkey;
6454 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006455 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006456 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006457 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006458 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006459
6460 /*
6461 * First check if it's not a curly-braces thing: {expr}.
6462 * Must do this without evaluating, otherwise a function may be called
6463 * twice. Unfortunately this means we need to call eval1() twice for the
6464 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006465 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006466 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006467 if (*start != '}')
6468 {
6469 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6470 return FAIL;
6471 if (*start == '}')
6472 return NOTDONE;
6473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006474
6475 if (evaluate)
6476 {
6477 d = dict_alloc();
6478 if (d == NULL)
6479 return FAIL;
6480 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006481 tvkey.v_type = VAR_UNKNOWN;
6482 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483
6484 *arg = skipwhite(*arg + 1);
6485 while (**arg != '}' && **arg != NUL)
6486 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006487 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006488 goto failret;
6489 if (**arg != ':')
6490 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006491 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006492 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006493 goto failret;
6494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006495 key = get_tv_string_buf_chk(&tvkey, buf);
6496 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006498 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6499 if (key != NULL)
6500 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502 goto failret;
6503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504
6505 *arg = skipwhite(*arg + 1);
6506 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6507 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006508 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006509 goto failret;
6510 }
6511 if (evaluate)
6512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006513 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006514 if (item != NULL)
6515 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006516 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006517 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006518 clear_tv(&tv);
6519 goto failret;
6520 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006521 item = dictitem_alloc(key);
6522 clear_tv(&tvkey);
6523 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006524 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006525 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006526 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006527 if (dict_add(d, item) == FAIL)
6528 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529 }
6530 }
6531
6532 if (**arg == '}')
6533 break;
6534 if (**arg != ',')
6535 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006536 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006537 goto failret;
6538 }
6539 *arg = skipwhite(*arg + 1);
6540 }
6541
6542 if (**arg != '}')
6543 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006544 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006545failret:
6546 if (evaluate)
6547 dict_free(d);
6548 return FAIL;
6549 }
6550
6551 *arg = skipwhite(*arg + 1);
6552 if (evaluate)
6553 {
6554 rettv->v_type = VAR_DICT;
6555 rettv->vval.v_dict = d;
6556 ++d->dv_refcount;
6557 }
6558
6559 return OK;
6560}
6561
Bram Moolenaar8c711452005-01-14 21:53:12 +00006562/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 * Return a string with the string representation of a variable.
6564 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006566 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006567 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 * May return NULL;
6569 */
6570 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006571echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006572 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006575 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006577 static int recurse = 0;
6578 char_u *r = NULL;
6579
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006581 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006582 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006583 *tofree = NULL;
6584 return NULL;
6585 }
6586 ++recurse;
6587
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588 switch (tv->v_type)
6589 {
6590 case VAR_FUNC:
6591 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006592 r = tv->vval.v_string;
6593 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006594
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006596 if (tv->vval.v_list == NULL)
6597 {
6598 *tofree = NULL;
6599 r = NULL;
6600 }
6601 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6602 {
6603 *tofree = NULL;
6604 r = (char_u *)"[...]";
6605 }
6606 else
6607 {
6608 tv->vval.v_list->lv_copyID = copyID;
6609 *tofree = list2string(tv, copyID);
6610 r = *tofree;
6611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006612 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006613
Bram Moolenaar8c711452005-01-14 21:53:12 +00006614 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006615 if (tv->vval.v_dict == NULL)
6616 {
6617 *tofree = NULL;
6618 r = NULL;
6619 }
6620 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6621 {
6622 *tofree = NULL;
6623 r = (char_u *)"{...}";
6624 }
6625 else
6626 {
6627 tv->vval.v_dict->dv_copyID = copyID;
6628 *tofree = dict2string(tv, copyID);
6629 r = *tofree;
6630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006631 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006632
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006633 case VAR_STRING:
6634 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006635 *tofree = NULL;
6636 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006639 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006640 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006641 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006643
6644 --recurse;
6645 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006646}
6647
6648/*
6649 * Return a string with the string representation of a variable.
6650 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6651 * "numbuf" is used for a number.
6652 * Puts quotes around strings, so that they can be parsed back by eval().
6653 * May return NULL;
6654 */
6655 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006656tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006657 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006658 char_u **tofree;
6659 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006660 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006661{
6662 switch (tv->v_type)
6663 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006664 case VAR_FUNC:
6665 *tofree = string_quote(tv->vval.v_string, TRUE);
6666 return *tofree;
6667 case VAR_STRING:
6668 *tofree = string_quote(tv->vval.v_string, FALSE);
6669 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006670 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006671 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006672 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006673 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006675 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678}
6679
6680/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 * Return string "str" in ' quotes, doubling ' characters.
6682 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684 */
6685 static char_u *
6686string_quote(str, function)
6687 char_u *str;
6688 int function;
6689{
Bram Moolenaar33570922005-01-25 22:26:29 +00006690 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691 char_u *p, *r, *s;
6692
Bram Moolenaar33570922005-01-25 22:26:29 +00006693 len = (function ? 13 : 3);
6694 if (str != NULL)
6695 {
6696 len += STRLEN(str);
6697 for (p = str; *p != NUL; mb_ptr_adv(p))
6698 if (*p == '\'')
6699 ++len;
6700 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 s = r = alloc(len);
6702 if (r != NULL)
6703 {
6704 if (function)
6705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006706 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 r += 10;
6708 }
6709 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006710 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006711 if (str != NULL)
6712 for (p = str; *p != NUL; )
6713 {
6714 if (*p == '\'')
6715 *r++ = '\'';
6716 MB_COPY_CHAR(p, r);
6717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006718 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 if (function)
6720 *r++ = ')';
6721 *r++ = NUL;
6722 }
6723 return s;
6724}
6725
6726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 * Get the value of an environment variable.
6728 * "arg" is pointing to the '$'. It is advanced to after the name.
6729 * If the environment variable was not set, silently assume it is empty.
6730 * Always return OK.
6731 */
6732 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006733get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 int evaluate;
6737{
6738 char_u *string = NULL;
6739 int len;
6740 int cc;
6741 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006742 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
6744 ++*arg;
6745 name = *arg;
6746 len = get_env_len(arg);
6747 if (evaluate)
6748 {
6749 if (len != 0)
6750 {
6751 cc = name[len];
6752 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006753 /* first try vim_getenv(), fast for normal environment vars */
6754 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006756 {
6757 if (!mustfree)
6758 string = vim_strsave(string);
6759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 else
6761 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006762 if (mustfree)
6763 vim_free(string);
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 /* next try expanding things like $VIM and ${HOME} */
6766 string = expand_env_save(name - 1);
6767 if (string != NULL && *string == '$')
6768 {
6769 vim_free(string);
6770 string = NULL;
6771 }
6772 }
6773 name[len] = cc;
6774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006775 rettv->v_type = VAR_STRING;
6776 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 }
6778
6779 return OK;
6780}
6781
6782/*
6783 * Array with names and number of arguments of all internal functions
6784 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6785 */
6786static struct fst
6787{
6788 char *f_name; /* function name */
6789 char f_min_argc; /* minimal number of arguments */
6790 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793} functions[] =
6794{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006795 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {"append", 2, 2, f_append},
6797 {"argc", 0, 0, f_argc},
6798 {"argidx", 0, 0, f_argidx},
6799 {"argv", 1, 1, f_argv},
6800 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006801 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {"bufexists", 1, 1, f_bufexists},
6803 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6804 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6805 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6806 {"buflisted", 1, 1, f_buflisted},
6807 {"bufloaded", 1, 1, f_bufloaded},
6808 {"bufname", 1, 1, f_bufname},
6809 {"bufnr", 1, 1, f_bufnr},
6810 {"bufwinnr", 1, 1, f_bufwinnr},
6811 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006812 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006813 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {"char2nr", 1, 1, f_char2nr},
6815 {"cindent", 1, 1, f_cindent},
6816 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006817#if defined(FEAT_INS_EXPAND)
6818 {"complete_add", 1, 1, f_complete_add},
6819 {"complete_check", 0, 0, f_complete_check},
6820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006822 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 {"cscope_connection",0,3, f_cscope_connection},
6825 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006826 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {"delete", 1, 1, f_delete},
6828 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006829 {"diff_filler", 1, 1, f_diff_filler},
6830 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006831 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006833 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {"eventhandler", 0, 0, f_eventhandler},
6835 {"executable", 1, 1, f_executable},
6836 {"exists", 1, 1, f_exists},
6837 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006838 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6840 {"filereadable", 1, 1, f_filereadable},
6841 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006842 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006843 {"finddir", 1, 3, f_finddir},
6844 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {"fnamemodify", 2, 2, f_fnamemodify},
6846 {"foldclosed", 1, 1, f_foldclosed},
6847 {"foldclosedend", 1, 1, f_foldclosedend},
6848 {"foldlevel", 1, 1, f_foldlevel},
6849 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006850 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006852 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006854 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006855 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 {"getbufvar", 2, 2, f_getbufvar},
6857 {"getchar", 0, 1, f_getchar},
6858 {"getcharmod", 0, 0, f_getcharmod},
6859 {"getcmdline", 0, 0, f_getcmdline},
6860 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006861 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006863 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006864 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {"getfsize", 1, 1, f_getfsize},
6866 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006867 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006868 {"getline", 1, 2, f_getline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006869 {"getloclist", 1, 1, f_getloclist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006870 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006871 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {"getregtype", 0, 1, f_getregtype},
6873 {"getwinposx", 0, 0, f_getwinposx},
6874 {"getwinposy", 0, 0, f_getwinposy},
6875 {"getwinvar", 2, 2, f_getwinvar},
6876 {"glob", 1, 1, f_glob},
6877 {"globpath", 2, 2, f_globpath},
6878 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {"hasmapto", 1, 2, f_hasmapto},
6881 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6882 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6883 {"histadd", 2, 2, f_histadd},
6884 {"histdel", 1, 2, f_histdel},
6885 {"histget", 1, 2, f_histget},
6886 {"histnr", 1, 1, f_histnr},
6887 {"hlID", 1, 1, f_hlID},
6888 {"hlexists", 1, 1, f_hlexists},
6889 {"hostname", 0, 0, f_hostname},
6890 {"iconv", 3, 3, f_iconv},
6891 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006892 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006893 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006895 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 {"inputrestore", 0, 0, f_inputrestore},
6897 {"inputsave", 0, 0, f_inputsave},
6898 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006899 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006901 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006902 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006903 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006904 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006906 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 {"libcall", 3, 3, f_libcall},
6908 {"libcallnr", 3, 3, f_libcallnr},
6909 {"line", 1, 1, f_line},
6910 {"line2byte", 1, 1, f_line2byte},
6911 {"lispindent", 1, 1, f_lispindent},
6912 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006913 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {"maparg", 1, 2, f_maparg},
6915 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006916 {"match", 2, 4, f_match},
6917 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006918 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006919 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006920 {"max", 1, 1, f_max},
6921 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006922#ifdef vim_mkdir
6923 {"mkdir", 1, 3, f_mkdir},
6924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {"mode", 0, 0, f_mode},
6926 {"nextnonblank", 1, 1, f_nextnonblank},
6927 {"nr2char", 1, 1, f_nr2char},
6928 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006929 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006930 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006931 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {"remote_expr", 2, 3, f_remote_expr},
6933 {"remote_foreground", 1, 1, f_remote_foreground},
6934 {"remote_peek", 1, 2, f_remote_peek},
6935 {"remote_read", 1, 1, f_remote_read},
6936 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006937 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006939 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006941 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006943 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 {"searchpair", 3, 5, f_searchpair},
6945 {"server2client", 2, 2, f_server2client},
6946 {"serverlist", 0, 0, f_serverlist},
6947 {"setbufvar", 3, 3, f_setbufvar},
6948 {"setcmdpos", 1, 1, f_setcmdpos},
6949 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006950 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006951 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {"setreg", 2, 3, f_setreg},
6953 {"setwinvar", 3, 3, f_setwinvar},
6954 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006955 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006956 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006957 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006958 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006959 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960#ifdef HAVE_STRFTIME
6961 {"strftime", 1, 2, f_strftime},
6962#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006963 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006964 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {"strlen", 1, 1, f_strlen},
6966 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006967 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {"strtrans", 1, 1, f_strtrans},
6969 {"submatch", 1, 1, f_submatch},
6970 {"substitute", 4, 4, f_substitute},
6971 {"synID", 3, 3, f_synID},
6972 {"synIDattr", 2, 3, f_synIDattr},
6973 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006974 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006975 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006976 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006978 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {"tolower", 1, 1, f_tolower},
6980 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006981 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006983 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {"virtcol", 1, 1, f_virtcol},
6985 {"visualmode", 0, 1, f_visualmode},
6986 {"winbufnr", 1, 1, f_winbufnr},
6987 {"wincol", 0, 0, f_wincol},
6988 {"winheight", 1, 1, f_winheight},
6989 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006990 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {"winrestcmd", 0, 0, f_winrestcmd},
6992 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006993 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994};
6995
6996#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6997
6998/*
6999 * Function given to ExpandGeneric() to obtain the list of internal
7000 * or user defined function names.
7001 */
7002 char_u *
7003get_function_name(xp, idx)
7004 expand_T *xp;
7005 int idx;
7006{
7007 static int intidx = -1;
7008 char_u *name;
7009
7010 if (idx == 0)
7011 intidx = -1;
7012 if (intidx < 0)
7013 {
7014 name = get_user_func_name(xp, idx);
7015 if (name != NULL)
7016 return name;
7017 }
7018 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7019 {
7020 STRCPY(IObuff, functions[intidx].f_name);
7021 STRCAT(IObuff, "(");
7022 if (functions[intidx].f_max_argc == 0)
7023 STRCAT(IObuff, ")");
7024 return IObuff;
7025 }
7026
7027 return NULL;
7028}
7029
7030/*
7031 * Function given to ExpandGeneric() to obtain the list of internal or
7032 * user defined variable or function names.
7033 */
7034/*ARGSUSED*/
7035 char_u *
7036get_expr_name(xp, idx)
7037 expand_T *xp;
7038 int idx;
7039{
7040 static int intidx = -1;
7041 char_u *name;
7042
7043 if (idx == 0)
7044 intidx = -1;
7045 if (intidx < 0)
7046 {
7047 name = get_function_name(xp, idx);
7048 if (name != NULL)
7049 return name;
7050 }
7051 return get_user_var_name(xp, ++intidx);
7052}
7053
7054#endif /* FEAT_CMDL_COMPL */
7055
7056/*
7057 * Find internal function in table above.
7058 * Return index, or -1 if not found
7059 */
7060 static int
7061find_internal_func(name)
7062 char_u *name; /* name of the function */
7063{
7064 int first = 0;
7065 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7066 int cmp;
7067 int x;
7068
7069 /*
7070 * Find the function name in the table. Binary search.
7071 */
7072 while (first <= last)
7073 {
7074 x = first + ((unsigned)(last - first) >> 1);
7075 cmp = STRCMP(name, functions[x].f_name);
7076 if (cmp < 0)
7077 last = x - 1;
7078 else if (cmp > 0)
7079 first = x + 1;
7080 else
7081 return x;
7082 }
7083 return -1;
7084}
7085
7086/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007087 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7088 * name it contains, otherwise return "name".
7089 */
7090 static char_u *
7091deref_func_name(name, lenp)
7092 char_u *name;
7093 int *lenp;
7094{
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007096 int cc;
7097
7098 cc = name[*lenp];
7099 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007100 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007101 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007103 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007105 {
7106 *lenp = 0;
7107 return (char_u *)""; /* just in case */
7108 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 *lenp = STRLEN(v->di_tv.vval.v_string);
7110 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007111 }
7112
7113 return name;
7114}
7115
7116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 * Allocate a variable for the result of a function.
7118 * Return OK or FAIL.
7119 */
7120 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007121get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7122 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 char_u *name; /* name of the function */
7124 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 char_u **arg; /* argument, pointing to the '(' */
7127 linenr_T firstline; /* first line of range */
7128 linenr_T lastline; /* last line of range */
7129 int *doesrange; /* return: function handled range */
7130 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
7133 char_u *argp;
7134 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 int argcount = 0; /* number of arguments found */
7137
7138 /*
7139 * Get the arguments.
7140 */
7141 argp = *arg;
7142 while (argcount < MAX_FUNC_ARGS)
7143 {
7144 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7145 if (*argp == ')' || *argp == ',' || *argp == NUL)
7146 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7148 {
7149 ret = FAIL;
7150 break;
7151 }
7152 ++argcount;
7153 if (*argp != ',')
7154 break;
7155 }
7156 if (*argp == ')')
7157 ++argp;
7158 else
7159 ret = FAIL;
7160
7161 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007162 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007163 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007165 {
7166 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007167 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007169 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
7172 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007173 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
7175 *arg = skipwhite(argp);
7176 return ret;
7177}
7178
7179
7180/*
7181 * Call a function with its resolved parameters
7182 * Return OK or FAIL.
7183 */
7184 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007185call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 char_u *name; /* name of the function */
7188 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 linenr_T firstline; /* first line of range */
7193 linenr_T lastline; /* last line of range */
7194 int *doesrange; /* return: function handled range */
7195 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199#define ERROR_UNKNOWN 0
7200#define ERROR_TOOMANY 1
7201#define ERROR_TOOFEW 2
7202#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203#define ERROR_DICT 4
7204#define ERROR_NONE 5
7205#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 int error = ERROR_NONE;
7207 int i;
7208 int llen;
7209 ufunc_T *fp;
7210 int cc;
7211#define FLEN_FIXED 40
7212 char_u fname_buf[FLEN_FIXED + 1];
7213 char_u *fname;
7214
7215 /*
7216 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7217 * Change <SNR>123_name() to K_SNR 123_name().
7218 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7219 */
7220 cc = name[len];
7221 name[len] = NUL;
7222 llen = eval_fname_script(name);
7223 if (llen > 0)
7224 {
7225 fname_buf[0] = K_SPECIAL;
7226 fname_buf[1] = KS_EXTRA;
7227 fname_buf[2] = (int)KE_SNR;
7228 i = 3;
7229 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7230 {
7231 if (current_SID <= 0)
7232 error = ERROR_SCRIPT;
7233 else
7234 {
7235 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7236 i = (int)STRLEN(fname_buf);
7237 }
7238 }
7239 if (i + STRLEN(name + llen) < FLEN_FIXED)
7240 {
7241 STRCPY(fname_buf + i, name + llen);
7242 fname = fname_buf;
7243 }
7244 else
7245 {
7246 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7247 if (fname == NULL)
7248 error = ERROR_OTHER;
7249 else
7250 {
7251 mch_memmove(fname, fname_buf, (size_t)i);
7252 STRCPY(fname + i, name + llen);
7253 }
7254 }
7255 }
7256 else
7257 fname = name;
7258
7259 *doesrange = FALSE;
7260
7261
7262 /* execute the function if no errors detected and executing */
7263 if (evaluate && error == ERROR_NONE)
7264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007265 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 error = ERROR_UNKNOWN;
7267
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007268 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
7270 /*
7271 * User defined function.
7272 */
7273 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007276 /* Trigger FuncUndefined event, may load the function. */
7277 if (fp == NULL
7278 && apply_autocmds(EVENT_FUNCUNDEFINED,
7279 fname, fname, TRUE, NULL)
7280 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007282 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 fp = find_func(fname);
7284 }
7285#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007286 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007287 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007288 {
7289 /* loaded a package, search for the function again */
7290 fp = find_func(fname);
7291 }
7292
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (fp != NULL)
7294 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007295 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007297 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007299 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007301 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 else
7304 {
7305 /*
7306 * Call the user function.
7307 * Save and restore search patterns, script variables and
7308 * redo buffer.
7309 */
7310 save_search_patterns();
7311 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007312 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007313 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007315 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7316 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7317 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007318 /* Function was unreferenced while being used, free it
7319 * now. */
7320 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 restoreRedobuff();
7322 restore_search_patterns();
7323 error = ERROR_NONE;
7324 }
7325 }
7326 }
7327 else
7328 {
7329 /*
7330 * Find the function name in the table, call its implementation.
7331 */
7332 i = find_internal_func(fname);
7333 if (i >= 0)
7334 {
7335 if (argcount < functions[i].f_min_argc)
7336 error = ERROR_TOOFEW;
7337 else if (argcount > functions[i].f_max_argc)
7338 error = ERROR_TOOMANY;
7339 else
7340 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007341 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007342 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 error = ERROR_NONE;
7344 }
7345 }
7346 }
7347 /*
7348 * The function call (or "FuncUndefined" autocommand sequence) might
7349 * have been aborted by an error, an interrupt, or an explicitly thrown
7350 * exception that has not been caught so far. This situation can be
7351 * tested for by calling aborting(). For an error in an internal
7352 * function or for the "E132" error in call_user_func(), however, the
7353 * throw point at which the "force_abort" flag (temporarily reset by
7354 * emsg()) is normally updated has not been reached yet. We need to
7355 * update that flag first to make aborting() reliable.
7356 */
7357 update_force_abort();
7358 }
7359 if (error == ERROR_NONE)
7360 ret = OK;
7361
7362 /*
7363 * Report an error unless the argument evaluation or function call has been
7364 * cancelled due to an aborting error, an interrupt, or an exception.
7365 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 if (!aborting())
7367 {
7368 switch (error)
7369 {
7370 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007371 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372 break;
7373 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 break;
7376 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007378 name);
7379 break;
7380 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 name);
7383 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 name);
7387 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 }
7389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390
7391 name[len] = cc;
7392 if (fname != name && fname != fname_buf)
7393 vim_free(fname);
7394
7395 return ret;
7396}
7397
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398/*
7399 * Give an error message with a function name. Handle <SNR> things.
7400 */
7401 static void
7402emsg_funcname(msg, name)
7403 char *msg;
7404 char_u *name;
7405{
7406 char_u *p;
7407
7408 if (*name == K_SPECIAL)
7409 p = concat_str((char_u *)"<SNR>", name + 3);
7410 else
7411 p = name;
7412 EMSG2(_(msg), p);
7413 if (p != name)
7414 vim_free(p);
7415}
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417/*********************************************
7418 * Implementation of the built-in functions
7419 */
7420
7421/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007422 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 */
7424 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007425f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 typval_T *argvars;
7427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007432 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007434 if ((l = argvars[0].vval.v_list) != NULL
7435 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7436 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007438 }
7439 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007440 EMSG(_(e_listreq));
7441}
7442
7443/*
7444 * "append(lnum, string/list)" function
7445 */
7446 static void
7447f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 typval_T *argvars;
7449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007450{
7451 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007452 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 list_T *l = NULL;
7454 listitem_T *li = NULL;
7455 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007456 long added = 0;
7457
Bram Moolenaar0d660222005-01-07 21:51:51 +00007458 lnum = get_tv_lnum(argvars);
7459 if (lnum >= 0
7460 && lnum <= curbuf->b_ml.ml_line_count
7461 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007462 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007463 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007464 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007465 l = argvars[1].vval.v_list;
7466 if (l == NULL)
7467 return;
7468 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007470 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007471 for (;;)
7472 {
7473 if (l == NULL)
7474 tv = &argvars[1]; /* append a string */
7475 else if (li == NULL)
7476 break; /* end of list */
7477 else
7478 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007479 line = get_tv_string_chk(tv);
7480 if (line == NULL) /* type error */
7481 {
7482 rettv->vval.v_number = 1; /* Failed */
7483 break;
7484 }
7485 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007486 ++added;
7487 if (l == NULL)
7488 break;
7489 li = li->li_next;
7490 }
7491
7492 appended_lines_mark(lnum, added);
7493 if (curwin->w_cursor.lnum > lnum)
7494 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007496 else
7497 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498}
7499
7500/*
7501 * "argc()" function
7502 */
7503/* ARGSUSED */
7504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007505f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 typval_T *argvars;
7507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510}
7511
7512/*
7513 * "argidx()" function
7514 */
7515/* ARGSUSED */
7516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007517f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 typval_T *argvars;
7519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007521 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522}
7523
7524/*
7525 * "argv(nr)" function
7526 */
7527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007528f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 typval_T *argvars;
7530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
7532 int idx;
7533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007534 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 rettv->vval.v_string = NULL;
7539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540}
7541
7542/*
7543 * "browse(save, title, initdir, default)" function
7544 */
7545/* ARGSUSED */
7546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007547f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 typval_T *argvars;
7549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550{
7551#ifdef FEAT_BROWSE
7552 int save;
7553 char_u *title;
7554 char_u *initdir;
7555 char_u *defname;
7556 char_u buf[NUMBUFLEN];
7557 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007558 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007560 save = get_tv_number_chk(&argvars[0], &error);
7561 title = get_tv_string_chk(&argvars[1]);
7562 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7563 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007565 if (error || title == NULL || initdir == NULL || defname == NULL)
7566 rettv->vval.v_string = NULL;
7567 else
7568 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007569 do_browse(save ? BROWSE_SAVE : 0,
7570 title, defname, NULL, initdir, NULL, curbuf);
7571#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007573#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007574 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007575}
7576
7577/*
7578 * "browsedir(title, initdir)" function
7579 */
7580/* ARGSUSED */
7581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007582f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 typval_T *argvars;
7584 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007585{
7586#ifdef FEAT_BROWSE
7587 char_u *title;
7588 char_u *initdir;
7589 char_u buf[NUMBUFLEN];
7590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007591 title = get_tv_string_chk(&argvars[0]);
7592 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007594 if (title == NULL || initdir == NULL)
7595 rettv->vval.v_string = NULL;
7596 else
7597 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007598 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603}
7604
Bram Moolenaar33570922005-01-25 22:26:29 +00007605static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007606
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607/*
7608 * Find a buffer by number or exact name.
7609 */
7610 static buf_T *
7611find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007616 if (avar->v_type == VAR_NUMBER)
7617 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007618 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007620 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007621 if (buf == NULL)
7622 {
7623 /* No full path name match, try a match with a URL or a "nofile"
7624 * buffer, these don't use the full path. */
7625 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7626 if (buf->b_fname != NULL
7627 && (path_with_url(buf->b_fname)
7628#ifdef FEAT_QUICKFIX
7629 || bt_nofile(buf)
7630#endif
7631 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007632 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007633 break;
7634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636 return buf;
7637}
7638
7639/*
7640 * "bufexists(expr)" function
7641 */
7642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 typval_T *argvars;
7645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
7650/*
7651 * "buflisted(expr)" function
7652 */
7653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 typval_T *argvars;
7656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
7658 buf_T *buf;
7659
7660 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662}
7663
7664/*
7665 * "bufloaded(expr)" function
7666 */
7667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 typval_T *argvars;
7670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
7672 buf_T *buf;
7673
7674 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007675 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676}
7677
Bram Moolenaar33570922005-01-25 22:26:29 +00007678static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680/*
7681 * Get buffer by number or pattern.
7682 */
7683 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007684get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 int save_magic;
7689 char_u *save_cpo;
7690 buf_T *buf;
7691
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007692 if (tv->v_type == VAR_NUMBER)
7693 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007694 if (tv->v_type != VAR_STRING)
7695 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (name == NULL || *name == NUL)
7697 return curbuf;
7698 if (name[0] == '$' && name[1] == NUL)
7699 return lastbuf;
7700
7701 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7702 save_magic = p_magic;
7703 p_magic = TRUE;
7704 save_cpo = p_cpo;
7705 p_cpo = (char_u *)"";
7706
7707 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7708 TRUE, FALSE));
7709
7710 p_magic = save_magic;
7711 p_cpo = save_cpo;
7712
7713 /* If not found, try expanding the name, like done for bufexists(). */
7714 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007715 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
7717 return buf;
7718}
7719
7720/*
7721 * "bufname(expr)" function
7722 */
7723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 typval_T *argvars;
7726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 buf_T *buf;
7729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007730 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007732 buf = get_buf_tv(&argvars[0]);
7733 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007735 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 --emsg_off;
7739}
7740
7741/*
7742 * "bufnr(expr)" function
7743 */
7744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007745f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 typval_T *argvars;
7747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
7749 buf_T *buf;
7750
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007751 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 --emsg_off;
7759}
7760
7761/*
7762 * "bufwinnr(nr)" function
7763 */
7764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 typval_T *argvars;
7767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769#ifdef FEAT_WINDOWS
7770 win_T *wp;
7771 int winnr = 0;
7772#endif
7773 buf_T *buf;
7774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007775 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778#ifdef FEAT_WINDOWS
7779 for (wp = firstwin; wp; wp = wp->w_next)
7780 {
7781 ++winnr;
7782 if (wp->w_buffer == buf)
7783 break;
7784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007787 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#endif
7789 --emsg_off;
7790}
7791
7792/*
7793 * "byte2line(byte)" function
7794 */
7795/*ARGSUSED*/
7796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 typval_T *argvars;
7799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803#else
7804 long boff = 0;
7805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007806 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 (linenr_T)0, &boff);
7812#endif
7813}
7814
7815/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007816 * "byteidx()" function
7817 */
7818/*ARGSUSED*/
7819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007821 typval_T *argvars;
7822 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007823{
7824#ifdef FEAT_MBYTE
7825 char_u *t;
7826#endif
7827 char_u *str;
7828 long idx;
7829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007830 str = get_tv_string_chk(&argvars[0]);
7831 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007832 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007833 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007834 return;
7835
7836#ifdef FEAT_MBYTE
7837 t = str;
7838 for ( ; idx > 0; idx--)
7839 {
7840 if (*t == NUL) /* EOL reached */
7841 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007842 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007845#else
7846 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007848#endif
7849}
7850
7851/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007852 * "call(func, arglist)" function
7853 */
7854 static void
7855f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *argvars;
7857 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007858{
7859 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007861 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007863 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007865
7866 rettv->vval.v_number = 0;
7867 if (argvars[1].v_type != VAR_LIST)
7868 {
7869 EMSG(_(e_listreq));
7870 return;
7871 }
7872 if (argvars[1].vval.v_list == NULL)
7873 return;
7874
7875 if (argvars[0].v_type == VAR_FUNC)
7876 func = argvars[0].vval.v_string;
7877 else
7878 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007879 if (*func == NUL)
7880 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007881
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 if (argvars[2].v_type != VAR_UNKNOWN)
7883 {
7884 if (argvars[2].v_type != VAR_DICT)
7885 {
7886 EMSG(_(e_dictreq));
7887 return;
7888 }
7889 selfdict = argvars[2].vval.v_dict;
7890 }
7891
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007892 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7893 item = item->li_next)
7894 {
7895 if (argc == MAX_FUNC_ARGS)
7896 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007897 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007898 break;
7899 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007900 /* Make a copy of each argument. This is needed to be able to set
7901 * v_lock to VAR_FIXED in the copy without changing the original list.
7902 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007903 copy_tv(&item->li_tv, &argv[argc++]);
7904 }
7905
7906 if (item == NULL)
7907 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007908 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7909 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007910
7911 /* Free the arguments. */
7912 while (argc > 0)
7913 clear_tv(&argv[--argc]);
7914}
7915
7916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 * "char2nr(string)" function
7918 */
7919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 typval_T *argvars;
7922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
7924#ifdef FEAT_MBYTE
7925 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007926 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 else
7928#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007929 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930}
7931
7932/*
7933 * "cindent(lnum)" function
7934 */
7935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007936f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 typval_T *argvars;
7938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940#ifdef FEAT_CINDENT
7941 pos_T pos;
7942 linenr_T lnum;
7943
7944 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007945 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7947 {
7948 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007949 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 curwin->w_cursor = pos;
7951 }
7952 else
7953#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007954 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955}
7956
7957/*
7958 * "col(string)" function
7959 */
7960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 typval_T *argvars;
7963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 colnr_T col = 0;
7966 pos_T *fp;
7967
7968 fp = var2fpos(&argvars[0], FALSE);
7969 if (fp != NULL)
7970 {
7971 if (fp->col == MAXCOL)
7972 {
7973 /* '> can be MAXCOL, get the length of the line then */
7974 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7975 col = STRLEN(ml_get(fp->lnum)) + 1;
7976 else
7977 col = MAXCOL;
7978 }
7979 else
7980 {
7981 col = fp->col + 1;
7982#ifdef FEAT_VIRTUALEDIT
7983 /* col(".") when the cursor is on the NUL at the end of the line
7984 * because of "coladd" can be seen as an extra column. */
7985 if (virtual_active() && fp == &curwin->w_cursor)
7986 {
7987 char_u *p = ml_get_cursor();
7988
7989 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7990 curwin->w_virtcol - curwin->w_cursor.coladd))
7991 {
7992# ifdef FEAT_MBYTE
7993 int l;
7994
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007995 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 col += l;
7997# else
7998 if (*p != NUL && p[1] == NUL)
7999 ++col;
8000# endif
8001 }
8002 }
8003#endif
8004 }
8005 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007}
8008
Bram Moolenaar572cb562005-08-05 21:35:02 +00008009#if defined(FEAT_INS_EXPAND)
8010/*
8011 * "complete_add()" function
8012 */
8013/*ARGSUSED*/
8014 static void
8015f_complete_add(argvars, rettv)
8016 typval_T *argvars;
8017 typval_T *rettv;
8018{
8019 char_u *s;
8020
8021 s = get_tv_string_chk(&argvars[0]);
8022 if (s != NULL)
8023 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
8024}
8025
8026/*
8027 * "complete_check()" function
8028 */
8029/*ARGSUSED*/
8030 static void
8031f_complete_check(argvars, rettv)
8032 typval_T *argvars;
8033 typval_T *rettv;
8034{
8035 int saved = RedrawingDisabled;
8036
8037 RedrawingDisabled = 0;
8038 ins_compl_check_keys(0);
8039 rettv->vval.v_number = compl_interrupted;
8040 RedrawingDisabled = saved;
8041}
8042#endif
8043
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044/*
8045 * "confirm(message, buttons[, default [, type]])" function
8046 */
8047/*ARGSUSED*/
8048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008050 typval_T *argvars;
8051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
8053#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8054 char_u *message;
8055 char_u *buttons = NULL;
8056 char_u buf[NUMBUFLEN];
8057 char_u buf2[NUMBUFLEN];
8058 int def = 1;
8059 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008060 char_u *typestr;
8061 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008063 message = get_tv_string_chk(&argvars[0]);
8064 if (message == NULL)
8065 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008066 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008068 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8069 if (buttons == NULL)
8070 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008071 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008073 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008074 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008076 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8077 if (typestr == NULL)
8078 error = TRUE;
8079 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 switch (TOUPPER_ASC(*typestr))
8082 {
8083 case 'E': type = VIM_ERROR; break;
8084 case 'Q': type = VIM_QUESTION; break;
8085 case 'I': type = VIM_INFO; break;
8086 case 'W': type = VIM_WARNING; break;
8087 case 'G': type = VIM_GENERIC; break;
8088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 }
8090 }
8091 }
8092 }
8093
8094 if (buttons == NULL || *buttons == NUL)
8095 buttons = (char_u *)_("&Ok");
8096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008097 if (error)
8098 rettv->vval.v_number = 0;
8099 else
8100 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 def, NULL);
8102#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008103 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104#endif
8105}
8106
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008107/*
8108 * "copy()" function
8109 */
8110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 typval_T *argvars;
8113 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008114{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008115 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117
8118/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008119 * "count()" function
8120 */
8121 static void
8122f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008123 typval_T *argvars;
8124 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008125{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 long n = 0;
8127 int ic = FALSE;
8128
Bram Moolenaare9a41262005-01-15 22:18:47 +00008129 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008130 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008131 listitem_T *li;
8132 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008133 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008134
Bram Moolenaare9a41262005-01-15 22:18:47 +00008135 if ((l = argvars[0].vval.v_list) != NULL)
8136 {
8137 li = l->lv_first;
8138 if (argvars[2].v_type != VAR_UNKNOWN)
8139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008140 int error = FALSE;
8141
8142 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008143 if (argvars[3].v_type != VAR_UNKNOWN)
8144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008145 idx = get_tv_number_chk(&argvars[3], &error);
8146 if (!error)
8147 {
8148 li = list_find(l, idx);
8149 if (li == NULL)
8150 EMSGN(_(e_listidx), idx);
8151 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008153 if (error)
8154 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008155 }
8156
8157 for ( ; li != NULL; li = li->li_next)
8158 if (tv_equal(&li->li_tv, &argvars[1], ic))
8159 ++n;
8160 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008162 else if (argvars[0].v_type == VAR_DICT)
8163 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008164 int todo;
8165 dict_T *d;
8166 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008167
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008168 if ((d = argvars[0].vval.v_dict) != NULL)
8169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008170 int error = FALSE;
8171
Bram Moolenaare9a41262005-01-15 22:18:47 +00008172 if (argvars[2].v_type != VAR_UNKNOWN)
8173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008174 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008175 if (argvars[3].v_type != VAR_UNKNOWN)
8176 EMSG(_(e_invarg));
8177 }
8178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008179 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008180 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008181 {
8182 if (!HASHITEM_EMPTY(hi))
8183 {
8184 --todo;
8185 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8186 ++n;
8187 }
8188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 }
8190 }
8191 else
8192 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008193 rettv->vval.v_number = n;
8194}
8195
8196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8198 *
8199 * Checks the existence of a cscope connection.
8200 */
8201/*ARGSUSED*/
8202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 typval_T *argvars;
8205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207#ifdef FEAT_CSCOPE
8208 int num = 0;
8209 char_u *dbpath = NULL;
8210 char_u *prepend = NULL;
8211 char_u buf[NUMBUFLEN];
8212
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008213 if (argvars[0].v_type != VAR_UNKNOWN
8214 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008216 num = (int)get_tv_number(&argvars[0]);
8217 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008218 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 }
8221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008222 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225#endif
8226}
8227
8228/*
8229 * "cursor(lnum, col)" function
8230 *
8231 * Moves the cursor to the specified line and column
8232 */
8233/*ARGSUSED*/
8234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008236 typval_T *argvars;
8237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239 long line, col;
8240
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008242 col = get_tv_number_chk(&argvars[1], NULL);
8243 if (line < 0 || col < 0)
8244 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if (line > 0)
8246 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 if (col > 0)
8248 curwin->w_cursor.col = col - 1;
8249#ifdef FEAT_VIRTUALEDIT
8250 curwin->w_cursor.coladd = 0;
8251#endif
8252
8253 /* Make sure the cursor is in a valid position. */
8254 check_cursor();
8255#ifdef FEAT_MBYTE
8256 /* Correct cursor for multi-byte character. */
8257 if (has_mbyte)
8258 mb_adjust_cursor();
8259#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008260
8261 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262}
8263
8264/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008265 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 */
8267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008268f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 typval_T *argvars;
8270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008272 int noref = 0;
8273
8274 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008275 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008276 if (noref < 0 || noref > 1)
8277 EMSG(_(e_invarg));
8278 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008279 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280}
8281
8282/*
8283 * "delete()" function
8284 */
8285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008286f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008287 typval_T *argvars;
8288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289{
8290 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294}
8295
8296/*
8297 * "did_filetype()" function
8298 */
8299/*ARGSUSED*/
8300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008301f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008302 typval_T *argvars;
8303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304{
8305#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008306 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309#endif
8310}
8311
8312/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008313 * "diff_filler()" function
8314 */
8315/*ARGSUSED*/
8316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 typval_T *argvars;
8319 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008320{
8321#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008323#endif
8324}
8325
8326/*
8327 * "diff_hlID()" function
8328 */
8329/*ARGSUSED*/
8330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 typval_T *argvars;
8333 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008334{
8335#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008336 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008337 static linenr_T prev_lnum = 0;
8338 static int changedtick = 0;
8339 static int fnum = 0;
8340 static int change_start = 0;
8341 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008342 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008343 int filler_lines;
8344 int col;
8345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008346 if (lnum < 0) /* ignore type error in {lnum} arg */
8347 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008348 if (lnum != prev_lnum
8349 || changedtick != curbuf->b_changedtick
8350 || fnum != curbuf->b_fnum)
8351 {
8352 /* New line, buffer, change: need to get the values. */
8353 filler_lines = diff_check(curwin, lnum);
8354 if (filler_lines < 0)
8355 {
8356 if (filler_lines == -1)
8357 {
8358 change_start = MAXCOL;
8359 change_end = -1;
8360 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8361 hlID = HLF_ADD; /* added line */
8362 else
8363 hlID = HLF_CHD; /* changed line */
8364 }
8365 else
8366 hlID = HLF_ADD; /* added line */
8367 }
8368 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008369 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008370 prev_lnum = lnum;
8371 changedtick = curbuf->b_changedtick;
8372 fnum = curbuf->b_fnum;
8373 }
8374
8375 if (hlID == HLF_CHD || hlID == HLF_TXD)
8376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008377 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008378 if (col >= change_start && col <= change_end)
8379 hlID = HLF_TXD; /* changed text */
8380 else
8381 hlID = HLF_CHD; /* changed line */
8382 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008383 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008384#endif
8385}
8386
8387/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008388 * "empty({expr})" function
8389 */
8390 static void
8391f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008392 typval_T *argvars;
8393 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008394{
8395 int n;
8396
8397 switch (argvars[0].v_type)
8398 {
8399 case VAR_STRING:
8400 case VAR_FUNC:
8401 n = argvars[0].vval.v_string == NULL
8402 || *argvars[0].vval.v_string == NUL;
8403 break;
8404 case VAR_NUMBER:
8405 n = argvars[0].vval.v_number == 0;
8406 break;
8407 case VAR_LIST:
8408 n = argvars[0].vval.v_list == NULL
8409 || argvars[0].vval.v_list->lv_first == NULL;
8410 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008411 case VAR_DICT:
8412 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008414 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008415 default:
8416 EMSG2(_(e_intern2), "f_empty()");
8417 n = 0;
8418 }
8419
8420 rettv->vval.v_number = n;
8421}
8422
8423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 * "escape({string}, {chars})" function
8425 */
8426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008427f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008428 typval_T *argvars;
8429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430{
8431 char_u buf[NUMBUFLEN];
8432
Bram Moolenaar758711c2005-02-02 23:11:38 +00008433 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8434 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436}
8437
8438/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008439 * "eval()" function
8440 */
8441/*ARGSUSED*/
8442 static void
8443f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008444 typval_T *argvars;
8445 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008446{
8447 char_u *s;
8448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008449 s = get_tv_string_chk(&argvars[0]);
8450 if (s != NULL)
8451 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008453 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8454 {
8455 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008456 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008457 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008458 else if (*s != NUL)
8459 EMSG(_(e_trailing));
8460}
8461
8462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 * "eventhandler()" function
8464 */
8465/*ARGSUSED*/
8466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008468 typval_T *argvars;
8469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472}
8473
8474/*
8475 * "executable()" function
8476 */
8477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008478f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008479 typval_T *argvars;
8480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008482 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483}
8484
8485/*
8486 * "exists()" function
8487 */
8488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008490 typval_T *argvars;
8491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 char_u *p;
8494 char_u *name;
8495 int n = FALSE;
8496 int len = 0;
8497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (*p == '$') /* environment variable */
8500 {
8501 /* first try "normal" environment variables (fast) */
8502 if (mch_getenv(p + 1) != NULL)
8503 n = TRUE;
8504 else
8505 {
8506 /* try expanding things like $VIM and ${HOME} */
8507 p = expand_env_save(p);
8508 if (p != NULL && *p != '$')
8509 n = TRUE;
8510 vim_free(p);
8511 }
8512 }
8513 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 else if (*p == '*') /* internal or user defined function */
8516 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008517 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 }
8519 else if (*p == ':')
8520 {
8521 n = cmd_exists(p + 1);
8522 }
8523 else if (*p == '#')
8524 {
8525#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008526 if (p[1] == '#')
8527 n = autocmd_supported(p + 2);
8528 else
8529 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530#endif
8531 }
8532 else /* internal variable */
8533 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008534 char_u *tofree;
8535 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008537 /* get_name_len() takes care of expanding curly braces */
8538 name = p;
8539 len = get_name_len(&p, &tofree, TRUE, FALSE);
8540 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008542 if (tofree != NULL)
8543 name = tofree;
8544 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8545 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008547 /* handle d.key, l[idx], f(expr) */
8548 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8549 if (n)
8550 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 }
8552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008554 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 }
8556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558}
8559
8560/*
8561 * "expand()" function
8562 */
8563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008565 typval_T *argvars;
8566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
8568 char_u *s;
8569 int len;
8570 char_u *errormsg;
8571 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8572 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008573 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575 rettv->v_type = VAR_STRING;
8576 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (*s == '%' || *s == '#' || *s == '<')
8578 {
8579 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 --emsg_off;
8582 }
8583 else
8584 {
8585 /* When the optional second argument is non-zero, don't remove matches
8586 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008587 if (argvars[1].v_type != VAR_UNKNOWN
8588 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008590 if (!error)
8591 {
8592 ExpandInit(&xpc);
8593 xpc.xp_context = EXPAND_FILES;
8594 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8595 ExpandCleanup(&xpc);
8596 }
8597 else
8598 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 }
8600}
8601
8602/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008603 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008604 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008605 */
8606 static void
8607f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008608 typval_T *argvars;
8609 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008610{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008611 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008612 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008613 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008614 list_T *l1, *l2;
8615 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008616 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008617 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008618
Bram Moolenaare9a41262005-01-15 22:18:47 +00008619 l1 = argvars[0].vval.v_list;
8620 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008621 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8622 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623 {
8624 if (argvars[2].v_type != VAR_UNKNOWN)
8625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008626 before = get_tv_number_chk(&argvars[2], &error);
8627 if (error)
8628 return; /* type error; errmsg already given */
8629
Bram Moolenaar758711c2005-02-02 23:11:38 +00008630 if (before == l1->lv_len)
8631 item = NULL;
8632 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008634 item = list_find(l1, before);
8635 if (item == NULL)
8636 {
8637 EMSGN(_(e_listidx), before);
8638 return;
8639 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 }
8641 }
8642 else
8643 item = NULL;
8644 list_extend(l1, l2, item);
8645
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 copy_tv(&argvars[0], rettv);
8647 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008648 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008649 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8650 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008651 dict_T *d1, *d2;
8652 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008653 char_u *action;
8654 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008656 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008657
8658 d1 = argvars[0].vval.v_dict;
8659 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008660 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8661 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008662 {
8663 /* Check the third argument. */
8664 if (argvars[2].v_type != VAR_UNKNOWN)
8665 {
8666 static char *(av[]) = {"keep", "force", "error"};
8667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008668 action = get_tv_string_chk(&argvars[2]);
8669 if (action == NULL)
8670 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008671 for (i = 0; i < 3; ++i)
8672 if (STRCMP(action, av[i]) == 0)
8673 break;
8674 if (i == 3)
8675 {
8676 EMSGN(_(e_invarg2), action);
8677 return;
8678 }
8679 }
8680 else
8681 action = (char_u *)"force";
8682
8683 /* Go over all entries in the second dict and add them to the
8684 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 todo = d2->dv_hashtab.ht_used;
8686 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008687 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008688 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008689 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008690 --todo;
8691 di1 = dict_find(d1, hi2->hi_key, -1);
8692 if (di1 == NULL)
8693 {
8694 di1 = dictitem_copy(HI2DI(hi2));
8695 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8696 dictitem_free(di1);
8697 }
8698 else if (*action == 'e')
8699 {
8700 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8701 break;
8702 }
8703 else if (*action == 'f')
8704 {
8705 clear_tv(&di1->di_tv);
8706 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8707 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008708 }
8709 }
8710
Bram Moolenaare9a41262005-01-15 22:18:47 +00008711 copy_tv(&argvars[0], rettv);
8712 }
8713 }
8714 else
8715 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008716}
8717
8718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 * "filereadable()" function
8720 */
8721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
8726 FILE *fd;
8727 char_u *p;
8728 int n;
8729
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8732 {
8733 n = TRUE;
8734 fclose(fd);
8735 }
8736 else
8737 n = FALSE;
8738
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740}
8741
8742/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008743 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 * rights to write into.
8745 */
8746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008748 typval_T *argvars;
8749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008751 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752}
8753
Bram Moolenaar33570922005-01-25 22:26:29 +00008754static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008755
8756 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008757findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008758 typval_T *argvars;
8759 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008760 int dir;
8761{
8762#ifdef FEAT_SEARCHPATH
8763 char_u *fname;
8764 char_u *fresult = NULL;
8765 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8766 char_u *p;
8767 char_u pathbuf[NUMBUFLEN];
8768 int count = 1;
8769 int first = TRUE;
8770
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008772
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008773 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8776 if (p == NULL)
8777 count = -1; /* error */
8778 else
8779 {
8780 if (*p != NUL)
8781 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008783 if (argvars[2].v_type != VAR_UNKNOWN)
8784 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8785 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008786 }
8787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008788 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008790 do
8791 {
8792 vim_free(fresult);
8793 fresult = find_file_in_path_option(first ? fname : NULL,
8794 first ? (int)STRLEN(fname) : 0,
8795 0, first, path, dir, NULL);
8796 first = FALSE;
8797 } while (--count > 0 && fresult != NULL);
8798 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008799
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008800 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008801#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008803#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008804 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008805}
8806
Bram Moolenaar33570922005-01-25 22:26:29 +00008807static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8808static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008809
8810/*
8811 * Implementation of map() and filter().
8812 */
8813 static void
8814filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008815 typval_T *argvars;
8816 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008817 int map;
8818{
8819 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008820 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008821 listitem_T *li, *nli;
8822 list_T *l = NULL;
8823 dictitem_T *di;
8824 hashtab_T *ht;
8825 hashitem_T *hi;
8826 dict_T *d = NULL;
8827 typval_T save_val;
8828 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008829 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008830 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008831 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8832
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008833
8834 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008835 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008836 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008837 if ((l = argvars[0].vval.v_list) == NULL
8838 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008839 return;
8840 }
8841 else if (argvars[0].v_type == VAR_DICT)
8842 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008843 if ((d = argvars[0].vval.v_dict) == NULL
8844 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008845 return;
8846 }
8847 else
8848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008849 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 return;
8851 }
8852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008853 expr = get_tv_string_buf_chk(&argvars[1], buf);
8854 /* On type errors, the preceding call has already displayed an error
8855 * message. Avoid a misleading error message for an empty string that
8856 * was not passed as argument. */
8857 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008859 prepare_vimvar(VV_VAL, &save_val);
8860 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008862 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008863 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008864 prepare_vimvar(VV_KEY, &save_key);
8865 vimvars[VV_KEY].vv_type = VAR_STRING;
8866
8867 ht = &d->dv_hashtab;
8868 hash_lock(ht);
8869 todo = ht->ht_used;
8870 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008872 if (!HASHITEM_EMPTY(hi))
8873 {
8874 --todo;
8875 di = HI2DI(hi);
8876 if (tv_check_lock(di->di_tv.v_lock, msg))
8877 break;
8878 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8879 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8880 break;
8881 if (!map && rem)
8882 dictitem_remove(d, di);
8883 clear_tv(&vimvars[VV_KEY].vv_tv);
8884 }
8885 }
8886 hash_unlock(ht);
8887
8888 restore_vimvar(VV_KEY, &save_key);
8889 }
8890 else
8891 {
8892 for (li = l->lv_first; li != NULL; li = nli)
8893 {
8894 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008895 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008896 nli = li->li_next;
8897 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008898 break;
8899 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008900 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008901 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008902 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008905 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008906
8907 copy_tv(&argvars[0], rettv);
8908}
8909
8910 static int
8911filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008912 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008913 char_u *expr;
8914 int map;
8915 int *remp;
8916{
Bram Moolenaar33570922005-01-25 22:26:29 +00008917 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008918 char_u *s;
8919
Bram Moolenaar33570922005-01-25 22:26:29 +00008920 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921 s = expr;
8922 if (eval1(&s, &rettv, TRUE) == FAIL)
8923 return FAIL;
8924 if (*s != NUL) /* check for trailing chars after expr */
8925 {
8926 EMSG2(_(e_invexpr2), s);
8927 return FAIL;
8928 }
8929 if (map)
8930 {
8931 /* map(): replace the list item value */
8932 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008933 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008934 *tv = rettv;
8935 }
8936 else
8937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008938 int error = FALSE;
8939
Bram Moolenaare9a41262005-01-15 22:18:47 +00008940 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008942 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 /* On type error, nothing has been removed; return FAIL to stop the
8944 * loop. The error message was given by get_tv_number_chk(). */
8945 if (error)
8946 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008948 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008949 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008950}
8951
8952/*
8953 * "filter()" function
8954 */
8955 static void
8956f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008957 typval_T *argvars;
8958 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008959{
8960 filter_map(argvars, rettv, FALSE);
8961}
8962
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008963/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008964 * "finddir({fname}[, {path}[, {count}]])" function
8965 */
8966 static void
8967f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 typval_T *argvars;
8969 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008970{
8971 findfilendir(argvars, rettv, TRUE);
8972}
8973
8974/*
8975 * "findfile({fname}[, {path}[, {count}]])" function
8976 */
8977 static void
8978f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008979 typval_T *argvars;
8980 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008981{
8982 findfilendir(argvars, rettv, FALSE);
8983}
8984
8985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 * "fnamemodify({fname}, {mods})" function
8987 */
8988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008990 typval_T *argvars;
8991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992{
8993 char_u *fname;
8994 char_u *mods;
8995 int usedlen = 0;
8996 int len;
8997 char_u *fbuf = NULL;
8998 char_u buf[NUMBUFLEN];
8999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009000 fname = get_tv_string_chk(&argvars[0]);
9001 mods = get_tv_string_buf_chk(&argvars[1], buf);
9002 if (fname == NULL || mods == NULL)
9003 fname = NULL;
9004 else
9005 {
9006 len = (int)STRLEN(fname);
9007 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009014 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 vim_free(fbuf);
9016}
9017
Bram Moolenaar33570922005-01-25 22:26:29 +00009018static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
9020/*
9021 * "foldclosed()" function
9022 */
9023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009025 typval_T *argvars;
9026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 int end;
9028{
9029#ifdef FEAT_FOLDING
9030 linenr_T lnum;
9031 linenr_T first, last;
9032
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009033 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9035 {
9036 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9037 {
9038 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 return;
9043 }
9044 }
9045#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047}
9048
9049/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009050 * "foldclosed()" function
9051 */
9052 static void
9053f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 typval_T *argvars;
9055 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009056{
9057 foldclosed_both(argvars, rettv, FALSE);
9058}
9059
9060/*
9061 * "foldclosedend()" function
9062 */
9063 static void
9064f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 typval_T *argvars;
9066 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009067{
9068 foldclosed_both(argvars, rettv, TRUE);
9069}
9070
9071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 * "foldlevel()" function
9073 */
9074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 typval_T *argvars;
9077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
9079#ifdef FEAT_FOLDING
9080 linenr_T lnum;
9081
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 else
9086#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088}
9089
9090/*
9091 * "foldtext()" function
9092 */
9093/*ARGSUSED*/
9094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *argvars;
9097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
9099#ifdef FEAT_FOLDING
9100 linenr_T lnum;
9101 char_u *s;
9102 char_u *r;
9103 int len;
9104 char *txt;
9105#endif
9106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 rettv->v_type = VAR_STRING;
9108 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009110 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9111 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9112 <= curbuf->b_ml.ml_line_count
9113 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 {
9115 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009116 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9117 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 {
9119 if (!linewhite(lnum))
9120 break;
9121 ++lnum;
9122 }
9123
9124 /* Find interesting text in this line. */
9125 s = skipwhite(ml_get(lnum));
9126 /* skip C comment-start */
9127 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009130 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009131 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009132 {
9133 s = skipwhite(ml_get(lnum + 1));
9134 if (*s == '*')
9135 s = skipwhite(s + 1);
9136 }
9137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 txt = _("+-%s%3ld lines: ");
9139 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009140 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 + 20 /* for %3ld */
9142 + STRLEN(s))); /* concatenated */
9143 if (r != NULL)
9144 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009145 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9146 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9147 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 len = (int)STRLEN(r);
9149 STRCAT(r, s);
9150 /* remove 'foldmarker' and 'commentstring' */
9151 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 }
9154 }
9155#endif
9156}
9157
9158/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009159 * "foldtextresult(lnum)" function
9160 */
9161/*ARGSUSED*/
9162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *argvars;
9165 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009166{
9167#ifdef FEAT_FOLDING
9168 linenr_T lnum;
9169 char_u *text;
9170 char_u buf[51];
9171 foldinfo_T foldinfo;
9172 int fold_count;
9173#endif
9174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->v_type = VAR_STRING;
9176 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009177#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 /* treat illegal types and illegal string values for {lnum} the same */
9180 if (lnum < 0)
9181 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009182 fold_count = foldedCount(curwin, lnum, &foldinfo);
9183 if (fold_count > 0)
9184 {
9185 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9186 &foldinfo, buf);
9187 if (text == buf)
9188 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009190 }
9191#endif
9192}
9193
9194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 * "foreground()" function
9196 */
9197/*ARGSUSED*/
9198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009200 typval_T *argvars;
9201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204#ifdef FEAT_GUI
9205 if (gui.in_use)
9206 gui_mch_set_foreground();
9207#else
9208# ifdef WIN32
9209 win32_set_foreground();
9210# endif
9211#endif
9212}
9213
9214/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009215 * "function()" function
9216 */
9217/*ARGSUSED*/
9218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009220 typval_T *argvars;
9221 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009222{
9223 char_u *s;
9224
Bram Moolenaara7043832005-01-21 11:56:39 +00009225 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009227 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009228 EMSG2(_(e_invarg2), s);
9229 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009230 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009231 else
9232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009233 rettv->vval.v_string = vim_strsave(s);
9234 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009235 }
9236}
9237
9238/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009239 * "garbagecollect()" function
9240 */
9241/*ARGSUSED*/
9242 static void
9243f_garbagecollect(argvars, rettv)
9244 typval_T *argvars;
9245 typval_T *rettv;
9246{
9247 garbage_collect();
9248}
9249
9250/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009251 * "get()" function
9252 */
9253 static void
9254f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009255 typval_T *argvars;
9256 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009257{
Bram Moolenaar33570922005-01-25 22:26:29 +00009258 listitem_T *li;
9259 list_T *l;
9260 dictitem_T *di;
9261 dict_T *d;
9262 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009263
Bram Moolenaare9a41262005-01-15 22:18:47 +00009264 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009265 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009266 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009268 int error = FALSE;
9269
9270 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9271 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009272 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009273 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009274 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009275 else if (argvars[0].v_type == VAR_DICT)
9276 {
9277 if ((d = argvars[0].vval.v_dict) != NULL)
9278 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009279 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 if (di != NULL)
9281 tv = &di->di_tv;
9282 }
9283 }
9284 else
9285 EMSG2(_(e_listdictarg), "get()");
9286
9287 if (tv == NULL)
9288 {
9289 if (argvars[2].v_type == VAR_UNKNOWN)
9290 rettv->vval.v_number = 0;
9291 else
9292 copy_tv(&argvars[2], rettv);
9293 }
9294 else
9295 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009296}
9297
Bram Moolenaar342337a2005-07-21 21:11:17 +00009298static 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 +00009299
9300/*
9301 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009302 * Return a range (from start to end) of lines in rettv from the specified
9303 * buffer.
9304 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009305 */
9306 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009307get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009308 buf_T *buf;
9309 linenr_T start;
9310 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009311 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009312 typval_T *rettv;
9313{
9314 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009315 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009316
Bram Moolenaar342337a2005-07-21 21:11:17 +00009317 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009318 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009319 l = list_alloc();
9320 if (l == NULL)
9321 return;
9322
9323 rettv->vval.v_list = l;
9324 rettv->v_type = VAR_LIST;
9325 ++l->lv_refcount;
9326 }
9327 else
9328 rettv->vval.v_number = 0;
9329
9330 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9331 return;
9332
9333 if (!retlist)
9334 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009335 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9336 p = ml_get_buf(buf, start, FALSE);
9337 else
9338 p = (char_u *)"";
9339
9340 rettv->v_type = VAR_STRING;
9341 rettv->vval.v_string = vim_strsave(p);
9342 }
9343 else
9344 {
9345 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009346 return;
9347
9348 if (start < 1)
9349 start = 1;
9350 if (end > buf->b_ml.ml_line_count)
9351 end = buf->b_ml.ml_line_count;
9352 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009353 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9354 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009355 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009356 }
9357}
9358
9359/*
9360 * "getbufline()" function
9361 */
9362 static void
9363f_getbufline(argvars, rettv)
9364 typval_T *argvars;
9365 typval_T *rettv;
9366{
9367 linenr_T lnum;
9368 linenr_T end;
9369 buf_T *buf;
9370
9371 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9372 ++emsg_off;
9373 buf = get_buf_tv(&argvars[0]);
9374 --emsg_off;
9375
Bram Moolenaar661b1822005-07-28 22:36:45 +00009376 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009377 if (argvars[2].v_type == VAR_UNKNOWN)
9378 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009379 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009380 end = get_tv_lnum_buf(&argvars[2], buf);
9381
Bram Moolenaar342337a2005-07-21 21:11:17 +00009382 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009383}
9384
Bram Moolenaar0d660222005-01-07 21:51:51 +00009385/*
9386 * "getbufvar()" function
9387 */
9388 static void
9389f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009390 typval_T *argvars;
9391 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009392{
9393 buf_T *buf;
9394 buf_T *save_curbuf;
9395 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9399 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009400 ++emsg_off;
9401 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009402
9403 rettv->v_type = VAR_STRING;
9404 rettv->vval.v_string = NULL;
9405
9406 if (buf != NULL && varname != NULL)
9407 {
9408 if (*varname == '&') /* buffer-local-option */
9409 {
9410 /* set curbuf to be our buf, temporarily */
9411 save_curbuf = curbuf;
9412 curbuf = buf;
9413
9414 get_option_tv(&varname, rettv, TRUE);
9415
9416 /* restore previous notion of curbuf */
9417 curbuf = save_curbuf;
9418 }
9419 else
9420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 if (*varname == NUL)
9422 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9423 * scope prefix before the NUL byte is required by
9424 * find_var_in_ht(). */
9425 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009426 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009427 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009428 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009429 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009430 }
9431 }
9432
9433 --emsg_off;
9434}
9435
9436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 * "getchar()" function
9438 */
9439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009441 typval_T *argvars;
9442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443{
9444 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009445 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446
9447 ++no_mapping;
9448 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009449 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 /* getchar(): blocking wait. */
9451 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 /* getchar(1): only check if char avail */
9454 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009455 else if (error || vpeekc() == NUL)
9456 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 n = 0;
9458 else
9459 /* getchar(0) and char avail: return char */
9460 n = safe_vgetc();
9461 --no_mapping;
9462 --allow_keys;
9463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 if (IS_SPECIAL(n) || mod_mask != 0)
9466 {
9467 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9468 int i = 0;
9469
9470 /* Turn a special key into three bytes, plus modifier. */
9471 if (mod_mask != 0)
9472 {
9473 temp[i++] = K_SPECIAL;
9474 temp[i++] = KS_MODIFIER;
9475 temp[i++] = mod_mask;
9476 }
9477 if (IS_SPECIAL(n))
9478 {
9479 temp[i++] = K_SPECIAL;
9480 temp[i++] = K_SECOND(n);
9481 temp[i++] = K_THIRD(n);
9482 }
9483#ifdef FEAT_MBYTE
9484 else if (has_mbyte)
9485 i += (*mb_char2bytes)(n, temp + i);
9486#endif
9487 else
9488 temp[i++] = n;
9489 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->v_type = VAR_STRING;
9491 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 }
9493}
9494
9495/*
9496 * "getcharmod()" function
9497 */
9498/*ARGSUSED*/
9499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009501 typval_T *argvars;
9502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505}
9506
9507/*
9508 * "getcmdline()" function
9509 */
9510/*ARGSUSED*/
9511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009513 typval_T *argvars;
9514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 rettv->v_type = VAR_STRING;
9517 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518}
9519
9520/*
9521 * "getcmdpos()" function
9522 */
9523/*ARGSUSED*/
9524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530}
9531
9532/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009533 * "getcmdtype()" function
9534 */
9535/*ARGSUSED*/
9536 static void
9537f_getcmdtype(argvars, rettv)
9538 typval_T *argvars;
9539 typval_T *rettv;
9540{
9541 rettv->v_type = VAR_STRING;
9542 rettv->vval.v_string = alloc(2);
9543 if (rettv->vval.v_string != NULL)
9544 {
9545 rettv->vval.v_string[0] = get_cmdline_type();
9546 rettv->vval.v_string[1] = NUL;
9547 }
9548}
9549
9550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 * "getcwd()" function
9552 */
9553/*ARGSUSED*/
9554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009556 typval_T *argvars;
9557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
9559 char_u cwd[MAXPATHL];
9560
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 else
9565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009568 if (rettv->vval.v_string != NULL)
9569 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570#endif
9571 }
9572}
9573
9574/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009575 * "getfontname()" function
9576 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009577/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009580 typval_T *argvars;
9581 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583 rettv->v_type = VAR_STRING;
9584 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009585#ifdef FEAT_GUI
9586 if (gui.in_use)
9587 {
9588 GuiFont font;
9589 char_u *name = NULL;
9590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009592 {
9593 /* Get the "Normal" font. Either the name saved by
9594 * hl_set_font_name() or from the font ID. */
9595 font = gui.norm_font;
9596 name = hl_get_font_name();
9597 }
9598 else
9599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009601 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9602 return;
9603 font = gui_mch_get_font(name, FALSE);
9604 if (font == NOFONT)
9605 return; /* Invalid font name, return empty string. */
9606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009607 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009608 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009609 gui_mch_free_font(font);
9610 }
9611#endif
9612}
9613
9614/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009615 * "getfperm({fname})" function
9616 */
9617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009619 typval_T *argvars;
9620 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009621{
9622 char_u *fname;
9623 struct stat st;
9624 char_u *perm = NULL;
9625 char_u flags[] = "rwx";
9626 int i;
9627
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009631 if (mch_stat((char *)fname, &st) >= 0)
9632 {
9633 perm = vim_strsave((char_u *)"---------");
9634 if (perm != NULL)
9635 {
9636 for (i = 0; i < 9; i++)
9637 {
9638 if (st.st_mode & (1 << (8 - i)))
9639 perm[i] = flags[i % 3];
9640 }
9641 }
9642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009644}
9645
9646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 * "getfsize({fname})" function
9648 */
9649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009651 typval_T *argvars;
9652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
9654 char_u *fname;
9655 struct stat st;
9656
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660
9661 if (mch_stat((char *)fname, &st) >= 0)
9662 {
9663 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 }
9668 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670}
9671
9672/*
9673 * "getftime({fname})" function
9674 */
9675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 typval_T *argvars;
9678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679{
9680 char_u *fname;
9681 struct stat st;
9682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684
9685 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689}
9690
9691/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009692 * "getftype({fname})" function
9693 */
9694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009696 typval_T *argvars;
9697 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009698{
9699 char_u *fname;
9700 struct stat st;
9701 char_u *type = NULL;
9702 char *t;
9703
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009707 if (mch_lstat((char *)fname, &st) >= 0)
9708 {
9709#ifdef S_ISREG
9710 if (S_ISREG(st.st_mode))
9711 t = "file";
9712 else if (S_ISDIR(st.st_mode))
9713 t = "dir";
9714# ifdef S_ISLNK
9715 else if (S_ISLNK(st.st_mode))
9716 t = "link";
9717# endif
9718# ifdef S_ISBLK
9719 else if (S_ISBLK(st.st_mode))
9720 t = "bdev";
9721# endif
9722# ifdef S_ISCHR
9723 else if (S_ISCHR(st.st_mode))
9724 t = "cdev";
9725# endif
9726# ifdef S_ISFIFO
9727 else if (S_ISFIFO(st.st_mode))
9728 t = "fifo";
9729# endif
9730# ifdef S_ISSOCK
9731 else if (S_ISSOCK(st.st_mode))
9732 t = "fifo";
9733# endif
9734 else
9735 t = "other";
9736#else
9737# ifdef S_IFMT
9738 switch (st.st_mode & S_IFMT)
9739 {
9740 case S_IFREG: t = "file"; break;
9741 case S_IFDIR: t = "dir"; break;
9742# ifdef S_IFLNK
9743 case S_IFLNK: t = "link"; break;
9744# endif
9745# ifdef S_IFBLK
9746 case S_IFBLK: t = "bdev"; break;
9747# endif
9748# ifdef S_IFCHR
9749 case S_IFCHR: t = "cdev"; break;
9750# endif
9751# ifdef S_IFIFO
9752 case S_IFIFO: t = "fifo"; break;
9753# endif
9754# ifdef S_IFSOCK
9755 case S_IFSOCK: t = "socket"; break;
9756# endif
9757 default: t = "other";
9758 }
9759# else
9760 if (mch_isdir(fname))
9761 t = "dir";
9762 else
9763 t = "file";
9764# endif
9765#endif
9766 type = vim_strsave((char_u *)t);
9767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009769}
9770
9771/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009772 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009773 */
9774 static void
9775f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009776 typval_T *argvars;
9777 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009778{
9779 linenr_T lnum;
9780 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009781 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009782
9783 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009784 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009785 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009786 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009787 retlist = FALSE;
9788 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009789 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009790 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009791 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009792 retlist = TRUE;
9793 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009794
Bram Moolenaar342337a2005-07-21 21:11:17 +00009795 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009796}
9797
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009798static void get_qf_ll_ist __ARGS((win_T *wp, typval_T *rettv));
9799
Bram Moolenaar0d660222005-01-07 21:51:51 +00009800/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009801 * Shared by getqflist() and getloclist() functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009802 */
Bram Moolenaar2641f772005-03-25 21:58:17 +00009803 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009804get_qf_ll_ist(wp, rettv)
9805 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009806 typval_T *rettv;
9807{
9808#ifdef FEAT_QUICKFIX
9809 list_T *l;
9810#endif
9811
9812 rettv->vval.v_number = FALSE;
9813#ifdef FEAT_QUICKFIX
9814 l = list_alloc();
9815 if (l != NULL)
9816 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009817 rettv->vval.v_list = l;
9818 rettv->v_type = VAR_LIST;
9819 ++l->lv_refcount;
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009820 (void)get_errorlist(wp, l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009821 }
9822#endif
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009823
9824}
9825
9826/*
9827 * "getloclist()" function
9828 */
9829/*ARGSUSED*/
9830 static void
9831f_getloclist(argvars, rettv)
9832 typval_T *argvars;
9833 typval_T *rettv;
9834{
9835 win_T *win;
9836
9837 rettv->vval.v_number = FALSE;
9838
9839 win = find_win_by_nr(&argvars[0]);
9840 if (win != NULL)
9841 get_qf_ll_ist(win, rettv);
9842}
9843
9844/*
9845 * "getqflist()" function
9846 */
9847/*ARGSUSED*/
9848 static void
9849f_getqflist(argvars, rettv)
9850 typval_T *argvars;
9851 typval_T *rettv;
9852{
9853 get_qf_ll_ist(NULL, rettv);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009854}
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) */