blob: bf0157a59e00722dc4bd9c425ce662829defdeb4 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002 *
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003 * MzScheme interface by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar75676462013-01-30 14:55:42 +01004 * Based on work by Brent Fulgham <bfulgham@debian.org>
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005 * (Based on lots of help from Matthew Flatt)
6 *
7 * This consists of six parts:
8 * 1. MzScheme interpreter main program
9 * 2. Routines that handle the external interface between MzScheme and
10 * Vim.
11 * 3. MzScheme input/output handlers: writes output via [e]msg().
12 * 4. Implementation of the Vim Features for MzScheme
13 * 5. Vim Window-related Manipulation Functions.
14 * 6. Vim Buffer-related Manipulation Functions
15 *
16 * NOTES
17 * 1. Memory, allocated with scheme_malloc*, need not to be freed explicitly,
18 * garbage collector will do it self
19 * 2. Requires at least NORMAL features. I can't imagine why one may want
20 * to build with SMALL or TINY features but with MzScheme interface.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000021 * 3. I don't use K&R-style functions. Anyways, MzScheme headers are ANSI.
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022 */
23
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024#include "vim.h"
Bram Moolenaar049377e2007-05-12 15:32:12 +000025
Bram Moolenaar325b7a22004-07-05 15:58:32 +000026#include "if_mzsch.h"
27
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010028// Only do the following when the feature is enabled. Needed for "make
29// depend".
Bram Moolenaar76b92b22006-03-24 22:46:53 +000030#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 */
Bram Moolenaar4f974752019-02-17 17:44:42 +010050#if MZSCHEME_VERSION_MAJOR >= 500 && defined(MSWIN) \
Bram Moolenaar4e640bd2016-01-16 16:20:38 +010051 && 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 Moolenaar2ab2e862019-12-04 21:24:53 +010066// Base data structures
Bram Moolenaar325b7a22004-07-05 15:58:32 +000067#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;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010093 int mina; // arity information
Bram Moolenaar325b7a22004-07-05 15:58:32 +000094 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 Moolenaar2ab2e862019-12-04 21:24:53 +0100118// Buffer-related commands
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000119static Scheme_Object *buffer_new(buf_T *buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000120static Scheme_Object *get_buffer_by_num(void *, int, Scheme_Object **);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000121static vim_mz_buffer *get_vim_curr_buffer(void);
122
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100123// Window-related commands
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000124static Scheme_Object *window_new(win_T *win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000125static vim_mz_window *get_vim_curr_window(void);
126
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000127/*
128 *========================================================================
129 * Internal Function Prototypes
130 *========================================================================
131 */
132static int vim_error_check(void);
133static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100134static int startup_mzscheme(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000135static char *string_to_line(Scheme_Object *obj);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100136#if MZSCHEME_VERSION_MAJOR >= 501
Bram Moolenaar75676462013-01-30 14:55:42 +0100137# define OUTPUT_LEN_TYPE intptr_t
138#else
139# define OUTPUT_LEN_TYPE long
140#endif
141static void do_output(char *mesg, OUTPUT_LEN_TYPE len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000142static void do_printf(char *format, ...);
143static void do_flush(void);
144static Scheme_Object *_apply_thunk_catch_exceptions(
145 Scheme_Object *, Scheme_Object **);
146static Scheme_Object *extract_exn_message(Scheme_Object *v);
147static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
148static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000149static void register_vim_exn(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000150static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
151 int argc, Scheme_Object **argv);
152static vim_mz_window *get_window_arg(const char *fname, int argnum,
153 int argc, Scheme_Object **argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000154static int line_in_range(linenr_T, buf_T *);
155static void check_line_range(linenr_T, buf_T *);
156static void mz_fix_cursor(int lo, int hi, int extra);
157
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000158static int eval_with_exn_handling(void *, Scheme_Closed_Prim *,
159 Scheme_Object **ret);
160static void make_modules(void);
161static void init_exn_catching_apply(void);
162static int mzscheme_env_main(Scheme_Env *env, int argc, char **argv);
163static int mzscheme_init(void);
164#ifdef FEAT_EVAL
Bram Moolenaar75676462013-01-30 14:55:42 +0100165static Scheme_Object *vim_to_mzscheme(typval_T *vim_value);
166static Scheme_Object *vim_to_mzscheme_impl(typval_T *vim_value, int depth,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000167 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100168static int mzscheme_to_vim(Scheme_Object *obj, typval_T *tv);
169static int mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100170 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100171static Scheme_Object *vim_funcref(void *data, int argc, Scheme_Object **argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000172#endif
173
174#ifdef MZ_PRECISE_GC
Bram Moolenaar64404472010-06-26 06:24:45 +0200175static int buffer_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000176{
177 return gcBYTES_TO_WORDS(sizeof(vim_mz_buffer));
178}
179static int buffer_mark_proc(void *obj)
180{
181 return buffer_size_proc(obj);
182}
183static int buffer_fixup_proc(void *obj)
184{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100185 // apparently not needed as the object will be uncollectable while
186 // the buffer is alive
187 // vim_mz_buffer* buf = (vim_mz_buffer*) obj;
188 // buf->buf->b_mzscheme_ref = GC_fixup_self(obj);
189
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000190 return buffer_size_proc(obj);
191}
Bram Moolenaar64404472010-06-26 06:24:45 +0200192static int window_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000193{
194 return gcBYTES_TO_WORDS(sizeof(vim_mz_window));
195}
196static int window_mark_proc(void *obj)
197{
198 return window_size_proc(obj);
199}
200static int window_fixup_proc(void *obj)
201{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100202 // apparently not needed as the object will be uncollectable while
203 // the window is alive
204 // vim_mz_window* win = (vim_mz_window*) obj;
205 // win->win->w_mzscheme_ref = GC_fixup_self(obj);
206 //
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000207 return window_size_proc(obj);
208}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100209// with precise GC, w_mzscheme_ref and b_mzscheme_ref are immobile boxes
210// containing pointers to a window/buffer
211// with conservative GC these are simply pointers
Bram Moolenaar75676462013-01-30 14:55:42 +0100212# define WINDOW_REF(win) *(vim_mz_window **)((win)->w_mzscheme_ref)
213# define BUFFER_REF(buf) *(vim_mz_buffer **)((buf)->b_mzscheme_ref)
214#else
215# define WINDOW_REF(win) (vim_mz_window *)((win)->w_mzscheme_ref)
216# define BUFFER_REF(buf) (vim_mz_buffer *)((buf)->b_mzscheme_ref)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000217#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000218
Bram Moolenaar4349c572016-01-30 13:28:28 +0100219#if defined(DYNAMIC_MZSCHEME) || defined(PROTO)
Bram Moolenaar33570922005-01-25 22:26:29 +0000220static Scheme_Object *dll_scheme_eof;
221static Scheme_Object *dll_scheme_false;
222static Scheme_Object *dll_scheme_void;
223static Scheme_Object *dll_scheme_null;
224static Scheme_Object *dll_scheme_true;
225
226static Scheme_Thread **dll_scheme_current_thread_ptr;
227
228static void (**dll_scheme_console_printf_ptr)(char *str, ...);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100229static void (**dll_scheme_console_output_ptr)(char *str, OUTPUT_LEN_TYPE len);
Bram Moolenaar33570922005-01-25 22:26:29 +0000230static void (**dll_scheme_notify_multithread_ptr)(int on);
231
232static void *(*dll_GC_malloc)(size_t size_in_bytes);
233static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
234static Scheme_Env *(*dll_scheme_basic_env)(void);
235static void (*dll_scheme_check_threads)(void);
236static void (*dll_scheme_register_static)(void *ptr, long size);
237static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
238static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
239 Scheme_Env *env);
240static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
241 Scheme_Object *val, Scheme_Env *env);
242static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
243 Scheme_Object **rands);
244static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000245# if MZSCHEME_VERSION_MAJOR >= 299
246static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100247static Scheme_Object *(*dll_scheme_make_path)(const char *chars);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000248# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000249static void (*dll_scheme_close_input_port)(Scheme_Object *port);
250static void (*dll_scheme_count_lines)(Scheme_Object *port);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000251#if MZSCHEME_VERSION_MAJOR < 360
Bram Moolenaar33570922005-01-25 22:26:29 +0000252static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000253#else
254static Scheme_Object *(*dll_scheme_current_continuation_marks)(Scheme_Object *prompt_tag);
255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000256static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100257static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, OUTPUT_LEN_TYPE *len);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000258static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
Bram Moolenaar33570922005-01-25 22:26:29 +0000259static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
260 int _num_rands, Scheme_Object **rands, int val);
261static void (*dll_scheme_dont_gc_ptr)(void *p);
262static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
263static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
264 Scheme_Env *env);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000265static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 Scheme_Env *env, int all);
267static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000268# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000269static char *(*dll_scheme_format)(char *format, int flen, int argc,
270 Scheme_Object **argv, long *rlen);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000271# else
272static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100273 Scheme_Object **argv, OUTPUT_LEN_TYPE *rlen);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000274static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000275# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000276static void (*dll_scheme_gc_ptr_ok)(void *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000277# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000278static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
279 long *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000280# else
281static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100282 OUTPUT_LEN_TYPE *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000283# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000284static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
285static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
286 Scheme_Env *env);
287static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
288 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
289 mzshort maxa);
290static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000291static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
Bram Moolenaar33570922005-01-25 22:26:29 +0000292 Scheme_Object *cdr);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000293static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
294 const char *name, mzshort mina, mzshort maxa);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000295# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000296static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
297static Scheme_Object *(*dll_scheme_make_string_output_port)();
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000298# else
299static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
300static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
301# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000302static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
303 int argc, Scheme_Object **argv);
304static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
305 Scheme_Object *field_names, int flags, int *count_out);
306static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
307 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
308 int num_uninit_fields, Scheme_Object *uninit_val,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000309 Scheme_Object *properties
310# if MZSCHEME_VERSION_MAJOR >= 299
311 , Scheme_Object *guard
312# endif
313 );
Bram Moolenaar33570922005-01-25 22:26:29 +0000314static Scheme_Object **(*dll_scheme_make_struct_values)(
315 Scheme_Object *struct_type, Scheme_Object **names, int count,
316 int flags);
317static Scheme_Type (*dll_scheme_make_type)(const char *name);
318static Scheme_Object *(*dll_scheme_make_vector)(int size,
319 Scheme_Object *fill);
320static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
321static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
322 const char *who);
323static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
324 Scheme_Env *for_env);
325static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
326static void (*dll_scheme_raise)(Scheme_Object *exn);
327static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
328static void (*dll_scheme_signal_error)(const char *msg, ...);
329static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
330 int which, int argc, Scheme_Object **argv);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000331# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000332static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000333 Scheme_Object *o);
334static Scheme_Config *(*dll_scheme_current_config)(void);
335static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
336 (Scheme_Object *s);
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000337static Scheme_Object *(*dll_scheme_char_string_to_path)
338 (Scheme_Object *s);
Bram Moolenaar75676462013-01-30 14:55:42 +0100339static void *(*dll_scheme_set_collects_path)(Scheme_Object *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000340# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000341static Scheme_Hash_Table *(*dll_scheme_make_hash_table)(int type);
342static void (*dll_scheme_hash_set)(Scheme_Hash_Table *table,
343 Scheme_Object *key, Scheme_Object *value);
344static Scheme_Object *(*dll_scheme_hash_get)(Scheme_Hash_Table *table,
345 Scheme_Object *key);
346static Scheme_Object *(*dll_scheme_make_double)(double d);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000347static Scheme_Object *(*dll_scheme_make_sized_byte_string)(char *chars,
348 long len, int copy);
349static Scheme_Object *(*dll_scheme_namespace_require)(Scheme_Object *req);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100350static 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);
351# ifdef MZ_PRECISE_GC
352static void *(*dll_GC_malloc_one_tagged)(size_t size_in_bytes);
353static void (*dll_GC_register_traversers)(short tag, Size_Proc size, Mark_Proc mark, Fixup_Proc fixup, int is_constant_size, int is_atomic);
354# endif
355# if MZSCHEME_VERSION_MAJOR >= 400
356static void (*dll_scheme_init_collection_paths)(Scheme_Env *global_env, Scheme_Object *extra_dirs);
357static void **(*dll_scheme_malloc_immobile_box)(void *p);
358static void (*dll_scheme_free_immobile_box)(void **b);
359# endif
360# if MZSCHEME_VERSION_MAJOR >= 500
361# ifdef TRAMPOLINED_MZVIM_STARTUP
362static int (*dll_scheme_main_setup)(int no_auto_statics, Scheme_Env_Main _main, int argc, char **argv);
363# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
364static void (*dll_scheme_register_tls_space)(void *tls_space, int _tls_index);
365# endif
366# endif
367# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
368static Thread_Local_Variables *(*dll_scheme_external_get_thread_local_variables)(void);
369# endif
370# endif
371# if MZSCHEME_VERSION_MAJOR >= 600
372static void (*dll_scheme_embedded_load)(intptr_t len, const char *s, int predefined);
373static void (*dll_scheme_register_embedded_load)(intptr_t len, const char *s);
374static void (*dll_scheme_set_config_path)(Scheme_Object *p);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000375# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000376
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100377#if defined(DYNAMIC_MZSCHEME) // not when defined(PROTO)
Bram Moolenaar4349c572016-01-30 13:28:28 +0100378
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100379// arrays are imported directly
Bram Moolenaar33570922005-01-25 22:26:29 +0000380# define scheme_eof dll_scheme_eof
381# define scheme_false dll_scheme_false
382# define scheme_void dll_scheme_void
383# define scheme_null dll_scheme_null
384# define scheme_true dll_scheme_true
385
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100386// pointers are GetProceAddress'ed as pointers to pointer
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100387#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
388# define scheme_current_thread (*dll_scheme_current_thread_ptr)
389# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000390# define scheme_console_printf (*dll_scheme_console_printf_ptr)
391# define scheme_console_output (*dll_scheme_console_output_ptr)
392# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
393
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100394// and functions in a usual way
Bram Moolenaar33570922005-01-25 22:26:29 +0000395# define GC_malloc dll_GC_malloc
396# define GC_malloc_atomic dll_GC_malloc_atomic
397
398# define scheme_add_global dll_scheme_add_global
399# define scheme_add_global_symbol dll_scheme_add_global_symbol
400# define scheme_apply dll_scheme_apply
401# define scheme_basic_env dll_scheme_basic_env
402# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000403# if MZSCHEME_VERSION_MAJOR >= 299
404# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100405# define scheme_make_path dll_scheme_make_path
Bram Moolenaar555b2802005-05-19 21:08:39 +0000406# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000407# define scheme_check_threads dll_scheme_check_threads
408# define scheme_close_input_port dll_scheme_close_input_port
409# define scheme_count_lines dll_scheme_count_lines
410# define scheme_current_continuation_marks \
411 dll_scheme_current_continuation_marks
412# define scheme_display dll_scheme_display
413# define scheme_display_to_string dll_scheme_display_to_string
414# define scheme_do_eval dll_scheme_do_eval
415# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000416# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000417# define scheme_eval dll_scheme_eval
418# define scheme_eval_string dll_scheme_eval_string
419# define scheme_eval_string_all dll_scheme_eval_string_all
420# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000421# if MZSCHEME_VERSION_MAJOR < 299
422# define scheme_format dll_scheme_format
423# else
424# define scheme_format_utf8 dll_scheme_format_utf8
425# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000426# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000427# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100428# define scheme_get_sized_byte_string_output dll_scheme_get_sized_string_output
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000429# else
430# define scheme_get_sized_byte_string_output \
431 dll_scheme_get_sized_byte_string_output
Bram Moolenaar75676462013-01-30 14:55:42 +0100432# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000433# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000434# define scheme_intern_symbol dll_scheme_intern_symbol
435# define scheme_lookup_global dll_scheme_lookup_global
436# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
437# define scheme_make_integer_value dll_scheme_make_integer_value
Bram Moolenaar33570922005-01-25 22:26:29 +0000438# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000439# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000440# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100441# define scheme_make_byte_string dll_scheme_make_string
442# define scheme_make_byte_string_output_port dll_scheme_make_string_output_port
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000443# else
444# define scheme_make_byte_string dll_scheme_make_byte_string
445# define scheme_make_byte_string_output_port \
446 dll_scheme_make_byte_string_output_port
447# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000448# define scheme_make_struct_instance dll_scheme_make_struct_instance
449# define scheme_make_struct_names dll_scheme_make_struct_names
450# define scheme_make_struct_type dll_scheme_make_struct_type
451# define scheme_make_struct_values dll_scheme_make_struct_values
452# define scheme_make_type dll_scheme_make_type
453# define scheme_make_vector dll_scheme_make_vector
454# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
455# define scheme_open_input_file dll_scheme_open_input_file
456# define scheme_primitive_module dll_scheme_primitive_module
457# define scheme_proper_list_length dll_scheme_proper_list_length
458# define scheme_raise dll_scheme_raise
459# define scheme_read dll_scheme_read
460# define scheme_register_static dll_scheme_register_static
461# define scheme_set_stack_base dll_scheme_set_stack_base
462# define scheme_signal_error dll_scheme_signal_error
463# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000464# if MZSCHEME_VERSION_MAJOR >= 299
465# define scheme_set_param dll_scheme_set_param
466# define scheme_current_config dll_scheme_current_config
467# define scheme_char_string_to_byte_string \
468 dll_scheme_char_string_to_byte_string
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000469# define scheme_char_string_to_path \
470 dll_scheme_char_string_to_path
Bram Moolenaar75676462013-01-30 14:55:42 +0100471# define scheme_set_collects_path dll_scheme_set_collects_path
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000472# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000473# define scheme_make_hash_table dll_scheme_make_hash_table
474# define scheme_hash_set dll_scheme_hash_set
475# define scheme_hash_get dll_scheme_hash_get
476# define scheme_make_double dll_scheme_make_double
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100477# define scheme_make_sized_byte_string dll_scheme_make_sized_byte_string
478# define scheme_namespace_require dll_scheme_namespace_require
479# define scheme_dynamic_wind dll_scheme_dynamic_wind
480# ifdef MZ_PRECISE_GC
481# define GC_malloc_one_tagged dll_GC_malloc_one_tagged
482# define GC_register_traversers dll_GC_register_traversers
483# endif
484# if MZSCHEME_VERSION_MAJOR >= 400
485# ifdef TRAMPOLINED_MZVIM_STARTUP
486# define scheme_main_setup dll_scheme_main_setup
487# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
488# define scheme_register_tls_space dll_scheme_register_tls_space
489# endif
490# endif
491# define scheme_init_collection_paths dll_scheme_init_collection_paths
492# define scheme_malloc_immobile_box dll_scheme_malloc_immobile_box
493# define scheme_free_immobile_box dll_scheme_free_immobile_box
494# endif
495# if MZSCHEME_VERSION_MAJOR >= 600
496# define scheme_embedded_load dll_scheme_embedded_load
497# define scheme_register_embedded_load dll_scheme_register_embedded_load
498# define scheme_set_config_path dll_scheme_set_config_path
499# endif
500
501# if MZSCHEME_VERSION_MAJOR >= 500
502# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100503// define as function for macro in schthread.h
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100504Thread_Local_Variables *
505scheme_external_get_thread_local_variables(void)
506{
507 return dll_scheme_external_get_thread_local_variables();
508}
509# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000510# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000511
Bram Moolenaar4349c572016-01-30 13:28:28 +0100512#endif
513
Bram Moolenaar33570922005-01-25 22:26:29 +0000514typedef struct
515{
516 char *name;
517 void **ptr;
518} Thunk_Info;
519
520static Thunk_Info mzgc_imports[] = {
521 {"GC_malloc", (void **)&dll_GC_malloc},
522 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
523 {NULL, NULL}};
524
525static Thunk_Info mzsch_imports[] = {
526 {"scheme_eof", (void **)&dll_scheme_eof},
527 {"scheme_false", (void **)&dll_scheme_false},
528 {"scheme_void", (void **)&dll_scheme_void},
529 {"scheme_null", (void **)&dll_scheme_null},
530 {"scheme_true", (void **)&dll_scheme_true},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100531#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
Bram Moolenaar33570922005-01-25 22:26:29 +0000532 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100533#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000534 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
535 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000536 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000537 (void **)&dll_scheme_notify_multithread_ptr},
538 {"scheme_add_global", (void **)&dll_scheme_add_global},
539 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
540 {"scheme_apply", (void **)&dll_scheme_apply},
541 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000542# if MZSCHEME_VERSION_MAJOR >= 299
543 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100544 {"scheme_make_path", (void **)&dll_scheme_make_path},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000545# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000546 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
547 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
548 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
549 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000550 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000551 (void **)&dll_scheme_current_continuation_marks},
552 {"scheme_display", (void **)&dll_scheme_display},
553 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
554 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
555 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000556 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000557 {"scheme_eval", (void **)&dll_scheme_eval},
558 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
559 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000560 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000561 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000562# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000563 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000564# else
565 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000566 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000567#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000568 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000569# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000570 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000571 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000572# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000573 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000574 (void **)&dll_scheme_get_sized_byte_string_output},
575#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000576 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
577 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000578 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000579 (void **)&dll_scheme_make_closed_prim_w_arity},
580 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
Bram Moolenaar33570922005-01-25 22:26:29 +0000581 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000582 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000583# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000584 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000585 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000586 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000587# else
588 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000589 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000590 (void **)&dll_scheme_make_byte_string_output_port},
591# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000592 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000593 (void **)&dll_scheme_make_struct_instance},
594 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
595 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
596 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
597 {"scheme_make_type", (void **)&dll_scheme_make_type},
598 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
599 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
600 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
601 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
602 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
603 {"scheme_raise", (void **)&dll_scheme_raise},
604 {"scheme_read", (void **)&dll_scheme_read},
605 {"scheme_register_static", (void **)&dll_scheme_register_static},
606 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
607 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
608 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000609# if MZSCHEME_VERSION_MAJOR >= 299
610 {"scheme_set_param", (void **)&dll_scheme_set_param},
611 {"scheme_current_config", (void **)&dll_scheme_current_config},
612 {"scheme_char_string_to_byte_string",
613 (void **)&dll_scheme_char_string_to_byte_string},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000614 {"scheme_char_string_to_path", (void **)&dll_scheme_char_string_to_path},
Bram Moolenaar75676462013-01-30 14:55:42 +0100615 {"scheme_set_collects_path", (void **)&dll_scheme_set_collects_path},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000616# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000617 {"scheme_make_hash_table", (void **)&dll_scheme_make_hash_table},
618 {"scheme_hash_set", (void **)&dll_scheme_hash_set},
619 {"scheme_hash_get", (void **)&dll_scheme_hash_get},
620 {"scheme_make_double", (void **)&dll_scheme_make_double},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000621 {"scheme_make_sized_byte_string", (void **)&dll_scheme_make_sized_byte_string},
622 {"scheme_namespace_require", (void **)&dll_scheme_namespace_require},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100623 {"scheme_dynamic_wind", (void **)&dll_scheme_dynamic_wind},
624# ifdef MZ_PRECISE_GC
625 {"GC_malloc_one_tagged", (void **)&dll_GC_malloc_one_tagged},
626 {"GC_register_traversers", (void **)&dll_GC_register_traversers},
627# endif
628# if MZSCHEME_VERSION_MAJOR >= 400
629# ifdef TRAMPOLINED_MZVIM_STARTUP
630 {"scheme_main_setup", (void **)&dll_scheme_main_setup},
631# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
632 {"scheme_register_tls_space", (void **)&dll_scheme_register_tls_space},
633# endif
634# endif
635 {"scheme_init_collection_paths", (void **)&dll_scheme_init_collection_paths},
636 {"scheme_malloc_immobile_box", (void **)&dll_scheme_malloc_immobile_box},
637 {"scheme_free_immobile_box", (void **)&dll_scheme_free_immobile_box},
638# endif
639# if MZSCHEME_VERSION_MAJOR >= 500
640# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
641 {"scheme_external_get_thread_local_variables", (void **)&dll_scheme_external_get_thread_local_variables},
642# endif
643# endif
644# if MZSCHEME_VERSION_MAJOR >= 600
645 {"scheme_embedded_load", (void **)&dll_scheme_embedded_load},
646 {"scheme_register_embedded_load", (void **)&dll_scheme_register_embedded_load},
647 {"scheme_set_config_path", (void **)&dll_scheme_set_config_path},
648# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000649 {NULL, NULL}};
650
651static HINSTANCE hMzGC = 0;
652static HINSTANCE hMzSch = 0;
653
654static void dynamic_mzscheme_end(void);
655static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
656 int verbose);
657
658 static int
659mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
660{
661 Thunk_Info *thunk = NULL;
662
663 if (hMzGC && hMzSch)
664 return OK;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200665 hMzSch = vimLoadLib(sch_dll);
666 hMzGC = vimLoadLib(gc_dll);
Bram Moolenaar33570922005-01-25 22:26:29 +0000667
Bram Moolenaar33570922005-01-25 22:26:29 +0000668 if (!hMzGC)
669 {
670 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100671 semsg(_(e_loadlib), gc_dll);
Bram Moolenaar33570922005-01-25 22:26:29 +0000672 return FAIL;
673 }
674
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100675 if (!hMzSch)
676 {
677 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100678 semsg(_(e_loadlib), sch_dll);
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100679 return FAIL;
680 }
681
Bram Moolenaar33570922005-01-25 22:26:29 +0000682 for (thunk = mzsch_imports; thunk->name; thunk++)
683 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000684 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000685 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
686 {
687 FreeLibrary(hMzSch);
688 hMzSch = 0;
689 FreeLibrary(hMzGC);
690 hMzGC = 0;
691 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100692 semsg(_(e_loadfunc), thunk->name);
Bram Moolenaar33570922005-01-25 22:26:29 +0000693 return FAIL;
694 }
695 }
696 for (thunk = mzgc_imports; thunk->name; thunk++)
697 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000698 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000699 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
700 {
701 FreeLibrary(hMzSch);
702 hMzSch = 0;
703 FreeLibrary(hMzGC);
704 hMzGC = 0;
705 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100706 semsg(_(e_loadfunc), thunk->name);
Bram Moolenaar33570922005-01-25 22:26:29 +0000707 return FAIL;
708 }
709 }
710 return OK;
711}
712
713 int
714mzscheme_enabled(int verbose)
715{
716 return mzscheme_runtime_link_init(
Bram Moolenaar0ab35b22017-10-08 17:41:37 +0200717 (char *)p_mzschemedll, (char *)p_mzschemegcdll, verbose) == OK;
Bram Moolenaar33570922005-01-25 22:26:29 +0000718}
719
720 static void
721dynamic_mzscheme_end(void)
722{
723 if (hMzSch)
724 {
725 FreeLibrary(hMzSch);
726 hMzSch = 0;
727 }
728 if (hMzGC)
729 {
730 FreeLibrary(hMzGC);
731 hMzGC = 0;
732 }
733}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100734#endif // DYNAMIC_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +0000735
Bram Moolenaar75676462013-01-30 14:55:42 +0100736#if MZSCHEME_VERSION_MAJOR < 299
737# define GUARANTEED_STRING_ARG(proc, num) GUARANTEE_STRING(proc, num)
738#else
739 static Scheme_Object *
740guaranteed_byte_string_arg(char *proc, int num, int argc, Scheme_Object **argv)
741{
742 if (SCHEME_BYTE_STRINGP(argv[num]))
743 {
744 return argv[num];
745 }
746 else if (SCHEME_CHAR_STRINGP(argv[num]))
747 {
748 Scheme_Object *tmp = NULL;
749 MZ_GC_DECL_REG(2);
750 MZ_GC_VAR_IN_REG(0, argv[num]);
751 MZ_GC_VAR_IN_REG(1, tmp);
752 MZ_GC_REG();
753 tmp = scheme_char_string_to_byte_string(argv[num]);
754 MZ_GC_UNREG();
755 return tmp;
756 }
757 else
758 scheme_wrong_type(proc, "string", num, argc, argv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100759 // unreachable
Bram Moolenaar75676462013-01-30 14:55:42 +0100760 return scheme_void;
761}
762# define GUARANTEED_STRING_ARG(proc, num) guaranteed_byte_string_arg(proc, num, argc, argv)
763#endif
764
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100765// need to put it here for dynamic stuff to work
Bram Moolenaare484c942009-09-11 10:21:41 +0000766#if defined(INCLUDE_MZSCHEME_BASE)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000767# include "mzscheme_base.c"
768#endif
769
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000770/*
771 *========================================================================
772 * 1. MzScheme interpreter startup
773 *========================================================================
774 */
775
776static Scheme_Type mz_buffer_type;
777static Scheme_Type mz_window_type;
778
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000779static int initialized = FALSE;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100780#ifdef DYNAMIC_MZSCHEME
781static int disabled = FALSE;
782#endif
783static int load_base_module_failed = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000784
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100785// global environment
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000786static Scheme_Env *environment = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100787// output/error handlers
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000788static Scheme_Object *curout = NULL;
789static Scheme_Object *curerr = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100790// exn:vim exception
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000791static Scheme_Object *exn_catching_apply = NULL;
792static Scheme_Object *exn_p = NULL;
793static Scheme_Object *exn_message = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100794static Scheme_Object *vim_exn = NULL; // Vim Error exception
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000795
796#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
797static void *stack_base = NULL;
798#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000799
800static long range_start;
801static long range_end;
802
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100803// MzScheme threads scheduling stuff
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000804static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000805
Bram Moolenaar4f974752019-02-17 17:44:42 +0100806#if defined(FEAT_GUI_MSWIN)
Bram Moolenaardec6c7b2016-06-02 11:57:38 +0200807static void CALLBACK timer_proc(HWND, UINT, UINT_PTR, DWORD);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000808static UINT timer_id = 0;
809#elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100810static gboolean timer_proc(gpointer);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000811static guint timer_id = 0;
812#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
813static void timer_proc(XtPointer, XtIntervalId *);
814static XtIntervalId timer_id = (XtIntervalId)0;
815#elif defined(FEAT_GUI_MAC)
816pascal void timer_proc(EventLoopTimerRef, void *);
817static EventLoopTimerRef timer_id = NULL;
818static EventLoopTimerUPP timerUPP;
819#endif
820
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100821#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) // Win32 console and Unix
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000822 void
823mzvim_check_threads(void)
824{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100825 // Last time MzScheme threads were scheduled
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000826 static time_t mz_last_time = 0;
827
828 if (mz_threads_allow && p_mzq > 0)
829 {
830 time_t now = time(NULL);
831
832 if ((now - mz_last_time) * 1000 > p_mzq)
833 {
834 mz_last_time = now;
835 scheme_check_threads();
836 }
837 }
838}
839#endif
840
Bram Moolenaar4349c572016-01-30 13:28:28 +0100841#if defined(MZSCHEME_GUI_THREADS) || defined(PROTO)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000842static void setup_timer(void);
843static void remove_timer(void);
844
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100845// timers are presented in GUI only
Bram Moolenaar4f974752019-02-17 17:44:42 +0100846# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000847 static void CALLBACK
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200848timer_proc(HWND hwnd UNUSED, UINT uMsg UNUSED, UINT_PTR idEvent UNUSED, DWORD dwTime UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000849# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100850 static gboolean
Bram Moolenaar64404472010-06-26 06:24:45 +0200851timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000852# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000853 static void
Bram Moolenaar64404472010-06-26 06:24:45 +0200854timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000855# elif defined(FEAT_GUI_MAC)
856 pascal void
Bram Moolenaar64404472010-06-26 06:24:45 +0200857timer_proc(EventLoopTimerRef theTimer UNUSED, void *userData UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000858# endif
859{
860 scheme_check_threads();
861# if defined(FEAT_GUI_GTK)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100862 return TRUE; // continue receiving notifications
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000863# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100864 // renew timeout
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000865 if (mz_threads_allow && p_mzq > 0)
866 timer_id = XtAppAddTimeOut(app_context, p_mzq,
867 timer_proc, NULL);
868# endif
869}
870
871 static void
872setup_timer(void)
873{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100874# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000875 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
876# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100877 timer_id = g_timeout_add((guint)p_mzq, (GSourceFunc)timer_proc, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000878# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
879 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
880# elif defined(FEAT_GUI_MAC)
881 timerUPP = NewEventLoopTimerUPP(timer_proc);
882 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
883 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
884# endif
885}
886
887 static void
888remove_timer(void)
889{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100890# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000891 KillTimer(NULL, timer_id);
892# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100893 g_source_remove(timer_id);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000894# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
895 XtRemoveTimeOut(timer_id);
896# elif defined(FEAT_GUI_MAC)
897 RemoveEventLoopTimer(timer_id);
898 DisposeEventLoopTimerUPP(timerUPP);
899# endif
900 timer_id = 0;
901}
902
903 void
904mzvim_reset_timer(void)
905{
906 if (timer_id != 0)
907 remove_timer();
908 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
909 setup_timer();
910}
911
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100912#endif // MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000913
914 static void
915notify_multithread(int on)
916{
917 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000918#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000919 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
920 setup_timer();
921 if (!on && timer_id != 0)
922 remove_timer();
923#endif
924}
925
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000926 void
927mzscheme_end(void)
928{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100929 // We can not unload the DLL. Racket's thread might be still alive.
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100930#if 0
Bram Moolenaar33570922005-01-25 22:26:29 +0000931#ifdef DYNAMIC_MZSCHEME
932 dynamic_mzscheme_end();
933#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100934#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000935}
936
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100937#if HAVE_TLS_SPACE
938# if defined(_MSC_VER)
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100939static __declspec(thread) void *tls_space;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100940extern intptr_t _tls_index;
941# elif defined(__MINGW32__)
942static __thread void *tls_space;
943extern intptr_t _tls_index;
944# else
945static THREAD_LOCAL void *tls_space;
946static intptr_t _tls_index = 0;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100947# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100948#endif
949
Bram Moolenaar54b63522016-08-26 12:55:09 +0200950/*
951 * mzscheme_main() is called early in main().
952 * We may call scheme_main_setup() which calls mzscheme_env_main() which then
953 * trampolines into vim_main2(), which never returns.
954 */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100955 int
Bram Moolenaar54b63522016-08-26 12:55:09 +0200956mzscheme_main(void)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000957{
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200958 int argc = 0;
959 char *argv = NULL;
960
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100961#ifdef DYNAMIC_MZSCHEME
962 /*
963 * Racket requires trampolined startup. We can not load it later.
964 * If dynamic dll loading is failed, disable it.
965 */
966 if (!mzscheme_enabled(FALSE))
967 {
968 disabled = TRUE;
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200969 return vim_main2();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100970 }
971#endif
Bram Moolenaar5c5c9802015-07-10 16:12:48 +0200972#ifdef HAVE_TLS_SPACE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100973 scheme_register_tls_space(&tls_space, _tls_index);
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100974#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100975#ifdef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200976 return scheme_main_setup(TRUE, mzscheme_env_main, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000977#else
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200978 return mzscheme_env_main(NULL, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000979#endif
980}
981
982 static int
Bram Moolenaar54b63522016-08-26 12:55:09 +0200983mzscheme_env_main(Scheme_Env *env, int argc UNUSED, char **argv UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000984{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100985#ifdef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100986 // Scheme has created the environment for us
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100987 environment = env;
988#else
989# ifdef MZ_PRECISE_GC
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000990 Scheme_Object *dummy = NULL;
991 MZ_GC_DECL_REG(1);
992 MZ_GC_VAR_IN_REG(0, dummy);
993
994 stack_base = &__gc_var_stack__;
995# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000996 int dummy = 0;
997 stack_base = (void *)&dummy;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100998# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000999#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001000
Bram Moolenaar54b63522016-08-26 12:55:09 +02001001 vim_main2();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001002 // not reached, vim_main2() will loop until exit()
Bram Moolenaar54b63522016-08-26 12:55:09 +02001003
1004 return 0;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001005}
1006
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001007 static Scheme_Object*
1008load_base_module(void *data)
1009{
1010 scheme_namespace_require(scheme_intern_symbol((char *)data));
1011 return scheme_null;
1012}
1013
1014 static Scheme_Object *
Bram Moolenaar8b29aba2016-03-28 22:17:16 +02001015load_base_module_on_error(void *data UNUSED)
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001016{
1017 load_base_module_failed = TRUE;
1018 return scheme_null;
1019}
1020
1021 static int
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001022startup_mzscheme(void)
1023{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001024#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001025 scheme_set_stack_base(stack_base, 1);
1026#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001027
Bram Moolenaar75676462013-01-30 14:55:42 +01001028#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001029 // in newer versions of precise GC the initial env has been created
Bram Moolenaar75676462013-01-30 14:55:42 +01001030 environment = scheme_basic_env();
1031#endif
1032
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001033 MZ_REGISTER_STATIC(environment);
1034 MZ_REGISTER_STATIC(curout);
1035 MZ_REGISTER_STATIC(curerr);
1036 MZ_REGISTER_STATIC(exn_catching_apply);
1037 MZ_REGISTER_STATIC(exn_p);
1038 MZ_REGISTER_STATIC(exn_message);
1039 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001040
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001041 MZ_GC_CHECK();
1042
1043#ifdef INCLUDE_MZSCHEME_BASE
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001044 // invoke function from generated and included mzscheme_base.c
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001045 declare_modules(environment);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001046#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001047
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001048 // setup 'current-library-collection-paths' parameter
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001049# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001050 {
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001051 Scheme_Object *coll_path = NULL;
1052 int mustfree = FALSE;
1053 char_u *s;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001054
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001055 MZ_GC_DECL_REG(1);
1056 MZ_GC_VAR_IN_REG(0, coll_path);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001057 MZ_GC_REG();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001058 // workaround for dynamic loading on windows
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001059 s = vim_getenv((char_u *)"PLTCOLLECTS", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001060 if (s != NULL)
1061 {
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001062 coll_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001063 MZ_GC_CHECK();
1064 if (mustfree)
1065 vim_free(s);
1066 }
1067# ifdef MZSCHEME_COLLECTS
1068 if (coll_path == NULL)
1069 {
1070 coll_path = scheme_make_path(MZSCHEME_COLLECTS);
1071 MZ_GC_CHECK();
1072 }
Bram Moolenaar39d7d512013-01-31 21:09:15 +01001073# endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001074 if (coll_path != NULL)
1075 {
1076 scheme_set_collects_path(coll_path);
1077 MZ_GC_CHECK();
1078 }
1079 MZ_GC_UNREG();
1080 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001081# else
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001082# ifdef MZSCHEME_COLLECTS
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001083 {
1084 Scheme_Object *coll_string = NULL;
1085 Scheme_Object *coll_pair = NULL;
1086 Scheme_Config *config = NULL;
1087
1088 MZ_GC_DECL_REG(3);
1089 MZ_GC_VAR_IN_REG(0, coll_string);
1090 MZ_GC_VAR_IN_REG(1, coll_pair);
1091 MZ_GC_VAR_IN_REG(2, config);
1092 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001093 coll_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001094 MZ_GC_CHECK();
1095 coll_pair = scheme_make_pair(coll_string, scheme_null);
1096 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001097 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001098 MZ_GC_CHECK();
1099 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
1100 MZ_GC_CHECK();
1101 MZ_GC_UNREG();
1102 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001103# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001104#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001105
1106# if MZSCHEME_VERSION_MAJOR >= 600
1107 {
1108 Scheme_Object *config_path = NULL;
1109 int mustfree = FALSE;
1110 char_u *s;
1111
1112 MZ_GC_DECL_REG(1);
1113 MZ_GC_VAR_IN_REG(0, config_path);
1114 MZ_GC_REG();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001115 // workaround for dynamic loading on windows
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001116 s = vim_getenv((char_u *)"PLTCONFIGDIR", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001117 if (s != NULL)
1118 {
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001119 config_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001120 MZ_GC_CHECK();
1121 if (mustfree)
1122 vim_free(s);
1123 }
1124#ifdef MZSCHEME_CONFIGDIR
1125 if (config_path == NULL)
1126 {
1127 config_path = scheme_make_path(MZSCHEME_CONFIGDIR);
1128 MZ_GC_CHECK();
1129 }
1130#endif
1131 if (config_path != NULL)
1132 {
1133 scheme_set_config_path(config_path);
1134 MZ_GC_CHECK();
1135 }
1136 MZ_GC_UNREG();
1137 }
1138# endif
1139
1140#if MZSCHEME_VERSION_MAJOR >= 400
1141 scheme_init_collection_paths(environment, scheme_null);
1142#endif
1143
1144 /*
1145 * versions 4.x do not provide Scheme bindings by default
1146 * we need to add them explicitly
1147 */
1148 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001149 // use error handler to avoid abort
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001150 scheme_dynamic_wind(NULL, load_base_module, NULL,
1151 load_base_module_on_error, "racket/base");
1152 if (load_base_module_failed)
1153 {
1154 load_base_module_failed = FALSE;
1155 scheme_dynamic_wind(NULL, load_base_module, NULL,
1156 load_base_module_on_error, "scheme/base");
1157 if (load_base_module_failed)
1158 return -1;
1159 }
1160 }
1161
1162 register_vim_exn();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001163 // use new environment to initialise exception handling
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001164 init_exn_catching_apply();
1165
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001166 // redirect output
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001167 scheme_console_output = do_output;
1168 scheme_console_printf = do_printf;
1169
Bram Moolenaar555b2802005-05-19 21:08:39 +00001170#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +00001171 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001172 Scheme_Object *make_security_guard = NULL;
1173 MZ_GC_DECL_REG(1);
1174 MZ_GC_VAR_IN_REG(0, make_security_guard);
1175 MZ_GC_REG();
1176
1177#if MZSCHEME_VERSION_MAJOR < 400
1178 {
1179 Scheme_Object *make_security_guard_symbol = NULL;
1180 MZ_GC_DECL_REG(1);
1181 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
1182 MZ_GC_REG();
1183 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
1184 MZ_GC_CHECK();
1185 make_security_guard = scheme_lookup_global(
1186 make_security_guard_symbol, environment);
1187 MZ_GC_UNREG();
1188 }
1189#else
1190 make_security_guard = scheme_builtin_value("make-security-guard");
1191 MZ_GC_CHECK();
1192#endif
1193
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001194 // setup sandbox guards
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001195 if (make_security_guard != NULL)
1196 {
1197 Scheme_Object *args[3] = {NULL, NULL, NULL};
1198 Scheme_Object *guard = NULL;
1199 Scheme_Config *config = NULL;
1200 MZ_GC_DECL_REG(5);
1201 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
1202 MZ_GC_VAR_IN_REG(3, guard);
1203 MZ_GC_VAR_IN_REG(4, config);
1204 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001205 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001206 MZ_GC_CHECK();
1207 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
1208 MZ_GC_CHECK();
1209 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
1210 "sandbox-file-guard", 3, 3);
1211 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1212 "sandbox-network-guard", 4, 4);
1213 guard = scheme_apply(make_security_guard, 3, args);
1214 MZ_GC_CHECK();
1215 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1216 MZ_GC_CHECK();
1217 MZ_GC_UNREG();
1218 }
1219 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001220 }
1221#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001222 // Create buffer and window types for use in Scheme code
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001223 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001224 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001225 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001226 MZ_GC_CHECK();
1227#ifdef MZ_PRECISE_GC
1228 GC_register_traversers(mz_buffer_type,
1229 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1230 TRUE, TRUE);
1231 GC_register_traversers(mz_window_type,
1232 window_size_proc, window_mark_proc, window_fixup_proc,
1233 TRUE, TRUE);
1234#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001235
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001236 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001237
1238 /*
1239 * setup callback to receive notifications
1240 * whether thread scheduling is (or not) required
1241 */
1242 scheme_notify_multithread = notify_multithread;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001243
1244 return 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001245}
1246
1247/*
1248 * This routine is called for each new invocation of MzScheme
1249 * to make sure things are properly initialized.
1250 */
1251 static int
1252mzscheme_init(void)
1253{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001254 if (!initialized)
1255 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001256#ifdef DYNAMIC_MZSCHEME
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001257 if (disabled || !mzscheme_enabled(TRUE))
Bram Moolenaar33570922005-01-25 22:26:29 +00001258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001259 emsg(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
Bram Moolenaar33570922005-01-25 22:26:29 +00001260 return -1;
1261 }
1262#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001263 if (load_base_module_failed || startup_mzscheme())
1264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001265 emsg(_("E895: Sorry, this command is disabled, the MzScheme's racket/base module could not be loaded."));
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001266 return -1;
1267 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001268 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001269 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001270 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001271 Scheme_Config *config = NULL;
1272 MZ_GC_DECL_REG(1);
1273 MZ_GC_VAR_IN_REG(0, config);
1274 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001275 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001276 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001277 // recreate ports each call effectively clearing these ones
Bram Moolenaar75676462013-01-30 14:55:42 +01001278 curout = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001279 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001280 curerr = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001281 MZ_GC_CHECK();
1282 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1283 MZ_GC_CHECK();
1284 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1285 MZ_GC_CHECK();
1286 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001287 }
1288
1289 return 0;
1290}
1291
1292/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001293 *========================================================================
1294 * 2. External Interface
1295 *========================================================================
1296 */
1297
1298/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001299 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001300 */
1301 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001302eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001303{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001304 Scheme_Object *value = NULL;
1305 Scheme_Object *exn = NULL;
1306 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001307
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001308 MZ_GC_DECL_REG(3);
1309 MZ_GC_VAR_IN_REG(0, value);
1310 MZ_GC_VAR_IN_REG(1, exn);
1311 MZ_GC_VAR_IN_REG(2, prim);
1312 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001313
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001314 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1315 MZ_GC_CHECK();
1316 value = _apply_thunk_catch_exceptions(prim, &exn);
1317 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001318
1319 if (!value)
1320 {
1321 value = extract_exn_message(exn);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001322 // Got an exn?
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001323 if (value)
1324 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001325 scheme_display(value, curerr); // Send to stderr-vim
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001326 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001327 do_flush();
1328 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001329 MZ_GC_UNREG();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001330 // `raise' was called on some arbitrary value
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001331 return FAIL;
1332 }
1333
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001334 if (ret != NULL) // if pointer to retval supported give it up
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001335 *ret = value;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001336 // Print any result, as long as it's not a void
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001337 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001338 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001339 scheme_display(value, curout); // Send to stdout-vim
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001340 MZ_GC_CHECK();
1341 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001342
1343 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001344 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001345 return OK;
1346}
1347
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001348/*
1349 * :mzscheme
1350 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001351 static int
1352do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1353{
1354 if (mzscheme_init())
1355 return FAIL;
1356
1357 range_start = eap->line1;
1358 range_end = eap->line2;
1359
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001360 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001361}
1362
1363/*
1364 * Routine called by VIM when deleting a buffer
1365 */
1366 void
1367mzscheme_buffer_free(buf_T *buf)
1368{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001369 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001370 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001371 vim_mz_buffer *bp = NULL;
1372 MZ_GC_DECL_REG(1);
1373 MZ_GC_VAR_IN_REG(0, bp);
1374 MZ_GC_REG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001375
Bram Moolenaar75676462013-01-30 14:55:42 +01001376 bp = BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001377 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001378#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001379 scheme_gc_ptr_ok(bp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001380#else
1381 scheme_free_immobile_box(buf->b_mzscheme_ref);
1382#endif
1383 buf->b_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001384 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001385 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001386 }
1387}
1388
1389/*
1390 * Routine called by VIM when deleting a Window
1391 */
1392 void
1393mzscheme_window_free(win_T *win)
1394{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001395 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001396 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001397 vim_mz_window *wp = NULL;
1398 MZ_GC_DECL_REG(1);
1399 MZ_GC_VAR_IN_REG(0, wp);
1400 MZ_GC_REG();
1401 wp = WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001402 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001403#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001404 scheme_gc_ptr_ok(wp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001405#else
1406 scheme_free_immobile_box(win->w_mzscheme_ref);
1407#endif
1408 win->w_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001409 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001410 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001411 }
1412}
1413
1414/*
1415 * ":mzscheme" (or ":mz")
1416 */
1417 void
1418ex_mzscheme(exarg_T *eap)
1419{
1420 char_u *script;
1421
1422 script = script_get(eap, eap->arg);
1423 if (!eap->skip)
1424 {
1425 if (script == NULL)
1426 do_mzscheme_command(eap, eap->arg, do_eval);
1427 else
1428 {
1429 do_mzscheme_command(eap, script, do_eval);
1430 vim_free(script);
1431 }
1432 }
1433}
1434
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001435 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001436do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001437{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001438 Scheme_Object *expr = NULL;
1439 Scheme_Object *result = NULL;
1440 char *file = NULL;
1441 Port_Info *pinfo = (Port_Info *)data;
1442
1443 MZ_GC_DECL_REG(3);
1444 MZ_GC_VAR_IN_REG(0, expr);
1445 MZ_GC_VAR_IN_REG(1, result);
1446 MZ_GC_VAR_IN_REG(2, file);
1447 MZ_GC_REG();
1448
1449 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1450 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001451
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001452 // make Vim expansion
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001453 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001454 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001455 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001456 scheme_count_lines(pinfo->port); // to get accurate read error location
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001457 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001458
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001459 // Like REPL but print only last result
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001460 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001461 {
1462 result = scheme_eval(expr, environment);
1463 MZ_GC_CHECK();
1464 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001465
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001466 // errors will be caught in do_mzscheme_command and ex_mzfile
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001467 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001468 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001469 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001470 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001471 return result;
1472}
1473
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001474/*
1475 * :mzfile
1476 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001477 void
1478ex_mzfile(exarg_T *eap)
1479{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001480 Port_Info pinfo = {NULL, NULL};
1481
1482 MZ_GC_DECL_REG(1);
1483 MZ_GC_VAR_IN_REG(0, pinfo.port);
1484 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001485
1486 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001487 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001488 && pinfo.port != NULL) // looks like port was not closed
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001489 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001490 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001491 MZ_GC_CHECK();
1492 }
1493 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001494}
1495
1496
1497/*
1498 *========================================================================
1499 * Exception handling code -- cribbed form the MzScheme sources and
1500 * Matthew Flatt's "Inside PLT MzScheme" document.
1501 *========================================================================
1502 */
1503 static void
1504init_exn_catching_apply(void)
1505{
1506 if (!exn_catching_apply)
1507 {
1508 char *e =
1509 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001510 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001511 "(cons #t (thunk))))";
1512
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001513 exn_catching_apply = scheme_eval_string(e, environment);
1514 MZ_GC_CHECK();
1515 exn_p = scheme_builtin_value("exn?");
1516 MZ_GC_CHECK();
1517 exn_message = scheme_builtin_value("exn-message");
1518 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001519 }
1520}
1521
1522/*
1523 * This function applies a thunk, returning the Scheme value if there's
1524 * no exception, otherwise returning NULL and setting *exn to the raised
1525 * value (usually an exn structure).
1526 */
1527 static Scheme_Object *
1528_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1529{
1530 Scheme_Object *v;
1531
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001532 v = _scheme_apply(exn_catching_apply, 1, &f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001533 // v is a pair: (cons #t value) or (cons #f exn)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001534
1535 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1536 return SCHEME_CDR(v);
1537 else
1538 {
1539 *exn = SCHEME_CDR(v);
1540 return NULL;
1541 }
1542}
1543
1544 static Scheme_Object *
1545extract_exn_message(Scheme_Object *v)
1546{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001547 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1548 return _scheme_apply(exn_message, 1, &v);
1549 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001550 return NULL; // Not an exn structure
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001551}
1552
1553 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001554do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001555{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001556 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001557}
1558
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001559/*
1560 *========================================================================
1561 * 3. MzScheme I/O Handlers
1562 *========================================================================
1563 */
1564 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001565do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001566{
1567 char *p, *prev;
1568
1569 prev = mesg;
1570 p = strchr(prev, '\n');
1571 while (p)
1572 {
1573 *p = '\0';
1574 if (error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001575 emsg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001576 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001577 msg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001578 prev = p + 1;
1579 p = strchr(prev, '\n');
1580 }
1581
1582 if (error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001583 emsg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001584 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001585 msg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001586}
1587
1588 static void
Bram Moolenaar75676462013-01-30 14:55:42 +01001589do_output(char *mesg, OUTPUT_LEN_TYPE len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001590{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001591 // TODO: use len, the string may not be NUL terminated
Bram Moolenaar64404472010-06-26 06:24:45 +02001592 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001593}
1594
1595 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001596do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001597{
Bram Moolenaar64404472010-06-26 06:24:45 +02001598 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001599}
1600
1601 static void
1602do_printf(char *format, ...)
1603{
Bram Moolenaar64404472010-06-26 06:24:45 +02001604 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001605}
1606
1607 static void
1608do_flush(void)
1609{
1610 char *buff;
Bram Moolenaar75676462013-01-30 14:55:42 +01001611 OUTPUT_LEN_TYPE length;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001612
Bram Moolenaar75676462013-01-30 14:55:42 +01001613 buff = scheme_get_sized_byte_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001614 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001615 if (length)
1616 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001617 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001618 return;
1619 }
1620
Bram Moolenaar75676462013-01-30 14:55:42 +01001621 buff = scheme_get_sized_byte_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001622 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001623 if (length)
1624 do_output(buff, length);
1625}
1626
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001627/*
1628 *========================================================================
1629 * 4. Implementation of the Vim Features for MzScheme
1630 *========================================================================
1631 */
1632
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001633/*
1634 * (command {command-string})
1635 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001636 static Scheme_Object *
1637vim_command(void *data, int argc, Scheme_Object **argv)
1638{
1639 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001640 Scheme_Object *cmd = NULL;
1641 MZ_GC_DECL_REG(1);
1642 MZ_GC_VAR_IN_REG(0, cmd);
1643 MZ_GC_REG();
1644 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001645
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001646 // may be use do_cmdline_cmd?
Bram Moolenaar75676462013-01-30 14:55:42 +01001647 do_cmdline(BYTE_STRING_VALUE(cmd), NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001648 update_screen(VALID);
1649
Bram Moolenaar75676462013-01-30 14:55:42 +01001650 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001651 raise_if_error();
1652 return scheme_void;
1653}
1654
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001655/*
1656 * (eval {expr-string})
1657 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001658 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001659vim_eval(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001660{
1661#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001662 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001663 Scheme_Object *result = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001664 typval_T *vim_result;
Bram Moolenaar75676462013-01-30 14:55:42 +01001665 Scheme_Object *expr = NULL;
1666 MZ_GC_DECL_REG(2);
1667 MZ_GC_VAR_IN_REG(0, result);
1668 MZ_GC_VAR_IN_REG(1, expr);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001669 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001670 expr = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001671
Bram Moolenaar75676462013-01-30 14:55:42 +01001672 vim_result = eval_expr(BYTE_STRING_VALUE(expr), NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001673
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001674 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001675 raise_vim_exn(_("invalid expression"));
1676
Bram Moolenaar75676462013-01-30 14:55:42 +01001677 result = vim_to_mzscheme(vim_result);
1678 MZ_GC_CHECK();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001679 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001680
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001681 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001682 return result;
1683#else
1684 raise_vim_exn(_("expressions disabled at compile time"));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001685 // unreachable
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001686 return scheme_false;
1687#endif
1688}
1689
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001690/*
1691 * (range-start)
1692 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001693 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001694get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001695{
1696 return scheme_make_integer(range_start);
1697}
1698
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001699/*
1700 * (range-end)
1701 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001702 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001703get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001704{
1705 return scheme_make_integer(range_end);
1706}
1707
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001708/*
1709 * (beep)
1710 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001711 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001712mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001713{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001714 vim_beep(BO_LANG);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001715 return scheme_void;
1716}
1717
1718static Scheme_Object *M_global = NULL;
1719
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001720/*
1721 * (get-option {option-name}) [buffer/window]
1722 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001723 static Scheme_Object *
1724get_option(void *data, int argc, Scheme_Object **argv)
1725{
1726 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001727 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001728 char *strval;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001729 int rc;
Bram Moolenaar75676462013-01-30 14:55:42 +01001730 Scheme_Object *rval = NULL;
1731 Scheme_Object *name = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001732 int opt_flags = 0;
1733 buf_T *save_curb = curbuf;
1734 win_T *save_curw = curwin;
1735
Bram Moolenaar75676462013-01-30 14:55:42 +01001736 MZ_GC_DECL_REG(2);
1737 MZ_GC_VAR_IN_REG(0, rval);
1738 MZ_GC_VAR_IN_REG(1, name);
1739 MZ_GC_REG();
1740
1741 name = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001742
1743 if (argc > 1)
1744 {
1745 if (M_global == NULL)
1746 {
1747 MZ_REGISTER_STATIC(M_global);
1748 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001749 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001750 }
1751
1752 if (argv[1] == M_global)
1753 opt_flags = OPT_GLOBAL;
1754 else if (SCHEME_VIMBUFFERP(argv[1]))
1755 {
1756 curbuf = get_valid_buffer(argv[1]);
1757 opt_flags = OPT_LOCAL;
1758 }
1759 else if (SCHEME_VIMWINDOWP(argv[1]))
1760 {
1761 win_T *win = get_valid_window(argv[1]);
1762
1763 curwin = win;
1764 curbuf = win->w_buffer;
1765 opt_flags = OPT_LOCAL;
1766 }
1767 else
1768 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1769 }
1770
Bram Moolenaar75676462013-01-30 14:55:42 +01001771 rc = get_option_value(BYTE_STRING_VALUE(name), &value, (char_u **)&strval, opt_flags);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001772 curbuf = save_curb;
1773 curwin = save_curw;
1774
1775 switch (rc)
1776 {
1777 case 1:
Bram Moolenaar75676462013-01-30 14:55:42 +01001778 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001779 return scheme_make_integer_value(value);
1780 case 0:
Bram Moolenaar75676462013-01-30 14:55:42 +01001781 rval = scheme_make_byte_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001782 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001783 vim_free(strval);
Bram Moolenaar75676462013-01-30 14:55:42 +01001784 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001785 return rval;
1786 case -1:
1787 case -2:
Bram Moolenaar75676462013-01-30 14:55:42 +01001788 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001789 raise_vim_exn(_("hidden option"));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001790 //NOTREACHED
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001791 case -3:
Bram Moolenaar75676462013-01-30 14:55:42 +01001792 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001793 raise_vim_exn(_("unknown option"));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001794 //NOTREACHED
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001795 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001796 // unreachable
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001797 return scheme_void;
1798}
1799
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001800/*
1801 * (set-option {option-changing-string} [buffer/window])
1802 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001803 static Scheme_Object *
1804set_option(void *data, int argc, Scheme_Object **argv)
1805{
Bram Moolenaar75676462013-01-30 14:55:42 +01001806 char_u *command = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001807 int opt_flags = 0;
1808 buf_T *save_curb = curbuf;
1809 win_T *save_curw = curwin;
1810 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001811 Scheme_Object *cmd = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001812
Bram Moolenaar75676462013-01-30 14:55:42 +01001813 MZ_GC_DECL_REG(1);
1814 MZ_GC_VAR_IN_REG(0, cmd);
1815 MZ_GC_REG();
1816 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
1817
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001818 if (argc > 1)
1819 {
1820 if (M_global == NULL)
1821 {
1822 MZ_REGISTER_STATIC(M_global);
1823 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001824 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001825 }
1826
1827 if (argv[1] == M_global)
1828 opt_flags = OPT_GLOBAL;
1829 else if (SCHEME_VIMBUFFERP(argv[1]))
1830 {
1831 curbuf = get_valid_buffer(argv[1]);
1832 opt_flags = OPT_LOCAL;
1833 }
1834 else if (SCHEME_VIMWINDOWP(argv[1]))
1835 {
1836 win_T *win = get_valid_window(argv[1]);
1837 curwin = win;
1838 curbuf = win->w_buffer;
1839 opt_flags = OPT_LOCAL;
1840 }
1841 else
1842 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1843 }
1844
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001845 // do_set can modify cmd, make copy
Bram Moolenaar75676462013-01-30 14:55:42 +01001846 command = vim_strsave(BYTE_STRING_VALUE(cmd));
1847 MZ_GC_UNREG();
1848 do_set(command, opt_flags);
1849 vim_free(command);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001850 update_screen(NOT_VALID);
1851 curbuf = save_curb;
1852 curwin = save_curw;
1853 raise_if_error();
1854 return scheme_void;
1855}
1856
1857/*
1858 *===========================================================================
1859 * 5. Vim Window-related Manipulation Functions
1860 *===========================================================================
1861 */
1862
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001863/*
1864 * (curr-win)
1865 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001866 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001867get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001868{
1869 return (Scheme_Object *)get_vim_curr_window();
1870}
1871
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001872/*
1873 * (win-count)
1874 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001875 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001876get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001877{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001878 int n = 0;
Bram Moolenaard2142212013-01-30 17:41:50 +01001879 win_T *w;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001880
Bram Moolenaar29323592016-07-24 22:04:11 +02001881 FOR_ALL_WINDOWS(w)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001882 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001883 return scheme_make_integer(n);
1884}
1885
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001886/*
1887 * (get-win-list [buffer])
1888 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001889 static Scheme_Object *
1890get_window_list(void *data, int argc, Scheme_Object **argv)
1891{
1892 Vim_Prim *prim = (Vim_Prim *)data;
1893 vim_mz_buffer *buf;
1894 Scheme_Object *list;
Bram Moolenaard2142212013-01-30 17:41:50 +01001895 win_T *w = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001896
1897 buf = get_buffer_arg(prim->name, 0, argc, argv);
1898 list = scheme_null;
1899
Bram Moolenaard2142212013-01-30 17:41:50 +01001900 for ( ; w != NULL; w = w->w_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001901 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001902 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001903 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001904 MZ_GC_CHECK();
1905 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001906
1907 return list;
1908}
1909
1910 static Scheme_Object *
1911window_new(win_T *win)
1912{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001913 vim_mz_window *self = NULL;
1914
1915 MZ_GC_DECL_REG(1);
1916 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001917
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001918 // We need to handle deletion of windows underneath us.
1919 // If we add a "w_mzscheme_ref" field to the win_T structure,
1920 // then we can get at it in win_free() in vim.
1921 //
1922 // On a win_free() we set the Scheme object's win_T *field
1923 // to an invalid value. We trap all uses of a window
1924 // object, and reject them if the win_T *field is invalid.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001925 if (win->w_mzscheme_ref != NULL)
Bram Moolenaar75676462013-01-30 14:55:42 +01001926 return (Scheme_Object *)WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001927
Bram Moolenaar75676462013-01-30 14:55:42 +01001928 MZ_GC_REG();
1929 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_window));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001930 vim_memset(self, 0, sizeof(vim_mz_window));
Bram Moolenaar75676462013-01-30 14:55:42 +01001931#ifndef MZ_PRECISE_GC
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001932 scheme_dont_gc_ptr(self); // because win isn't visible to GC
Bram Moolenaar75676462013-01-30 14:55:42 +01001933#else
1934 win->w_mzscheme_ref = scheme_malloc_immobile_box(NULL);
1935#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001936 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001937 WINDOW_REF(win) = self;
1938 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001939 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001940 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001941
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001942 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001943 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001944}
1945
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001946/*
1947 * (get-win-num [window])
1948 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001949 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001950get_window_num(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001951{
Bram Moolenaard2142212013-01-30 17:41:50 +01001952 int nr = 1;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001953 Vim_Prim *prim = (Vim_Prim *)data;
1954 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001955 win_T *wp;
1956
1957 for (wp = firstwin; wp != win; wp = wp->w_next)
1958 ++nr;
1959
1960 return scheme_make_integer(nr);
1961}
1962
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001963/*
1964 * (get-win-by-num {windownum})
1965 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001966 static Scheme_Object *
1967get_window_by_num(void *data, int argc, Scheme_Object **argv)
1968{
1969 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaard2142212013-01-30 17:41:50 +01001970 win_T *win = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001971 int fnum;
1972
1973 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1974 if (fnum < 1)
1975 scheme_signal_error(_("window index is out of range"));
1976
Bram Moolenaard2142212013-01-30 17:41:50 +01001977 for ( ; win != NULL; win = win->w_next, --fnum)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001978 if (fnum == 1) // to be 1-based
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001979 return window_new(win);
1980
1981 return scheme_false;
1982}
1983
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001984/*
1985 * (get-win-buffer [window])
1986 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001987 static Scheme_Object *
1988get_window_buffer(void *data, int argc, Scheme_Object **argv)
1989{
1990 Vim_Prim *prim = (Vim_Prim *)data;
1991 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1992
1993 return buffer_new(win->win->w_buffer);
1994}
1995
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001996/*
1997 * (get-win-height [window])
1998 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001999 static Scheme_Object *
2000get_window_height(void *data, int argc, Scheme_Object **argv)
2001{
2002 Vim_Prim *prim = (Vim_Prim *)data;
2003 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2004
2005 return scheme_make_integer(win->win->w_height);
2006}
2007
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002008/*
2009 * (set-win-height {height} [window])
2010 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002011 static Scheme_Object *
2012set_window_height(void *data, int argc, Scheme_Object **argv)
2013{
2014 Vim_Prim *prim = (Vim_Prim *)data;
2015 vim_mz_window *win;
2016 win_T *savewin;
2017 int height;
2018
2019 win = get_window_arg(prim->name, 1, argc, argv);
2020 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2021
2022#ifdef FEAT_GUI
2023 need_mouse_correct = TRUE;
2024#endif
2025
2026 savewin = curwin;
2027 curwin = win->win;
2028 win_setheight(height);
2029 curwin = savewin;
2030
2031 raise_if_error();
2032 return scheme_void;
2033}
2034
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002035/*
2036 * (get-win-width [window])
2037 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002038 static Scheme_Object *
2039get_window_width(void *data, int argc, Scheme_Object **argv)
2040{
2041 Vim_Prim *prim = (Vim_Prim *)data;
2042 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2043
Bram Moolenaar02631462017-09-22 15:20:32 +02002044 return scheme_make_integer(win->win->w_width);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002045}
2046
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002047/*
2048 * (set-win-width {width} [window])
2049 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002050 static Scheme_Object *
2051set_window_width(void *data, int argc, Scheme_Object **argv)
2052{
2053 Vim_Prim *prim = (Vim_Prim *)data;
2054 vim_mz_window *win;
2055 win_T *savewin;
2056 int width = 0;
2057
2058 win = get_window_arg(prim->name, 1, argc, argv);
2059 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2060
2061# ifdef FEAT_GUI
2062 need_mouse_correct = TRUE;
2063# endif
2064
2065 savewin = curwin;
2066 curwin = win->win;
2067 win_setwidth(width);
2068 curwin = savewin;
2069
2070 raise_if_error();
2071 return scheme_void;
2072}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002073
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002074/*
2075 * (get-cursor [window]) -> (line . col)
2076 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002077 static Scheme_Object *
2078get_cursor(void *data, int argc, Scheme_Object **argv)
2079{
2080 Vim_Prim *prim = (Vim_Prim *)data;
2081 vim_mz_window *win;
2082 pos_T pos;
2083
2084 win = get_window_arg(prim->name, 0, argc, argv);
2085 pos = win->win->w_cursor;
2086 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
2087 scheme_make_integer_value((long)pos.col + 1));
2088}
2089
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002090/*
2091 * (set-cursor (line . col) [window])
2092 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002093 static Scheme_Object *
2094set_cursor(void *data, int argc, Scheme_Object **argv)
2095{
2096 Vim_Prim *prim = (Vim_Prim *)data;
2097 vim_mz_window *win;
2098 long lnum = 0;
2099 long col = 0;
2100
Bram Moolenaar555b2802005-05-19 21:08:39 +00002101#ifdef HAVE_SANDBOX
2102 sandbox_check();
2103#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002104 win = get_window_arg(prim->name, 1, argc, argv);
2105 GUARANTEE_PAIR(prim->name, 0);
2106
2107 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
2108 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
2109 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
2110
2111 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
2112 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
2113
2114 check_line_range(lnum, win->win->w_buffer);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002115 // don't know how to catch invalid column value
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002116
2117 win->win->w_cursor.lnum = lnum;
2118 win->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02002119 win->win->w_set_curswant = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002120 update_screen(VALID);
2121
2122 raise_if_error();
2123 return scheme_void;
2124}
2125/*
2126 *===========================================================================
2127 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002128 *===========================================================================
2129 */
2130
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002131/*
2132 * (open-buff {filename})
2133 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002134 static Scheme_Object *
2135mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
2136{
2137 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002138 int num = 0;
Bram Moolenaar75676462013-01-30 14:55:42 +01002139 Scheme_Object *onum = NULL;
2140 Scheme_Object *buf = NULL;
2141 Scheme_Object *fname;
2142
2143 MZ_GC_DECL_REG(3);
2144 MZ_GC_VAR_IN_REG(0, onum);
2145 MZ_GC_VAR_IN_REG(1, buf);
2146 MZ_GC_VAR_IN_REG(2, fname);
2147 MZ_GC_REG();
2148 fname = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002149
Bram Moolenaar555b2802005-05-19 21:08:39 +00002150#ifdef HAVE_SANDBOX
2151 sandbox_check();
2152#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002153 // TODO make open existing file
Bram Moolenaar75676462013-01-30 14:55:42 +01002154 num = buflist_add(BYTE_STRING_VALUE(fname), BLN_LISTED | BLN_CURBUF);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002155
2156 if (num == 0)
2157 raise_vim_exn(_("couldn't open buffer"));
2158
2159 onum = scheme_make_integer(num);
Bram Moolenaar75676462013-01-30 14:55:42 +01002160 buf = get_buffer_by_num(data, 1, &onum);
2161 MZ_GC_UNREG();
2162 return buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002163}
2164
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002165/*
2166 * (get-buff-by-num {buffernum})
2167 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002168 static Scheme_Object *
2169get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
2170{
2171 Vim_Prim *prim = (Vim_Prim *)data;
2172 buf_T *buf;
2173 int fnum;
2174
2175 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2176
Bram Moolenaar29323592016-07-24 22:04:11 +02002177 FOR_ALL_BUFFERS(buf)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002178 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002179 return buffer_new(buf);
2180
2181 return scheme_false;
2182}
2183
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002184/*
2185 * (get-buff-by-name {buffername})
2186 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002187 static Scheme_Object *
2188get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
2189{
2190 Vim_Prim *prim = (Vim_Prim *)data;
2191 buf_T *buf;
Bram Moolenaar75676462013-01-30 14:55:42 +01002192 Scheme_Object *buffer = NULL;
2193 Scheme_Object *fname = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002194
Bram Moolenaar75676462013-01-30 14:55:42 +01002195 MZ_GC_DECL_REG(2);
2196 MZ_GC_VAR_IN_REG(0, buffer);
2197 MZ_GC_VAR_IN_REG(1, fname);
2198 MZ_GC_REG();
2199 fname = GUARANTEED_STRING_ARG(prim->name, 0);
2200 buffer = scheme_false;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002201
Bram Moolenaar29323592016-07-24 22:04:11 +02002202 FOR_ALL_BUFFERS(buf)
Bram Moolenaar75676462013-01-30 14:55:42 +01002203 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002204 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002205 // empty string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002206 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002207 if (BYTE_STRING_VALUE(fname)[0] == NUL)
2208 buffer = buffer_new(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002209 }
Bram Moolenaar75676462013-01-30 14:55:42 +01002210 else if (!fnamecmp(buf->b_ffname, BYTE_STRING_VALUE(fname))
2211 || !fnamecmp(buf->b_sfname, BYTE_STRING_VALUE(fname)))
2212 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002213 // either short or long filename matches
Bram Moolenaar75676462013-01-30 14:55:42 +01002214 buffer = buffer_new(buf);
2215 }
2216 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002217
Bram Moolenaar75676462013-01-30 14:55:42 +01002218 MZ_GC_UNREG();
2219 return buffer;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002220}
2221
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002222/*
2223 * (get-next-buff [buffer])
2224 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002225 static Scheme_Object *
2226get_next_buffer(void *data, int argc, Scheme_Object **argv)
2227{
2228 Vim_Prim *prim = (Vim_Prim *)data;
2229 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2230
2231 if (buf->b_next == NULL)
2232 return scheme_false;
2233 else
2234 return buffer_new(buf->b_next);
2235}
2236
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002237/*
2238 * (get-prev-buff [buffer])
2239 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002240 static Scheme_Object *
2241get_prev_buffer(void *data, int argc, Scheme_Object **argv)
2242{
2243 Vim_Prim *prim = (Vim_Prim *)data;
2244 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2245
2246 if (buf->b_prev == NULL)
2247 return scheme_false;
2248 else
2249 return buffer_new(buf->b_prev);
2250}
2251
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002252/*
2253 * (get-buff-num [buffer])
2254 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002255 static Scheme_Object *
2256get_buffer_num(void *data, int argc, Scheme_Object **argv)
2257{
2258 Vim_Prim *prim = (Vim_Prim *)data;
2259 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2260
2261 return scheme_make_integer(buf->buf->b_fnum);
2262}
2263
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002264/*
2265 * (buff-count)
2266 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002267 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002268get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002269{
2270 buf_T *b;
2271 int n = 0;
2272
Bram Moolenaar29323592016-07-24 22:04:11 +02002273 FOR_ALL_BUFFERS(b) ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002274 return scheme_make_integer(n);
2275}
2276
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002277/*
2278 * (get-buff-name [buffer])
2279 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002280 static Scheme_Object *
2281get_buffer_name(void *data, int argc, Scheme_Object **argv)
2282{
2283 Vim_Prim *prim = (Vim_Prim *)data;
2284 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2285
Bram Moolenaar75676462013-01-30 14:55:42 +01002286 return scheme_make_byte_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002287}
2288
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002289/*
2290 * (curr-buff)
2291 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002292 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002293get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002294{
2295 return (Scheme_Object *)get_vim_curr_buffer();
2296}
2297
2298 static Scheme_Object *
2299buffer_new(buf_T *buf)
2300{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002301 vim_mz_buffer *self = NULL;
2302
2303 MZ_GC_DECL_REG(1);
2304 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002305
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002306 // We need to handle deletion of buffers underneath us.
2307 // If we add a "b_mzscheme_ref" field to the buf_T structure,
2308 // then we can get at it in buf_freeall() in vim.
Bram Moolenaare344bea2005-09-01 20:46:49 +00002309 if (buf->b_mzscheme_ref)
Bram Moolenaar75676462013-01-30 14:55:42 +01002310 return (Scheme_Object *)BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002311
Bram Moolenaar75676462013-01-30 14:55:42 +01002312 MZ_GC_REG();
2313 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_buffer));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002314 vim_memset(self, 0, sizeof(vim_mz_buffer));
Bram Moolenaar75676462013-01-30 14:55:42 +01002315#ifndef MZ_PRECISE_GC
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002316 scheme_dont_gc_ptr(self); // because buf isn't visible to GC
Bram Moolenaar75676462013-01-30 14:55:42 +01002317#else
2318 buf->b_mzscheme_ref = scheme_malloc_immobile_box(NULL);
2319#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002320 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01002321 BUFFER_REF(buf) = self;
2322 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002323 self->buf = buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002324 self->so.type = mz_buffer_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002325
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002326 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01002327 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002328}
2329
2330/*
2331 * (get-buff-size [buffer])
2332 *
2333 * Get the size (number of lines) in the current buffer.
2334 */
2335 static Scheme_Object *
2336get_buffer_size(void *data, int argc, Scheme_Object **argv)
2337{
2338 Vim_Prim *prim = (Vim_Prim *)data;
2339 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2340
2341 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
2342}
2343
2344/*
2345 * (get-buff-line {linenr} [buffer])
2346 *
2347 * Get a line from the specified buffer. The line number is
2348 * in Vim format (1-based). The line is returned as a MzScheme
2349 * string object.
2350 */
2351 static Scheme_Object *
2352get_buffer_line(void *data, int argc, Scheme_Object **argv)
2353{
2354 Vim_Prim *prim = (Vim_Prim *)data;
2355 vim_mz_buffer *buf;
2356 int linenr;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002357 char_u *line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002358
2359 buf = get_buffer_arg(prim->name, 1, argc, argv);
2360 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2361 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2362
2363 raise_if_error();
Bram Moolenaar75676462013-01-30 14:55:42 +01002364 return scheme_make_byte_string((char *)line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002365}
2366
2367
2368/*
2369 * (get-buff-line-list {start} {end} [buffer])
2370 *
2371 * Get a list of lines from the specified buffer. The line numbers
2372 * are in Vim format (1-based). The range is from lo up to, but not
2373 * including, hi. The list is returned as a list of string objects.
2374 */
2375 static Scheme_Object *
2376get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2377{
2378 Vim_Prim *prim = (Vim_Prim *)data;
2379 vim_mz_buffer *buf;
2380 int i, hi, lo, n;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002381 Scheme_Object *list = NULL;
2382
2383 MZ_GC_DECL_REG(1);
2384 MZ_GC_VAR_IN_REG(0, list);
2385 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002386
2387 buf = get_buffer_arg(prim->name, 2, argc, argv);
2388 list = scheme_null;
2389 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2390 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2391
2392 /*
2393 * Handle some error conditions
2394 */
2395 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002396 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002397
2398 if (hi < 0)
2399 hi = 0;
2400 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002401 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002402
2403 n = hi - lo;
2404
2405 for (i = n; i >= 0; --i)
2406 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002407 Scheme_Object *str = scheme_make_byte_string(
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002408 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2409 raise_if_error();
2410
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002411 // Set the list item
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002412 list = scheme_make_pair(str, list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002413 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002414 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002415 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002416 return list;
2417}
2418
2419/*
2420 * (set-buff-line {linenr} {string/#f} [buffer])
2421 *
2422 * Replace a line in the specified buffer. The line number is
2423 * in Vim format (1-based). The replacement line is given as
2424 * an MzScheme string object. The object is checked for validity
2425 * and correct format. An exception is thrown if the values are not
2426 * the correct format.
2427 *
2428 * It returns a Scheme Object that indicates the length of the
2429 * string changed.
2430 */
2431 static Scheme_Object *
2432set_buffer_line(void *data, int argc, Scheme_Object **argv)
2433{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002434 // First of all, we check the value of the supplied MzScheme object.
2435 // There are three cases:
2436 // 1. #f - this is a deletion.
2437 // 2. A string - this is a replacement.
2438 // 3. Anything else - this is an error.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002439 Vim_Prim *prim = (Vim_Prim *)data;
2440 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002441 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002442 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002443 int n;
2444
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002445 MZ_GC_DECL_REG(1);
2446 MZ_GC_VAR_IN_REG(0, line);
2447 MZ_GC_REG();
2448
Bram Moolenaar555b2802005-05-19 21:08:39 +00002449#ifdef HAVE_SANDBOX
2450 sandbox_check();
2451#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002452 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2453 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002454 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002455 line = argv[1];
2456 buf = get_buffer_arg(prim->name, 2, argc, argv);
2457
2458 check_line_range(n, buf->buf);
2459
2460 if (SCHEME_FALSEP(line))
2461 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002462 buf_T *savebuf = curbuf;
2463
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002464 curbuf = buf->buf;
2465
2466 if (u_savedel((linenr_T)n, 1L) == FAIL)
2467 {
2468 curbuf = savebuf;
2469 raise_vim_exn(_("cannot save undo information"));
2470 }
2471 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2472 {
2473 curbuf = savebuf;
2474 raise_vim_exn(_("cannot delete line"));
2475 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002476 if (buf->buf == curwin->w_buffer)
2477 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002478 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002479
2480 curbuf = savebuf;
2481
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002482 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002483 raise_if_error();
2484 return scheme_void;
2485 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002486 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002487 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002488 // Otherwise it's a line
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002489 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002490
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002491 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002492
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002493 curbuf = buf->buf;
2494
2495 if (u_savesub((linenr_T)n) == FAIL)
2496 {
2497 curbuf = savebuf;
2498 vim_free(save);
2499 raise_vim_exn(_("cannot save undo information"));
2500 }
2501 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2502 {
2503 curbuf = savebuf;
2504 vim_free(save);
2505 raise_vim_exn(_("cannot replace line"));
2506 }
2507 else
2508 {
2509 vim_free(save);
2510 changed_bytes((linenr_T)n, 0);
2511 }
2512
2513 curbuf = savebuf;
2514
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002515 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002516 if (buf->buf == curwin->w_buffer)
2517 check_cursor_col();
2518
2519 MZ_GC_UNREG();
2520 raise_if_error();
2521 return scheme_void;
2522 }
2523}
2524
2525 static void
2526free_array(char **array)
2527{
2528 char **curr = array;
2529 while (*curr != NULL)
2530 vim_free(*curr++);
2531 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002532}
2533
2534/*
2535 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2536 *
2537 * Replace a range of lines in the specified buffer. The line numbers are in
2538 * Vim format (1-based). The range is from lo up to, but not including, hi.
2539 * The replacement lines are given as a Scheme list of string objects. The
2540 * list is checked for validity and correct format.
2541 *
2542 * Errors are returned as a value of FAIL. The return value is OK on success.
2543 * If OK is returned and len_change is not NULL, *len_change is set to the
2544 * change in the buffer length.
2545 */
2546 static Scheme_Object *
2547set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2548{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002549 // First of all, we check the type of the supplied MzScheme object.
2550 // There are three cases:
2551 // 1. #f - this is a deletion.
2552 // 2. A list - this is a replacement.
2553 // 3. Anything else - this is an error.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002554 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002555 vim_mz_buffer *buf = NULL;
2556 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002557 int i, old_len, new_len, hi, lo;
2558 long extra;
2559
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002560 MZ_GC_DECL_REG(1);
2561 MZ_GC_VAR_IN_REG(0, line_list);
2562 MZ_GC_REG();
2563
Bram Moolenaar555b2802005-05-19 21:08:39 +00002564#ifdef HAVE_SANDBOX
2565 sandbox_check();
2566#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002567 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2568 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2569 if (!SCHEME_PAIRP(argv[2])
2570 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2571 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2572 line_list = argv[2];
2573 buf = get_buffer_arg(prim->name, 3, argc, argv);
2574 old_len = hi - lo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002575 if (old_len < 0) // process inverse values wisely
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002576 {
2577 i = lo;
2578 lo = hi;
2579 hi = i;
2580 old_len = -old_len;
2581 }
2582 extra = 0;
2583
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002584 check_line_range(lo, buf->buf); // inclusive
2585 check_line_range(hi - 1, buf->buf); // exclusive
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002586
2587 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2588 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002589 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002590 curbuf = buf->buf;
2591
2592 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2593 {
2594 curbuf = savebuf;
2595 raise_vim_exn(_("cannot save undo information"));
2596 }
2597 else
2598 {
2599 for (i = 0; i < old_len; i++)
2600 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2601 {
2602 curbuf = savebuf;
2603 raise_vim_exn(_("cannot delete line"));
2604 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002605 if (buf->buf == curwin->w_buffer)
2606 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002607 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002608 }
2609
2610 curbuf = savebuf;
2611
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002612 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002613 raise_if_error();
2614 return scheme_void;
2615 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002616 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002617 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002618 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002619
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002620 // List
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002621 new_len = scheme_proper_list_length(line_list);
2622 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002623 if (new_len < 0) // improper or cyclic list
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002624 scheme_wrong_type(prim->name, "proper list",
2625 2, argc, argv);
2626 else
2627 {
2628 char **array = NULL;
2629 Scheme_Object *line = NULL;
2630 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002631
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002632 MZ_GC_DECL_REG(2);
2633 MZ_GC_VAR_IN_REG(0, line);
2634 MZ_GC_VAR_IN_REG(1, rest);
2635 MZ_GC_REG();
2636
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002637 array = ALLOC_MULT(char *, new_len + 1);
Bram Moolenaar75676462013-01-30 14:55:42 +01002638 vim_memset(array, 0, (new_len+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002639
2640 rest = line_list;
2641 for (i = 0; i < new_len; ++i)
2642 {
2643 line = SCHEME_CAR(rest);
2644 rest = SCHEME_CDR(rest);
2645 if (!SCHEME_STRINGP(line))
2646 {
2647 free_array(array);
2648 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2649 }
2650 array[i] = string_to_line(line);
2651 }
2652
2653 curbuf = buf->buf;
2654
2655 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2656 {
2657 curbuf = savebuf;
2658 free_array(array);
2659 raise_vim_exn(_("cannot save undo information"));
2660 }
2661
2662 /*
2663 * If the size of the range is reducing (ie, new_len < old_len) we
2664 * need to delete some old_len. We do this at the start, by
2665 * repeatedly deleting line "lo".
2666 */
2667 for (i = 0; i < old_len - new_len; ++i)
2668 {
2669 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2670 {
2671 curbuf = savebuf;
2672 free_array(array);
2673 raise_vim_exn(_("cannot delete line"));
2674 }
2675 extra--;
2676 }
2677
2678 /*
2679 * For as long as possible, replace the existing old_len with the
2680 * new old_len. This is a more efficient operation, as it requires
2681 * less memory allocation and freeing.
2682 */
2683 for (i = 0; i < old_len && i < new_len; i++)
2684 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2685 {
2686 curbuf = savebuf;
2687 free_array(array);
2688 raise_vim_exn(_("cannot replace line"));
2689 }
2690
2691 /*
2692 * Now we may need to insert the remaining new_len. We don't need to
2693 * free the string passed back because MzScheme has control of that
2694 * memory.
2695 */
2696 while (i < new_len)
2697 {
2698 if (ml_append((linenr_T)(lo + i - 1),
2699 (char_u *)array[i], 0, FALSE) == FAIL)
2700 {
2701 curbuf = savebuf;
2702 free_array(array);
2703 raise_vim_exn(_("cannot insert line"));
2704 }
2705 ++i;
2706 ++extra;
2707 }
2708 MZ_GC_UNREG();
2709 free_array(array);
2710 }
2711
2712 /*
2713 * Adjust marks. Invalidate any which lie in the
2714 * changed range, and move any in the remainder of the buffer.
2715 */
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002716 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2717 (long)MAXLNUM, (long)extra);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002718 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2719
2720 if (buf->buf == curwin->w_buffer)
2721 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002722 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002723
2724 MZ_GC_UNREG();
2725 raise_if_error();
2726 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002727 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002728}
2729
2730/*
2731 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2732 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002733 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002734 * The line number is in Vim format (1-based). The lines to be inserted are
2735 * given as an MzScheme list of string objects or as a single string. The lines
2736 * to be added are checked for validity and correct format. Errors are
2737 * returned as a value of FAIL. The return value is OK on success.
2738 * If OK is returned and len_change is not NULL, *len_change
2739 * is set to the change in the buffer length.
2740 */
2741 static Scheme_Object *
2742insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2743{
2744 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002745 vim_mz_buffer *buf = NULL;
2746 Scheme_Object *list = NULL;
2747 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002748 int i, n, size;
2749
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002750 MZ_GC_DECL_REG(1);
2751 MZ_GC_VAR_IN_REG(0, list);
2752 MZ_GC_REG();
2753
Bram Moolenaar555b2802005-05-19 21:08:39 +00002754#ifdef HAVE_SANDBOX
2755 sandbox_check();
2756#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002757 /*
2758 * First of all, we check the type of the supplied MzScheme object.
2759 * It must be a string or a list, or the call is in error.
2760 */
2761 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2762 list = argv[1];
2763
2764 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2765 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2766 buf = get_buffer_arg(prim->name, 2, argc, argv);
2767
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002768 if (n != 0) // 0 can be used in insert
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002769 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002770 if (SCHEME_STRINGP(list))
2771 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002772 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002773
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002774 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002775 curbuf = buf->buf;
2776
2777 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2778 {
2779 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002780 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002781 raise_vim_exn(_("cannot save undo information"));
2782 }
2783 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2784 {
2785 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002786 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002787 raise_vim_exn(_("cannot insert line"));
2788 }
2789 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002790 {
2791 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002792 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002793 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002794
2795 curbuf = savebuf;
2796 update_screen(VALID);
2797
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002798 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002799 raise_if_error();
2800 return scheme_void;
2801 }
2802
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002803 // List
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002804 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002805 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002806 if (size < 0) // improper or cyclic list
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002807 scheme_wrong_type(prim->name, "proper list",
2808 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002809 else
2810 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002811 Scheme_Object *line = NULL;
2812 Scheme_Object *rest = NULL;
2813 char **array;
2814 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002815
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002816 MZ_GC_DECL_REG(2);
2817 MZ_GC_VAR_IN_REG(0, line);
2818 MZ_GC_VAR_IN_REG(1, rest);
2819 MZ_GC_REG();
2820
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002821 array = ALLOC_MULT(char *, size + 1);
Bram Moolenaar75676462013-01-30 14:55:42 +01002822 vim_memset(array, 0, (size+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002823
2824 rest = list;
2825 for (i = 0; i < size; ++i)
2826 {
2827 line = SCHEME_CAR(rest);
2828 rest = SCHEME_CDR(rest);
2829 array[i] = string_to_line(line);
2830 }
2831
2832 curbuf = buf->buf;
2833
2834 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2835 {
2836 curbuf = savebuf;
2837 free_array(array);
2838 raise_vim_exn(_("cannot save undo information"));
2839 }
2840 else
2841 {
2842 for (i = 0; i < size; ++i)
2843 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2844 0, FALSE) == FAIL)
2845 {
2846 curbuf = savebuf;
2847 free_array(array);
2848 raise_vim_exn(_("cannot insert line"));
2849 }
2850
2851 if (i > 0)
2852 appended_lines_mark((linenr_T)n, (long)i);
2853 }
2854 free_array(array);
2855 MZ_GC_UNREG();
2856 curbuf = savebuf;
2857 update_screen(VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002858 }
2859
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002860 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002861 raise_if_error();
2862 return scheme_void;
2863}
2864
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002865/*
2866 * Predicates
2867 */
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002868/*
2869 * (buff? obj)
2870 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002871 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002872vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002873{
2874 if (SCHEME_VIMBUFFERP(argv[0]))
2875 return scheme_true;
2876 else
2877 return scheme_false;
2878}
2879
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002880/*
2881 * (win? obj)
2882 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002883 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002884vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002885{
2886 if (SCHEME_VIMWINDOWP(argv[0]))
2887 return scheme_true;
2888 else
2889 return scheme_false;
2890}
2891
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002892/*
2893 * (buff-valid? obj)
2894 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002895 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002896vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002897{
2898 if (SCHEME_VIMBUFFERP(argv[0])
2899 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2900 return scheme_true;
2901 else
2902 return scheme_false;
2903}
2904
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002905/*
2906 * (win-valid? obj)
2907 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002908 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002909vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002910{
2911 if (SCHEME_VIMWINDOWP(argv[0])
2912 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2913 return scheme_true;
2914 else
2915 return scheme_false;
2916}
2917
2918/*
2919 *===========================================================================
2920 * Utilities
2921 *===========================================================================
2922 */
2923
2924/*
2925 * Convert an MzScheme string into a Vim line.
2926 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002927 * All internal nulls are replaced by newline characters.
2928 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002929 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002930 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002931 */
2932 static char *
2933string_to_line(Scheme_Object *obj)
2934{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002935 char *scheme_str = NULL;
2936 char *vim_str = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01002937 OUTPUT_LEN_TYPE len;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002938 int i;
2939
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002940 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002941
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002942 // Error checking: String must not contain newlines, as we
2943 // are replacing a single line, and we must replace it with
2944 // a single line.
Bram Moolenaar75676462013-01-30 14:55:42 +01002945 if (memchr(scheme_str, '\n', len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002946 scheme_signal_error(_("string cannot contain newlines"));
2947
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002948 vim_str = alloc(len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002949
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002950 // Create a copy of the string, with internal nulls replaced by
2951 // newline characters, as is the vim convention.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002952 for (i = 0; i < len; ++i)
2953 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002954 if (scheme_str[i] == '\0')
2955 vim_str[i] = '\n';
2956 else
2957 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002958 }
2959
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002960 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002961
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002962 MZ_GC_CHECK();
2963 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002964}
2965
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002966#ifdef FEAT_EVAL
2967/*
2968 * Convert Vim value into MzScheme, adopted from if_python.c
2969 */
2970 static Scheme_Object *
Bram Moolenaar75676462013-01-30 14:55:42 +01002971vim_to_mzscheme(typval_T *vim_value)
2972{
2973 Scheme_Object *result = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002974 // hash table to store visited values to avoid infinite loops
Bram Moolenaar75676462013-01-30 14:55:42 +01002975 Scheme_Hash_Table *visited = NULL;
2976
2977 MZ_GC_DECL_REG(2);
2978 MZ_GC_VAR_IN_REG(0, result);
2979 MZ_GC_VAR_IN_REG(1, visited);
2980 MZ_GC_REG();
2981
2982 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2983 MZ_GC_CHECK();
2984
2985 result = vim_to_mzscheme_impl(vim_value, 1, visited);
2986
2987 MZ_GC_UNREG();
2988 return result;
2989}
2990
2991 static Scheme_Object *
2992vim_to_mzscheme_impl(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002993{
2994 Scheme_Object *result = NULL;
2995 int new_value = TRUE;
2996
Bram Moolenaar75676462013-01-30 14:55:42 +01002997 MZ_GC_DECL_REG(2);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002998 MZ_GC_VAR_IN_REG(0, result);
Bram Moolenaar75676462013-01-30 14:55:42 +01002999 MZ_GC_VAR_IN_REG(1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003000 MZ_GC_REG();
3001
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003002 // Avoid infinite recursion
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003003 if (depth > 100)
3004 {
3005 MZ_GC_UNREG();
3006 return scheme_void;
3007 }
3008
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003009 // Check if we run into a recursive loop. The item must be in visited
3010 // then and we can use it again.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003011 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
3012 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003013 if (result != NULL) // found, do nothing
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003014 new_value = FALSE;
3015 else if (vim_value->v_type == VAR_STRING)
3016 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003017 result = scheme_make_byte_string((char *)vim_value->vval.v_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003018 MZ_GC_CHECK();
3019 }
3020 else if (vim_value->v_type == VAR_NUMBER)
3021 {
3022 result = scheme_make_integer((long)vim_value->vval.v_number);
3023 MZ_GC_CHECK();
3024 }
3025# ifdef FEAT_FLOAT
3026 else if (vim_value->v_type == VAR_FLOAT)
3027 {
3028 result = scheme_make_double((double)vim_value->vval.v_float);
3029 MZ_GC_CHECK();
3030 }
3031# endif
3032 else if (vim_value->v_type == VAR_LIST)
3033 {
3034 list_T *list = vim_value->vval.v_list;
3035 listitem_T *curr;
3036
3037 if (list == NULL || list->lv_first == NULL)
3038 result = scheme_null;
3039 else
3040 {
3041 Scheme_Object *obj = NULL;
3042
3043 MZ_GC_DECL_REG(1);
3044 MZ_GC_VAR_IN_REG(0, obj);
3045 MZ_GC_REG();
3046
3047 curr = list->lv_last;
Bram Moolenaar75676462013-01-30 14:55:42 +01003048 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003049 result = scheme_make_pair(obj, scheme_null);
3050 MZ_GC_CHECK();
3051
3052 while (curr != list->lv_first)
3053 {
3054 curr = curr->li_prev;
Bram Moolenaar75676462013-01-30 14:55:42 +01003055 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003056 result = scheme_make_pair(obj, result);
3057 MZ_GC_CHECK();
3058 }
3059 }
3060 MZ_GC_UNREG();
3061 }
3062 else if (vim_value->v_type == VAR_DICT)
3063 {
3064 Scheme_Object *key = NULL;
3065 Scheme_Object *obj = NULL;
3066
3067 MZ_GC_DECL_REG(2);
3068 MZ_GC_VAR_IN_REG(0, key);
3069 MZ_GC_VAR_IN_REG(1, obj);
3070 MZ_GC_REG();
3071
3072 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
3073 MZ_GC_CHECK();
3074 if (vim_value->vval.v_dict != NULL)
3075 {
3076 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
3077 long_u todo = ht->ht_used;
3078 hashitem_T *hi;
3079 dictitem_T *di;
3080
3081 for (hi = ht->ht_array; todo > 0; ++hi)
3082 {
3083 if (!HASHITEM_EMPTY(hi))
3084 {
3085 --todo;
3086
3087 di = dict_lookup(hi);
Bram Moolenaar75676462013-01-30 14:55:42 +01003088 obj = vim_to_mzscheme_impl(&di->di_tv, depth + 1, visited);
3089 key = scheme_make_byte_string((char *)hi->hi_key);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003090 MZ_GC_CHECK();
3091 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
3092 MZ_GC_CHECK();
3093 }
3094 }
3095 }
3096 MZ_GC_UNREG();
3097 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003098 else if (vim_value->v_type == VAR_FUNC)
3099 {
3100 Scheme_Object *funcname = NULL;
3101
3102 MZ_GC_DECL_REG(1);
3103 MZ_GC_VAR_IN_REG(0, funcname);
3104 MZ_GC_REG();
3105
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003106 // FIXME: func_ref() and func_unref() are needed.
Bram Moolenaar75676462013-01-30 14:55:42 +01003107 funcname = scheme_make_byte_string((char *)vim_value->vval.v_string);
3108 MZ_GC_CHECK();
3109 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3110 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3111 MZ_GC_CHECK();
3112
3113 MZ_GC_UNREG();
3114 }
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003115 else if (vim_value->v_type == VAR_PARTIAL)
3116 {
3117 if (vim_value->vval.v_partial == NULL)
3118 result = scheme_null;
3119 else
3120 {
3121 Scheme_Object *funcname = NULL;
3122
3123 MZ_GC_DECL_REG(1);
3124 MZ_GC_VAR_IN_REG(0, funcname);
3125 MZ_GC_REG();
3126
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003127 // FIXME: func_ref() and func_unref() are needed.
3128 // TODO: Support pt_dict and pt_argv.
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003129 funcname = scheme_make_byte_string(
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003130 (char *)partial_name(vim_value->vval.v_partial));
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003131 MZ_GC_CHECK();
3132 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3133 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3134 MZ_GC_CHECK();
3135
3136 MZ_GC_UNREG();
3137 }
3138 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003139 else if (vim_value->v_type == VAR_SPECIAL)
3140 {
3141 if (vim_value->vval.v_number <= VVAL_TRUE)
3142 result = scheme_make_integer((long)vim_value->vval.v_number);
3143 else
3144 result = scheme_null;
3145 MZ_GC_CHECK();
3146 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003147 else
3148 {
3149 result = scheme_void;
3150 new_value = FALSE;
3151 }
3152 if (new_value)
3153 {
3154 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
3155 MZ_GC_CHECK();
3156 }
3157 MZ_GC_UNREG();
3158 return result;
3159}
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003160
3161 static int
Bram Moolenaar75676462013-01-30 14:55:42 +01003162mzscheme_to_vim(Scheme_Object *obj, typval_T *tv)
3163{
3164 int i, status;
3165 Scheme_Hash_Table *visited = NULL;
3166
3167 MZ_GC_DECL_REG(2);
3168 MZ_GC_VAR_IN_REG(0, obj);
3169 MZ_GC_VAR_IN_REG(1, visited);
3170 MZ_GC_REG();
3171
3172 visited = scheme_make_hash_table(SCHEME_hash_ptr);
3173 MZ_GC_CHECK();
3174
3175 status = mzscheme_to_vim_impl(obj, tv, 1, visited);
3176 for (i = 0; i < visited->size; ++i)
3177 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003178 // free up remembered objects
Bram Moolenaar75676462013-01-30 14:55:42 +01003179 if (visited->vals[i] != NULL)
3180 free_tv((typval_T *)visited->vals[i]);
3181 }
3182
3183 MZ_GC_UNREG();
3184 return status;
3185}
3186 static int
3187mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003188 Scheme_Hash_Table *visited)
3189{
3190 int status = OK;
3191 typval_T *found;
Bram Moolenaar75676462013-01-30 14:55:42 +01003192
3193 MZ_GC_DECL_REG(2);
3194 MZ_GC_VAR_IN_REG(0, obj);
3195 MZ_GC_VAR_IN_REG(1, visited);
3196 MZ_GC_REG();
3197
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003198 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003199 if (depth > 100) // limit the deepest recursion level
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003200 {
3201 tv->v_type = VAR_NUMBER;
3202 tv->vval.v_number = 0;
3203 return FAIL;
3204 }
3205
3206 found = (typval_T *)scheme_hash_get(visited, obj);
3207 if (found != NULL)
3208 copy_tv(found, tv);
3209 else if (SCHEME_VOIDP(obj))
3210 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003211 tv->v_type = VAR_SPECIAL;
3212 tv->vval.v_number = VVAL_NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003213 }
3214 else if (SCHEME_INTP(obj))
3215 {
3216 tv->v_type = VAR_NUMBER;
3217 tv->vval.v_number = SCHEME_INT_VAL(obj);
3218 }
3219 else if (SCHEME_BOOLP(obj))
3220 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003221 tv->v_type = VAR_SPECIAL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003222 tv->vval.v_number = SCHEME_TRUEP(obj);
3223 }
3224# ifdef FEAT_FLOAT
3225 else if (SCHEME_DBLP(obj))
3226 {
3227 tv->v_type = VAR_FLOAT;
3228 tv->vval.v_float = SCHEME_DBL_VAL(obj);
3229 }
3230# endif
Bram Moolenaar75676462013-01-30 14:55:42 +01003231 else if (SCHEME_BYTE_STRINGP(obj))
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003232 {
3233 tv->v_type = VAR_STRING;
Bram Moolenaar75676462013-01-30 14:55:42 +01003234 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(obj));
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003235 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003236# if MZSCHEME_VERSION_MAJOR >= 299
3237 else if (SCHEME_CHAR_STRINGP(obj))
3238 {
3239 Scheme_Object *tmp = NULL;
3240 MZ_GC_DECL_REG(1);
3241 MZ_GC_VAR_IN_REG(0, tmp);
3242 MZ_GC_REG();
3243
3244 tmp = scheme_char_string_to_byte_string(obj);
3245 tv->v_type = VAR_STRING;
3246 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(tmp));
3247 MZ_GC_UNREG();
3248 }
3249#endif
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003250 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
3251 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3252 {
3253 list_T *list = list_alloc();
3254 if (list == NULL)
3255 status = FAIL;
3256 else
3257 {
3258 int i;
3259 Scheme_Object *curr = NULL;
3260 Scheme_Object *cval = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003261 // temporary var to hold current element of vectors and pairs
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003262 typval_T *v;
3263
3264 MZ_GC_DECL_REG(2);
3265 MZ_GC_VAR_IN_REG(0, curr);
3266 MZ_GC_VAR_IN_REG(1, cval);
3267 MZ_GC_REG();
3268
3269 tv->v_type = VAR_LIST;
3270 tv->vval.v_list = list;
3271 ++list->lv_refcount;
3272
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003273 v = ALLOC_ONE(typval_T);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003274 if (v == NULL)
3275 status = FAIL;
3276 else
3277 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003278 // add the value in advance to allow handling of self-referential
3279 // data structures
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003280 typval_T *visited_tv = ALLOC_ONE(typval_T);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003281 copy_tv(tv, visited_tv);
3282 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3283
3284 if (SCHEME_VECTORP(obj))
3285 {
3286 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
3287 {
3288 cval = SCHEME_VEC_ELS(obj)[i];
Bram Moolenaar75676462013-01-30 14:55:42 +01003289 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003290 if (status == FAIL)
3291 break;
3292 status = list_append_tv(list, v);
3293 clear_tv(v);
3294 if (status == FAIL)
3295 break;
3296 }
3297 }
3298 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3299 {
3300 for (curr = obj;
3301 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
3302 curr = SCHEME_CDR(curr))
3303 {
3304 cval = SCHEME_CAR(curr);
Bram Moolenaar75676462013-01-30 14:55:42 +01003305 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003306 if (status == FAIL)
3307 break;
3308 status = list_append_tv(list, v);
3309 clear_tv(v);
3310 if (status == FAIL)
3311 break;
3312 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003313 // improper list not terminated with null
3314 // need to handle the last element
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003315 if (status == OK && !SCHEME_NULLP(curr))
3316 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003317 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003318 if (status == OK)
3319 {
3320 status = list_append_tv(list, v);
3321 clear_tv(v);
3322 }
3323 }
3324 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003325 // nothing to do for scheme_null
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003326 vim_free(v);
3327 }
3328 MZ_GC_UNREG();
3329 }
3330 }
3331 else if (SCHEME_HASHTP(obj))
3332 {
3333 int i;
3334 dict_T *dict;
3335 Scheme_Object *key = NULL;
3336 Scheme_Object *val = NULL;
3337
3338 MZ_GC_DECL_REG(2);
3339 MZ_GC_VAR_IN_REG(0, key);
3340 MZ_GC_VAR_IN_REG(1, val);
3341 MZ_GC_REG();
3342
3343 dict = dict_alloc();
3344 if (dict == NULL)
3345 status = FAIL;
3346 else
3347 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003348 typval_T *visited_tv = ALLOC_ONE(typval_T);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003349
3350 tv->v_type = VAR_DICT;
3351 tv->vval.v_dict = dict;
3352 ++dict->dv_refcount;
3353
3354 copy_tv(tv, visited_tv);
3355 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3356
3357 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
3358 {
3359 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
3360 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003361 // generate item for `display'ed Scheme key
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003362 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
3363 ((Scheme_Hash_Table *) obj)->keys[i]));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003364 // convert Scheme val to Vim and add it to the dict
Bram Moolenaar75676462013-01-30 14:55:42 +01003365 if (mzscheme_to_vim_impl(((Scheme_Hash_Table *) obj)->vals[i],
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003366 &item->di_tv, depth + 1, visited) == FAIL
3367 || dict_add(dict, item) == FAIL)
3368 {
3369 dictitem_free(item);
3370 status = FAIL;
3371 break;
3372 }
3373 }
3374
3375 }
3376 }
3377 MZ_GC_UNREG();
3378 }
3379 else
3380 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003381 // `display' any other value to string
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003382 tv->v_type = VAR_STRING;
3383 tv->vval.v_string = (char_u *)string_to_line(obj);
3384 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003385 MZ_GC_UNREG();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003386 return status;
3387}
3388
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003389/*
3390 * Scheme prim procedure wrapping Vim funcref
3391 */
Bram Moolenaar75676462013-01-30 14:55:42 +01003392 static Scheme_Object *
3393vim_funcref(void *name, int argc, Scheme_Object **argv)
3394{
3395 int i;
3396 typval_T args;
3397 int status = OK;
3398 Scheme_Object *result = NULL;
3399 list_T *list = list_alloc();
3400
3401 MZ_GC_DECL_REG(1);
3402 MZ_GC_VAR_IN_REG(0, result);
3403 MZ_GC_REG();
3404
3405 result = scheme_void;
3406 if (list == NULL)
3407 status = FAIL;
3408 else
3409 {
3410 args.v_type = VAR_LIST;
3411 args.vval.v_list = list;
3412 ++list->lv_refcount;
3413 for (i = 0; status == OK && i < argc; ++i)
3414 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003415 typval_T *v = ALLOC_ONE(typval_T);
Bram Moolenaar75676462013-01-30 14:55:42 +01003416 if (v == NULL)
3417 status = FAIL;
3418 else
3419 {
3420 status = mzscheme_to_vim(argv[i], v);
3421 if (status == OK)
3422 {
3423 status = list_append_tv(list, v);
3424 clear_tv(v);
3425 }
3426 vim_free(v);
3427 }
3428 }
3429 if (status == OK)
3430 {
3431 typval_T ret;
3432 ret.v_type = VAR_UNKNOWN;
3433
3434 mzscheme_call_vim(BYTE_STRING_VALUE((Scheme_Object *)name), &args, &ret);
3435 MZ_GC_CHECK();
3436 result = vim_to_mzscheme(&ret);
3437 clear_tv(&ret);
3438 MZ_GC_CHECK();
3439 }
3440 }
3441 clear_tv(&args);
3442 MZ_GC_UNREG();
3443 if (status != OK)
3444 raise_vim_exn(_("error converting Scheme values to Vim"));
3445 else
3446 raise_if_error();
3447 return result;
3448}
3449
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003450 void
3451do_mzeval(char_u *str, typval_T *rettv)
3452{
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003453 Scheme_Object *ret = NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003454
Bram Moolenaar75676462013-01-30 14:55:42 +01003455 MZ_GC_DECL_REG(1);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003456 MZ_GC_VAR_IN_REG(0, ret);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003457 MZ_GC_REG();
3458
3459 if (mzscheme_init())
3460 {
3461 MZ_GC_UNREG();
3462 return;
3463 }
3464
3465 MZ_GC_CHECK();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003466 if (eval_with_exn_handling(str, do_eval, &ret) == OK)
Bram Moolenaar75676462013-01-30 14:55:42 +01003467 mzscheme_to_vim(ret, rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003468
3469 MZ_GC_UNREG();
3470}
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003471#endif
3472
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003473/*
3474 * Check to see whether a Vim error has been reported, or a keyboard
3475 * interrupt (from vim --> got_int) has been detected.
3476 */
3477 static int
3478vim_error_check(void)
3479{
3480 return (got_int || did_emsg);
3481}
3482
3483/*
3484 * register Scheme exn:vim
3485 */
3486 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003487register_vim_exn(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003488{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003489 int nc = 0;
3490 int i;
3491 Scheme_Object *struct_exn = NULL;
3492 Scheme_Object *exn_name = NULL;
3493
3494 MZ_GC_DECL_REG(2);
3495 MZ_GC_VAR_IN_REG(0, struct_exn);
3496 MZ_GC_VAR_IN_REG(1, exn_name);
3497 MZ_GC_REG();
3498
3499 exn_name = scheme_intern_symbol("exn:vim");
3500 MZ_GC_CHECK();
3501 struct_exn = scheme_builtin_value("struct:exn");
3502 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003503
3504 if (vim_exn == NULL)
3505 vim_exn = scheme_make_struct_type(exn_name,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003506 struct_exn, NULL, 0, 0, NULL, NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003507#if MZSCHEME_VERSION_MAJOR >= 299
3508 , NULL
3509#endif
3510 );
3511
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003512
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003513 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003514 Scheme_Object **tmp = NULL;
3515 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
3516 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
3517 MZ_GC_DECL_REG(6);
3518 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
3519 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
3520 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003521
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003522 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003523 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
3524 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003525
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003526 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
3527 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
3528 MZ_GC_CHECK();
3529
3530 for (i = 0; i < nc; i++)
3531 {
3532 scheme_add_global_symbol(exn_names[i],
3533 exn_values[i], environment);
3534 MZ_GC_CHECK();
3535 }
3536 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003537 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003538 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003539}
3540
3541/*
3542 * raise exn:vim, may be with additional info string
3543 */
3544 void
3545raise_vim_exn(const char *add_info)
3546{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003547 char *fmt = _("Vim error: ~a");
3548 Scheme_Object *argv[2] = {NULL, NULL};
3549 Scheme_Object *exn = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01003550 Scheme_Object *byte_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003551
Bram Moolenaar75676462013-01-30 14:55:42 +01003552 MZ_GC_DECL_REG(5);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003553 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
3554 MZ_GC_VAR_IN_REG(3, exn);
Bram Moolenaar75676462013-01-30 14:55:42 +01003555 MZ_GC_VAR_IN_REG(4, byte_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003556 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003557
3558 if (add_info != NULL)
3559 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003560 char *c_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003561 Scheme_Object *info = NULL;
3562
3563 MZ_GC_DECL_REG(3);
3564 MZ_GC_VAR_IN_REG(0, c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003565 MZ_GC_VAR_IN_REG(2, info);
3566 MZ_GC_REG();
3567
Bram Moolenaar75676462013-01-30 14:55:42 +01003568 info = scheme_make_byte_string(add_info);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003569 MZ_GC_CHECK();
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02003570 c_string = scheme_format_utf8(fmt, (int)STRLEN(fmt), 1, &info, NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003571 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01003572 byte_string = scheme_make_byte_string(c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003573 MZ_GC_CHECK();
3574 argv[0] = scheme_byte_string_to_char_string(byte_string);
Bram Moolenaar555b2802005-05-19 21:08:39 +00003575 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003576 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003577 }
3578 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003579 {
3580 byte_string = scheme_make_byte_string(_("Vim error"));
3581 MZ_GC_CHECK();
3582 argv[0] = scheme_byte_string_to_char_string(byte_string);
3583 MZ_GC_CHECK();
3584 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003585 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003586
Bram Moolenaar049377e2007-05-12 15:32:12 +00003587#if MZSCHEME_VERSION_MAJOR < 360
3588 argv[1] = scheme_current_continuation_marks();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003589 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003590#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003591 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003592 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003593#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003594
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003595 exn = scheme_make_struct_instance(vim_exn, 2, argv);
3596 MZ_GC_CHECK();
3597 scheme_raise(exn);
3598 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003599}
3600
3601 void
3602raise_if_error(void)
3603{
3604 if (vim_error_check())
3605 raise_vim_exn(NULL);
3606}
3607
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003608/*
3609 * get buffer:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003610 * either current
3611 * or passed as argv[argnum] with checks
3612 */
3613 static vim_mz_buffer *
3614get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3615{
3616 vim_mz_buffer *b;
3617
3618 if (argc < argnum + 1)
3619 return get_vim_curr_buffer();
3620 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3621 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3622 b = (vim_mz_buffer *)argv[argnum];
3623 (void)get_valid_buffer(argv[argnum]);
3624 return b;
3625}
3626
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003627/*
3628 * get window:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003629 * either current
3630 * or passed as argv[argnum] with checks
3631 */
3632 static vim_mz_window *
3633get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3634{
3635 vim_mz_window *w;
3636
3637 if (argc < argnum + 1)
3638 return get_vim_curr_window();
3639 w = (vim_mz_window *)argv[argnum];
3640 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3641 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3642 (void)get_valid_window(argv[argnum]);
3643 return w;
3644}
3645
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003646/*
3647 * get valid Vim buffer from Scheme_Object*
3648 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003649buf_T *get_valid_buffer(void *obj)
3650{
3651 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3652
3653 if (buf == INVALID_BUFFER_VALUE)
3654 scheme_signal_error(_("buffer is invalid"));
3655 return buf;
3656}
3657
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003658/*
3659 * get valid Vim window from Scheme_Object*
3660 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003661win_T *get_valid_window(void *obj)
3662{
3663 win_T *win = ((vim_mz_window *)obj)->win;
3664 if (win == INVALID_WINDOW_VALUE)
3665 scheme_signal_error(_("window is invalid"));
3666 return win;
3667}
3668
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003669 int
3670mzthreads_allowed(void)
3671{
3672 return mz_threads_allow;
3673}
3674
3675 static int
3676line_in_range(linenr_T lnum, buf_T *buf)
3677{
3678 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3679}
3680
3681 static void
3682check_line_range(linenr_T lnum, buf_T *buf)
3683{
3684 if (!line_in_range(lnum, buf))
3685 scheme_signal_error(_("linenr out of range"));
3686}
3687
3688/*
3689 * Check if deleting lines made the cursor position invalid
3690 * (or you'll get msg from Vim about invalid linenr).
3691 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3692 * deleted). Got from if_python.c
3693 */
3694 static void
3695mz_fix_cursor(int lo, int hi, int extra)
3696{
3697 if (curwin->w_cursor.lnum >= lo)
3698 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003699 // Adjust the cursor position if it's in/after the changed
3700 // lines.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003701 if (curwin->w_cursor.lnum >= hi)
3702 {
3703 curwin->w_cursor.lnum += extra;
3704 check_cursor_col();
3705 }
3706 else if (extra < 0)
3707 {
3708 curwin->w_cursor.lnum = lo;
3709 check_cursor();
3710 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003711 else
3712 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003713 changed_cline_bef_curs();
3714 }
3715 invalidate_botline();
3716}
3717
3718static Vim_Prim prims[]=
3719{
3720 /*
3721 * Buffer-related commands
3722 */
3723 {get_buffer_line, "get-buff-line", 1, 2},
3724 {set_buffer_line, "set-buff-line", 2, 3},
3725 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3726 {get_buffer_name, "get-buff-name", 0, 1},
3727 {get_buffer_num, "get-buff-num", 0, 1},
3728 {get_buffer_size, "get-buff-size", 0, 1},
3729 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3730 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3731 {get_curr_buffer, "curr-buff", 0, 0},
3732 {get_buffer_count, "buff-count", 0, 0},
3733 {get_next_buffer, "get-next-buff", 0, 1},
3734 {get_prev_buffer, "get-prev-buff", 0, 1},
3735 {mzscheme_open_buffer, "open-buff", 1, 1},
3736 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3737 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003738 /*
3739 * Window-related commands
3740 */
3741 {get_curr_win, "curr-win", 0, 0},
3742 {get_window_count, "win-count", 0, 0},
3743 {get_window_by_num, "get-win-by-num", 1, 1},
3744 {get_window_num, "get-win-num", 0, 1},
3745 {get_window_buffer, "get-win-buffer", 0, 1},
3746 {get_window_height, "get-win-height", 0, 1},
3747 {set_window_height, "set-win-height", 1, 2},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003748 {get_window_width, "get-win-width", 0, 1},
3749 {set_window_width, "set-win-width", 1, 2},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003750 {get_cursor, "get-cursor", 0, 1},
3751 {set_cursor, "set-cursor", 1, 2},
3752 {get_window_list, "get-win-list", 0, 1},
3753 /*
3754 * Vim-related commands
3755 */
3756 {vim_command, "command", 1, 1},
3757 {vim_eval, "eval", 1, 1},
3758 {get_range_start, "range-start", 0, 0},
3759 {get_range_end, "range-end", 0, 0},
3760 {mzscheme_beep, "beep", 0, 0},
3761 {get_option, "get-option", 1, 2},
3762 {set_option, "set-option", 1, 2},
3763 /*
3764 * small utilities
3765 */
3766 {vim_bufferp, "buff?", 1, 1},
3767 {vim_windowp, "win?", 1, 1},
3768 {vim_buffer_validp, "buff-valid?", 1, 1},
3769 {vim_window_validp, "win-valid?", 1, 1}
3770};
3771
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003772/*
3773 * return MzScheme wrapper for curbuf
3774 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003775 static vim_mz_buffer *
3776get_vim_curr_buffer(void)
3777{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003778 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003779 return (vim_mz_buffer *)buffer_new(curbuf);
3780 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003781 return BUFFER_REF(curbuf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003782}
3783
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003784/*
3785 * return MzScheme wrapper for curwin
3786 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003787 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 Moolenaar2ab2e862019-12-04 21:24:53 +01003814 // 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 Moolenaar2ab2e862019-12-04 21:24:53 +01003841/*
3842 * security guards to force Vim's sandbox restrictions on MzScheme level
3843 */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003844 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003845sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003846{
3847 if (sandbox)
3848 {
3849 Scheme_Object *requested_access = argv[2];
3850
3851 if (M_write == NULL)
3852 {
3853 MZ_REGISTER_STATIC(M_write);
3854 M_write = scheme_intern_symbol("write");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003855 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003856 }
3857 if (M_read == NULL)
3858 {
3859 MZ_REGISTER_STATIC(M_read);
3860 M_read = scheme_intern_symbol("read");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003861 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003862 }
3863 if (M_execute == NULL)
3864 {
3865 MZ_REGISTER_STATIC(M_execute);
3866 M_execute = scheme_intern_symbol("execute");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003867 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003868 }
3869 if (M_delete == NULL)
3870 {
3871 MZ_REGISTER_STATIC(M_delete);
3872 M_delete = scheme_intern_symbol("delete");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003873 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003874 }
3875
3876 while (!SCHEME_NULLP(requested_access))
3877 {
3878 Scheme_Object *item = SCHEME_CAR(requested_access);
3879 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3880 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
Bram Moolenaar555b2802005-05-19 21:08:39 +00003881 raise_vim_exn(_("not allowed in the Vim sandbox"));
Bram Moolenaar555b2802005-05-19 21:08:39 +00003882 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