blob: 657782273163bc3fd98408a00884a2303664d74b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002 *
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 **);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000148static Scheme_Object *get_window_width(void *, int, Scheme_Object **);
149static Scheme_Object *set_window_width(void *, int, Scheme_Object **);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000150static Scheme_Object *get_cursor(void *, int, Scheme_Object **);
151static Scheme_Object *set_cursor(void *, int, Scheme_Object **);
152static Scheme_Object *get_window_list(void *, int, Scheme_Object **);
153static vim_mz_window *get_vim_curr_window(void);
154
155/* Vim-related commands */
156static Scheme_Object *mzscheme_beep(void *, int, Scheme_Object **);
157static Scheme_Object *get_option(void *, int, Scheme_Object **);
158static Scheme_Object *set_option(void *, int, Scheme_Object **);
159static Scheme_Object *vim_command(void *, int, Scheme_Object **);
160static Scheme_Object *vim_eval(void *, int, Scheme_Object **);
161static Scheme_Object *vim_bufferp(void *data, int, Scheme_Object **);
162static Scheme_Object *vim_windowp(void *data, int, Scheme_Object **);
163static Scheme_Object *vim_buffer_validp(void *data, int, Scheme_Object **);
164static Scheme_Object *vim_window_validp(void *data, int, Scheme_Object **);
165
166/*
167 *========================================================================
168 * Internal Function Prototypes
169 *========================================================================
170 */
171static int vim_error_check(void);
172static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100173static int startup_mzscheme(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000174static char *string_to_line(Scheme_Object *obj);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100175#if MZSCHEME_VERSION_MAJOR >= 501
Bram Moolenaar75676462013-01-30 14:55:42 +0100176# define OUTPUT_LEN_TYPE intptr_t
177#else
178# define OUTPUT_LEN_TYPE long
179#endif
180static void do_output(char *mesg, OUTPUT_LEN_TYPE len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181static void do_printf(char *format, ...);
182static void do_flush(void);
183static Scheme_Object *_apply_thunk_catch_exceptions(
184 Scheme_Object *, Scheme_Object **);
185static Scheme_Object *extract_exn_message(Scheme_Object *v);
186static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
187static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000188static void register_vim_exn(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000189static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
190 int argc, Scheme_Object **argv);
191static vim_mz_window *get_window_arg(const char *fname, int argnum,
192 int argc, Scheme_Object **argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000193static int line_in_range(linenr_T, buf_T *);
194static void check_line_range(linenr_T, buf_T *);
195static void mz_fix_cursor(int lo, int hi, int extra);
196
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000197static int eval_with_exn_handling(void *, Scheme_Closed_Prim *,
198 Scheme_Object **ret);
199static void make_modules(void);
200static void init_exn_catching_apply(void);
201static int mzscheme_env_main(Scheme_Env *env, int argc, char **argv);
202static int mzscheme_init(void);
203#ifdef FEAT_EVAL
Bram Moolenaar75676462013-01-30 14:55:42 +0100204static Scheme_Object *vim_to_mzscheme(typval_T *vim_value);
205static Scheme_Object *vim_to_mzscheme_impl(typval_T *vim_value, int depth,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000206 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100207static int mzscheme_to_vim(Scheme_Object *obj, typval_T *tv);
208static int mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100209 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100210static Scheme_Object *vim_funcref(void *data, int argc, Scheme_Object **argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000211#endif
212
213#ifdef MZ_PRECISE_GC
Bram Moolenaar64404472010-06-26 06:24:45 +0200214static int buffer_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000215{
216 return gcBYTES_TO_WORDS(sizeof(vim_mz_buffer));
217}
218static int buffer_mark_proc(void *obj)
219{
220 return buffer_size_proc(obj);
221}
222static int buffer_fixup_proc(void *obj)
223{
Bram Moolenaar75676462013-01-30 14:55:42 +0100224 /* apparently not needed as the object will be uncollectable while
225 * the buffer is alive
226 */
227 /*
228 vim_mz_buffer* buf = (vim_mz_buffer*) obj;
229 buf->buf->b_mzscheme_ref = GC_fixup_self(obj);
230 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000231 return buffer_size_proc(obj);
232}
Bram Moolenaar64404472010-06-26 06:24:45 +0200233static int window_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000234{
235 return gcBYTES_TO_WORDS(sizeof(vim_mz_window));
236}
237static int window_mark_proc(void *obj)
238{
239 return window_size_proc(obj);
240}
241static int window_fixup_proc(void *obj)
242{
Bram Moolenaar75676462013-01-30 14:55:42 +0100243 /* apparently not needed as the object will be uncollectable while
244 * the window is alive
245 */
246 /*
247 vim_mz_window* win = (vim_mz_window*) obj;
248 win->win->w_mzscheme_ref = GC_fixup_self(obj);
249 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000250 return window_size_proc(obj);
251}
Bram Moolenaar75676462013-01-30 14:55:42 +0100252/* with precise GC, w_mzscheme_ref and b_mzscheme_ref are immobile boxes
253 * containing pointers to a window/buffer
254 * with conservative GC these are simply pointers*/
255# define WINDOW_REF(win) *(vim_mz_window **)((win)->w_mzscheme_ref)
256# define BUFFER_REF(buf) *(vim_mz_buffer **)((buf)->b_mzscheme_ref)
257#else
258# define WINDOW_REF(win) (vim_mz_window *)((win)->w_mzscheme_ref)
259# define BUFFER_REF(buf) (vim_mz_buffer *)((buf)->b_mzscheme_ref)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000260#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000261
Bram Moolenaar4349c572016-01-30 13:28:28 +0100262#if defined(DYNAMIC_MZSCHEME) || defined(PROTO)
Bram Moolenaar33570922005-01-25 22:26:29 +0000263static Scheme_Object *dll_scheme_eof;
264static Scheme_Object *dll_scheme_false;
265static Scheme_Object *dll_scheme_void;
266static Scheme_Object *dll_scheme_null;
267static Scheme_Object *dll_scheme_true;
268
269static Scheme_Thread **dll_scheme_current_thread_ptr;
270
271static void (**dll_scheme_console_printf_ptr)(char *str, ...);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100272static void (**dll_scheme_console_output_ptr)(char *str, OUTPUT_LEN_TYPE len);
Bram Moolenaar33570922005-01-25 22:26:29 +0000273static void (**dll_scheme_notify_multithread_ptr)(int on);
274
275static void *(*dll_GC_malloc)(size_t size_in_bytes);
276static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
277static Scheme_Env *(*dll_scheme_basic_env)(void);
278static void (*dll_scheme_check_threads)(void);
279static void (*dll_scheme_register_static)(void *ptr, long size);
280static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
281static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
282 Scheme_Env *env);
283static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
284 Scheme_Object *val, Scheme_Env *env);
285static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
286 Scheme_Object **rands);
287static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000288# if MZSCHEME_VERSION_MAJOR >= 299
289static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100290static Scheme_Object *(*dll_scheme_make_path)(const char *chars);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000291# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000292static void (*dll_scheme_close_input_port)(Scheme_Object *port);
293static void (*dll_scheme_count_lines)(Scheme_Object *port);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000294#if MZSCHEME_VERSION_MAJOR < 360
Bram Moolenaar33570922005-01-25 22:26:29 +0000295static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000296#else
297static Scheme_Object *(*dll_scheme_current_continuation_marks)(Scheme_Object *prompt_tag);
298#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000299static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100300static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, OUTPUT_LEN_TYPE *len);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000301static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
Bram Moolenaar33570922005-01-25 22:26:29 +0000302static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
303 int _num_rands, Scheme_Object **rands, int val);
304static void (*dll_scheme_dont_gc_ptr)(void *p);
305static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
306static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
307 Scheme_Env *env);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000308static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
Bram Moolenaar33570922005-01-25 22:26:29 +0000309 Scheme_Env *env, int all);
310static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000311# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000312static char *(*dll_scheme_format)(char *format, int flen, int argc,
313 Scheme_Object **argv, long *rlen);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000314# else
315static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100316 Scheme_Object **argv, OUTPUT_LEN_TYPE *rlen);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000317static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000318# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000319static void (*dll_scheme_gc_ptr_ok)(void *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000320# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000321static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
322 long *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000323# else
324static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100325 OUTPUT_LEN_TYPE *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000326# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000327static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
328static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
329 Scheme_Env *env);
330static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
331 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
332 mzshort maxa);
333static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000334static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
Bram Moolenaar33570922005-01-25 22:26:29 +0000335 Scheme_Object *cdr);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000336static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
337 const char *name, mzshort mina, mzshort maxa);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000338# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000339static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
340static Scheme_Object *(*dll_scheme_make_string_output_port)();
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000341# else
342static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
343static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
344# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000345static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
346 int argc, Scheme_Object **argv);
347static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
348 Scheme_Object *field_names, int flags, int *count_out);
349static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
350 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
351 int num_uninit_fields, Scheme_Object *uninit_val,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000352 Scheme_Object *properties
353# if MZSCHEME_VERSION_MAJOR >= 299
354 , Scheme_Object *guard
355# endif
356 );
Bram Moolenaar33570922005-01-25 22:26:29 +0000357static Scheme_Object **(*dll_scheme_make_struct_values)(
358 Scheme_Object *struct_type, Scheme_Object **names, int count,
359 int flags);
360static Scheme_Type (*dll_scheme_make_type)(const char *name);
361static Scheme_Object *(*dll_scheme_make_vector)(int size,
362 Scheme_Object *fill);
363static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
364static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
365 const char *who);
366static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
367 Scheme_Env *for_env);
368static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
369static void (*dll_scheme_raise)(Scheme_Object *exn);
370static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
371static void (*dll_scheme_signal_error)(const char *msg, ...);
372static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
373 int which, int argc, Scheme_Object **argv);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000374# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000375static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000376 Scheme_Object *o);
377static Scheme_Config *(*dll_scheme_current_config)(void);
378static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
379 (Scheme_Object *s);
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000380static Scheme_Object *(*dll_scheme_char_string_to_path)
381 (Scheme_Object *s);
Bram Moolenaar75676462013-01-30 14:55:42 +0100382static void *(*dll_scheme_set_collects_path)(Scheme_Object *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000383# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000384static Scheme_Hash_Table *(*dll_scheme_make_hash_table)(int type);
385static void (*dll_scheme_hash_set)(Scheme_Hash_Table *table,
386 Scheme_Object *key, Scheme_Object *value);
387static Scheme_Object *(*dll_scheme_hash_get)(Scheme_Hash_Table *table,
388 Scheme_Object *key);
389static Scheme_Object *(*dll_scheme_make_double)(double d);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000390static Scheme_Object *(*dll_scheme_make_sized_byte_string)(char *chars,
391 long len, int copy);
392static Scheme_Object *(*dll_scheme_namespace_require)(Scheme_Object *req);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100393static 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);
394# ifdef MZ_PRECISE_GC
395static void *(*dll_GC_malloc_one_tagged)(size_t size_in_bytes);
396static void (*dll_GC_register_traversers)(short tag, Size_Proc size, Mark_Proc mark, Fixup_Proc fixup, int is_constant_size, int is_atomic);
397# endif
398# if MZSCHEME_VERSION_MAJOR >= 400
399static void (*dll_scheme_init_collection_paths)(Scheme_Env *global_env, Scheme_Object *extra_dirs);
400static void **(*dll_scheme_malloc_immobile_box)(void *p);
401static void (*dll_scheme_free_immobile_box)(void **b);
402# endif
403# if MZSCHEME_VERSION_MAJOR >= 500
404# ifdef TRAMPOLINED_MZVIM_STARTUP
405static int (*dll_scheme_main_setup)(int no_auto_statics, Scheme_Env_Main _main, int argc, char **argv);
406# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
407static void (*dll_scheme_register_tls_space)(void *tls_space, int _tls_index);
408# endif
409# endif
410# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
411static Thread_Local_Variables *(*dll_scheme_external_get_thread_local_variables)(void);
412# endif
413# endif
414# if MZSCHEME_VERSION_MAJOR >= 600
415static void (*dll_scheme_embedded_load)(intptr_t len, const char *s, int predefined);
416static void (*dll_scheme_register_embedded_load)(intptr_t len, const char *s);
417static void (*dll_scheme_set_config_path)(Scheme_Object *p);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000418# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000419
Bram Moolenaar4349c572016-01-30 13:28:28 +0100420#if defined(DYNAMIC_MZSCHEME) /* not when defined(PROTO) */
421
Bram Moolenaar33570922005-01-25 22:26:29 +0000422/* arrays are imported directly */
423# define scheme_eof dll_scheme_eof
424# define scheme_false dll_scheme_false
425# define scheme_void dll_scheme_void
426# define scheme_null dll_scheme_null
427# define scheme_true dll_scheme_true
428
429/* pointers are GetProceAddress'ed as pointers to pointer */
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100430#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
431# define scheme_current_thread (*dll_scheme_current_thread_ptr)
432# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000433# define scheme_console_printf (*dll_scheme_console_printf_ptr)
434# define scheme_console_output (*dll_scheme_console_output_ptr)
435# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
436
437/* and functions in a usual way */
438# define GC_malloc dll_GC_malloc
439# define GC_malloc_atomic dll_GC_malloc_atomic
440
441# define scheme_add_global dll_scheme_add_global
442# define scheme_add_global_symbol dll_scheme_add_global_symbol
443# define scheme_apply dll_scheme_apply
444# define scheme_basic_env dll_scheme_basic_env
445# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000446# if MZSCHEME_VERSION_MAJOR >= 299
447# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100448# define scheme_make_path dll_scheme_make_path
Bram Moolenaar555b2802005-05-19 21:08:39 +0000449# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000450# define scheme_check_threads dll_scheme_check_threads
451# define scheme_close_input_port dll_scheme_close_input_port
452# define scheme_count_lines dll_scheme_count_lines
453# define scheme_current_continuation_marks \
454 dll_scheme_current_continuation_marks
455# define scheme_display dll_scheme_display
456# define scheme_display_to_string dll_scheme_display_to_string
457# define scheme_do_eval dll_scheme_do_eval
458# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000459# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000460# define scheme_eval dll_scheme_eval
461# define scheme_eval_string dll_scheme_eval_string
462# define scheme_eval_string_all dll_scheme_eval_string_all
463# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000464# if MZSCHEME_VERSION_MAJOR < 299
465# define scheme_format dll_scheme_format
466# else
467# define scheme_format_utf8 dll_scheme_format_utf8
468# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000470# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100471# define scheme_get_sized_byte_string_output dll_scheme_get_sized_string_output
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000472# else
473# define scheme_get_sized_byte_string_output \
474 dll_scheme_get_sized_byte_string_output
Bram Moolenaar75676462013-01-30 14:55:42 +0100475# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000476# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477# define scheme_intern_symbol dll_scheme_intern_symbol
478# define scheme_lookup_global dll_scheme_lookup_global
479# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
480# define scheme_make_integer_value dll_scheme_make_integer_value
Bram Moolenaar33570922005-01-25 22:26:29 +0000481# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000482# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000483# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100484# define scheme_make_byte_string dll_scheme_make_string
485# define scheme_make_byte_string_output_port dll_scheme_make_string_output_port
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000486# else
487# define scheme_make_byte_string dll_scheme_make_byte_string
488# define scheme_make_byte_string_output_port \
489 dll_scheme_make_byte_string_output_port
490# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000491# define scheme_make_struct_instance dll_scheme_make_struct_instance
492# define scheme_make_struct_names dll_scheme_make_struct_names
493# define scheme_make_struct_type dll_scheme_make_struct_type
494# define scheme_make_struct_values dll_scheme_make_struct_values
495# define scheme_make_type dll_scheme_make_type
496# define scheme_make_vector dll_scheme_make_vector
497# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
498# define scheme_open_input_file dll_scheme_open_input_file
499# define scheme_primitive_module dll_scheme_primitive_module
500# define scheme_proper_list_length dll_scheme_proper_list_length
501# define scheme_raise dll_scheme_raise
502# define scheme_read dll_scheme_read
503# define scheme_register_static dll_scheme_register_static
504# define scheme_set_stack_base dll_scheme_set_stack_base
505# define scheme_signal_error dll_scheme_signal_error
506# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000507# if MZSCHEME_VERSION_MAJOR >= 299
508# define scheme_set_param dll_scheme_set_param
509# define scheme_current_config dll_scheme_current_config
510# define scheme_char_string_to_byte_string \
511 dll_scheme_char_string_to_byte_string
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000512# define scheme_char_string_to_path \
513 dll_scheme_char_string_to_path
Bram Moolenaar75676462013-01-30 14:55:42 +0100514# define scheme_set_collects_path dll_scheme_set_collects_path
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000515# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000516# define scheme_make_hash_table dll_scheme_make_hash_table
517# define scheme_hash_set dll_scheme_hash_set
518# define scheme_hash_get dll_scheme_hash_get
519# define scheme_make_double dll_scheme_make_double
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100520# define scheme_make_sized_byte_string dll_scheme_make_sized_byte_string
521# define scheme_namespace_require dll_scheme_namespace_require
522# define scheme_dynamic_wind dll_scheme_dynamic_wind
523# ifdef MZ_PRECISE_GC
524# define GC_malloc_one_tagged dll_GC_malloc_one_tagged
525# define GC_register_traversers dll_GC_register_traversers
526# endif
527# if MZSCHEME_VERSION_MAJOR >= 400
528# ifdef TRAMPOLINED_MZVIM_STARTUP
529# define scheme_main_setup dll_scheme_main_setup
530# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
531# define scheme_register_tls_space dll_scheme_register_tls_space
532# endif
533# endif
534# define scheme_init_collection_paths dll_scheme_init_collection_paths
535# define scheme_malloc_immobile_box dll_scheme_malloc_immobile_box
536# define scheme_free_immobile_box dll_scheme_free_immobile_box
537# endif
538# if MZSCHEME_VERSION_MAJOR >= 600
539# define scheme_embedded_load dll_scheme_embedded_load
540# define scheme_register_embedded_load dll_scheme_register_embedded_load
541# define scheme_set_config_path dll_scheme_set_config_path
542# endif
543
544# if MZSCHEME_VERSION_MAJOR >= 500
545# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200546/* define as function for macro in schthread.h */
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100547Thread_Local_Variables *
548scheme_external_get_thread_local_variables(void)
549{
550 return dll_scheme_external_get_thread_local_variables();
551}
552# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000553# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000554
Bram Moolenaar4349c572016-01-30 13:28:28 +0100555#endif
556
Bram Moolenaar33570922005-01-25 22:26:29 +0000557typedef struct
558{
559 char *name;
560 void **ptr;
561} Thunk_Info;
562
563static Thunk_Info mzgc_imports[] = {
564 {"GC_malloc", (void **)&dll_GC_malloc},
565 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
566 {NULL, NULL}};
567
568static Thunk_Info mzsch_imports[] = {
569 {"scheme_eof", (void **)&dll_scheme_eof},
570 {"scheme_false", (void **)&dll_scheme_false},
571 {"scheme_void", (void **)&dll_scheme_void},
572 {"scheme_null", (void **)&dll_scheme_null},
573 {"scheme_true", (void **)&dll_scheme_true},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100574#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
Bram Moolenaar33570922005-01-25 22:26:29 +0000575 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100576#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000577 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
578 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000579 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000580 (void **)&dll_scheme_notify_multithread_ptr},
581 {"scheme_add_global", (void **)&dll_scheme_add_global},
582 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
583 {"scheme_apply", (void **)&dll_scheme_apply},
584 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000585# if MZSCHEME_VERSION_MAJOR >= 299
586 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100587 {"scheme_make_path", (void **)&dll_scheme_make_path},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000588# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000589 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
590 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
591 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
592 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000593 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000594 (void **)&dll_scheme_current_continuation_marks},
595 {"scheme_display", (void **)&dll_scheme_display},
596 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
597 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
598 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000599 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000600 {"scheme_eval", (void **)&dll_scheme_eval},
601 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
602 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000604 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000605# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000606 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000607# else
608 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000609 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000610#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000611 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000613 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000614 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000615# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000617 (void **)&dll_scheme_get_sized_byte_string_output},
618#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000619 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
620 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000621 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000622 (void **)&dll_scheme_make_closed_prim_w_arity},
623 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
Bram Moolenaar33570922005-01-25 22:26:29 +0000624 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000625 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000626# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000627 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000628 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000629 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000630# else
631 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000632 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000633 (void **)&dll_scheme_make_byte_string_output_port},
634# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000636 (void **)&dll_scheme_make_struct_instance},
637 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
638 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
639 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
640 {"scheme_make_type", (void **)&dll_scheme_make_type},
641 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
642 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
643 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
644 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
645 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
646 {"scheme_raise", (void **)&dll_scheme_raise},
647 {"scheme_read", (void **)&dll_scheme_read},
648 {"scheme_register_static", (void **)&dll_scheme_register_static},
649 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
650 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
651 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000652# if MZSCHEME_VERSION_MAJOR >= 299
653 {"scheme_set_param", (void **)&dll_scheme_set_param},
654 {"scheme_current_config", (void **)&dll_scheme_current_config},
655 {"scheme_char_string_to_byte_string",
656 (void **)&dll_scheme_char_string_to_byte_string},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000657 {"scheme_char_string_to_path", (void **)&dll_scheme_char_string_to_path},
Bram Moolenaar75676462013-01-30 14:55:42 +0100658 {"scheme_set_collects_path", (void **)&dll_scheme_set_collects_path},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000659# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000660 {"scheme_make_hash_table", (void **)&dll_scheme_make_hash_table},
661 {"scheme_hash_set", (void **)&dll_scheme_hash_set},
662 {"scheme_hash_get", (void **)&dll_scheme_hash_get},
663 {"scheme_make_double", (void **)&dll_scheme_make_double},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000664 {"scheme_make_sized_byte_string", (void **)&dll_scheme_make_sized_byte_string},
665 {"scheme_namespace_require", (void **)&dll_scheme_namespace_require},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100666 {"scheme_dynamic_wind", (void **)&dll_scheme_dynamic_wind},
667# ifdef MZ_PRECISE_GC
668 {"GC_malloc_one_tagged", (void **)&dll_GC_malloc_one_tagged},
669 {"GC_register_traversers", (void **)&dll_GC_register_traversers},
670# endif
671# if MZSCHEME_VERSION_MAJOR >= 400
672# ifdef TRAMPOLINED_MZVIM_STARTUP
673 {"scheme_main_setup", (void **)&dll_scheme_main_setup},
674# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
675 {"scheme_register_tls_space", (void **)&dll_scheme_register_tls_space},
676# endif
677# endif
678 {"scheme_init_collection_paths", (void **)&dll_scheme_init_collection_paths},
679 {"scheme_malloc_immobile_box", (void **)&dll_scheme_malloc_immobile_box},
680 {"scheme_free_immobile_box", (void **)&dll_scheme_free_immobile_box},
681# endif
682# if MZSCHEME_VERSION_MAJOR >= 500
683# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
684 {"scheme_external_get_thread_local_variables", (void **)&dll_scheme_external_get_thread_local_variables},
685# endif
686# endif
687# if MZSCHEME_VERSION_MAJOR >= 600
688 {"scheme_embedded_load", (void **)&dll_scheme_embedded_load},
689 {"scheme_register_embedded_load", (void **)&dll_scheme_register_embedded_load},
690 {"scheme_set_config_path", (void **)&dll_scheme_set_config_path},
691# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000692 {NULL, NULL}};
693
694static HINSTANCE hMzGC = 0;
695static HINSTANCE hMzSch = 0;
696
697static void dynamic_mzscheme_end(void);
698static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
699 int verbose);
700
701 static int
702mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
703{
704 Thunk_Info *thunk = NULL;
705
706 if (hMzGC && hMzSch)
707 return OK;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200708 hMzSch = vimLoadLib(sch_dll);
709 hMzGC = vimLoadLib(gc_dll);
Bram Moolenaar33570922005-01-25 22:26:29 +0000710
Bram Moolenaar33570922005-01-25 22:26:29 +0000711 if (!hMzGC)
712 {
713 if (verbose)
714 EMSG2(_(e_loadlib), gc_dll);
715 return FAIL;
716 }
717
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100718 if (!hMzSch)
719 {
720 if (verbose)
721 EMSG2(_(e_loadlib), sch_dll);
722 return FAIL;
723 }
724
Bram Moolenaar33570922005-01-25 22:26:29 +0000725 for (thunk = mzsch_imports; thunk->name; thunk++)
726 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000727 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
729 {
730 FreeLibrary(hMzSch);
731 hMzSch = 0;
732 FreeLibrary(hMzGC);
733 hMzGC = 0;
734 if (verbose)
735 EMSG2(_(e_loadfunc), thunk->name);
736 return FAIL;
737 }
738 }
739 for (thunk = mzgc_imports; thunk->name; thunk++)
740 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000741 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000742 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
743 {
744 FreeLibrary(hMzSch);
745 hMzSch = 0;
746 FreeLibrary(hMzGC);
747 hMzGC = 0;
748 if (verbose)
749 EMSG2(_(e_loadfunc), thunk->name);
750 return FAIL;
751 }
752 }
753 return OK;
754}
755
756 int
757mzscheme_enabled(int verbose)
758{
759 return mzscheme_runtime_link_init(
760 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
761}
762
763 static void
764dynamic_mzscheme_end(void)
765{
766 if (hMzSch)
767 {
768 FreeLibrary(hMzSch);
769 hMzSch = 0;
770 }
771 if (hMzGC)
772 {
773 FreeLibrary(hMzGC);
774 hMzGC = 0;
775 }
776}
777#endif /* DYNAMIC_MZSCHEME */
778
Bram Moolenaar75676462013-01-30 14:55:42 +0100779#if MZSCHEME_VERSION_MAJOR < 299
780# define GUARANTEED_STRING_ARG(proc, num) GUARANTEE_STRING(proc, num)
781#else
782 static Scheme_Object *
783guaranteed_byte_string_arg(char *proc, int num, int argc, Scheme_Object **argv)
784{
785 if (SCHEME_BYTE_STRINGP(argv[num]))
786 {
787 return argv[num];
788 }
789 else if (SCHEME_CHAR_STRINGP(argv[num]))
790 {
791 Scheme_Object *tmp = NULL;
792 MZ_GC_DECL_REG(2);
793 MZ_GC_VAR_IN_REG(0, argv[num]);
794 MZ_GC_VAR_IN_REG(1, tmp);
795 MZ_GC_REG();
796 tmp = scheme_char_string_to_byte_string(argv[num]);
797 MZ_GC_UNREG();
798 return tmp;
799 }
800 else
801 scheme_wrong_type(proc, "string", num, argc, argv);
802 /* unreachable */
803 return scheme_void;
804}
805# define GUARANTEED_STRING_ARG(proc, num) guaranteed_byte_string_arg(proc, num, argc, argv)
806#endif
807
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000808/* need to put it here for dynamic stuff to work */
Bram Moolenaare484c942009-09-11 10:21:41 +0000809#if defined(INCLUDE_MZSCHEME_BASE)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000810# include "mzscheme_base.c"
811#endif
812
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000813/*
814 *========================================================================
815 * 1. MzScheme interpreter startup
816 *========================================================================
817 */
818
819static Scheme_Type mz_buffer_type;
820static Scheme_Type mz_window_type;
821
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000822static int initialized = FALSE;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100823#ifdef DYNAMIC_MZSCHEME
824static int disabled = FALSE;
825#endif
826static int load_base_module_failed = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000827
828/* global environment */
829static Scheme_Env *environment = NULL;
830/* output/error handlers */
831static Scheme_Object *curout = NULL;
832static Scheme_Object *curerr = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000833/* exn:vim exception */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000834static Scheme_Object *exn_catching_apply = NULL;
835static Scheme_Object *exn_p = NULL;
836static Scheme_Object *exn_message = NULL;
837static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000838
839#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
840static void *stack_base = NULL;
841#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000842
843static long range_start;
844static long range_end;
845
846/* MzScheme threads scheduling stuff */
847static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000848
849#if defined(FEAT_GUI_W32)
Bram Moolenaardec6c7b2016-06-02 11:57:38 +0200850static void CALLBACK timer_proc(HWND, UINT, UINT_PTR, DWORD);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000851static UINT timer_id = 0;
852#elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100853# if GTK_CHECK_VERSION(3,0,0)
854static gboolean timer_proc(gpointer);
855# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000856static gint timer_proc(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100857# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000858static guint timer_id = 0;
859#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
860static void timer_proc(XtPointer, XtIntervalId *);
861static XtIntervalId timer_id = (XtIntervalId)0;
862#elif defined(FEAT_GUI_MAC)
863pascal void timer_proc(EventLoopTimerRef, void *);
864static EventLoopTimerRef timer_id = NULL;
865static EventLoopTimerUPP timerUPP;
866#endif
867
868#ifndef FEAT_GUI_W32 /* Win32 console and Unix */
869 void
870mzvim_check_threads(void)
871{
872 /* Last time MzScheme threads were scheduled */
873 static time_t mz_last_time = 0;
874
875 if (mz_threads_allow && p_mzq > 0)
876 {
877 time_t now = time(NULL);
878
879 if ((now - mz_last_time) * 1000 > p_mzq)
880 {
881 mz_last_time = now;
882 scheme_check_threads();
883 }
884 }
885}
886#endif
887
Bram Moolenaar4349c572016-01-30 13:28:28 +0100888#if defined(MZSCHEME_GUI_THREADS) || defined(PROTO)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000889static void setup_timer(void);
890static void remove_timer(void);
891
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000892/* timers are presented in GUI only */
893# if defined(FEAT_GUI_W32)
894 static void CALLBACK
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200895timer_proc(HWND hwnd UNUSED, UINT uMsg UNUSED, UINT_PTR idEvent UNUSED, DWORD dwTime UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000896# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100897# if GTK_CHECK_VERSION(3,0,0)
898 static gboolean
899# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000900 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100901# endif
Bram Moolenaar64404472010-06-26 06:24:45 +0200902timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000903# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000904 static void
Bram Moolenaar64404472010-06-26 06:24:45 +0200905timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000906# elif defined(FEAT_GUI_MAC)
907 pascal void
Bram Moolenaar64404472010-06-26 06:24:45 +0200908timer_proc(EventLoopTimerRef theTimer UNUSED, void *userData UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000909# endif
910{
911 scheme_check_threads();
912# if defined(FEAT_GUI_GTK)
913 return TRUE; /* continue receiving notifications */
914# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
915 /* renew timeout */
916 if (mz_threads_allow && p_mzq > 0)
917 timer_id = XtAppAddTimeOut(app_context, p_mzq,
918 timer_proc, NULL);
919# endif
920}
921
922 static void
923setup_timer(void)
924{
925# if defined(FEAT_GUI_W32)
926 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
927# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100928# if GTK_CHECK_VERSION(3,0,0)
929 timer_id = g_timeout_add((guint)p_mzq, (GSourceFunc)timer_proc, NULL);
930# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000931 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100932# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000933# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
934 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
935# elif defined(FEAT_GUI_MAC)
936 timerUPP = NewEventLoopTimerUPP(timer_proc);
937 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
938 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
939# endif
940}
941
942 static void
943remove_timer(void)
944{
945# if defined(FEAT_GUI_W32)
946 KillTimer(NULL, timer_id);
947# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100948# if GTK_CHECK_VERSION(3,0,0)
949 g_source_remove(timer_id);
950# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000951 gtk_timeout_remove(timer_id);
Bram Moolenaar98921892016-02-23 17:14:37 +0100952# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000953# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
954 XtRemoveTimeOut(timer_id);
955# elif defined(FEAT_GUI_MAC)
956 RemoveEventLoopTimer(timer_id);
957 DisposeEventLoopTimerUPP(timerUPP);
958# endif
959 timer_id = 0;
960}
961
962 void
963mzvim_reset_timer(void)
964{
965 if (timer_id != 0)
966 remove_timer();
967 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
968 setup_timer();
969}
970
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000971#endif /* MZSCHEME_GUI_THREADS */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000972
973 static void
974notify_multithread(int on)
975{
976 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000977#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000978 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
979 setup_timer();
980 if (!on && timer_id != 0)
981 remove_timer();
982#endif
983}
984
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000985 void
986mzscheme_end(void)
987{
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100988 /* We can not unload the DLL. Racket's thread might be still alive. */
989#if 0
Bram Moolenaar33570922005-01-25 22:26:29 +0000990#ifdef DYNAMIC_MZSCHEME
991 dynamic_mzscheme_end();
992#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100993#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000994}
995
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100996#if HAVE_TLS_SPACE
997# if defined(_MSC_VER)
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100998static __declspec(thread) void *tls_space;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100999extern intptr_t _tls_index;
1000# elif defined(__MINGW32__)
1001static __thread void *tls_space;
1002extern intptr_t _tls_index;
1003# else
1004static THREAD_LOCAL void *tls_space;
1005static intptr_t _tls_index = 0;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001006# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001007#endif
1008
Bram Moolenaar54b63522016-08-26 12:55:09 +02001009/*
1010 * mzscheme_main() is called early in main().
1011 * We may call scheme_main_setup() which calls mzscheme_env_main() which then
1012 * trampolines into vim_main2(), which never returns.
1013 */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001014 int
Bram Moolenaar54b63522016-08-26 12:55:09 +02001015mzscheme_main(void)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001016{
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001017 int argc = 0;
1018 char *argv = NULL;
1019
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001020#ifdef DYNAMIC_MZSCHEME
1021 /*
1022 * Racket requires trampolined startup. We can not load it later.
1023 * If dynamic dll loading is failed, disable it.
1024 */
1025 if (!mzscheme_enabled(FALSE))
1026 {
1027 disabled = TRUE;
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001028 return vim_main2();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001029 }
1030#endif
Bram Moolenaar5c5c9802015-07-10 16:12:48 +02001031#ifdef HAVE_TLS_SPACE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001032 scheme_register_tls_space(&tls_space, _tls_index);
Bram Moolenaar2d0860d2010-11-03 21:59:30 +01001033#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001034#ifdef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001035 return scheme_main_setup(TRUE, mzscheme_env_main, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001036#else
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001037 return mzscheme_env_main(NULL, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001038#endif
1039}
1040
1041 static int
Bram Moolenaar54b63522016-08-26 12:55:09 +02001042mzscheme_env_main(Scheme_Env *env, int argc UNUSED, char **argv UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001043{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001044#ifdef TRAMPOLINED_MZVIM_STARTUP
1045 /* Scheme has created the environment for us */
1046 environment = env;
1047#else
1048# ifdef MZ_PRECISE_GC
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001049 Scheme_Object *dummy = NULL;
1050 MZ_GC_DECL_REG(1);
1051 MZ_GC_VAR_IN_REG(0, dummy);
1052
1053 stack_base = &__gc_var_stack__;
1054# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001055 int dummy = 0;
1056 stack_base = (void *)&dummy;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001057# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001058#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001059
Bram Moolenaar54b63522016-08-26 12:55:09 +02001060 vim_main2();
1061 /* not reached, vim_main2() will loop until exit() */
1062
1063 return 0;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001064}
1065
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001066 static Scheme_Object*
1067load_base_module(void *data)
1068{
1069 scheme_namespace_require(scheme_intern_symbol((char *)data));
1070 return scheme_null;
1071}
1072
1073 static Scheme_Object *
Bram Moolenaar8b29aba2016-03-28 22:17:16 +02001074load_base_module_on_error(void *data UNUSED)
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001075{
1076 load_base_module_failed = TRUE;
1077 return scheme_null;
1078}
1079
1080 static int
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001081startup_mzscheme(void)
1082{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001083#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001084 scheme_set_stack_base(stack_base, 1);
1085#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001086
Bram Moolenaar75676462013-01-30 14:55:42 +01001087#ifndef TRAMPOLINED_MZVIM_STARTUP
1088 /* in newer versions of precise GC the initial env has been created */
1089 environment = scheme_basic_env();
1090#endif
1091
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001092 MZ_REGISTER_STATIC(environment);
1093 MZ_REGISTER_STATIC(curout);
1094 MZ_REGISTER_STATIC(curerr);
1095 MZ_REGISTER_STATIC(exn_catching_apply);
1096 MZ_REGISTER_STATIC(exn_p);
1097 MZ_REGISTER_STATIC(exn_message);
1098 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001099
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001100 MZ_GC_CHECK();
1101
1102#ifdef INCLUDE_MZSCHEME_BASE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001103 /* invoke function from generated and included mzscheme_base.c */
1104 declare_modules(environment);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001105#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001106
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001107 /* setup 'current-library-collection-paths' parameter */
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001108# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001109 {
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001110 Scheme_Object *coll_path = NULL;
1111 int mustfree = FALSE;
1112 char_u *s;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001113
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001114 MZ_GC_DECL_REG(1);
1115 MZ_GC_VAR_IN_REG(0, coll_path);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001116 MZ_GC_REG();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001117 /* workaround for dynamic loading on windows */
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001118 s = vim_getenv((char_u *)"PLTCOLLECTS", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001119 if (s != NULL)
1120 {
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001121 coll_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001122 MZ_GC_CHECK();
1123 if (mustfree)
1124 vim_free(s);
1125 }
1126# ifdef MZSCHEME_COLLECTS
1127 if (coll_path == NULL)
1128 {
1129 coll_path = scheme_make_path(MZSCHEME_COLLECTS);
1130 MZ_GC_CHECK();
1131 }
Bram Moolenaar39d7d512013-01-31 21:09:15 +01001132# endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001133 if (coll_path != NULL)
1134 {
1135 scheme_set_collects_path(coll_path);
1136 MZ_GC_CHECK();
1137 }
1138 MZ_GC_UNREG();
1139 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001140# else
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001141# ifdef MZSCHEME_COLLECTS
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001142 {
1143 Scheme_Object *coll_string = NULL;
1144 Scheme_Object *coll_pair = NULL;
1145 Scheme_Config *config = NULL;
1146
1147 MZ_GC_DECL_REG(3);
1148 MZ_GC_VAR_IN_REG(0, coll_string);
1149 MZ_GC_VAR_IN_REG(1, coll_pair);
1150 MZ_GC_VAR_IN_REG(2, config);
1151 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001152 coll_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001153 MZ_GC_CHECK();
1154 coll_pair = scheme_make_pair(coll_string, scheme_null);
1155 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001156 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001157 MZ_GC_CHECK();
1158 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
1159 MZ_GC_CHECK();
1160 MZ_GC_UNREG();
1161 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001162# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001163#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001164
1165# if MZSCHEME_VERSION_MAJOR >= 600
1166 {
1167 Scheme_Object *config_path = NULL;
1168 int mustfree = FALSE;
1169 char_u *s;
1170
1171 MZ_GC_DECL_REG(1);
1172 MZ_GC_VAR_IN_REG(0, config_path);
1173 MZ_GC_REG();
1174 /* workaround for dynamic loading on windows */
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001175 s = vim_getenv((char_u *)"PLTCONFIGDIR", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001176 if (s != NULL)
1177 {
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001178 config_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001179 MZ_GC_CHECK();
1180 if (mustfree)
1181 vim_free(s);
1182 }
1183#ifdef MZSCHEME_CONFIGDIR
1184 if (config_path == NULL)
1185 {
1186 config_path = scheme_make_path(MZSCHEME_CONFIGDIR);
1187 MZ_GC_CHECK();
1188 }
1189#endif
1190 if (config_path != NULL)
1191 {
1192 scheme_set_config_path(config_path);
1193 MZ_GC_CHECK();
1194 }
1195 MZ_GC_UNREG();
1196 }
1197# endif
1198
1199#if MZSCHEME_VERSION_MAJOR >= 400
1200 scheme_init_collection_paths(environment, scheme_null);
1201#endif
1202
1203 /*
1204 * versions 4.x do not provide Scheme bindings by default
1205 * we need to add them explicitly
1206 */
1207 {
1208 /* use error handler to avoid abort */
1209 scheme_dynamic_wind(NULL, load_base_module, NULL,
1210 load_base_module_on_error, "racket/base");
1211 if (load_base_module_failed)
1212 {
1213 load_base_module_failed = FALSE;
1214 scheme_dynamic_wind(NULL, load_base_module, NULL,
1215 load_base_module_on_error, "scheme/base");
1216 if (load_base_module_failed)
1217 return -1;
1218 }
1219 }
1220
1221 register_vim_exn();
1222 /* use new environment to initialise exception handling */
1223 init_exn_catching_apply();
1224
1225 /* redirect output */
1226 scheme_console_output = do_output;
1227 scheme_console_printf = do_printf;
1228
Bram Moolenaar555b2802005-05-19 21:08:39 +00001229#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +00001230 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001231 Scheme_Object *make_security_guard = NULL;
1232 MZ_GC_DECL_REG(1);
1233 MZ_GC_VAR_IN_REG(0, make_security_guard);
1234 MZ_GC_REG();
1235
1236#if MZSCHEME_VERSION_MAJOR < 400
1237 {
1238 Scheme_Object *make_security_guard_symbol = NULL;
1239 MZ_GC_DECL_REG(1);
1240 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
1241 MZ_GC_REG();
1242 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
1243 MZ_GC_CHECK();
1244 make_security_guard = scheme_lookup_global(
1245 make_security_guard_symbol, environment);
1246 MZ_GC_UNREG();
1247 }
1248#else
1249 make_security_guard = scheme_builtin_value("make-security-guard");
1250 MZ_GC_CHECK();
1251#endif
1252
1253 /* setup sandbox guards */
1254 if (make_security_guard != NULL)
1255 {
1256 Scheme_Object *args[3] = {NULL, NULL, NULL};
1257 Scheme_Object *guard = NULL;
1258 Scheme_Config *config = NULL;
1259 MZ_GC_DECL_REG(5);
1260 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
1261 MZ_GC_VAR_IN_REG(3, guard);
1262 MZ_GC_VAR_IN_REG(4, config);
1263 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001264 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001265 MZ_GC_CHECK();
1266 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
1267 MZ_GC_CHECK();
1268 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
1269 "sandbox-file-guard", 3, 3);
1270 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1271 "sandbox-network-guard", 4, 4);
1272 guard = scheme_apply(make_security_guard, 3, args);
1273 MZ_GC_CHECK();
1274 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1275 MZ_GC_CHECK();
1276 MZ_GC_UNREG();
1277 }
1278 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001279 }
1280#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001281 /* Create buffer and window types for use in Scheme code */
1282 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001283 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001284 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001285 MZ_GC_CHECK();
1286#ifdef MZ_PRECISE_GC
1287 GC_register_traversers(mz_buffer_type,
1288 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1289 TRUE, TRUE);
1290 GC_register_traversers(mz_window_type,
1291 window_size_proc, window_mark_proc, window_fixup_proc,
1292 TRUE, TRUE);
1293#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001294
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001295 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001296
1297 /*
1298 * setup callback to receive notifications
1299 * whether thread scheduling is (or not) required
1300 */
1301 scheme_notify_multithread = notify_multithread;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001302
1303 return 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001304}
1305
1306/*
1307 * This routine is called for each new invocation of MzScheme
1308 * to make sure things are properly initialized.
1309 */
1310 static int
1311mzscheme_init(void)
1312{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001313 if (!initialized)
1314 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001315#ifdef DYNAMIC_MZSCHEME
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001316 if (disabled || !mzscheme_enabled(TRUE))
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 {
Bram Moolenaarb849e712009-06-24 15:51:37 +00001318 EMSG(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
Bram Moolenaar33570922005-01-25 22:26:29 +00001319 return -1;
1320 }
1321#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001322 if (load_base_module_failed || startup_mzscheme())
1323 {
Bram Moolenaar9e3be262016-01-24 13:58:40 +01001324 EMSG(_("E895: Sorry, this command is disabled, the MzScheme's racket/base module could not be loaded."));
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001325 return -1;
1326 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001327 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001328 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001329 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001330 Scheme_Config *config = NULL;
1331 MZ_GC_DECL_REG(1);
1332 MZ_GC_VAR_IN_REG(0, config);
1333 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001334 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001335 MZ_GC_CHECK();
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001336 /* recreate ports each call effectively clearing these ones */
Bram Moolenaar75676462013-01-30 14:55:42 +01001337 curout = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001338 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001339 curerr = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001340 MZ_GC_CHECK();
1341 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1342 MZ_GC_CHECK();
1343 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1344 MZ_GC_CHECK();
1345 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001346 }
1347
1348 return 0;
1349}
1350
1351/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001352 *========================================================================
1353 * 2. External Interface
1354 *========================================================================
1355 */
1356
1357/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001358 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001359 */
1360 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001361eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001362{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001363 Scheme_Object *value = NULL;
1364 Scheme_Object *exn = NULL;
1365 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001366
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001367 MZ_GC_DECL_REG(3);
1368 MZ_GC_VAR_IN_REG(0, value);
1369 MZ_GC_VAR_IN_REG(1, exn);
1370 MZ_GC_VAR_IN_REG(2, prim);
1371 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001372
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001373 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1374 MZ_GC_CHECK();
1375 value = _apply_thunk_catch_exceptions(prim, &exn);
1376 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001377
1378 if (!value)
1379 {
1380 value = extract_exn_message(exn);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001381 /* Got an exn? */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001382 if (value)
1383 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001384 scheme_display(value, curerr); /* Send to stderr-vim */
1385 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001386 do_flush();
1387 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001388 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001389 /* `raise' was called on some arbitrary value */
1390 return FAIL;
1391 }
1392
1393 if (ret != NULL) /* if pointer to retval supported give it up */
1394 *ret = value;
1395 /* Print any result, as long as it's not a void */
1396 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001397 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001398 scheme_display(value, curout); /* Send to stdout-vim */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001399 MZ_GC_CHECK();
1400 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001401
1402 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001403 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001404 return OK;
1405}
1406
1407/* :mzscheme */
1408 static int
1409do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1410{
1411 if (mzscheme_init())
1412 return FAIL;
1413
1414 range_start = eap->line1;
1415 range_end = eap->line2;
1416
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001417 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001418}
1419
1420/*
1421 * Routine called by VIM when deleting a buffer
1422 */
1423 void
1424mzscheme_buffer_free(buf_T *buf)
1425{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001426 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001427 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001428 vim_mz_buffer *bp = NULL;
1429 MZ_GC_DECL_REG(1);
1430 MZ_GC_VAR_IN_REG(0, bp);
1431 MZ_GC_REG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001432
Bram Moolenaar75676462013-01-30 14:55:42 +01001433 bp = BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001434 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001435#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001436 scheme_gc_ptr_ok(bp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001437#else
1438 scheme_free_immobile_box(buf->b_mzscheme_ref);
1439#endif
1440 buf->b_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001441 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001442 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001443 }
1444}
1445
1446/*
1447 * Routine called by VIM when deleting a Window
1448 */
1449 void
1450mzscheme_window_free(win_T *win)
1451{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001452 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001453 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001454 vim_mz_window *wp = NULL;
1455 MZ_GC_DECL_REG(1);
1456 MZ_GC_VAR_IN_REG(0, wp);
1457 MZ_GC_REG();
1458 wp = WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001459 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001460#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001461 scheme_gc_ptr_ok(wp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001462#else
1463 scheme_free_immobile_box(win->w_mzscheme_ref);
1464#endif
1465 win->w_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001466 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001467 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001468 }
1469}
1470
1471/*
1472 * ":mzscheme" (or ":mz")
1473 */
1474 void
1475ex_mzscheme(exarg_T *eap)
1476{
1477 char_u *script;
1478
1479 script = script_get(eap, eap->arg);
1480 if (!eap->skip)
1481 {
1482 if (script == NULL)
1483 do_mzscheme_command(eap, eap->arg, do_eval);
1484 else
1485 {
1486 do_mzscheme_command(eap, script, do_eval);
1487 vim_free(script);
1488 }
1489 }
1490}
1491
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001492 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001493do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001494{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001495 Scheme_Object *expr = NULL;
1496 Scheme_Object *result = NULL;
1497 char *file = NULL;
1498 Port_Info *pinfo = (Port_Info *)data;
1499
1500 MZ_GC_DECL_REG(3);
1501 MZ_GC_VAR_IN_REG(0, expr);
1502 MZ_GC_VAR_IN_REG(1, result);
1503 MZ_GC_VAR_IN_REG(2, file);
1504 MZ_GC_REG();
1505
1506 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1507 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001508
1509 /* make Vim expansion */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001510 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001511 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001512 MZ_GC_CHECK();
1513 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1514 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001515
1516 /* Like REPL but print only last result */
1517 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001518 {
1519 result = scheme_eval(expr, environment);
1520 MZ_GC_CHECK();
1521 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001522
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001523 /* errors will be caught in do_mzscheme_command and ex_mzfile */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001524 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001525 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001526 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001527 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001528 return result;
1529}
1530
1531/* :mzfile */
1532 void
1533ex_mzfile(exarg_T *eap)
1534{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001535 Port_Info pinfo = {NULL, NULL};
1536
1537 MZ_GC_DECL_REG(1);
1538 MZ_GC_VAR_IN_REG(0, pinfo.port);
1539 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001540
1541 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001542 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1543 && pinfo.port != NULL) /* looks like port was not closed */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001544 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001545 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001546 MZ_GC_CHECK();
1547 }
1548 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001549}
1550
1551
1552/*
1553 *========================================================================
1554 * Exception handling code -- cribbed form the MzScheme sources and
1555 * Matthew Flatt's "Inside PLT MzScheme" document.
1556 *========================================================================
1557 */
1558 static void
1559init_exn_catching_apply(void)
1560{
1561 if (!exn_catching_apply)
1562 {
1563 char *e =
1564 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001565 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001566 "(cons #t (thunk))))";
1567
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001568 exn_catching_apply = scheme_eval_string(e, environment);
1569 MZ_GC_CHECK();
1570 exn_p = scheme_builtin_value("exn?");
1571 MZ_GC_CHECK();
1572 exn_message = scheme_builtin_value("exn-message");
1573 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001574 }
1575}
1576
1577/*
1578 * This function applies a thunk, returning the Scheme value if there's
1579 * no exception, otherwise returning NULL and setting *exn to the raised
1580 * value (usually an exn structure).
1581 */
1582 static Scheme_Object *
1583_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1584{
1585 Scheme_Object *v;
1586
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001587 v = _scheme_apply(exn_catching_apply, 1, &f);
1588 /* v is a pair: (cons #t value) or (cons #f exn) */
1589
1590 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1591 return SCHEME_CDR(v);
1592 else
1593 {
1594 *exn = SCHEME_CDR(v);
1595 return NULL;
1596 }
1597}
1598
1599 static Scheme_Object *
1600extract_exn_message(Scheme_Object *v)
1601{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001602 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1603 return _scheme_apply(exn_message, 1, &v);
1604 else
1605 return NULL; /* Not an exn structure */
1606}
1607
1608 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001609do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001610{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001611 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001612}
1613
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001614/*
1615 *========================================================================
1616 * 3. MzScheme I/O Handlers
1617 *========================================================================
1618 */
1619 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001620do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001621{
1622 char *p, *prev;
1623
1624 prev = mesg;
1625 p = strchr(prev, '\n');
1626 while (p)
1627 {
1628 *p = '\0';
1629 if (error)
1630 EMSG(prev);
1631 else
1632 MSG(prev);
1633 prev = p + 1;
1634 p = strchr(prev, '\n');
1635 }
1636
1637 if (error)
1638 EMSG(prev);
1639 else
1640 MSG(prev);
1641}
1642
1643 static void
Bram Moolenaar75676462013-01-30 14:55:42 +01001644do_output(char *mesg, OUTPUT_LEN_TYPE len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001645{
Bram Moolenaar02e14d62012-11-28 15:37:51 +01001646 /* TODO: use len, the string may not be NUL terminated */
Bram Moolenaar64404472010-06-26 06:24:45 +02001647 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001648}
1649
1650 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001651do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001652{
Bram Moolenaar64404472010-06-26 06:24:45 +02001653 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001654}
1655
1656 static void
1657do_printf(char *format, ...)
1658{
Bram Moolenaar64404472010-06-26 06:24:45 +02001659 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001660}
1661
1662 static void
1663do_flush(void)
1664{
1665 char *buff;
Bram Moolenaar75676462013-01-30 14:55:42 +01001666 OUTPUT_LEN_TYPE length;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001667
Bram Moolenaar75676462013-01-30 14:55:42 +01001668 buff = scheme_get_sized_byte_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001669 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001670 if (length)
1671 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001672 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001673 return;
1674 }
1675
Bram Moolenaar75676462013-01-30 14:55:42 +01001676 buff = scheme_get_sized_byte_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001677 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001678 if (length)
1679 do_output(buff, length);
1680}
1681
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001682/*
1683 *========================================================================
1684 * 4. Implementation of the Vim Features for MzScheme
1685 *========================================================================
1686 */
1687
1688/* (command {command-string}) */
1689 static Scheme_Object *
1690vim_command(void *data, int argc, Scheme_Object **argv)
1691{
1692 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001693 Scheme_Object *cmd = NULL;
1694 MZ_GC_DECL_REG(1);
1695 MZ_GC_VAR_IN_REG(0, cmd);
1696 MZ_GC_REG();
1697 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001698
1699 /* may be use do_cmdline_cmd? */
Bram Moolenaar75676462013-01-30 14:55:42 +01001700 do_cmdline(BYTE_STRING_VALUE(cmd), NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001701 update_screen(VALID);
1702
Bram Moolenaar75676462013-01-30 14:55:42 +01001703 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001704 raise_if_error();
1705 return scheme_void;
1706}
1707
1708/* (eval {expr-string}) */
1709 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001710vim_eval(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001711{
1712#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001713 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001714 Scheme_Object *result = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001715 typval_T *vim_result;
Bram Moolenaar75676462013-01-30 14:55:42 +01001716 Scheme_Object *expr = NULL;
1717 MZ_GC_DECL_REG(2);
1718 MZ_GC_VAR_IN_REG(0, result);
1719 MZ_GC_VAR_IN_REG(1, expr);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001720 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001721 expr = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001722
Bram Moolenaar75676462013-01-30 14:55:42 +01001723 vim_result = eval_expr(BYTE_STRING_VALUE(expr), NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001724
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001725 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001726 raise_vim_exn(_("invalid expression"));
1727
Bram Moolenaar75676462013-01-30 14:55:42 +01001728 result = vim_to_mzscheme(vim_result);
1729 MZ_GC_CHECK();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001730 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001731
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001732 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001733 return result;
1734#else
1735 raise_vim_exn(_("expressions disabled at compile time"));
1736 /* unreachable */
1737 return scheme_false;
1738#endif
1739}
1740
1741/* (range-start) */
1742 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001743get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001744{
1745 return scheme_make_integer(range_start);
1746}
1747
1748/* (range-end) */
1749 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001750get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001751{
1752 return scheme_make_integer(range_end);
1753}
1754
1755/* (beep) */
1756 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001757mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001758{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001759 vim_beep(BO_LANG);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001760 return scheme_void;
1761}
1762
1763static Scheme_Object *M_global = NULL;
1764
1765/* (get-option {option-name}) [buffer/window] */
1766 static Scheme_Object *
1767get_option(void *data, int argc, Scheme_Object **argv)
1768{
1769 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001770 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001771 char *strval;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001772 int rc;
Bram Moolenaar75676462013-01-30 14:55:42 +01001773 Scheme_Object *rval = NULL;
1774 Scheme_Object *name = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001775 int opt_flags = 0;
1776 buf_T *save_curb = curbuf;
1777 win_T *save_curw = curwin;
1778
Bram Moolenaar75676462013-01-30 14:55:42 +01001779 MZ_GC_DECL_REG(2);
1780 MZ_GC_VAR_IN_REG(0, rval);
1781 MZ_GC_VAR_IN_REG(1, name);
1782 MZ_GC_REG();
1783
1784 name = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001785
1786 if (argc > 1)
1787 {
1788 if (M_global == NULL)
1789 {
1790 MZ_REGISTER_STATIC(M_global);
1791 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001792 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001793 }
1794
1795 if (argv[1] == M_global)
1796 opt_flags = OPT_GLOBAL;
1797 else if (SCHEME_VIMBUFFERP(argv[1]))
1798 {
1799 curbuf = get_valid_buffer(argv[1]);
1800 opt_flags = OPT_LOCAL;
1801 }
1802 else if (SCHEME_VIMWINDOWP(argv[1]))
1803 {
1804 win_T *win = get_valid_window(argv[1]);
1805
1806 curwin = win;
1807 curbuf = win->w_buffer;
1808 opt_flags = OPT_LOCAL;
1809 }
1810 else
1811 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1812 }
1813
Bram Moolenaar75676462013-01-30 14:55:42 +01001814 rc = get_option_value(BYTE_STRING_VALUE(name), &value, (char_u **)&strval, opt_flags);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001815 curbuf = save_curb;
1816 curwin = save_curw;
1817
1818 switch (rc)
1819 {
1820 case 1:
Bram Moolenaar75676462013-01-30 14:55:42 +01001821 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001822 return scheme_make_integer_value(value);
1823 case 0:
Bram Moolenaar75676462013-01-30 14:55:42 +01001824 rval = scheme_make_byte_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001825 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001826 vim_free(strval);
Bram Moolenaar75676462013-01-30 14:55:42 +01001827 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001828 return rval;
1829 case -1:
1830 case -2:
Bram Moolenaar75676462013-01-30 14:55:42 +01001831 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001832 raise_vim_exn(_("hidden option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001833 case -3:
Bram Moolenaar75676462013-01-30 14:55:42 +01001834 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001835 raise_vim_exn(_("unknown option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001836 }
1837 /* unreachable */
1838 return scheme_void;
1839}
1840
1841/* (set-option {option-changing-string} [buffer/window]) */
1842 static Scheme_Object *
1843set_option(void *data, int argc, Scheme_Object **argv)
1844{
Bram Moolenaar75676462013-01-30 14:55:42 +01001845 char_u *command = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001846 int opt_flags = 0;
1847 buf_T *save_curb = curbuf;
1848 win_T *save_curw = curwin;
1849 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001850 Scheme_Object *cmd = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001851
Bram Moolenaar75676462013-01-30 14:55:42 +01001852 MZ_GC_DECL_REG(1);
1853 MZ_GC_VAR_IN_REG(0, cmd);
1854 MZ_GC_REG();
1855 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
1856
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001857 if (argc > 1)
1858 {
1859 if (M_global == NULL)
1860 {
1861 MZ_REGISTER_STATIC(M_global);
1862 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001863 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001864 }
1865
1866 if (argv[1] == M_global)
1867 opt_flags = OPT_GLOBAL;
1868 else if (SCHEME_VIMBUFFERP(argv[1]))
1869 {
1870 curbuf = get_valid_buffer(argv[1]);
1871 opt_flags = OPT_LOCAL;
1872 }
1873 else if (SCHEME_VIMWINDOWP(argv[1]))
1874 {
1875 win_T *win = get_valid_window(argv[1]);
1876 curwin = win;
1877 curbuf = win->w_buffer;
1878 opt_flags = OPT_LOCAL;
1879 }
1880 else
1881 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1882 }
1883
1884 /* do_set can modify cmd, make copy */
Bram Moolenaar75676462013-01-30 14:55:42 +01001885 command = vim_strsave(BYTE_STRING_VALUE(cmd));
1886 MZ_GC_UNREG();
1887 do_set(command, opt_flags);
1888 vim_free(command);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001889 update_screen(NOT_VALID);
1890 curbuf = save_curb;
1891 curwin = save_curw;
1892 raise_if_error();
1893 return scheme_void;
1894}
1895
1896/*
1897 *===========================================================================
1898 * 5. Vim Window-related Manipulation Functions
1899 *===========================================================================
1900 */
1901
1902/* (curr-win) */
1903 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001904get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001905{
1906 return (Scheme_Object *)get_vim_curr_window();
1907}
1908
1909/* (win-count) */
1910 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001911get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001912{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001913 int n = 0;
Bram Moolenaard2142212013-01-30 17:41:50 +01001914 win_T *w;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001915
Bram Moolenaar29323592016-07-24 22:04:11 +02001916 FOR_ALL_WINDOWS(w)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001917 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001918 return scheme_make_integer(n);
1919}
1920
1921/* (get-win-list [buffer]) */
1922 static Scheme_Object *
1923get_window_list(void *data, int argc, Scheme_Object **argv)
1924{
1925 Vim_Prim *prim = (Vim_Prim *)data;
1926 vim_mz_buffer *buf;
1927 Scheme_Object *list;
Bram Moolenaard2142212013-01-30 17:41:50 +01001928 win_T *w = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001929
1930 buf = get_buffer_arg(prim->name, 0, argc, argv);
1931 list = scheme_null;
1932
Bram Moolenaard2142212013-01-30 17:41:50 +01001933 for ( ; w != NULL; w = w->w_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001934 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001935 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001936 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001937 MZ_GC_CHECK();
1938 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001939
1940 return list;
1941}
1942
1943 static Scheme_Object *
1944window_new(win_T *win)
1945{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001946 vim_mz_window *self = NULL;
1947
1948 MZ_GC_DECL_REG(1);
1949 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001950
1951 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001952 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001953 * then we can get at it in win_free() in vim.
1954 *
1955 * On a win_free() we set the Scheme object's win_T *field
1956 * to an invalid value. We trap all uses of a window
1957 * object, and reject them if the win_T *field is invalid.
1958 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001959 if (win->w_mzscheme_ref != NULL)
Bram Moolenaar75676462013-01-30 14:55:42 +01001960 return (Scheme_Object *)WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001961
Bram Moolenaar75676462013-01-30 14:55:42 +01001962 MZ_GC_REG();
1963 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_window));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001964 vim_memset(self, 0, sizeof(vim_mz_window));
Bram Moolenaar75676462013-01-30 14:55:42 +01001965#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001966 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaar75676462013-01-30 14:55:42 +01001967#else
1968 win->w_mzscheme_ref = scheme_malloc_immobile_box(NULL);
1969#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001970 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001971 WINDOW_REF(win) = self;
1972 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001973 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001974 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001975
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001976 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001977 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001978}
1979
1980/* (get-win-num [window]) */
1981 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001982get_window_num(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001983{
Bram Moolenaard2142212013-01-30 17:41:50 +01001984 int nr = 1;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001985 Vim_Prim *prim = (Vim_Prim *)data;
1986 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001987 win_T *wp;
1988
1989 for (wp = firstwin; wp != win; wp = wp->w_next)
1990 ++nr;
1991
1992 return scheme_make_integer(nr);
1993}
1994
1995/* (get-win-by-num {windownum}) */
1996 static Scheme_Object *
1997get_window_by_num(void *data, int argc, Scheme_Object **argv)
1998{
1999 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaard2142212013-01-30 17:41:50 +01002000 win_T *win = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002001 int fnum;
2002
2003 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2004 if (fnum < 1)
2005 scheme_signal_error(_("window index is out of range"));
2006
Bram Moolenaard2142212013-01-30 17:41:50 +01002007 for ( ; win != NULL; win = win->w_next, --fnum)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002008 if (fnum == 1) /* to be 1-based */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002009 return window_new(win);
2010
2011 return scheme_false;
2012}
2013
2014/* (get-win-buffer [window]) */
2015 static Scheme_Object *
2016get_window_buffer(void *data, int argc, Scheme_Object **argv)
2017{
2018 Vim_Prim *prim = (Vim_Prim *)data;
2019 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2020
2021 return buffer_new(win->win->w_buffer);
2022}
2023
2024/* (get-win-height [window]) */
2025 static Scheme_Object *
2026get_window_height(void *data, int argc, Scheme_Object **argv)
2027{
2028 Vim_Prim *prim = (Vim_Prim *)data;
2029 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2030
2031 return scheme_make_integer(win->win->w_height);
2032}
2033
2034/* (set-win-height {height} [window]) */
2035 static Scheme_Object *
2036set_window_height(void *data, int argc, Scheme_Object **argv)
2037{
2038 Vim_Prim *prim = (Vim_Prim *)data;
2039 vim_mz_window *win;
2040 win_T *savewin;
2041 int height;
2042
2043 win = get_window_arg(prim->name, 1, argc, argv);
2044 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2045
2046#ifdef FEAT_GUI
2047 need_mouse_correct = TRUE;
2048#endif
2049
2050 savewin = curwin;
2051 curwin = win->win;
2052 win_setheight(height);
2053 curwin = savewin;
2054
2055 raise_if_error();
2056 return scheme_void;
2057}
2058
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002059/* (get-win-width [window]) */
2060 static Scheme_Object *
2061get_window_width(void *data, int argc, Scheme_Object **argv)
2062{
2063 Vim_Prim *prim = (Vim_Prim *)data;
2064 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2065
2066 return scheme_make_integer(W_WIDTH(win->win));
2067}
2068
2069/* (set-win-width {width} [window]) */
2070 static Scheme_Object *
2071set_window_width(void *data, int argc, Scheme_Object **argv)
2072{
2073 Vim_Prim *prim = (Vim_Prim *)data;
2074 vim_mz_window *win;
2075 win_T *savewin;
2076 int width = 0;
2077
2078 win = get_window_arg(prim->name, 1, argc, argv);
2079 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2080
2081# ifdef FEAT_GUI
2082 need_mouse_correct = TRUE;
2083# endif
2084
2085 savewin = curwin;
2086 curwin = win->win;
2087 win_setwidth(width);
2088 curwin = savewin;
2089
2090 raise_if_error();
2091 return scheme_void;
2092}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002093
2094/* (get-cursor [window]) -> (line . col) */
2095 static Scheme_Object *
2096get_cursor(void *data, int argc, Scheme_Object **argv)
2097{
2098 Vim_Prim *prim = (Vim_Prim *)data;
2099 vim_mz_window *win;
2100 pos_T pos;
2101
2102 win = get_window_arg(prim->name, 0, argc, argv);
2103 pos = win->win->w_cursor;
2104 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
2105 scheme_make_integer_value((long)pos.col + 1));
2106}
2107
2108/* (set-cursor (line . col) [window]) */
2109 static Scheme_Object *
2110set_cursor(void *data, int argc, Scheme_Object **argv)
2111{
2112 Vim_Prim *prim = (Vim_Prim *)data;
2113 vim_mz_window *win;
2114 long lnum = 0;
2115 long col = 0;
2116
Bram Moolenaar555b2802005-05-19 21:08:39 +00002117#ifdef HAVE_SANDBOX
2118 sandbox_check();
2119#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002120 win = get_window_arg(prim->name, 1, argc, argv);
2121 GUARANTEE_PAIR(prim->name, 0);
2122
2123 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
2124 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
2125 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
2126
2127 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
2128 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
2129
2130 check_line_range(lnum, win->win->w_buffer);
2131 /* don't know how to catch invalid column value */
2132
2133 win->win->w_cursor.lnum = lnum;
2134 win->win->w_cursor.col = col;
2135 update_screen(VALID);
2136
2137 raise_if_error();
2138 return scheme_void;
2139}
2140/*
2141 *===========================================================================
2142 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002143 *===========================================================================
2144 */
2145
2146/* (open-buff {filename}) */
2147 static Scheme_Object *
2148mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
2149{
2150 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002151 int num = 0;
Bram Moolenaar75676462013-01-30 14:55:42 +01002152 Scheme_Object *onum = NULL;
2153 Scheme_Object *buf = NULL;
2154 Scheme_Object *fname;
2155
2156 MZ_GC_DECL_REG(3);
2157 MZ_GC_VAR_IN_REG(0, onum);
2158 MZ_GC_VAR_IN_REG(1, buf);
2159 MZ_GC_VAR_IN_REG(2, fname);
2160 MZ_GC_REG();
2161 fname = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002162
Bram Moolenaar555b2802005-05-19 21:08:39 +00002163#ifdef HAVE_SANDBOX
2164 sandbox_check();
2165#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002166 /* TODO make open existing file */
Bram Moolenaar75676462013-01-30 14:55:42 +01002167 num = buflist_add(BYTE_STRING_VALUE(fname), BLN_LISTED | BLN_CURBUF);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002168
2169 if (num == 0)
2170 raise_vim_exn(_("couldn't open buffer"));
2171
2172 onum = scheme_make_integer(num);
Bram Moolenaar75676462013-01-30 14:55:42 +01002173 buf = get_buffer_by_num(data, 1, &onum);
2174 MZ_GC_UNREG();
2175 return buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002176}
2177
2178/* (get-buff-by-num {buffernum}) */
2179 static Scheme_Object *
2180get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
2181{
2182 Vim_Prim *prim = (Vim_Prim *)data;
2183 buf_T *buf;
2184 int fnum;
2185
2186 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2187
Bram Moolenaar29323592016-07-24 22:04:11 +02002188 FOR_ALL_BUFFERS(buf)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002189 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002190 return buffer_new(buf);
2191
2192 return scheme_false;
2193}
2194
2195/* (get-buff-by-name {buffername}) */
2196 static Scheme_Object *
2197get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
2198{
2199 Vim_Prim *prim = (Vim_Prim *)data;
2200 buf_T *buf;
Bram Moolenaar75676462013-01-30 14:55:42 +01002201 Scheme_Object *buffer = NULL;
2202 Scheme_Object *fname = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002203
Bram Moolenaar75676462013-01-30 14:55:42 +01002204 MZ_GC_DECL_REG(2);
2205 MZ_GC_VAR_IN_REG(0, buffer);
2206 MZ_GC_VAR_IN_REG(1, fname);
2207 MZ_GC_REG();
2208 fname = GUARANTEED_STRING_ARG(prim->name, 0);
2209 buffer = scheme_false;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002210
Bram Moolenaar29323592016-07-24 22:04:11 +02002211 FOR_ALL_BUFFERS(buf)
Bram Moolenaar75676462013-01-30 14:55:42 +01002212 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002213 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2214 /* empty string */
2215 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002216 if (BYTE_STRING_VALUE(fname)[0] == NUL)
2217 buffer = buffer_new(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002218 }
Bram Moolenaar75676462013-01-30 14:55:42 +01002219 else if (!fnamecmp(buf->b_ffname, BYTE_STRING_VALUE(fname))
2220 || !fnamecmp(buf->b_sfname, BYTE_STRING_VALUE(fname)))
2221 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002222 /* either short or long filename matches */
Bram Moolenaar75676462013-01-30 14:55:42 +01002223 buffer = buffer_new(buf);
2224 }
2225 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002226
Bram Moolenaar75676462013-01-30 14:55:42 +01002227 MZ_GC_UNREG();
2228 return buffer;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002229}
2230
2231/* (get-next-buff [buffer]) */
2232 static Scheme_Object *
2233get_next_buffer(void *data, int argc, Scheme_Object **argv)
2234{
2235 Vim_Prim *prim = (Vim_Prim *)data;
2236 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2237
2238 if (buf->b_next == NULL)
2239 return scheme_false;
2240 else
2241 return buffer_new(buf->b_next);
2242}
2243
2244/* (get-prev-buff [buffer]) */
2245 static Scheme_Object *
2246get_prev_buffer(void *data, int argc, Scheme_Object **argv)
2247{
2248 Vim_Prim *prim = (Vim_Prim *)data;
2249 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2250
2251 if (buf->b_prev == NULL)
2252 return scheme_false;
2253 else
2254 return buffer_new(buf->b_prev);
2255}
2256
2257/* (get-buff-num [buffer]) */
2258 static Scheme_Object *
2259get_buffer_num(void *data, int argc, Scheme_Object **argv)
2260{
2261 Vim_Prim *prim = (Vim_Prim *)data;
2262 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2263
2264 return scheme_make_integer(buf->buf->b_fnum);
2265}
2266
2267/* (buff-count) */
2268 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002269get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002270{
2271 buf_T *b;
2272 int n = 0;
2273
Bram Moolenaar29323592016-07-24 22:04:11 +02002274 FOR_ALL_BUFFERS(b) ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002275 return scheme_make_integer(n);
2276}
2277
2278/* (get-buff-name [buffer]) */
2279 static Scheme_Object *
2280get_buffer_name(void *data, int argc, Scheme_Object **argv)
2281{
2282 Vim_Prim *prim = (Vim_Prim *)data;
2283 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2284
Bram Moolenaar75676462013-01-30 14:55:42 +01002285 return scheme_make_byte_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002286}
2287
2288/* (curr-buff) */
2289 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002290get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002291{
2292 return (Scheme_Object *)get_vim_curr_buffer();
2293}
2294
2295 static Scheme_Object *
2296buffer_new(buf_T *buf)
2297{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002298 vim_mz_buffer *self = NULL;
2299
2300 MZ_GC_DECL_REG(1);
2301 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002302
2303 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00002304 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002305 * then we can get at it in buf_freeall() in vim.
2306 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00002307 if (buf->b_mzscheme_ref)
Bram Moolenaar75676462013-01-30 14:55:42 +01002308 return (Scheme_Object *)BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002309
Bram Moolenaar75676462013-01-30 14:55:42 +01002310 MZ_GC_REG();
2311 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_buffer));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002312 vim_memset(self, 0, sizeof(vim_mz_buffer));
Bram Moolenaar75676462013-01-30 14:55:42 +01002313#ifndef MZ_PRECISE_GC
2314 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
2315#else
2316 buf->b_mzscheme_ref = scheme_malloc_immobile_box(NULL);
2317#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002318 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01002319 BUFFER_REF(buf) = self;
2320 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002321 self->buf = buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002322 self->so.type = mz_buffer_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002323
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002324 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01002325 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002326}
2327
2328/*
2329 * (get-buff-size [buffer])
2330 *
2331 * Get the size (number of lines) in the current buffer.
2332 */
2333 static Scheme_Object *
2334get_buffer_size(void *data, int argc, Scheme_Object **argv)
2335{
2336 Vim_Prim *prim = (Vim_Prim *)data;
2337 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2338
2339 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
2340}
2341
2342/*
2343 * (get-buff-line {linenr} [buffer])
2344 *
2345 * Get a line from the specified buffer. The line number is
2346 * in Vim format (1-based). The line is returned as a MzScheme
2347 * string object.
2348 */
2349 static Scheme_Object *
2350get_buffer_line(void *data, int argc, Scheme_Object **argv)
2351{
2352 Vim_Prim *prim = (Vim_Prim *)data;
2353 vim_mz_buffer *buf;
2354 int linenr;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002355 char_u *line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002356
2357 buf = get_buffer_arg(prim->name, 1, argc, argv);
2358 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2359 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2360
2361 raise_if_error();
Bram Moolenaar75676462013-01-30 14:55:42 +01002362 return scheme_make_byte_string((char *)line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002363}
2364
2365
2366/*
2367 * (get-buff-line-list {start} {end} [buffer])
2368 *
2369 * Get a list of lines from the specified buffer. The line numbers
2370 * are in Vim format (1-based). The range is from lo up to, but not
2371 * including, hi. The list is returned as a list of string objects.
2372 */
2373 static Scheme_Object *
2374get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2375{
2376 Vim_Prim *prim = (Vim_Prim *)data;
2377 vim_mz_buffer *buf;
2378 int i, hi, lo, n;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002379 Scheme_Object *list = NULL;
2380
2381 MZ_GC_DECL_REG(1);
2382 MZ_GC_VAR_IN_REG(0, list);
2383 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002384
2385 buf = get_buffer_arg(prim->name, 2, argc, argv);
2386 list = scheme_null;
2387 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2388 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2389
2390 /*
2391 * Handle some error conditions
2392 */
2393 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002394 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002395
2396 if (hi < 0)
2397 hi = 0;
2398 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002399 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002400
2401 n = hi - lo;
2402
2403 for (i = n; i >= 0; --i)
2404 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002405 Scheme_Object *str = scheme_make_byte_string(
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002406 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2407 raise_if_error();
2408
2409 /* Set the list item */
2410 list = scheme_make_pair(str, list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002411 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002412 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002413 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002414 return list;
2415}
2416
2417/*
2418 * (set-buff-line {linenr} {string/#f} [buffer])
2419 *
2420 * Replace a line in the specified buffer. The line number is
2421 * in Vim format (1-based). The replacement line is given as
2422 * an MzScheme string object. The object is checked for validity
2423 * and correct format. An exception is thrown if the values are not
2424 * the correct format.
2425 *
2426 * It returns a Scheme Object that indicates the length of the
2427 * string changed.
2428 */
2429 static Scheme_Object *
2430set_buffer_line(void *data, int argc, Scheme_Object **argv)
2431{
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002432 /* First of all, we check the value of the supplied MzScheme object.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002433 * There are three cases:
2434 * 1. #f - this is a deletion.
2435 * 2. A string - this is a replacement.
2436 * 3. Anything else - this is an error.
2437 */
2438 Vim_Prim *prim = (Vim_Prim *)data;
2439 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002440 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002441 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002442 int n;
2443
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002444 MZ_GC_DECL_REG(1);
2445 MZ_GC_VAR_IN_REG(0, line);
2446 MZ_GC_REG();
2447
Bram Moolenaar555b2802005-05-19 21:08:39 +00002448#ifdef HAVE_SANDBOX
2449 sandbox_check();
2450#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002451 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2452 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002453 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002454 line = argv[1];
2455 buf = get_buffer_arg(prim->name, 2, argc, argv);
2456
2457 check_line_range(n, buf->buf);
2458
2459 if (SCHEME_FALSEP(line))
2460 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002461 buf_T *savebuf = curbuf;
2462
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002463 curbuf = buf->buf;
2464
2465 if (u_savedel((linenr_T)n, 1L) == FAIL)
2466 {
2467 curbuf = savebuf;
2468 raise_vim_exn(_("cannot save undo information"));
2469 }
2470 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2471 {
2472 curbuf = savebuf;
2473 raise_vim_exn(_("cannot delete line"));
2474 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002475 if (buf->buf == curwin->w_buffer)
2476 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002477 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002478
2479 curbuf = savebuf;
2480
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002481 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002482 raise_if_error();
2483 return scheme_void;
2484 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002485 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002486 {
2487 /* Otherwise it's a line */
2488 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002489
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002490 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002491
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002492 curbuf = buf->buf;
2493
2494 if (u_savesub((linenr_T)n) == FAIL)
2495 {
2496 curbuf = savebuf;
2497 vim_free(save);
2498 raise_vim_exn(_("cannot save undo information"));
2499 }
2500 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2501 {
2502 curbuf = savebuf;
2503 vim_free(save);
2504 raise_vim_exn(_("cannot replace line"));
2505 }
2506 else
2507 {
2508 vim_free(save);
2509 changed_bytes((linenr_T)n, 0);
2510 }
2511
2512 curbuf = savebuf;
2513
2514 /* Check that the cursor is not beyond the end of the line now. */
2515 if (buf->buf == curwin->w_buffer)
2516 check_cursor_col();
2517
2518 MZ_GC_UNREG();
2519 raise_if_error();
2520 return scheme_void;
2521 }
2522}
2523
2524 static void
2525free_array(char **array)
2526{
2527 char **curr = array;
2528 while (*curr != NULL)
2529 vim_free(*curr++);
2530 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002531}
2532
2533/*
2534 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2535 *
2536 * Replace a range of lines in the specified buffer. The line numbers are in
2537 * Vim format (1-based). The range is from lo up to, but not including, hi.
2538 * The replacement lines are given as a Scheme list of string objects. The
2539 * list is checked for validity and correct format.
2540 *
2541 * Errors are returned as a value of FAIL. The return value is OK on success.
2542 * If OK is returned and len_change is not NULL, *len_change is set to the
2543 * change in the buffer length.
2544 */
2545 static Scheme_Object *
2546set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2547{
2548 /* First of all, we check the type of the supplied MzScheme object.
2549 * There are three cases:
2550 * 1. #f - this is a deletion.
2551 * 2. A list - this is a replacement.
2552 * 3. Anything else - this is an error.
2553 */
2554 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002555 vim_mz_buffer *buf = NULL;
2556 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002557 int i, old_len, new_len, hi, lo;
2558 long extra;
2559
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002560 MZ_GC_DECL_REG(1);
2561 MZ_GC_VAR_IN_REG(0, line_list);
2562 MZ_GC_REG();
2563
Bram Moolenaar555b2802005-05-19 21:08:39 +00002564#ifdef HAVE_SANDBOX
2565 sandbox_check();
2566#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002567 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2568 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2569 if (!SCHEME_PAIRP(argv[2])
2570 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2571 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2572 line_list = argv[2];
2573 buf = get_buffer_arg(prim->name, 3, argc, argv);
2574 old_len = hi - lo;
2575 if (old_len < 0) /* process inverse values wisely */
2576 {
2577 i = lo;
2578 lo = hi;
2579 hi = i;
2580 old_len = -old_len;
2581 }
2582 extra = 0;
2583
2584 check_line_range(lo, buf->buf); /* inclusive */
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002585 check_line_range(hi - 1, buf->buf); /* exclusive */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002586
2587 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2588 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002589 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002590 curbuf = buf->buf;
2591
2592 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2593 {
2594 curbuf = savebuf;
2595 raise_vim_exn(_("cannot save undo information"));
2596 }
2597 else
2598 {
2599 for (i = 0; i < old_len; i++)
2600 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2601 {
2602 curbuf = savebuf;
2603 raise_vim_exn(_("cannot delete line"));
2604 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002605 if (buf->buf == curwin->w_buffer)
2606 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002607 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002608 }
2609
2610 curbuf = savebuf;
2611
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002612 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002613 raise_if_error();
2614 return scheme_void;
2615 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002616 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002617 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002618 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002619
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002620 /* List */
2621 new_len = scheme_proper_list_length(line_list);
2622 MZ_GC_CHECK();
2623 if (new_len < 0) /* improper or cyclic list */
2624 scheme_wrong_type(prim->name, "proper list",
2625 2, argc, argv);
2626 else
2627 {
2628 char **array = NULL;
2629 Scheme_Object *line = NULL;
2630 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002631
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002632 MZ_GC_DECL_REG(2);
2633 MZ_GC_VAR_IN_REG(0, line);
2634 MZ_GC_VAR_IN_REG(1, rest);
2635 MZ_GC_REG();
2636
Bram Moolenaar75676462013-01-30 14:55:42 +01002637 array = (char **)alloc((new_len+1)* sizeof(char *));
2638 vim_memset(array, 0, (new_len+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002639
2640 rest = line_list;
2641 for (i = 0; i < new_len; ++i)
2642 {
2643 line = SCHEME_CAR(rest);
2644 rest = SCHEME_CDR(rest);
2645 if (!SCHEME_STRINGP(line))
2646 {
2647 free_array(array);
2648 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2649 }
2650 array[i] = string_to_line(line);
2651 }
2652
2653 curbuf = buf->buf;
2654
2655 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2656 {
2657 curbuf = savebuf;
2658 free_array(array);
2659 raise_vim_exn(_("cannot save undo information"));
2660 }
2661
2662 /*
2663 * If the size of the range is reducing (ie, new_len < old_len) we
2664 * need to delete some old_len. We do this at the start, by
2665 * repeatedly deleting line "lo".
2666 */
2667 for (i = 0; i < old_len - new_len; ++i)
2668 {
2669 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2670 {
2671 curbuf = savebuf;
2672 free_array(array);
2673 raise_vim_exn(_("cannot delete line"));
2674 }
2675 extra--;
2676 }
2677
2678 /*
2679 * For as long as possible, replace the existing old_len with the
2680 * new old_len. This is a more efficient operation, as it requires
2681 * less memory allocation and freeing.
2682 */
2683 for (i = 0; i < old_len && i < new_len; i++)
2684 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2685 {
2686 curbuf = savebuf;
2687 free_array(array);
2688 raise_vim_exn(_("cannot replace line"));
2689 }
2690
2691 /*
2692 * Now we may need to insert the remaining new_len. We don't need to
2693 * free the string passed back because MzScheme has control of that
2694 * memory.
2695 */
2696 while (i < new_len)
2697 {
2698 if (ml_append((linenr_T)(lo + i - 1),
2699 (char_u *)array[i], 0, FALSE) == FAIL)
2700 {
2701 curbuf = savebuf;
2702 free_array(array);
2703 raise_vim_exn(_("cannot insert line"));
2704 }
2705 ++i;
2706 ++extra;
2707 }
2708 MZ_GC_UNREG();
2709 free_array(array);
2710 }
2711
2712 /*
2713 * Adjust marks. Invalidate any which lie in the
2714 * changed range, and move any in the remainder of the buffer.
2715 */
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002716 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2717 (long)MAXLNUM, (long)extra);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002718 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2719
2720 if (buf->buf == curwin->w_buffer)
2721 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002722 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002723
2724 MZ_GC_UNREG();
2725 raise_if_error();
2726 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002727 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002728}
2729
2730/*
2731 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2732 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002733 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002734 * The line number is in Vim format (1-based). The lines to be inserted are
2735 * given as an MzScheme list of string objects or as a single string. The lines
2736 * to be added are checked for validity and correct format. Errors are
2737 * returned as a value of FAIL. The return value is OK on success.
2738 * If OK is returned and len_change is not NULL, *len_change
2739 * is set to the change in the buffer length.
2740 */
2741 static Scheme_Object *
2742insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2743{
2744 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002745 vim_mz_buffer *buf = NULL;
2746 Scheme_Object *list = NULL;
2747 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002748 int i, n, size;
2749
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002750 MZ_GC_DECL_REG(1);
2751 MZ_GC_VAR_IN_REG(0, list);
2752 MZ_GC_REG();
2753
Bram Moolenaar555b2802005-05-19 21:08:39 +00002754#ifdef HAVE_SANDBOX
2755 sandbox_check();
2756#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002757 /*
2758 * First of all, we check the type of the supplied MzScheme object.
2759 * It must be a string or a list, or the call is in error.
2760 */
2761 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2762 list = argv[1];
2763
2764 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2765 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2766 buf = get_buffer_arg(prim->name, 2, argc, argv);
2767
2768 if (n != 0) /* 0 can be used in insert */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002769 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002770 if (SCHEME_STRINGP(list))
2771 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002772 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002773
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002774 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002775 curbuf = buf->buf;
2776
2777 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2778 {
2779 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002780 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002781 raise_vim_exn(_("cannot save undo information"));
2782 }
2783 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2784 {
2785 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002786 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002787 raise_vim_exn(_("cannot insert line"));
2788 }
2789 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002790 {
2791 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002792 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002793 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002794
2795 curbuf = savebuf;
2796 update_screen(VALID);
2797
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002798 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002799 raise_if_error();
2800 return scheme_void;
2801 }
2802
2803 /* List */
2804 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002805 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002806 if (size < 0) /* improper or cyclic list */
2807 scheme_wrong_type(prim->name, "proper list",
2808 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002809 else
2810 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002811 Scheme_Object *line = NULL;
2812 Scheme_Object *rest = NULL;
2813 char **array;
2814 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002815
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002816 MZ_GC_DECL_REG(2);
2817 MZ_GC_VAR_IN_REG(0, line);
2818 MZ_GC_VAR_IN_REG(1, rest);
2819 MZ_GC_REG();
2820
Bram Moolenaar75676462013-01-30 14:55:42 +01002821 array = (char **)alloc((size+1) * sizeof(char *));
2822 vim_memset(array, 0, (size+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002823
2824 rest = list;
2825 for (i = 0; i < size; ++i)
2826 {
2827 line = SCHEME_CAR(rest);
2828 rest = SCHEME_CDR(rest);
2829 array[i] = string_to_line(line);
2830 }
2831
2832 curbuf = buf->buf;
2833
2834 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2835 {
2836 curbuf = savebuf;
2837 free_array(array);
2838 raise_vim_exn(_("cannot save undo information"));
2839 }
2840 else
2841 {
2842 for (i = 0; i < size; ++i)
2843 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2844 0, FALSE) == FAIL)
2845 {
2846 curbuf = savebuf;
2847 free_array(array);
2848 raise_vim_exn(_("cannot insert line"));
2849 }
2850
2851 if (i > 0)
2852 appended_lines_mark((linenr_T)n, (long)i);
2853 }
2854 free_array(array);
2855 MZ_GC_UNREG();
2856 curbuf = savebuf;
2857 update_screen(VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002858 }
2859
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002860 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002861 raise_if_error();
2862 return scheme_void;
2863}
2864
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002865/*
2866 * Predicates
2867 */
2868/* (buff? obj) */
2869 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002870vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002871{
2872 if (SCHEME_VIMBUFFERP(argv[0]))
2873 return scheme_true;
2874 else
2875 return scheme_false;
2876}
2877
2878/* (win? obj) */
2879 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002880vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002881{
2882 if (SCHEME_VIMWINDOWP(argv[0]))
2883 return scheme_true;
2884 else
2885 return scheme_false;
2886}
2887
2888/* (buff-valid? obj) */
2889 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002890vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002891{
2892 if (SCHEME_VIMBUFFERP(argv[0])
2893 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2894 return scheme_true;
2895 else
2896 return scheme_false;
2897}
2898
2899/* (win-valid? obj) */
2900 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002901vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002902{
2903 if (SCHEME_VIMWINDOWP(argv[0])
2904 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2905 return scheme_true;
2906 else
2907 return scheme_false;
2908}
2909
2910/*
2911 *===========================================================================
2912 * Utilities
2913 *===========================================================================
2914 */
2915
2916/*
2917 * Convert an MzScheme string into a Vim line.
2918 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002919 * All internal nulls are replaced by newline characters.
2920 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002921 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002922 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002923 */
2924 static char *
2925string_to_line(Scheme_Object *obj)
2926{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002927 char *scheme_str = NULL;
2928 char *vim_str = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01002929 OUTPUT_LEN_TYPE len;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002930 int i;
2931
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002932 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002933
2934 /* Error checking: String must not contain newlines, as we
2935 * are replacing a single line, and we must replace it with
2936 * a single line.
2937 */
Bram Moolenaar75676462013-01-30 14:55:42 +01002938 if (memchr(scheme_str, '\n', len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002939 scheme_signal_error(_("string cannot contain newlines"));
2940
Bram Moolenaar75676462013-01-30 14:55:42 +01002941 vim_str = (char *)alloc(len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002942
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002943 /* Create a copy of the string, with internal nulls replaced by
2944 * newline characters, as is the vim convention.
2945 */
2946 for (i = 0; i < len; ++i)
2947 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002948 if (scheme_str[i] == '\0')
2949 vim_str[i] = '\n';
2950 else
2951 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002952 }
2953
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002954 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002955
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002956 MZ_GC_CHECK();
2957 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002958}
2959
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002960#ifdef FEAT_EVAL
2961/*
2962 * Convert Vim value into MzScheme, adopted from if_python.c
2963 */
2964 static Scheme_Object *
Bram Moolenaar75676462013-01-30 14:55:42 +01002965vim_to_mzscheme(typval_T *vim_value)
2966{
2967 Scheme_Object *result = NULL;
2968 /* hash table to store visited values to avoid infinite loops */
2969 Scheme_Hash_Table *visited = NULL;
2970
2971 MZ_GC_DECL_REG(2);
2972 MZ_GC_VAR_IN_REG(0, result);
2973 MZ_GC_VAR_IN_REG(1, visited);
2974 MZ_GC_REG();
2975
2976 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2977 MZ_GC_CHECK();
2978
2979 result = vim_to_mzscheme_impl(vim_value, 1, visited);
2980
2981 MZ_GC_UNREG();
2982 return result;
2983}
2984
2985 static Scheme_Object *
2986vim_to_mzscheme_impl(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002987{
2988 Scheme_Object *result = NULL;
2989 int new_value = TRUE;
2990
Bram Moolenaar75676462013-01-30 14:55:42 +01002991 MZ_GC_DECL_REG(2);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002992 MZ_GC_VAR_IN_REG(0, result);
Bram Moolenaar75676462013-01-30 14:55:42 +01002993 MZ_GC_VAR_IN_REG(1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002994 MZ_GC_REG();
2995
2996 /* Avoid infinite recursion */
2997 if (depth > 100)
2998 {
2999 MZ_GC_UNREG();
3000 return scheme_void;
3001 }
3002
3003 /* Check if we run into a recursive loop. The item must be in visited
3004 * then and we can use it again.
3005 */
3006 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
3007 MZ_GC_CHECK();
3008 if (result != NULL) /* found, do nothing */
3009 new_value = FALSE;
3010 else if (vim_value->v_type == VAR_STRING)
3011 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003012 result = scheme_make_byte_string((char *)vim_value->vval.v_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003013 MZ_GC_CHECK();
3014 }
3015 else if (vim_value->v_type == VAR_NUMBER)
3016 {
3017 result = scheme_make_integer((long)vim_value->vval.v_number);
3018 MZ_GC_CHECK();
3019 }
3020# ifdef FEAT_FLOAT
3021 else if (vim_value->v_type == VAR_FLOAT)
3022 {
3023 result = scheme_make_double((double)vim_value->vval.v_float);
3024 MZ_GC_CHECK();
3025 }
3026# endif
3027 else if (vim_value->v_type == VAR_LIST)
3028 {
3029 list_T *list = vim_value->vval.v_list;
3030 listitem_T *curr;
3031
3032 if (list == NULL || list->lv_first == NULL)
3033 result = scheme_null;
3034 else
3035 {
3036 Scheme_Object *obj = NULL;
3037
3038 MZ_GC_DECL_REG(1);
3039 MZ_GC_VAR_IN_REG(0, obj);
3040 MZ_GC_REG();
3041
3042 curr = list->lv_last;
Bram Moolenaar75676462013-01-30 14:55:42 +01003043 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003044 result = scheme_make_pair(obj, scheme_null);
3045 MZ_GC_CHECK();
3046
3047 while (curr != list->lv_first)
3048 {
3049 curr = curr->li_prev;
Bram Moolenaar75676462013-01-30 14:55:42 +01003050 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003051 result = scheme_make_pair(obj, result);
3052 MZ_GC_CHECK();
3053 }
3054 }
3055 MZ_GC_UNREG();
3056 }
3057 else if (vim_value->v_type == VAR_DICT)
3058 {
3059 Scheme_Object *key = NULL;
3060 Scheme_Object *obj = NULL;
3061
3062 MZ_GC_DECL_REG(2);
3063 MZ_GC_VAR_IN_REG(0, key);
3064 MZ_GC_VAR_IN_REG(1, obj);
3065 MZ_GC_REG();
3066
3067 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
3068 MZ_GC_CHECK();
3069 if (vim_value->vval.v_dict != NULL)
3070 {
3071 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
3072 long_u todo = ht->ht_used;
3073 hashitem_T *hi;
3074 dictitem_T *di;
3075
3076 for (hi = ht->ht_array; todo > 0; ++hi)
3077 {
3078 if (!HASHITEM_EMPTY(hi))
3079 {
3080 --todo;
3081
3082 di = dict_lookup(hi);
Bram Moolenaar75676462013-01-30 14:55:42 +01003083 obj = vim_to_mzscheme_impl(&di->di_tv, depth + 1, visited);
3084 key = scheme_make_byte_string((char *)hi->hi_key);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003085 MZ_GC_CHECK();
3086 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
3087 MZ_GC_CHECK();
3088 }
3089 }
3090 }
3091 MZ_GC_UNREG();
3092 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003093 else if (vim_value->v_type == VAR_FUNC)
3094 {
3095 Scheme_Object *funcname = NULL;
3096
3097 MZ_GC_DECL_REG(1);
3098 MZ_GC_VAR_IN_REG(0, funcname);
3099 MZ_GC_REG();
3100
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003101 /* FIXME: func_ref() and func_unref() are needed. */
Bram Moolenaar75676462013-01-30 14:55:42 +01003102 funcname = scheme_make_byte_string((char *)vim_value->vval.v_string);
3103 MZ_GC_CHECK();
3104 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3105 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3106 MZ_GC_CHECK();
3107
3108 MZ_GC_UNREG();
3109 }
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003110 else if (vim_value->v_type == VAR_PARTIAL)
3111 {
3112 if (vim_value->vval.v_partial == NULL)
3113 result = scheme_null;
3114 else
3115 {
3116 Scheme_Object *funcname = NULL;
3117
3118 MZ_GC_DECL_REG(1);
3119 MZ_GC_VAR_IN_REG(0, funcname);
3120 MZ_GC_REG();
3121
3122 /* FIXME: func_ref() and func_unref() are needed. */
3123 /* TODO: Support pt_dict and pt_argv. */
3124 funcname = scheme_make_byte_string(
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003125 (char *)partial_name(vim_value->vval.v_partial));
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003126 MZ_GC_CHECK();
3127 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3128 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3129 MZ_GC_CHECK();
3130
3131 MZ_GC_UNREG();
3132 }
3133 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003134 else if (vim_value->v_type == VAR_SPECIAL)
3135 {
3136 if (vim_value->vval.v_number <= VVAL_TRUE)
3137 result = scheme_make_integer((long)vim_value->vval.v_number);
3138 else
3139 result = scheme_null;
3140 MZ_GC_CHECK();
3141 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003142 else
3143 {
3144 result = scheme_void;
3145 new_value = FALSE;
3146 }
3147 if (new_value)
3148 {
3149 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
3150 MZ_GC_CHECK();
3151 }
3152 MZ_GC_UNREG();
3153 return result;
3154}
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003155
3156 static int
Bram Moolenaar75676462013-01-30 14:55:42 +01003157mzscheme_to_vim(Scheme_Object *obj, typval_T *tv)
3158{
3159 int i, status;
3160 Scheme_Hash_Table *visited = NULL;
3161
3162 MZ_GC_DECL_REG(2);
3163 MZ_GC_VAR_IN_REG(0, obj);
3164 MZ_GC_VAR_IN_REG(1, visited);
3165 MZ_GC_REG();
3166
3167 visited = scheme_make_hash_table(SCHEME_hash_ptr);
3168 MZ_GC_CHECK();
3169
3170 status = mzscheme_to_vim_impl(obj, tv, 1, visited);
3171 for (i = 0; i < visited->size; ++i)
3172 {
3173 /* free up remembered objects */
3174 if (visited->vals[i] != NULL)
3175 free_tv((typval_T *)visited->vals[i]);
3176 }
3177
3178 MZ_GC_UNREG();
3179 return status;
3180}
3181 static int
3182mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003183 Scheme_Hash_Table *visited)
3184{
3185 int status = OK;
3186 typval_T *found;
Bram Moolenaar75676462013-01-30 14:55:42 +01003187
3188 MZ_GC_DECL_REG(2);
3189 MZ_GC_VAR_IN_REG(0, obj);
3190 MZ_GC_VAR_IN_REG(1, visited);
3191 MZ_GC_REG();
3192
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003193 MZ_GC_CHECK();
3194 if (depth > 100) /* limit the deepest recursion level */
3195 {
3196 tv->v_type = VAR_NUMBER;
3197 tv->vval.v_number = 0;
3198 return FAIL;
3199 }
3200
3201 found = (typval_T *)scheme_hash_get(visited, obj);
3202 if (found != NULL)
3203 copy_tv(found, tv);
3204 else if (SCHEME_VOIDP(obj))
3205 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003206 tv->v_type = VAR_SPECIAL;
3207 tv->vval.v_number = VVAL_NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003208 }
3209 else if (SCHEME_INTP(obj))
3210 {
3211 tv->v_type = VAR_NUMBER;
3212 tv->vval.v_number = SCHEME_INT_VAL(obj);
3213 }
3214 else if (SCHEME_BOOLP(obj))
3215 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003216 tv->v_type = VAR_SPECIAL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003217 tv->vval.v_number = SCHEME_TRUEP(obj);
3218 }
3219# ifdef FEAT_FLOAT
3220 else if (SCHEME_DBLP(obj))
3221 {
3222 tv->v_type = VAR_FLOAT;
3223 tv->vval.v_float = SCHEME_DBL_VAL(obj);
3224 }
3225# endif
Bram Moolenaar75676462013-01-30 14:55:42 +01003226 else if (SCHEME_BYTE_STRINGP(obj))
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003227 {
3228 tv->v_type = VAR_STRING;
Bram Moolenaar75676462013-01-30 14:55:42 +01003229 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(obj));
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003230 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003231# if MZSCHEME_VERSION_MAJOR >= 299
3232 else if (SCHEME_CHAR_STRINGP(obj))
3233 {
3234 Scheme_Object *tmp = NULL;
3235 MZ_GC_DECL_REG(1);
3236 MZ_GC_VAR_IN_REG(0, tmp);
3237 MZ_GC_REG();
3238
3239 tmp = scheme_char_string_to_byte_string(obj);
3240 tv->v_type = VAR_STRING;
3241 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(tmp));
3242 MZ_GC_UNREG();
3243 }
3244#endif
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003245 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
3246 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3247 {
3248 list_T *list = list_alloc();
3249 if (list == NULL)
3250 status = FAIL;
3251 else
3252 {
3253 int i;
3254 Scheme_Object *curr = NULL;
3255 Scheme_Object *cval = NULL;
3256 /* temporary var to hold current element of vectors and pairs */
3257 typval_T *v;
3258
3259 MZ_GC_DECL_REG(2);
3260 MZ_GC_VAR_IN_REG(0, curr);
3261 MZ_GC_VAR_IN_REG(1, cval);
3262 MZ_GC_REG();
3263
3264 tv->v_type = VAR_LIST;
3265 tv->vval.v_list = list;
3266 ++list->lv_refcount;
3267
3268 v = (typval_T *)alloc(sizeof(typval_T));
3269 if (v == NULL)
3270 status = FAIL;
3271 else
3272 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003273 /* add the value in advance to allow handling of self-referential
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003274 * data structures */
3275 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3276 copy_tv(tv, visited_tv);
3277 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3278
3279 if (SCHEME_VECTORP(obj))
3280 {
3281 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
3282 {
3283 cval = SCHEME_VEC_ELS(obj)[i];
Bram Moolenaar75676462013-01-30 14:55:42 +01003284 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003285 if (status == FAIL)
3286 break;
3287 status = list_append_tv(list, v);
3288 clear_tv(v);
3289 if (status == FAIL)
3290 break;
3291 }
3292 }
3293 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3294 {
3295 for (curr = obj;
3296 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
3297 curr = SCHEME_CDR(curr))
3298 {
3299 cval = SCHEME_CAR(curr);
Bram Moolenaar75676462013-01-30 14:55:42 +01003300 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003301 if (status == FAIL)
3302 break;
3303 status = list_append_tv(list, v);
3304 clear_tv(v);
3305 if (status == FAIL)
3306 break;
3307 }
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003308 /* improper list not terminated with null
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003309 * need to handle the last element */
3310 if (status == OK && !SCHEME_NULLP(curr))
3311 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003312 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003313 if (status == OK)
3314 {
3315 status = list_append_tv(list, v);
3316 clear_tv(v);
3317 }
3318 }
3319 }
3320 /* nothing to do for scheme_null */
3321 vim_free(v);
3322 }
3323 MZ_GC_UNREG();
3324 }
3325 }
3326 else if (SCHEME_HASHTP(obj))
3327 {
3328 int i;
3329 dict_T *dict;
3330 Scheme_Object *key = NULL;
3331 Scheme_Object *val = NULL;
3332
3333 MZ_GC_DECL_REG(2);
3334 MZ_GC_VAR_IN_REG(0, key);
3335 MZ_GC_VAR_IN_REG(1, val);
3336 MZ_GC_REG();
3337
3338 dict = dict_alloc();
3339 if (dict == NULL)
3340 status = FAIL;
3341 else
3342 {
3343 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3344
3345 tv->v_type = VAR_DICT;
3346 tv->vval.v_dict = dict;
3347 ++dict->dv_refcount;
3348
3349 copy_tv(tv, visited_tv);
3350 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3351
3352 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
3353 {
3354 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
3355 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003356 /* generate item for `display'ed Scheme key */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003357 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
3358 ((Scheme_Hash_Table *) obj)->keys[i]));
3359 /* convert Scheme val to Vim and add it to the dict */
Bram Moolenaar75676462013-01-30 14:55:42 +01003360 if (mzscheme_to_vim_impl(((Scheme_Hash_Table *) obj)->vals[i],
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003361 &item->di_tv, depth + 1, visited) == FAIL
3362 || dict_add(dict, item) == FAIL)
3363 {
3364 dictitem_free(item);
3365 status = FAIL;
3366 break;
3367 }
3368 }
3369
3370 }
3371 }
3372 MZ_GC_UNREG();
3373 }
3374 else
3375 {
3376 /* `display' any other value to string */
3377 tv->v_type = VAR_STRING;
3378 tv->vval.v_string = (char_u *)string_to_line(obj);
3379 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003380 MZ_GC_UNREG();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003381 return status;
3382}
3383
Bram Moolenaar75676462013-01-30 14:55:42 +01003384/* Scheme prim procedure wrapping Vim funcref */
3385 static Scheme_Object *
3386vim_funcref(void *name, int argc, Scheme_Object **argv)
3387{
3388 int i;
3389 typval_T args;
3390 int status = OK;
3391 Scheme_Object *result = NULL;
3392 list_T *list = list_alloc();
3393
3394 MZ_GC_DECL_REG(1);
3395 MZ_GC_VAR_IN_REG(0, result);
3396 MZ_GC_REG();
3397
3398 result = scheme_void;
3399 if (list == NULL)
3400 status = FAIL;
3401 else
3402 {
3403 args.v_type = VAR_LIST;
3404 args.vval.v_list = list;
3405 ++list->lv_refcount;
3406 for (i = 0; status == OK && i < argc; ++i)
3407 {
3408 typval_T *v = (typval_T *)alloc(sizeof(typval_T));
3409 if (v == NULL)
3410 status = FAIL;
3411 else
3412 {
3413 status = mzscheme_to_vim(argv[i], v);
3414 if (status == OK)
3415 {
3416 status = list_append_tv(list, v);
3417 clear_tv(v);
3418 }
3419 vim_free(v);
3420 }
3421 }
3422 if (status == OK)
3423 {
3424 typval_T ret;
3425 ret.v_type = VAR_UNKNOWN;
3426
3427 mzscheme_call_vim(BYTE_STRING_VALUE((Scheme_Object *)name), &args, &ret);
3428 MZ_GC_CHECK();
3429 result = vim_to_mzscheme(&ret);
3430 clear_tv(&ret);
3431 MZ_GC_CHECK();
3432 }
3433 }
3434 clear_tv(&args);
3435 MZ_GC_UNREG();
3436 if (status != OK)
3437 raise_vim_exn(_("error converting Scheme values to Vim"));
3438 else
3439 raise_if_error();
3440 return result;
3441}
3442
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003443 void
3444do_mzeval(char_u *str, typval_T *rettv)
3445{
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003446 Scheme_Object *ret = NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003447
Bram Moolenaar75676462013-01-30 14:55:42 +01003448 MZ_GC_DECL_REG(1);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003449 MZ_GC_VAR_IN_REG(0, ret);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003450 MZ_GC_REG();
3451
3452 if (mzscheme_init())
3453 {
3454 MZ_GC_UNREG();
3455 return;
3456 }
3457
3458 MZ_GC_CHECK();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003459 if (eval_with_exn_handling(str, do_eval, &ret) == OK)
Bram Moolenaar75676462013-01-30 14:55:42 +01003460 mzscheme_to_vim(ret, rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003461
3462 MZ_GC_UNREG();
3463}
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003464#endif
3465
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003466/*
3467 * Check to see whether a Vim error has been reported, or a keyboard
3468 * interrupt (from vim --> got_int) has been detected.
3469 */
3470 static int
3471vim_error_check(void)
3472{
3473 return (got_int || did_emsg);
3474}
3475
3476/*
3477 * register Scheme exn:vim
3478 */
3479 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003480register_vim_exn(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003481{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003482 int nc = 0;
3483 int i;
3484 Scheme_Object *struct_exn = NULL;
3485 Scheme_Object *exn_name = NULL;
3486
3487 MZ_GC_DECL_REG(2);
3488 MZ_GC_VAR_IN_REG(0, struct_exn);
3489 MZ_GC_VAR_IN_REG(1, exn_name);
3490 MZ_GC_REG();
3491
3492 exn_name = scheme_intern_symbol("exn:vim");
3493 MZ_GC_CHECK();
3494 struct_exn = scheme_builtin_value("struct:exn");
3495 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003496
3497 if (vim_exn == NULL)
3498 vim_exn = scheme_make_struct_type(exn_name,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003499 struct_exn, NULL, 0, 0, NULL, NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003500#if MZSCHEME_VERSION_MAJOR >= 299
3501 , NULL
3502#endif
3503 );
3504
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003505
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003506 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003507 Scheme_Object **tmp = NULL;
3508 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
3509 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
3510 MZ_GC_DECL_REG(6);
3511 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
3512 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
3513 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003514
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003515 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003516 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
3517 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003518
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003519 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
3520 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
3521 MZ_GC_CHECK();
3522
3523 for (i = 0; i < nc; i++)
3524 {
3525 scheme_add_global_symbol(exn_names[i],
3526 exn_values[i], environment);
3527 MZ_GC_CHECK();
3528 }
3529 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003530 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003531 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003532}
3533
3534/*
3535 * raise exn:vim, may be with additional info string
3536 */
3537 void
3538raise_vim_exn(const char *add_info)
3539{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003540 char *fmt = _("Vim error: ~a");
3541 Scheme_Object *argv[2] = {NULL, NULL};
3542 Scheme_Object *exn = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01003543 Scheme_Object *byte_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003544
Bram Moolenaar75676462013-01-30 14:55:42 +01003545 MZ_GC_DECL_REG(5);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003546 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
3547 MZ_GC_VAR_IN_REG(3, exn);
Bram Moolenaar75676462013-01-30 14:55:42 +01003548 MZ_GC_VAR_IN_REG(4, byte_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003549 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003550
3551 if (add_info != NULL)
3552 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003553 char *c_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003554 Scheme_Object *info = NULL;
3555
3556 MZ_GC_DECL_REG(3);
3557 MZ_GC_VAR_IN_REG(0, c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003558 MZ_GC_VAR_IN_REG(2, info);
3559 MZ_GC_REG();
3560
Bram Moolenaar75676462013-01-30 14:55:42 +01003561 info = scheme_make_byte_string(add_info);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003562 MZ_GC_CHECK();
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02003563 c_string = scheme_format_utf8(fmt, (int)STRLEN(fmt), 1, &info, NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003564 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01003565 byte_string = scheme_make_byte_string(c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003566 MZ_GC_CHECK();
3567 argv[0] = scheme_byte_string_to_char_string(byte_string);
Bram Moolenaar555b2802005-05-19 21:08:39 +00003568 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003569 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003570 }
3571 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003572 {
3573 byte_string = scheme_make_byte_string(_("Vim error"));
3574 MZ_GC_CHECK();
3575 argv[0] = scheme_byte_string_to_char_string(byte_string);
3576 MZ_GC_CHECK();
3577 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003578 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003579
Bram Moolenaar049377e2007-05-12 15:32:12 +00003580#if MZSCHEME_VERSION_MAJOR < 360
3581 argv[1] = scheme_current_continuation_marks();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003582 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003583#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003584 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003585 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003586#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003587
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003588 exn = scheme_make_struct_instance(vim_exn, 2, argv);
3589 MZ_GC_CHECK();
3590 scheme_raise(exn);
3591 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003592}
3593
3594 void
3595raise_if_error(void)
3596{
3597 if (vim_error_check())
3598 raise_vim_exn(NULL);
3599}
3600
3601/* get buffer:
3602 * either current
3603 * or passed as argv[argnum] with checks
3604 */
3605 static vim_mz_buffer *
3606get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3607{
3608 vim_mz_buffer *b;
3609
3610 if (argc < argnum + 1)
3611 return get_vim_curr_buffer();
3612 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3613 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3614 b = (vim_mz_buffer *)argv[argnum];
3615 (void)get_valid_buffer(argv[argnum]);
3616 return b;
3617}
3618
3619/* get window:
3620 * either current
3621 * or passed as argv[argnum] with checks
3622 */
3623 static vim_mz_window *
3624get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3625{
3626 vim_mz_window *w;
3627
3628 if (argc < argnum + 1)
3629 return get_vim_curr_window();
3630 w = (vim_mz_window *)argv[argnum];
3631 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3632 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3633 (void)get_valid_window(argv[argnum]);
3634 return w;
3635}
3636
3637/* get valid Vim buffer from Scheme_Object* */
3638buf_T *get_valid_buffer(void *obj)
3639{
3640 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3641
3642 if (buf == INVALID_BUFFER_VALUE)
3643 scheme_signal_error(_("buffer is invalid"));
3644 return buf;
3645}
3646
3647/* get valid Vim window from Scheme_Object* */
3648win_T *get_valid_window(void *obj)
3649{
3650 win_T *win = ((vim_mz_window *)obj)->win;
3651 if (win == INVALID_WINDOW_VALUE)
3652 scheme_signal_error(_("window is invalid"));
3653 return win;
3654}
3655
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003656 int
3657mzthreads_allowed(void)
3658{
3659 return mz_threads_allow;
3660}
3661
3662 static int
3663line_in_range(linenr_T lnum, buf_T *buf)
3664{
3665 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3666}
3667
3668 static void
3669check_line_range(linenr_T lnum, buf_T *buf)
3670{
3671 if (!line_in_range(lnum, buf))
3672 scheme_signal_error(_("linenr out of range"));
3673}
3674
3675/*
3676 * Check if deleting lines made the cursor position invalid
3677 * (or you'll get msg from Vim about invalid linenr).
3678 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3679 * deleted). Got from if_python.c
3680 */
3681 static void
3682mz_fix_cursor(int lo, int hi, int extra)
3683{
3684 if (curwin->w_cursor.lnum >= lo)
3685 {
3686 /* Adjust the cursor position if it's in/after the changed
3687 * lines. */
3688 if (curwin->w_cursor.lnum >= hi)
3689 {
3690 curwin->w_cursor.lnum += extra;
3691 check_cursor_col();
3692 }
3693 else if (extra < 0)
3694 {
3695 curwin->w_cursor.lnum = lo;
3696 check_cursor();
3697 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003698 else
3699 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003700 changed_cline_bef_curs();
3701 }
3702 invalidate_botline();
3703}
3704
3705static Vim_Prim prims[]=
3706{
3707 /*
3708 * Buffer-related commands
3709 */
3710 {get_buffer_line, "get-buff-line", 1, 2},
3711 {set_buffer_line, "set-buff-line", 2, 3},
3712 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3713 {get_buffer_name, "get-buff-name", 0, 1},
3714 {get_buffer_num, "get-buff-num", 0, 1},
3715 {get_buffer_size, "get-buff-size", 0, 1},
3716 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3717 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3718 {get_curr_buffer, "curr-buff", 0, 0},
3719 {get_buffer_count, "buff-count", 0, 0},
3720 {get_next_buffer, "get-next-buff", 0, 1},
3721 {get_prev_buffer, "get-prev-buff", 0, 1},
3722 {mzscheme_open_buffer, "open-buff", 1, 1},
3723 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3724 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003725 /*
3726 * Window-related commands
3727 */
3728 {get_curr_win, "curr-win", 0, 0},
3729 {get_window_count, "win-count", 0, 0},
3730 {get_window_by_num, "get-win-by-num", 1, 1},
3731 {get_window_num, "get-win-num", 0, 1},
3732 {get_window_buffer, "get-win-buffer", 0, 1},
3733 {get_window_height, "get-win-height", 0, 1},
3734 {set_window_height, "set-win-height", 1, 2},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003735 {get_window_width, "get-win-width", 0, 1},
3736 {set_window_width, "set-win-width", 1, 2},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003737 {get_cursor, "get-cursor", 0, 1},
3738 {set_cursor, "set-cursor", 1, 2},
3739 {get_window_list, "get-win-list", 0, 1},
3740 /*
3741 * Vim-related commands
3742 */
3743 {vim_command, "command", 1, 1},
3744 {vim_eval, "eval", 1, 1},
3745 {get_range_start, "range-start", 0, 0},
3746 {get_range_end, "range-end", 0, 0},
3747 {mzscheme_beep, "beep", 0, 0},
3748 {get_option, "get-option", 1, 2},
3749 {set_option, "set-option", 1, 2},
3750 /*
3751 * small utilities
3752 */
3753 {vim_bufferp, "buff?", 1, 1},
3754 {vim_windowp, "win?", 1, 1},
3755 {vim_buffer_validp, "buff-valid?", 1, 1},
3756 {vim_window_validp, "win-valid?", 1, 1}
3757};
3758
3759/* return MzScheme wrapper for curbuf */
3760 static vim_mz_buffer *
3761get_vim_curr_buffer(void)
3762{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003763 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003764 return (vim_mz_buffer *)buffer_new(curbuf);
3765 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003766 return BUFFER_REF(curbuf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003767}
3768
3769/* return MzScheme wrapper for curwin */
3770 static vim_mz_window *
3771get_vim_curr_window(void)
3772{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003773 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003774 return (vim_mz_window *)window_new(curwin);
3775 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003776 return WINDOW_REF(curwin);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003777}
3778
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003779 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003780make_modules(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003781{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003782 int i;
3783 Scheme_Env *mod = NULL;
3784 Scheme_Object *vimext_symbol = NULL;
3785 Scheme_Object *closed_prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003786
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003787 MZ_GC_DECL_REG(3);
3788 MZ_GC_VAR_IN_REG(0, mod);
3789 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3790 MZ_GC_VAR_IN_REG(2, closed_prim);
3791 MZ_GC_REG();
3792
3793 vimext_symbol = scheme_intern_symbol("vimext");
3794 MZ_GC_CHECK();
3795 mod = scheme_primitive_module(vimext_symbol, environment);
3796 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003797 /* all prims made closed so they can access their own names */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003798 for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003799 {
3800 Vim_Prim *prim = prims + i;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003801 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3802 prim->mina, prim->maxa);
3803 scheme_add_global(prim->name, closed_prim, mod);
3804 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003805 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003806 scheme_finish_primitive_module(mod);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003807 MZ_GC_CHECK();
3808 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003809}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003810
Bram Moolenaar555b2802005-05-19 21:08:39 +00003811#ifdef HAVE_SANDBOX
3812static Scheme_Object *M_write = NULL;
3813static Scheme_Object *M_read = NULL;
3814static Scheme_Object *M_execute = NULL;
3815static Scheme_Object *M_delete = NULL;
3816
3817 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003818sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003819{
3820 if (sandbox)
3821 raise_vim_exn(_("not allowed in the Vim sandbox"));
3822}
3823
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003824/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003825 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003826sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003827{
3828 if (sandbox)
3829 {
3830 Scheme_Object *requested_access = argv[2];
3831
3832 if (M_write == NULL)
3833 {
3834 MZ_REGISTER_STATIC(M_write);
3835 M_write = scheme_intern_symbol("write");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003836 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003837 }
3838 if (M_read == NULL)
3839 {
3840 MZ_REGISTER_STATIC(M_read);
3841 M_read = scheme_intern_symbol("read");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003842 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003843 }
3844 if (M_execute == NULL)
3845 {
3846 MZ_REGISTER_STATIC(M_execute);
3847 M_execute = scheme_intern_symbol("execute");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003848 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003849 }
3850 if (M_delete == NULL)
3851 {
3852 MZ_REGISTER_STATIC(M_delete);
3853 M_delete = scheme_intern_symbol("delete");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003854 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003855 }
3856
3857 while (!SCHEME_NULLP(requested_access))
3858 {
3859 Scheme_Object *item = SCHEME_CAR(requested_access);
3860 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3861 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
3862 {
3863 raise_vim_exn(_("not allowed in the Vim sandbox"));
3864 }
3865 requested_access = SCHEME_CDR(requested_access);
3866 }
3867 }
3868 return scheme_void;
3869}
3870
3871 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003872sandbox_network_guard(int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003873{
3874 return scheme_void;
3875}
3876#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00003877
3878#endif