blob: f291da71b75db295549841a6680574a28948ea19 [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 Moolenaar4349c572016-01-30 13:28:28 +010032#ifdef PROTO
33typedef int Scheme_Object;
34typedef int Scheme_Closed_Prim;
35typedef int Scheme_Env;
36typedef int Scheme_Hash_Table;
37typedef int Scheme_Type;
38typedef int Scheme_Thread;
39typedef int Scheme_Closed_Prim;
40typedef int mzshort;
41typedef int Scheme_Prim;
42typedef int HINSTANCE;
43#endif
44
Bram Moolenaar4e640bd2016-01-16 16:20:38 +010045/*
46 * scheme_register_tls_space is only available on 32-bit Windows until
47 * racket-6.3. See
48 * http://docs.racket-lang.org/inside/im_memoryalloc.html?q=scheme_register_tls_space
49 */
50#if MZSCHEME_VERSION_MAJOR >= 500 && defined(WIN32) \
51 && defined(USE_THREAD_LOCAL) \
52 && (!defined(_WIN64) || MZSCHEME_VERSION_MAJOR >= 603)
53# define HAVE_TLS_SPACE 1
54#endif
55
56/*
57 * Since version 4.x precise GC requires trampolined startup.
58 * Futures and places in version 5.x need it too.
59 */
60#if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR >= 400 \
61 || MZSCHEME_VERSION_MAJOR >= 500 \
62 && (defined(MZ_USE_FUTURES) || defined(MZ_USE_PLACES))
63# define TRAMPOLINED_MZVIM_STARTUP
64#endif
65
Bram Moolenaar325b7a22004-07-05 15:58:32 +000066/* Base data structures */
67#define SCHEME_VIMBUFFERP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_buffer_type)
68#define SCHEME_VIMWINDOWP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_window_type)
69
70typedef struct
71{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000072 Scheme_Object so;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000073 buf_T *buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000074} vim_mz_buffer;
75
76#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
77
78typedef struct
79{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000080 Scheme_Object so;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000081 win_T *win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000082} vim_mz_window;
83
84#define INVALID_WINDOW_VALUE ((win_T *)(-1))
85
86/*
87 * Prims that form MzScheme Vim interface
88 */
89typedef struct
90{
91 Scheme_Closed_Prim *prim;
92 char *name;
93 int mina; /* arity information */
94 int maxa;
95} Vim_Prim;
96
97typedef struct
98{
99 char *name;
100 Scheme_Object *port;
101} Port_Info;
102
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000103/*
104 *========================================================================
105 * Vim-Control Commands
106 *========================================================================
107 */
108/*
109 *========================================================================
110 * Utility functions for the vim/mzscheme interface
111 *========================================================================
112 */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000113#ifdef HAVE_SANDBOX
114static Scheme_Object *sandbox_file_guard(int, Scheme_Object **);
115static Scheme_Object *sandbox_network_guard(int, Scheme_Object **);
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000116static void sandbox_check(void);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000117#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000118/* Buffer-related commands */
119static Scheme_Object *buffer_new(buf_T *buf);
120static Scheme_Object *get_buffer_by_name(void *, int, Scheme_Object **);
121static Scheme_Object *get_buffer_by_num(void *, int, Scheme_Object **);
122static Scheme_Object *get_buffer_count(void *, int, Scheme_Object **);
123static Scheme_Object *get_buffer_line(void *, int, Scheme_Object **);
124static Scheme_Object *get_buffer_line_list(void *, int, Scheme_Object **);
125static Scheme_Object *get_buffer_name(void *, int, Scheme_Object **);
126static Scheme_Object *get_buffer_num(void *, int, Scheme_Object **);
127static Scheme_Object *get_buffer_size(void *, int, Scheme_Object **);
128static Scheme_Object *get_curr_buffer(void *, int, Scheme_Object **);
129static Scheme_Object *get_next_buffer(void *, int, Scheme_Object **);
130static Scheme_Object *get_prev_buffer(void *, int, Scheme_Object **);
131static Scheme_Object *mzscheme_open_buffer(void *, int, Scheme_Object **);
132static Scheme_Object *set_buffer_line(void *, int, Scheme_Object **);
133static Scheme_Object *set_buffer_line_list(void *, int, Scheme_Object **);
134static Scheme_Object *insert_buffer_line_list(void *, int, Scheme_Object **);
135static Scheme_Object *get_range_start(void *, int, Scheme_Object **);
136static Scheme_Object *get_range_end(void *, int, Scheme_Object **);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000137static vim_mz_buffer *get_vim_curr_buffer(void);
138
139/* Window-related commands */
140static Scheme_Object *window_new(win_T *win);
141static Scheme_Object *get_curr_win(void *, int, Scheme_Object **);
142static Scheme_Object *get_window_count(void *, int, Scheme_Object **);
143static Scheme_Object *get_window_by_num(void *, int, Scheme_Object **);
144static Scheme_Object *get_window_num(void *, int, Scheme_Object **);
145static Scheme_Object *get_window_buffer(void *, int, Scheme_Object **);
146static Scheme_Object *get_window_height(void *, int, Scheme_Object **);
147static Scheme_Object *set_window_height(void *, int, Scheme_Object **);
148#ifdef FEAT_VERTSPLIT
149static Scheme_Object *get_window_width(void *, int, Scheme_Object **);
150static Scheme_Object *set_window_width(void *, int, Scheme_Object **);
151#endif
152static Scheme_Object *get_cursor(void *, int, Scheme_Object **);
153static Scheme_Object *set_cursor(void *, int, Scheme_Object **);
154static Scheme_Object *get_window_list(void *, int, Scheme_Object **);
155static vim_mz_window *get_vim_curr_window(void);
156
157/* Vim-related commands */
158static Scheme_Object *mzscheme_beep(void *, int, Scheme_Object **);
159static Scheme_Object *get_option(void *, int, Scheme_Object **);
160static Scheme_Object *set_option(void *, int, Scheme_Object **);
161static Scheme_Object *vim_command(void *, int, Scheme_Object **);
162static Scheme_Object *vim_eval(void *, int, Scheme_Object **);
163static Scheme_Object *vim_bufferp(void *data, int, Scheme_Object **);
164static Scheme_Object *vim_windowp(void *data, int, Scheme_Object **);
165static Scheme_Object *vim_buffer_validp(void *data, int, Scheme_Object **);
166static Scheme_Object *vim_window_validp(void *data, int, Scheme_Object **);
167
168/*
169 *========================================================================
170 * Internal Function Prototypes
171 *========================================================================
172 */
173static int vim_error_check(void);
174static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100175static int startup_mzscheme(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000176static char *string_to_line(Scheme_Object *obj);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100177#if MZSCHEME_VERSION_MAJOR >= 501
Bram Moolenaar75676462013-01-30 14:55:42 +0100178# define OUTPUT_LEN_TYPE intptr_t
179#else
180# define OUTPUT_LEN_TYPE long
181#endif
182static void do_output(char *mesg, OUTPUT_LEN_TYPE len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000183static void do_printf(char *format, ...);
184static void do_flush(void);
185static Scheme_Object *_apply_thunk_catch_exceptions(
186 Scheme_Object *, Scheme_Object **);
187static Scheme_Object *extract_exn_message(Scheme_Object *v);
188static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
189static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000190static void register_vim_exn(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000191static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
192 int argc, Scheme_Object **argv);
193static vim_mz_window *get_window_arg(const char *fname, int argnum,
194 int argc, Scheme_Object **argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000195static int line_in_range(linenr_T, buf_T *);
196static void check_line_range(linenr_T, buf_T *);
197static void mz_fix_cursor(int lo, int hi, int extra);
198
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000199static int eval_with_exn_handling(void *, Scheme_Closed_Prim *,
200 Scheme_Object **ret);
201static void make_modules(void);
202static void init_exn_catching_apply(void);
203static int mzscheme_env_main(Scheme_Env *env, int argc, char **argv);
204static int mzscheme_init(void);
205#ifdef FEAT_EVAL
Bram Moolenaar75676462013-01-30 14:55:42 +0100206static Scheme_Object *vim_to_mzscheme(typval_T *vim_value);
207static Scheme_Object *vim_to_mzscheme_impl(typval_T *vim_value, int depth,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000208 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100209static int mzscheme_to_vim(Scheme_Object *obj, typval_T *tv);
210static int mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100211 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100212static Scheme_Object *vim_funcref(void *data, int argc, Scheme_Object **argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000213#endif
214
215#ifdef MZ_PRECISE_GC
Bram Moolenaar64404472010-06-26 06:24:45 +0200216static int buffer_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000217{
218 return gcBYTES_TO_WORDS(sizeof(vim_mz_buffer));
219}
220static int buffer_mark_proc(void *obj)
221{
222 return buffer_size_proc(obj);
223}
224static int buffer_fixup_proc(void *obj)
225{
Bram Moolenaar75676462013-01-30 14:55:42 +0100226 /* apparently not needed as the object will be uncollectable while
227 * the buffer is alive
228 */
229 /*
230 vim_mz_buffer* buf = (vim_mz_buffer*) obj;
231 buf->buf->b_mzscheme_ref = GC_fixup_self(obj);
232 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000233 return buffer_size_proc(obj);
234}
Bram Moolenaar64404472010-06-26 06:24:45 +0200235static int window_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000236{
237 return gcBYTES_TO_WORDS(sizeof(vim_mz_window));
238}
239static int window_mark_proc(void *obj)
240{
241 return window_size_proc(obj);
242}
243static int window_fixup_proc(void *obj)
244{
Bram Moolenaar75676462013-01-30 14:55:42 +0100245 /* apparently not needed as the object will be uncollectable while
246 * the window is alive
247 */
248 /*
249 vim_mz_window* win = (vim_mz_window*) obj;
250 win->win->w_mzscheme_ref = GC_fixup_self(obj);
251 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000252 return window_size_proc(obj);
253}
Bram Moolenaar75676462013-01-30 14:55:42 +0100254/* with precise GC, w_mzscheme_ref and b_mzscheme_ref are immobile boxes
255 * containing pointers to a window/buffer
256 * with conservative GC these are simply pointers*/
257# define WINDOW_REF(win) *(vim_mz_window **)((win)->w_mzscheme_ref)
258# define BUFFER_REF(buf) *(vim_mz_buffer **)((buf)->b_mzscheme_ref)
259#else
260# define WINDOW_REF(win) (vim_mz_window *)((win)->w_mzscheme_ref)
261# define BUFFER_REF(buf) (vim_mz_buffer *)((buf)->b_mzscheme_ref)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000262#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000263
Bram Moolenaar4349c572016-01-30 13:28:28 +0100264#if defined(DYNAMIC_MZSCHEME) || defined(PROTO)
Bram Moolenaar33570922005-01-25 22:26:29 +0000265static Scheme_Object *dll_scheme_eof;
266static Scheme_Object *dll_scheme_false;
267static Scheme_Object *dll_scheme_void;
268static Scheme_Object *dll_scheme_null;
269static Scheme_Object *dll_scheme_true;
270
271static Scheme_Thread **dll_scheme_current_thread_ptr;
272
273static void (**dll_scheme_console_printf_ptr)(char *str, ...);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100274static void (**dll_scheme_console_output_ptr)(char *str, OUTPUT_LEN_TYPE len);
Bram Moolenaar33570922005-01-25 22:26:29 +0000275static void (**dll_scheme_notify_multithread_ptr)(int on);
276
277static void *(*dll_GC_malloc)(size_t size_in_bytes);
278static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
279static Scheme_Env *(*dll_scheme_basic_env)(void);
280static void (*dll_scheme_check_threads)(void);
281static void (*dll_scheme_register_static)(void *ptr, long size);
282static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
283static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
284 Scheme_Env *env);
285static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
286 Scheme_Object *val, Scheme_Env *env);
287static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
288 Scheme_Object **rands);
289static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000290# if MZSCHEME_VERSION_MAJOR >= 299
291static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100292static Scheme_Object *(*dll_scheme_make_path)(const char *chars);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000293# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000294static void (*dll_scheme_close_input_port)(Scheme_Object *port);
295static void (*dll_scheme_count_lines)(Scheme_Object *port);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000296#if MZSCHEME_VERSION_MAJOR < 360
Bram Moolenaar33570922005-01-25 22:26:29 +0000297static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000298#else
299static Scheme_Object *(*dll_scheme_current_continuation_marks)(Scheme_Object *prompt_tag);
300#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000301static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100302static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, OUTPUT_LEN_TYPE *len);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000303static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
Bram Moolenaar33570922005-01-25 22:26:29 +0000304static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
305 int _num_rands, Scheme_Object **rands, int val);
306static void (*dll_scheme_dont_gc_ptr)(void *p);
307static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
308static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
309 Scheme_Env *env);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000310static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
Bram Moolenaar33570922005-01-25 22:26:29 +0000311 Scheme_Env *env, int all);
312static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000313# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000314static char *(*dll_scheme_format)(char *format, int flen, int argc,
315 Scheme_Object **argv, long *rlen);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000316# else
317static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100318 Scheme_Object **argv, OUTPUT_LEN_TYPE *rlen);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000319static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000320# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000321static void (*dll_scheme_gc_ptr_ok)(void *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000322# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000323static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
324 long *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000325# else
326static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100327 OUTPUT_LEN_TYPE *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000328# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000329static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
330static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
331 Scheme_Env *env);
332static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
333 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
334 mzshort maxa);
335static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000336static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
Bram Moolenaar33570922005-01-25 22:26:29 +0000337 Scheme_Object *cdr);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000338static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
339 const char *name, mzshort mina, mzshort maxa);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000340# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000341static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
342static Scheme_Object *(*dll_scheme_make_string_output_port)();
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000343# else
344static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
345static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
346# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000347static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
348 int argc, Scheme_Object **argv);
349static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
350 Scheme_Object *field_names, int flags, int *count_out);
351static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
352 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
353 int num_uninit_fields, Scheme_Object *uninit_val,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000354 Scheme_Object *properties
355# if MZSCHEME_VERSION_MAJOR >= 299
356 , Scheme_Object *guard
357# endif
358 );
Bram Moolenaar33570922005-01-25 22:26:29 +0000359static Scheme_Object **(*dll_scheme_make_struct_values)(
360 Scheme_Object *struct_type, Scheme_Object **names, int count,
361 int flags);
362static Scheme_Type (*dll_scheme_make_type)(const char *name);
363static Scheme_Object *(*dll_scheme_make_vector)(int size,
364 Scheme_Object *fill);
365static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
366static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
367 const char *who);
368static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
369 Scheme_Env *for_env);
370static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
371static void (*dll_scheme_raise)(Scheme_Object *exn);
372static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
373static void (*dll_scheme_signal_error)(const char *msg, ...);
374static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
375 int which, int argc, Scheme_Object **argv);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000376# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000377static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000378 Scheme_Object *o);
379static Scheme_Config *(*dll_scheme_current_config)(void);
380static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
381 (Scheme_Object *s);
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000382static Scheme_Object *(*dll_scheme_char_string_to_path)
383 (Scheme_Object *s);
Bram Moolenaar75676462013-01-30 14:55:42 +0100384static void *(*dll_scheme_set_collects_path)(Scheme_Object *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000385# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000386static Scheme_Hash_Table *(*dll_scheme_make_hash_table)(int type);
387static void (*dll_scheme_hash_set)(Scheme_Hash_Table *table,
388 Scheme_Object *key, Scheme_Object *value);
389static Scheme_Object *(*dll_scheme_hash_get)(Scheme_Hash_Table *table,
390 Scheme_Object *key);
391static Scheme_Object *(*dll_scheme_make_double)(double d);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000392static Scheme_Object *(*dll_scheme_make_sized_byte_string)(char *chars,
393 long len, int copy);
394static Scheme_Object *(*dll_scheme_namespace_require)(Scheme_Object *req);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100395static 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);
396# ifdef MZ_PRECISE_GC
397static void *(*dll_GC_malloc_one_tagged)(size_t size_in_bytes);
398static void (*dll_GC_register_traversers)(short tag, Size_Proc size, Mark_Proc mark, Fixup_Proc fixup, int is_constant_size, int is_atomic);
399# endif
400# if MZSCHEME_VERSION_MAJOR >= 400
401static void (*dll_scheme_init_collection_paths)(Scheme_Env *global_env, Scheme_Object *extra_dirs);
402static void **(*dll_scheme_malloc_immobile_box)(void *p);
403static void (*dll_scheme_free_immobile_box)(void **b);
404# endif
405# if MZSCHEME_VERSION_MAJOR >= 500
406# ifdef TRAMPOLINED_MZVIM_STARTUP
407static int (*dll_scheme_main_setup)(int no_auto_statics, Scheme_Env_Main _main, int argc, char **argv);
408# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
409static void (*dll_scheme_register_tls_space)(void *tls_space, int _tls_index);
410# endif
411# endif
412# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
413static Thread_Local_Variables *(*dll_scheme_external_get_thread_local_variables)(void);
414# endif
415# endif
416# if MZSCHEME_VERSION_MAJOR >= 600
417static void (*dll_scheme_embedded_load)(intptr_t len, const char *s, int predefined);
418static void (*dll_scheme_register_embedded_load)(intptr_t len, const char *s);
419static void (*dll_scheme_set_config_path)(Scheme_Object *p);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000420# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000421
Bram Moolenaar4349c572016-01-30 13:28:28 +0100422#if defined(DYNAMIC_MZSCHEME) /* not when defined(PROTO) */
423
Bram Moolenaar33570922005-01-25 22:26:29 +0000424/* arrays are imported directly */
425# define scheme_eof dll_scheme_eof
426# define scheme_false dll_scheme_false
427# define scheme_void dll_scheme_void
428# define scheme_null dll_scheme_null
429# define scheme_true dll_scheme_true
430
431/* pointers are GetProceAddress'ed as pointers to pointer */
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100432#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
433# define scheme_current_thread (*dll_scheme_current_thread_ptr)
434# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000435# define scheme_console_printf (*dll_scheme_console_printf_ptr)
436# define scheme_console_output (*dll_scheme_console_output_ptr)
437# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
438
439/* and functions in a usual way */
440# define GC_malloc dll_GC_malloc
441# define GC_malloc_atomic dll_GC_malloc_atomic
442
443# define scheme_add_global dll_scheme_add_global
444# define scheme_add_global_symbol dll_scheme_add_global_symbol
445# define scheme_apply dll_scheme_apply
446# define scheme_basic_env dll_scheme_basic_env
447# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000448# if MZSCHEME_VERSION_MAJOR >= 299
449# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100450# define scheme_make_path dll_scheme_make_path
Bram Moolenaar555b2802005-05-19 21:08:39 +0000451# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000452# define scheme_check_threads dll_scheme_check_threads
453# define scheme_close_input_port dll_scheme_close_input_port
454# define scheme_count_lines dll_scheme_count_lines
455# define scheme_current_continuation_marks \
456 dll_scheme_current_continuation_marks
457# define scheme_display dll_scheme_display
458# define scheme_display_to_string dll_scheme_display_to_string
459# define scheme_do_eval dll_scheme_do_eval
460# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000461# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000462# define scheme_eval dll_scheme_eval
463# define scheme_eval_string dll_scheme_eval_string
464# define scheme_eval_string_all dll_scheme_eval_string_all
465# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000466# if MZSCHEME_VERSION_MAJOR < 299
467# define scheme_format dll_scheme_format
468# else
469# define scheme_format_utf8 dll_scheme_format_utf8
470# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000472# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100473# define scheme_get_sized_byte_string_output dll_scheme_get_sized_string_output
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000474# else
475# define scheme_get_sized_byte_string_output \
476 dll_scheme_get_sized_byte_string_output
Bram Moolenaar75676462013-01-30 14:55:42 +0100477# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000478# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479# define scheme_intern_symbol dll_scheme_intern_symbol
480# define scheme_lookup_global dll_scheme_lookup_global
481# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
482# define scheme_make_integer_value dll_scheme_make_integer_value
Bram Moolenaar33570922005-01-25 22:26:29 +0000483# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000484# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000485# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100486# define scheme_make_byte_string dll_scheme_make_string
487# define scheme_make_byte_string_output_port dll_scheme_make_string_output_port
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000488# else
489# define scheme_make_byte_string dll_scheme_make_byte_string
490# define scheme_make_byte_string_output_port \
491 dll_scheme_make_byte_string_output_port
492# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000493# define scheme_make_struct_instance dll_scheme_make_struct_instance
494# define scheme_make_struct_names dll_scheme_make_struct_names
495# define scheme_make_struct_type dll_scheme_make_struct_type
496# define scheme_make_struct_values dll_scheme_make_struct_values
497# define scheme_make_type dll_scheme_make_type
498# define scheme_make_vector dll_scheme_make_vector
499# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
500# define scheme_open_input_file dll_scheme_open_input_file
501# define scheme_primitive_module dll_scheme_primitive_module
502# define scheme_proper_list_length dll_scheme_proper_list_length
503# define scheme_raise dll_scheme_raise
504# define scheme_read dll_scheme_read
505# define scheme_register_static dll_scheme_register_static
506# define scheme_set_stack_base dll_scheme_set_stack_base
507# define scheme_signal_error dll_scheme_signal_error
508# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000509# if MZSCHEME_VERSION_MAJOR >= 299
510# define scheme_set_param dll_scheme_set_param
511# define scheme_current_config dll_scheme_current_config
512# define scheme_char_string_to_byte_string \
513 dll_scheme_char_string_to_byte_string
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000514# define scheme_char_string_to_path \
515 dll_scheme_char_string_to_path
Bram Moolenaar75676462013-01-30 14:55:42 +0100516# define scheme_set_collects_path dll_scheme_set_collects_path
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000517# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000518# define scheme_make_hash_table dll_scheme_make_hash_table
519# define scheme_hash_set dll_scheme_hash_set
520# define scheme_hash_get dll_scheme_hash_get
521# define scheme_make_double dll_scheme_make_double
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100522# define scheme_make_sized_byte_string dll_scheme_make_sized_byte_string
523# define scheme_namespace_require dll_scheme_namespace_require
524# define scheme_dynamic_wind dll_scheme_dynamic_wind
525# ifdef MZ_PRECISE_GC
526# define GC_malloc_one_tagged dll_GC_malloc_one_tagged
527# define GC_register_traversers dll_GC_register_traversers
528# endif
529# if MZSCHEME_VERSION_MAJOR >= 400
530# ifdef TRAMPOLINED_MZVIM_STARTUP
531# define scheme_main_setup dll_scheme_main_setup
532# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
533# define scheme_register_tls_space dll_scheme_register_tls_space
534# endif
535# endif
536# define scheme_init_collection_paths dll_scheme_init_collection_paths
537# define scheme_malloc_immobile_box dll_scheme_malloc_immobile_box
538# define scheme_free_immobile_box dll_scheme_free_immobile_box
539# endif
540# if MZSCHEME_VERSION_MAJOR >= 600
541# define scheme_embedded_load dll_scheme_embedded_load
542# define scheme_register_embedded_load dll_scheme_register_embedded_load
543# define scheme_set_config_path dll_scheme_set_config_path
544# endif
545
546# if MZSCHEME_VERSION_MAJOR >= 500
547# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
548/* define as function for macro in schshread.h */
549Thread_Local_Variables *
550scheme_external_get_thread_local_variables(void)
551{
552 return dll_scheme_external_get_thread_local_variables();
553}
554# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000555# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000556
Bram Moolenaar4349c572016-01-30 13:28:28 +0100557#endif
558
Bram Moolenaar33570922005-01-25 22:26:29 +0000559typedef struct
560{
561 char *name;
562 void **ptr;
563} Thunk_Info;
564
565static Thunk_Info mzgc_imports[] = {
566 {"GC_malloc", (void **)&dll_GC_malloc},
567 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
568 {NULL, NULL}};
569
570static Thunk_Info mzsch_imports[] = {
571 {"scheme_eof", (void **)&dll_scheme_eof},
572 {"scheme_false", (void **)&dll_scheme_false},
573 {"scheme_void", (void **)&dll_scheme_void},
574 {"scheme_null", (void **)&dll_scheme_null},
575 {"scheme_true", (void **)&dll_scheme_true},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100576#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
Bram Moolenaar33570922005-01-25 22:26:29 +0000577 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100578#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000579 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
580 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000581 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000582 (void **)&dll_scheme_notify_multithread_ptr},
583 {"scheme_add_global", (void **)&dll_scheme_add_global},
584 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
585 {"scheme_apply", (void **)&dll_scheme_apply},
586 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000587# if MZSCHEME_VERSION_MAJOR >= 299
588 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100589 {"scheme_make_path", (void **)&dll_scheme_make_path},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000590# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000591 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
592 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
593 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
594 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000595 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000596 (void **)&dll_scheme_current_continuation_marks},
597 {"scheme_display", (void **)&dll_scheme_display},
598 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
599 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
600 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000601 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000602 {"scheme_eval", (void **)&dll_scheme_eval},
603 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
604 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000605 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000606 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000607# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000608 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000609# else
610 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000611 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000613 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000614# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000615 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000617# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000618 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000619 (void **)&dll_scheme_get_sized_byte_string_output},
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
622 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000623 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000624 (void **)&dll_scheme_make_closed_prim_w_arity},
625 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
Bram Moolenaar33570922005-01-25 22:26:29 +0000626 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000627 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000628# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000629 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000630 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000631 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000632# else
633 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000634 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000635 (void **)&dll_scheme_make_byte_string_output_port},
636# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000637 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000638 (void **)&dll_scheme_make_struct_instance},
639 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
640 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
641 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
642 {"scheme_make_type", (void **)&dll_scheme_make_type},
643 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
644 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
645 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
646 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
647 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
648 {"scheme_raise", (void **)&dll_scheme_raise},
649 {"scheme_read", (void **)&dll_scheme_read},
650 {"scheme_register_static", (void **)&dll_scheme_register_static},
651 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
652 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
653 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000654# if MZSCHEME_VERSION_MAJOR >= 299
655 {"scheme_set_param", (void **)&dll_scheme_set_param},
656 {"scheme_current_config", (void **)&dll_scheme_current_config},
657 {"scheme_char_string_to_byte_string",
658 (void **)&dll_scheme_char_string_to_byte_string},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000659 {"scheme_char_string_to_path", (void **)&dll_scheme_char_string_to_path},
Bram Moolenaar75676462013-01-30 14:55:42 +0100660 {"scheme_set_collects_path", (void **)&dll_scheme_set_collects_path},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000661# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000662 {"scheme_make_hash_table", (void **)&dll_scheme_make_hash_table},
663 {"scheme_hash_set", (void **)&dll_scheme_hash_set},
664 {"scheme_hash_get", (void **)&dll_scheme_hash_get},
665 {"scheme_make_double", (void **)&dll_scheme_make_double},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000666 {"scheme_make_sized_byte_string", (void **)&dll_scheme_make_sized_byte_string},
667 {"scheme_namespace_require", (void **)&dll_scheme_namespace_require},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100668 {"scheme_dynamic_wind", (void **)&dll_scheme_dynamic_wind},
669# ifdef MZ_PRECISE_GC
670 {"GC_malloc_one_tagged", (void **)&dll_GC_malloc_one_tagged},
671 {"GC_register_traversers", (void **)&dll_GC_register_traversers},
672# endif
673# if MZSCHEME_VERSION_MAJOR >= 400
674# ifdef TRAMPOLINED_MZVIM_STARTUP
675 {"scheme_main_setup", (void **)&dll_scheme_main_setup},
676# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
677 {"scheme_register_tls_space", (void **)&dll_scheme_register_tls_space},
678# endif
679# endif
680 {"scheme_init_collection_paths", (void **)&dll_scheme_init_collection_paths},
681 {"scheme_malloc_immobile_box", (void **)&dll_scheme_malloc_immobile_box},
682 {"scheme_free_immobile_box", (void **)&dll_scheme_free_immobile_box},
683# endif
684# if MZSCHEME_VERSION_MAJOR >= 500
685# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
686 {"scheme_external_get_thread_local_variables", (void **)&dll_scheme_external_get_thread_local_variables},
687# endif
688# endif
689# if MZSCHEME_VERSION_MAJOR >= 600
690 {"scheme_embedded_load", (void **)&dll_scheme_embedded_load},
691 {"scheme_register_embedded_load", (void **)&dll_scheme_register_embedded_load},
692 {"scheme_set_config_path", (void **)&dll_scheme_set_config_path},
693# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000694 {NULL, NULL}};
695
696static HINSTANCE hMzGC = 0;
697static HINSTANCE hMzSch = 0;
698
699static void dynamic_mzscheme_end(void);
700static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
701 int verbose);
702
703 static int
704mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
705{
706 Thunk_Info *thunk = NULL;
707
708 if (hMzGC && hMzSch)
709 return OK;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200710 hMzSch = vimLoadLib(sch_dll);
711 hMzGC = vimLoadLib(gc_dll);
Bram Moolenaar33570922005-01-25 22:26:29 +0000712
Bram Moolenaar33570922005-01-25 22:26:29 +0000713 if (!hMzGC)
714 {
715 if (verbose)
716 EMSG2(_(e_loadlib), gc_dll);
717 return FAIL;
718 }
719
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100720 if (!hMzSch)
721 {
722 if (verbose)
723 EMSG2(_(e_loadlib), sch_dll);
724 return FAIL;
725 }
726
Bram Moolenaar33570922005-01-25 22:26:29 +0000727 for (thunk = mzsch_imports; thunk->name; thunk++)
728 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000729 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
731 {
732 FreeLibrary(hMzSch);
733 hMzSch = 0;
734 FreeLibrary(hMzGC);
735 hMzGC = 0;
736 if (verbose)
737 EMSG2(_(e_loadfunc), thunk->name);
738 return FAIL;
739 }
740 }
741 for (thunk = mzgc_imports; thunk->name; thunk++)
742 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000743 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000744 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
745 {
746 FreeLibrary(hMzSch);
747 hMzSch = 0;
748 FreeLibrary(hMzGC);
749 hMzGC = 0;
750 if (verbose)
751 EMSG2(_(e_loadfunc), thunk->name);
752 return FAIL;
753 }
754 }
755 return OK;
756}
757
758 int
759mzscheme_enabled(int verbose)
760{
761 return mzscheme_runtime_link_init(
762 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
763}
764
765 static void
766dynamic_mzscheme_end(void)
767{
768 if (hMzSch)
769 {
770 FreeLibrary(hMzSch);
771 hMzSch = 0;
772 }
773 if (hMzGC)
774 {
775 FreeLibrary(hMzGC);
776 hMzGC = 0;
777 }
778}
779#endif /* DYNAMIC_MZSCHEME */
780
Bram Moolenaar75676462013-01-30 14:55:42 +0100781#if MZSCHEME_VERSION_MAJOR < 299
782# define GUARANTEED_STRING_ARG(proc, num) GUARANTEE_STRING(proc, num)
783#else
784 static Scheme_Object *
785guaranteed_byte_string_arg(char *proc, int num, int argc, Scheme_Object **argv)
786{
787 if (SCHEME_BYTE_STRINGP(argv[num]))
788 {
789 return argv[num];
790 }
791 else if (SCHEME_CHAR_STRINGP(argv[num]))
792 {
793 Scheme_Object *tmp = NULL;
794 MZ_GC_DECL_REG(2);
795 MZ_GC_VAR_IN_REG(0, argv[num]);
796 MZ_GC_VAR_IN_REG(1, tmp);
797 MZ_GC_REG();
798 tmp = scheme_char_string_to_byte_string(argv[num]);
799 MZ_GC_UNREG();
800 return tmp;
801 }
802 else
803 scheme_wrong_type(proc, "string", num, argc, argv);
804 /* unreachable */
805 return scheme_void;
806}
807# define GUARANTEED_STRING_ARG(proc, num) guaranteed_byte_string_arg(proc, num, argc, argv)
808#endif
809
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000810/* need to put it here for dynamic stuff to work */
Bram Moolenaare484c942009-09-11 10:21:41 +0000811#if defined(INCLUDE_MZSCHEME_BASE)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000812# include "mzscheme_base.c"
813#endif
814
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000815/*
816 *========================================================================
817 * 1. MzScheme interpreter startup
818 *========================================================================
819 */
820
821static Scheme_Type mz_buffer_type;
822static Scheme_Type mz_window_type;
823
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000824static int initialized = FALSE;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100825#ifdef DYNAMIC_MZSCHEME
826static int disabled = FALSE;
827#endif
828static int load_base_module_failed = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000829
830/* global environment */
831static Scheme_Env *environment = NULL;
832/* output/error handlers */
833static Scheme_Object *curout = NULL;
834static Scheme_Object *curerr = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000835/* exn:vim exception */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000836static Scheme_Object *exn_catching_apply = NULL;
837static Scheme_Object *exn_p = NULL;
838static Scheme_Object *exn_message = NULL;
839static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000840
841#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
842static void *stack_base = NULL;
843#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000844
845static long range_start;
846static long range_end;
847
848/* MzScheme threads scheduling stuff */
849static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000850
851#if defined(FEAT_GUI_W32)
852static void CALLBACK timer_proc(HWND, UINT, UINT, DWORD);
853static UINT timer_id = 0;
854#elif defined(FEAT_GUI_GTK)
855static gint timer_proc(gpointer);
856static guint timer_id = 0;
857#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
858static void timer_proc(XtPointer, XtIntervalId *);
859static XtIntervalId timer_id = (XtIntervalId)0;
860#elif defined(FEAT_GUI_MAC)
861pascal void timer_proc(EventLoopTimerRef, void *);
862static EventLoopTimerRef timer_id = NULL;
863static EventLoopTimerUPP timerUPP;
864#endif
865
866#ifndef FEAT_GUI_W32 /* Win32 console and Unix */
867 void
868mzvim_check_threads(void)
869{
870 /* Last time MzScheme threads were scheduled */
871 static time_t mz_last_time = 0;
872
873 if (mz_threads_allow && p_mzq > 0)
874 {
875 time_t now = time(NULL);
876
877 if ((now - mz_last_time) * 1000 > p_mzq)
878 {
879 mz_last_time = now;
880 scheme_check_threads();
881 }
882 }
883}
884#endif
885
Bram Moolenaar4349c572016-01-30 13:28:28 +0100886#if defined(MZSCHEME_GUI_THREADS) || defined(PROTO)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000887static void setup_timer(void);
888static void remove_timer(void);
889
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000890/* timers are presented in GUI only */
891# if defined(FEAT_GUI_W32)
892 static void CALLBACK
Bram Moolenaar64404472010-06-26 06:24:45 +0200893timer_proc(HWND hwnd UNUSED, UINT uMsg UNUSED, UINT idEvent UNUSED, DWORD dwTime UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000894# elif defined(FEAT_GUI_GTK)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000895 static gint
Bram Moolenaar64404472010-06-26 06:24:45 +0200896timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000897# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000898 static void
Bram Moolenaar64404472010-06-26 06:24:45 +0200899timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000900# elif defined(FEAT_GUI_MAC)
901 pascal void
Bram Moolenaar64404472010-06-26 06:24:45 +0200902timer_proc(EventLoopTimerRef theTimer UNUSED, void *userData UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000903# endif
904{
905 scheme_check_threads();
906# if defined(FEAT_GUI_GTK)
907 return TRUE; /* continue receiving notifications */
908# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
909 /* renew timeout */
910 if (mz_threads_allow && p_mzq > 0)
911 timer_id = XtAppAddTimeOut(app_context, p_mzq,
912 timer_proc, NULL);
913# endif
914}
915
916 static void
917setup_timer(void)
918{
919# if defined(FEAT_GUI_W32)
920 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
921# elif defined(FEAT_GUI_GTK)
922 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
923# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
924 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
925# elif defined(FEAT_GUI_MAC)
926 timerUPP = NewEventLoopTimerUPP(timer_proc);
927 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
928 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
929# endif
930}
931
932 static void
933remove_timer(void)
934{
935# if defined(FEAT_GUI_W32)
936 KillTimer(NULL, timer_id);
937# elif defined(FEAT_GUI_GTK)
938 gtk_timeout_remove(timer_id);
939# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
940 XtRemoveTimeOut(timer_id);
941# elif defined(FEAT_GUI_MAC)
942 RemoveEventLoopTimer(timer_id);
943 DisposeEventLoopTimerUPP(timerUPP);
944# endif
945 timer_id = 0;
946}
947
948 void
949mzvim_reset_timer(void)
950{
951 if (timer_id != 0)
952 remove_timer();
953 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
954 setup_timer();
955}
956
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000957#endif /* MZSCHEME_GUI_THREADS */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000958
959 static void
960notify_multithread(int on)
961{
962 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000963#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000964 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
965 setup_timer();
966 if (!on && timer_id != 0)
967 remove_timer();
968#endif
969}
970
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000971 void
972mzscheme_end(void)
973{
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100974 /* We can not unload the DLL. Racket's thread might be still alive. */
975#if 0
Bram Moolenaar33570922005-01-25 22:26:29 +0000976#ifdef DYNAMIC_MZSCHEME
977 dynamic_mzscheme_end();
978#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100979#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000980}
981
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100982#if HAVE_TLS_SPACE
983# if defined(_MSC_VER)
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100984static __declspec(thread) void *tls_space;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100985extern intptr_t _tls_index;
986# elif defined(__MINGW32__)
987static __thread void *tls_space;
988extern intptr_t _tls_index;
989# else
990static THREAD_LOCAL void *tls_space;
991static intptr_t _tls_index = 0;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100992# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100993#endif
994
995 int
996mzscheme_main(int argc, char** argv)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000997{
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100998#ifdef DYNAMIC_MZSCHEME
999 /*
1000 * Racket requires trampolined startup. We can not load it later.
1001 * If dynamic dll loading is failed, disable it.
1002 */
1003 if (!mzscheme_enabled(FALSE))
1004 {
1005 disabled = TRUE;
1006 return vim_main2(argc, argv);
1007 }
1008#endif
Bram Moolenaar5c5c9802015-07-10 16:12:48 +02001009#ifdef HAVE_TLS_SPACE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001010 scheme_register_tls_space(&tls_space, _tls_index);
Bram Moolenaar2d0860d2010-11-03 21:59:30 +01001011#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001012#ifdef TRAMPOLINED_MZVIM_STARTUP
1013 return scheme_main_setup(TRUE, mzscheme_env_main, argc, argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001014#else
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001015 return mzscheme_env_main(NULL, argc, argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001016#endif
1017}
1018
1019 static int
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001020mzscheme_env_main(Scheme_Env *env, int argc, char **argv)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001021{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001022 int vim_main_result;
1023#ifdef TRAMPOLINED_MZVIM_STARTUP
1024 /* Scheme has created the environment for us */
1025 environment = env;
1026#else
1027# ifdef MZ_PRECISE_GC
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001028 Scheme_Object *dummy = NULL;
1029 MZ_GC_DECL_REG(1);
1030 MZ_GC_VAR_IN_REG(0, dummy);
1031
1032 stack_base = &__gc_var_stack__;
1033# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001034 int dummy = 0;
1035 stack_base = (void *)&dummy;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001036# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001037#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001038
1039 /* mzscheme_main is called as a trampoline from main.
1040 * We trampoline into vim_main2
1041 * Passing argc, argv through from mzscheme_main
1042 */
1043 vim_main_result = vim_main2(argc, argv);
1044#if !defined(TRAMPOLINED_MZVIM_STARTUP) && defined(MZ_PRECISE_GC)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001045 /* releasing dummy */
1046 MZ_GC_REG();
1047 MZ_GC_UNREG();
1048#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001049 return vim_main_result;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001050}
1051
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001052 static Scheme_Object*
1053load_base_module(void *data)
1054{
1055 scheme_namespace_require(scheme_intern_symbol((char *)data));
1056 return scheme_null;
1057}
1058
1059 static Scheme_Object *
1060load_base_module_on_error(void *data)
1061{
1062 load_base_module_failed = TRUE;
1063 return scheme_null;
1064}
1065
1066 static int
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001067startup_mzscheme(void)
1068{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001069#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001070 scheme_set_stack_base(stack_base, 1);
1071#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001072
Bram Moolenaar75676462013-01-30 14:55:42 +01001073#ifndef TRAMPOLINED_MZVIM_STARTUP
1074 /* in newer versions of precise GC the initial env has been created */
1075 environment = scheme_basic_env();
1076#endif
1077
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001078 MZ_REGISTER_STATIC(environment);
1079 MZ_REGISTER_STATIC(curout);
1080 MZ_REGISTER_STATIC(curerr);
1081 MZ_REGISTER_STATIC(exn_catching_apply);
1082 MZ_REGISTER_STATIC(exn_p);
1083 MZ_REGISTER_STATIC(exn_message);
1084 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001085
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001086 MZ_GC_CHECK();
1087
1088#ifdef INCLUDE_MZSCHEME_BASE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001089 /* invoke function from generated and included mzscheme_base.c */
1090 declare_modules(environment);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001091#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001092
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001093 /* setup 'current-library-collection-paths' parameter */
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001094# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001095 {
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001096 Scheme_Object *coll_path = NULL;
1097 int mustfree = FALSE;
1098 char_u *s;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001099
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001100 MZ_GC_DECL_REG(1);
1101 MZ_GC_VAR_IN_REG(0, coll_path);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001102 MZ_GC_REG();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001103 /* workaround for dynamic loading on windows */
1104 s = vim_getenv("PLTCOLLECTS", &mustfree);
1105 if (s != NULL)
1106 {
1107 coll_path = scheme_make_path(s);
1108 MZ_GC_CHECK();
1109 if (mustfree)
1110 vim_free(s);
1111 }
1112# ifdef MZSCHEME_COLLECTS
1113 if (coll_path == NULL)
1114 {
1115 coll_path = scheme_make_path(MZSCHEME_COLLECTS);
1116 MZ_GC_CHECK();
1117 }
Bram Moolenaar39d7d512013-01-31 21:09:15 +01001118# endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001119 if (coll_path != NULL)
1120 {
1121 scheme_set_collects_path(coll_path);
1122 MZ_GC_CHECK();
1123 }
1124 MZ_GC_UNREG();
1125 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001126# else
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001127# ifdef MZSCHEME_COLLECTS
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001128 {
1129 Scheme_Object *coll_string = NULL;
1130 Scheme_Object *coll_pair = NULL;
1131 Scheme_Config *config = NULL;
1132
1133 MZ_GC_DECL_REG(3);
1134 MZ_GC_VAR_IN_REG(0, coll_string);
1135 MZ_GC_VAR_IN_REG(1, coll_pair);
1136 MZ_GC_VAR_IN_REG(2, config);
1137 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001138 coll_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001139 MZ_GC_CHECK();
1140 coll_pair = scheme_make_pair(coll_string, scheme_null);
1141 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001142 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001143 MZ_GC_CHECK();
1144 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
1145 MZ_GC_CHECK();
1146 MZ_GC_UNREG();
1147 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001148# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001149#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001150
1151# if MZSCHEME_VERSION_MAJOR >= 600
1152 {
1153 Scheme_Object *config_path = NULL;
1154 int mustfree = FALSE;
1155 char_u *s;
1156
1157 MZ_GC_DECL_REG(1);
1158 MZ_GC_VAR_IN_REG(0, config_path);
1159 MZ_GC_REG();
1160 /* workaround for dynamic loading on windows */
1161 s = vim_getenv("PLTCONFIGDIR", &mustfree);
1162 if (s != NULL)
1163 {
1164 config_path = scheme_make_path(s);
1165 MZ_GC_CHECK();
1166 if (mustfree)
1167 vim_free(s);
1168 }
1169#ifdef MZSCHEME_CONFIGDIR
1170 if (config_path == NULL)
1171 {
1172 config_path = scheme_make_path(MZSCHEME_CONFIGDIR);
1173 MZ_GC_CHECK();
1174 }
1175#endif
1176 if (config_path != NULL)
1177 {
1178 scheme_set_config_path(config_path);
1179 MZ_GC_CHECK();
1180 }
1181 MZ_GC_UNREG();
1182 }
1183# endif
1184
1185#if MZSCHEME_VERSION_MAJOR >= 400
1186 scheme_init_collection_paths(environment, scheme_null);
1187#endif
1188
1189 /*
1190 * versions 4.x do not provide Scheme bindings by default
1191 * we need to add them explicitly
1192 */
1193 {
1194 /* use error handler to avoid abort */
1195 scheme_dynamic_wind(NULL, load_base_module, NULL,
1196 load_base_module_on_error, "racket/base");
1197 if (load_base_module_failed)
1198 {
1199 load_base_module_failed = FALSE;
1200 scheme_dynamic_wind(NULL, load_base_module, NULL,
1201 load_base_module_on_error, "scheme/base");
1202 if (load_base_module_failed)
1203 return -1;
1204 }
1205 }
1206
1207 register_vim_exn();
1208 /* use new environment to initialise exception handling */
1209 init_exn_catching_apply();
1210
1211 /* redirect output */
1212 scheme_console_output = do_output;
1213 scheme_console_printf = do_printf;
1214
Bram Moolenaar555b2802005-05-19 21:08:39 +00001215#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +00001216 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001217 Scheme_Object *make_security_guard = NULL;
1218 MZ_GC_DECL_REG(1);
1219 MZ_GC_VAR_IN_REG(0, make_security_guard);
1220 MZ_GC_REG();
1221
1222#if MZSCHEME_VERSION_MAJOR < 400
1223 {
1224 Scheme_Object *make_security_guard_symbol = NULL;
1225 MZ_GC_DECL_REG(1);
1226 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
1227 MZ_GC_REG();
1228 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
1229 MZ_GC_CHECK();
1230 make_security_guard = scheme_lookup_global(
1231 make_security_guard_symbol, environment);
1232 MZ_GC_UNREG();
1233 }
1234#else
1235 make_security_guard = scheme_builtin_value("make-security-guard");
1236 MZ_GC_CHECK();
1237#endif
1238
1239 /* setup sandbox guards */
1240 if (make_security_guard != NULL)
1241 {
1242 Scheme_Object *args[3] = {NULL, NULL, NULL};
1243 Scheme_Object *guard = NULL;
1244 Scheme_Config *config = NULL;
1245 MZ_GC_DECL_REG(5);
1246 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
1247 MZ_GC_VAR_IN_REG(3, guard);
1248 MZ_GC_VAR_IN_REG(4, config);
1249 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001250 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001251 MZ_GC_CHECK();
1252 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
1253 MZ_GC_CHECK();
1254 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
1255 "sandbox-file-guard", 3, 3);
1256 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1257 "sandbox-network-guard", 4, 4);
1258 guard = scheme_apply(make_security_guard, 3, args);
1259 MZ_GC_CHECK();
1260 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1261 MZ_GC_CHECK();
1262 MZ_GC_UNREG();
1263 }
1264 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001265 }
1266#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001267 /* Create buffer and window types for use in Scheme code */
1268 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001269 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001270 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001271 MZ_GC_CHECK();
1272#ifdef MZ_PRECISE_GC
1273 GC_register_traversers(mz_buffer_type,
1274 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1275 TRUE, TRUE);
1276 GC_register_traversers(mz_window_type,
1277 window_size_proc, window_mark_proc, window_fixup_proc,
1278 TRUE, TRUE);
1279#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001280
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001281 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001282
1283 /*
1284 * setup callback to receive notifications
1285 * whether thread scheduling is (or not) required
1286 */
1287 scheme_notify_multithread = notify_multithread;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001288
1289 return 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001290}
1291
1292/*
1293 * This routine is called for each new invocation of MzScheme
1294 * to make sure things are properly initialized.
1295 */
1296 static int
1297mzscheme_init(void)
1298{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001299 if (!initialized)
1300 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001301#ifdef DYNAMIC_MZSCHEME
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001302 if (disabled || !mzscheme_enabled(TRUE))
Bram Moolenaar33570922005-01-25 22:26:29 +00001303 {
Bram Moolenaarb849e712009-06-24 15:51:37 +00001304 EMSG(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
Bram Moolenaar33570922005-01-25 22:26:29 +00001305 return -1;
1306 }
1307#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001308 if (load_base_module_failed || startup_mzscheme())
1309 {
Bram Moolenaar9e3be262016-01-24 13:58:40 +01001310 EMSG(_("E895: Sorry, this command is disabled, the MzScheme's racket/base module could not be loaded."));
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001311 return -1;
1312 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001313 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001314 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001315 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001316 Scheme_Config *config = NULL;
1317 MZ_GC_DECL_REG(1);
1318 MZ_GC_VAR_IN_REG(0, config);
1319 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001320 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001321 MZ_GC_CHECK();
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001322 /* recreate ports each call effectively clearing these ones */
Bram Moolenaar75676462013-01-30 14:55:42 +01001323 curout = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001324 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001325 curerr = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001326 MZ_GC_CHECK();
1327 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1328 MZ_GC_CHECK();
1329 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1330 MZ_GC_CHECK();
1331 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001332 }
1333
1334 return 0;
1335}
1336
1337/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001338 *========================================================================
1339 * 2. External Interface
1340 *========================================================================
1341 */
1342
1343/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001344 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001345 */
1346 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001347eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001348{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001349 Scheme_Object *value = NULL;
1350 Scheme_Object *exn = NULL;
1351 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001352
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001353 MZ_GC_DECL_REG(3);
1354 MZ_GC_VAR_IN_REG(0, value);
1355 MZ_GC_VAR_IN_REG(1, exn);
1356 MZ_GC_VAR_IN_REG(2, prim);
1357 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001358
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001359 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1360 MZ_GC_CHECK();
1361 value = _apply_thunk_catch_exceptions(prim, &exn);
1362 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001363
1364 if (!value)
1365 {
1366 value = extract_exn_message(exn);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001367 /* Got an exn? */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001368 if (value)
1369 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001370 scheme_display(value, curerr); /* Send to stderr-vim */
1371 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001372 do_flush();
1373 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001374 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001375 /* `raise' was called on some arbitrary value */
1376 return FAIL;
1377 }
1378
1379 if (ret != NULL) /* if pointer to retval supported give it up */
1380 *ret = value;
1381 /* Print any result, as long as it's not a void */
1382 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001383 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001384 scheme_display(value, curout); /* Send to stdout-vim */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001385 MZ_GC_CHECK();
1386 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001387
1388 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001389 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001390 return OK;
1391}
1392
1393/* :mzscheme */
1394 static int
1395do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1396{
1397 if (mzscheme_init())
1398 return FAIL;
1399
1400 range_start = eap->line1;
1401 range_end = eap->line2;
1402
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001403 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001404}
1405
1406/*
1407 * Routine called by VIM when deleting a buffer
1408 */
1409 void
1410mzscheme_buffer_free(buf_T *buf)
1411{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001412 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001413 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001414 vim_mz_buffer *bp = NULL;
1415 MZ_GC_DECL_REG(1);
1416 MZ_GC_VAR_IN_REG(0, bp);
1417 MZ_GC_REG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001418
Bram Moolenaar75676462013-01-30 14:55:42 +01001419 bp = BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001420 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001421#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001422 scheme_gc_ptr_ok(bp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001423#else
1424 scheme_free_immobile_box(buf->b_mzscheme_ref);
1425#endif
1426 buf->b_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001427 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001428 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001429 }
1430}
1431
1432/*
1433 * Routine called by VIM when deleting a Window
1434 */
1435 void
1436mzscheme_window_free(win_T *win)
1437{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001438 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001439 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001440 vim_mz_window *wp = NULL;
1441 MZ_GC_DECL_REG(1);
1442 MZ_GC_VAR_IN_REG(0, wp);
1443 MZ_GC_REG();
1444 wp = WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001445 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001446#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001447 scheme_gc_ptr_ok(wp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001448#else
1449 scheme_free_immobile_box(win->w_mzscheme_ref);
1450#endif
1451 win->w_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001452 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001453 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001454 }
1455}
1456
1457/*
1458 * ":mzscheme" (or ":mz")
1459 */
1460 void
1461ex_mzscheme(exarg_T *eap)
1462{
1463 char_u *script;
1464
1465 script = script_get(eap, eap->arg);
1466 if (!eap->skip)
1467 {
1468 if (script == NULL)
1469 do_mzscheme_command(eap, eap->arg, do_eval);
1470 else
1471 {
1472 do_mzscheme_command(eap, script, do_eval);
1473 vim_free(script);
1474 }
1475 }
1476}
1477
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001478 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001479do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001480{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001481 Scheme_Object *expr = NULL;
1482 Scheme_Object *result = NULL;
1483 char *file = NULL;
1484 Port_Info *pinfo = (Port_Info *)data;
1485
1486 MZ_GC_DECL_REG(3);
1487 MZ_GC_VAR_IN_REG(0, expr);
1488 MZ_GC_VAR_IN_REG(1, result);
1489 MZ_GC_VAR_IN_REG(2, file);
1490 MZ_GC_REG();
1491
1492 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1493 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001494
1495 /* make Vim expansion */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001496 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001497 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001498 MZ_GC_CHECK();
1499 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1500 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001501
1502 /* Like REPL but print only last result */
1503 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001504 {
1505 result = scheme_eval(expr, environment);
1506 MZ_GC_CHECK();
1507 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001508
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001509 /* errors will be caught in do_mzscheme_command and ex_mzfile */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001510 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001511 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001512 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001513 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001514 return result;
1515}
1516
1517/* :mzfile */
1518 void
1519ex_mzfile(exarg_T *eap)
1520{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001521 Port_Info pinfo = {NULL, NULL};
1522
1523 MZ_GC_DECL_REG(1);
1524 MZ_GC_VAR_IN_REG(0, pinfo.port);
1525 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001526
1527 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001528 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1529 && pinfo.port != NULL) /* looks like port was not closed */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001530 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001531 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001532 MZ_GC_CHECK();
1533 }
1534 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001535}
1536
1537
1538/*
1539 *========================================================================
1540 * Exception handling code -- cribbed form the MzScheme sources and
1541 * Matthew Flatt's "Inside PLT MzScheme" document.
1542 *========================================================================
1543 */
1544 static void
1545init_exn_catching_apply(void)
1546{
1547 if (!exn_catching_apply)
1548 {
1549 char *e =
1550 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001551 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001552 "(cons #t (thunk))))";
1553
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001554 exn_catching_apply = scheme_eval_string(e, environment);
1555 MZ_GC_CHECK();
1556 exn_p = scheme_builtin_value("exn?");
1557 MZ_GC_CHECK();
1558 exn_message = scheme_builtin_value("exn-message");
1559 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001560 }
1561}
1562
1563/*
1564 * This function applies a thunk, returning the Scheme value if there's
1565 * no exception, otherwise returning NULL and setting *exn to the raised
1566 * value (usually an exn structure).
1567 */
1568 static Scheme_Object *
1569_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1570{
1571 Scheme_Object *v;
1572
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001573 v = _scheme_apply(exn_catching_apply, 1, &f);
1574 /* v is a pair: (cons #t value) or (cons #f exn) */
1575
1576 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1577 return SCHEME_CDR(v);
1578 else
1579 {
1580 *exn = SCHEME_CDR(v);
1581 return NULL;
1582 }
1583}
1584
1585 static Scheme_Object *
1586extract_exn_message(Scheme_Object *v)
1587{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001588 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1589 return _scheme_apply(exn_message, 1, &v);
1590 else
1591 return NULL; /* Not an exn structure */
1592}
1593
1594 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001595do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001596{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001597 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001598}
1599
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001600/*
1601 *========================================================================
1602 * 3. MzScheme I/O Handlers
1603 *========================================================================
1604 */
1605 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001606do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001607{
1608 char *p, *prev;
1609
1610 prev = mesg;
1611 p = strchr(prev, '\n');
1612 while (p)
1613 {
1614 *p = '\0';
1615 if (error)
1616 EMSG(prev);
1617 else
1618 MSG(prev);
1619 prev = p + 1;
1620 p = strchr(prev, '\n');
1621 }
1622
1623 if (error)
1624 EMSG(prev);
1625 else
1626 MSG(prev);
1627}
1628
1629 static void
Bram Moolenaar75676462013-01-30 14:55:42 +01001630do_output(char *mesg, OUTPUT_LEN_TYPE len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001631{
Bram Moolenaar02e14d62012-11-28 15:37:51 +01001632 /* TODO: use len, the string may not be NUL terminated */
Bram Moolenaar64404472010-06-26 06:24:45 +02001633 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001634}
1635
1636 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001637do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001638{
Bram Moolenaar64404472010-06-26 06:24:45 +02001639 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001640}
1641
1642 static void
1643do_printf(char *format, ...)
1644{
Bram Moolenaar64404472010-06-26 06:24:45 +02001645 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001646}
1647
1648 static void
1649do_flush(void)
1650{
1651 char *buff;
Bram Moolenaar75676462013-01-30 14:55:42 +01001652 OUTPUT_LEN_TYPE length;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001653
Bram Moolenaar75676462013-01-30 14:55:42 +01001654 buff = scheme_get_sized_byte_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001655 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001656 if (length)
1657 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001658 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001659 return;
1660 }
1661
Bram Moolenaar75676462013-01-30 14:55:42 +01001662 buff = scheme_get_sized_byte_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001663 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001664 if (length)
1665 do_output(buff, length);
1666}
1667
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001668/*
1669 *========================================================================
1670 * 4. Implementation of the Vim Features for MzScheme
1671 *========================================================================
1672 */
1673
1674/* (command {command-string}) */
1675 static Scheme_Object *
1676vim_command(void *data, int argc, Scheme_Object **argv)
1677{
1678 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001679 Scheme_Object *cmd = NULL;
1680 MZ_GC_DECL_REG(1);
1681 MZ_GC_VAR_IN_REG(0, cmd);
1682 MZ_GC_REG();
1683 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001684
1685 /* may be use do_cmdline_cmd? */
Bram Moolenaar75676462013-01-30 14:55:42 +01001686 do_cmdline(BYTE_STRING_VALUE(cmd), NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001687 update_screen(VALID);
1688
Bram Moolenaar75676462013-01-30 14:55:42 +01001689 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001690 raise_if_error();
1691 return scheme_void;
1692}
1693
1694/* (eval {expr-string}) */
1695 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001696vim_eval(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001697{
1698#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001699 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001700 Scheme_Object *result = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001701 typval_T *vim_result;
Bram Moolenaar75676462013-01-30 14:55:42 +01001702 Scheme_Object *expr = NULL;
1703 MZ_GC_DECL_REG(2);
1704 MZ_GC_VAR_IN_REG(0, result);
1705 MZ_GC_VAR_IN_REG(1, expr);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001706 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001707 expr = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001708
Bram Moolenaar75676462013-01-30 14:55:42 +01001709 vim_result = eval_expr(BYTE_STRING_VALUE(expr), NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001710
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001711 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001712 raise_vim_exn(_("invalid expression"));
1713
Bram Moolenaar75676462013-01-30 14:55:42 +01001714 result = vim_to_mzscheme(vim_result);
1715 MZ_GC_CHECK();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001716 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001717
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001718 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001719 return result;
1720#else
1721 raise_vim_exn(_("expressions disabled at compile time"));
1722 /* unreachable */
1723 return scheme_false;
1724#endif
1725}
1726
1727/* (range-start) */
1728 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001729get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001730{
1731 return scheme_make_integer(range_start);
1732}
1733
1734/* (range-end) */
1735 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001736get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001737{
1738 return scheme_make_integer(range_end);
1739}
1740
1741/* (beep) */
1742 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001743mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001744{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001745 vim_beep(BO_LANG);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001746 return scheme_void;
1747}
1748
1749static Scheme_Object *M_global = NULL;
1750
1751/* (get-option {option-name}) [buffer/window] */
1752 static Scheme_Object *
1753get_option(void *data, int argc, Scheme_Object **argv)
1754{
1755 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001756 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001757 char *strval;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001758 int rc;
Bram Moolenaar75676462013-01-30 14:55:42 +01001759 Scheme_Object *rval = NULL;
1760 Scheme_Object *name = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001761 int opt_flags = 0;
1762 buf_T *save_curb = curbuf;
1763 win_T *save_curw = curwin;
1764
Bram Moolenaar75676462013-01-30 14:55:42 +01001765 MZ_GC_DECL_REG(2);
1766 MZ_GC_VAR_IN_REG(0, rval);
1767 MZ_GC_VAR_IN_REG(1, name);
1768 MZ_GC_REG();
1769
1770 name = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001771
1772 if (argc > 1)
1773 {
1774 if (M_global == NULL)
1775 {
1776 MZ_REGISTER_STATIC(M_global);
1777 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001778 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001779 }
1780
1781 if (argv[1] == M_global)
1782 opt_flags = OPT_GLOBAL;
1783 else if (SCHEME_VIMBUFFERP(argv[1]))
1784 {
1785 curbuf = get_valid_buffer(argv[1]);
1786 opt_flags = OPT_LOCAL;
1787 }
1788 else if (SCHEME_VIMWINDOWP(argv[1]))
1789 {
1790 win_T *win = get_valid_window(argv[1]);
1791
1792 curwin = win;
1793 curbuf = win->w_buffer;
1794 opt_flags = OPT_LOCAL;
1795 }
1796 else
1797 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1798 }
1799
Bram Moolenaar75676462013-01-30 14:55:42 +01001800 rc = get_option_value(BYTE_STRING_VALUE(name), &value, (char_u **)&strval, opt_flags);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001801 curbuf = save_curb;
1802 curwin = save_curw;
1803
1804 switch (rc)
1805 {
1806 case 1:
Bram Moolenaar75676462013-01-30 14:55:42 +01001807 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001808 return scheme_make_integer_value(value);
1809 case 0:
Bram Moolenaar75676462013-01-30 14:55:42 +01001810 rval = scheme_make_byte_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001811 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001812 vim_free(strval);
Bram Moolenaar75676462013-01-30 14:55:42 +01001813 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001814 return rval;
1815 case -1:
1816 case -2:
Bram Moolenaar75676462013-01-30 14:55:42 +01001817 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001818 raise_vim_exn(_("hidden option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001819 case -3:
Bram Moolenaar75676462013-01-30 14:55:42 +01001820 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001821 raise_vim_exn(_("unknown option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001822 }
1823 /* unreachable */
1824 return scheme_void;
1825}
1826
1827/* (set-option {option-changing-string} [buffer/window]) */
1828 static Scheme_Object *
1829set_option(void *data, int argc, Scheme_Object **argv)
1830{
Bram Moolenaar75676462013-01-30 14:55:42 +01001831 char_u *command = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001832 int opt_flags = 0;
1833 buf_T *save_curb = curbuf;
1834 win_T *save_curw = curwin;
1835 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001836 Scheme_Object *cmd = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001837
Bram Moolenaar75676462013-01-30 14:55:42 +01001838 MZ_GC_DECL_REG(1);
1839 MZ_GC_VAR_IN_REG(0, cmd);
1840 MZ_GC_REG();
1841 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
1842
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001843 if (argc > 1)
1844 {
1845 if (M_global == NULL)
1846 {
1847 MZ_REGISTER_STATIC(M_global);
1848 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001849 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001850 }
1851
1852 if (argv[1] == M_global)
1853 opt_flags = OPT_GLOBAL;
1854 else if (SCHEME_VIMBUFFERP(argv[1]))
1855 {
1856 curbuf = get_valid_buffer(argv[1]);
1857 opt_flags = OPT_LOCAL;
1858 }
1859 else if (SCHEME_VIMWINDOWP(argv[1]))
1860 {
1861 win_T *win = get_valid_window(argv[1]);
1862 curwin = win;
1863 curbuf = win->w_buffer;
1864 opt_flags = OPT_LOCAL;
1865 }
1866 else
1867 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1868 }
1869
1870 /* do_set can modify cmd, make copy */
Bram Moolenaar75676462013-01-30 14:55:42 +01001871 command = vim_strsave(BYTE_STRING_VALUE(cmd));
1872 MZ_GC_UNREG();
1873 do_set(command, opt_flags);
1874 vim_free(command);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001875 update_screen(NOT_VALID);
1876 curbuf = save_curb;
1877 curwin = save_curw;
1878 raise_if_error();
1879 return scheme_void;
1880}
1881
1882/*
1883 *===========================================================================
1884 * 5. Vim Window-related Manipulation Functions
1885 *===========================================================================
1886 */
1887
1888/* (curr-win) */
1889 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001890get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001891{
1892 return (Scheme_Object *)get_vim_curr_window();
1893}
1894
1895/* (win-count) */
1896 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001897get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001898{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001899 int n = 0;
Bram Moolenaard2142212013-01-30 17:41:50 +01001900#ifdef FEAT_WINDOWS
1901 win_T *w;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001902
Bram Moolenaarf740b292006-02-16 22:11:02 +00001903 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaard2142212013-01-30 17:41:50 +01001904#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001905 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001906 return scheme_make_integer(n);
1907}
1908
1909/* (get-win-list [buffer]) */
1910 static Scheme_Object *
1911get_window_list(void *data, int argc, Scheme_Object **argv)
1912{
1913 Vim_Prim *prim = (Vim_Prim *)data;
1914 vim_mz_buffer *buf;
1915 Scheme_Object *list;
Bram Moolenaard2142212013-01-30 17:41:50 +01001916 win_T *w = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001917
1918 buf = get_buffer_arg(prim->name, 0, argc, argv);
1919 list = scheme_null;
1920
Bram Moolenaard2142212013-01-30 17:41:50 +01001921#ifdef FEAT_WINDOWS
1922 for ( ; w != NULL; w = w->w_next)
1923#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001924 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001925 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001926 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001927 MZ_GC_CHECK();
1928 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001929
1930 return list;
1931}
1932
1933 static Scheme_Object *
1934window_new(win_T *win)
1935{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001936 vim_mz_window *self = NULL;
1937
1938 MZ_GC_DECL_REG(1);
1939 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001940
1941 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001942 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001943 * then we can get at it in win_free() in vim.
1944 *
1945 * On a win_free() we set the Scheme object's win_T *field
1946 * to an invalid value. We trap all uses of a window
1947 * object, and reject them if the win_T *field is invalid.
1948 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001949 if (win->w_mzscheme_ref != NULL)
Bram Moolenaar75676462013-01-30 14:55:42 +01001950 return (Scheme_Object *)WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001951
Bram Moolenaar75676462013-01-30 14:55:42 +01001952 MZ_GC_REG();
1953 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_window));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001954 vim_memset(self, 0, sizeof(vim_mz_window));
Bram Moolenaar75676462013-01-30 14:55:42 +01001955#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001956 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaar75676462013-01-30 14:55:42 +01001957#else
1958 win->w_mzscheme_ref = scheme_malloc_immobile_box(NULL);
1959#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001960 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001961 WINDOW_REF(win) = self;
1962 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001963 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001964 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001965
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001966 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001967 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001968}
1969
1970/* (get-win-num [window]) */
1971 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001972get_window_num(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001973{
Bram Moolenaard2142212013-01-30 17:41:50 +01001974 int nr = 1;
1975#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001976 Vim_Prim *prim = (Vim_Prim *)data;
1977 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001978 win_T *wp;
1979
1980 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaard2142212013-01-30 17:41:50 +01001981#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001982 ++nr;
1983
1984 return scheme_make_integer(nr);
1985}
1986
1987/* (get-win-by-num {windownum}) */
1988 static Scheme_Object *
1989get_window_by_num(void *data, int argc, Scheme_Object **argv)
1990{
1991 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaard2142212013-01-30 17:41:50 +01001992 win_T *win = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001993 int fnum;
1994
1995 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1996 if (fnum < 1)
1997 scheme_signal_error(_("window index is out of range"));
1998
Bram Moolenaard2142212013-01-30 17:41:50 +01001999#ifdef FEAT_WINDOWS
2000 for ( ; win != NULL; win = win->w_next, --fnum)
2001#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002002 if (fnum == 1) /* to be 1-based */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002003 return window_new(win);
2004
2005 return scheme_false;
2006}
2007
2008/* (get-win-buffer [window]) */
2009 static Scheme_Object *
2010get_window_buffer(void *data, int argc, Scheme_Object **argv)
2011{
2012 Vim_Prim *prim = (Vim_Prim *)data;
2013 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2014
2015 return buffer_new(win->win->w_buffer);
2016}
2017
2018/* (get-win-height [window]) */
2019 static Scheme_Object *
2020get_window_height(void *data, int argc, Scheme_Object **argv)
2021{
2022 Vim_Prim *prim = (Vim_Prim *)data;
2023 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2024
2025 return scheme_make_integer(win->win->w_height);
2026}
2027
2028/* (set-win-height {height} [window]) */
2029 static Scheme_Object *
2030set_window_height(void *data, int argc, Scheme_Object **argv)
2031{
2032 Vim_Prim *prim = (Vim_Prim *)data;
2033 vim_mz_window *win;
2034 win_T *savewin;
2035 int height;
2036
2037 win = get_window_arg(prim->name, 1, argc, argv);
2038 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2039
2040#ifdef FEAT_GUI
2041 need_mouse_correct = TRUE;
2042#endif
2043
2044 savewin = curwin;
2045 curwin = win->win;
2046 win_setheight(height);
2047 curwin = savewin;
2048
2049 raise_if_error();
2050 return scheme_void;
2051}
2052
2053#ifdef FEAT_VERTSPLIT
2054/* (get-win-width [window]) */
2055 static Scheme_Object *
2056get_window_width(void *data, int argc, Scheme_Object **argv)
2057{
2058 Vim_Prim *prim = (Vim_Prim *)data;
2059 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2060
2061 return scheme_make_integer(W_WIDTH(win->win));
2062}
2063
2064/* (set-win-width {width} [window]) */
2065 static Scheme_Object *
2066set_window_width(void *data, int argc, Scheme_Object **argv)
2067{
2068 Vim_Prim *prim = (Vim_Prim *)data;
2069 vim_mz_window *win;
2070 win_T *savewin;
2071 int width = 0;
2072
2073 win = get_window_arg(prim->name, 1, argc, argv);
2074 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2075
2076# ifdef FEAT_GUI
2077 need_mouse_correct = TRUE;
2078# endif
2079
2080 savewin = curwin;
2081 curwin = win->win;
2082 win_setwidth(width);
2083 curwin = savewin;
2084
2085 raise_if_error();
2086 return scheme_void;
2087}
2088#endif
2089
2090/* (get-cursor [window]) -> (line . col) */
2091 static Scheme_Object *
2092get_cursor(void *data, int argc, Scheme_Object **argv)
2093{
2094 Vim_Prim *prim = (Vim_Prim *)data;
2095 vim_mz_window *win;
2096 pos_T pos;
2097
2098 win = get_window_arg(prim->name, 0, argc, argv);
2099 pos = win->win->w_cursor;
2100 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
2101 scheme_make_integer_value((long)pos.col + 1));
2102}
2103
2104/* (set-cursor (line . col) [window]) */
2105 static Scheme_Object *
2106set_cursor(void *data, int argc, Scheme_Object **argv)
2107{
2108 Vim_Prim *prim = (Vim_Prim *)data;
2109 vim_mz_window *win;
2110 long lnum = 0;
2111 long col = 0;
2112
Bram Moolenaar555b2802005-05-19 21:08:39 +00002113#ifdef HAVE_SANDBOX
2114 sandbox_check();
2115#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002116 win = get_window_arg(prim->name, 1, argc, argv);
2117 GUARANTEE_PAIR(prim->name, 0);
2118
2119 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
2120 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
2121 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
2122
2123 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
2124 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
2125
2126 check_line_range(lnum, win->win->w_buffer);
2127 /* don't know how to catch invalid column value */
2128
2129 win->win->w_cursor.lnum = lnum;
2130 win->win->w_cursor.col = col;
2131 update_screen(VALID);
2132
2133 raise_if_error();
2134 return scheme_void;
2135}
2136/*
2137 *===========================================================================
2138 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002139 *===========================================================================
2140 */
2141
2142/* (open-buff {filename}) */
2143 static Scheme_Object *
2144mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
2145{
2146 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002147 int num = 0;
Bram Moolenaar75676462013-01-30 14:55:42 +01002148 Scheme_Object *onum = NULL;
2149 Scheme_Object *buf = NULL;
2150 Scheme_Object *fname;
2151
2152 MZ_GC_DECL_REG(3);
2153 MZ_GC_VAR_IN_REG(0, onum);
2154 MZ_GC_VAR_IN_REG(1, buf);
2155 MZ_GC_VAR_IN_REG(2, fname);
2156 MZ_GC_REG();
2157 fname = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002158
Bram Moolenaar555b2802005-05-19 21:08:39 +00002159#ifdef HAVE_SANDBOX
2160 sandbox_check();
2161#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002162 /* TODO make open existing file */
Bram Moolenaar75676462013-01-30 14:55:42 +01002163 num = buflist_add(BYTE_STRING_VALUE(fname), BLN_LISTED | BLN_CURBUF);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002164
2165 if (num == 0)
2166 raise_vim_exn(_("couldn't open buffer"));
2167
2168 onum = scheme_make_integer(num);
Bram Moolenaar75676462013-01-30 14:55:42 +01002169 buf = get_buffer_by_num(data, 1, &onum);
2170 MZ_GC_UNREG();
2171 return buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002172}
2173
2174/* (get-buff-by-num {buffernum}) */
2175 static Scheme_Object *
2176get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
2177{
2178 Vim_Prim *prim = (Vim_Prim *)data;
2179 buf_T *buf;
2180 int fnum;
2181
2182 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2183
2184 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002185 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002186 return buffer_new(buf);
2187
2188 return scheme_false;
2189}
2190
2191/* (get-buff-by-name {buffername}) */
2192 static Scheme_Object *
2193get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
2194{
2195 Vim_Prim *prim = (Vim_Prim *)data;
2196 buf_T *buf;
Bram Moolenaar75676462013-01-30 14:55:42 +01002197 Scheme_Object *buffer = NULL;
2198 Scheme_Object *fname = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002199
Bram Moolenaar75676462013-01-30 14:55:42 +01002200 MZ_GC_DECL_REG(2);
2201 MZ_GC_VAR_IN_REG(0, buffer);
2202 MZ_GC_VAR_IN_REG(1, fname);
2203 MZ_GC_REG();
2204 fname = GUARANTEED_STRING_ARG(prim->name, 0);
2205 buffer = scheme_false;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002206
2207 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaar75676462013-01-30 14:55:42 +01002208 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002209 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2210 /* empty string */
2211 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002212 if (BYTE_STRING_VALUE(fname)[0] == NUL)
2213 buffer = buffer_new(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002214 }
Bram Moolenaar75676462013-01-30 14:55:42 +01002215 else if (!fnamecmp(buf->b_ffname, BYTE_STRING_VALUE(fname))
2216 || !fnamecmp(buf->b_sfname, BYTE_STRING_VALUE(fname)))
2217 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002218 /* either short or long filename matches */
Bram Moolenaar75676462013-01-30 14:55:42 +01002219 buffer = buffer_new(buf);
2220 }
2221 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002222
Bram Moolenaar75676462013-01-30 14:55:42 +01002223 MZ_GC_UNREG();
2224 return buffer;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002225}
2226
2227/* (get-next-buff [buffer]) */
2228 static Scheme_Object *
2229get_next_buffer(void *data, int argc, Scheme_Object **argv)
2230{
2231 Vim_Prim *prim = (Vim_Prim *)data;
2232 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2233
2234 if (buf->b_next == NULL)
2235 return scheme_false;
2236 else
2237 return buffer_new(buf->b_next);
2238}
2239
2240/* (get-prev-buff [buffer]) */
2241 static Scheme_Object *
2242get_prev_buffer(void *data, int argc, Scheme_Object **argv)
2243{
2244 Vim_Prim *prim = (Vim_Prim *)data;
2245 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2246
2247 if (buf->b_prev == NULL)
2248 return scheme_false;
2249 else
2250 return buffer_new(buf->b_prev);
2251}
2252
2253/* (get-buff-num [buffer]) */
2254 static Scheme_Object *
2255get_buffer_num(void *data, int argc, Scheme_Object **argv)
2256{
2257 Vim_Prim *prim = (Vim_Prim *)data;
2258 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2259
2260 return scheme_make_integer(buf->buf->b_fnum);
2261}
2262
2263/* (buff-count) */
2264 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002265get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002266{
2267 buf_T *b;
2268 int n = 0;
2269
2270 for (b = firstbuf; b; b = b->b_next) ++n;
2271 return scheme_make_integer(n);
2272}
2273
2274/* (get-buff-name [buffer]) */
2275 static Scheme_Object *
2276get_buffer_name(void *data, int argc, Scheme_Object **argv)
2277{
2278 Vim_Prim *prim = (Vim_Prim *)data;
2279 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2280
Bram Moolenaar75676462013-01-30 14:55:42 +01002281 return scheme_make_byte_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002282}
2283
2284/* (curr-buff) */
2285 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002286get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002287{
2288 return (Scheme_Object *)get_vim_curr_buffer();
2289}
2290
2291 static Scheme_Object *
2292buffer_new(buf_T *buf)
2293{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002294 vim_mz_buffer *self = NULL;
2295
2296 MZ_GC_DECL_REG(1);
2297 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002298
2299 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00002300 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002301 * then we can get at it in buf_freeall() in vim.
2302 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00002303 if (buf->b_mzscheme_ref)
Bram Moolenaar75676462013-01-30 14:55:42 +01002304 return (Scheme_Object *)BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002305
Bram Moolenaar75676462013-01-30 14:55:42 +01002306 MZ_GC_REG();
2307 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_buffer));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002308 vim_memset(self, 0, sizeof(vim_mz_buffer));
Bram Moolenaar75676462013-01-30 14:55:42 +01002309#ifndef MZ_PRECISE_GC
2310 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
2311#else
2312 buf->b_mzscheme_ref = scheme_malloc_immobile_box(NULL);
2313#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002314 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01002315 BUFFER_REF(buf) = self;
2316 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002317 self->buf = buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002318 self->so.type = mz_buffer_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002319
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002320 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01002321 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002322}
2323
2324/*
2325 * (get-buff-size [buffer])
2326 *
2327 * Get the size (number of lines) in the current buffer.
2328 */
2329 static Scheme_Object *
2330get_buffer_size(void *data, int argc, Scheme_Object **argv)
2331{
2332 Vim_Prim *prim = (Vim_Prim *)data;
2333 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2334
2335 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
2336}
2337
2338/*
2339 * (get-buff-line {linenr} [buffer])
2340 *
2341 * Get a line from the specified buffer. The line number is
2342 * in Vim format (1-based). The line is returned as a MzScheme
2343 * string object.
2344 */
2345 static Scheme_Object *
2346get_buffer_line(void *data, int argc, Scheme_Object **argv)
2347{
2348 Vim_Prim *prim = (Vim_Prim *)data;
2349 vim_mz_buffer *buf;
2350 int linenr;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002351 char_u *line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002352
2353 buf = get_buffer_arg(prim->name, 1, argc, argv);
2354 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2355 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2356
2357 raise_if_error();
Bram Moolenaar75676462013-01-30 14:55:42 +01002358 return scheme_make_byte_string((char *)line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002359}
2360
2361
2362/*
2363 * (get-buff-line-list {start} {end} [buffer])
2364 *
2365 * Get a list of lines from the specified buffer. The line numbers
2366 * are in Vim format (1-based). The range is from lo up to, but not
2367 * including, hi. The list is returned as a list of string objects.
2368 */
2369 static Scheme_Object *
2370get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2371{
2372 Vim_Prim *prim = (Vim_Prim *)data;
2373 vim_mz_buffer *buf;
2374 int i, hi, lo, n;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002375 Scheme_Object *list = NULL;
2376
2377 MZ_GC_DECL_REG(1);
2378 MZ_GC_VAR_IN_REG(0, list);
2379 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002380
2381 buf = get_buffer_arg(prim->name, 2, argc, argv);
2382 list = scheme_null;
2383 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2384 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2385
2386 /*
2387 * Handle some error conditions
2388 */
2389 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002390 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002391
2392 if (hi < 0)
2393 hi = 0;
2394 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002395 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002396
2397 n = hi - lo;
2398
2399 for (i = n; i >= 0; --i)
2400 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002401 Scheme_Object *str = scheme_make_byte_string(
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002402 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2403 raise_if_error();
2404
2405 /* Set the list item */
2406 list = scheme_make_pair(str, list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002407 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002408 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002409 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002410 return list;
2411}
2412
2413/*
2414 * (set-buff-line {linenr} {string/#f} [buffer])
2415 *
2416 * Replace a line in the specified buffer. The line number is
2417 * in Vim format (1-based). The replacement line is given as
2418 * an MzScheme string object. The object is checked for validity
2419 * and correct format. An exception is thrown if the values are not
2420 * the correct format.
2421 *
2422 * It returns a Scheme Object that indicates the length of the
2423 * string changed.
2424 */
2425 static Scheme_Object *
2426set_buffer_line(void *data, int argc, Scheme_Object **argv)
2427{
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002428 /* First of all, we check the value of the supplied MzScheme object.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002429 * There are three cases:
2430 * 1. #f - this is a deletion.
2431 * 2. A string - this is a replacement.
2432 * 3. Anything else - this is an error.
2433 */
2434 Vim_Prim *prim = (Vim_Prim *)data;
2435 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002436 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002437 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002438 int n;
2439
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002440 MZ_GC_DECL_REG(1);
2441 MZ_GC_VAR_IN_REG(0, line);
2442 MZ_GC_REG();
2443
Bram Moolenaar555b2802005-05-19 21:08:39 +00002444#ifdef HAVE_SANDBOX
2445 sandbox_check();
2446#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002447 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2448 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002449 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002450 line = argv[1];
2451 buf = get_buffer_arg(prim->name, 2, argc, argv);
2452
2453 check_line_range(n, buf->buf);
2454
2455 if (SCHEME_FALSEP(line))
2456 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002457 buf_T *savebuf = curbuf;
2458
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002459 curbuf = buf->buf;
2460
2461 if (u_savedel((linenr_T)n, 1L) == FAIL)
2462 {
2463 curbuf = savebuf;
2464 raise_vim_exn(_("cannot save undo information"));
2465 }
2466 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2467 {
2468 curbuf = savebuf;
2469 raise_vim_exn(_("cannot delete line"));
2470 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002471 if (buf->buf == curwin->w_buffer)
2472 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002473 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002474
2475 curbuf = savebuf;
2476
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002477 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002478 raise_if_error();
2479 return scheme_void;
2480 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002481 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002482 {
2483 /* Otherwise it's a line */
2484 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002485
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002486 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002487
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002488 curbuf = buf->buf;
2489
2490 if (u_savesub((linenr_T)n) == FAIL)
2491 {
2492 curbuf = savebuf;
2493 vim_free(save);
2494 raise_vim_exn(_("cannot save undo information"));
2495 }
2496 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2497 {
2498 curbuf = savebuf;
2499 vim_free(save);
2500 raise_vim_exn(_("cannot replace line"));
2501 }
2502 else
2503 {
2504 vim_free(save);
2505 changed_bytes((linenr_T)n, 0);
2506 }
2507
2508 curbuf = savebuf;
2509
2510 /* Check that the cursor is not beyond the end of the line now. */
2511 if (buf->buf == curwin->w_buffer)
2512 check_cursor_col();
2513
2514 MZ_GC_UNREG();
2515 raise_if_error();
2516 return scheme_void;
2517 }
2518}
2519
2520 static void
2521free_array(char **array)
2522{
2523 char **curr = array;
2524 while (*curr != NULL)
2525 vim_free(*curr++);
2526 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002527}
2528
2529/*
2530 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2531 *
2532 * Replace a range of lines in the specified buffer. The line numbers are in
2533 * Vim format (1-based). The range is from lo up to, but not including, hi.
2534 * The replacement lines are given as a Scheme list of string objects. The
2535 * list is checked for validity and correct format.
2536 *
2537 * Errors are returned as a value of FAIL. The return value is OK on success.
2538 * If OK is returned and len_change is not NULL, *len_change is set to the
2539 * change in the buffer length.
2540 */
2541 static Scheme_Object *
2542set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2543{
2544 /* First of all, we check the type of the supplied MzScheme object.
2545 * There are three cases:
2546 * 1. #f - this is a deletion.
2547 * 2. A list - this is a replacement.
2548 * 3. Anything else - this is an error.
2549 */
2550 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002551 vim_mz_buffer *buf = NULL;
2552 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002553 int i, old_len, new_len, hi, lo;
2554 long extra;
2555
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002556 MZ_GC_DECL_REG(1);
2557 MZ_GC_VAR_IN_REG(0, line_list);
2558 MZ_GC_REG();
2559
Bram Moolenaar555b2802005-05-19 21:08:39 +00002560#ifdef HAVE_SANDBOX
2561 sandbox_check();
2562#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002563 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2564 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2565 if (!SCHEME_PAIRP(argv[2])
2566 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2567 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2568 line_list = argv[2];
2569 buf = get_buffer_arg(prim->name, 3, argc, argv);
2570 old_len = hi - lo;
2571 if (old_len < 0) /* process inverse values wisely */
2572 {
2573 i = lo;
2574 lo = hi;
2575 hi = i;
2576 old_len = -old_len;
2577 }
2578 extra = 0;
2579
2580 check_line_range(lo, buf->buf); /* inclusive */
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002581 check_line_range(hi - 1, buf->buf); /* exclusive */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002582
2583 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2584 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002585 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002586 curbuf = buf->buf;
2587
2588 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2589 {
2590 curbuf = savebuf;
2591 raise_vim_exn(_("cannot save undo information"));
2592 }
2593 else
2594 {
2595 for (i = 0; i < old_len; i++)
2596 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2597 {
2598 curbuf = savebuf;
2599 raise_vim_exn(_("cannot delete line"));
2600 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002601 if (buf->buf == curwin->w_buffer)
2602 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002603 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002604 }
2605
2606 curbuf = savebuf;
2607
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002608 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002609 raise_if_error();
2610 return scheme_void;
2611 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002612 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002613 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002614 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002615
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002616 /* List */
2617 new_len = scheme_proper_list_length(line_list);
2618 MZ_GC_CHECK();
2619 if (new_len < 0) /* improper or cyclic list */
2620 scheme_wrong_type(prim->name, "proper list",
2621 2, argc, argv);
2622 else
2623 {
2624 char **array = NULL;
2625 Scheme_Object *line = NULL;
2626 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002627
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002628 MZ_GC_DECL_REG(2);
2629 MZ_GC_VAR_IN_REG(0, line);
2630 MZ_GC_VAR_IN_REG(1, rest);
2631 MZ_GC_REG();
2632
Bram Moolenaar75676462013-01-30 14:55:42 +01002633 array = (char **)alloc((new_len+1)* sizeof(char *));
2634 vim_memset(array, 0, (new_len+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002635
2636 rest = line_list;
2637 for (i = 0; i < new_len; ++i)
2638 {
2639 line = SCHEME_CAR(rest);
2640 rest = SCHEME_CDR(rest);
2641 if (!SCHEME_STRINGP(line))
2642 {
2643 free_array(array);
2644 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2645 }
2646 array[i] = string_to_line(line);
2647 }
2648
2649 curbuf = buf->buf;
2650
2651 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2652 {
2653 curbuf = savebuf;
2654 free_array(array);
2655 raise_vim_exn(_("cannot save undo information"));
2656 }
2657
2658 /*
2659 * If the size of the range is reducing (ie, new_len < old_len) we
2660 * need to delete some old_len. We do this at the start, by
2661 * repeatedly deleting line "lo".
2662 */
2663 for (i = 0; i < old_len - new_len; ++i)
2664 {
2665 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2666 {
2667 curbuf = savebuf;
2668 free_array(array);
2669 raise_vim_exn(_("cannot delete line"));
2670 }
2671 extra--;
2672 }
2673
2674 /*
2675 * For as long as possible, replace the existing old_len with the
2676 * new old_len. This is a more efficient operation, as it requires
2677 * less memory allocation and freeing.
2678 */
2679 for (i = 0; i < old_len && i < new_len; i++)
2680 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2681 {
2682 curbuf = savebuf;
2683 free_array(array);
2684 raise_vim_exn(_("cannot replace line"));
2685 }
2686
2687 /*
2688 * Now we may need to insert the remaining new_len. We don't need to
2689 * free the string passed back because MzScheme has control of that
2690 * memory.
2691 */
2692 while (i < new_len)
2693 {
2694 if (ml_append((linenr_T)(lo + i - 1),
2695 (char_u *)array[i], 0, FALSE) == FAIL)
2696 {
2697 curbuf = savebuf;
2698 free_array(array);
2699 raise_vim_exn(_("cannot insert line"));
2700 }
2701 ++i;
2702 ++extra;
2703 }
2704 MZ_GC_UNREG();
2705 free_array(array);
2706 }
2707
2708 /*
2709 * Adjust marks. Invalidate any which lie in the
2710 * changed range, and move any in the remainder of the buffer.
2711 */
2712 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra);
2713 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2714
2715 if (buf->buf == curwin->w_buffer)
2716 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002717 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002718
2719 MZ_GC_UNREG();
2720 raise_if_error();
2721 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002722 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002723}
2724
2725/*
2726 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2727 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002728 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002729 * The line number is in Vim format (1-based). The lines to be inserted are
2730 * given as an MzScheme list of string objects or as a single string. The lines
2731 * to be added are checked for validity and correct format. Errors are
2732 * returned as a value of FAIL. The return value is OK on success.
2733 * If OK is returned and len_change is not NULL, *len_change
2734 * is set to the change in the buffer length.
2735 */
2736 static Scheme_Object *
2737insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2738{
2739 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002740 vim_mz_buffer *buf = NULL;
2741 Scheme_Object *list = NULL;
2742 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002743 int i, n, size;
2744
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002745 MZ_GC_DECL_REG(1);
2746 MZ_GC_VAR_IN_REG(0, list);
2747 MZ_GC_REG();
2748
Bram Moolenaar555b2802005-05-19 21:08:39 +00002749#ifdef HAVE_SANDBOX
2750 sandbox_check();
2751#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002752 /*
2753 * First of all, we check the type of the supplied MzScheme object.
2754 * It must be a string or a list, or the call is in error.
2755 */
2756 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2757 list = argv[1];
2758
2759 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2760 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2761 buf = get_buffer_arg(prim->name, 2, argc, argv);
2762
2763 if (n != 0) /* 0 can be used in insert */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002764 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002765 if (SCHEME_STRINGP(list))
2766 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002767 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002768
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002769 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002770 curbuf = buf->buf;
2771
2772 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2773 {
2774 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002775 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002776 raise_vim_exn(_("cannot save undo information"));
2777 }
2778 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2779 {
2780 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002781 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002782 raise_vim_exn(_("cannot insert line"));
2783 }
2784 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002785 {
2786 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002787 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002788 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002789
2790 curbuf = savebuf;
2791 update_screen(VALID);
2792
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002793 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002794 raise_if_error();
2795 return scheme_void;
2796 }
2797
2798 /* List */
2799 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002800 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002801 if (size < 0) /* improper or cyclic list */
2802 scheme_wrong_type(prim->name, "proper list",
2803 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002804 else
2805 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002806 Scheme_Object *line = NULL;
2807 Scheme_Object *rest = NULL;
2808 char **array;
2809 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002810
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002811 MZ_GC_DECL_REG(2);
2812 MZ_GC_VAR_IN_REG(0, line);
2813 MZ_GC_VAR_IN_REG(1, rest);
2814 MZ_GC_REG();
2815
Bram Moolenaar75676462013-01-30 14:55:42 +01002816 array = (char **)alloc((size+1) * sizeof(char *));
2817 vim_memset(array, 0, (size+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002818
2819 rest = list;
2820 for (i = 0; i < size; ++i)
2821 {
2822 line = SCHEME_CAR(rest);
2823 rest = SCHEME_CDR(rest);
2824 array[i] = string_to_line(line);
2825 }
2826
2827 curbuf = buf->buf;
2828
2829 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2830 {
2831 curbuf = savebuf;
2832 free_array(array);
2833 raise_vim_exn(_("cannot save undo information"));
2834 }
2835 else
2836 {
2837 for (i = 0; i < size; ++i)
2838 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2839 0, FALSE) == FAIL)
2840 {
2841 curbuf = savebuf;
2842 free_array(array);
2843 raise_vim_exn(_("cannot insert line"));
2844 }
2845
2846 if (i > 0)
2847 appended_lines_mark((linenr_T)n, (long)i);
2848 }
2849 free_array(array);
2850 MZ_GC_UNREG();
2851 curbuf = savebuf;
2852 update_screen(VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002853 }
2854
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002855 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002856 raise_if_error();
2857 return scheme_void;
2858}
2859
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002860/*
2861 * Predicates
2862 */
2863/* (buff? obj) */
2864 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002865vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002866{
2867 if (SCHEME_VIMBUFFERP(argv[0]))
2868 return scheme_true;
2869 else
2870 return scheme_false;
2871}
2872
2873/* (win? obj) */
2874 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002875vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002876{
2877 if (SCHEME_VIMWINDOWP(argv[0]))
2878 return scheme_true;
2879 else
2880 return scheme_false;
2881}
2882
2883/* (buff-valid? obj) */
2884 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002885vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002886{
2887 if (SCHEME_VIMBUFFERP(argv[0])
2888 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2889 return scheme_true;
2890 else
2891 return scheme_false;
2892}
2893
2894/* (win-valid? obj) */
2895 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002896vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002897{
2898 if (SCHEME_VIMWINDOWP(argv[0])
2899 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2900 return scheme_true;
2901 else
2902 return scheme_false;
2903}
2904
2905/*
2906 *===========================================================================
2907 * Utilities
2908 *===========================================================================
2909 */
2910
2911/*
2912 * Convert an MzScheme string into a Vim line.
2913 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002914 * All internal nulls are replaced by newline characters.
2915 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002916 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002917 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002918 */
2919 static char *
2920string_to_line(Scheme_Object *obj)
2921{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002922 char *scheme_str = NULL;
2923 char *vim_str = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01002924 OUTPUT_LEN_TYPE len;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002925 int i;
2926
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002927 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002928
2929 /* Error checking: String must not contain newlines, as we
2930 * are replacing a single line, and we must replace it with
2931 * a single line.
2932 */
Bram Moolenaar75676462013-01-30 14:55:42 +01002933 if (memchr(scheme_str, '\n', len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002934 scheme_signal_error(_("string cannot contain newlines"));
2935
Bram Moolenaar75676462013-01-30 14:55:42 +01002936 vim_str = (char *)alloc(len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002937
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002938 /* Create a copy of the string, with internal nulls replaced by
2939 * newline characters, as is the vim convention.
2940 */
2941 for (i = 0; i < len; ++i)
2942 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002943 if (scheme_str[i] == '\0')
2944 vim_str[i] = '\n';
2945 else
2946 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002947 }
2948
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002949 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002950
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002951 MZ_GC_CHECK();
2952 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002953}
2954
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002955#ifdef FEAT_EVAL
2956/*
2957 * Convert Vim value into MzScheme, adopted from if_python.c
2958 */
2959 static Scheme_Object *
Bram Moolenaar75676462013-01-30 14:55:42 +01002960vim_to_mzscheme(typval_T *vim_value)
2961{
2962 Scheme_Object *result = NULL;
2963 /* hash table to store visited values to avoid infinite loops */
2964 Scheme_Hash_Table *visited = NULL;
2965
2966 MZ_GC_DECL_REG(2);
2967 MZ_GC_VAR_IN_REG(0, result);
2968 MZ_GC_VAR_IN_REG(1, visited);
2969 MZ_GC_REG();
2970
2971 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2972 MZ_GC_CHECK();
2973
2974 result = vim_to_mzscheme_impl(vim_value, 1, visited);
2975
2976 MZ_GC_UNREG();
2977 return result;
2978}
2979
2980 static Scheme_Object *
2981vim_to_mzscheme_impl(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002982{
2983 Scheme_Object *result = NULL;
2984 int new_value = TRUE;
2985
Bram Moolenaar75676462013-01-30 14:55:42 +01002986 MZ_GC_DECL_REG(2);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002987 MZ_GC_VAR_IN_REG(0, result);
Bram Moolenaar75676462013-01-30 14:55:42 +01002988 MZ_GC_VAR_IN_REG(1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002989 MZ_GC_REG();
2990
2991 /* Avoid infinite recursion */
2992 if (depth > 100)
2993 {
2994 MZ_GC_UNREG();
2995 return scheme_void;
2996 }
2997
2998 /* Check if we run into a recursive loop. The item must be in visited
2999 * then and we can use it again.
3000 */
3001 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
3002 MZ_GC_CHECK();
3003 if (result != NULL) /* found, do nothing */
3004 new_value = FALSE;
3005 else if (vim_value->v_type == VAR_STRING)
3006 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003007 result = scheme_make_byte_string((char *)vim_value->vval.v_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003008 MZ_GC_CHECK();
3009 }
3010 else if (vim_value->v_type == VAR_NUMBER)
3011 {
3012 result = scheme_make_integer((long)vim_value->vval.v_number);
3013 MZ_GC_CHECK();
3014 }
3015# ifdef FEAT_FLOAT
3016 else if (vim_value->v_type == VAR_FLOAT)
3017 {
3018 result = scheme_make_double((double)vim_value->vval.v_float);
3019 MZ_GC_CHECK();
3020 }
3021# endif
3022 else if (vim_value->v_type == VAR_LIST)
3023 {
3024 list_T *list = vim_value->vval.v_list;
3025 listitem_T *curr;
3026
3027 if (list == NULL || list->lv_first == NULL)
3028 result = scheme_null;
3029 else
3030 {
3031 Scheme_Object *obj = NULL;
3032
3033 MZ_GC_DECL_REG(1);
3034 MZ_GC_VAR_IN_REG(0, obj);
3035 MZ_GC_REG();
3036
3037 curr = list->lv_last;
Bram Moolenaar75676462013-01-30 14:55:42 +01003038 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003039 result = scheme_make_pair(obj, scheme_null);
3040 MZ_GC_CHECK();
3041
3042 while (curr != list->lv_first)
3043 {
3044 curr = curr->li_prev;
Bram Moolenaar75676462013-01-30 14:55:42 +01003045 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003046 result = scheme_make_pair(obj, result);
3047 MZ_GC_CHECK();
3048 }
3049 }
3050 MZ_GC_UNREG();
3051 }
3052 else if (vim_value->v_type == VAR_DICT)
3053 {
3054 Scheme_Object *key = NULL;
3055 Scheme_Object *obj = NULL;
3056
3057 MZ_GC_DECL_REG(2);
3058 MZ_GC_VAR_IN_REG(0, key);
3059 MZ_GC_VAR_IN_REG(1, obj);
3060 MZ_GC_REG();
3061
3062 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
3063 MZ_GC_CHECK();
3064 if (vim_value->vval.v_dict != NULL)
3065 {
3066 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
3067 long_u todo = ht->ht_used;
3068 hashitem_T *hi;
3069 dictitem_T *di;
3070
3071 for (hi = ht->ht_array; todo > 0; ++hi)
3072 {
3073 if (!HASHITEM_EMPTY(hi))
3074 {
3075 --todo;
3076
3077 di = dict_lookup(hi);
Bram Moolenaar75676462013-01-30 14:55:42 +01003078 obj = vim_to_mzscheme_impl(&di->di_tv, depth + 1, visited);
3079 key = scheme_make_byte_string((char *)hi->hi_key);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003080 MZ_GC_CHECK();
3081 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
3082 MZ_GC_CHECK();
3083 }
3084 }
3085 }
3086 MZ_GC_UNREG();
3087 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003088 else if (vim_value->v_type == VAR_FUNC)
3089 {
3090 Scheme_Object *funcname = NULL;
3091
3092 MZ_GC_DECL_REG(1);
3093 MZ_GC_VAR_IN_REG(0, funcname);
3094 MZ_GC_REG();
3095
3096 funcname = scheme_make_byte_string((char *)vim_value->vval.v_string);
3097 MZ_GC_CHECK();
3098 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3099 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3100 MZ_GC_CHECK();
3101
3102 MZ_GC_UNREG();
3103 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003104 else if (vim_value->v_type == VAR_SPECIAL)
3105 {
3106 if (vim_value->vval.v_number <= VVAL_TRUE)
3107 result = scheme_make_integer((long)vim_value->vval.v_number);
3108 else
3109 result = scheme_null;
3110 MZ_GC_CHECK();
3111 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003112 else
3113 {
3114 result = scheme_void;
3115 new_value = FALSE;
3116 }
3117 if (new_value)
3118 {
3119 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
3120 MZ_GC_CHECK();
3121 }
3122 MZ_GC_UNREG();
3123 return result;
3124}
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003125
3126 static int
Bram Moolenaar75676462013-01-30 14:55:42 +01003127mzscheme_to_vim(Scheme_Object *obj, typval_T *tv)
3128{
3129 int i, status;
3130 Scheme_Hash_Table *visited = NULL;
3131
3132 MZ_GC_DECL_REG(2);
3133 MZ_GC_VAR_IN_REG(0, obj);
3134 MZ_GC_VAR_IN_REG(1, visited);
3135 MZ_GC_REG();
3136
3137 visited = scheme_make_hash_table(SCHEME_hash_ptr);
3138 MZ_GC_CHECK();
3139
3140 status = mzscheme_to_vim_impl(obj, tv, 1, visited);
3141 for (i = 0; i < visited->size; ++i)
3142 {
3143 /* free up remembered objects */
3144 if (visited->vals[i] != NULL)
3145 free_tv((typval_T *)visited->vals[i]);
3146 }
3147
3148 MZ_GC_UNREG();
3149 return status;
3150}
3151 static int
3152mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003153 Scheme_Hash_Table *visited)
3154{
3155 int status = OK;
3156 typval_T *found;
Bram Moolenaar75676462013-01-30 14:55:42 +01003157
3158 MZ_GC_DECL_REG(2);
3159 MZ_GC_VAR_IN_REG(0, obj);
3160 MZ_GC_VAR_IN_REG(1, visited);
3161 MZ_GC_REG();
3162
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003163 MZ_GC_CHECK();
3164 if (depth > 100) /* limit the deepest recursion level */
3165 {
3166 tv->v_type = VAR_NUMBER;
3167 tv->vval.v_number = 0;
3168 return FAIL;
3169 }
3170
3171 found = (typval_T *)scheme_hash_get(visited, obj);
3172 if (found != NULL)
3173 copy_tv(found, tv);
3174 else if (SCHEME_VOIDP(obj))
3175 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003176 tv->v_type = VAR_SPECIAL;
3177 tv->vval.v_number = VVAL_NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003178 }
3179 else if (SCHEME_INTP(obj))
3180 {
3181 tv->v_type = VAR_NUMBER;
3182 tv->vval.v_number = SCHEME_INT_VAL(obj);
3183 }
3184 else if (SCHEME_BOOLP(obj))
3185 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003186 tv->v_type = VAR_SPECIAL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003187 tv->vval.v_number = SCHEME_TRUEP(obj);
3188 }
3189# ifdef FEAT_FLOAT
3190 else if (SCHEME_DBLP(obj))
3191 {
3192 tv->v_type = VAR_FLOAT;
3193 tv->vval.v_float = SCHEME_DBL_VAL(obj);
3194 }
3195# endif
Bram Moolenaar75676462013-01-30 14:55:42 +01003196 else if (SCHEME_BYTE_STRINGP(obj))
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003197 {
3198 tv->v_type = VAR_STRING;
Bram Moolenaar75676462013-01-30 14:55:42 +01003199 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(obj));
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003200 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003201# if MZSCHEME_VERSION_MAJOR >= 299
3202 else if (SCHEME_CHAR_STRINGP(obj))
3203 {
3204 Scheme_Object *tmp = NULL;
3205 MZ_GC_DECL_REG(1);
3206 MZ_GC_VAR_IN_REG(0, tmp);
3207 MZ_GC_REG();
3208
3209 tmp = scheme_char_string_to_byte_string(obj);
3210 tv->v_type = VAR_STRING;
3211 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(tmp));
3212 MZ_GC_UNREG();
3213 }
3214#endif
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003215 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
3216 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3217 {
3218 list_T *list = list_alloc();
3219 if (list == NULL)
3220 status = FAIL;
3221 else
3222 {
3223 int i;
3224 Scheme_Object *curr = NULL;
3225 Scheme_Object *cval = NULL;
3226 /* temporary var to hold current element of vectors and pairs */
3227 typval_T *v;
3228
3229 MZ_GC_DECL_REG(2);
3230 MZ_GC_VAR_IN_REG(0, curr);
3231 MZ_GC_VAR_IN_REG(1, cval);
3232 MZ_GC_REG();
3233
3234 tv->v_type = VAR_LIST;
3235 tv->vval.v_list = list;
3236 ++list->lv_refcount;
3237
3238 v = (typval_T *)alloc(sizeof(typval_T));
3239 if (v == NULL)
3240 status = FAIL;
3241 else
3242 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003243 /* add the value in advance to allow handling of self-referential
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003244 * data structures */
3245 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3246 copy_tv(tv, visited_tv);
3247 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3248
3249 if (SCHEME_VECTORP(obj))
3250 {
3251 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
3252 {
3253 cval = SCHEME_VEC_ELS(obj)[i];
Bram Moolenaar75676462013-01-30 14:55:42 +01003254 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003255 if (status == FAIL)
3256 break;
3257 status = list_append_tv(list, v);
3258 clear_tv(v);
3259 if (status == FAIL)
3260 break;
3261 }
3262 }
3263 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3264 {
3265 for (curr = obj;
3266 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
3267 curr = SCHEME_CDR(curr))
3268 {
3269 cval = SCHEME_CAR(curr);
Bram Moolenaar75676462013-01-30 14:55:42 +01003270 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003271 if (status == FAIL)
3272 break;
3273 status = list_append_tv(list, v);
3274 clear_tv(v);
3275 if (status == FAIL)
3276 break;
3277 }
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003278 /* improper list not terminated with null
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003279 * need to handle the last element */
3280 if (status == OK && !SCHEME_NULLP(curr))
3281 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003282 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003283 if (status == OK)
3284 {
3285 status = list_append_tv(list, v);
3286 clear_tv(v);
3287 }
3288 }
3289 }
3290 /* nothing to do for scheme_null */
3291 vim_free(v);
3292 }
3293 MZ_GC_UNREG();
3294 }
3295 }
3296 else if (SCHEME_HASHTP(obj))
3297 {
3298 int i;
3299 dict_T *dict;
3300 Scheme_Object *key = NULL;
3301 Scheme_Object *val = NULL;
3302
3303 MZ_GC_DECL_REG(2);
3304 MZ_GC_VAR_IN_REG(0, key);
3305 MZ_GC_VAR_IN_REG(1, val);
3306 MZ_GC_REG();
3307
3308 dict = dict_alloc();
3309 if (dict == NULL)
3310 status = FAIL;
3311 else
3312 {
3313 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3314
3315 tv->v_type = VAR_DICT;
3316 tv->vval.v_dict = dict;
3317 ++dict->dv_refcount;
3318
3319 copy_tv(tv, visited_tv);
3320 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3321
3322 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
3323 {
3324 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
3325 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003326 /* generate item for `display'ed Scheme key */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003327 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
3328 ((Scheme_Hash_Table *) obj)->keys[i]));
3329 /* convert Scheme val to Vim and add it to the dict */
Bram Moolenaar75676462013-01-30 14:55:42 +01003330 if (mzscheme_to_vim_impl(((Scheme_Hash_Table *) obj)->vals[i],
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003331 &item->di_tv, depth + 1, visited) == FAIL
3332 || dict_add(dict, item) == FAIL)
3333 {
3334 dictitem_free(item);
3335 status = FAIL;
3336 break;
3337 }
3338 }
3339
3340 }
3341 }
3342 MZ_GC_UNREG();
3343 }
3344 else
3345 {
3346 /* `display' any other value to string */
3347 tv->v_type = VAR_STRING;
3348 tv->vval.v_string = (char_u *)string_to_line(obj);
3349 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003350 MZ_GC_UNREG();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003351 return status;
3352}
3353
Bram Moolenaar75676462013-01-30 14:55:42 +01003354/* Scheme prim procedure wrapping Vim funcref */
3355 static Scheme_Object *
3356vim_funcref(void *name, int argc, Scheme_Object **argv)
3357{
3358 int i;
3359 typval_T args;
3360 int status = OK;
3361 Scheme_Object *result = NULL;
3362 list_T *list = list_alloc();
3363
3364 MZ_GC_DECL_REG(1);
3365 MZ_GC_VAR_IN_REG(0, result);
3366 MZ_GC_REG();
3367
3368 result = scheme_void;
3369 if (list == NULL)
3370 status = FAIL;
3371 else
3372 {
3373 args.v_type = VAR_LIST;
3374 args.vval.v_list = list;
3375 ++list->lv_refcount;
3376 for (i = 0; status == OK && i < argc; ++i)
3377 {
3378 typval_T *v = (typval_T *)alloc(sizeof(typval_T));
3379 if (v == NULL)
3380 status = FAIL;
3381 else
3382 {
3383 status = mzscheme_to_vim(argv[i], v);
3384 if (status == OK)
3385 {
3386 status = list_append_tv(list, v);
3387 clear_tv(v);
3388 }
3389 vim_free(v);
3390 }
3391 }
3392 if (status == OK)
3393 {
3394 typval_T ret;
3395 ret.v_type = VAR_UNKNOWN;
3396
3397 mzscheme_call_vim(BYTE_STRING_VALUE((Scheme_Object *)name), &args, &ret);
3398 MZ_GC_CHECK();
3399 result = vim_to_mzscheme(&ret);
3400 clear_tv(&ret);
3401 MZ_GC_CHECK();
3402 }
3403 }
3404 clear_tv(&args);
3405 MZ_GC_UNREG();
3406 if (status != OK)
3407 raise_vim_exn(_("error converting Scheme values to Vim"));
3408 else
3409 raise_if_error();
3410 return result;
3411}
3412
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003413 void
3414do_mzeval(char_u *str, typval_T *rettv)
3415{
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003416 Scheme_Object *ret = NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003417
Bram Moolenaar75676462013-01-30 14:55:42 +01003418 MZ_GC_DECL_REG(1);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003419 MZ_GC_VAR_IN_REG(0, ret);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003420 MZ_GC_REG();
3421
3422 if (mzscheme_init())
3423 {
3424 MZ_GC_UNREG();
3425 return;
3426 }
3427
3428 MZ_GC_CHECK();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003429 if (eval_with_exn_handling(str, do_eval, &ret) == OK)
Bram Moolenaar75676462013-01-30 14:55:42 +01003430 mzscheme_to_vim(ret, rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003431
3432 MZ_GC_UNREG();
3433}
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003434#endif
3435
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003436/*
3437 * Check to see whether a Vim error has been reported, or a keyboard
3438 * interrupt (from vim --> got_int) has been detected.
3439 */
3440 static int
3441vim_error_check(void)
3442{
3443 return (got_int || did_emsg);
3444}
3445
3446/*
3447 * register Scheme exn:vim
3448 */
3449 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003450register_vim_exn(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003451{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003452 int nc = 0;
3453 int i;
3454 Scheme_Object *struct_exn = NULL;
3455 Scheme_Object *exn_name = NULL;
3456
3457 MZ_GC_DECL_REG(2);
3458 MZ_GC_VAR_IN_REG(0, struct_exn);
3459 MZ_GC_VAR_IN_REG(1, exn_name);
3460 MZ_GC_REG();
3461
3462 exn_name = scheme_intern_symbol("exn:vim");
3463 MZ_GC_CHECK();
3464 struct_exn = scheme_builtin_value("struct:exn");
3465 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003466
3467 if (vim_exn == NULL)
3468 vim_exn = scheme_make_struct_type(exn_name,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003469 struct_exn, NULL, 0, 0, NULL, NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003470#if MZSCHEME_VERSION_MAJOR >= 299
3471 , NULL
3472#endif
3473 );
3474
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003475
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003476 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003477 Scheme_Object **tmp = NULL;
3478 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
3479 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
3480 MZ_GC_DECL_REG(6);
3481 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
3482 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
3483 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003484
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003485 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003486 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
3487 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003488
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003489 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
3490 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
3491 MZ_GC_CHECK();
3492
3493 for (i = 0; i < nc; i++)
3494 {
3495 scheme_add_global_symbol(exn_names[i],
3496 exn_values[i], environment);
3497 MZ_GC_CHECK();
3498 }
3499 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003500 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003501 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003502}
3503
3504/*
3505 * raise exn:vim, may be with additional info string
3506 */
3507 void
3508raise_vim_exn(const char *add_info)
3509{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003510 char *fmt = _("Vim error: ~a");
3511 Scheme_Object *argv[2] = {NULL, NULL};
3512 Scheme_Object *exn = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01003513 Scheme_Object *byte_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003514
Bram Moolenaar75676462013-01-30 14:55:42 +01003515 MZ_GC_DECL_REG(5);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003516 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
3517 MZ_GC_VAR_IN_REG(3, exn);
Bram Moolenaar75676462013-01-30 14:55:42 +01003518 MZ_GC_VAR_IN_REG(4, byte_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003519 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003520
3521 if (add_info != NULL)
3522 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003523 char *c_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003524 Scheme_Object *info = NULL;
3525
3526 MZ_GC_DECL_REG(3);
3527 MZ_GC_VAR_IN_REG(0, c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003528 MZ_GC_VAR_IN_REG(2, info);
3529 MZ_GC_REG();
3530
Bram Moolenaar75676462013-01-30 14:55:42 +01003531 info = scheme_make_byte_string(add_info);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003532 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01003533 c_string = scheme_format_utf8(fmt, STRLEN(fmt), 1, &info, NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003534 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01003535 byte_string = scheme_make_byte_string(c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003536 MZ_GC_CHECK();
3537 argv[0] = scheme_byte_string_to_char_string(byte_string);
Bram Moolenaar555b2802005-05-19 21:08:39 +00003538 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003539 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003540 }
3541 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003542 {
3543 byte_string = scheme_make_byte_string(_("Vim error"));
3544 MZ_GC_CHECK();
3545 argv[0] = scheme_byte_string_to_char_string(byte_string);
3546 MZ_GC_CHECK();
3547 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003548 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003549
Bram Moolenaar049377e2007-05-12 15:32:12 +00003550#if MZSCHEME_VERSION_MAJOR < 360
3551 argv[1] = scheme_current_continuation_marks();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003552 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003553#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003554 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003555 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003556#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003557
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003558 exn = scheme_make_struct_instance(vim_exn, 2, argv);
3559 MZ_GC_CHECK();
3560 scheme_raise(exn);
3561 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003562}
3563
3564 void
3565raise_if_error(void)
3566{
3567 if (vim_error_check())
3568 raise_vim_exn(NULL);
3569}
3570
3571/* get buffer:
3572 * either current
3573 * or passed as argv[argnum] with checks
3574 */
3575 static vim_mz_buffer *
3576get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3577{
3578 vim_mz_buffer *b;
3579
3580 if (argc < argnum + 1)
3581 return get_vim_curr_buffer();
3582 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3583 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3584 b = (vim_mz_buffer *)argv[argnum];
3585 (void)get_valid_buffer(argv[argnum]);
3586 return b;
3587}
3588
3589/* get window:
3590 * either current
3591 * or passed as argv[argnum] with checks
3592 */
3593 static vim_mz_window *
3594get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3595{
3596 vim_mz_window *w;
3597
3598 if (argc < argnum + 1)
3599 return get_vim_curr_window();
3600 w = (vim_mz_window *)argv[argnum];
3601 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3602 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3603 (void)get_valid_window(argv[argnum]);
3604 return w;
3605}
3606
3607/* get valid Vim buffer from Scheme_Object* */
3608buf_T *get_valid_buffer(void *obj)
3609{
3610 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3611
3612 if (buf == INVALID_BUFFER_VALUE)
3613 scheme_signal_error(_("buffer is invalid"));
3614 return buf;
3615}
3616
3617/* get valid Vim window from Scheme_Object* */
3618win_T *get_valid_window(void *obj)
3619{
3620 win_T *win = ((vim_mz_window *)obj)->win;
3621 if (win == INVALID_WINDOW_VALUE)
3622 scheme_signal_error(_("window is invalid"));
3623 return win;
3624}
3625
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003626 int
3627mzthreads_allowed(void)
3628{
3629 return mz_threads_allow;
3630}
3631
3632 static int
3633line_in_range(linenr_T lnum, buf_T *buf)
3634{
3635 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3636}
3637
3638 static void
3639check_line_range(linenr_T lnum, buf_T *buf)
3640{
3641 if (!line_in_range(lnum, buf))
3642 scheme_signal_error(_("linenr out of range"));
3643}
3644
3645/*
3646 * Check if deleting lines made the cursor position invalid
3647 * (or you'll get msg from Vim about invalid linenr).
3648 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3649 * deleted). Got from if_python.c
3650 */
3651 static void
3652mz_fix_cursor(int lo, int hi, int extra)
3653{
3654 if (curwin->w_cursor.lnum >= lo)
3655 {
3656 /* Adjust the cursor position if it's in/after the changed
3657 * lines. */
3658 if (curwin->w_cursor.lnum >= hi)
3659 {
3660 curwin->w_cursor.lnum += extra;
3661 check_cursor_col();
3662 }
3663 else if (extra < 0)
3664 {
3665 curwin->w_cursor.lnum = lo;
3666 check_cursor();
3667 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003668 else
3669 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003670 changed_cline_bef_curs();
3671 }
3672 invalidate_botline();
3673}
3674
3675static Vim_Prim prims[]=
3676{
3677 /*
3678 * Buffer-related commands
3679 */
3680 {get_buffer_line, "get-buff-line", 1, 2},
3681 {set_buffer_line, "set-buff-line", 2, 3},
3682 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3683 {get_buffer_name, "get-buff-name", 0, 1},
3684 {get_buffer_num, "get-buff-num", 0, 1},
3685 {get_buffer_size, "get-buff-size", 0, 1},
3686 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3687 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3688 {get_curr_buffer, "curr-buff", 0, 0},
3689 {get_buffer_count, "buff-count", 0, 0},
3690 {get_next_buffer, "get-next-buff", 0, 1},
3691 {get_prev_buffer, "get-prev-buff", 0, 1},
3692 {mzscheme_open_buffer, "open-buff", 1, 1},
3693 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3694 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003695 /*
3696 * Window-related commands
3697 */
3698 {get_curr_win, "curr-win", 0, 0},
3699 {get_window_count, "win-count", 0, 0},
3700 {get_window_by_num, "get-win-by-num", 1, 1},
3701 {get_window_num, "get-win-num", 0, 1},
3702 {get_window_buffer, "get-win-buffer", 0, 1},
3703 {get_window_height, "get-win-height", 0, 1},
3704 {set_window_height, "set-win-height", 1, 2},
3705#ifdef FEAT_VERTSPLIT
3706 {get_window_width, "get-win-width", 0, 1},
3707 {set_window_width, "set-win-width", 1, 2},
3708#endif
3709 {get_cursor, "get-cursor", 0, 1},
3710 {set_cursor, "set-cursor", 1, 2},
3711 {get_window_list, "get-win-list", 0, 1},
3712 /*
3713 * Vim-related commands
3714 */
3715 {vim_command, "command", 1, 1},
3716 {vim_eval, "eval", 1, 1},
3717 {get_range_start, "range-start", 0, 0},
3718 {get_range_end, "range-end", 0, 0},
3719 {mzscheme_beep, "beep", 0, 0},
3720 {get_option, "get-option", 1, 2},
3721 {set_option, "set-option", 1, 2},
3722 /*
3723 * small utilities
3724 */
3725 {vim_bufferp, "buff?", 1, 1},
3726 {vim_windowp, "win?", 1, 1},
3727 {vim_buffer_validp, "buff-valid?", 1, 1},
3728 {vim_window_validp, "win-valid?", 1, 1}
3729};
3730
3731/* return MzScheme wrapper for curbuf */
3732 static vim_mz_buffer *
3733get_vim_curr_buffer(void)
3734{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003735 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003736 return (vim_mz_buffer *)buffer_new(curbuf);
3737 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003738 return BUFFER_REF(curbuf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003739}
3740
3741/* return MzScheme wrapper for curwin */
3742 static vim_mz_window *
3743get_vim_curr_window(void)
3744{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003745 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003746 return (vim_mz_window *)window_new(curwin);
3747 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003748 return WINDOW_REF(curwin);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003749}
3750
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003751 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003752make_modules(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003753{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003754 int i;
3755 Scheme_Env *mod = NULL;
3756 Scheme_Object *vimext_symbol = NULL;
3757 Scheme_Object *closed_prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003758
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003759 MZ_GC_DECL_REG(3);
3760 MZ_GC_VAR_IN_REG(0, mod);
3761 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3762 MZ_GC_VAR_IN_REG(2, closed_prim);
3763 MZ_GC_REG();
3764
3765 vimext_symbol = scheme_intern_symbol("vimext");
3766 MZ_GC_CHECK();
3767 mod = scheme_primitive_module(vimext_symbol, environment);
3768 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003769 /* all prims made closed so they can access their own names */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003770 for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003771 {
3772 Vim_Prim *prim = prims + i;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003773 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3774 prim->mina, prim->maxa);
3775 scheme_add_global(prim->name, closed_prim, mod);
3776 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003777 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003778 scheme_finish_primitive_module(mod);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003779 MZ_GC_CHECK();
3780 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003781}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003782
Bram Moolenaar555b2802005-05-19 21:08:39 +00003783#ifdef HAVE_SANDBOX
3784static Scheme_Object *M_write = NULL;
3785static Scheme_Object *M_read = NULL;
3786static Scheme_Object *M_execute = NULL;
3787static Scheme_Object *M_delete = NULL;
3788
3789 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003790sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003791{
3792 if (sandbox)
3793 raise_vim_exn(_("not allowed in the Vim sandbox"));
3794}
3795
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003796/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003797 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003798sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003799{
3800 if (sandbox)
3801 {
3802 Scheme_Object *requested_access = argv[2];
3803
3804 if (M_write == NULL)
3805 {
3806 MZ_REGISTER_STATIC(M_write);
3807 M_write = scheme_intern_symbol("write");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003808 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003809 }
3810 if (M_read == NULL)
3811 {
3812 MZ_REGISTER_STATIC(M_read);
3813 M_read = scheme_intern_symbol("read");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003814 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003815 }
3816 if (M_execute == NULL)
3817 {
3818 MZ_REGISTER_STATIC(M_execute);
3819 M_execute = scheme_intern_symbol("execute");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003820 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003821 }
3822 if (M_delete == NULL)
3823 {
3824 MZ_REGISTER_STATIC(M_delete);
3825 M_delete = scheme_intern_symbol("delete");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003826 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003827 }
3828
3829 while (!SCHEME_NULLP(requested_access))
3830 {
3831 Scheme_Object *item = SCHEME_CAR(requested_access);
3832 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3833 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
3834 {
3835 raise_vim_exn(_("not allowed in the Vim sandbox"));
3836 }
3837 requested_access = SCHEME_CDR(requested_access);
3838 }
3839 }
3840 return scheme_void;
3841}
3842
3843 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003844sandbox_network_guard(int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003845{
3846 return scheme_void;
3847}
3848#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00003849
3850#endif