blob: 9038195cb4d63bc7d9541ac6277f7d9206db00dd [file] [log] [blame]
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003 * MzScheme interface by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar75676462013-01-30 14:55:42 +01004 * Based on work by Brent Fulgham <bfulgham@debian.org>
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005 * (Based on lots of help from Matthew Flatt)
6 *
7 * This consists of six parts:
8 * 1. MzScheme interpreter main program
9 * 2. Routines that handle the external interface between MzScheme and
10 * Vim.
11 * 3. MzScheme input/output handlers: writes output via [e]msg().
12 * 4. Implementation of the Vim Features for MzScheme
13 * 5. Vim Window-related Manipulation Functions.
14 * 6. Vim Buffer-related Manipulation Functions
15 *
16 * NOTES
17 * 1. Memory, allocated with scheme_malloc*, need not to be freed explicitly,
18 * garbage collector will do it self
19 * 2. Requires at least NORMAL features. I can't imagine why one may want
20 * to build with SMALL or TINY features but with MzScheme interface.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000021 * 3. I don't use K&R-style functions. Anyways, MzScheme headers are ANSI.
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022 */
23
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024#include "vim.h"
Bram Moolenaar049377e2007-05-12 15:32:12 +000025
Bram Moolenaar325b7a22004-07-05 15:58:32 +000026#include "if_mzsch.h"
27
Bram Moolenaar76b92b22006-03-24 22:46:53 +000028/* Only do the following when the feature is enabled. Needed for "make
29 * depend". */
30#if defined(FEAT_MZSCHEME) || defined(PROTO)
31
Bram Moolenaar4e640bd2016-01-16 16:20:38 +010032/*
33 * scheme_register_tls_space is only available on 32-bit Windows until
34 * racket-6.3. See
35 * http://docs.racket-lang.org/inside/im_memoryalloc.html?q=scheme_register_tls_space
36 */
37#if MZSCHEME_VERSION_MAJOR >= 500 && defined(WIN32) \
38 && defined(USE_THREAD_LOCAL) \
39 && (!defined(_WIN64) || MZSCHEME_VERSION_MAJOR >= 603)
40# define HAVE_TLS_SPACE 1
41#endif
42
43/*
44 * Since version 4.x precise GC requires trampolined startup.
45 * Futures and places in version 5.x need it too.
46 */
47#if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR >= 400 \
48 || MZSCHEME_VERSION_MAJOR >= 500 \
49 && (defined(MZ_USE_FUTURES) || defined(MZ_USE_PLACES))
50# define TRAMPOLINED_MZVIM_STARTUP
51#endif
52
Bram Moolenaar325b7a22004-07-05 15:58:32 +000053/* Base data structures */
54#define SCHEME_VIMBUFFERP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_buffer_type)
55#define SCHEME_VIMWINDOWP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_window_type)
56
57typedef struct
58{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000059 Scheme_Object so;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000060 buf_T *buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000061} vim_mz_buffer;
62
63#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
64
65typedef struct
66{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000067 Scheme_Object so;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000068 win_T *win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000069} vim_mz_window;
70
71#define INVALID_WINDOW_VALUE ((win_T *)(-1))
72
73/*
74 * Prims that form MzScheme Vim interface
75 */
76typedef struct
77{
78 Scheme_Closed_Prim *prim;
79 char *name;
80 int mina; /* arity information */
81 int maxa;
82} Vim_Prim;
83
84typedef struct
85{
86 char *name;
87 Scheme_Object *port;
88} Port_Info;
89
Bram Moolenaar325b7a22004-07-05 15:58:32 +000090/*
91 *========================================================================
92 * Vim-Control Commands
93 *========================================================================
94 */
95/*
96 *========================================================================
97 * Utility functions for the vim/mzscheme interface
98 *========================================================================
99 */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000100#ifdef HAVE_SANDBOX
101static Scheme_Object *sandbox_file_guard(int, Scheme_Object **);
102static Scheme_Object *sandbox_network_guard(int, Scheme_Object **);
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000103static void sandbox_check(void);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000104#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000105/* Buffer-related commands */
106static Scheme_Object *buffer_new(buf_T *buf);
107static Scheme_Object *get_buffer_by_name(void *, int, Scheme_Object **);
108static Scheme_Object *get_buffer_by_num(void *, int, Scheme_Object **);
109static Scheme_Object *get_buffer_count(void *, int, Scheme_Object **);
110static Scheme_Object *get_buffer_line(void *, int, Scheme_Object **);
111static Scheme_Object *get_buffer_line_list(void *, int, Scheme_Object **);
112static Scheme_Object *get_buffer_name(void *, int, Scheme_Object **);
113static Scheme_Object *get_buffer_num(void *, int, Scheme_Object **);
114static Scheme_Object *get_buffer_size(void *, int, Scheme_Object **);
115static Scheme_Object *get_curr_buffer(void *, int, Scheme_Object **);
116static Scheme_Object *get_next_buffer(void *, int, Scheme_Object **);
117static Scheme_Object *get_prev_buffer(void *, int, Scheme_Object **);
118static Scheme_Object *mzscheme_open_buffer(void *, int, Scheme_Object **);
119static Scheme_Object *set_buffer_line(void *, int, Scheme_Object **);
120static Scheme_Object *set_buffer_line_list(void *, int, Scheme_Object **);
121static Scheme_Object *insert_buffer_line_list(void *, int, Scheme_Object **);
122static Scheme_Object *get_range_start(void *, int, Scheme_Object **);
123static Scheme_Object *get_range_end(void *, int, Scheme_Object **);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000124static vim_mz_buffer *get_vim_curr_buffer(void);
125
126/* Window-related commands */
127static Scheme_Object *window_new(win_T *win);
128static Scheme_Object *get_curr_win(void *, int, Scheme_Object **);
129static Scheme_Object *get_window_count(void *, int, Scheme_Object **);
130static Scheme_Object *get_window_by_num(void *, int, Scheme_Object **);
131static Scheme_Object *get_window_num(void *, int, Scheme_Object **);
132static Scheme_Object *get_window_buffer(void *, int, Scheme_Object **);
133static Scheme_Object *get_window_height(void *, int, Scheme_Object **);
134static Scheme_Object *set_window_height(void *, int, Scheme_Object **);
135#ifdef FEAT_VERTSPLIT
136static Scheme_Object *get_window_width(void *, int, Scheme_Object **);
137static Scheme_Object *set_window_width(void *, int, Scheme_Object **);
138#endif
139static Scheme_Object *get_cursor(void *, int, Scheme_Object **);
140static Scheme_Object *set_cursor(void *, int, Scheme_Object **);
141static Scheme_Object *get_window_list(void *, int, Scheme_Object **);
142static vim_mz_window *get_vim_curr_window(void);
143
144/* Vim-related commands */
145static Scheme_Object *mzscheme_beep(void *, int, Scheme_Object **);
146static Scheme_Object *get_option(void *, int, Scheme_Object **);
147static Scheme_Object *set_option(void *, int, Scheme_Object **);
148static Scheme_Object *vim_command(void *, int, Scheme_Object **);
149static Scheme_Object *vim_eval(void *, int, Scheme_Object **);
150static Scheme_Object *vim_bufferp(void *data, int, Scheme_Object **);
151static Scheme_Object *vim_windowp(void *data, int, Scheme_Object **);
152static Scheme_Object *vim_buffer_validp(void *data, int, Scheme_Object **);
153static Scheme_Object *vim_window_validp(void *data, int, Scheme_Object **);
154
155/*
156 *========================================================================
157 * Internal Function Prototypes
158 *========================================================================
159 */
160static int vim_error_check(void);
161static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100162static int startup_mzscheme(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000163static char *string_to_line(Scheme_Object *obj);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100164#if MZSCHEME_VERSION_MAJOR >= 501
Bram Moolenaar75676462013-01-30 14:55:42 +0100165# define OUTPUT_LEN_TYPE intptr_t
166#else
167# define OUTPUT_LEN_TYPE long
168#endif
169static void do_output(char *mesg, OUTPUT_LEN_TYPE len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000170static void do_printf(char *format, ...);
171static void do_flush(void);
172static Scheme_Object *_apply_thunk_catch_exceptions(
173 Scheme_Object *, Scheme_Object **);
174static Scheme_Object *extract_exn_message(Scheme_Object *v);
175static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
176static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000177static void register_vim_exn(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000178static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
179 int argc, Scheme_Object **argv);
180static vim_mz_window *get_window_arg(const char *fname, int argnum,
181 int argc, Scheme_Object **argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000182static int line_in_range(linenr_T, buf_T *);
183static void check_line_range(linenr_T, buf_T *);
184static void mz_fix_cursor(int lo, int hi, int extra);
185
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000186static int eval_with_exn_handling(void *, Scheme_Closed_Prim *,
187 Scheme_Object **ret);
188static void make_modules(void);
189static void init_exn_catching_apply(void);
190static int mzscheme_env_main(Scheme_Env *env, int argc, char **argv);
191static int mzscheme_init(void);
192#ifdef FEAT_EVAL
Bram Moolenaar75676462013-01-30 14:55:42 +0100193static Scheme_Object *vim_to_mzscheme(typval_T *vim_value);
194static Scheme_Object *vim_to_mzscheme_impl(typval_T *vim_value, int depth,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000195 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100196static int mzscheme_to_vim(Scheme_Object *obj, typval_T *tv);
197static int mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100198 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100199static Scheme_Object *vim_funcref(void *data, int argc, Scheme_Object **argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000200#endif
201
202#ifdef MZ_PRECISE_GC
Bram Moolenaar64404472010-06-26 06:24:45 +0200203static int buffer_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000204{
205 return gcBYTES_TO_WORDS(sizeof(vim_mz_buffer));
206}
207static int buffer_mark_proc(void *obj)
208{
209 return buffer_size_proc(obj);
210}
211static int buffer_fixup_proc(void *obj)
212{
Bram Moolenaar75676462013-01-30 14:55:42 +0100213 /* apparently not needed as the object will be uncollectable while
214 * the buffer is alive
215 */
216 /*
217 vim_mz_buffer* buf = (vim_mz_buffer*) obj;
218 buf->buf->b_mzscheme_ref = GC_fixup_self(obj);
219 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000220 return buffer_size_proc(obj);
221}
Bram Moolenaar64404472010-06-26 06:24:45 +0200222static int window_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000223{
224 return gcBYTES_TO_WORDS(sizeof(vim_mz_window));
225}
226static int window_mark_proc(void *obj)
227{
228 return window_size_proc(obj);
229}
230static int window_fixup_proc(void *obj)
231{
Bram Moolenaar75676462013-01-30 14:55:42 +0100232 /* apparently not needed as the object will be uncollectable while
233 * the window is alive
234 */
235 /*
236 vim_mz_window* win = (vim_mz_window*) obj;
237 win->win->w_mzscheme_ref = GC_fixup_self(obj);
238 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000239 return window_size_proc(obj);
240}
Bram Moolenaar75676462013-01-30 14:55:42 +0100241/* with precise GC, w_mzscheme_ref and b_mzscheme_ref are immobile boxes
242 * containing pointers to a window/buffer
243 * with conservative GC these are simply pointers*/
244# define WINDOW_REF(win) *(vim_mz_window **)((win)->w_mzscheme_ref)
245# define BUFFER_REF(buf) *(vim_mz_buffer **)((buf)->b_mzscheme_ref)
246#else
247# define WINDOW_REF(win) (vim_mz_window *)((win)->w_mzscheme_ref)
248# define BUFFER_REF(buf) (vim_mz_buffer *)((buf)->b_mzscheme_ref)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000249#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000250
Bram Moolenaar33570922005-01-25 22:26:29 +0000251#ifdef DYNAMIC_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +0000252static Scheme_Object *dll_scheme_eof;
253static Scheme_Object *dll_scheme_false;
254static Scheme_Object *dll_scheme_void;
255static Scheme_Object *dll_scheme_null;
256static Scheme_Object *dll_scheme_true;
257
258static Scheme_Thread **dll_scheme_current_thread_ptr;
259
260static void (**dll_scheme_console_printf_ptr)(char *str, ...);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100261static void (**dll_scheme_console_output_ptr)(char *str, OUTPUT_LEN_TYPE len);
Bram Moolenaar33570922005-01-25 22:26:29 +0000262static void (**dll_scheme_notify_multithread_ptr)(int on);
263
264static void *(*dll_GC_malloc)(size_t size_in_bytes);
265static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
266static Scheme_Env *(*dll_scheme_basic_env)(void);
267static void (*dll_scheme_check_threads)(void);
268static void (*dll_scheme_register_static)(void *ptr, long size);
269static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
270static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
271 Scheme_Env *env);
272static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
273 Scheme_Object *val, Scheme_Env *env);
274static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
275 Scheme_Object **rands);
276static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000277# if MZSCHEME_VERSION_MAJOR >= 299
278static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100279static Scheme_Object *(*dll_scheme_make_path)(const char *chars);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000280# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000281static void (*dll_scheme_close_input_port)(Scheme_Object *port);
282static void (*dll_scheme_count_lines)(Scheme_Object *port);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000283#if MZSCHEME_VERSION_MAJOR < 360
Bram Moolenaar33570922005-01-25 22:26:29 +0000284static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000285#else
286static Scheme_Object *(*dll_scheme_current_continuation_marks)(Scheme_Object *prompt_tag);
287#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000288static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100289static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, OUTPUT_LEN_TYPE *len);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000290static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
Bram Moolenaar33570922005-01-25 22:26:29 +0000291static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
292 int _num_rands, Scheme_Object **rands, int val);
293static void (*dll_scheme_dont_gc_ptr)(void *p);
294static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
295static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
296 Scheme_Env *env);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000297static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 Scheme_Env *env, int all);
299static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000300# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000301static char *(*dll_scheme_format)(char *format, int flen, int argc,
302 Scheme_Object **argv, long *rlen);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000303# else
304static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100305 Scheme_Object **argv, OUTPUT_LEN_TYPE *rlen);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000306static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000307# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000308static void (*dll_scheme_gc_ptr_ok)(void *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000309# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000310static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
311 long *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000312# else
313static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100314 OUTPUT_LEN_TYPE *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000315# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000316static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
317static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
318 Scheme_Env *env);
319static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
320 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
321 mzshort maxa);
322static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000323static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
Bram Moolenaar33570922005-01-25 22:26:29 +0000324 Scheme_Object *cdr);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000325static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
326 const char *name, mzshort mina, mzshort maxa);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000327# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000328static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
329static Scheme_Object *(*dll_scheme_make_string_output_port)();
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000330# else
331static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
332static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
333# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000334static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
335 int argc, Scheme_Object **argv);
336static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
337 Scheme_Object *field_names, int flags, int *count_out);
338static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
339 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
340 int num_uninit_fields, Scheme_Object *uninit_val,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000341 Scheme_Object *properties
342# if MZSCHEME_VERSION_MAJOR >= 299
343 , Scheme_Object *guard
344# endif
345 );
Bram Moolenaar33570922005-01-25 22:26:29 +0000346static Scheme_Object **(*dll_scheme_make_struct_values)(
347 Scheme_Object *struct_type, Scheme_Object **names, int count,
348 int flags);
349static Scheme_Type (*dll_scheme_make_type)(const char *name);
350static Scheme_Object *(*dll_scheme_make_vector)(int size,
351 Scheme_Object *fill);
352static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
353static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
354 const char *who);
355static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
356 Scheme_Env *for_env);
357static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
358static void (*dll_scheme_raise)(Scheme_Object *exn);
359static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
360static void (*dll_scheme_signal_error)(const char *msg, ...);
361static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
362 int which, int argc, Scheme_Object **argv);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000363# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000364static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000365 Scheme_Object *o);
366static Scheme_Config *(*dll_scheme_current_config)(void);
367static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
368 (Scheme_Object *s);
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000369static Scheme_Object *(*dll_scheme_char_string_to_path)
370 (Scheme_Object *s);
Bram Moolenaar75676462013-01-30 14:55:42 +0100371static void *(*dll_scheme_set_collects_path)(Scheme_Object *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000372# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000373static Scheme_Hash_Table *(*dll_scheme_make_hash_table)(int type);
374static void (*dll_scheme_hash_set)(Scheme_Hash_Table *table,
375 Scheme_Object *key, Scheme_Object *value);
376static Scheme_Object *(*dll_scheme_hash_get)(Scheme_Hash_Table *table,
377 Scheme_Object *key);
378static Scheme_Object *(*dll_scheme_make_double)(double d);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000379static Scheme_Object *(*dll_scheme_make_sized_byte_string)(char *chars,
380 long len, int copy);
381static Scheme_Object *(*dll_scheme_namespace_require)(Scheme_Object *req);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100382static Scheme_Object *(*dll_scheme_dynamic_wind)(void (*pre)(void *), Scheme_Object *(* volatile act)(void *), void (* volatile post)(void *), Scheme_Object *(*jmp_handler)(void *), void * volatile data);
383# ifdef MZ_PRECISE_GC
384static void *(*dll_GC_malloc_one_tagged)(size_t size_in_bytes);
385static void (*dll_GC_register_traversers)(short tag, Size_Proc size, Mark_Proc mark, Fixup_Proc fixup, int is_constant_size, int is_atomic);
386# endif
387# if MZSCHEME_VERSION_MAJOR >= 400
388static void (*dll_scheme_init_collection_paths)(Scheme_Env *global_env, Scheme_Object *extra_dirs);
389static void **(*dll_scheme_malloc_immobile_box)(void *p);
390static void (*dll_scheme_free_immobile_box)(void **b);
391# endif
392# if MZSCHEME_VERSION_MAJOR >= 500
393# ifdef TRAMPOLINED_MZVIM_STARTUP
394static int (*dll_scheme_main_setup)(int no_auto_statics, Scheme_Env_Main _main, int argc, char **argv);
395# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
396static void (*dll_scheme_register_tls_space)(void *tls_space, int _tls_index);
397# endif
398# endif
399# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
400static Thread_Local_Variables *(*dll_scheme_external_get_thread_local_variables)(void);
401# endif
402# endif
403# if MZSCHEME_VERSION_MAJOR >= 600
404static void (*dll_scheme_embedded_load)(intptr_t len, const char *s, int predefined);
405static void (*dll_scheme_register_embedded_load)(intptr_t len, const char *s);
406static void (*dll_scheme_set_config_path)(Scheme_Object *p);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000407# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000408
409/* arrays are imported directly */
410# define scheme_eof dll_scheme_eof
411# define scheme_false dll_scheme_false
412# define scheme_void dll_scheme_void
413# define scheme_null dll_scheme_null
414# define scheme_true dll_scheme_true
415
416/* pointers are GetProceAddress'ed as pointers to pointer */
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100417#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
418# define scheme_current_thread (*dll_scheme_current_thread_ptr)
419# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000420# define scheme_console_printf (*dll_scheme_console_printf_ptr)
421# define scheme_console_output (*dll_scheme_console_output_ptr)
422# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
423
424/* and functions in a usual way */
425# define GC_malloc dll_GC_malloc
426# define GC_malloc_atomic dll_GC_malloc_atomic
427
428# define scheme_add_global dll_scheme_add_global
429# define scheme_add_global_symbol dll_scheme_add_global_symbol
430# define scheme_apply dll_scheme_apply
431# define scheme_basic_env dll_scheme_basic_env
432# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000433# if MZSCHEME_VERSION_MAJOR >= 299
434# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100435# define scheme_make_path dll_scheme_make_path
Bram Moolenaar555b2802005-05-19 21:08:39 +0000436# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000437# define scheme_check_threads dll_scheme_check_threads
438# define scheme_close_input_port dll_scheme_close_input_port
439# define scheme_count_lines dll_scheme_count_lines
440# define scheme_current_continuation_marks \
441 dll_scheme_current_continuation_marks
442# define scheme_display dll_scheme_display
443# define scheme_display_to_string dll_scheme_display_to_string
444# define scheme_do_eval dll_scheme_do_eval
445# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000446# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000447# define scheme_eval dll_scheme_eval
448# define scheme_eval_string dll_scheme_eval_string
449# define scheme_eval_string_all dll_scheme_eval_string_all
450# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000451# if MZSCHEME_VERSION_MAJOR < 299
452# define scheme_format dll_scheme_format
453# else
454# define scheme_format_utf8 dll_scheme_format_utf8
455# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000457# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100458# define scheme_get_sized_byte_string_output dll_scheme_get_sized_string_output
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000459# else
460# define scheme_get_sized_byte_string_output \
461 dll_scheme_get_sized_byte_string_output
Bram Moolenaar75676462013-01-30 14:55:42 +0100462# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000463# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000464# define scheme_intern_symbol dll_scheme_intern_symbol
465# define scheme_lookup_global dll_scheme_lookup_global
466# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
467# define scheme_make_integer_value dll_scheme_make_integer_value
Bram Moolenaar33570922005-01-25 22:26:29 +0000468# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000469# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000470# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100471# define scheme_make_byte_string dll_scheme_make_string
472# define scheme_make_byte_string_output_port dll_scheme_make_string_output_port
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000473# else
474# define scheme_make_byte_string dll_scheme_make_byte_string
475# define scheme_make_byte_string_output_port \
476 dll_scheme_make_byte_string_output_port
477# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000478# define scheme_make_struct_instance dll_scheme_make_struct_instance
479# define scheme_make_struct_names dll_scheme_make_struct_names
480# define scheme_make_struct_type dll_scheme_make_struct_type
481# define scheme_make_struct_values dll_scheme_make_struct_values
482# define scheme_make_type dll_scheme_make_type
483# define scheme_make_vector dll_scheme_make_vector
484# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
485# define scheme_open_input_file dll_scheme_open_input_file
486# define scheme_primitive_module dll_scheme_primitive_module
487# define scheme_proper_list_length dll_scheme_proper_list_length
488# define scheme_raise dll_scheme_raise
489# define scheme_read dll_scheme_read
490# define scheme_register_static dll_scheme_register_static
491# define scheme_set_stack_base dll_scheme_set_stack_base
492# define scheme_signal_error dll_scheme_signal_error
493# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000494# if MZSCHEME_VERSION_MAJOR >= 299
495# define scheme_set_param dll_scheme_set_param
496# define scheme_current_config dll_scheme_current_config
497# define scheme_char_string_to_byte_string \
498 dll_scheme_char_string_to_byte_string
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000499# define scheme_char_string_to_path \
500 dll_scheme_char_string_to_path
Bram Moolenaar75676462013-01-30 14:55:42 +0100501# define scheme_set_collects_path dll_scheme_set_collects_path
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000502# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000503# define scheme_make_hash_table dll_scheme_make_hash_table
504# define scheme_hash_set dll_scheme_hash_set
505# define scheme_hash_get dll_scheme_hash_get
506# define scheme_make_double dll_scheme_make_double
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100507# define scheme_make_sized_byte_string dll_scheme_make_sized_byte_string
508# define scheme_namespace_require dll_scheme_namespace_require
509# define scheme_dynamic_wind dll_scheme_dynamic_wind
510# ifdef MZ_PRECISE_GC
511# define GC_malloc_one_tagged dll_GC_malloc_one_tagged
512# define GC_register_traversers dll_GC_register_traversers
513# endif
514# if MZSCHEME_VERSION_MAJOR >= 400
515# ifdef TRAMPOLINED_MZVIM_STARTUP
516# define scheme_main_setup dll_scheme_main_setup
517# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
518# define scheme_register_tls_space dll_scheme_register_tls_space
519# endif
520# endif
521# define scheme_init_collection_paths dll_scheme_init_collection_paths
522# define scheme_malloc_immobile_box dll_scheme_malloc_immobile_box
523# define scheme_free_immobile_box dll_scheme_free_immobile_box
524# endif
525# if MZSCHEME_VERSION_MAJOR >= 600
526# define scheme_embedded_load dll_scheme_embedded_load
527# define scheme_register_embedded_load dll_scheme_register_embedded_load
528# define scheme_set_config_path dll_scheme_set_config_path
529# endif
530
531# if MZSCHEME_VERSION_MAJOR >= 500
532# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
533/* define as function for macro in schshread.h */
534Thread_Local_Variables *
535scheme_external_get_thread_local_variables(void)
536{
537 return dll_scheme_external_get_thread_local_variables();
538}
539# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000540# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000541
542typedef struct
543{
544 char *name;
545 void **ptr;
546} Thunk_Info;
547
548static Thunk_Info mzgc_imports[] = {
549 {"GC_malloc", (void **)&dll_GC_malloc},
550 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
551 {NULL, NULL}};
552
553static Thunk_Info mzsch_imports[] = {
554 {"scheme_eof", (void **)&dll_scheme_eof},
555 {"scheme_false", (void **)&dll_scheme_false},
556 {"scheme_void", (void **)&dll_scheme_void},
557 {"scheme_null", (void **)&dll_scheme_null},
558 {"scheme_true", (void **)&dll_scheme_true},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100559#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
Bram Moolenaar33570922005-01-25 22:26:29 +0000560 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100561#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000562 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
563 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000564 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000565 (void **)&dll_scheme_notify_multithread_ptr},
566 {"scheme_add_global", (void **)&dll_scheme_add_global},
567 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
568 {"scheme_apply", (void **)&dll_scheme_apply},
569 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000570# if MZSCHEME_VERSION_MAJOR >= 299
571 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100572 {"scheme_make_path", (void **)&dll_scheme_make_path},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000573# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000574 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
575 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
576 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
577 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000578 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000579 (void **)&dll_scheme_current_continuation_marks},
580 {"scheme_display", (void **)&dll_scheme_display},
581 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
582 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
583 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000584 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000585 {"scheme_eval", (void **)&dll_scheme_eval},
586 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
587 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000588 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000589 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000590# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000591 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000592# else
593 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000594 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000595#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000596 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000597# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000598 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000599 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000601 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602 (void **)&dll_scheme_get_sized_byte_string_output},
603#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000604 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
605 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000607 (void **)&dll_scheme_make_closed_prim_w_arity},
608 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
Bram Moolenaar33570922005-01-25 22:26:29 +0000609 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000610 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000611# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000612 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000613 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000614 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000615# else
616 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000617 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000618 (void **)&dll_scheme_make_byte_string_output_port},
619# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000620 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000621 (void **)&dll_scheme_make_struct_instance},
622 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
623 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
624 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
625 {"scheme_make_type", (void **)&dll_scheme_make_type},
626 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
627 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
628 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
629 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
630 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
631 {"scheme_raise", (void **)&dll_scheme_raise},
632 {"scheme_read", (void **)&dll_scheme_read},
633 {"scheme_register_static", (void **)&dll_scheme_register_static},
634 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
635 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
636 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000637# if MZSCHEME_VERSION_MAJOR >= 299
638 {"scheme_set_param", (void **)&dll_scheme_set_param},
639 {"scheme_current_config", (void **)&dll_scheme_current_config},
640 {"scheme_char_string_to_byte_string",
641 (void **)&dll_scheme_char_string_to_byte_string},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000642 {"scheme_char_string_to_path", (void **)&dll_scheme_char_string_to_path},
Bram Moolenaar75676462013-01-30 14:55:42 +0100643 {"scheme_set_collects_path", (void **)&dll_scheme_set_collects_path},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000644# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000645 {"scheme_make_hash_table", (void **)&dll_scheme_make_hash_table},
646 {"scheme_hash_set", (void **)&dll_scheme_hash_set},
647 {"scheme_hash_get", (void **)&dll_scheme_hash_get},
648 {"scheme_make_double", (void **)&dll_scheme_make_double},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000649 {"scheme_make_sized_byte_string", (void **)&dll_scheme_make_sized_byte_string},
650 {"scheme_namespace_require", (void **)&dll_scheme_namespace_require},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100651 {"scheme_dynamic_wind", (void **)&dll_scheme_dynamic_wind},
652# ifdef MZ_PRECISE_GC
653 {"GC_malloc_one_tagged", (void **)&dll_GC_malloc_one_tagged},
654 {"GC_register_traversers", (void **)&dll_GC_register_traversers},
655# endif
656# if MZSCHEME_VERSION_MAJOR >= 400
657# ifdef TRAMPOLINED_MZVIM_STARTUP
658 {"scheme_main_setup", (void **)&dll_scheme_main_setup},
659# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
660 {"scheme_register_tls_space", (void **)&dll_scheme_register_tls_space},
661# endif
662# endif
663 {"scheme_init_collection_paths", (void **)&dll_scheme_init_collection_paths},
664 {"scheme_malloc_immobile_box", (void **)&dll_scheme_malloc_immobile_box},
665 {"scheme_free_immobile_box", (void **)&dll_scheme_free_immobile_box},
666# endif
667# if MZSCHEME_VERSION_MAJOR >= 500
668# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
669 {"scheme_external_get_thread_local_variables", (void **)&dll_scheme_external_get_thread_local_variables},
670# endif
671# endif
672# if MZSCHEME_VERSION_MAJOR >= 600
673 {"scheme_embedded_load", (void **)&dll_scheme_embedded_load},
674 {"scheme_register_embedded_load", (void **)&dll_scheme_register_embedded_load},
675 {"scheme_set_config_path", (void **)&dll_scheme_set_config_path},
676# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000677 {NULL, NULL}};
678
679static HINSTANCE hMzGC = 0;
680static HINSTANCE hMzSch = 0;
681
682static void dynamic_mzscheme_end(void);
683static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
684 int verbose);
685
686 static int
687mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
688{
689 Thunk_Info *thunk = NULL;
690
691 if (hMzGC && hMzSch)
692 return OK;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200693 hMzSch = vimLoadLib(sch_dll);
694 hMzGC = vimLoadLib(gc_dll);
Bram Moolenaar33570922005-01-25 22:26:29 +0000695
Bram Moolenaar33570922005-01-25 22:26:29 +0000696 if (!hMzGC)
697 {
698 if (verbose)
699 EMSG2(_(e_loadlib), gc_dll);
700 return FAIL;
701 }
702
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100703 if (!hMzSch)
704 {
705 if (verbose)
706 EMSG2(_(e_loadlib), sch_dll);
707 return FAIL;
708 }
709
Bram Moolenaar33570922005-01-25 22:26:29 +0000710 for (thunk = mzsch_imports; thunk->name; thunk++)
711 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000712 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000713 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
714 {
715 FreeLibrary(hMzSch);
716 hMzSch = 0;
717 FreeLibrary(hMzGC);
718 hMzGC = 0;
719 if (verbose)
720 EMSG2(_(e_loadfunc), thunk->name);
721 return FAIL;
722 }
723 }
724 for (thunk = mzgc_imports; thunk->name; thunk++)
725 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000726 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000727 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
728 {
729 FreeLibrary(hMzSch);
730 hMzSch = 0;
731 FreeLibrary(hMzGC);
732 hMzGC = 0;
733 if (verbose)
734 EMSG2(_(e_loadfunc), thunk->name);
735 return FAIL;
736 }
737 }
738 return OK;
739}
740
741 int
742mzscheme_enabled(int verbose)
743{
744 return mzscheme_runtime_link_init(
745 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
746}
747
748 static void
749dynamic_mzscheme_end(void)
750{
751 if (hMzSch)
752 {
753 FreeLibrary(hMzSch);
754 hMzSch = 0;
755 }
756 if (hMzGC)
757 {
758 FreeLibrary(hMzGC);
759 hMzGC = 0;
760 }
761}
762#endif /* DYNAMIC_MZSCHEME */
763
Bram Moolenaar75676462013-01-30 14:55:42 +0100764#if MZSCHEME_VERSION_MAJOR < 299
765# define GUARANTEED_STRING_ARG(proc, num) GUARANTEE_STRING(proc, num)
766#else
767 static Scheme_Object *
768guaranteed_byte_string_arg(char *proc, int num, int argc, Scheme_Object **argv)
769{
770 if (SCHEME_BYTE_STRINGP(argv[num]))
771 {
772 return argv[num];
773 }
774 else if (SCHEME_CHAR_STRINGP(argv[num]))
775 {
776 Scheme_Object *tmp = NULL;
777 MZ_GC_DECL_REG(2);
778 MZ_GC_VAR_IN_REG(0, argv[num]);
779 MZ_GC_VAR_IN_REG(1, tmp);
780 MZ_GC_REG();
781 tmp = scheme_char_string_to_byte_string(argv[num]);
782 MZ_GC_UNREG();
783 return tmp;
784 }
785 else
786 scheme_wrong_type(proc, "string", num, argc, argv);
787 /* unreachable */
788 return scheme_void;
789}
790# define GUARANTEED_STRING_ARG(proc, num) guaranteed_byte_string_arg(proc, num, argc, argv)
791#endif
792
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000793/* need to put it here for dynamic stuff to work */
Bram Moolenaare484c942009-09-11 10:21:41 +0000794#if defined(INCLUDE_MZSCHEME_BASE)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000795# include "mzscheme_base.c"
796#endif
797
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000798/*
799 *========================================================================
800 * 1. MzScheme interpreter startup
801 *========================================================================
802 */
803
804static Scheme_Type mz_buffer_type;
805static Scheme_Type mz_window_type;
806
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000807static int initialized = FALSE;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100808#ifdef DYNAMIC_MZSCHEME
809static int disabled = FALSE;
810#endif
811static int load_base_module_failed = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000812
813/* global environment */
814static Scheme_Env *environment = NULL;
815/* output/error handlers */
816static Scheme_Object *curout = NULL;
817static Scheme_Object *curerr = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000818/* exn:vim exception */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000819static Scheme_Object *exn_catching_apply = NULL;
820static Scheme_Object *exn_p = NULL;
821static Scheme_Object *exn_message = NULL;
822static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000823
824#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
825static void *stack_base = NULL;
826#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000827
828static long range_start;
829static long range_end;
830
831/* MzScheme threads scheduling stuff */
832static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000833
834#if defined(FEAT_GUI_W32)
835static void CALLBACK timer_proc(HWND, UINT, UINT, DWORD);
836static UINT timer_id = 0;
837#elif defined(FEAT_GUI_GTK)
838static gint timer_proc(gpointer);
839static guint timer_id = 0;
840#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
841static void timer_proc(XtPointer, XtIntervalId *);
842static XtIntervalId timer_id = (XtIntervalId)0;
843#elif defined(FEAT_GUI_MAC)
844pascal void timer_proc(EventLoopTimerRef, void *);
845static EventLoopTimerRef timer_id = NULL;
846static EventLoopTimerUPP timerUPP;
847#endif
848
849#ifndef FEAT_GUI_W32 /* Win32 console and Unix */
850 void
851mzvim_check_threads(void)
852{
853 /* Last time MzScheme threads were scheduled */
854 static time_t mz_last_time = 0;
855
856 if (mz_threads_allow && p_mzq > 0)
857 {
858 time_t now = time(NULL);
859
860 if ((now - mz_last_time) * 1000 > p_mzq)
861 {
862 mz_last_time = now;
863 scheme_check_threads();
864 }
865 }
866}
867#endif
868
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000869#ifdef MZSCHEME_GUI_THREADS
870static void setup_timer(void);
871static void remove_timer(void);
872
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000873/* timers are presented in GUI only */
874# if defined(FEAT_GUI_W32)
875 static void CALLBACK
Bram Moolenaar64404472010-06-26 06:24:45 +0200876timer_proc(HWND hwnd UNUSED, UINT uMsg UNUSED, UINT idEvent UNUSED, DWORD dwTime UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000877# elif defined(FEAT_GUI_GTK)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000878 static gint
Bram Moolenaar64404472010-06-26 06:24:45 +0200879timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000880# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000881 static void
Bram Moolenaar64404472010-06-26 06:24:45 +0200882timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000883# elif defined(FEAT_GUI_MAC)
884 pascal void
Bram Moolenaar64404472010-06-26 06:24:45 +0200885timer_proc(EventLoopTimerRef theTimer UNUSED, void *userData UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000886# endif
887{
888 scheme_check_threads();
889# if defined(FEAT_GUI_GTK)
890 return TRUE; /* continue receiving notifications */
891# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
892 /* renew timeout */
893 if (mz_threads_allow && p_mzq > 0)
894 timer_id = XtAppAddTimeOut(app_context, p_mzq,
895 timer_proc, NULL);
896# endif
897}
898
899 static void
900setup_timer(void)
901{
902# if defined(FEAT_GUI_W32)
903 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
904# elif defined(FEAT_GUI_GTK)
905 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
906# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
907 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
908# elif defined(FEAT_GUI_MAC)
909 timerUPP = NewEventLoopTimerUPP(timer_proc);
910 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
911 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
912# endif
913}
914
915 static void
916remove_timer(void)
917{
918# if defined(FEAT_GUI_W32)
919 KillTimer(NULL, timer_id);
920# elif defined(FEAT_GUI_GTK)
921 gtk_timeout_remove(timer_id);
922# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
923 XtRemoveTimeOut(timer_id);
924# elif defined(FEAT_GUI_MAC)
925 RemoveEventLoopTimer(timer_id);
926 DisposeEventLoopTimerUPP(timerUPP);
927# endif
928 timer_id = 0;
929}
930
931 void
932mzvim_reset_timer(void)
933{
934 if (timer_id != 0)
935 remove_timer();
936 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
937 setup_timer();
938}
939
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000940#endif /* MZSCHEME_GUI_THREADS */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000941
942 static void
943notify_multithread(int on)
944{
945 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000946#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000947 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
948 setup_timer();
949 if (!on && timer_id != 0)
950 remove_timer();
951#endif
952}
953
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000954 void
955mzscheme_end(void)
956{
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100957 /* We can not unload the DLL. Racket's thread might be still alive. */
958#if 0
Bram Moolenaar33570922005-01-25 22:26:29 +0000959#ifdef DYNAMIC_MZSCHEME
960 dynamic_mzscheme_end();
961#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100962#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000963}
964
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100965#if HAVE_TLS_SPACE
966# if defined(_MSC_VER)
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100967static __declspec(thread) void *tls_space;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100968extern intptr_t _tls_index;
969# elif defined(__MINGW32__)
970static __thread void *tls_space;
971extern intptr_t _tls_index;
972# else
973static THREAD_LOCAL void *tls_space;
974static intptr_t _tls_index = 0;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100975# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100976#endif
977
978 int
979mzscheme_main(int argc, char** argv)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000980{
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100981#ifdef DYNAMIC_MZSCHEME
982 /*
983 * Racket requires trampolined startup. We can not load it later.
984 * If dynamic dll loading is failed, disable it.
985 */
986 if (!mzscheme_enabled(FALSE))
987 {
988 disabled = TRUE;
989 return vim_main2(argc, argv);
990 }
991#endif
Bram Moolenaar5c5c9802015-07-10 16:12:48 +0200992#ifdef HAVE_TLS_SPACE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100993 scheme_register_tls_space(&tls_space, _tls_index);
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100994#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100995#ifdef TRAMPOLINED_MZVIM_STARTUP
996 return scheme_main_setup(TRUE, mzscheme_env_main, argc, argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000997#else
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100998 return mzscheme_env_main(NULL, argc, argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000999#endif
1000}
1001
1002 static int
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001003mzscheme_env_main(Scheme_Env *env, int argc, char **argv)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001004{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001005 int vim_main_result;
1006#ifdef TRAMPOLINED_MZVIM_STARTUP
1007 /* Scheme has created the environment for us */
1008 environment = env;
1009#else
1010# ifdef MZ_PRECISE_GC
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001011 Scheme_Object *dummy = NULL;
1012 MZ_GC_DECL_REG(1);
1013 MZ_GC_VAR_IN_REG(0, dummy);
1014
1015 stack_base = &__gc_var_stack__;
1016# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001017 int dummy = 0;
1018 stack_base = (void *)&dummy;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001019# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001020#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001021
1022 /* mzscheme_main is called as a trampoline from main.
1023 * We trampoline into vim_main2
1024 * Passing argc, argv through from mzscheme_main
1025 */
1026 vim_main_result = vim_main2(argc, argv);
1027#if !defined(TRAMPOLINED_MZVIM_STARTUP) && defined(MZ_PRECISE_GC)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001028 /* releasing dummy */
1029 MZ_GC_REG();
1030 MZ_GC_UNREG();
1031#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001032 return vim_main_result;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001033}
1034
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001035 static Scheme_Object*
1036load_base_module(void *data)
1037{
1038 scheme_namespace_require(scheme_intern_symbol((char *)data));
1039 return scheme_null;
1040}
1041
1042 static Scheme_Object *
1043load_base_module_on_error(void *data)
1044{
1045 load_base_module_failed = TRUE;
1046 return scheme_null;
1047}
1048
1049 static int
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001050startup_mzscheme(void)
1051{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001052#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001053 scheme_set_stack_base(stack_base, 1);
1054#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001055
Bram Moolenaar75676462013-01-30 14:55:42 +01001056#ifndef TRAMPOLINED_MZVIM_STARTUP
1057 /* in newer versions of precise GC the initial env has been created */
1058 environment = scheme_basic_env();
1059#endif
1060
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001061 MZ_REGISTER_STATIC(environment);
1062 MZ_REGISTER_STATIC(curout);
1063 MZ_REGISTER_STATIC(curerr);
1064 MZ_REGISTER_STATIC(exn_catching_apply);
1065 MZ_REGISTER_STATIC(exn_p);
1066 MZ_REGISTER_STATIC(exn_message);
1067 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001068
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001069 MZ_GC_CHECK();
1070
1071#ifdef INCLUDE_MZSCHEME_BASE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001072 /* invoke function from generated and included mzscheme_base.c */
1073 declare_modules(environment);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001074#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001075
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001076 /* setup 'current-library-collection-paths' parameter */
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001077# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001078 {
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001079 Scheme_Object *coll_path = NULL;
1080 int mustfree = FALSE;
1081 char_u *s;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001082
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001083 MZ_GC_DECL_REG(1);
1084 MZ_GC_VAR_IN_REG(0, coll_path);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001085 MZ_GC_REG();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001086 /* workaround for dynamic loading on windows */
1087 s = vim_getenv("PLTCOLLECTS", &mustfree);
1088 if (s != NULL)
1089 {
1090 coll_path = scheme_make_path(s);
1091 MZ_GC_CHECK();
1092 if (mustfree)
1093 vim_free(s);
1094 }
1095# ifdef MZSCHEME_COLLECTS
1096 if (coll_path == NULL)
1097 {
1098 coll_path = scheme_make_path(MZSCHEME_COLLECTS);
1099 MZ_GC_CHECK();
1100 }
Bram Moolenaar39d7d512013-01-31 21:09:15 +01001101# endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001102 if (coll_path != NULL)
1103 {
1104 scheme_set_collects_path(coll_path);
1105 MZ_GC_CHECK();
1106 }
1107 MZ_GC_UNREG();
1108 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001109# else
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001110# ifdef MZSCHEME_COLLECTS
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001111 {
1112 Scheme_Object *coll_string = NULL;
1113 Scheme_Object *coll_pair = NULL;
1114 Scheme_Config *config = NULL;
1115
1116 MZ_GC_DECL_REG(3);
1117 MZ_GC_VAR_IN_REG(0, coll_string);
1118 MZ_GC_VAR_IN_REG(1, coll_pair);
1119 MZ_GC_VAR_IN_REG(2, config);
1120 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001121 coll_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001122 MZ_GC_CHECK();
1123 coll_pair = scheme_make_pair(coll_string, scheme_null);
1124 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001125 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001126 MZ_GC_CHECK();
1127 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
1128 MZ_GC_CHECK();
1129 MZ_GC_UNREG();
1130 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001131# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001132#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001133
1134# if MZSCHEME_VERSION_MAJOR >= 600
1135 {
1136 Scheme_Object *config_path = NULL;
1137 int mustfree = FALSE;
1138 char_u *s;
1139
1140 MZ_GC_DECL_REG(1);
1141 MZ_GC_VAR_IN_REG(0, config_path);
1142 MZ_GC_REG();
1143 /* workaround for dynamic loading on windows */
1144 s = vim_getenv("PLTCONFIGDIR", &mustfree);
1145 if (s != NULL)
1146 {
1147 config_path = scheme_make_path(s);
1148 MZ_GC_CHECK();
1149 if (mustfree)
1150 vim_free(s);
1151 }
1152#ifdef MZSCHEME_CONFIGDIR
1153 if (config_path == NULL)
1154 {
1155 config_path = scheme_make_path(MZSCHEME_CONFIGDIR);
1156 MZ_GC_CHECK();
1157 }
1158#endif
1159 if (config_path != NULL)
1160 {
1161 scheme_set_config_path(config_path);
1162 MZ_GC_CHECK();
1163 }
1164 MZ_GC_UNREG();
1165 }
1166# endif
1167
1168#if MZSCHEME_VERSION_MAJOR >= 400
1169 scheme_init_collection_paths(environment, scheme_null);
1170#endif
1171
1172 /*
1173 * versions 4.x do not provide Scheme bindings by default
1174 * we need to add them explicitly
1175 */
1176 {
1177 /* use error handler to avoid abort */
1178 scheme_dynamic_wind(NULL, load_base_module, NULL,
1179 load_base_module_on_error, "racket/base");
1180 if (load_base_module_failed)
1181 {
1182 load_base_module_failed = FALSE;
1183 scheme_dynamic_wind(NULL, load_base_module, NULL,
1184 load_base_module_on_error, "scheme/base");
1185 if (load_base_module_failed)
1186 return -1;
1187 }
1188 }
1189
1190 register_vim_exn();
1191 /* use new environment to initialise exception handling */
1192 init_exn_catching_apply();
1193
1194 /* redirect output */
1195 scheme_console_output = do_output;
1196 scheme_console_printf = do_printf;
1197
Bram Moolenaar555b2802005-05-19 21:08:39 +00001198#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +00001199 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001200 Scheme_Object *make_security_guard = NULL;
1201 MZ_GC_DECL_REG(1);
1202 MZ_GC_VAR_IN_REG(0, make_security_guard);
1203 MZ_GC_REG();
1204
1205#if MZSCHEME_VERSION_MAJOR < 400
1206 {
1207 Scheme_Object *make_security_guard_symbol = NULL;
1208 MZ_GC_DECL_REG(1);
1209 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
1210 MZ_GC_REG();
1211 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
1212 MZ_GC_CHECK();
1213 make_security_guard = scheme_lookup_global(
1214 make_security_guard_symbol, environment);
1215 MZ_GC_UNREG();
1216 }
1217#else
1218 make_security_guard = scheme_builtin_value("make-security-guard");
1219 MZ_GC_CHECK();
1220#endif
1221
1222 /* setup sandbox guards */
1223 if (make_security_guard != NULL)
1224 {
1225 Scheme_Object *args[3] = {NULL, NULL, NULL};
1226 Scheme_Object *guard = NULL;
1227 Scheme_Config *config = NULL;
1228 MZ_GC_DECL_REG(5);
1229 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
1230 MZ_GC_VAR_IN_REG(3, guard);
1231 MZ_GC_VAR_IN_REG(4, config);
1232 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001233 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001234 MZ_GC_CHECK();
1235 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
1236 MZ_GC_CHECK();
1237 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
1238 "sandbox-file-guard", 3, 3);
1239 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1240 "sandbox-network-guard", 4, 4);
1241 guard = scheme_apply(make_security_guard, 3, args);
1242 MZ_GC_CHECK();
1243 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1244 MZ_GC_CHECK();
1245 MZ_GC_UNREG();
1246 }
1247 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001248 }
1249#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001250 /* Create buffer and window types for use in Scheme code */
1251 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001252 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001253 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001254 MZ_GC_CHECK();
1255#ifdef MZ_PRECISE_GC
1256 GC_register_traversers(mz_buffer_type,
1257 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1258 TRUE, TRUE);
1259 GC_register_traversers(mz_window_type,
1260 window_size_proc, window_mark_proc, window_fixup_proc,
1261 TRUE, TRUE);
1262#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001263
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001264 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001265
1266 /*
1267 * setup callback to receive notifications
1268 * whether thread scheduling is (or not) required
1269 */
1270 scheme_notify_multithread = notify_multithread;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001271
1272 return 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001273}
1274
1275/*
1276 * This routine is called for each new invocation of MzScheme
1277 * to make sure things are properly initialized.
1278 */
1279 static int
1280mzscheme_init(void)
1281{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001282 if (!initialized)
1283 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001284#ifdef DYNAMIC_MZSCHEME
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001285 if (disabled || !mzscheme_enabled(TRUE))
Bram Moolenaar33570922005-01-25 22:26:29 +00001286 {
Bram Moolenaarb849e712009-06-24 15:51:37 +00001287 EMSG(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
Bram Moolenaar33570922005-01-25 22:26:29 +00001288 return -1;
1289 }
1290#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001291 if (load_base_module_failed || startup_mzscheme())
1292 {
Bram Moolenaar9e3be262016-01-24 13:58:40 +01001293 EMSG(_("E895: Sorry, this command is disabled, the MzScheme's racket/base module could not be loaded."));
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001294 return -1;
1295 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001296 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001297 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001298 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001299 Scheme_Config *config = NULL;
1300 MZ_GC_DECL_REG(1);
1301 MZ_GC_VAR_IN_REG(0, config);
1302 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001303 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001304 MZ_GC_CHECK();
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001305 /* recreate ports each call effectively clearing these ones */
Bram Moolenaar75676462013-01-30 14:55:42 +01001306 curout = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001307 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001308 curerr = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001309 MZ_GC_CHECK();
1310 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1311 MZ_GC_CHECK();
1312 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1313 MZ_GC_CHECK();
1314 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001315 }
1316
1317 return 0;
1318}
1319
1320/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001321 *========================================================================
1322 * 2. External Interface
1323 *========================================================================
1324 */
1325
1326/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001327 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001328 */
1329 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001330eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001331{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001332 Scheme_Object *value = NULL;
1333 Scheme_Object *exn = NULL;
1334 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001335
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001336 MZ_GC_DECL_REG(3);
1337 MZ_GC_VAR_IN_REG(0, value);
1338 MZ_GC_VAR_IN_REG(1, exn);
1339 MZ_GC_VAR_IN_REG(2, prim);
1340 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001341
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001342 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1343 MZ_GC_CHECK();
1344 value = _apply_thunk_catch_exceptions(prim, &exn);
1345 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001346
1347 if (!value)
1348 {
1349 value = extract_exn_message(exn);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001350 /* Got an exn? */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001351 if (value)
1352 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001353 scheme_display(value, curerr); /* Send to stderr-vim */
1354 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001355 do_flush();
1356 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001357 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001358 /* `raise' was called on some arbitrary value */
1359 return FAIL;
1360 }
1361
1362 if (ret != NULL) /* if pointer to retval supported give it up */
1363 *ret = value;
1364 /* Print any result, as long as it's not a void */
1365 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001366 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001367 scheme_display(value, curout); /* Send to stdout-vim */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001368 MZ_GC_CHECK();
1369 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001370
1371 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001372 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001373 return OK;
1374}
1375
1376/* :mzscheme */
1377 static int
1378do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1379{
1380 if (mzscheme_init())
1381 return FAIL;
1382
1383 range_start = eap->line1;
1384 range_end = eap->line2;
1385
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001386 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001387}
1388
1389/*
1390 * Routine called by VIM when deleting a buffer
1391 */
1392 void
1393mzscheme_buffer_free(buf_T *buf)
1394{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001395 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001396 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001397 vim_mz_buffer *bp = NULL;
1398 MZ_GC_DECL_REG(1);
1399 MZ_GC_VAR_IN_REG(0, bp);
1400 MZ_GC_REG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001401
Bram Moolenaar75676462013-01-30 14:55:42 +01001402 bp = BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001403 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001404#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001405 scheme_gc_ptr_ok(bp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001406#else
1407 scheme_free_immobile_box(buf->b_mzscheme_ref);
1408#endif
1409 buf->b_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001410 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001411 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001412 }
1413}
1414
1415/*
1416 * Routine called by VIM when deleting a Window
1417 */
1418 void
1419mzscheme_window_free(win_T *win)
1420{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001421 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001422 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001423 vim_mz_window *wp = NULL;
1424 MZ_GC_DECL_REG(1);
1425 MZ_GC_VAR_IN_REG(0, wp);
1426 MZ_GC_REG();
1427 wp = WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001428 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001429#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001430 scheme_gc_ptr_ok(wp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001431#else
1432 scheme_free_immobile_box(win->w_mzscheme_ref);
1433#endif
1434 win->w_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001435 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001436 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001437 }
1438}
1439
1440/*
1441 * ":mzscheme" (or ":mz")
1442 */
1443 void
1444ex_mzscheme(exarg_T *eap)
1445{
1446 char_u *script;
1447
1448 script = script_get(eap, eap->arg);
1449 if (!eap->skip)
1450 {
1451 if (script == NULL)
1452 do_mzscheme_command(eap, eap->arg, do_eval);
1453 else
1454 {
1455 do_mzscheme_command(eap, script, do_eval);
1456 vim_free(script);
1457 }
1458 }
1459}
1460
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001461 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001462do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001463{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001464 Scheme_Object *expr = NULL;
1465 Scheme_Object *result = NULL;
1466 char *file = NULL;
1467 Port_Info *pinfo = (Port_Info *)data;
1468
1469 MZ_GC_DECL_REG(3);
1470 MZ_GC_VAR_IN_REG(0, expr);
1471 MZ_GC_VAR_IN_REG(1, result);
1472 MZ_GC_VAR_IN_REG(2, file);
1473 MZ_GC_REG();
1474
1475 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1476 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001477
1478 /* make Vim expansion */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001479 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001480 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001481 MZ_GC_CHECK();
1482 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1483 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001484
1485 /* Like REPL but print only last result */
1486 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001487 {
1488 result = scheme_eval(expr, environment);
1489 MZ_GC_CHECK();
1490 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001491
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001492 /* errors will be caught in do_mzscheme_command and ex_mzfile */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001493 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001494 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001495 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001496 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001497 return result;
1498}
1499
1500/* :mzfile */
1501 void
1502ex_mzfile(exarg_T *eap)
1503{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001504 Port_Info pinfo = {NULL, NULL};
1505
1506 MZ_GC_DECL_REG(1);
1507 MZ_GC_VAR_IN_REG(0, pinfo.port);
1508 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001509
1510 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001511 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1512 && pinfo.port != NULL) /* looks like port was not closed */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001513 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001514 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001515 MZ_GC_CHECK();
1516 }
1517 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001518}
1519
1520
1521/*
1522 *========================================================================
1523 * Exception handling code -- cribbed form the MzScheme sources and
1524 * Matthew Flatt's "Inside PLT MzScheme" document.
1525 *========================================================================
1526 */
1527 static void
1528init_exn_catching_apply(void)
1529{
1530 if (!exn_catching_apply)
1531 {
1532 char *e =
1533 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001534 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001535 "(cons #t (thunk))))";
1536
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001537 exn_catching_apply = scheme_eval_string(e, environment);
1538 MZ_GC_CHECK();
1539 exn_p = scheme_builtin_value("exn?");
1540 MZ_GC_CHECK();
1541 exn_message = scheme_builtin_value("exn-message");
1542 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001543 }
1544}
1545
1546/*
1547 * This function applies a thunk, returning the Scheme value if there's
1548 * no exception, otherwise returning NULL and setting *exn to the raised
1549 * value (usually an exn structure).
1550 */
1551 static Scheme_Object *
1552_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1553{
1554 Scheme_Object *v;
1555
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001556 v = _scheme_apply(exn_catching_apply, 1, &f);
1557 /* v is a pair: (cons #t value) or (cons #f exn) */
1558
1559 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1560 return SCHEME_CDR(v);
1561 else
1562 {
1563 *exn = SCHEME_CDR(v);
1564 return NULL;
1565 }
1566}
1567
1568 static Scheme_Object *
1569extract_exn_message(Scheme_Object *v)
1570{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001571 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1572 return _scheme_apply(exn_message, 1, &v);
1573 else
1574 return NULL; /* Not an exn structure */
1575}
1576
1577 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001578do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001579{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001580 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001581}
1582
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001583/*
1584 *========================================================================
1585 * 3. MzScheme I/O Handlers
1586 *========================================================================
1587 */
1588 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001589do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001590{
1591 char *p, *prev;
1592
1593 prev = mesg;
1594 p = strchr(prev, '\n');
1595 while (p)
1596 {
1597 *p = '\0';
1598 if (error)
1599 EMSG(prev);
1600 else
1601 MSG(prev);
1602 prev = p + 1;
1603 p = strchr(prev, '\n');
1604 }
1605
1606 if (error)
1607 EMSG(prev);
1608 else
1609 MSG(prev);
1610}
1611
1612 static void
Bram Moolenaar75676462013-01-30 14:55:42 +01001613do_output(char *mesg, OUTPUT_LEN_TYPE len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001614{
Bram Moolenaar02e14d62012-11-28 15:37:51 +01001615 /* TODO: use len, the string may not be NUL terminated */
Bram Moolenaar64404472010-06-26 06:24:45 +02001616 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001617}
1618
1619 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001620do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001621{
Bram Moolenaar64404472010-06-26 06:24:45 +02001622 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001623}
1624
1625 static void
1626do_printf(char *format, ...)
1627{
Bram Moolenaar64404472010-06-26 06:24:45 +02001628 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001629}
1630
1631 static void
1632do_flush(void)
1633{
1634 char *buff;
Bram Moolenaar75676462013-01-30 14:55:42 +01001635 OUTPUT_LEN_TYPE length;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001636
Bram Moolenaar75676462013-01-30 14:55:42 +01001637 buff = scheme_get_sized_byte_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001638 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001639 if (length)
1640 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001641 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001642 return;
1643 }
1644
Bram Moolenaar75676462013-01-30 14:55:42 +01001645 buff = scheme_get_sized_byte_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001646 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001647 if (length)
1648 do_output(buff, length);
1649}
1650
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001651/*
1652 *========================================================================
1653 * 4. Implementation of the Vim Features for MzScheme
1654 *========================================================================
1655 */
1656
1657/* (command {command-string}) */
1658 static Scheme_Object *
1659vim_command(void *data, int argc, Scheme_Object **argv)
1660{
1661 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001662 Scheme_Object *cmd = NULL;
1663 MZ_GC_DECL_REG(1);
1664 MZ_GC_VAR_IN_REG(0, cmd);
1665 MZ_GC_REG();
1666 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001667
1668 /* may be use do_cmdline_cmd? */
Bram Moolenaar75676462013-01-30 14:55:42 +01001669 do_cmdline(BYTE_STRING_VALUE(cmd), NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001670 update_screen(VALID);
1671
Bram Moolenaar75676462013-01-30 14:55:42 +01001672 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001673 raise_if_error();
1674 return scheme_void;
1675}
1676
1677/* (eval {expr-string}) */
1678 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001679vim_eval(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001680{
1681#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001682 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001683 Scheme_Object *result = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001684 typval_T *vim_result;
Bram Moolenaar75676462013-01-30 14:55:42 +01001685 Scheme_Object *expr = NULL;
1686 MZ_GC_DECL_REG(2);
1687 MZ_GC_VAR_IN_REG(0, result);
1688 MZ_GC_VAR_IN_REG(1, expr);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001689 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001690 expr = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001691
Bram Moolenaar75676462013-01-30 14:55:42 +01001692 vim_result = eval_expr(BYTE_STRING_VALUE(expr), NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001693
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001694 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001695 raise_vim_exn(_("invalid expression"));
1696
Bram Moolenaar75676462013-01-30 14:55:42 +01001697 result = vim_to_mzscheme(vim_result);
1698 MZ_GC_CHECK();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001699 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001700
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001701 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001702 return result;
1703#else
1704 raise_vim_exn(_("expressions disabled at compile time"));
1705 /* unreachable */
1706 return scheme_false;
1707#endif
1708}
1709
1710/* (range-start) */
1711 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001712get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001713{
1714 return scheme_make_integer(range_start);
1715}
1716
1717/* (range-end) */
1718 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001719get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001720{
1721 return scheme_make_integer(range_end);
1722}
1723
1724/* (beep) */
1725 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001726mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001727{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001728 vim_beep(BO_LANG);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001729 return scheme_void;
1730}
1731
1732static Scheme_Object *M_global = NULL;
1733
1734/* (get-option {option-name}) [buffer/window] */
1735 static Scheme_Object *
1736get_option(void *data, int argc, Scheme_Object **argv)
1737{
1738 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001739 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001740 char *strval;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001741 int rc;
Bram Moolenaar75676462013-01-30 14:55:42 +01001742 Scheme_Object *rval = NULL;
1743 Scheme_Object *name = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001744 int opt_flags = 0;
1745 buf_T *save_curb = curbuf;
1746 win_T *save_curw = curwin;
1747
Bram Moolenaar75676462013-01-30 14:55:42 +01001748 MZ_GC_DECL_REG(2);
1749 MZ_GC_VAR_IN_REG(0, rval);
1750 MZ_GC_VAR_IN_REG(1, name);
1751 MZ_GC_REG();
1752
1753 name = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001754
1755 if (argc > 1)
1756 {
1757 if (M_global == NULL)
1758 {
1759 MZ_REGISTER_STATIC(M_global);
1760 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001761 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001762 }
1763
1764 if (argv[1] == M_global)
1765 opt_flags = OPT_GLOBAL;
1766 else if (SCHEME_VIMBUFFERP(argv[1]))
1767 {
1768 curbuf = get_valid_buffer(argv[1]);
1769 opt_flags = OPT_LOCAL;
1770 }
1771 else if (SCHEME_VIMWINDOWP(argv[1]))
1772 {
1773 win_T *win = get_valid_window(argv[1]);
1774
1775 curwin = win;
1776 curbuf = win->w_buffer;
1777 opt_flags = OPT_LOCAL;
1778 }
1779 else
1780 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1781 }
1782
Bram Moolenaar75676462013-01-30 14:55:42 +01001783 rc = get_option_value(BYTE_STRING_VALUE(name), &value, (char_u **)&strval, opt_flags);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001784 curbuf = save_curb;
1785 curwin = save_curw;
1786
1787 switch (rc)
1788 {
1789 case 1:
Bram Moolenaar75676462013-01-30 14:55:42 +01001790 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001791 return scheme_make_integer_value(value);
1792 case 0:
Bram Moolenaar75676462013-01-30 14:55:42 +01001793 rval = scheme_make_byte_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001794 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001795 vim_free(strval);
Bram Moolenaar75676462013-01-30 14:55:42 +01001796 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001797 return rval;
1798 case -1:
1799 case -2:
Bram Moolenaar75676462013-01-30 14:55:42 +01001800 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001801 raise_vim_exn(_("hidden option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001802 case -3:
Bram Moolenaar75676462013-01-30 14:55:42 +01001803 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001804 raise_vim_exn(_("unknown option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001805 }
1806 /* unreachable */
1807 return scheme_void;
1808}
1809
1810/* (set-option {option-changing-string} [buffer/window]) */
1811 static Scheme_Object *
1812set_option(void *data, int argc, Scheme_Object **argv)
1813{
Bram Moolenaar75676462013-01-30 14:55:42 +01001814 char_u *command = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001815 int opt_flags = 0;
1816 buf_T *save_curb = curbuf;
1817 win_T *save_curw = curwin;
1818 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001819 Scheme_Object *cmd = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001820
Bram Moolenaar75676462013-01-30 14:55:42 +01001821 MZ_GC_DECL_REG(1);
1822 MZ_GC_VAR_IN_REG(0, cmd);
1823 MZ_GC_REG();
1824 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
1825
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001826 if (argc > 1)
1827 {
1828 if (M_global == NULL)
1829 {
1830 MZ_REGISTER_STATIC(M_global);
1831 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001832 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001833 }
1834
1835 if (argv[1] == M_global)
1836 opt_flags = OPT_GLOBAL;
1837 else if (SCHEME_VIMBUFFERP(argv[1]))
1838 {
1839 curbuf = get_valid_buffer(argv[1]);
1840 opt_flags = OPT_LOCAL;
1841 }
1842 else if (SCHEME_VIMWINDOWP(argv[1]))
1843 {
1844 win_T *win = get_valid_window(argv[1]);
1845 curwin = win;
1846 curbuf = win->w_buffer;
1847 opt_flags = OPT_LOCAL;
1848 }
1849 else
1850 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1851 }
1852
1853 /* do_set can modify cmd, make copy */
Bram Moolenaar75676462013-01-30 14:55:42 +01001854 command = vim_strsave(BYTE_STRING_VALUE(cmd));
1855 MZ_GC_UNREG();
1856 do_set(command, opt_flags);
1857 vim_free(command);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001858 update_screen(NOT_VALID);
1859 curbuf = save_curb;
1860 curwin = save_curw;
1861 raise_if_error();
1862 return scheme_void;
1863}
1864
1865/*
1866 *===========================================================================
1867 * 5. Vim Window-related Manipulation Functions
1868 *===========================================================================
1869 */
1870
1871/* (curr-win) */
1872 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001873get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001874{
1875 return (Scheme_Object *)get_vim_curr_window();
1876}
1877
1878/* (win-count) */
1879 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001880get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001881{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001882 int n = 0;
Bram Moolenaard2142212013-01-30 17:41:50 +01001883#ifdef FEAT_WINDOWS
1884 win_T *w;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001885
Bram Moolenaarf740b292006-02-16 22:11:02 +00001886 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaard2142212013-01-30 17:41:50 +01001887#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001888 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001889 return scheme_make_integer(n);
1890}
1891
1892/* (get-win-list [buffer]) */
1893 static Scheme_Object *
1894get_window_list(void *data, int argc, Scheme_Object **argv)
1895{
1896 Vim_Prim *prim = (Vim_Prim *)data;
1897 vim_mz_buffer *buf;
1898 Scheme_Object *list;
Bram Moolenaard2142212013-01-30 17:41:50 +01001899 win_T *w = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001900
1901 buf = get_buffer_arg(prim->name, 0, argc, argv);
1902 list = scheme_null;
1903
Bram Moolenaard2142212013-01-30 17:41:50 +01001904#ifdef FEAT_WINDOWS
1905 for ( ; w != NULL; w = w->w_next)
1906#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001907 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001908 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001909 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001910 MZ_GC_CHECK();
1911 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001912
1913 return list;
1914}
1915
1916 static Scheme_Object *
1917window_new(win_T *win)
1918{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001919 vim_mz_window *self = NULL;
1920
1921 MZ_GC_DECL_REG(1);
1922 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001923
1924 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001925 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001926 * then we can get at it in win_free() in vim.
1927 *
1928 * On a win_free() we set the Scheme object's win_T *field
1929 * to an invalid value. We trap all uses of a window
1930 * object, and reject them if the win_T *field is invalid.
1931 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001932 if (win->w_mzscheme_ref != NULL)
Bram Moolenaar75676462013-01-30 14:55:42 +01001933 return (Scheme_Object *)WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001934
Bram Moolenaar75676462013-01-30 14:55:42 +01001935 MZ_GC_REG();
1936 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_window));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001937 vim_memset(self, 0, sizeof(vim_mz_window));
Bram Moolenaar75676462013-01-30 14:55:42 +01001938#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001939 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaar75676462013-01-30 14:55:42 +01001940#else
1941 win->w_mzscheme_ref = scheme_malloc_immobile_box(NULL);
1942#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001943 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001944 WINDOW_REF(win) = self;
1945 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001946 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001947 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001948
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001949 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001950 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001951}
1952
1953/* (get-win-num [window]) */
1954 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001955get_window_num(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001956{
Bram Moolenaard2142212013-01-30 17:41:50 +01001957 int nr = 1;
1958#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001959 Vim_Prim *prim = (Vim_Prim *)data;
1960 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001961 win_T *wp;
1962
1963 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaard2142212013-01-30 17:41:50 +01001964#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001965 ++nr;
1966
1967 return scheme_make_integer(nr);
1968}
1969
1970/* (get-win-by-num {windownum}) */
1971 static Scheme_Object *
1972get_window_by_num(void *data, int argc, Scheme_Object **argv)
1973{
1974 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaard2142212013-01-30 17:41:50 +01001975 win_T *win = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001976 int fnum;
1977
1978 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1979 if (fnum < 1)
1980 scheme_signal_error(_("window index is out of range"));
1981
Bram Moolenaard2142212013-01-30 17:41:50 +01001982#ifdef FEAT_WINDOWS
1983 for ( ; win != NULL; win = win->w_next, --fnum)
1984#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001985 if (fnum == 1) /* to be 1-based */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001986 return window_new(win);
1987
1988 return scheme_false;
1989}
1990
1991/* (get-win-buffer [window]) */
1992 static Scheme_Object *
1993get_window_buffer(void *data, int argc, Scheme_Object **argv)
1994{
1995 Vim_Prim *prim = (Vim_Prim *)data;
1996 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1997
1998 return buffer_new(win->win->w_buffer);
1999}
2000
2001/* (get-win-height [window]) */
2002 static Scheme_Object *
2003get_window_height(void *data, int argc, Scheme_Object **argv)
2004{
2005 Vim_Prim *prim = (Vim_Prim *)data;
2006 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2007
2008 return scheme_make_integer(win->win->w_height);
2009}
2010
2011/* (set-win-height {height} [window]) */
2012 static Scheme_Object *
2013set_window_height(void *data, int argc, Scheme_Object **argv)
2014{
2015 Vim_Prim *prim = (Vim_Prim *)data;
2016 vim_mz_window *win;
2017 win_T *savewin;
2018 int height;
2019
2020 win = get_window_arg(prim->name, 1, argc, argv);
2021 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2022
2023#ifdef FEAT_GUI
2024 need_mouse_correct = TRUE;
2025#endif
2026
2027 savewin = curwin;
2028 curwin = win->win;
2029 win_setheight(height);
2030 curwin = savewin;
2031
2032 raise_if_error();
2033 return scheme_void;
2034}
2035
2036#ifdef FEAT_VERTSPLIT
2037/* (get-win-width [window]) */
2038 static Scheme_Object *
2039get_window_width(void *data, int argc, Scheme_Object **argv)
2040{
2041 Vim_Prim *prim = (Vim_Prim *)data;
2042 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2043
2044 return scheme_make_integer(W_WIDTH(win->win));
2045}
2046
2047/* (set-win-width {width} [window]) */
2048 static Scheme_Object *
2049set_window_width(void *data, int argc, Scheme_Object **argv)
2050{
2051 Vim_Prim *prim = (Vim_Prim *)data;
2052 vim_mz_window *win;
2053 win_T *savewin;
2054 int width = 0;
2055
2056 win = get_window_arg(prim->name, 1, argc, argv);
2057 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2058
2059# ifdef FEAT_GUI
2060 need_mouse_correct = TRUE;
2061# endif
2062
2063 savewin = curwin;
2064 curwin = win->win;
2065 win_setwidth(width);
2066 curwin = savewin;
2067
2068 raise_if_error();
2069 return scheme_void;
2070}
2071#endif
2072
2073/* (get-cursor [window]) -> (line . col) */
2074 static Scheme_Object *
2075get_cursor(void *data, int argc, Scheme_Object **argv)
2076{
2077 Vim_Prim *prim = (Vim_Prim *)data;
2078 vim_mz_window *win;
2079 pos_T pos;
2080
2081 win = get_window_arg(prim->name, 0, argc, argv);
2082 pos = win->win->w_cursor;
2083 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
2084 scheme_make_integer_value((long)pos.col + 1));
2085}
2086
2087/* (set-cursor (line . col) [window]) */
2088 static Scheme_Object *
2089set_cursor(void *data, int argc, Scheme_Object **argv)
2090{
2091 Vim_Prim *prim = (Vim_Prim *)data;
2092 vim_mz_window *win;
2093 long lnum = 0;
2094 long col = 0;
2095
Bram Moolenaar555b2802005-05-19 21:08:39 +00002096#ifdef HAVE_SANDBOX
2097 sandbox_check();
2098#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002099 win = get_window_arg(prim->name, 1, argc, argv);
2100 GUARANTEE_PAIR(prim->name, 0);
2101
2102 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
2103 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
2104 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
2105
2106 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
2107 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
2108
2109 check_line_range(lnum, win->win->w_buffer);
2110 /* don't know how to catch invalid column value */
2111
2112 win->win->w_cursor.lnum = lnum;
2113 win->win->w_cursor.col = col;
2114 update_screen(VALID);
2115
2116 raise_if_error();
2117 return scheme_void;
2118}
2119/*
2120 *===========================================================================
2121 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002122 *===========================================================================
2123 */
2124
2125/* (open-buff {filename}) */
2126 static Scheme_Object *
2127mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
2128{
2129 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002130 int num = 0;
Bram Moolenaar75676462013-01-30 14:55:42 +01002131 Scheme_Object *onum = NULL;
2132 Scheme_Object *buf = NULL;
2133 Scheme_Object *fname;
2134
2135 MZ_GC_DECL_REG(3);
2136 MZ_GC_VAR_IN_REG(0, onum);
2137 MZ_GC_VAR_IN_REG(1, buf);
2138 MZ_GC_VAR_IN_REG(2, fname);
2139 MZ_GC_REG();
2140 fname = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002141
Bram Moolenaar555b2802005-05-19 21:08:39 +00002142#ifdef HAVE_SANDBOX
2143 sandbox_check();
2144#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002145 /* TODO make open existing file */
Bram Moolenaar75676462013-01-30 14:55:42 +01002146 num = buflist_add(BYTE_STRING_VALUE(fname), BLN_LISTED | BLN_CURBUF);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002147
2148 if (num == 0)
2149 raise_vim_exn(_("couldn't open buffer"));
2150
2151 onum = scheme_make_integer(num);
Bram Moolenaar75676462013-01-30 14:55:42 +01002152 buf = get_buffer_by_num(data, 1, &onum);
2153 MZ_GC_UNREG();
2154 return buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002155}
2156
2157/* (get-buff-by-num {buffernum}) */
2158 static Scheme_Object *
2159get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
2160{
2161 Vim_Prim *prim = (Vim_Prim *)data;
2162 buf_T *buf;
2163 int fnum;
2164
2165 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2166
2167 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002168 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002169 return buffer_new(buf);
2170
2171 return scheme_false;
2172}
2173
2174/* (get-buff-by-name {buffername}) */
2175 static Scheme_Object *
2176get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
2177{
2178 Vim_Prim *prim = (Vim_Prim *)data;
2179 buf_T *buf;
Bram Moolenaar75676462013-01-30 14:55:42 +01002180 Scheme_Object *buffer = NULL;
2181 Scheme_Object *fname = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002182
Bram Moolenaar75676462013-01-30 14:55:42 +01002183 MZ_GC_DECL_REG(2);
2184 MZ_GC_VAR_IN_REG(0, buffer);
2185 MZ_GC_VAR_IN_REG(1, fname);
2186 MZ_GC_REG();
2187 fname = GUARANTEED_STRING_ARG(prim->name, 0);
2188 buffer = scheme_false;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002189
2190 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaar75676462013-01-30 14:55:42 +01002191 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002192 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2193 /* empty string */
2194 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002195 if (BYTE_STRING_VALUE(fname)[0] == NUL)
2196 buffer = buffer_new(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002197 }
Bram Moolenaar75676462013-01-30 14:55:42 +01002198 else if (!fnamecmp(buf->b_ffname, BYTE_STRING_VALUE(fname))
2199 || !fnamecmp(buf->b_sfname, BYTE_STRING_VALUE(fname)))
2200 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002201 /* either short or long filename matches */
Bram Moolenaar75676462013-01-30 14:55:42 +01002202 buffer = buffer_new(buf);
2203 }
2204 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002205
Bram Moolenaar75676462013-01-30 14:55:42 +01002206 MZ_GC_UNREG();
2207 return buffer;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002208}
2209
2210/* (get-next-buff [buffer]) */
2211 static Scheme_Object *
2212get_next_buffer(void *data, int argc, Scheme_Object **argv)
2213{
2214 Vim_Prim *prim = (Vim_Prim *)data;
2215 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2216
2217 if (buf->b_next == NULL)
2218 return scheme_false;
2219 else
2220 return buffer_new(buf->b_next);
2221}
2222
2223/* (get-prev-buff [buffer]) */
2224 static Scheme_Object *
2225get_prev_buffer(void *data, int argc, Scheme_Object **argv)
2226{
2227 Vim_Prim *prim = (Vim_Prim *)data;
2228 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2229
2230 if (buf->b_prev == NULL)
2231 return scheme_false;
2232 else
2233 return buffer_new(buf->b_prev);
2234}
2235
2236/* (get-buff-num [buffer]) */
2237 static Scheme_Object *
2238get_buffer_num(void *data, int argc, Scheme_Object **argv)
2239{
2240 Vim_Prim *prim = (Vim_Prim *)data;
2241 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2242
2243 return scheme_make_integer(buf->buf->b_fnum);
2244}
2245
2246/* (buff-count) */
2247 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002248get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002249{
2250 buf_T *b;
2251 int n = 0;
2252
2253 for (b = firstbuf; b; b = b->b_next) ++n;
2254 return scheme_make_integer(n);
2255}
2256
2257/* (get-buff-name [buffer]) */
2258 static Scheme_Object *
2259get_buffer_name(void *data, int argc, Scheme_Object **argv)
2260{
2261 Vim_Prim *prim = (Vim_Prim *)data;
2262 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2263
Bram Moolenaar75676462013-01-30 14:55:42 +01002264 return scheme_make_byte_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002265}
2266
2267/* (curr-buff) */
2268 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002269get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002270{
2271 return (Scheme_Object *)get_vim_curr_buffer();
2272}
2273
2274 static Scheme_Object *
2275buffer_new(buf_T *buf)
2276{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002277 vim_mz_buffer *self = NULL;
2278
2279 MZ_GC_DECL_REG(1);
2280 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002281
2282 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00002283 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002284 * then we can get at it in buf_freeall() in vim.
2285 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00002286 if (buf->b_mzscheme_ref)
Bram Moolenaar75676462013-01-30 14:55:42 +01002287 return (Scheme_Object *)BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002288
Bram Moolenaar75676462013-01-30 14:55:42 +01002289 MZ_GC_REG();
2290 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_buffer));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002291 vim_memset(self, 0, sizeof(vim_mz_buffer));
Bram Moolenaar75676462013-01-30 14:55:42 +01002292#ifndef MZ_PRECISE_GC
2293 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
2294#else
2295 buf->b_mzscheme_ref = scheme_malloc_immobile_box(NULL);
2296#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002297 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01002298 BUFFER_REF(buf) = self;
2299 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002300 self->buf = buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002301 self->so.type = mz_buffer_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002302
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002303 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01002304 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002305}
2306
2307/*
2308 * (get-buff-size [buffer])
2309 *
2310 * Get the size (number of lines) in the current buffer.
2311 */
2312 static Scheme_Object *
2313get_buffer_size(void *data, int argc, Scheme_Object **argv)
2314{
2315 Vim_Prim *prim = (Vim_Prim *)data;
2316 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2317
2318 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
2319}
2320
2321/*
2322 * (get-buff-line {linenr} [buffer])
2323 *
2324 * Get a line from the specified buffer. The line number is
2325 * in Vim format (1-based). The line is returned as a MzScheme
2326 * string object.
2327 */
2328 static Scheme_Object *
2329get_buffer_line(void *data, int argc, Scheme_Object **argv)
2330{
2331 Vim_Prim *prim = (Vim_Prim *)data;
2332 vim_mz_buffer *buf;
2333 int linenr;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002334 char_u *line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002335
2336 buf = get_buffer_arg(prim->name, 1, argc, argv);
2337 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2338 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2339
2340 raise_if_error();
Bram Moolenaar75676462013-01-30 14:55:42 +01002341 return scheme_make_byte_string((char *)line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002342}
2343
2344
2345/*
2346 * (get-buff-line-list {start} {end} [buffer])
2347 *
2348 * Get a list of lines from the specified buffer. The line numbers
2349 * are in Vim format (1-based). The range is from lo up to, but not
2350 * including, hi. The list is returned as a list of string objects.
2351 */
2352 static Scheme_Object *
2353get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2354{
2355 Vim_Prim *prim = (Vim_Prim *)data;
2356 vim_mz_buffer *buf;
2357 int i, hi, lo, n;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002358 Scheme_Object *list = NULL;
2359
2360 MZ_GC_DECL_REG(1);
2361 MZ_GC_VAR_IN_REG(0, list);
2362 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002363
2364 buf = get_buffer_arg(prim->name, 2, argc, argv);
2365 list = scheme_null;
2366 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2367 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2368
2369 /*
2370 * Handle some error conditions
2371 */
2372 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002373 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002374
2375 if (hi < 0)
2376 hi = 0;
2377 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002378 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002379
2380 n = hi - lo;
2381
2382 for (i = n; i >= 0; --i)
2383 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002384 Scheme_Object *str = scheme_make_byte_string(
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002385 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2386 raise_if_error();
2387
2388 /* Set the list item */
2389 list = scheme_make_pair(str, list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002390 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002391 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002392 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002393 return list;
2394}
2395
2396/*
2397 * (set-buff-line {linenr} {string/#f} [buffer])
2398 *
2399 * Replace a line in the specified buffer. The line number is
2400 * in Vim format (1-based). The replacement line is given as
2401 * an MzScheme string object. The object is checked for validity
2402 * and correct format. An exception is thrown if the values are not
2403 * the correct format.
2404 *
2405 * It returns a Scheme Object that indicates the length of the
2406 * string changed.
2407 */
2408 static Scheme_Object *
2409set_buffer_line(void *data, int argc, Scheme_Object **argv)
2410{
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002411 /* First of all, we check the value of the supplied MzScheme object.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002412 * There are three cases:
2413 * 1. #f - this is a deletion.
2414 * 2. A string - this is a replacement.
2415 * 3. Anything else - this is an error.
2416 */
2417 Vim_Prim *prim = (Vim_Prim *)data;
2418 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002419 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002420 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002421 int n;
2422
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002423 MZ_GC_DECL_REG(1);
2424 MZ_GC_VAR_IN_REG(0, line);
2425 MZ_GC_REG();
2426
Bram Moolenaar555b2802005-05-19 21:08:39 +00002427#ifdef HAVE_SANDBOX
2428 sandbox_check();
2429#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002430 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2431 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002432 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002433 line = argv[1];
2434 buf = get_buffer_arg(prim->name, 2, argc, argv);
2435
2436 check_line_range(n, buf->buf);
2437
2438 if (SCHEME_FALSEP(line))
2439 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002440 buf_T *savebuf = curbuf;
2441
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002442 curbuf = buf->buf;
2443
2444 if (u_savedel((linenr_T)n, 1L) == FAIL)
2445 {
2446 curbuf = savebuf;
2447 raise_vim_exn(_("cannot save undo information"));
2448 }
2449 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2450 {
2451 curbuf = savebuf;
2452 raise_vim_exn(_("cannot delete line"));
2453 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002454 if (buf->buf == curwin->w_buffer)
2455 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002456 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002457
2458 curbuf = savebuf;
2459
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002460 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002461 raise_if_error();
2462 return scheme_void;
2463 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002464 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002465 {
2466 /* Otherwise it's a line */
2467 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002468
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002469 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002470
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002471 curbuf = buf->buf;
2472
2473 if (u_savesub((linenr_T)n) == FAIL)
2474 {
2475 curbuf = savebuf;
2476 vim_free(save);
2477 raise_vim_exn(_("cannot save undo information"));
2478 }
2479 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2480 {
2481 curbuf = savebuf;
2482 vim_free(save);
2483 raise_vim_exn(_("cannot replace line"));
2484 }
2485 else
2486 {
2487 vim_free(save);
2488 changed_bytes((linenr_T)n, 0);
2489 }
2490
2491 curbuf = savebuf;
2492
2493 /* Check that the cursor is not beyond the end of the line now. */
2494 if (buf->buf == curwin->w_buffer)
2495 check_cursor_col();
2496
2497 MZ_GC_UNREG();
2498 raise_if_error();
2499 return scheme_void;
2500 }
2501}
2502
2503 static void
2504free_array(char **array)
2505{
2506 char **curr = array;
2507 while (*curr != NULL)
2508 vim_free(*curr++);
2509 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002510}
2511
2512/*
2513 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2514 *
2515 * Replace a range of lines in the specified buffer. The line numbers are in
2516 * Vim format (1-based). The range is from lo up to, but not including, hi.
2517 * The replacement lines are given as a Scheme list of string objects. The
2518 * list is checked for validity and correct format.
2519 *
2520 * Errors are returned as a value of FAIL. The return value is OK on success.
2521 * If OK is returned and len_change is not NULL, *len_change is set to the
2522 * change in the buffer length.
2523 */
2524 static Scheme_Object *
2525set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2526{
2527 /* First of all, we check the type of the supplied MzScheme object.
2528 * There are three cases:
2529 * 1. #f - this is a deletion.
2530 * 2. A list - this is a replacement.
2531 * 3. Anything else - this is an error.
2532 */
2533 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002534 vim_mz_buffer *buf = NULL;
2535 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002536 int i, old_len, new_len, hi, lo;
2537 long extra;
2538
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002539 MZ_GC_DECL_REG(1);
2540 MZ_GC_VAR_IN_REG(0, line_list);
2541 MZ_GC_REG();
2542
Bram Moolenaar555b2802005-05-19 21:08:39 +00002543#ifdef HAVE_SANDBOX
2544 sandbox_check();
2545#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002546 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2547 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2548 if (!SCHEME_PAIRP(argv[2])
2549 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2550 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2551 line_list = argv[2];
2552 buf = get_buffer_arg(prim->name, 3, argc, argv);
2553 old_len = hi - lo;
2554 if (old_len < 0) /* process inverse values wisely */
2555 {
2556 i = lo;
2557 lo = hi;
2558 hi = i;
2559 old_len = -old_len;
2560 }
2561 extra = 0;
2562
2563 check_line_range(lo, buf->buf); /* inclusive */
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002564 check_line_range(hi - 1, buf->buf); /* exclusive */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002565
2566 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2567 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002568 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002569 curbuf = buf->buf;
2570
2571 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2572 {
2573 curbuf = savebuf;
2574 raise_vim_exn(_("cannot save undo information"));
2575 }
2576 else
2577 {
2578 for (i = 0; i < old_len; i++)
2579 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2580 {
2581 curbuf = savebuf;
2582 raise_vim_exn(_("cannot delete line"));
2583 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002584 if (buf->buf == curwin->w_buffer)
2585 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002586 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002587 }
2588
2589 curbuf = savebuf;
2590
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002591 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002592 raise_if_error();
2593 return scheme_void;
2594 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002595 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002596 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002597 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002598
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002599 /* List */
2600 new_len = scheme_proper_list_length(line_list);
2601 MZ_GC_CHECK();
2602 if (new_len < 0) /* improper or cyclic list */
2603 scheme_wrong_type(prim->name, "proper list",
2604 2, argc, argv);
2605 else
2606 {
2607 char **array = NULL;
2608 Scheme_Object *line = NULL;
2609 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002610
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002611 MZ_GC_DECL_REG(2);
2612 MZ_GC_VAR_IN_REG(0, line);
2613 MZ_GC_VAR_IN_REG(1, rest);
2614 MZ_GC_REG();
2615
Bram Moolenaar75676462013-01-30 14:55:42 +01002616 array = (char **)alloc((new_len+1)* sizeof(char *));
2617 vim_memset(array, 0, (new_len+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002618
2619 rest = line_list;
2620 for (i = 0; i < new_len; ++i)
2621 {
2622 line = SCHEME_CAR(rest);
2623 rest = SCHEME_CDR(rest);
2624 if (!SCHEME_STRINGP(line))
2625 {
2626 free_array(array);
2627 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2628 }
2629 array[i] = string_to_line(line);
2630 }
2631
2632 curbuf = buf->buf;
2633
2634 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2635 {
2636 curbuf = savebuf;
2637 free_array(array);
2638 raise_vim_exn(_("cannot save undo information"));
2639 }
2640
2641 /*
2642 * If the size of the range is reducing (ie, new_len < old_len) we
2643 * need to delete some old_len. We do this at the start, by
2644 * repeatedly deleting line "lo".
2645 */
2646 for (i = 0; i < old_len - new_len; ++i)
2647 {
2648 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2649 {
2650 curbuf = savebuf;
2651 free_array(array);
2652 raise_vim_exn(_("cannot delete line"));
2653 }
2654 extra--;
2655 }
2656
2657 /*
2658 * For as long as possible, replace the existing old_len with the
2659 * new old_len. This is a more efficient operation, as it requires
2660 * less memory allocation and freeing.
2661 */
2662 for (i = 0; i < old_len && i < new_len; i++)
2663 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2664 {
2665 curbuf = savebuf;
2666 free_array(array);
2667 raise_vim_exn(_("cannot replace line"));
2668 }
2669
2670 /*
2671 * Now we may need to insert the remaining new_len. We don't need to
2672 * free the string passed back because MzScheme has control of that
2673 * memory.
2674 */
2675 while (i < new_len)
2676 {
2677 if (ml_append((linenr_T)(lo + i - 1),
2678 (char_u *)array[i], 0, FALSE) == FAIL)
2679 {
2680 curbuf = savebuf;
2681 free_array(array);
2682 raise_vim_exn(_("cannot insert line"));
2683 }
2684 ++i;
2685 ++extra;
2686 }
2687 MZ_GC_UNREG();
2688 free_array(array);
2689 }
2690
2691 /*
2692 * Adjust marks. Invalidate any which lie in the
2693 * changed range, and move any in the remainder of the buffer.
2694 */
2695 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra);
2696 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2697
2698 if (buf->buf == curwin->w_buffer)
2699 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002700 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002701
2702 MZ_GC_UNREG();
2703 raise_if_error();
2704 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002705 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002706}
2707
2708/*
2709 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2710 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002711 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002712 * The line number is in Vim format (1-based). The lines to be inserted are
2713 * given as an MzScheme list of string objects or as a single string. The lines
2714 * to be added are checked for validity and correct format. Errors are
2715 * returned as a value of FAIL. The return value is OK on success.
2716 * If OK is returned and len_change is not NULL, *len_change
2717 * is set to the change in the buffer length.
2718 */
2719 static Scheme_Object *
2720insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2721{
2722 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002723 vim_mz_buffer *buf = NULL;
2724 Scheme_Object *list = NULL;
2725 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002726 int i, n, size;
2727
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002728 MZ_GC_DECL_REG(1);
2729 MZ_GC_VAR_IN_REG(0, list);
2730 MZ_GC_REG();
2731
Bram Moolenaar555b2802005-05-19 21:08:39 +00002732#ifdef HAVE_SANDBOX
2733 sandbox_check();
2734#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002735 /*
2736 * First of all, we check the type of the supplied MzScheme object.
2737 * It must be a string or a list, or the call is in error.
2738 */
2739 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2740 list = argv[1];
2741
2742 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2743 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2744 buf = get_buffer_arg(prim->name, 2, argc, argv);
2745
2746 if (n != 0) /* 0 can be used in insert */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002747 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002748 if (SCHEME_STRINGP(list))
2749 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002750 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002751
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002752 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002753 curbuf = buf->buf;
2754
2755 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2756 {
2757 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002758 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002759 raise_vim_exn(_("cannot save undo information"));
2760 }
2761 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2762 {
2763 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002764 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002765 raise_vim_exn(_("cannot insert line"));
2766 }
2767 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002768 {
2769 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002770 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002771 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002772
2773 curbuf = savebuf;
2774 update_screen(VALID);
2775
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002776 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002777 raise_if_error();
2778 return scheme_void;
2779 }
2780
2781 /* List */
2782 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002783 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002784 if (size < 0) /* improper or cyclic list */
2785 scheme_wrong_type(prim->name, "proper list",
2786 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002787 else
2788 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002789 Scheme_Object *line = NULL;
2790 Scheme_Object *rest = NULL;
2791 char **array;
2792 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002793
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002794 MZ_GC_DECL_REG(2);
2795 MZ_GC_VAR_IN_REG(0, line);
2796 MZ_GC_VAR_IN_REG(1, rest);
2797 MZ_GC_REG();
2798
Bram Moolenaar75676462013-01-30 14:55:42 +01002799 array = (char **)alloc((size+1) * sizeof(char *));
2800 vim_memset(array, 0, (size+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002801
2802 rest = list;
2803 for (i = 0; i < size; ++i)
2804 {
2805 line = SCHEME_CAR(rest);
2806 rest = SCHEME_CDR(rest);
2807 array[i] = string_to_line(line);
2808 }
2809
2810 curbuf = buf->buf;
2811
2812 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2813 {
2814 curbuf = savebuf;
2815 free_array(array);
2816 raise_vim_exn(_("cannot save undo information"));
2817 }
2818 else
2819 {
2820 for (i = 0; i < size; ++i)
2821 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2822 0, FALSE) == FAIL)
2823 {
2824 curbuf = savebuf;
2825 free_array(array);
2826 raise_vim_exn(_("cannot insert line"));
2827 }
2828
2829 if (i > 0)
2830 appended_lines_mark((linenr_T)n, (long)i);
2831 }
2832 free_array(array);
2833 MZ_GC_UNREG();
2834 curbuf = savebuf;
2835 update_screen(VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002836 }
2837
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002838 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002839 raise_if_error();
2840 return scheme_void;
2841}
2842
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002843/*
2844 * Predicates
2845 */
2846/* (buff? obj) */
2847 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002848vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002849{
2850 if (SCHEME_VIMBUFFERP(argv[0]))
2851 return scheme_true;
2852 else
2853 return scheme_false;
2854}
2855
2856/* (win? obj) */
2857 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002858vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002859{
2860 if (SCHEME_VIMWINDOWP(argv[0]))
2861 return scheme_true;
2862 else
2863 return scheme_false;
2864}
2865
2866/* (buff-valid? obj) */
2867 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002868vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002869{
2870 if (SCHEME_VIMBUFFERP(argv[0])
2871 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2872 return scheme_true;
2873 else
2874 return scheme_false;
2875}
2876
2877/* (win-valid? obj) */
2878 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002879vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002880{
2881 if (SCHEME_VIMWINDOWP(argv[0])
2882 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2883 return scheme_true;
2884 else
2885 return scheme_false;
2886}
2887
2888/*
2889 *===========================================================================
2890 * Utilities
2891 *===========================================================================
2892 */
2893
2894/*
2895 * Convert an MzScheme string into a Vim line.
2896 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002897 * All internal nulls are replaced by newline characters.
2898 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002899 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002900 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002901 */
2902 static char *
2903string_to_line(Scheme_Object *obj)
2904{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002905 char *scheme_str = NULL;
2906 char *vim_str = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01002907 OUTPUT_LEN_TYPE len;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002908 int i;
2909
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002910 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002911
2912 /* Error checking: String must not contain newlines, as we
2913 * are replacing a single line, and we must replace it with
2914 * a single line.
2915 */
Bram Moolenaar75676462013-01-30 14:55:42 +01002916 if (memchr(scheme_str, '\n', len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002917 scheme_signal_error(_("string cannot contain newlines"));
2918
Bram Moolenaar75676462013-01-30 14:55:42 +01002919 vim_str = (char *)alloc(len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002920
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002921 /* Create a copy of the string, with internal nulls replaced by
2922 * newline characters, as is the vim convention.
2923 */
2924 for (i = 0; i < len; ++i)
2925 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002926 if (scheme_str[i] == '\0')
2927 vim_str[i] = '\n';
2928 else
2929 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002930 }
2931
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002932 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002933
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002934 MZ_GC_CHECK();
2935 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002936}
2937
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002938#ifdef FEAT_EVAL
2939/*
2940 * Convert Vim value into MzScheme, adopted from if_python.c
2941 */
2942 static Scheme_Object *
Bram Moolenaar75676462013-01-30 14:55:42 +01002943vim_to_mzscheme(typval_T *vim_value)
2944{
2945 Scheme_Object *result = NULL;
2946 /* hash table to store visited values to avoid infinite loops */
2947 Scheme_Hash_Table *visited = NULL;
2948
2949 MZ_GC_DECL_REG(2);
2950 MZ_GC_VAR_IN_REG(0, result);
2951 MZ_GC_VAR_IN_REG(1, visited);
2952 MZ_GC_REG();
2953
2954 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2955 MZ_GC_CHECK();
2956
2957 result = vim_to_mzscheme_impl(vim_value, 1, visited);
2958
2959 MZ_GC_UNREG();
2960 return result;
2961}
2962
2963 static Scheme_Object *
2964vim_to_mzscheme_impl(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002965{
2966 Scheme_Object *result = NULL;
2967 int new_value = TRUE;
2968
Bram Moolenaar75676462013-01-30 14:55:42 +01002969 MZ_GC_DECL_REG(2);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002970 MZ_GC_VAR_IN_REG(0, result);
Bram Moolenaar75676462013-01-30 14:55:42 +01002971 MZ_GC_VAR_IN_REG(1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002972 MZ_GC_REG();
2973
2974 /* Avoid infinite recursion */
2975 if (depth > 100)
2976 {
2977 MZ_GC_UNREG();
2978 return scheme_void;
2979 }
2980
2981 /* Check if we run into a recursive loop. The item must be in visited
2982 * then and we can use it again.
2983 */
2984 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
2985 MZ_GC_CHECK();
2986 if (result != NULL) /* found, do nothing */
2987 new_value = FALSE;
2988 else if (vim_value->v_type == VAR_STRING)
2989 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002990 result = scheme_make_byte_string((char *)vim_value->vval.v_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002991 MZ_GC_CHECK();
2992 }
2993 else if (vim_value->v_type == VAR_NUMBER)
2994 {
2995 result = scheme_make_integer((long)vim_value->vval.v_number);
2996 MZ_GC_CHECK();
2997 }
2998# ifdef FEAT_FLOAT
2999 else if (vim_value->v_type == VAR_FLOAT)
3000 {
3001 result = scheme_make_double((double)vim_value->vval.v_float);
3002 MZ_GC_CHECK();
3003 }
3004# endif
3005 else if (vim_value->v_type == VAR_LIST)
3006 {
3007 list_T *list = vim_value->vval.v_list;
3008 listitem_T *curr;
3009
3010 if (list == NULL || list->lv_first == NULL)
3011 result = scheme_null;
3012 else
3013 {
3014 Scheme_Object *obj = NULL;
3015
3016 MZ_GC_DECL_REG(1);
3017 MZ_GC_VAR_IN_REG(0, obj);
3018 MZ_GC_REG();
3019
3020 curr = list->lv_last;
Bram Moolenaar75676462013-01-30 14:55:42 +01003021 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003022 result = scheme_make_pair(obj, scheme_null);
3023 MZ_GC_CHECK();
3024
3025 while (curr != list->lv_first)
3026 {
3027 curr = curr->li_prev;
Bram Moolenaar75676462013-01-30 14:55:42 +01003028 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003029 result = scheme_make_pair(obj, result);
3030 MZ_GC_CHECK();
3031 }
3032 }
3033 MZ_GC_UNREG();
3034 }
3035 else if (vim_value->v_type == VAR_DICT)
3036 {
3037 Scheme_Object *key = NULL;
3038 Scheme_Object *obj = NULL;
3039
3040 MZ_GC_DECL_REG(2);
3041 MZ_GC_VAR_IN_REG(0, key);
3042 MZ_GC_VAR_IN_REG(1, obj);
3043 MZ_GC_REG();
3044
3045 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
3046 MZ_GC_CHECK();
3047 if (vim_value->vval.v_dict != NULL)
3048 {
3049 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
3050 long_u todo = ht->ht_used;
3051 hashitem_T *hi;
3052 dictitem_T *di;
3053
3054 for (hi = ht->ht_array; todo > 0; ++hi)
3055 {
3056 if (!HASHITEM_EMPTY(hi))
3057 {
3058 --todo;
3059
3060 di = dict_lookup(hi);
Bram Moolenaar75676462013-01-30 14:55:42 +01003061 obj = vim_to_mzscheme_impl(&di->di_tv, depth + 1, visited);
3062 key = scheme_make_byte_string((char *)hi->hi_key);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003063 MZ_GC_CHECK();
3064 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
3065 MZ_GC_CHECK();
3066 }
3067 }
3068 }
3069 MZ_GC_UNREG();
3070 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003071 else if (vim_value->v_type == VAR_FUNC)
3072 {
3073 Scheme_Object *funcname = NULL;
3074
3075 MZ_GC_DECL_REG(1);
3076 MZ_GC_VAR_IN_REG(0, funcname);
3077 MZ_GC_REG();
3078
3079 funcname = scheme_make_byte_string((char *)vim_value->vval.v_string);
3080 MZ_GC_CHECK();
3081 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3082 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3083 MZ_GC_CHECK();
3084
3085 MZ_GC_UNREG();
3086 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003087 else if (vim_value->v_type == VAR_SPECIAL)
3088 {
3089 if (vim_value->vval.v_number <= VVAL_TRUE)
3090 result = scheme_make_integer((long)vim_value->vval.v_number);
3091 else
3092 result = scheme_null;
3093 MZ_GC_CHECK();
3094 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003095 else
3096 {
3097 result = scheme_void;
3098 new_value = FALSE;
3099 }
3100 if (new_value)
3101 {
3102 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
3103 MZ_GC_CHECK();
3104 }
3105 MZ_GC_UNREG();
3106 return result;
3107}
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003108
3109 static int
Bram Moolenaar75676462013-01-30 14:55:42 +01003110mzscheme_to_vim(Scheme_Object *obj, typval_T *tv)
3111{
3112 int i, status;
3113 Scheme_Hash_Table *visited = NULL;
3114
3115 MZ_GC_DECL_REG(2);
3116 MZ_GC_VAR_IN_REG(0, obj);
3117 MZ_GC_VAR_IN_REG(1, visited);
3118 MZ_GC_REG();
3119
3120 visited = scheme_make_hash_table(SCHEME_hash_ptr);
3121 MZ_GC_CHECK();
3122
3123 status = mzscheme_to_vim_impl(obj, tv, 1, visited);
3124 for (i = 0; i < visited->size; ++i)
3125 {
3126 /* free up remembered objects */
3127 if (visited->vals[i] != NULL)
3128 free_tv((typval_T *)visited->vals[i]);
3129 }
3130
3131 MZ_GC_UNREG();
3132 return status;
3133}
3134 static int
3135mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003136 Scheme_Hash_Table *visited)
3137{
3138 int status = OK;
3139 typval_T *found;
Bram Moolenaar75676462013-01-30 14:55:42 +01003140
3141 MZ_GC_DECL_REG(2);
3142 MZ_GC_VAR_IN_REG(0, obj);
3143 MZ_GC_VAR_IN_REG(1, visited);
3144 MZ_GC_REG();
3145
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003146 MZ_GC_CHECK();
3147 if (depth > 100) /* limit the deepest recursion level */
3148 {
3149 tv->v_type = VAR_NUMBER;
3150 tv->vval.v_number = 0;
3151 return FAIL;
3152 }
3153
3154 found = (typval_T *)scheme_hash_get(visited, obj);
3155 if (found != NULL)
3156 copy_tv(found, tv);
3157 else if (SCHEME_VOIDP(obj))
3158 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003159 tv->v_type = VAR_SPECIAL;
3160 tv->vval.v_number = VVAL_NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003161 }
3162 else if (SCHEME_INTP(obj))
3163 {
3164 tv->v_type = VAR_NUMBER;
3165 tv->vval.v_number = SCHEME_INT_VAL(obj);
3166 }
3167 else if (SCHEME_BOOLP(obj))
3168 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003169 tv->v_type = VAR_SPECIAL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003170 tv->vval.v_number = SCHEME_TRUEP(obj);
3171 }
3172# ifdef FEAT_FLOAT
3173 else if (SCHEME_DBLP(obj))
3174 {
3175 tv->v_type = VAR_FLOAT;
3176 tv->vval.v_float = SCHEME_DBL_VAL(obj);
3177 }
3178# endif
Bram Moolenaar75676462013-01-30 14:55:42 +01003179 else if (SCHEME_BYTE_STRINGP(obj))
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003180 {
3181 tv->v_type = VAR_STRING;
Bram Moolenaar75676462013-01-30 14:55:42 +01003182 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(obj));
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003183 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003184# if MZSCHEME_VERSION_MAJOR >= 299
3185 else if (SCHEME_CHAR_STRINGP(obj))
3186 {
3187 Scheme_Object *tmp = NULL;
3188 MZ_GC_DECL_REG(1);
3189 MZ_GC_VAR_IN_REG(0, tmp);
3190 MZ_GC_REG();
3191
3192 tmp = scheme_char_string_to_byte_string(obj);
3193 tv->v_type = VAR_STRING;
3194 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(tmp));
3195 MZ_GC_UNREG();
3196 }
3197#endif
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003198 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
3199 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3200 {
3201 list_T *list = list_alloc();
3202 if (list == NULL)
3203 status = FAIL;
3204 else
3205 {
3206 int i;
3207 Scheme_Object *curr = NULL;
3208 Scheme_Object *cval = NULL;
3209 /* temporary var to hold current element of vectors and pairs */
3210 typval_T *v;
3211
3212 MZ_GC_DECL_REG(2);
3213 MZ_GC_VAR_IN_REG(0, curr);
3214 MZ_GC_VAR_IN_REG(1, cval);
3215 MZ_GC_REG();
3216
3217 tv->v_type = VAR_LIST;
3218 tv->vval.v_list = list;
3219 ++list->lv_refcount;
3220
3221 v = (typval_T *)alloc(sizeof(typval_T));
3222 if (v == NULL)
3223 status = FAIL;
3224 else
3225 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003226 /* add the value in advance to allow handling of self-referential
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003227 * data structures */
3228 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3229 copy_tv(tv, visited_tv);
3230 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3231
3232 if (SCHEME_VECTORP(obj))
3233 {
3234 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
3235 {
3236 cval = SCHEME_VEC_ELS(obj)[i];
Bram Moolenaar75676462013-01-30 14:55:42 +01003237 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003238 if (status == FAIL)
3239 break;
3240 status = list_append_tv(list, v);
3241 clear_tv(v);
3242 if (status == FAIL)
3243 break;
3244 }
3245 }
3246 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3247 {
3248 for (curr = obj;
3249 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
3250 curr = SCHEME_CDR(curr))
3251 {
3252 cval = SCHEME_CAR(curr);
Bram Moolenaar75676462013-01-30 14:55:42 +01003253 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003254 if (status == FAIL)
3255 break;
3256 status = list_append_tv(list, v);
3257 clear_tv(v);
3258 if (status == FAIL)
3259 break;
3260 }
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003261 /* improper list not terminated with null
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003262 * need to handle the last element */
3263 if (status == OK && !SCHEME_NULLP(curr))
3264 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003265 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003266 if (status == OK)
3267 {
3268 status = list_append_tv(list, v);
3269 clear_tv(v);
3270 }
3271 }
3272 }
3273 /* nothing to do for scheme_null */
3274 vim_free(v);
3275 }
3276 MZ_GC_UNREG();
3277 }
3278 }
3279 else if (SCHEME_HASHTP(obj))
3280 {
3281 int i;
3282 dict_T *dict;
3283 Scheme_Object *key = NULL;
3284 Scheme_Object *val = NULL;
3285
3286 MZ_GC_DECL_REG(2);
3287 MZ_GC_VAR_IN_REG(0, key);
3288 MZ_GC_VAR_IN_REG(1, val);
3289 MZ_GC_REG();
3290
3291 dict = dict_alloc();
3292 if (dict == NULL)
3293 status = FAIL;
3294 else
3295 {
3296 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3297
3298 tv->v_type = VAR_DICT;
3299 tv->vval.v_dict = dict;
3300 ++dict->dv_refcount;
3301
3302 copy_tv(tv, visited_tv);
3303 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3304
3305 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
3306 {
3307 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
3308 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003309 /* generate item for `display'ed Scheme key */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003310 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
3311 ((Scheme_Hash_Table *) obj)->keys[i]));
3312 /* convert Scheme val to Vim and add it to the dict */
Bram Moolenaar75676462013-01-30 14:55:42 +01003313 if (mzscheme_to_vim_impl(((Scheme_Hash_Table *) obj)->vals[i],
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003314 &item->di_tv, depth + 1, visited) == FAIL
3315 || dict_add(dict, item) == FAIL)
3316 {
3317 dictitem_free(item);
3318 status = FAIL;
3319 break;
3320 }
3321 }
3322
3323 }
3324 }
3325 MZ_GC_UNREG();
3326 }
3327 else
3328 {
3329 /* `display' any other value to string */
3330 tv->v_type = VAR_STRING;
3331 tv->vval.v_string = (char_u *)string_to_line(obj);
3332 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003333 MZ_GC_UNREG();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003334 return status;
3335}
3336
Bram Moolenaar75676462013-01-30 14:55:42 +01003337/* Scheme prim procedure wrapping Vim funcref */
3338 static Scheme_Object *
3339vim_funcref(void *name, int argc, Scheme_Object **argv)
3340{
3341 int i;
3342 typval_T args;
3343 int status = OK;
3344 Scheme_Object *result = NULL;
3345 list_T *list = list_alloc();
3346
3347 MZ_GC_DECL_REG(1);
3348 MZ_GC_VAR_IN_REG(0, result);
3349 MZ_GC_REG();
3350
3351 result = scheme_void;
3352 if (list == NULL)
3353 status = FAIL;
3354 else
3355 {
3356 args.v_type = VAR_LIST;
3357 args.vval.v_list = list;
3358 ++list->lv_refcount;
3359 for (i = 0; status == OK && i < argc; ++i)
3360 {
3361 typval_T *v = (typval_T *)alloc(sizeof(typval_T));
3362 if (v == NULL)
3363 status = FAIL;
3364 else
3365 {
3366 status = mzscheme_to_vim(argv[i], v);
3367 if (status == OK)
3368 {
3369 status = list_append_tv(list, v);
3370 clear_tv(v);
3371 }
3372 vim_free(v);
3373 }
3374 }
3375 if (status == OK)
3376 {
3377 typval_T ret;
3378 ret.v_type = VAR_UNKNOWN;
3379
3380 mzscheme_call_vim(BYTE_STRING_VALUE((Scheme_Object *)name), &args, &ret);
3381 MZ_GC_CHECK();
3382 result = vim_to_mzscheme(&ret);
3383 clear_tv(&ret);
3384 MZ_GC_CHECK();
3385 }
3386 }
3387 clear_tv(&args);
3388 MZ_GC_UNREG();
3389 if (status != OK)
3390 raise_vim_exn(_("error converting Scheme values to Vim"));
3391 else
3392 raise_if_error();
3393 return result;
3394}
3395
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003396 void
3397do_mzeval(char_u *str, typval_T *rettv)
3398{
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003399 Scheme_Object *ret = NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003400
Bram Moolenaar75676462013-01-30 14:55:42 +01003401 MZ_GC_DECL_REG(1);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003402 MZ_GC_VAR_IN_REG(0, ret);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003403 MZ_GC_REG();
3404
3405 if (mzscheme_init())
3406 {
3407 MZ_GC_UNREG();
3408 return;
3409 }
3410
3411 MZ_GC_CHECK();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003412 if (eval_with_exn_handling(str, do_eval, &ret) == OK)
Bram Moolenaar75676462013-01-30 14:55:42 +01003413 mzscheme_to_vim(ret, rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003414
3415 MZ_GC_UNREG();
3416}
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003417#endif
3418
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003419/*
3420 * Check to see whether a Vim error has been reported, or a keyboard
3421 * interrupt (from vim --> got_int) has been detected.
3422 */
3423 static int
3424vim_error_check(void)
3425{
3426 return (got_int || did_emsg);
3427}
3428
3429/*
3430 * register Scheme exn:vim
3431 */
3432 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003433register_vim_exn(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003434{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003435 int nc = 0;
3436 int i;
3437 Scheme_Object *struct_exn = NULL;
3438 Scheme_Object *exn_name = NULL;
3439
3440 MZ_GC_DECL_REG(2);
3441 MZ_GC_VAR_IN_REG(0, struct_exn);
3442 MZ_GC_VAR_IN_REG(1, exn_name);
3443 MZ_GC_REG();
3444
3445 exn_name = scheme_intern_symbol("exn:vim");
3446 MZ_GC_CHECK();
3447 struct_exn = scheme_builtin_value("struct:exn");
3448 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003449
3450 if (vim_exn == NULL)
3451 vim_exn = scheme_make_struct_type(exn_name,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003452 struct_exn, NULL, 0, 0, NULL, NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003453#if MZSCHEME_VERSION_MAJOR >= 299
3454 , NULL
3455#endif
3456 );
3457
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003458
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003459 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003460 Scheme_Object **tmp = NULL;
3461 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
3462 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
3463 MZ_GC_DECL_REG(6);
3464 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
3465 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
3466 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003467
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003468 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003469 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
3470 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003471
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003472 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
3473 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
3474 MZ_GC_CHECK();
3475
3476 for (i = 0; i < nc; i++)
3477 {
3478 scheme_add_global_symbol(exn_names[i],
3479 exn_values[i], environment);
3480 MZ_GC_CHECK();
3481 }
3482 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003483 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003484 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003485}
3486
3487/*
3488 * raise exn:vim, may be with additional info string
3489 */
3490 void
3491raise_vim_exn(const char *add_info)
3492{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003493 char *fmt = _("Vim error: ~a");
3494 Scheme_Object *argv[2] = {NULL, NULL};
3495 Scheme_Object *exn = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01003496 Scheme_Object *byte_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003497
Bram Moolenaar75676462013-01-30 14:55:42 +01003498 MZ_GC_DECL_REG(5);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003499 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
3500 MZ_GC_VAR_IN_REG(3, exn);
Bram Moolenaar75676462013-01-30 14:55:42 +01003501 MZ_GC_VAR_IN_REG(4, byte_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003502 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003503
3504 if (add_info != NULL)
3505 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003506 char *c_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003507 Scheme_Object *info = NULL;
3508
3509 MZ_GC_DECL_REG(3);
3510 MZ_GC_VAR_IN_REG(0, c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003511 MZ_GC_VAR_IN_REG(2, info);
3512 MZ_GC_REG();
3513
Bram Moolenaar75676462013-01-30 14:55:42 +01003514 info = scheme_make_byte_string(add_info);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003515 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01003516 c_string = scheme_format_utf8(fmt, STRLEN(fmt), 1, &info, NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003517 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01003518 byte_string = scheme_make_byte_string(c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003519 MZ_GC_CHECK();
3520 argv[0] = scheme_byte_string_to_char_string(byte_string);
Bram Moolenaar555b2802005-05-19 21:08:39 +00003521 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003522 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003523 }
3524 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003525 {
3526 byte_string = scheme_make_byte_string(_("Vim error"));
3527 MZ_GC_CHECK();
3528 argv[0] = scheme_byte_string_to_char_string(byte_string);
3529 MZ_GC_CHECK();
3530 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003531 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003532
Bram Moolenaar049377e2007-05-12 15:32:12 +00003533#if MZSCHEME_VERSION_MAJOR < 360
3534 argv[1] = scheme_current_continuation_marks();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003535 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003536#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003537 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003538 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003539#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003540
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003541 exn = scheme_make_struct_instance(vim_exn, 2, argv);
3542 MZ_GC_CHECK();
3543 scheme_raise(exn);
3544 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003545}
3546
3547 void
3548raise_if_error(void)
3549{
3550 if (vim_error_check())
3551 raise_vim_exn(NULL);
3552}
3553
3554/* get buffer:
3555 * either current
3556 * or passed as argv[argnum] with checks
3557 */
3558 static vim_mz_buffer *
3559get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3560{
3561 vim_mz_buffer *b;
3562
3563 if (argc < argnum + 1)
3564 return get_vim_curr_buffer();
3565 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3566 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3567 b = (vim_mz_buffer *)argv[argnum];
3568 (void)get_valid_buffer(argv[argnum]);
3569 return b;
3570}
3571
3572/* get window:
3573 * either current
3574 * or passed as argv[argnum] with checks
3575 */
3576 static vim_mz_window *
3577get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3578{
3579 vim_mz_window *w;
3580
3581 if (argc < argnum + 1)
3582 return get_vim_curr_window();
3583 w = (vim_mz_window *)argv[argnum];
3584 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3585 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3586 (void)get_valid_window(argv[argnum]);
3587 return w;
3588}
3589
3590/* get valid Vim buffer from Scheme_Object* */
3591buf_T *get_valid_buffer(void *obj)
3592{
3593 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3594
3595 if (buf == INVALID_BUFFER_VALUE)
3596 scheme_signal_error(_("buffer is invalid"));
3597 return buf;
3598}
3599
3600/* get valid Vim window from Scheme_Object* */
3601win_T *get_valid_window(void *obj)
3602{
3603 win_T *win = ((vim_mz_window *)obj)->win;
3604 if (win == INVALID_WINDOW_VALUE)
3605 scheme_signal_error(_("window is invalid"));
3606 return win;
3607}
3608
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003609 int
3610mzthreads_allowed(void)
3611{
3612 return mz_threads_allow;
3613}
3614
3615 static int
3616line_in_range(linenr_T lnum, buf_T *buf)
3617{
3618 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3619}
3620
3621 static void
3622check_line_range(linenr_T lnum, buf_T *buf)
3623{
3624 if (!line_in_range(lnum, buf))
3625 scheme_signal_error(_("linenr out of range"));
3626}
3627
3628/*
3629 * Check if deleting lines made the cursor position invalid
3630 * (or you'll get msg from Vim about invalid linenr).
3631 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3632 * deleted). Got from if_python.c
3633 */
3634 static void
3635mz_fix_cursor(int lo, int hi, int extra)
3636{
3637 if (curwin->w_cursor.lnum >= lo)
3638 {
3639 /* Adjust the cursor position if it's in/after the changed
3640 * lines. */
3641 if (curwin->w_cursor.lnum >= hi)
3642 {
3643 curwin->w_cursor.lnum += extra;
3644 check_cursor_col();
3645 }
3646 else if (extra < 0)
3647 {
3648 curwin->w_cursor.lnum = lo;
3649 check_cursor();
3650 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003651 else
3652 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003653 changed_cline_bef_curs();
3654 }
3655 invalidate_botline();
3656}
3657
3658static Vim_Prim prims[]=
3659{
3660 /*
3661 * Buffer-related commands
3662 */
3663 {get_buffer_line, "get-buff-line", 1, 2},
3664 {set_buffer_line, "set-buff-line", 2, 3},
3665 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3666 {get_buffer_name, "get-buff-name", 0, 1},
3667 {get_buffer_num, "get-buff-num", 0, 1},
3668 {get_buffer_size, "get-buff-size", 0, 1},
3669 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3670 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3671 {get_curr_buffer, "curr-buff", 0, 0},
3672 {get_buffer_count, "buff-count", 0, 0},
3673 {get_next_buffer, "get-next-buff", 0, 1},
3674 {get_prev_buffer, "get-prev-buff", 0, 1},
3675 {mzscheme_open_buffer, "open-buff", 1, 1},
3676 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3677 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003678 /*
3679 * Window-related commands
3680 */
3681 {get_curr_win, "curr-win", 0, 0},
3682 {get_window_count, "win-count", 0, 0},
3683 {get_window_by_num, "get-win-by-num", 1, 1},
3684 {get_window_num, "get-win-num", 0, 1},
3685 {get_window_buffer, "get-win-buffer", 0, 1},
3686 {get_window_height, "get-win-height", 0, 1},
3687 {set_window_height, "set-win-height", 1, 2},
3688#ifdef FEAT_VERTSPLIT
3689 {get_window_width, "get-win-width", 0, 1},
3690 {set_window_width, "set-win-width", 1, 2},
3691#endif
3692 {get_cursor, "get-cursor", 0, 1},
3693 {set_cursor, "set-cursor", 1, 2},
3694 {get_window_list, "get-win-list", 0, 1},
3695 /*
3696 * Vim-related commands
3697 */
3698 {vim_command, "command", 1, 1},
3699 {vim_eval, "eval", 1, 1},
3700 {get_range_start, "range-start", 0, 0},
3701 {get_range_end, "range-end", 0, 0},
3702 {mzscheme_beep, "beep", 0, 0},
3703 {get_option, "get-option", 1, 2},
3704 {set_option, "set-option", 1, 2},
3705 /*
3706 * small utilities
3707 */
3708 {vim_bufferp, "buff?", 1, 1},
3709 {vim_windowp, "win?", 1, 1},
3710 {vim_buffer_validp, "buff-valid?", 1, 1},
3711 {vim_window_validp, "win-valid?", 1, 1}
3712};
3713
3714/* return MzScheme wrapper for curbuf */
3715 static vim_mz_buffer *
3716get_vim_curr_buffer(void)
3717{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003718 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003719 return (vim_mz_buffer *)buffer_new(curbuf);
3720 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003721 return BUFFER_REF(curbuf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003722}
3723
3724/* return MzScheme wrapper for curwin */
3725 static vim_mz_window *
3726get_vim_curr_window(void)
3727{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003728 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003729 return (vim_mz_window *)window_new(curwin);
3730 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003731 return WINDOW_REF(curwin);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003732}
3733
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003734 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003735make_modules()
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003736{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003737 int i;
3738 Scheme_Env *mod = NULL;
3739 Scheme_Object *vimext_symbol = NULL;
3740 Scheme_Object *closed_prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003741
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003742 MZ_GC_DECL_REG(3);
3743 MZ_GC_VAR_IN_REG(0, mod);
3744 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3745 MZ_GC_VAR_IN_REG(2, closed_prim);
3746 MZ_GC_REG();
3747
3748 vimext_symbol = scheme_intern_symbol("vimext");
3749 MZ_GC_CHECK();
3750 mod = scheme_primitive_module(vimext_symbol, environment);
3751 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003752 /* all prims made closed so they can access their own names */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003753 for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003754 {
3755 Vim_Prim *prim = prims + i;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003756 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3757 prim->mina, prim->maxa);
3758 scheme_add_global(prim->name, closed_prim, mod);
3759 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003760 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003761 scheme_finish_primitive_module(mod);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003762 MZ_GC_CHECK();
3763 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003764}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003765
Bram Moolenaar555b2802005-05-19 21:08:39 +00003766#ifdef HAVE_SANDBOX
3767static Scheme_Object *M_write = NULL;
3768static Scheme_Object *M_read = NULL;
3769static Scheme_Object *M_execute = NULL;
3770static Scheme_Object *M_delete = NULL;
3771
3772 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003773sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003774{
3775 if (sandbox)
3776 raise_vim_exn(_("not allowed in the Vim sandbox"));
3777}
3778
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003779/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003780 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003781sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003782{
3783 if (sandbox)
3784 {
3785 Scheme_Object *requested_access = argv[2];
3786
3787 if (M_write == NULL)
3788 {
3789 MZ_REGISTER_STATIC(M_write);
3790 M_write = scheme_intern_symbol("write");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003791 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003792 }
3793 if (M_read == NULL)
3794 {
3795 MZ_REGISTER_STATIC(M_read);
3796 M_read = scheme_intern_symbol("read");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003797 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003798 }
3799 if (M_execute == NULL)
3800 {
3801 MZ_REGISTER_STATIC(M_execute);
3802 M_execute = scheme_intern_symbol("execute");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003803 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003804 }
3805 if (M_delete == NULL)
3806 {
3807 MZ_REGISTER_STATIC(M_delete);
3808 M_delete = scheme_intern_symbol("delete");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003809 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003810 }
3811
3812 while (!SCHEME_NULLP(requested_access))
3813 {
3814 Scheme_Object *item = SCHEME_CAR(requested_access);
3815 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3816 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
3817 {
3818 raise_vim_exn(_("not allowed in the Vim sandbox"));
3819 }
3820 requested_access = SCHEME_CDR(requested_access);
3821 }
3822 }
3823 return scheme_void;
3824}
3825
3826 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003827sandbox_network_guard(int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003828{
3829 return scheme_void;
3830}
3831#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00003832
3833#endif