blob: 41388109ea7cae4021da7b4e42f9ca9a5d3409f6 [file] [log] [blame]
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003 * MzScheme interface by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar75676462013-01-30 14:55:42 +01004 * Based on work by Brent Fulgham <bfulgham@debian.org>
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005 * (Based on lots of help from Matthew Flatt)
6 *
7 * This consists of six parts:
8 * 1. MzScheme interpreter main program
9 * 2. Routines that handle the external interface between MzScheme and
10 * Vim.
11 * 3. MzScheme input/output handlers: writes output via [e]msg().
12 * 4. Implementation of the Vim Features for MzScheme
13 * 5. Vim Window-related Manipulation Functions.
14 * 6. Vim Buffer-related Manipulation Functions
15 *
16 * NOTES
17 * 1. Memory, allocated with scheme_malloc*, need not to be freed explicitly,
18 * garbage collector will do it self
19 * 2. Requires at least NORMAL features. I can't imagine why one may want
20 * to build with SMALL or TINY features but with MzScheme interface.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000021 * 3. I don't use K&R-style functions. Anyways, MzScheme headers are ANSI.
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022 */
23
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024#include "vim.h"
Bram Moolenaar049377e2007-05-12 15:32:12 +000025
Bram Moolenaar325b7a22004-07-05 15:58:32 +000026#include "if_mzsch.h"
27
Bram Moolenaar76b92b22006-03-24 22:46:53 +000028/* Only do the following when the feature is enabled. Needed for "make
29 * depend". */
30#if defined(FEAT_MZSCHEME) || defined(PROTO)
31
Bram Moolenaar4349c572016-01-30 13:28:28 +010032#ifdef PROTO
33typedef int Scheme_Object;
34typedef int Scheme_Closed_Prim;
35typedef int Scheme_Env;
36typedef int Scheme_Hash_Table;
37typedef int Scheme_Type;
38typedef int Scheme_Thread;
39typedef int Scheme_Closed_Prim;
40typedef int mzshort;
41typedef int Scheme_Prim;
42typedef int HINSTANCE;
43#endif
44
Bram Moolenaar4e640bd2016-01-16 16:20:38 +010045/*
46 * scheme_register_tls_space is only available on 32-bit Windows until
47 * racket-6.3. See
48 * http://docs.racket-lang.org/inside/im_memoryalloc.html?q=scheme_register_tls_space
49 */
50#if MZSCHEME_VERSION_MAJOR >= 500 && defined(WIN32) \
51 && defined(USE_THREAD_LOCAL) \
52 && (!defined(_WIN64) || MZSCHEME_VERSION_MAJOR >= 603)
53# define HAVE_TLS_SPACE 1
54#endif
55
56/*
57 * Since version 4.x precise GC requires trampolined startup.
58 * Futures and places in version 5.x need it too.
59 */
60#if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR >= 400 \
61 || MZSCHEME_VERSION_MAJOR >= 500 \
62 && (defined(MZ_USE_FUTURES) || defined(MZ_USE_PLACES))
63# define TRAMPOLINED_MZVIM_STARTUP
64#endif
65
Bram Moolenaar325b7a22004-07-05 15:58:32 +000066/* Base data structures */
67#define SCHEME_VIMBUFFERP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_buffer_type)
68#define SCHEME_VIMWINDOWP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_window_type)
69
70typedef struct
71{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000072 Scheme_Object so;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000073 buf_T *buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000074} vim_mz_buffer;
75
76#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
77
78typedef struct
79{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000080 Scheme_Object so;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000081 win_T *win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000082} vim_mz_window;
83
84#define INVALID_WINDOW_VALUE ((win_T *)(-1))
85
86/*
87 * Prims that form MzScheme Vim interface
88 */
89typedef struct
90{
91 Scheme_Closed_Prim *prim;
92 char *name;
93 int mina; /* arity information */
94 int maxa;
95} Vim_Prim;
96
97typedef struct
98{
99 char *name;
100 Scheme_Object *port;
101} Port_Info;
102
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000103/*
104 *========================================================================
105 * Vim-Control Commands
106 *========================================================================
107 */
108/*
109 *========================================================================
110 * Utility functions for the vim/mzscheme interface
111 *========================================================================
112 */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000113#ifdef HAVE_SANDBOX
114static Scheme_Object *sandbox_file_guard(int, Scheme_Object **);
115static Scheme_Object *sandbox_network_guard(int, Scheme_Object **);
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000116static void sandbox_check(void);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000117#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000118/* Buffer-related commands */
119static Scheme_Object *buffer_new(buf_T *buf);
120static Scheme_Object *get_buffer_by_name(void *, int, Scheme_Object **);
121static Scheme_Object *get_buffer_by_num(void *, int, Scheme_Object **);
122static Scheme_Object *get_buffer_count(void *, int, Scheme_Object **);
123static Scheme_Object *get_buffer_line(void *, int, Scheme_Object **);
124static Scheme_Object *get_buffer_line_list(void *, int, Scheme_Object **);
125static Scheme_Object *get_buffer_name(void *, int, Scheme_Object **);
126static Scheme_Object *get_buffer_num(void *, int, Scheme_Object **);
127static Scheme_Object *get_buffer_size(void *, int, Scheme_Object **);
128static Scheme_Object *get_curr_buffer(void *, int, Scheme_Object **);
129static Scheme_Object *get_next_buffer(void *, int, Scheme_Object **);
130static Scheme_Object *get_prev_buffer(void *, int, Scheme_Object **);
131static Scheme_Object *mzscheme_open_buffer(void *, int, Scheme_Object **);
132static Scheme_Object *set_buffer_line(void *, int, Scheme_Object **);
133static Scheme_Object *set_buffer_line_list(void *, int, Scheme_Object **);
134static Scheme_Object *insert_buffer_line_list(void *, int, Scheme_Object **);
135static Scheme_Object *get_range_start(void *, int, Scheme_Object **);
136static Scheme_Object *get_range_end(void *, int, Scheme_Object **);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000137static vim_mz_buffer *get_vim_curr_buffer(void);
138
139/* Window-related commands */
140static Scheme_Object *window_new(win_T *win);
141static Scheme_Object *get_curr_win(void *, int, Scheme_Object **);
142static Scheme_Object *get_window_count(void *, int, Scheme_Object **);
143static Scheme_Object *get_window_by_num(void *, int, Scheme_Object **);
144static Scheme_Object *get_window_num(void *, int, Scheme_Object **);
145static Scheme_Object *get_window_buffer(void *, int, Scheme_Object **);
146static Scheme_Object *get_window_height(void *, int, Scheme_Object **);
147static Scheme_Object *set_window_height(void *, int, Scheme_Object **);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100148#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000149static Scheme_Object *get_window_width(void *, int, Scheme_Object **);
150static Scheme_Object *set_window_width(void *, int, Scheme_Object **);
151#endif
152static Scheme_Object *get_cursor(void *, int, Scheme_Object **);
153static Scheme_Object *set_cursor(void *, int, Scheme_Object **);
154static Scheme_Object *get_window_list(void *, int, Scheme_Object **);
155static vim_mz_window *get_vim_curr_window(void);
156
157/* Vim-related commands */
158static Scheme_Object *mzscheme_beep(void *, int, Scheme_Object **);
159static Scheme_Object *get_option(void *, int, Scheme_Object **);
160static Scheme_Object *set_option(void *, int, Scheme_Object **);
161static Scheme_Object *vim_command(void *, int, Scheme_Object **);
162static Scheme_Object *vim_eval(void *, int, Scheme_Object **);
163static Scheme_Object *vim_bufferp(void *data, int, Scheme_Object **);
164static Scheme_Object *vim_windowp(void *data, int, Scheme_Object **);
165static Scheme_Object *vim_buffer_validp(void *data, int, Scheme_Object **);
166static Scheme_Object *vim_window_validp(void *data, int, Scheme_Object **);
167
168/*
169 *========================================================================
170 * Internal Function Prototypes
171 *========================================================================
172 */
173static int vim_error_check(void);
174static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100175static int startup_mzscheme(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000176static char *string_to_line(Scheme_Object *obj);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100177#if MZSCHEME_VERSION_MAJOR >= 501
Bram Moolenaar75676462013-01-30 14:55:42 +0100178# define OUTPUT_LEN_TYPE intptr_t
179#else
180# define OUTPUT_LEN_TYPE long
181#endif
182static void do_output(char *mesg, OUTPUT_LEN_TYPE len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000183static void do_printf(char *format, ...);
184static void do_flush(void);
185static Scheme_Object *_apply_thunk_catch_exceptions(
186 Scheme_Object *, Scheme_Object **);
187static Scheme_Object *extract_exn_message(Scheme_Object *v);
188static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
189static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000190static void register_vim_exn(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000191static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
192 int argc, Scheme_Object **argv);
193static vim_mz_window *get_window_arg(const char *fname, int argnum,
194 int argc, Scheme_Object **argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000195static int line_in_range(linenr_T, buf_T *);
196static void check_line_range(linenr_T, buf_T *);
197static void mz_fix_cursor(int lo, int hi, int extra);
198
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000199static int eval_with_exn_handling(void *, Scheme_Closed_Prim *,
200 Scheme_Object **ret);
201static void make_modules(void);
202static void init_exn_catching_apply(void);
203static int mzscheme_env_main(Scheme_Env *env, int argc, char **argv);
204static int mzscheme_init(void);
205#ifdef FEAT_EVAL
Bram Moolenaar75676462013-01-30 14:55:42 +0100206static Scheme_Object *vim_to_mzscheme(typval_T *vim_value);
207static Scheme_Object *vim_to_mzscheme_impl(typval_T *vim_value, int depth,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000208 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100209static int mzscheme_to_vim(Scheme_Object *obj, typval_T *tv);
210static int mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100211 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100212static Scheme_Object *vim_funcref(void *data, int argc, Scheme_Object **argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000213#endif
214
215#ifdef MZ_PRECISE_GC
Bram Moolenaar64404472010-06-26 06:24:45 +0200216static int buffer_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000217{
218 return gcBYTES_TO_WORDS(sizeof(vim_mz_buffer));
219}
220static int buffer_mark_proc(void *obj)
221{
222 return buffer_size_proc(obj);
223}
224static int buffer_fixup_proc(void *obj)
225{
Bram Moolenaar75676462013-01-30 14:55:42 +0100226 /* apparently not needed as the object will be uncollectable while
227 * the buffer is alive
228 */
229 /*
230 vim_mz_buffer* buf = (vim_mz_buffer*) obj;
231 buf->buf->b_mzscheme_ref = GC_fixup_self(obj);
232 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000233 return buffer_size_proc(obj);
234}
Bram Moolenaar64404472010-06-26 06:24:45 +0200235static int window_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000236{
237 return gcBYTES_TO_WORDS(sizeof(vim_mz_window));
238}
239static int window_mark_proc(void *obj)
240{
241 return window_size_proc(obj);
242}
243static int window_fixup_proc(void *obj)
244{
Bram Moolenaar75676462013-01-30 14:55:42 +0100245 /* apparently not needed as the object will be uncollectable while
246 * the window is alive
247 */
248 /*
249 vim_mz_window* win = (vim_mz_window*) obj;
250 win->win->w_mzscheme_ref = GC_fixup_self(obj);
251 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000252 return window_size_proc(obj);
253}
Bram Moolenaar75676462013-01-30 14:55:42 +0100254/* with precise GC, w_mzscheme_ref and b_mzscheme_ref are immobile boxes
255 * containing pointers to a window/buffer
256 * with conservative GC these are simply pointers*/
257# define WINDOW_REF(win) *(vim_mz_window **)((win)->w_mzscheme_ref)
258# define BUFFER_REF(buf) *(vim_mz_buffer **)((buf)->b_mzscheme_ref)
259#else
260# define WINDOW_REF(win) (vim_mz_window *)((win)->w_mzscheme_ref)
261# define BUFFER_REF(buf) (vim_mz_buffer *)((buf)->b_mzscheme_ref)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000262#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000263
Bram Moolenaar4349c572016-01-30 13:28:28 +0100264#if defined(DYNAMIC_MZSCHEME) || defined(PROTO)
Bram Moolenaar33570922005-01-25 22:26:29 +0000265static Scheme_Object *dll_scheme_eof;
266static Scheme_Object *dll_scheme_false;
267static Scheme_Object *dll_scheme_void;
268static Scheme_Object *dll_scheme_null;
269static Scheme_Object *dll_scheme_true;
270
271static Scheme_Thread **dll_scheme_current_thread_ptr;
272
273static void (**dll_scheme_console_printf_ptr)(char *str, ...);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100274static void (**dll_scheme_console_output_ptr)(char *str, OUTPUT_LEN_TYPE len);
Bram Moolenaar33570922005-01-25 22:26:29 +0000275static void (**dll_scheme_notify_multithread_ptr)(int on);
276
277static void *(*dll_GC_malloc)(size_t size_in_bytes);
278static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
279static Scheme_Env *(*dll_scheme_basic_env)(void);
280static void (*dll_scheme_check_threads)(void);
281static void (*dll_scheme_register_static)(void *ptr, long size);
282static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
283static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
284 Scheme_Env *env);
285static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
286 Scheme_Object *val, Scheme_Env *env);
287static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
288 Scheme_Object **rands);
289static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000290# if MZSCHEME_VERSION_MAJOR >= 299
291static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100292static Scheme_Object *(*dll_scheme_make_path)(const char *chars);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000293# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000294static void (*dll_scheme_close_input_port)(Scheme_Object *port);
295static void (*dll_scheme_count_lines)(Scheme_Object *port);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000296#if MZSCHEME_VERSION_MAJOR < 360
Bram Moolenaar33570922005-01-25 22:26:29 +0000297static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000298#else
299static Scheme_Object *(*dll_scheme_current_continuation_marks)(Scheme_Object *prompt_tag);
300#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000301static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100302static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, OUTPUT_LEN_TYPE *len);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000303static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
Bram Moolenaar33570922005-01-25 22:26:29 +0000304static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
305 int _num_rands, Scheme_Object **rands, int val);
306static void (*dll_scheme_dont_gc_ptr)(void *p);
307static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
308static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
309 Scheme_Env *env);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000310static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
Bram Moolenaar33570922005-01-25 22:26:29 +0000311 Scheme_Env *env, int all);
312static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000313# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000314static char *(*dll_scheme_format)(char *format, int flen, int argc,
315 Scheme_Object **argv, long *rlen);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000316# else
317static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100318 Scheme_Object **argv, OUTPUT_LEN_TYPE *rlen);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000319static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000320# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000321static void (*dll_scheme_gc_ptr_ok)(void *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000322# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000323static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
324 long *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000325# else
326static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100327 OUTPUT_LEN_TYPE *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000328# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000329static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
330static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
331 Scheme_Env *env);
332static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
333 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
334 mzshort maxa);
335static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000336static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
Bram Moolenaar33570922005-01-25 22:26:29 +0000337 Scheme_Object *cdr);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000338static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
339 const char *name, mzshort mina, mzshort maxa);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000340# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000341static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
342static Scheme_Object *(*dll_scheme_make_string_output_port)();
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000343# else
344static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
345static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
346# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000347static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
348 int argc, Scheme_Object **argv);
349static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
350 Scheme_Object *field_names, int flags, int *count_out);
351static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
352 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
353 int num_uninit_fields, Scheme_Object *uninit_val,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000354 Scheme_Object *properties
355# if MZSCHEME_VERSION_MAJOR >= 299
356 , Scheme_Object *guard
357# endif
358 );
Bram Moolenaar33570922005-01-25 22:26:29 +0000359static Scheme_Object **(*dll_scheme_make_struct_values)(
360 Scheme_Object *struct_type, Scheme_Object **names, int count,
361 int flags);
362static Scheme_Type (*dll_scheme_make_type)(const char *name);
363static Scheme_Object *(*dll_scheme_make_vector)(int size,
364 Scheme_Object *fill);
365static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
366static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
367 const char *who);
368static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
369 Scheme_Env *for_env);
370static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
371static void (*dll_scheme_raise)(Scheme_Object *exn);
372static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
373static void (*dll_scheme_signal_error)(const char *msg, ...);
374static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
375 int which, int argc, Scheme_Object **argv);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000376# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000377static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000378 Scheme_Object *o);
379static Scheme_Config *(*dll_scheme_current_config)(void);
380static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
381 (Scheme_Object *s);
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000382static Scheme_Object *(*dll_scheme_char_string_to_path)
383 (Scheme_Object *s);
Bram Moolenaar75676462013-01-30 14:55:42 +0100384static void *(*dll_scheme_set_collects_path)(Scheme_Object *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000385# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000386static Scheme_Hash_Table *(*dll_scheme_make_hash_table)(int type);
387static void (*dll_scheme_hash_set)(Scheme_Hash_Table *table,
388 Scheme_Object *key, Scheme_Object *value);
389static Scheme_Object *(*dll_scheme_hash_get)(Scheme_Hash_Table *table,
390 Scheme_Object *key);
391static Scheme_Object *(*dll_scheme_make_double)(double d);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000392static Scheme_Object *(*dll_scheme_make_sized_byte_string)(char *chars,
393 long len, int copy);
394static Scheme_Object *(*dll_scheme_namespace_require)(Scheme_Object *req);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100395static Scheme_Object *(*dll_scheme_dynamic_wind)(void (*pre)(void *), Scheme_Object *(* volatile act)(void *), void (* volatile post)(void *), Scheme_Object *(*jmp_handler)(void *), void * volatile data);
396# ifdef MZ_PRECISE_GC
397static void *(*dll_GC_malloc_one_tagged)(size_t size_in_bytes);
398static void (*dll_GC_register_traversers)(short tag, Size_Proc size, Mark_Proc mark, Fixup_Proc fixup, int is_constant_size, int is_atomic);
399# endif
400# if MZSCHEME_VERSION_MAJOR >= 400
401static void (*dll_scheme_init_collection_paths)(Scheme_Env *global_env, Scheme_Object *extra_dirs);
402static void **(*dll_scheme_malloc_immobile_box)(void *p);
403static void (*dll_scheme_free_immobile_box)(void **b);
404# endif
405# if MZSCHEME_VERSION_MAJOR >= 500
406# ifdef TRAMPOLINED_MZVIM_STARTUP
407static int (*dll_scheme_main_setup)(int no_auto_statics, Scheme_Env_Main _main, int argc, char **argv);
408# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
409static void (*dll_scheme_register_tls_space)(void *tls_space, int _tls_index);
410# endif
411# endif
412# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
413static Thread_Local_Variables *(*dll_scheme_external_get_thread_local_variables)(void);
414# endif
415# endif
416# if MZSCHEME_VERSION_MAJOR >= 600
417static void (*dll_scheme_embedded_load)(intptr_t len, const char *s, int predefined);
418static void (*dll_scheme_register_embedded_load)(intptr_t len, const char *s);
419static void (*dll_scheme_set_config_path)(Scheme_Object *p);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000420# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000421
Bram Moolenaar4349c572016-01-30 13:28:28 +0100422#if defined(DYNAMIC_MZSCHEME) /* not when defined(PROTO) */
423
Bram Moolenaar33570922005-01-25 22:26:29 +0000424/* arrays are imported directly */
425# define scheme_eof dll_scheme_eof
426# define scheme_false dll_scheme_false
427# define scheme_void dll_scheme_void
428# define scheme_null dll_scheme_null
429# define scheme_true dll_scheme_true
430
431/* pointers are GetProceAddress'ed as pointers to pointer */
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100432#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
433# define scheme_current_thread (*dll_scheme_current_thread_ptr)
434# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000435# define scheme_console_printf (*dll_scheme_console_printf_ptr)
436# define scheme_console_output (*dll_scheme_console_output_ptr)
437# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
438
439/* and functions in a usual way */
440# define GC_malloc dll_GC_malloc
441# define GC_malloc_atomic dll_GC_malloc_atomic
442
443# define scheme_add_global dll_scheme_add_global
444# define scheme_add_global_symbol dll_scheme_add_global_symbol
445# define scheme_apply dll_scheme_apply
446# define scheme_basic_env dll_scheme_basic_env
447# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000448# if MZSCHEME_VERSION_MAJOR >= 299
449# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100450# define scheme_make_path dll_scheme_make_path
Bram Moolenaar555b2802005-05-19 21:08:39 +0000451# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000452# define scheme_check_threads dll_scheme_check_threads
453# define scheme_close_input_port dll_scheme_close_input_port
454# define scheme_count_lines dll_scheme_count_lines
455# define scheme_current_continuation_marks \
456 dll_scheme_current_continuation_marks
457# define scheme_display dll_scheme_display
458# define scheme_display_to_string dll_scheme_display_to_string
459# define scheme_do_eval dll_scheme_do_eval
460# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000461# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000462# define scheme_eval dll_scheme_eval
463# define scheme_eval_string dll_scheme_eval_string
464# define scheme_eval_string_all dll_scheme_eval_string_all
465# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000466# if MZSCHEME_VERSION_MAJOR < 299
467# define scheme_format dll_scheme_format
468# else
469# define scheme_format_utf8 dll_scheme_format_utf8
470# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000472# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100473# define scheme_get_sized_byte_string_output dll_scheme_get_sized_string_output
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000474# else
475# define scheme_get_sized_byte_string_output \
476 dll_scheme_get_sized_byte_string_output
Bram Moolenaar75676462013-01-30 14:55:42 +0100477# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000478# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479# define scheme_intern_symbol dll_scheme_intern_symbol
480# define scheme_lookup_global dll_scheme_lookup_global
481# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
482# define scheme_make_integer_value dll_scheme_make_integer_value
Bram Moolenaar33570922005-01-25 22:26:29 +0000483# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000484# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000485# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100486# define scheme_make_byte_string dll_scheme_make_string
487# define scheme_make_byte_string_output_port dll_scheme_make_string_output_port
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000488# else
489# define scheme_make_byte_string dll_scheme_make_byte_string
490# define scheme_make_byte_string_output_port \
491 dll_scheme_make_byte_string_output_port
492# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000493# define scheme_make_struct_instance dll_scheme_make_struct_instance
494# define scheme_make_struct_names dll_scheme_make_struct_names
495# define scheme_make_struct_type dll_scheme_make_struct_type
496# define scheme_make_struct_values dll_scheme_make_struct_values
497# define scheme_make_type dll_scheme_make_type
498# define scheme_make_vector dll_scheme_make_vector
499# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
500# define scheme_open_input_file dll_scheme_open_input_file
501# define scheme_primitive_module dll_scheme_primitive_module
502# define scheme_proper_list_length dll_scheme_proper_list_length
503# define scheme_raise dll_scheme_raise
504# define scheme_read dll_scheme_read
505# define scheme_register_static dll_scheme_register_static
506# define scheme_set_stack_base dll_scheme_set_stack_base
507# define scheme_signal_error dll_scheme_signal_error
508# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000509# if MZSCHEME_VERSION_MAJOR >= 299
510# define scheme_set_param dll_scheme_set_param
511# define scheme_current_config dll_scheme_current_config
512# define scheme_char_string_to_byte_string \
513 dll_scheme_char_string_to_byte_string
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000514# define scheme_char_string_to_path \
515 dll_scheme_char_string_to_path
Bram Moolenaar75676462013-01-30 14:55:42 +0100516# define scheme_set_collects_path dll_scheme_set_collects_path
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000517# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000518# define scheme_make_hash_table dll_scheme_make_hash_table
519# define scheme_hash_set dll_scheme_hash_set
520# define scheme_hash_get dll_scheme_hash_get
521# define scheme_make_double dll_scheme_make_double
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100522# define scheme_make_sized_byte_string dll_scheme_make_sized_byte_string
523# define scheme_namespace_require dll_scheme_namespace_require
524# define scheme_dynamic_wind dll_scheme_dynamic_wind
525# ifdef MZ_PRECISE_GC
526# define GC_malloc_one_tagged dll_GC_malloc_one_tagged
527# define GC_register_traversers dll_GC_register_traversers
528# endif
529# if MZSCHEME_VERSION_MAJOR >= 400
530# ifdef TRAMPOLINED_MZVIM_STARTUP
531# define scheme_main_setup dll_scheme_main_setup
532# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
533# define scheme_register_tls_space dll_scheme_register_tls_space
534# endif
535# endif
536# define scheme_init_collection_paths dll_scheme_init_collection_paths
537# define scheme_malloc_immobile_box dll_scheme_malloc_immobile_box
538# define scheme_free_immobile_box dll_scheme_free_immobile_box
539# endif
540# if MZSCHEME_VERSION_MAJOR >= 600
541# define scheme_embedded_load dll_scheme_embedded_load
542# define scheme_register_embedded_load dll_scheme_register_embedded_load
543# define scheme_set_config_path dll_scheme_set_config_path
544# endif
545
546# if MZSCHEME_VERSION_MAJOR >= 500
547# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200548/* define as function for macro in schthread.h */
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100549Thread_Local_Variables *
550scheme_external_get_thread_local_variables(void)
551{
552 return dll_scheme_external_get_thread_local_variables();
553}
554# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000555# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000556
Bram Moolenaar4349c572016-01-30 13:28:28 +0100557#endif
558
Bram Moolenaar33570922005-01-25 22:26:29 +0000559typedef struct
560{
561 char *name;
562 void **ptr;
563} Thunk_Info;
564
565static Thunk_Info mzgc_imports[] = {
566 {"GC_malloc", (void **)&dll_GC_malloc},
567 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
568 {NULL, NULL}};
569
570static Thunk_Info mzsch_imports[] = {
571 {"scheme_eof", (void **)&dll_scheme_eof},
572 {"scheme_false", (void **)&dll_scheme_false},
573 {"scheme_void", (void **)&dll_scheme_void},
574 {"scheme_null", (void **)&dll_scheme_null},
575 {"scheme_true", (void **)&dll_scheme_true},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100576#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
Bram Moolenaar33570922005-01-25 22:26:29 +0000577 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100578#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000579 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
580 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000581 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000582 (void **)&dll_scheme_notify_multithread_ptr},
583 {"scheme_add_global", (void **)&dll_scheme_add_global},
584 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
585 {"scheme_apply", (void **)&dll_scheme_apply},
586 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000587# if MZSCHEME_VERSION_MAJOR >= 299
588 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100589 {"scheme_make_path", (void **)&dll_scheme_make_path},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000590# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000591 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
592 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
593 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
594 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000595 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000596 (void **)&dll_scheme_current_continuation_marks},
597 {"scheme_display", (void **)&dll_scheme_display},
598 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
599 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
600 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000601 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000602 {"scheme_eval", (void **)&dll_scheme_eval},
603 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
604 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000605 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000606 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000607# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000608 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000609# else
610 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000611 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000613 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000614# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000615 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000617# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000618 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000619 (void **)&dll_scheme_get_sized_byte_string_output},
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
622 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000623 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000624 (void **)&dll_scheme_make_closed_prim_w_arity},
625 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
Bram Moolenaar33570922005-01-25 22:26:29 +0000626 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000627 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000628# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000629 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000630 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000631 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000632# else
633 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000634 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000635 (void **)&dll_scheme_make_byte_string_output_port},
636# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000637 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000638 (void **)&dll_scheme_make_struct_instance},
639 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
640 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
641 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
642 {"scheme_make_type", (void **)&dll_scheme_make_type},
643 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
644 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
645 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
646 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
647 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
648 {"scheme_raise", (void **)&dll_scheme_raise},
649 {"scheme_read", (void **)&dll_scheme_read},
650 {"scheme_register_static", (void **)&dll_scheme_register_static},
651 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
652 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
653 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000654# if MZSCHEME_VERSION_MAJOR >= 299
655 {"scheme_set_param", (void **)&dll_scheme_set_param},
656 {"scheme_current_config", (void **)&dll_scheme_current_config},
657 {"scheme_char_string_to_byte_string",
658 (void **)&dll_scheme_char_string_to_byte_string},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000659 {"scheme_char_string_to_path", (void **)&dll_scheme_char_string_to_path},
Bram Moolenaar75676462013-01-30 14:55:42 +0100660 {"scheme_set_collects_path", (void **)&dll_scheme_set_collects_path},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000661# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000662 {"scheme_make_hash_table", (void **)&dll_scheme_make_hash_table},
663 {"scheme_hash_set", (void **)&dll_scheme_hash_set},
664 {"scheme_hash_get", (void **)&dll_scheme_hash_get},
665 {"scheme_make_double", (void **)&dll_scheme_make_double},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000666 {"scheme_make_sized_byte_string", (void **)&dll_scheme_make_sized_byte_string},
667 {"scheme_namespace_require", (void **)&dll_scheme_namespace_require},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100668 {"scheme_dynamic_wind", (void **)&dll_scheme_dynamic_wind},
669# ifdef MZ_PRECISE_GC
670 {"GC_malloc_one_tagged", (void **)&dll_GC_malloc_one_tagged},
671 {"GC_register_traversers", (void **)&dll_GC_register_traversers},
672# endif
673# if MZSCHEME_VERSION_MAJOR >= 400
674# ifdef TRAMPOLINED_MZVIM_STARTUP
675 {"scheme_main_setup", (void **)&dll_scheme_main_setup},
676# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
677 {"scheme_register_tls_space", (void **)&dll_scheme_register_tls_space},
678# endif
679# endif
680 {"scheme_init_collection_paths", (void **)&dll_scheme_init_collection_paths},
681 {"scheme_malloc_immobile_box", (void **)&dll_scheme_malloc_immobile_box},
682 {"scheme_free_immobile_box", (void **)&dll_scheme_free_immobile_box},
683# endif
684# if MZSCHEME_VERSION_MAJOR >= 500
685# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
686 {"scheme_external_get_thread_local_variables", (void **)&dll_scheme_external_get_thread_local_variables},
687# endif
688# endif
689# if MZSCHEME_VERSION_MAJOR >= 600
690 {"scheme_embedded_load", (void **)&dll_scheme_embedded_load},
691 {"scheme_register_embedded_load", (void **)&dll_scheme_register_embedded_load},
692 {"scheme_set_config_path", (void **)&dll_scheme_set_config_path},
693# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000694 {NULL, NULL}};
695
696static HINSTANCE hMzGC = 0;
697static HINSTANCE hMzSch = 0;
698
699static void dynamic_mzscheme_end(void);
700static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
701 int verbose);
702
703 static int
704mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
705{
706 Thunk_Info *thunk = NULL;
707
708 if (hMzGC && hMzSch)
709 return OK;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200710 hMzSch = vimLoadLib(sch_dll);
711 hMzGC = vimLoadLib(gc_dll);
Bram Moolenaar33570922005-01-25 22:26:29 +0000712
Bram Moolenaar33570922005-01-25 22:26:29 +0000713 if (!hMzGC)
714 {
715 if (verbose)
716 EMSG2(_(e_loadlib), gc_dll);
717 return FAIL;
718 }
719
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100720 if (!hMzSch)
721 {
722 if (verbose)
723 EMSG2(_(e_loadlib), sch_dll);
724 return FAIL;
725 }
726
Bram Moolenaar33570922005-01-25 22:26:29 +0000727 for (thunk = mzsch_imports; thunk->name; thunk++)
728 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000729 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
731 {
732 FreeLibrary(hMzSch);
733 hMzSch = 0;
734 FreeLibrary(hMzGC);
735 hMzGC = 0;
736 if (verbose)
737 EMSG2(_(e_loadfunc), thunk->name);
738 return FAIL;
739 }
740 }
741 for (thunk = mzgc_imports; thunk->name; thunk++)
742 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000743 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000744 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
745 {
746 FreeLibrary(hMzSch);
747 hMzSch = 0;
748 FreeLibrary(hMzGC);
749 hMzGC = 0;
750 if (verbose)
751 EMSG2(_(e_loadfunc), thunk->name);
752 return FAIL;
753 }
754 }
755 return OK;
756}
757
758 int
759mzscheme_enabled(int verbose)
760{
761 return mzscheme_runtime_link_init(
762 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
763}
764
765 static void
766dynamic_mzscheme_end(void)
767{
768 if (hMzSch)
769 {
770 FreeLibrary(hMzSch);
771 hMzSch = 0;
772 }
773 if (hMzGC)
774 {
775 FreeLibrary(hMzGC);
776 hMzGC = 0;
777 }
778}
779#endif /* DYNAMIC_MZSCHEME */
780
Bram Moolenaar75676462013-01-30 14:55:42 +0100781#if MZSCHEME_VERSION_MAJOR < 299
782# define GUARANTEED_STRING_ARG(proc, num) GUARANTEE_STRING(proc, num)
783#else
784 static Scheme_Object *
785guaranteed_byte_string_arg(char *proc, int num, int argc, Scheme_Object **argv)
786{
787 if (SCHEME_BYTE_STRINGP(argv[num]))
788 {
789 return argv[num];
790 }
791 else if (SCHEME_CHAR_STRINGP(argv[num]))
792 {
793 Scheme_Object *tmp = NULL;
794 MZ_GC_DECL_REG(2);
795 MZ_GC_VAR_IN_REG(0, argv[num]);
796 MZ_GC_VAR_IN_REG(1, tmp);
797 MZ_GC_REG();
798 tmp = scheme_char_string_to_byte_string(argv[num]);
799 MZ_GC_UNREG();
800 return tmp;
801 }
802 else
803 scheme_wrong_type(proc, "string", num, argc, argv);
804 /* unreachable */
805 return scheme_void;
806}
807# define GUARANTEED_STRING_ARG(proc, num) guaranteed_byte_string_arg(proc, num, argc, argv)
808#endif
809
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000810/* need to put it here for dynamic stuff to work */
Bram Moolenaare484c942009-09-11 10:21:41 +0000811#if defined(INCLUDE_MZSCHEME_BASE)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000812# include "mzscheme_base.c"
813#endif
814
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000815/*
816 *========================================================================
817 * 1. MzScheme interpreter startup
818 *========================================================================
819 */
820
821static Scheme_Type mz_buffer_type;
822static Scheme_Type mz_window_type;
823
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000824static int initialized = FALSE;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100825#ifdef DYNAMIC_MZSCHEME
826static int disabled = FALSE;
827#endif
828static int load_base_module_failed = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000829
830/* global environment */
831static Scheme_Env *environment = NULL;
832/* output/error handlers */
833static Scheme_Object *curout = NULL;
834static Scheme_Object *curerr = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000835/* exn:vim exception */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000836static Scheme_Object *exn_catching_apply = NULL;
837static Scheme_Object *exn_p = NULL;
838static Scheme_Object *exn_message = NULL;
839static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000840
841#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
842static void *stack_base = NULL;
843#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000844
845static long range_start;
846static long range_end;
847
848/* MzScheme threads scheduling stuff */
849static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000850
851#if defined(FEAT_GUI_W32)
Bram Moolenaardec6c7b2016-06-02 11:57:38 +0200852static void CALLBACK timer_proc(HWND, UINT, UINT_PTR, DWORD);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000853static UINT timer_id = 0;
854#elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100855# if GTK_CHECK_VERSION(3,0,0)
856static gboolean timer_proc(gpointer);
857# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000858static gint timer_proc(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100859# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000860static guint timer_id = 0;
861#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
862static void timer_proc(XtPointer, XtIntervalId *);
863static XtIntervalId timer_id = (XtIntervalId)0;
864#elif defined(FEAT_GUI_MAC)
865pascal void timer_proc(EventLoopTimerRef, void *);
866static EventLoopTimerRef timer_id = NULL;
867static EventLoopTimerUPP timerUPP;
868#endif
869
870#ifndef FEAT_GUI_W32 /* Win32 console and Unix */
871 void
872mzvim_check_threads(void)
873{
874 /* Last time MzScheme threads were scheduled */
875 static time_t mz_last_time = 0;
876
877 if (mz_threads_allow && p_mzq > 0)
878 {
879 time_t now = time(NULL);
880
881 if ((now - mz_last_time) * 1000 > p_mzq)
882 {
883 mz_last_time = now;
884 scheme_check_threads();
885 }
886 }
887}
888#endif
889
Bram Moolenaar4349c572016-01-30 13:28:28 +0100890#if defined(MZSCHEME_GUI_THREADS) || defined(PROTO)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000891static void setup_timer(void);
892static void remove_timer(void);
893
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000894/* timers are presented in GUI only */
895# if defined(FEAT_GUI_W32)
896 static void CALLBACK
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200897timer_proc(HWND hwnd UNUSED, UINT uMsg UNUSED, UINT_PTR idEvent UNUSED, DWORD dwTime UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000898# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100899# if GTK_CHECK_VERSION(3,0,0)
900 static gboolean
901# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000902 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100903# endif
Bram Moolenaar64404472010-06-26 06:24:45 +0200904timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000905# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000906 static void
Bram Moolenaar64404472010-06-26 06:24:45 +0200907timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000908# elif defined(FEAT_GUI_MAC)
909 pascal void
Bram Moolenaar64404472010-06-26 06:24:45 +0200910timer_proc(EventLoopTimerRef theTimer UNUSED, void *userData UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000911# endif
912{
913 scheme_check_threads();
914# if defined(FEAT_GUI_GTK)
915 return TRUE; /* continue receiving notifications */
916# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
917 /* renew timeout */
918 if (mz_threads_allow && p_mzq > 0)
919 timer_id = XtAppAddTimeOut(app_context, p_mzq,
920 timer_proc, NULL);
921# endif
922}
923
924 static void
925setup_timer(void)
926{
927# if defined(FEAT_GUI_W32)
928 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
929# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100930# if GTK_CHECK_VERSION(3,0,0)
931 timer_id = g_timeout_add((guint)p_mzq, (GSourceFunc)timer_proc, NULL);
932# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000933 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100934# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000935# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
936 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
937# elif defined(FEAT_GUI_MAC)
938 timerUPP = NewEventLoopTimerUPP(timer_proc);
939 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
940 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
941# endif
942}
943
944 static void
945remove_timer(void)
946{
947# if defined(FEAT_GUI_W32)
948 KillTimer(NULL, timer_id);
949# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100950# if GTK_CHECK_VERSION(3,0,0)
951 g_source_remove(timer_id);
952# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000953 gtk_timeout_remove(timer_id);
Bram Moolenaar98921892016-02-23 17:14:37 +0100954# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000955# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
956 XtRemoveTimeOut(timer_id);
957# elif defined(FEAT_GUI_MAC)
958 RemoveEventLoopTimer(timer_id);
959 DisposeEventLoopTimerUPP(timerUPP);
960# endif
961 timer_id = 0;
962}
963
964 void
965mzvim_reset_timer(void)
966{
967 if (timer_id != 0)
968 remove_timer();
969 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
970 setup_timer();
971}
972
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000973#endif /* MZSCHEME_GUI_THREADS */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000974
975 static void
976notify_multithread(int on)
977{
978 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000979#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000980 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
981 setup_timer();
982 if (!on && timer_id != 0)
983 remove_timer();
984#endif
985}
986
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000987 void
988mzscheme_end(void)
989{
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100990 /* We can not unload the DLL. Racket's thread might be still alive. */
991#if 0
Bram Moolenaar33570922005-01-25 22:26:29 +0000992#ifdef DYNAMIC_MZSCHEME
993 dynamic_mzscheme_end();
994#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100995#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000996}
997
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100998#if HAVE_TLS_SPACE
999# if defined(_MSC_VER)
Bram Moolenaar2d0860d2010-11-03 21:59:30 +01001000static __declspec(thread) void *tls_space;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001001extern intptr_t _tls_index;
1002# elif defined(__MINGW32__)
1003static __thread void *tls_space;
1004extern intptr_t _tls_index;
1005# else
1006static THREAD_LOCAL void *tls_space;
1007static intptr_t _tls_index = 0;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001008# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001009#endif
1010
1011 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001012mzscheme_main()
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001013{
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001014 int argc = 0;
1015 char *argv = NULL;
1016
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001017#ifdef DYNAMIC_MZSCHEME
1018 /*
1019 * Racket requires trampolined startup. We can not load it later.
1020 * If dynamic dll loading is failed, disable it.
1021 */
1022 if (!mzscheme_enabled(FALSE))
1023 {
1024 disabled = TRUE;
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001025 return vim_main2();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001026 }
1027#endif
Bram Moolenaar5c5c9802015-07-10 16:12:48 +02001028#ifdef HAVE_TLS_SPACE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001029 scheme_register_tls_space(&tls_space, _tls_index);
Bram Moolenaar2d0860d2010-11-03 21:59:30 +01001030#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001031#ifdef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001032 return scheme_main_setup(TRUE, mzscheme_env_main, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001033#else
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001034 return mzscheme_env_main(NULL, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001035#endif
1036}
1037
1038 static int
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001039mzscheme_env_main(Scheme_Env *env, int argc, char **argv)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001040{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001041 int vim_main_result;
1042#ifdef TRAMPOLINED_MZVIM_STARTUP
1043 /* Scheme has created the environment for us */
1044 environment = env;
1045#else
1046# ifdef MZ_PRECISE_GC
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001047 Scheme_Object *dummy = NULL;
1048 MZ_GC_DECL_REG(1);
1049 MZ_GC_VAR_IN_REG(0, dummy);
1050
1051 stack_base = &__gc_var_stack__;
1052# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001053 int dummy = 0;
1054 stack_base = (void *)&dummy;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001055# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001056#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001057
1058 /* mzscheme_main is called as a trampoline from main.
1059 * We trampoline into vim_main2
1060 * Passing argc, argv through from mzscheme_main
1061 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001062 vim_main_result = vim_main2();
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001063#if !defined(TRAMPOLINED_MZVIM_STARTUP) && defined(MZ_PRECISE_GC)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001064 /* releasing dummy */
1065 MZ_GC_REG();
1066 MZ_GC_UNREG();
1067#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001068 return vim_main_result;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001069}
1070
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001071 static Scheme_Object*
1072load_base_module(void *data)
1073{
1074 scheme_namespace_require(scheme_intern_symbol((char *)data));
1075 return scheme_null;
1076}
1077
1078 static Scheme_Object *
Bram Moolenaar8b29aba2016-03-28 22:17:16 +02001079load_base_module_on_error(void *data UNUSED)
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001080{
1081 load_base_module_failed = TRUE;
1082 return scheme_null;
1083}
1084
1085 static int
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001086startup_mzscheme(void)
1087{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001088#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001089 scheme_set_stack_base(stack_base, 1);
1090#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001091
Bram Moolenaar75676462013-01-30 14:55:42 +01001092#ifndef TRAMPOLINED_MZVIM_STARTUP
1093 /* in newer versions of precise GC the initial env has been created */
1094 environment = scheme_basic_env();
1095#endif
1096
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001097 MZ_REGISTER_STATIC(environment);
1098 MZ_REGISTER_STATIC(curout);
1099 MZ_REGISTER_STATIC(curerr);
1100 MZ_REGISTER_STATIC(exn_catching_apply);
1101 MZ_REGISTER_STATIC(exn_p);
1102 MZ_REGISTER_STATIC(exn_message);
1103 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001104
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001105 MZ_GC_CHECK();
1106
1107#ifdef INCLUDE_MZSCHEME_BASE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001108 /* invoke function from generated and included mzscheme_base.c */
1109 declare_modules(environment);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001110#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001111
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001112 /* setup 'current-library-collection-paths' parameter */
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001113# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001114 {
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001115 Scheme_Object *coll_path = NULL;
1116 int mustfree = FALSE;
1117 char_u *s;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001118
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001119 MZ_GC_DECL_REG(1);
1120 MZ_GC_VAR_IN_REG(0, coll_path);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001121 MZ_GC_REG();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001122 /* workaround for dynamic loading on windows */
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001123 s = vim_getenv((char_u *)"PLTCOLLECTS", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001124 if (s != NULL)
1125 {
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001126 coll_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001127 MZ_GC_CHECK();
1128 if (mustfree)
1129 vim_free(s);
1130 }
1131# ifdef MZSCHEME_COLLECTS
1132 if (coll_path == NULL)
1133 {
1134 coll_path = scheme_make_path(MZSCHEME_COLLECTS);
1135 MZ_GC_CHECK();
1136 }
Bram Moolenaar39d7d512013-01-31 21:09:15 +01001137# endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001138 if (coll_path != NULL)
1139 {
1140 scheme_set_collects_path(coll_path);
1141 MZ_GC_CHECK();
1142 }
1143 MZ_GC_UNREG();
1144 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001145# else
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001146# ifdef MZSCHEME_COLLECTS
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001147 {
1148 Scheme_Object *coll_string = NULL;
1149 Scheme_Object *coll_pair = NULL;
1150 Scheme_Config *config = NULL;
1151
1152 MZ_GC_DECL_REG(3);
1153 MZ_GC_VAR_IN_REG(0, coll_string);
1154 MZ_GC_VAR_IN_REG(1, coll_pair);
1155 MZ_GC_VAR_IN_REG(2, config);
1156 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001157 coll_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001158 MZ_GC_CHECK();
1159 coll_pair = scheme_make_pair(coll_string, scheme_null);
1160 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001161 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001162 MZ_GC_CHECK();
1163 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
1164 MZ_GC_CHECK();
1165 MZ_GC_UNREG();
1166 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001167# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001168#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001169
1170# if MZSCHEME_VERSION_MAJOR >= 600
1171 {
1172 Scheme_Object *config_path = NULL;
1173 int mustfree = FALSE;
1174 char_u *s;
1175
1176 MZ_GC_DECL_REG(1);
1177 MZ_GC_VAR_IN_REG(0, config_path);
1178 MZ_GC_REG();
1179 /* workaround for dynamic loading on windows */
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001180 s = vim_getenv((char_u *)"PLTCONFIGDIR", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001181 if (s != NULL)
1182 {
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001183 config_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001184 MZ_GC_CHECK();
1185 if (mustfree)
1186 vim_free(s);
1187 }
1188#ifdef MZSCHEME_CONFIGDIR
1189 if (config_path == NULL)
1190 {
1191 config_path = scheme_make_path(MZSCHEME_CONFIGDIR);
1192 MZ_GC_CHECK();
1193 }
1194#endif
1195 if (config_path != NULL)
1196 {
1197 scheme_set_config_path(config_path);
1198 MZ_GC_CHECK();
1199 }
1200 MZ_GC_UNREG();
1201 }
1202# endif
1203
1204#if MZSCHEME_VERSION_MAJOR >= 400
1205 scheme_init_collection_paths(environment, scheme_null);
1206#endif
1207
1208 /*
1209 * versions 4.x do not provide Scheme bindings by default
1210 * we need to add them explicitly
1211 */
1212 {
1213 /* use error handler to avoid abort */
1214 scheme_dynamic_wind(NULL, load_base_module, NULL,
1215 load_base_module_on_error, "racket/base");
1216 if (load_base_module_failed)
1217 {
1218 load_base_module_failed = FALSE;
1219 scheme_dynamic_wind(NULL, load_base_module, NULL,
1220 load_base_module_on_error, "scheme/base");
1221 if (load_base_module_failed)
1222 return -1;
1223 }
1224 }
1225
1226 register_vim_exn();
1227 /* use new environment to initialise exception handling */
1228 init_exn_catching_apply();
1229
1230 /* redirect output */
1231 scheme_console_output = do_output;
1232 scheme_console_printf = do_printf;
1233
Bram Moolenaar555b2802005-05-19 21:08:39 +00001234#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +00001235 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001236 Scheme_Object *make_security_guard = NULL;
1237 MZ_GC_DECL_REG(1);
1238 MZ_GC_VAR_IN_REG(0, make_security_guard);
1239 MZ_GC_REG();
1240
1241#if MZSCHEME_VERSION_MAJOR < 400
1242 {
1243 Scheme_Object *make_security_guard_symbol = NULL;
1244 MZ_GC_DECL_REG(1);
1245 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
1246 MZ_GC_REG();
1247 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
1248 MZ_GC_CHECK();
1249 make_security_guard = scheme_lookup_global(
1250 make_security_guard_symbol, environment);
1251 MZ_GC_UNREG();
1252 }
1253#else
1254 make_security_guard = scheme_builtin_value("make-security-guard");
1255 MZ_GC_CHECK();
1256#endif
1257
1258 /* setup sandbox guards */
1259 if (make_security_guard != NULL)
1260 {
1261 Scheme_Object *args[3] = {NULL, NULL, NULL};
1262 Scheme_Object *guard = NULL;
1263 Scheme_Config *config = NULL;
1264 MZ_GC_DECL_REG(5);
1265 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
1266 MZ_GC_VAR_IN_REG(3, guard);
1267 MZ_GC_VAR_IN_REG(4, config);
1268 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001269 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001270 MZ_GC_CHECK();
1271 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
1272 MZ_GC_CHECK();
1273 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
1274 "sandbox-file-guard", 3, 3);
1275 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1276 "sandbox-network-guard", 4, 4);
1277 guard = scheme_apply(make_security_guard, 3, args);
1278 MZ_GC_CHECK();
1279 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1280 MZ_GC_CHECK();
1281 MZ_GC_UNREG();
1282 }
1283 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001284 }
1285#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001286 /* Create buffer and window types for use in Scheme code */
1287 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001288 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001289 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001290 MZ_GC_CHECK();
1291#ifdef MZ_PRECISE_GC
1292 GC_register_traversers(mz_buffer_type,
1293 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1294 TRUE, TRUE);
1295 GC_register_traversers(mz_window_type,
1296 window_size_proc, window_mark_proc, window_fixup_proc,
1297 TRUE, TRUE);
1298#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001300 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001301
1302 /*
1303 * setup callback to receive notifications
1304 * whether thread scheduling is (or not) required
1305 */
1306 scheme_notify_multithread = notify_multithread;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001307
1308 return 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001309}
1310
1311/*
1312 * This routine is called for each new invocation of MzScheme
1313 * to make sure things are properly initialized.
1314 */
1315 static int
1316mzscheme_init(void)
1317{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001318 if (!initialized)
1319 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001320#ifdef DYNAMIC_MZSCHEME
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001321 if (disabled || !mzscheme_enabled(TRUE))
Bram Moolenaar33570922005-01-25 22:26:29 +00001322 {
Bram Moolenaarb849e712009-06-24 15:51:37 +00001323 EMSG(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
Bram Moolenaar33570922005-01-25 22:26:29 +00001324 return -1;
1325 }
1326#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001327 if (load_base_module_failed || startup_mzscheme())
1328 {
Bram Moolenaar9e3be262016-01-24 13:58:40 +01001329 EMSG(_("E895: Sorry, this command is disabled, the MzScheme's racket/base module could not be loaded."));
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001330 return -1;
1331 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001332 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001333 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001334 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001335 Scheme_Config *config = NULL;
1336 MZ_GC_DECL_REG(1);
1337 MZ_GC_VAR_IN_REG(0, config);
1338 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001339 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001340 MZ_GC_CHECK();
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001341 /* recreate ports each call effectively clearing these ones */
Bram Moolenaar75676462013-01-30 14:55:42 +01001342 curout = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001343 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001344 curerr = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001345 MZ_GC_CHECK();
1346 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1347 MZ_GC_CHECK();
1348 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1349 MZ_GC_CHECK();
1350 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001351 }
1352
1353 return 0;
1354}
1355
1356/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001357 *========================================================================
1358 * 2. External Interface
1359 *========================================================================
1360 */
1361
1362/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001363 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001364 */
1365 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001366eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001367{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001368 Scheme_Object *value = NULL;
1369 Scheme_Object *exn = NULL;
1370 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001371
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001372 MZ_GC_DECL_REG(3);
1373 MZ_GC_VAR_IN_REG(0, value);
1374 MZ_GC_VAR_IN_REG(1, exn);
1375 MZ_GC_VAR_IN_REG(2, prim);
1376 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001377
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001378 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1379 MZ_GC_CHECK();
1380 value = _apply_thunk_catch_exceptions(prim, &exn);
1381 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001382
1383 if (!value)
1384 {
1385 value = extract_exn_message(exn);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001386 /* Got an exn? */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001387 if (value)
1388 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001389 scheme_display(value, curerr); /* Send to stderr-vim */
1390 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001391 do_flush();
1392 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001393 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001394 /* `raise' was called on some arbitrary value */
1395 return FAIL;
1396 }
1397
1398 if (ret != NULL) /* if pointer to retval supported give it up */
1399 *ret = value;
1400 /* Print any result, as long as it's not a void */
1401 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001402 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001403 scheme_display(value, curout); /* Send to stdout-vim */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001404 MZ_GC_CHECK();
1405 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001406
1407 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001408 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001409 return OK;
1410}
1411
1412/* :mzscheme */
1413 static int
1414do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1415{
1416 if (mzscheme_init())
1417 return FAIL;
1418
1419 range_start = eap->line1;
1420 range_end = eap->line2;
1421
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001422 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001423}
1424
1425/*
1426 * Routine called by VIM when deleting a buffer
1427 */
1428 void
1429mzscheme_buffer_free(buf_T *buf)
1430{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001431 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001432 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001433 vim_mz_buffer *bp = NULL;
1434 MZ_GC_DECL_REG(1);
1435 MZ_GC_VAR_IN_REG(0, bp);
1436 MZ_GC_REG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001437
Bram Moolenaar75676462013-01-30 14:55:42 +01001438 bp = BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001439 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001440#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001441 scheme_gc_ptr_ok(bp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001442#else
1443 scheme_free_immobile_box(buf->b_mzscheme_ref);
1444#endif
1445 buf->b_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001446 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001447 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001448 }
1449}
1450
1451/*
1452 * Routine called by VIM when deleting a Window
1453 */
1454 void
1455mzscheme_window_free(win_T *win)
1456{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001457 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001458 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001459 vim_mz_window *wp = NULL;
1460 MZ_GC_DECL_REG(1);
1461 MZ_GC_VAR_IN_REG(0, wp);
1462 MZ_GC_REG();
1463 wp = WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001464 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001465#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001466 scheme_gc_ptr_ok(wp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001467#else
1468 scheme_free_immobile_box(win->w_mzscheme_ref);
1469#endif
1470 win->w_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001471 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001472 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001473 }
1474}
1475
1476/*
1477 * ":mzscheme" (or ":mz")
1478 */
1479 void
1480ex_mzscheme(exarg_T *eap)
1481{
1482 char_u *script;
1483
1484 script = script_get(eap, eap->arg);
1485 if (!eap->skip)
1486 {
1487 if (script == NULL)
1488 do_mzscheme_command(eap, eap->arg, do_eval);
1489 else
1490 {
1491 do_mzscheme_command(eap, script, do_eval);
1492 vim_free(script);
1493 }
1494 }
1495}
1496
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001497 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001498do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001499{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001500 Scheme_Object *expr = NULL;
1501 Scheme_Object *result = NULL;
1502 char *file = NULL;
1503 Port_Info *pinfo = (Port_Info *)data;
1504
1505 MZ_GC_DECL_REG(3);
1506 MZ_GC_VAR_IN_REG(0, expr);
1507 MZ_GC_VAR_IN_REG(1, result);
1508 MZ_GC_VAR_IN_REG(2, file);
1509 MZ_GC_REG();
1510
1511 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1512 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001513
1514 /* make Vim expansion */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001515 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001516 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001517 MZ_GC_CHECK();
1518 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1519 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001520
1521 /* Like REPL but print only last result */
1522 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001523 {
1524 result = scheme_eval(expr, environment);
1525 MZ_GC_CHECK();
1526 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001527
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001528 /* errors will be caught in do_mzscheme_command and ex_mzfile */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001529 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001530 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001531 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001532 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001533 return result;
1534}
1535
1536/* :mzfile */
1537 void
1538ex_mzfile(exarg_T *eap)
1539{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001540 Port_Info pinfo = {NULL, NULL};
1541
1542 MZ_GC_DECL_REG(1);
1543 MZ_GC_VAR_IN_REG(0, pinfo.port);
1544 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001545
1546 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001547 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1548 && pinfo.port != NULL) /* looks like port was not closed */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001549 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001550 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001551 MZ_GC_CHECK();
1552 }
1553 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001554}
1555
1556
1557/*
1558 *========================================================================
1559 * Exception handling code -- cribbed form the MzScheme sources and
1560 * Matthew Flatt's "Inside PLT MzScheme" document.
1561 *========================================================================
1562 */
1563 static void
1564init_exn_catching_apply(void)
1565{
1566 if (!exn_catching_apply)
1567 {
1568 char *e =
1569 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001570 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001571 "(cons #t (thunk))))";
1572
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001573 exn_catching_apply = scheme_eval_string(e, environment);
1574 MZ_GC_CHECK();
1575 exn_p = scheme_builtin_value("exn?");
1576 MZ_GC_CHECK();
1577 exn_message = scheme_builtin_value("exn-message");
1578 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001579 }
1580}
1581
1582/*
1583 * This function applies a thunk, returning the Scheme value if there's
1584 * no exception, otherwise returning NULL and setting *exn to the raised
1585 * value (usually an exn structure).
1586 */
1587 static Scheme_Object *
1588_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1589{
1590 Scheme_Object *v;
1591
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001592 v = _scheme_apply(exn_catching_apply, 1, &f);
1593 /* v is a pair: (cons #t value) or (cons #f exn) */
1594
1595 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1596 return SCHEME_CDR(v);
1597 else
1598 {
1599 *exn = SCHEME_CDR(v);
1600 return NULL;
1601 }
1602}
1603
1604 static Scheme_Object *
1605extract_exn_message(Scheme_Object *v)
1606{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001607 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1608 return _scheme_apply(exn_message, 1, &v);
1609 else
1610 return NULL; /* Not an exn structure */
1611}
1612
1613 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001614do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001615{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001616 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001617}
1618
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001619/*
1620 *========================================================================
1621 * 3. MzScheme I/O Handlers
1622 *========================================================================
1623 */
1624 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001625do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001626{
1627 char *p, *prev;
1628
1629 prev = mesg;
1630 p = strchr(prev, '\n');
1631 while (p)
1632 {
1633 *p = '\0';
1634 if (error)
1635 EMSG(prev);
1636 else
1637 MSG(prev);
1638 prev = p + 1;
1639 p = strchr(prev, '\n');
1640 }
1641
1642 if (error)
1643 EMSG(prev);
1644 else
1645 MSG(prev);
1646}
1647
1648 static void
Bram Moolenaar75676462013-01-30 14:55:42 +01001649do_output(char *mesg, OUTPUT_LEN_TYPE len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001650{
Bram Moolenaar02e14d62012-11-28 15:37:51 +01001651 /* TODO: use len, the string may not be NUL terminated */
Bram Moolenaar64404472010-06-26 06:24:45 +02001652 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001653}
1654
1655 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001656do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001657{
Bram Moolenaar64404472010-06-26 06:24:45 +02001658 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001659}
1660
1661 static void
1662do_printf(char *format, ...)
1663{
Bram Moolenaar64404472010-06-26 06:24:45 +02001664 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001665}
1666
1667 static void
1668do_flush(void)
1669{
1670 char *buff;
Bram Moolenaar75676462013-01-30 14:55:42 +01001671 OUTPUT_LEN_TYPE length;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001672
Bram Moolenaar75676462013-01-30 14:55:42 +01001673 buff = scheme_get_sized_byte_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001674 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001675 if (length)
1676 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001677 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001678 return;
1679 }
1680
Bram Moolenaar75676462013-01-30 14:55:42 +01001681 buff = scheme_get_sized_byte_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001682 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001683 if (length)
1684 do_output(buff, length);
1685}
1686
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001687/*
1688 *========================================================================
1689 * 4. Implementation of the Vim Features for MzScheme
1690 *========================================================================
1691 */
1692
1693/* (command {command-string}) */
1694 static Scheme_Object *
1695vim_command(void *data, int argc, Scheme_Object **argv)
1696{
1697 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001698 Scheme_Object *cmd = NULL;
1699 MZ_GC_DECL_REG(1);
1700 MZ_GC_VAR_IN_REG(0, cmd);
1701 MZ_GC_REG();
1702 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001703
1704 /* may be use do_cmdline_cmd? */
Bram Moolenaar75676462013-01-30 14:55:42 +01001705 do_cmdline(BYTE_STRING_VALUE(cmd), NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001706 update_screen(VALID);
1707
Bram Moolenaar75676462013-01-30 14:55:42 +01001708 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001709 raise_if_error();
1710 return scheme_void;
1711}
1712
1713/* (eval {expr-string}) */
1714 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001715vim_eval(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001716{
1717#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001718 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001719 Scheme_Object *result = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001720 typval_T *vim_result;
Bram Moolenaar75676462013-01-30 14:55:42 +01001721 Scheme_Object *expr = NULL;
1722 MZ_GC_DECL_REG(2);
1723 MZ_GC_VAR_IN_REG(0, result);
1724 MZ_GC_VAR_IN_REG(1, expr);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001725 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001726 expr = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001727
Bram Moolenaar75676462013-01-30 14:55:42 +01001728 vim_result = eval_expr(BYTE_STRING_VALUE(expr), NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001729
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001730 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001731 raise_vim_exn(_("invalid expression"));
1732
Bram Moolenaar75676462013-01-30 14:55:42 +01001733 result = vim_to_mzscheme(vim_result);
1734 MZ_GC_CHECK();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001735 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001736
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001737 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001738 return result;
1739#else
1740 raise_vim_exn(_("expressions disabled at compile time"));
1741 /* unreachable */
1742 return scheme_false;
1743#endif
1744}
1745
1746/* (range-start) */
1747 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001748get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001749{
1750 return scheme_make_integer(range_start);
1751}
1752
1753/* (range-end) */
1754 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001755get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001756{
1757 return scheme_make_integer(range_end);
1758}
1759
1760/* (beep) */
1761 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001762mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001763{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001764 vim_beep(BO_LANG);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001765 return scheme_void;
1766}
1767
1768static Scheme_Object *M_global = NULL;
1769
1770/* (get-option {option-name}) [buffer/window] */
1771 static Scheme_Object *
1772get_option(void *data, int argc, Scheme_Object **argv)
1773{
1774 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001775 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001776 char *strval;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001777 int rc;
Bram Moolenaar75676462013-01-30 14:55:42 +01001778 Scheme_Object *rval = NULL;
1779 Scheme_Object *name = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001780 int opt_flags = 0;
1781 buf_T *save_curb = curbuf;
1782 win_T *save_curw = curwin;
1783
Bram Moolenaar75676462013-01-30 14:55:42 +01001784 MZ_GC_DECL_REG(2);
1785 MZ_GC_VAR_IN_REG(0, rval);
1786 MZ_GC_VAR_IN_REG(1, name);
1787 MZ_GC_REG();
1788
1789 name = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001790
1791 if (argc > 1)
1792 {
1793 if (M_global == NULL)
1794 {
1795 MZ_REGISTER_STATIC(M_global);
1796 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001797 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001798 }
1799
1800 if (argv[1] == M_global)
1801 opt_flags = OPT_GLOBAL;
1802 else if (SCHEME_VIMBUFFERP(argv[1]))
1803 {
1804 curbuf = get_valid_buffer(argv[1]);
1805 opt_flags = OPT_LOCAL;
1806 }
1807 else if (SCHEME_VIMWINDOWP(argv[1]))
1808 {
1809 win_T *win = get_valid_window(argv[1]);
1810
1811 curwin = win;
1812 curbuf = win->w_buffer;
1813 opt_flags = OPT_LOCAL;
1814 }
1815 else
1816 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1817 }
1818
Bram Moolenaar75676462013-01-30 14:55:42 +01001819 rc = get_option_value(BYTE_STRING_VALUE(name), &value, (char_u **)&strval, opt_flags);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001820 curbuf = save_curb;
1821 curwin = save_curw;
1822
1823 switch (rc)
1824 {
1825 case 1:
Bram Moolenaar75676462013-01-30 14:55:42 +01001826 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001827 return scheme_make_integer_value(value);
1828 case 0:
Bram Moolenaar75676462013-01-30 14:55:42 +01001829 rval = scheme_make_byte_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001830 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001831 vim_free(strval);
Bram Moolenaar75676462013-01-30 14:55:42 +01001832 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001833 return rval;
1834 case -1:
1835 case -2:
Bram Moolenaar75676462013-01-30 14:55:42 +01001836 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001837 raise_vim_exn(_("hidden option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001838 case -3:
Bram Moolenaar75676462013-01-30 14:55:42 +01001839 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001840 raise_vim_exn(_("unknown option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001841 }
1842 /* unreachable */
1843 return scheme_void;
1844}
1845
1846/* (set-option {option-changing-string} [buffer/window]) */
1847 static Scheme_Object *
1848set_option(void *data, int argc, Scheme_Object **argv)
1849{
Bram Moolenaar75676462013-01-30 14:55:42 +01001850 char_u *command = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001851 int opt_flags = 0;
1852 buf_T *save_curb = curbuf;
1853 win_T *save_curw = curwin;
1854 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001855 Scheme_Object *cmd = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001856
Bram Moolenaar75676462013-01-30 14:55:42 +01001857 MZ_GC_DECL_REG(1);
1858 MZ_GC_VAR_IN_REG(0, cmd);
1859 MZ_GC_REG();
1860 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
1861
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001862 if (argc > 1)
1863 {
1864 if (M_global == NULL)
1865 {
1866 MZ_REGISTER_STATIC(M_global);
1867 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001868 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001869 }
1870
1871 if (argv[1] == M_global)
1872 opt_flags = OPT_GLOBAL;
1873 else if (SCHEME_VIMBUFFERP(argv[1]))
1874 {
1875 curbuf = get_valid_buffer(argv[1]);
1876 opt_flags = OPT_LOCAL;
1877 }
1878 else if (SCHEME_VIMWINDOWP(argv[1]))
1879 {
1880 win_T *win = get_valid_window(argv[1]);
1881 curwin = win;
1882 curbuf = win->w_buffer;
1883 opt_flags = OPT_LOCAL;
1884 }
1885 else
1886 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1887 }
1888
1889 /* do_set can modify cmd, make copy */
Bram Moolenaar75676462013-01-30 14:55:42 +01001890 command = vim_strsave(BYTE_STRING_VALUE(cmd));
1891 MZ_GC_UNREG();
1892 do_set(command, opt_flags);
1893 vim_free(command);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001894 update_screen(NOT_VALID);
1895 curbuf = save_curb;
1896 curwin = save_curw;
1897 raise_if_error();
1898 return scheme_void;
1899}
1900
1901/*
1902 *===========================================================================
1903 * 5. Vim Window-related Manipulation Functions
1904 *===========================================================================
1905 */
1906
1907/* (curr-win) */
1908 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001909get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001910{
1911 return (Scheme_Object *)get_vim_curr_window();
1912}
1913
1914/* (win-count) */
1915 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001916get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001917{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001918 int n = 0;
Bram Moolenaard2142212013-01-30 17:41:50 +01001919#ifdef FEAT_WINDOWS
1920 win_T *w;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001921
Bram Moolenaar29323592016-07-24 22:04:11 +02001922 FOR_ALL_WINDOWS(w)
Bram Moolenaard2142212013-01-30 17:41:50 +01001923#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001924 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001925 return scheme_make_integer(n);
1926}
1927
1928/* (get-win-list [buffer]) */
1929 static Scheme_Object *
1930get_window_list(void *data, int argc, Scheme_Object **argv)
1931{
1932 Vim_Prim *prim = (Vim_Prim *)data;
1933 vim_mz_buffer *buf;
1934 Scheme_Object *list;
Bram Moolenaard2142212013-01-30 17:41:50 +01001935 win_T *w = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001936
1937 buf = get_buffer_arg(prim->name, 0, argc, argv);
1938 list = scheme_null;
1939
Bram Moolenaard2142212013-01-30 17:41:50 +01001940#ifdef FEAT_WINDOWS
1941 for ( ; w != NULL; w = w->w_next)
1942#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001943 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001944 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001945 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001946 MZ_GC_CHECK();
1947 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001948
1949 return list;
1950}
1951
1952 static Scheme_Object *
1953window_new(win_T *win)
1954{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001955 vim_mz_window *self = NULL;
1956
1957 MZ_GC_DECL_REG(1);
1958 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001959
1960 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001961 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001962 * then we can get at it in win_free() in vim.
1963 *
1964 * On a win_free() we set the Scheme object's win_T *field
1965 * to an invalid value. We trap all uses of a window
1966 * object, and reject them if the win_T *field is invalid.
1967 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001968 if (win->w_mzscheme_ref != NULL)
Bram Moolenaar75676462013-01-30 14:55:42 +01001969 return (Scheme_Object *)WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001970
Bram Moolenaar75676462013-01-30 14:55:42 +01001971 MZ_GC_REG();
1972 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_window));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001973 vim_memset(self, 0, sizeof(vim_mz_window));
Bram Moolenaar75676462013-01-30 14:55:42 +01001974#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001975 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaar75676462013-01-30 14:55:42 +01001976#else
1977 win->w_mzscheme_ref = scheme_malloc_immobile_box(NULL);
1978#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001979 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001980 WINDOW_REF(win) = self;
1981 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001982 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001983 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001984
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001985 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001986 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001987}
1988
1989/* (get-win-num [window]) */
1990 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001991get_window_num(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001992{
Bram Moolenaard2142212013-01-30 17:41:50 +01001993 int nr = 1;
1994#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001995 Vim_Prim *prim = (Vim_Prim *)data;
1996 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001997 win_T *wp;
1998
1999 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaard2142212013-01-30 17:41:50 +01002000#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002001 ++nr;
2002
2003 return scheme_make_integer(nr);
2004}
2005
2006/* (get-win-by-num {windownum}) */
2007 static Scheme_Object *
2008get_window_by_num(void *data, int argc, Scheme_Object **argv)
2009{
2010 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaard2142212013-01-30 17:41:50 +01002011 win_T *win = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002012 int fnum;
2013
2014 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2015 if (fnum < 1)
2016 scheme_signal_error(_("window index is out of range"));
2017
Bram Moolenaard2142212013-01-30 17:41:50 +01002018#ifdef FEAT_WINDOWS
2019 for ( ; win != NULL; win = win->w_next, --fnum)
2020#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002021 if (fnum == 1) /* to be 1-based */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002022 return window_new(win);
2023
2024 return scheme_false;
2025}
2026
2027/* (get-win-buffer [window]) */
2028 static Scheme_Object *
2029get_window_buffer(void *data, int argc, Scheme_Object **argv)
2030{
2031 Vim_Prim *prim = (Vim_Prim *)data;
2032 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2033
2034 return buffer_new(win->win->w_buffer);
2035}
2036
2037/* (get-win-height [window]) */
2038 static Scheme_Object *
2039get_window_height(void *data, int argc, Scheme_Object **argv)
2040{
2041 Vim_Prim *prim = (Vim_Prim *)data;
2042 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2043
2044 return scheme_make_integer(win->win->w_height);
2045}
2046
2047/* (set-win-height {height} [window]) */
2048 static Scheme_Object *
2049set_window_height(void *data, int argc, Scheme_Object **argv)
2050{
2051 Vim_Prim *prim = (Vim_Prim *)data;
2052 vim_mz_window *win;
2053 win_T *savewin;
2054 int height;
2055
2056 win = get_window_arg(prim->name, 1, argc, argv);
2057 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2058
2059#ifdef FEAT_GUI
2060 need_mouse_correct = TRUE;
2061#endif
2062
2063 savewin = curwin;
2064 curwin = win->win;
2065 win_setheight(height);
2066 curwin = savewin;
2067
2068 raise_if_error();
2069 return scheme_void;
2070}
2071
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002072#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002073/* (get-win-width [window]) */
2074 static Scheme_Object *
2075get_window_width(void *data, int argc, Scheme_Object **argv)
2076{
2077 Vim_Prim *prim = (Vim_Prim *)data;
2078 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2079
2080 return scheme_make_integer(W_WIDTH(win->win));
2081}
2082
2083/* (set-win-width {width} [window]) */
2084 static Scheme_Object *
2085set_window_width(void *data, int argc, Scheme_Object **argv)
2086{
2087 Vim_Prim *prim = (Vim_Prim *)data;
2088 vim_mz_window *win;
2089 win_T *savewin;
2090 int width = 0;
2091
2092 win = get_window_arg(prim->name, 1, argc, argv);
2093 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2094
2095# ifdef FEAT_GUI
2096 need_mouse_correct = TRUE;
2097# endif
2098
2099 savewin = curwin;
2100 curwin = win->win;
2101 win_setwidth(width);
2102 curwin = savewin;
2103
2104 raise_if_error();
2105 return scheme_void;
2106}
2107#endif
2108
2109/* (get-cursor [window]) -> (line . col) */
2110 static Scheme_Object *
2111get_cursor(void *data, int argc, Scheme_Object **argv)
2112{
2113 Vim_Prim *prim = (Vim_Prim *)data;
2114 vim_mz_window *win;
2115 pos_T pos;
2116
2117 win = get_window_arg(prim->name, 0, argc, argv);
2118 pos = win->win->w_cursor;
2119 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
2120 scheme_make_integer_value((long)pos.col + 1));
2121}
2122
2123/* (set-cursor (line . col) [window]) */
2124 static Scheme_Object *
2125set_cursor(void *data, int argc, Scheme_Object **argv)
2126{
2127 Vim_Prim *prim = (Vim_Prim *)data;
2128 vim_mz_window *win;
2129 long lnum = 0;
2130 long col = 0;
2131
Bram Moolenaar555b2802005-05-19 21:08:39 +00002132#ifdef HAVE_SANDBOX
2133 sandbox_check();
2134#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002135 win = get_window_arg(prim->name, 1, argc, argv);
2136 GUARANTEE_PAIR(prim->name, 0);
2137
2138 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
2139 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
2140 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
2141
2142 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
2143 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
2144
2145 check_line_range(lnum, win->win->w_buffer);
2146 /* don't know how to catch invalid column value */
2147
2148 win->win->w_cursor.lnum = lnum;
2149 win->win->w_cursor.col = col;
2150 update_screen(VALID);
2151
2152 raise_if_error();
2153 return scheme_void;
2154}
2155/*
2156 *===========================================================================
2157 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002158 *===========================================================================
2159 */
2160
2161/* (open-buff {filename}) */
2162 static Scheme_Object *
2163mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
2164{
2165 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002166 int num = 0;
Bram Moolenaar75676462013-01-30 14:55:42 +01002167 Scheme_Object *onum = NULL;
2168 Scheme_Object *buf = NULL;
2169 Scheme_Object *fname;
2170
2171 MZ_GC_DECL_REG(3);
2172 MZ_GC_VAR_IN_REG(0, onum);
2173 MZ_GC_VAR_IN_REG(1, buf);
2174 MZ_GC_VAR_IN_REG(2, fname);
2175 MZ_GC_REG();
2176 fname = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002177
Bram Moolenaar555b2802005-05-19 21:08:39 +00002178#ifdef HAVE_SANDBOX
2179 sandbox_check();
2180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002181 /* TODO make open existing file */
Bram Moolenaar75676462013-01-30 14:55:42 +01002182 num = buflist_add(BYTE_STRING_VALUE(fname), BLN_LISTED | BLN_CURBUF);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002183
2184 if (num == 0)
2185 raise_vim_exn(_("couldn't open buffer"));
2186
2187 onum = scheme_make_integer(num);
Bram Moolenaar75676462013-01-30 14:55:42 +01002188 buf = get_buffer_by_num(data, 1, &onum);
2189 MZ_GC_UNREG();
2190 return buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002191}
2192
2193/* (get-buff-by-num {buffernum}) */
2194 static Scheme_Object *
2195get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
2196{
2197 Vim_Prim *prim = (Vim_Prim *)data;
2198 buf_T *buf;
2199 int fnum;
2200
2201 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2202
Bram Moolenaar29323592016-07-24 22:04:11 +02002203 FOR_ALL_BUFFERS(buf)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002204 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002205 return buffer_new(buf);
2206
2207 return scheme_false;
2208}
2209
2210/* (get-buff-by-name {buffername}) */
2211 static Scheme_Object *
2212get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
2213{
2214 Vim_Prim *prim = (Vim_Prim *)data;
2215 buf_T *buf;
Bram Moolenaar75676462013-01-30 14:55:42 +01002216 Scheme_Object *buffer = NULL;
2217 Scheme_Object *fname = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002218
Bram Moolenaar75676462013-01-30 14:55:42 +01002219 MZ_GC_DECL_REG(2);
2220 MZ_GC_VAR_IN_REG(0, buffer);
2221 MZ_GC_VAR_IN_REG(1, fname);
2222 MZ_GC_REG();
2223 fname = GUARANTEED_STRING_ARG(prim->name, 0);
2224 buffer = scheme_false;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002225
Bram Moolenaar29323592016-07-24 22:04:11 +02002226 FOR_ALL_BUFFERS(buf)
Bram Moolenaar75676462013-01-30 14:55:42 +01002227 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002228 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2229 /* empty string */
2230 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002231 if (BYTE_STRING_VALUE(fname)[0] == NUL)
2232 buffer = buffer_new(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002233 }
Bram Moolenaar75676462013-01-30 14:55:42 +01002234 else if (!fnamecmp(buf->b_ffname, BYTE_STRING_VALUE(fname))
2235 || !fnamecmp(buf->b_sfname, BYTE_STRING_VALUE(fname)))
2236 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002237 /* either short or long filename matches */
Bram Moolenaar75676462013-01-30 14:55:42 +01002238 buffer = buffer_new(buf);
2239 }
2240 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002241
Bram Moolenaar75676462013-01-30 14:55:42 +01002242 MZ_GC_UNREG();
2243 return buffer;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002244}
2245
2246/* (get-next-buff [buffer]) */
2247 static Scheme_Object *
2248get_next_buffer(void *data, int argc, Scheme_Object **argv)
2249{
2250 Vim_Prim *prim = (Vim_Prim *)data;
2251 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2252
2253 if (buf->b_next == NULL)
2254 return scheme_false;
2255 else
2256 return buffer_new(buf->b_next);
2257}
2258
2259/* (get-prev-buff [buffer]) */
2260 static Scheme_Object *
2261get_prev_buffer(void *data, int argc, Scheme_Object **argv)
2262{
2263 Vim_Prim *prim = (Vim_Prim *)data;
2264 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2265
2266 if (buf->b_prev == NULL)
2267 return scheme_false;
2268 else
2269 return buffer_new(buf->b_prev);
2270}
2271
2272/* (get-buff-num [buffer]) */
2273 static Scheme_Object *
2274get_buffer_num(void *data, int argc, Scheme_Object **argv)
2275{
2276 Vim_Prim *prim = (Vim_Prim *)data;
2277 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2278
2279 return scheme_make_integer(buf->buf->b_fnum);
2280}
2281
2282/* (buff-count) */
2283 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002284get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002285{
2286 buf_T *b;
2287 int n = 0;
2288
Bram Moolenaar29323592016-07-24 22:04:11 +02002289 FOR_ALL_BUFFERS(b) ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002290 return scheme_make_integer(n);
2291}
2292
2293/* (get-buff-name [buffer]) */
2294 static Scheme_Object *
2295get_buffer_name(void *data, int argc, Scheme_Object **argv)
2296{
2297 Vim_Prim *prim = (Vim_Prim *)data;
2298 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2299
Bram Moolenaar75676462013-01-30 14:55:42 +01002300 return scheme_make_byte_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002301}
2302
2303/* (curr-buff) */
2304 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002305get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002306{
2307 return (Scheme_Object *)get_vim_curr_buffer();
2308}
2309
2310 static Scheme_Object *
2311buffer_new(buf_T *buf)
2312{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002313 vim_mz_buffer *self = NULL;
2314
2315 MZ_GC_DECL_REG(1);
2316 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002317
2318 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00002319 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002320 * then we can get at it in buf_freeall() in vim.
2321 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00002322 if (buf->b_mzscheme_ref)
Bram Moolenaar75676462013-01-30 14:55:42 +01002323 return (Scheme_Object *)BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002324
Bram Moolenaar75676462013-01-30 14:55:42 +01002325 MZ_GC_REG();
2326 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_buffer));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002327 vim_memset(self, 0, sizeof(vim_mz_buffer));
Bram Moolenaar75676462013-01-30 14:55:42 +01002328#ifndef MZ_PRECISE_GC
2329 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
2330#else
2331 buf->b_mzscheme_ref = scheme_malloc_immobile_box(NULL);
2332#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002333 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01002334 BUFFER_REF(buf) = self;
2335 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002336 self->buf = buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002337 self->so.type = mz_buffer_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002338
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002339 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01002340 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002341}
2342
2343/*
2344 * (get-buff-size [buffer])
2345 *
2346 * Get the size (number of lines) in the current buffer.
2347 */
2348 static Scheme_Object *
2349get_buffer_size(void *data, int argc, Scheme_Object **argv)
2350{
2351 Vim_Prim *prim = (Vim_Prim *)data;
2352 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2353
2354 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
2355}
2356
2357/*
2358 * (get-buff-line {linenr} [buffer])
2359 *
2360 * Get a line from the specified buffer. The line number is
2361 * in Vim format (1-based). The line is returned as a MzScheme
2362 * string object.
2363 */
2364 static Scheme_Object *
2365get_buffer_line(void *data, int argc, Scheme_Object **argv)
2366{
2367 Vim_Prim *prim = (Vim_Prim *)data;
2368 vim_mz_buffer *buf;
2369 int linenr;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002370 char_u *line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002371
2372 buf = get_buffer_arg(prim->name, 1, argc, argv);
2373 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2374 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2375
2376 raise_if_error();
Bram Moolenaar75676462013-01-30 14:55:42 +01002377 return scheme_make_byte_string((char *)line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002378}
2379
2380
2381/*
2382 * (get-buff-line-list {start} {end} [buffer])
2383 *
2384 * Get a list of lines from the specified buffer. The line numbers
2385 * are in Vim format (1-based). The range is from lo up to, but not
2386 * including, hi. The list is returned as a list of string objects.
2387 */
2388 static Scheme_Object *
2389get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2390{
2391 Vim_Prim *prim = (Vim_Prim *)data;
2392 vim_mz_buffer *buf;
2393 int i, hi, lo, n;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002394 Scheme_Object *list = NULL;
2395
2396 MZ_GC_DECL_REG(1);
2397 MZ_GC_VAR_IN_REG(0, list);
2398 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002399
2400 buf = get_buffer_arg(prim->name, 2, argc, argv);
2401 list = scheme_null;
2402 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2403 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2404
2405 /*
2406 * Handle some error conditions
2407 */
2408 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002409 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002410
2411 if (hi < 0)
2412 hi = 0;
2413 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002414 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002415
2416 n = hi - lo;
2417
2418 for (i = n; i >= 0; --i)
2419 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002420 Scheme_Object *str = scheme_make_byte_string(
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002421 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2422 raise_if_error();
2423
2424 /* Set the list item */
2425 list = scheme_make_pair(str, list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002426 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002427 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002428 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002429 return list;
2430}
2431
2432/*
2433 * (set-buff-line {linenr} {string/#f} [buffer])
2434 *
2435 * Replace a line in the specified buffer. The line number is
2436 * in Vim format (1-based). The replacement line is given as
2437 * an MzScheme string object. The object is checked for validity
2438 * and correct format. An exception is thrown if the values are not
2439 * the correct format.
2440 *
2441 * It returns a Scheme Object that indicates the length of the
2442 * string changed.
2443 */
2444 static Scheme_Object *
2445set_buffer_line(void *data, int argc, Scheme_Object **argv)
2446{
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002447 /* First of all, we check the value of the supplied MzScheme object.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002448 * There are three cases:
2449 * 1. #f - this is a deletion.
2450 * 2. A string - this is a replacement.
2451 * 3. Anything else - this is an error.
2452 */
2453 Vim_Prim *prim = (Vim_Prim *)data;
2454 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002455 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002456 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002457 int n;
2458
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002459 MZ_GC_DECL_REG(1);
2460 MZ_GC_VAR_IN_REG(0, line);
2461 MZ_GC_REG();
2462
Bram Moolenaar555b2802005-05-19 21:08:39 +00002463#ifdef HAVE_SANDBOX
2464 sandbox_check();
2465#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002466 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2467 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002468 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002469 line = argv[1];
2470 buf = get_buffer_arg(prim->name, 2, argc, argv);
2471
2472 check_line_range(n, buf->buf);
2473
2474 if (SCHEME_FALSEP(line))
2475 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002476 buf_T *savebuf = curbuf;
2477
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002478 curbuf = buf->buf;
2479
2480 if (u_savedel((linenr_T)n, 1L) == FAIL)
2481 {
2482 curbuf = savebuf;
2483 raise_vim_exn(_("cannot save undo information"));
2484 }
2485 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2486 {
2487 curbuf = savebuf;
2488 raise_vim_exn(_("cannot delete line"));
2489 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002490 if (buf->buf == curwin->w_buffer)
2491 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002492 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002493
2494 curbuf = savebuf;
2495
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002496 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002497 raise_if_error();
2498 return scheme_void;
2499 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002500 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002501 {
2502 /* Otherwise it's a line */
2503 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002504
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002505 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002506
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002507 curbuf = buf->buf;
2508
2509 if (u_savesub((linenr_T)n) == FAIL)
2510 {
2511 curbuf = savebuf;
2512 vim_free(save);
2513 raise_vim_exn(_("cannot save undo information"));
2514 }
2515 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2516 {
2517 curbuf = savebuf;
2518 vim_free(save);
2519 raise_vim_exn(_("cannot replace line"));
2520 }
2521 else
2522 {
2523 vim_free(save);
2524 changed_bytes((linenr_T)n, 0);
2525 }
2526
2527 curbuf = savebuf;
2528
2529 /* Check that the cursor is not beyond the end of the line now. */
2530 if (buf->buf == curwin->w_buffer)
2531 check_cursor_col();
2532
2533 MZ_GC_UNREG();
2534 raise_if_error();
2535 return scheme_void;
2536 }
2537}
2538
2539 static void
2540free_array(char **array)
2541{
2542 char **curr = array;
2543 while (*curr != NULL)
2544 vim_free(*curr++);
2545 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002546}
2547
2548/*
2549 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2550 *
2551 * Replace a range of lines in the specified buffer. The line numbers are in
2552 * Vim format (1-based). The range is from lo up to, but not including, hi.
2553 * The replacement lines are given as a Scheme list of string objects. The
2554 * list is checked for validity and correct format.
2555 *
2556 * Errors are returned as a value of FAIL. The return value is OK on success.
2557 * If OK is returned and len_change is not NULL, *len_change is set to the
2558 * change in the buffer length.
2559 */
2560 static Scheme_Object *
2561set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2562{
2563 /* First of all, we check the type of the supplied MzScheme object.
2564 * There are three cases:
2565 * 1. #f - this is a deletion.
2566 * 2. A list - this is a replacement.
2567 * 3. Anything else - this is an error.
2568 */
2569 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002570 vim_mz_buffer *buf = NULL;
2571 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002572 int i, old_len, new_len, hi, lo;
2573 long extra;
2574
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002575 MZ_GC_DECL_REG(1);
2576 MZ_GC_VAR_IN_REG(0, line_list);
2577 MZ_GC_REG();
2578
Bram Moolenaar555b2802005-05-19 21:08:39 +00002579#ifdef HAVE_SANDBOX
2580 sandbox_check();
2581#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002582 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2583 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2584 if (!SCHEME_PAIRP(argv[2])
2585 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2586 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2587 line_list = argv[2];
2588 buf = get_buffer_arg(prim->name, 3, argc, argv);
2589 old_len = hi - lo;
2590 if (old_len < 0) /* process inverse values wisely */
2591 {
2592 i = lo;
2593 lo = hi;
2594 hi = i;
2595 old_len = -old_len;
2596 }
2597 extra = 0;
2598
2599 check_line_range(lo, buf->buf); /* inclusive */
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002600 check_line_range(hi - 1, buf->buf); /* exclusive */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002601
2602 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2603 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002604 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002605 curbuf = buf->buf;
2606
2607 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2608 {
2609 curbuf = savebuf;
2610 raise_vim_exn(_("cannot save undo information"));
2611 }
2612 else
2613 {
2614 for (i = 0; i < old_len; i++)
2615 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2616 {
2617 curbuf = savebuf;
2618 raise_vim_exn(_("cannot delete line"));
2619 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002620 if (buf->buf == curwin->w_buffer)
2621 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002622 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002623 }
2624
2625 curbuf = savebuf;
2626
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002627 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002628 raise_if_error();
2629 return scheme_void;
2630 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002631 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002632 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002633 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002634
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002635 /* List */
2636 new_len = scheme_proper_list_length(line_list);
2637 MZ_GC_CHECK();
2638 if (new_len < 0) /* improper or cyclic list */
2639 scheme_wrong_type(prim->name, "proper list",
2640 2, argc, argv);
2641 else
2642 {
2643 char **array = NULL;
2644 Scheme_Object *line = NULL;
2645 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002646
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002647 MZ_GC_DECL_REG(2);
2648 MZ_GC_VAR_IN_REG(0, line);
2649 MZ_GC_VAR_IN_REG(1, rest);
2650 MZ_GC_REG();
2651
Bram Moolenaar75676462013-01-30 14:55:42 +01002652 array = (char **)alloc((new_len+1)* sizeof(char *));
2653 vim_memset(array, 0, (new_len+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002654
2655 rest = line_list;
2656 for (i = 0; i < new_len; ++i)
2657 {
2658 line = SCHEME_CAR(rest);
2659 rest = SCHEME_CDR(rest);
2660 if (!SCHEME_STRINGP(line))
2661 {
2662 free_array(array);
2663 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2664 }
2665 array[i] = string_to_line(line);
2666 }
2667
2668 curbuf = buf->buf;
2669
2670 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2671 {
2672 curbuf = savebuf;
2673 free_array(array);
2674 raise_vim_exn(_("cannot save undo information"));
2675 }
2676
2677 /*
2678 * If the size of the range is reducing (ie, new_len < old_len) we
2679 * need to delete some old_len. We do this at the start, by
2680 * repeatedly deleting line "lo".
2681 */
2682 for (i = 0; i < old_len - new_len; ++i)
2683 {
2684 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2685 {
2686 curbuf = savebuf;
2687 free_array(array);
2688 raise_vim_exn(_("cannot delete line"));
2689 }
2690 extra--;
2691 }
2692
2693 /*
2694 * For as long as possible, replace the existing old_len with the
2695 * new old_len. This is a more efficient operation, as it requires
2696 * less memory allocation and freeing.
2697 */
2698 for (i = 0; i < old_len && i < new_len; i++)
2699 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2700 {
2701 curbuf = savebuf;
2702 free_array(array);
2703 raise_vim_exn(_("cannot replace line"));
2704 }
2705
2706 /*
2707 * Now we may need to insert the remaining new_len. We don't need to
2708 * free the string passed back because MzScheme has control of that
2709 * memory.
2710 */
2711 while (i < new_len)
2712 {
2713 if (ml_append((linenr_T)(lo + i - 1),
2714 (char_u *)array[i], 0, FALSE) == FAIL)
2715 {
2716 curbuf = savebuf;
2717 free_array(array);
2718 raise_vim_exn(_("cannot insert line"));
2719 }
2720 ++i;
2721 ++extra;
2722 }
2723 MZ_GC_UNREG();
2724 free_array(array);
2725 }
2726
2727 /*
2728 * Adjust marks. Invalidate any which lie in the
2729 * changed range, and move any in the remainder of the buffer.
2730 */
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002731 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2732 (long)MAXLNUM, (long)extra);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002733 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2734
2735 if (buf->buf == curwin->w_buffer)
2736 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002737 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002738
2739 MZ_GC_UNREG();
2740 raise_if_error();
2741 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002742 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002743}
2744
2745/*
2746 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2747 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002748 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002749 * The line number is in Vim format (1-based). The lines to be inserted are
2750 * given as an MzScheme list of string objects or as a single string. The lines
2751 * to be added are checked for validity and correct format. Errors are
2752 * returned as a value of FAIL. The return value is OK on success.
2753 * If OK is returned and len_change is not NULL, *len_change
2754 * is set to the change in the buffer length.
2755 */
2756 static Scheme_Object *
2757insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2758{
2759 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002760 vim_mz_buffer *buf = NULL;
2761 Scheme_Object *list = NULL;
2762 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002763 int i, n, size;
2764
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002765 MZ_GC_DECL_REG(1);
2766 MZ_GC_VAR_IN_REG(0, list);
2767 MZ_GC_REG();
2768
Bram Moolenaar555b2802005-05-19 21:08:39 +00002769#ifdef HAVE_SANDBOX
2770 sandbox_check();
2771#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002772 /*
2773 * First of all, we check the type of the supplied MzScheme object.
2774 * It must be a string or a list, or the call is in error.
2775 */
2776 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2777 list = argv[1];
2778
2779 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2780 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2781 buf = get_buffer_arg(prim->name, 2, argc, argv);
2782
2783 if (n != 0) /* 0 can be used in insert */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002784 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002785 if (SCHEME_STRINGP(list))
2786 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002787 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002788
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002789 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002790 curbuf = buf->buf;
2791
2792 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2793 {
2794 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002795 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002796 raise_vim_exn(_("cannot save undo information"));
2797 }
2798 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2799 {
2800 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002801 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002802 raise_vim_exn(_("cannot insert line"));
2803 }
2804 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002805 {
2806 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002807 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002808 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002809
2810 curbuf = savebuf;
2811 update_screen(VALID);
2812
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002813 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002814 raise_if_error();
2815 return scheme_void;
2816 }
2817
2818 /* List */
2819 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002820 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002821 if (size < 0) /* improper or cyclic list */
2822 scheme_wrong_type(prim->name, "proper list",
2823 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002824 else
2825 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002826 Scheme_Object *line = NULL;
2827 Scheme_Object *rest = NULL;
2828 char **array;
2829 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002830
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002831 MZ_GC_DECL_REG(2);
2832 MZ_GC_VAR_IN_REG(0, line);
2833 MZ_GC_VAR_IN_REG(1, rest);
2834 MZ_GC_REG();
2835
Bram Moolenaar75676462013-01-30 14:55:42 +01002836 array = (char **)alloc((size+1) * sizeof(char *));
2837 vim_memset(array, 0, (size+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002838
2839 rest = list;
2840 for (i = 0; i < size; ++i)
2841 {
2842 line = SCHEME_CAR(rest);
2843 rest = SCHEME_CDR(rest);
2844 array[i] = string_to_line(line);
2845 }
2846
2847 curbuf = buf->buf;
2848
2849 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2850 {
2851 curbuf = savebuf;
2852 free_array(array);
2853 raise_vim_exn(_("cannot save undo information"));
2854 }
2855 else
2856 {
2857 for (i = 0; i < size; ++i)
2858 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2859 0, FALSE) == FAIL)
2860 {
2861 curbuf = savebuf;
2862 free_array(array);
2863 raise_vim_exn(_("cannot insert line"));
2864 }
2865
2866 if (i > 0)
2867 appended_lines_mark((linenr_T)n, (long)i);
2868 }
2869 free_array(array);
2870 MZ_GC_UNREG();
2871 curbuf = savebuf;
2872 update_screen(VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002873 }
2874
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002875 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002876 raise_if_error();
2877 return scheme_void;
2878}
2879
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002880/*
2881 * Predicates
2882 */
2883/* (buff? obj) */
2884 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002885vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002886{
2887 if (SCHEME_VIMBUFFERP(argv[0]))
2888 return scheme_true;
2889 else
2890 return scheme_false;
2891}
2892
2893/* (win? obj) */
2894 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002895vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002896{
2897 if (SCHEME_VIMWINDOWP(argv[0]))
2898 return scheme_true;
2899 else
2900 return scheme_false;
2901}
2902
2903/* (buff-valid? obj) */
2904 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002905vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002906{
2907 if (SCHEME_VIMBUFFERP(argv[0])
2908 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2909 return scheme_true;
2910 else
2911 return scheme_false;
2912}
2913
2914/* (win-valid? obj) */
2915 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002916vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002917{
2918 if (SCHEME_VIMWINDOWP(argv[0])
2919 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2920 return scheme_true;
2921 else
2922 return scheme_false;
2923}
2924
2925/*
2926 *===========================================================================
2927 * Utilities
2928 *===========================================================================
2929 */
2930
2931/*
2932 * Convert an MzScheme string into a Vim line.
2933 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002934 * All internal nulls are replaced by newline characters.
2935 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002936 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002937 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002938 */
2939 static char *
2940string_to_line(Scheme_Object *obj)
2941{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002942 char *scheme_str = NULL;
2943 char *vim_str = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01002944 OUTPUT_LEN_TYPE len;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002945 int i;
2946
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002947 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002948
2949 /* Error checking: String must not contain newlines, as we
2950 * are replacing a single line, and we must replace it with
2951 * a single line.
2952 */
Bram Moolenaar75676462013-01-30 14:55:42 +01002953 if (memchr(scheme_str, '\n', len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002954 scheme_signal_error(_("string cannot contain newlines"));
2955
Bram Moolenaar75676462013-01-30 14:55:42 +01002956 vim_str = (char *)alloc(len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002957
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002958 /* Create a copy of the string, with internal nulls replaced by
2959 * newline characters, as is the vim convention.
2960 */
2961 for (i = 0; i < len; ++i)
2962 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002963 if (scheme_str[i] == '\0')
2964 vim_str[i] = '\n';
2965 else
2966 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002967 }
2968
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002969 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002970
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002971 MZ_GC_CHECK();
2972 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002973}
2974
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002975#ifdef FEAT_EVAL
2976/*
2977 * Convert Vim value into MzScheme, adopted from if_python.c
2978 */
2979 static Scheme_Object *
Bram Moolenaar75676462013-01-30 14:55:42 +01002980vim_to_mzscheme(typval_T *vim_value)
2981{
2982 Scheme_Object *result = NULL;
2983 /* hash table to store visited values to avoid infinite loops */
2984 Scheme_Hash_Table *visited = NULL;
2985
2986 MZ_GC_DECL_REG(2);
2987 MZ_GC_VAR_IN_REG(0, result);
2988 MZ_GC_VAR_IN_REG(1, visited);
2989 MZ_GC_REG();
2990
2991 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2992 MZ_GC_CHECK();
2993
2994 result = vim_to_mzscheme_impl(vim_value, 1, visited);
2995
2996 MZ_GC_UNREG();
2997 return result;
2998}
2999
3000 static Scheme_Object *
3001vim_to_mzscheme_impl(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003002{
3003 Scheme_Object *result = NULL;
3004 int new_value = TRUE;
3005
Bram Moolenaar75676462013-01-30 14:55:42 +01003006 MZ_GC_DECL_REG(2);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003007 MZ_GC_VAR_IN_REG(0, result);
Bram Moolenaar75676462013-01-30 14:55:42 +01003008 MZ_GC_VAR_IN_REG(1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003009 MZ_GC_REG();
3010
3011 /* Avoid infinite recursion */
3012 if (depth > 100)
3013 {
3014 MZ_GC_UNREG();
3015 return scheme_void;
3016 }
3017
3018 /* Check if we run into a recursive loop. The item must be in visited
3019 * then and we can use it again.
3020 */
3021 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
3022 MZ_GC_CHECK();
3023 if (result != NULL) /* found, do nothing */
3024 new_value = FALSE;
3025 else if (vim_value->v_type == VAR_STRING)
3026 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003027 result = scheme_make_byte_string((char *)vim_value->vval.v_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003028 MZ_GC_CHECK();
3029 }
3030 else if (vim_value->v_type == VAR_NUMBER)
3031 {
3032 result = scheme_make_integer((long)vim_value->vval.v_number);
3033 MZ_GC_CHECK();
3034 }
3035# ifdef FEAT_FLOAT
3036 else if (vim_value->v_type == VAR_FLOAT)
3037 {
3038 result = scheme_make_double((double)vim_value->vval.v_float);
3039 MZ_GC_CHECK();
3040 }
3041# endif
3042 else if (vim_value->v_type == VAR_LIST)
3043 {
3044 list_T *list = vim_value->vval.v_list;
3045 listitem_T *curr;
3046
3047 if (list == NULL || list->lv_first == NULL)
3048 result = scheme_null;
3049 else
3050 {
3051 Scheme_Object *obj = NULL;
3052
3053 MZ_GC_DECL_REG(1);
3054 MZ_GC_VAR_IN_REG(0, obj);
3055 MZ_GC_REG();
3056
3057 curr = list->lv_last;
Bram Moolenaar75676462013-01-30 14:55:42 +01003058 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003059 result = scheme_make_pair(obj, scheme_null);
3060 MZ_GC_CHECK();
3061
3062 while (curr != list->lv_first)
3063 {
3064 curr = curr->li_prev;
Bram Moolenaar75676462013-01-30 14:55:42 +01003065 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003066 result = scheme_make_pair(obj, result);
3067 MZ_GC_CHECK();
3068 }
3069 }
3070 MZ_GC_UNREG();
3071 }
3072 else if (vim_value->v_type == VAR_DICT)
3073 {
3074 Scheme_Object *key = NULL;
3075 Scheme_Object *obj = NULL;
3076
3077 MZ_GC_DECL_REG(2);
3078 MZ_GC_VAR_IN_REG(0, key);
3079 MZ_GC_VAR_IN_REG(1, obj);
3080 MZ_GC_REG();
3081
3082 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
3083 MZ_GC_CHECK();
3084 if (vim_value->vval.v_dict != NULL)
3085 {
3086 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
3087 long_u todo = ht->ht_used;
3088 hashitem_T *hi;
3089 dictitem_T *di;
3090
3091 for (hi = ht->ht_array; todo > 0; ++hi)
3092 {
3093 if (!HASHITEM_EMPTY(hi))
3094 {
3095 --todo;
3096
3097 di = dict_lookup(hi);
Bram Moolenaar75676462013-01-30 14:55:42 +01003098 obj = vim_to_mzscheme_impl(&di->di_tv, depth + 1, visited);
3099 key = scheme_make_byte_string((char *)hi->hi_key);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003100 MZ_GC_CHECK();
3101 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
3102 MZ_GC_CHECK();
3103 }
3104 }
3105 }
3106 MZ_GC_UNREG();
3107 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003108 else if (vim_value->v_type == VAR_FUNC)
3109 {
3110 Scheme_Object *funcname = NULL;
3111
3112 MZ_GC_DECL_REG(1);
3113 MZ_GC_VAR_IN_REG(0, funcname);
3114 MZ_GC_REG();
3115
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003116 /* FIXME: func_ref() and func_unref() are needed. */
Bram Moolenaar75676462013-01-30 14:55:42 +01003117 funcname = scheme_make_byte_string((char *)vim_value->vval.v_string);
3118 MZ_GC_CHECK();
3119 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3120 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3121 MZ_GC_CHECK();
3122
3123 MZ_GC_UNREG();
3124 }
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003125 else if (vim_value->v_type == VAR_PARTIAL)
3126 {
3127 if (vim_value->vval.v_partial == NULL)
3128 result = scheme_null;
3129 else
3130 {
3131 Scheme_Object *funcname = NULL;
3132
3133 MZ_GC_DECL_REG(1);
3134 MZ_GC_VAR_IN_REG(0, funcname);
3135 MZ_GC_REG();
3136
3137 /* FIXME: func_ref() and func_unref() are needed. */
3138 /* TODO: Support pt_dict and pt_argv. */
3139 funcname = scheme_make_byte_string(
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003140 (char *)partial_name(vim_value->vval.v_partial));
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003141 MZ_GC_CHECK();
3142 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3143 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3144 MZ_GC_CHECK();
3145
3146 MZ_GC_UNREG();
3147 }
3148 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003149 else if (vim_value->v_type == VAR_SPECIAL)
3150 {
3151 if (vim_value->vval.v_number <= VVAL_TRUE)
3152 result = scheme_make_integer((long)vim_value->vval.v_number);
3153 else
3154 result = scheme_null;
3155 MZ_GC_CHECK();
3156 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003157 else
3158 {
3159 result = scheme_void;
3160 new_value = FALSE;
3161 }
3162 if (new_value)
3163 {
3164 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
3165 MZ_GC_CHECK();
3166 }
3167 MZ_GC_UNREG();
3168 return result;
3169}
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003170
3171 static int
Bram Moolenaar75676462013-01-30 14:55:42 +01003172mzscheme_to_vim(Scheme_Object *obj, typval_T *tv)
3173{
3174 int i, status;
3175 Scheme_Hash_Table *visited = NULL;
3176
3177 MZ_GC_DECL_REG(2);
3178 MZ_GC_VAR_IN_REG(0, obj);
3179 MZ_GC_VAR_IN_REG(1, visited);
3180 MZ_GC_REG();
3181
3182 visited = scheme_make_hash_table(SCHEME_hash_ptr);
3183 MZ_GC_CHECK();
3184
3185 status = mzscheme_to_vim_impl(obj, tv, 1, visited);
3186 for (i = 0; i < visited->size; ++i)
3187 {
3188 /* free up remembered objects */
3189 if (visited->vals[i] != NULL)
3190 free_tv((typval_T *)visited->vals[i]);
3191 }
3192
3193 MZ_GC_UNREG();
3194 return status;
3195}
3196 static int
3197mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003198 Scheme_Hash_Table *visited)
3199{
3200 int status = OK;
3201 typval_T *found;
Bram Moolenaar75676462013-01-30 14:55:42 +01003202
3203 MZ_GC_DECL_REG(2);
3204 MZ_GC_VAR_IN_REG(0, obj);
3205 MZ_GC_VAR_IN_REG(1, visited);
3206 MZ_GC_REG();
3207
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003208 MZ_GC_CHECK();
3209 if (depth > 100) /* limit the deepest recursion level */
3210 {
3211 tv->v_type = VAR_NUMBER;
3212 tv->vval.v_number = 0;
3213 return FAIL;
3214 }
3215
3216 found = (typval_T *)scheme_hash_get(visited, obj);
3217 if (found != NULL)
3218 copy_tv(found, tv);
3219 else if (SCHEME_VOIDP(obj))
3220 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003221 tv->v_type = VAR_SPECIAL;
3222 tv->vval.v_number = VVAL_NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003223 }
3224 else if (SCHEME_INTP(obj))
3225 {
3226 tv->v_type = VAR_NUMBER;
3227 tv->vval.v_number = SCHEME_INT_VAL(obj);
3228 }
3229 else if (SCHEME_BOOLP(obj))
3230 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003231 tv->v_type = VAR_SPECIAL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003232 tv->vval.v_number = SCHEME_TRUEP(obj);
3233 }
3234# ifdef FEAT_FLOAT
3235 else if (SCHEME_DBLP(obj))
3236 {
3237 tv->v_type = VAR_FLOAT;
3238 tv->vval.v_float = SCHEME_DBL_VAL(obj);
3239 }
3240# endif
Bram Moolenaar75676462013-01-30 14:55:42 +01003241 else if (SCHEME_BYTE_STRINGP(obj))
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003242 {
3243 tv->v_type = VAR_STRING;
Bram Moolenaar75676462013-01-30 14:55:42 +01003244 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(obj));
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003245 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003246# if MZSCHEME_VERSION_MAJOR >= 299
3247 else if (SCHEME_CHAR_STRINGP(obj))
3248 {
3249 Scheme_Object *tmp = NULL;
3250 MZ_GC_DECL_REG(1);
3251 MZ_GC_VAR_IN_REG(0, tmp);
3252 MZ_GC_REG();
3253
3254 tmp = scheme_char_string_to_byte_string(obj);
3255 tv->v_type = VAR_STRING;
3256 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(tmp));
3257 MZ_GC_UNREG();
3258 }
3259#endif
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003260 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
3261 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3262 {
3263 list_T *list = list_alloc();
3264 if (list == NULL)
3265 status = FAIL;
3266 else
3267 {
3268 int i;
3269 Scheme_Object *curr = NULL;
3270 Scheme_Object *cval = NULL;
3271 /* temporary var to hold current element of vectors and pairs */
3272 typval_T *v;
3273
3274 MZ_GC_DECL_REG(2);
3275 MZ_GC_VAR_IN_REG(0, curr);
3276 MZ_GC_VAR_IN_REG(1, cval);
3277 MZ_GC_REG();
3278
3279 tv->v_type = VAR_LIST;
3280 tv->vval.v_list = list;
3281 ++list->lv_refcount;
3282
3283 v = (typval_T *)alloc(sizeof(typval_T));
3284 if (v == NULL)
3285 status = FAIL;
3286 else
3287 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003288 /* add the value in advance to allow handling of self-referential
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003289 * data structures */
3290 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3291 copy_tv(tv, visited_tv);
3292 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3293
3294 if (SCHEME_VECTORP(obj))
3295 {
3296 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
3297 {
3298 cval = SCHEME_VEC_ELS(obj)[i];
Bram Moolenaar75676462013-01-30 14:55:42 +01003299 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003300 if (status == FAIL)
3301 break;
3302 status = list_append_tv(list, v);
3303 clear_tv(v);
3304 if (status == FAIL)
3305 break;
3306 }
3307 }
3308 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3309 {
3310 for (curr = obj;
3311 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
3312 curr = SCHEME_CDR(curr))
3313 {
3314 cval = SCHEME_CAR(curr);
Bram Moolenaar75676462013-01-30 14:55:42 +01003315 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003316 if (status == FAIL)
3317 break;
3318 status = list_append_tv(list, v);
3319 clear_tv(v);
3320 if (status == FAIL)
3321 break;
3322 }
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003323 /* improper list not terminated with null
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003324 * need to handle the last element */
3325 if (status == OK && !SCHEME_NULLP(curr))
3326 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003327 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003328 if (status == OK)
3329 {
3330 status = list_append_tv(list, v);
3331 clear_tv(v);
3332 }
3333 }
3334 }
3335 /* nothing to do for scheme_null */
3336 vim_free(v);
3337 }
3338 MZ_GC_UNREG();
3339 }
3340 }
3341 else if (SCHEME_HASHTP(obj))
3342 {
3343 int i;
3344 dict_T *dict;
3345 Scheme_Object *key = NULL;
3346 Scheme_Object *val = NULL;
3347
3348 MZ_GC_DECL_REG(2);
3349 MZ_GC_VAR_IN_REG(0, key);
3350 MZ_GC_VAR_IN_REG(1, val);
3351 MZ_GC_REG();
3352
3353 dict = dict_alloc();
3354 if (dict == NULL)
3355 status = FAIL;
3356 else
3357 {
3358 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3359
3360 tv->v_type = VAR_DICT;
3361 tv->vval.v_dict = dict;
3362 ++dict->dv_refcount;
3363
3364 copy_tv(tv, visited_tv);
3365 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3366
3367 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
3368 {
3369 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
3370 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003371 /* generate item for `display'ed Scheme key */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003372 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
3373 ((Scheme_Hash_Table *) obj)->keys[i]));
3374 /* convert Scheme val to Vim and add it to the dict */
Bram Moolenaar75676462013-01-30 14:55:42 +01003375 if (mzscheme_to_vim_impl(((Scheme_Hash_Table *) obj)->vals[i],
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003376 &item->di_tv, depth + 1, visited) == FAIL
3377 || dict_add(dict, item) == FAIL)
3378 {
3379 dictitem_free(item);
3380 status = FAIL;
3381 break;
3382 }
3383 }
3384
3385 }
3386 }
3387 MZ_GC_UNREG();
3388 }
3389 else
3390 {
3391 /* `display' any other value to string */
3392 tv->v_type = VAR_STRING;
3393 tv->vval.v_string = (char_u *)string_to_line(obj);
3394 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003395 MZ_GC_UNREG();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003396 return status;
3397}
3398
Bram Moolenaar75676462013-01-30 14:55:42 +01003399/* Scheme prim procedure wrapping Vim funcref */
3400 static Scheme_Object *
3401vim_funcref(void *name, int argc, Scheme_Object **argv)
3402{
3403 int i;
3404 typval_T args;
3405 int status = OK;
3406 Scheme_Object *result = NULL;
3407 list_T *list = list_alloc();
3408
3409 MZ_GC_DECL_REG(1);
3410 MZ_GC_VAR_IN_REG(0, result);
3411 MZ_GC_REG();
3412
3413 result = scheme_void;
3414 if (list == NULL)
3415 status = FAIL;
3416 else
3417 {
3418 args.v_type = VAR_LIST;
3419 args.vval.v_list = list;
3420 ++list->lv_refcount;
3421 for (i = 0; status == OK && i < argc; ++i)
3422 {
3423 typval_T *v = (typval_T *)alloc(sizeof(typval_T));
3424 if (v == NULL)
3425 status = FAIL;
3426 else
3427 {
3428 status = mzscheme_to_vim(argv[i], v);
3429 if (status == OK)
3430 {
3431 status = list_append_tv(list, v);
3432 clear_tv(v);
3433 }
3434 vim_free(v);
3435 }
3436 }
3437 if (status == OK)
3438 {
3439 typval_T ret;
3440 ret.v_type = VAR_UNKNOWN;
3441
3442 mzscheme_call_vim(BYTE_STRING_VALUE((Scheme_Object *)name), &args, &ret);
3443 MZ_GC_CHECK();
3444 result = vim_to_mzscheme(&ret);
3445 clear_tv(&ret);
3446 MZ_GC_CHECK();
3447 }
3448 }
3449 clear_tv(&args);
3450 MZ_GC_UNREG();
3451 if (status != OK)
3452 raise_vim_exn(_("error converting Scheme values to Vim"));
3453 else
3454 raise_if_error();
3455 return result;
3456}
3457
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003458 void
3459do_mzeval(char_u *str, typval_T *rettv)
3460{
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003461 Scheme_Object *ret = NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003462
Bram Moolenaar75676462013-01-30 14:55:42 +01003463 MZ_GC_DECL_REG(1);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003464 MZ_GC_VAR_IN_REG(0, ret);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003465 MZ_GC_REG();
3466
3467 if (mzscheme_init())
3468 {
3469 MZ_GC_UNREG();
3470 return;
3471 }
3472
3473 MZ_GC_CHECK();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003474 if (eval_with_exn_handling(str, do_eval, &ret) == OK)
Bram Moolenaar75676462013-01-30 14:55:42 +01003475 mzscheme_to_vim(ret, rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003476
3477 MZ_GC_UNREG();
3478}
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003479#endif
3480
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003481/*
3482 * Check to see whether a Vim error has been reported, or a keyboard
3483 * interrupt (from vim --> got_int) has been detected.
3484 */
3485 static int
3486vim_error_check(void)
3487{
3488 return (got_int || did_emsg);
3489}
3490
3491/*
3492 * register Scheme exn:vim
3493 */
3494 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003495register_vim_exn(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003496{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003497 int nc = 0;
3498 int i;
3499 Scheme_Object *struct_exn = NULL;
3500 Scheme_Object *exn_name = NULL;
3501
3502 MZ_GC_DECL_REG(2);
3503 MZ_GC_VAR_IN_REG(0, struct_exn);
3504 MZ_GC_VAR_IN_REG(1, exn_name);
3505 MZ_GC_REG();
3506
3507 exn_name = scheme_intern_symbol("exn:vim");
3508 MZ_GC_CHECK();
3509 struct_exn = scheme_builtin_value("struct:exn");
3510 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003511
3512 if (vim_exn == NULL)
3513 vim_exn = scheme_make_struct_type(exn_name,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003514 struct_exn, NULL, 0, 0, NULL, NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003515#if MZSCHEME_VERSION_MAJOR >= 299
3516 , NULL
3517#endif
3518 );
3519
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003520
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003521 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003522 Scheme_Object **tmp = NULL;
3523 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
3524 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
3525 MZ_GC_DECL_REG(6);
3526 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
3527 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
3528 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003529
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003530 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003531 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
3532 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003533
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003534 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
3535 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
3536 MZ_GC_CHECK();
3537
3538 for (i = 0; i < nc; i++)
3539 {
3540 scheme_add_global_symbol(exn_names[i],
3541 exn_values[i], environment);
3542 MZ_GC_CHECK();
3543 }
3544 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003545 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003546 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003547}
3548
3549/*
3550 * raise exn:vim, may be with additional info string
3551 */
3552 void
3553raise_vim_exn(const char *add_info)
3554{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003555 char *fmt = _("Vim error: ~a");
3556 Scheme_Object *argv[2] = {NULL, NULL};
3557 Scheme_Object *exn = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01003558 Scheme_Object *byte_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003559
Bram Moolenaar75676462013-01-30 14:55:42 +01003560 MZ_GC_DECL_REG(5);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003561 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
3562 MZ_GC_VAR_IN_REG(3, exn);
Bram Moolenaar75676462013-01-30 14:55:42 +01003563 MZ_GC_VAR_IN_REG(4, byte_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003564 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003565
3566 if (add_info != NULL)
3567 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003568 char *c_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003569 Scheme_Object *info = NULL;
3570
3571 MZ_GC_DECL_REG(3);
3572 MZ_GC_VAR_IN_REG(0, c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003573 MZ_GC_VAR_IN_REG(2, info);
3574 MZ_GC_REG();
3575
Bram Moolenaar75676462013-01-30 14:55:42 +01003576 info = scheme_make_byte_string(add_info);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003577 MZ_GC_CHECK();
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02003578 c_string = scheme_format_utf8(fmt, (int)STRLEN(fmt), 1, &info, NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003579 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01003580 byte_string = scheme_make_byte_string(c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003581 MZ_GC_CHECK();
3582 argv[0] = scheme_byte_string_to_char_string(byte_string);
Bram Moolenaar555b2802005-05-19 21:08:39 +00003583 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003584 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003585 }
3586 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003587 {
3588 byte_string = scheme_make_byte_string(_("Vim error"));
3589 MZ_GC_CHECK();
3590 argv[0] = scheme_byte_string_to_char_string(byte_string);
3591 MZ_GC_CHECK();
3592 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003593 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003594
Bram Moolenaar049377e2007-05-12 15:32:12 +00003595#if MZSCHEME_VERSION_MAJOR < 360
3596 argv[1] = scheme_current_continuation_marks();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003597 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003598#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003599 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003600 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003601#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003602
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003603 exn = scheme_make_struct_instance(vim_exn, 2, argv);
3604 MZ_GC_CHECK();
3605 scheme_raise(exn);
3606 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003607}
3608
3609 void
3610raise_if_error(void)
3611{
3612 if (vim_error_check())
3613 raise_vim_exn(NULL);
3614}
3615
3616/* get buffer:
3617 * either current
3618 * or passed as argv[argnum] with checks
3619 */
3620 static vim_mz_buffer *
3621get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3622{
3623 vim_mz_buffer *b;
3624
3625 if (argc < argnum + 1)
3626 return get_vim_curr_buffer();
3627 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3628 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3629 b = (vim_mz_buffer *)argv[argnum];
3630 (void)get_valid_buffer(argv[argnum]);
3631 return b;
3632}
3633
3634/* get window:
3635 * either current
3636 * or passed as argv[argnum] with checks
3637 */
3638 static vim_mz_window *
3639get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3640{
3641 vim_mz_window *w;
3642
3643 if (argc < argnum + 1)
3644 return get_vim_curr_window();
3645 w = (vim_mz_window *)argv[argnum];
3646 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3647 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3648 (void)get_valid_window(argv[argnum]);
3649 return w;
3650}
3651
3652/* get valid Vim buffer from Scheme_Object* */
3653buf_T *get_valid_buffer(void *obj)
3654{
3655 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3656
3657 if (buf == INVALID_BUFFER_VALUE)
3658 scheme_signal_error(_("buffer is invalid"));
3659 return buf;
3660}
3661
3662/* get valid Vim window from Scheme_Object* */
3663win_T *get_valid_window(void *obj)
3664{
3665 win_T *win = ((vim_mz_window *)obj)->win;
3666 if (win == INVALID_WINDOW_VALUE)
3667 scheme_signal_error(_("window is invalid"));
3668 return win;
3669}
3670
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003671 int
3672mzthreads_allowed(void)
3673{
3674 return mz_threads_allow;
3675}
3676
3677 static int
3678line_in_range(linenr_T lnum, buf_T *buf)
3679{
3680 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3681}
3682
3683 static void
3684check_line_range(linenr_T lnum, buf_T *buf)
3685{
3686 if (!line_in_range(lnum, buf))
3687 scheme_signal_error(_("linenr out of range"));
3688}
3689
3690/*
3691 * Check if deleting lines made the cursor position invalid
3692 * (or you'll get msg from Vim about invalid linenr).
3693 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3694 * deleted). Got from if_python.c
3695 */
3696 static void
3697mz_fix_cursor(int lo, int hi, int extra)
3698{
3699 if (curwin->w_cursor.lnum >= lo)
3700 {
3701 /* Adjust the cursor position if it's in/after the changed
3702 * lines. */
3703 if (curwin->w_cursor.lnum >= hi)
3704 {
3705 curwin->w_cursor.lnum += extra;
3706 check_cursor_col();
3707 }
3708 else if (extra < 0)
3709 {
3710 curwin->w_cursor.lnum = lo;
3711 check_cursor();
3712 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003713 else
3714 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003715 changed_cline_bef_curs();
3716 }
3717 invalidate_botline();
3718}
3719
3720static Vim_Prim prims[]=
3721{
3722 /*
3723 * Buffer-related commands
3724 */
3725 {get_buffer_line, "get-buff-line", 1, 2},
3726 {set_buffer_line, "set-buff-line", 2, 3},
3727 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3728 {get_buffer_name, "get-buff-name", 0, 1},
3729 {get_buffer_num, "get-buff-num", 0, 1},
3730 {get_buffer_size, "get-buff-size", 0, 1},
3731 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3732 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3733 {get_curr_buffer, "curr-buff", 0, 0},
3734 {get_buffer_count, "buff-count", 0, 0},
3735 {get_next_buffer, "get-next-buff", 0, 1},
3736 {get_prev_buffer, "get-prev-buff", 0, 1},
3737 {mzscheme_open_buffer, "open-buff", 1, 1},
3738 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3739 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003740 /*
3741 * Window-related commands
3742 */
3743 {get_curr_win, "curr-win", 0, 0},
3744 {get_window_count, "win-count", 0, 0},
3745 {get_window_by_num, "get-win-by-num", 1, 1},
3746 {get_window_num, "get-win-num", 0, 1},
3747 {get_window_buffer, "get-win-buffer", 0, 1},
3748 {get_window_height, "get-win-height", 0, 1},
3749 {set_window_height, "set-win-height", 1, 2},
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003750#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003751 {get_window_width, "get-win-width", 0, 1},
3752 {set_window_width, "set-win-width", 1, 2},
3753#endif
3754 {get_cursor, "get-cursor", 0, 1},
3755 {set_cursor, "set-cursor", 1, 2},
3756 {get_window_list, "get-win-list", 0, 1},
3757 /*
3758 * Vim-related commands
3759 */
3760 {vim_command, "command", 1, 1},
3761 {vim_eval, "eval", 1, 1},
3762 {get_range_start, "range-start", 0, 0},
3763 {get_range_end, "range-end", 0, 0},
3764 {mzscheme_beep, "beep", 0, 0},
3765 {get_option, "get-option", 1, 2},
3766 {set_option, "set-option", 1, 2},
3767 /*
3768 * small utilities
3769 */
3770 {vim_bufferp, "buff?", 1, 1},
3771 {vim_windowp, "win?", 1, 1},
3772 {vim_buffer_validp, "buff-valid?", 1, 1},
3773 {vim_window_validp, "win-valid?", 1, 1}
3774};
3775
3776/* return MzScheme wrapper for curbuf */
3777 static vim_mz_buffer *
3778get_vim_curr_buffer(void)
3779{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003780 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003781 return (vim_mz_buffer *)buffer_new(curbuf);
3782 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003783 return BUFFER_REF(curbuf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003784}
3785
3786/* return MzScheme wrapper for curwin */
3787 static vim_mz_window *
3788get_vim_curr_window(void)
3789{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003790 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003791 return (vim_mz_window *)window_new(curwin);
3792 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003793 return WINDOW_REF(curwin);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003794}
3795
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003796 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003797make_modules(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003798{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003799 int i;
3800 Scheme_Env *mod = NULL;
3801 Scheme_Object *vimext_symbol = NULL;
3802 Scheme_Object *closed_prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003803
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003804 MZ_GC_DECL_REG(3);
3805 MZ_GC_VAR_IN_REG(0, mod);
3806 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3807 MZ_GC_VAR_IN_REG(2, closed_prim);
3808 MZ_GC_REG();
3809
3810 vimext_symbol = scheme_intern_symbol("vimext");
3811 MZ_GC_CHECK();
3812 mod = scheme_primitive_module(vimext_symbol, environment);
3813 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003814 /* all prims made closed so they can access their own names */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003815 for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003816 {
3817 Vim_Prim *prim = prims + i;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003818 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3819 prim->mina, prim->maxa);
3820 scheme_add_global(prim->name, closed_prim, mod);
3821 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003822 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003823 scheme_finish_primitive_module(mod);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003824 MZ_GC_CHECK();
3825 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003826}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003827
Bram Moolenaar555b2802005-05-19 21:08:39 +00003828#ifdef HAVE_SANDBOX
3829static Scheme_Object *M_write = NULL;
3830static Scheme_Object *M_read = NULL;
3831static Scheme_Object *M_execute = NULL;
3832static Scheme_Object *M_delete = NULL;
3833
3834 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003835sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003836{
3837 if (sandbox)
3838 raise_vim_exn(_("not allowed in the Vim sandbox"));
3839}
3840
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003841/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003842 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003843sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003844{
3845 if (sandbox)
3846 {
3847 Scheme_Object *requested_access = argv[2];
3848
3849 if (M_write == NULL)
3850 {
3851 MZ_REGISTER_STATIC(M_write);
3852 M_write = scheme_intern_symbol("write");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003853 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003854 }
3855 if (M_read == NULL)
3856 {
3857 MZ_REGISTER_STATIC(M_read);
3858 M_read = scheme_intern_symbol("read");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003859 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003860 }
3861 if (M_execute == NULL)
3862 {
3863 MZ_REGISTER_STATIC(M_execute);
3864 M_execute = scheme_intern_symbol("execute");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003865 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003866 }
3867 if (M_delete == NULL)
3868 {
3869 MZ_REGISTER_STATIC(M_delete);
3870 M_delete = scheme_intern_symbol("delete");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003871 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003872 }
3873
3874 while (!SCHEME_NULLP(requested_access))
3875 {
3876 Scheme_Object *item = SCHEME_CAR(requested_access);
3877 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3878 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
3879 {
3880 raise_vim_exn(_("not allowed in the Vim sandbox"));
3881 }
3882 requested_access = SCHEME_CDR(requested_access);
3883 }
3884 }
3885 return scheme_void;
3886}
3887
3888 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003889sandbox_network_guard(int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003890{
3891 return scheme_void;
3892}
3893#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00003894
3895#endif