blob: 7b5293670976e79c5027d91c9026f40d394d2081 [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;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000815#endif
816
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100817#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) // Win32 console and Unix
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000818 void
819mzvim_check_threads(void)
820{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100821 // Last time MzScheme threads were scheduled
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000822 static time_t mz_last_time = 0;
823
824 if (mz_threads_allow && p_mzq > 0)
825 {
826 time_t now = time(NULL);
827
828 if ((now - mz_last_time) * 1000 > p_mzq)
829 {
830 mz_last_time = now;
831 scheme_check_threads();
832 }
833 }
834}
835#endif
836
Bram Moolenaar4349c572016-01-30 13:28:28 +0100837#if defined(MZSCHEME_GUI_THREADS) || defined(PROTO)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000838static void setup_timer(void);
839static void remove_timer(void);
840
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100841// timers are presented in GUI only
Bram Moolenaar4f974752019-02-17 17:44:42 +0100842# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000843 static void CALLBACK
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200844timer_proc(HWND hwnd UNUSED, UINT uMsg UNUSED, UINT_PTR idEvent UNUSED, DWORD dwTime UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000845# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100846 static gboolean
Bram Moolenaar64404472010-06-26 06:24:45 +0200847timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000848# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000849 static void
Bram Moolenaar64404472010-06-26 06:24:45 +0200850timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000851# endif
852{
853 scheme_check_threads();
854# if defined(FEAT_GUI_GTK)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100855 return TRUE; // continue receiving notifications
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000856# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100857 // renew timeout
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000858 if (mz_threads_allow && p_mzq > 0)
859 timer_id = XtAppAddTimeOut(app_context, p_mzq,
860 timer_proc, NULL);
861# endif
862}
863
864 static void
865setup_timer(void)
866{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100867# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000868 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
869# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100870 timer_id = g_timeout_add((guint)p_mzq, (GSourceFunc)timer_proc, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000871# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
872 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000873# endif
874}
875
876 static void
877remove_timer(void)
878{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100879# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000880 KillTimer(NULL, timer_id);
881# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100882 g_source_remove(timer_id);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000883# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
884 XtRemoveTimeOut(timer_id);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000885# endif
886 timer_id = 0;
887}
888
889 void
890mzvim_reset_timer(void)
891{
892 if (timer_id != 0)
893 remove_timer();
894 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
895 setup_timer();
896}
897
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100898#endif // MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000899
900 static void
901notify_multithread(int on)
902{
903 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000904#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000905 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
906 setup_timer();
907 if (!on && timer_id != 0)
908 remove_timer();
909#endif
910}
911
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000912 void
913mzscheme_end(void)
914{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100915 // We can not unload the DLL. Racket's thread might be still alive.
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100916#if 0
Bram Moolenaar33570922005-01-25 22:26:29 +0000917#ifdef DYNAMIC_MZSCHEME
918 dynamic_mzscheme_end();
919#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100920#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000921}
922
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100923#if HAVE_TLS_SPACE
924# if defined(_MSC_VER)
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100925static __declspec(thread) void *tls_space;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100926extern intptr_t _tls_index;
927# elif defined(__MINGW32__)
928static __thread void *tls_space;
929extern intptr_t _tls_index;
930# else
931static THREAD_LOCAL void *tls_space;
932static intptr_t _tls_index = 0;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100933# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100934#endif
935
Bram Moolenaar54b63522016-08-26 12:55:09 +0200936/*
937 * mzscheme_main() is called early in main().
938 * We may call scheme_main_setup() which calls mzscheme_env_main() which then
939 * trampolines into vim_main2(), which never returns.
940 */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100941 int
Bram Moolenaar54b63522016-08-26 12:55:09 +0200942mzscheme_main(void)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000943{
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200944 int argc = 0;
945 char *argv = NULL;
946
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100947#ifdef DYNAMIC_MZSCHEME
948 /*
949 * Racket requires trampolined startup. We can not load it later.
950 * If dynamic dll loading is failed, disable it.
951 */
952 if (!mzscheme_enabled(FALSE))
953 {
954 disabled = TRUE;
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200955 return vim_main2();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100956 }
957#endif
Bram Moolenaar5c5c9802015-07-10 16:12:48 +0200958#ifdef HAVE_TLS_SPACE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100959 scheme_register_tls_space(&tls_space, _tls_index);
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100960#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100961#ifdef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200962 return scheme_main_setup(TRUE, mzscheme_env_main, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000963#else
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200964 return mzscheme_env_main(NULL, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000965#endif
966}
967
968 static int
Bram Moolenaar54b63522016-08-26 12:55:09 +0200969mzscheme_env_main(Scheme_Env *env, int argc UNUSED, char **argv UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000970{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100971#ifdef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100972 // Scheme has created the environment for us
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100973 environment = env;
974#else
975# ifdef MZ_PRECISE_GC
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000976 Scheme_Object *dummy = NULL;
977 MZ_GC_DECL_REG(1);
978 MZ_GC_VAR_IN_REG(0, dummy);
979
980 stack_base = &__gc_var_stack__;
981# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000982 int dummy = 0;
983 stack_base = (void *)&dummy;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100984# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000985#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100986
Bram Moolenaar54b63522016-08-26 12:55:09 +0200987 vim_main2();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100988 // not reached, vim_main2() will loop until exit()
Bram Moolenaar54b63522016-08-26 12:55:09 +0200989
990 return 0;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000991}
992
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100993 static Scheme_Object*
994load_base_module(void *data)
995{
996 scheme_namespace_require(scheme_intern_symbol((char *)data));
997 return scheme_null;
998}
999
1000 static Scheme_Object *
Bram Moolenaar8b29aba2016-03-28 22:17:16 +02001001load_base_module_on_error(void *data UNUSED)
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001002{
1003 load_base_module_failed = TRUE;
1004 return scheme_null;
1005}
1006
1007 static int
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001008startup_mzscheme(void)
1009{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001010#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001011 scheme_set_stack_base(stack_base, 1);
1012#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001013
Bram Moolenaar75676462013-01-30 14:55:42 +01001014#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001015 // in newer versions of precise GC the initial env has been created
Bram Moolenaar75676462013-01-30 14:55:42 +01001016 environment = scheme_basic_env();
1017#endif
1018
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001019 MZ_REGISTER_STATIC(environment);
1020 MZ_REGISTER_STATIC(curout);
1021 MZ_REGISTER_STATIC(curerr);
1022 MZ_REGISTER_STATIC(exn_catching_apply);
1023 MZ_REGISTER_STATIC(exn_p);
1024 MZ_REGISTER_STATIC(exn_message);
1025 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001026
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001027 MZ_GC_CHECK();
1028
1029#ifdef INCLUDE_MZSCHEME_BASE
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001030 // invoke function from generated and included mzscheme_base.c
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001031 declare_modules(environment);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001032#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001033
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001034 // setup 'current-library-collection-paths' parameter
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001035# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001036 {
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001037 Scheme_Object *coll_path = NULL;
1038 int mustfree = FALSE;
1039 char_u *s;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001040
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001041 MZ_GC_DECL_REG(1);
1042 MZ_GC_VAR_IN_REG(0, coll_path);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001043 MZ_GC_REG();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001044 // workaround for dynamic loading on windows
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001045 s = vim_getenv((char_u *)"PLTCOLLECTS", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001046 if (s != NULL)
1047 {
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001048 coll_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001049 MZ_GC_CHECK();
1050 if (mustfree)
1051 vim_free(s);
1052 }
1053# ifdef MZSCHEME_COLLECTS
1054 if (coll_path == NULL)
1055 {
1056 coll_path = scheme_make_path(MZSCHEME_COLLECTS);
1057 MZ_GC_CHECK();
1058 }
Bram Moolenaar39d7d512013-01-31 21:09:15 +01001059# endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001060 if (coll_path != NULL)
1061 {
1062 scheme_set_collects_path(coll_path);
1063 MZ_GC_CHECK();
1064 }
1065 MZ_GC_UNREG();
1066 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001067# else
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001068# ifdef MZSCHEME_COLLECTS
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001069 {
1070 Scheme_Object *coll_string = NULL;
1071 Scheme_Object *coll_pair = NULL;
1072 Scheme_Config *config = NULL;
1073
1074 MZ_GC_DECL_REG(3);
1075 MZ_GC_VAR_IN_REG(0, coll_string);
1076 MZ_GC_VAR_IN_REG(1, coll_pair);
1077 MZ_GC_VAR_IN_REG(2, config);
1078 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001079 coll_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001080 MZ_GC_CHECK();
1081 coll_pair = scheme_make_pair(coll_string, scheme_null);
1082 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001083 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001084 MZ_GC_CHECK();
1085 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
1086 MZ_GC_CHECK();
1087 MZ_GC_UNREG();
1088 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001089# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001090#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001091
1092# if MZSCHEME_VERSION_MAJOR >= 600
1093 {
1094 Scheme_Object *config_path = NULL;
1095 int mustfree = FALSE;
1096 char_u *s;
1097
1098 MZ_GC_DECL_REG(1);
1099 MZ_GC_VAR_IN_REG(0, config_path);
1100 MZ_GC_REG();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001101 // workaround for dynamic loading on windows
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001102 s = vim_getenv((char_u *)"PLTCONFIGDIR", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001103 if (s != NULL)
1104 {
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001105 config_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001106 MZ_GC_CHECK();
1107 if (mustfree)
1108 vim_free(s);
1109 }
1110#ifdef MZSCHEME_CONFIGDIR
1111 if (config_path == NULL)
1112 {
1113 config_path = scheme_make_path(MZSCHEME_CONFIGDIR);
1114 MZ_GC_CHECK();
1115 }
1116#endif
1117 if (config_path != NULL)
1118 {
1119 scheme_set_config_path(config_path);
1120 MZ_GC_CHECK();
1121 }
1122 MZ_GC_UNREG();
1123 }
1124# endif
1125
1126#if MZSCHEME_VERSION_MAJOR >= 400
1127 scheme_init_collection_paths(environment, scheme_null);
1128#endif
1129
1130 /*
1131 * versions 4.x do not provide Scheme bindings by default
1132 * we need to add them explicitly
1133 */
1134 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001135 // use error handler to avoid abort
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001136 scheme_dynamic_wind(NULL, load_base_module, NULL,
1137 load_base_module_on_error, "racket/base");
1138 if (load_base_module_failed)
1139 {
1140 load_base_module_failed = FALSE;
1141 scheme_dynamic_wind(NULL, load_base_module, NULL,
1142 load_base_module_on_error, "scheme/base");
1143 if (load_base_module_failed)
1144 return -1;
1145 }
1146 }
1147
1148 register_vim_exn();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001149 // use new environment to initialise exception handling
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001150 init_exn_catching_apply();
1151
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001152 // redirect output
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001153 scheme_console_output = do_output;
1154 scheme_console_printf = do_printf;
1155
Bram Moolenaar555b2802005-05-19 21:08:39 +00001156#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +00001157 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001158 Scheme_Object *make_security_guard = NULL;
1159 MZ_GC_DECL_REG(1);
1160 MZ_GC_VAR_IN_REG(0, make_security_guard);
1161 MZ_GC_REG();
1162
1163#if MZSCHEME_VERSION_MAJOR < 400
1164 {
1165 Scheme_Object *make_security_guard_symbol = NULL;
1166 MZ_GC_DECL_REG(1);
1167 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
1168 MZ_GC_REG();
1169 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
1170 MZ_GC_CHECK();
1171 make_security_guard = scheme_lookup_global(
1172 make_security_guard_symbol, environment);
1173 MZ_GC_UNREG();
1174 }
1175#else
1176 make_security_guard = scheme_builtin_value("make-security-guard");
1177 MZ_GC_CHECK();
1178#endif
1179
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001180 // setup sandbox guards
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001181 if (make_security_guard != NULL)
1182 {
1183 Scheme_Object *args[3] = {NULL, NULL, NULL};
1184 Scheme_Object *guard = NULL;
1185 Scheme_Config *config = NULL;
1186 MZ_GC_DECL_REG(5);
1187 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
1188 MZ_GC_VAR_IN_REG(3, guard);
1189 MZ_GC_VAR_IN_REG(4, config);
1190 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001191 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001192 MZ_GC_CHECK();
1193 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
1194 MZ_GC_CHECK();
1195 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
1196 "sandbox-file-guard", 3, 3);
1197 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1198 "sandbox-network-guard", 4, 4);
1199 guard = scheme_apply(make_security_guard, 3, args);
1200 MZ_GC_CHECK();
1201 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1202 MZ_GC_CHECK();
1203 MZ_GC_UNREG();
1204 }
1205 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001206 }
1207#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001208 // Create buffer and window types for use in Scheme code
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001209 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001210 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001211 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001212 MZ_GC_CHECK();
1213#ifdef MZ_PRECISE_GC
1214 GC_register_traversers(mz_buffer_type,
1215 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1216 TRUE, TRUE);
1217 GC_register_traversers(mz_window_type,
1218 window_size_proc, window_mark_proc, window_fixup_proc,
1219 TRUE, TRUE);
1220#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001221
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001222 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001223
1224 /*
1225 * setup callback to receive notifications
1226 * whether thread scheduling is (or not) required
1227 */
1228 scheme_notify_multithread = notify_multithread;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001229
1230 return 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001231}
1232
1233/*
1234 * This routine is called for each new invocation of MzScheme
1235 * to make sure things are properly initialized.
1236 */
1237 static int
1238mzscheme_init(void)
1239{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001240 if (!initialized)
1241 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001242#ifdef DYNAMIC_MZSCHEME
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001243 if (disabled || !mzscheme_enabled(TRUE))
Bram Moolenaar33570922005-01-25 22:26:29 +00001244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001245 emsg(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
Bram Moolenaar33570922005-01-25 22:26:29 +00001246 return -1;
1247 }
1248#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001249 if (load_base_module_failed || startup_mzscheme())
1250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001251 emsg(_("E895: Sorry, this command is disabled, the MzScheme's racket/base module could not be loaded."));
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001252 return -1;
1253 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001254 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001255 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001256 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001257 Scheme_Config *config = NULL;
1258 MZ_GC_DECL_REG(1);
1259 MZ_GC_VAR_IN_REG(0, config);
1260 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001261 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001262 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001263 // recreate ports each call effectively clearing these ones
Bram Moolenaar75676462013-01-30 14:55:42 +01001264 curout = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001265 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001266 curerr = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001267 MZ_GC_CHECK();
1268 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1269 MZ_GC_CHECK();
1270 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1271 MZ_GC_CHECK();
1272 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001273 }
1274
1275 return 0;
1276}
1277
1278/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001279 *========================================================================
1280 * 2. External Interface
1281 *========================================================================
1282 */
1283
1284/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001285 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001286 */
1287 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001288eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001289{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001290 Scheme_Object *value = NULL;
1291 Scheme_Object *exn = NULL;
1292 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001293
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001294 MZ_GC_DECL_REG(3);
1295 MZ_GC_VAR_IN_REG(0, value);
1296 MZ_GC_VAR_IN_REG(1, exn);
1297 MZ_GC_VAR_IN_REG(2, prim);
1298 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001300 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1301 MZ_GC_CHECK();
1302 value = _apply_thunk_catch_exceptions(prim, &exn);
1303 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001304
1305 if (!value)
1306 {
1307 value = extract_exn_message(exn);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001308 // Got an exn?
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001309 if (value)
1310 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001311 scheme_display(value, curerr); // Send to stderr-vim
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001312 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001313 do_flush();
1314 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001315 MZ_GC_UNREG();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001316 // `raise' was called on some arbitrary value
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001317 return FAIL;
1318 }
1319
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001320 if (ret != NULL) // if pointer to retval supported give it up
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001321 *ret = value;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001322 // Print any result, as long as it's not a void
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001323 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001324 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001325 scheme_display(value, curout); // Send to stdout-vim
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001326 MZ_GC_CHECK();
1327 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001328
1329 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001330 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001331 return OK;
1332}
1333
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001334/*
1335 * :mzscheme
1336 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001337 static int
1338do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1339{
1340 if (mzscheme_init())
1341 return FAIL;
1342
1343 range_start = eap->line1;
1344 range_end = eap->line2;
1345
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001346 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001347}
1348
1349/*
1350 * Routine called by VIM when deleting a buffer
1351 */
1352 void
1353mzscheme_buffer_free(buf_T *buf)
1354{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001355 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001356 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001357 vim_mz_buffer *bp = NULL;
1358 MZ_GC_DECL_REG(1);
1359 MZ_GC_VAR_IN_REG(0, bp);
1360 MZ_GC_REG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001361
Bram Moolenaar75676462013-01-30 14:55:42 +01001362 bp = BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001363 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001364#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001365 scheme_gc_ptr_ok(bp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001366#else
1367 scheme_free_immobile_box(buf->b_mzscheme_ref);
1368#endif
1369 buf->b_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001370 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001371 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001372 }
1373}
1374
1375/*
1376 * Routine called by VIM when deleting a Window
1377 */
1378 void
1379mzscheme_window_free(win_T *win)
1380{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001381 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001382 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001383 vim_mz_window *wp = NULL;
1384 MZ_GC_DECL_REG(1);
1385 MZ_GC_VAR_IN_REG(0, wp);
1386 MZ_GC_REG();
1387 wp = WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001388 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001389#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001390 scheme_gc_ptr_ok(wp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001391#else
1392 scheme_free_immobile_box(win->w_mzscheme_ref);
1393#endif
1394 win->w_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001395 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001396 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001397 }
1398}
1399
1400/*
1401 * ":mzscheme" (or ":mz")
1402 */
1403 void
1404ex_mzscheme(exarg_T *eap)
1405{
1406 char_u *script;
1407
1408 script = script_get(eap, eap->arg);
1409 if (!eap->skip)
1410 {
1411 if (script == NULL)
1412 do_mzscheme_command(eap, eap->arg, do_eval);
1413 else
1414 {
1415 do_mzscheme_command(eap, script, do_eval);
1416 vim_free(script);
1417 }
1418 }
1419}
1420
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001421 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001422do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001423{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001424 Scheme_Object *expr = NULL;
1425 Scheme_Object *result = NULL;
1426 char *file = NULL;
1427 Port_Info *pinfo = (Port_Info *)data;
1428
1429 MZ_GC_DECL_REG(3);
1430 MZ_GC_VAR_IN_REG(0, expr);
1431 MZ_GC_VAR_IN_REG(1, result);
1432 MZ_GC_VAR_IN_REG(2, file);
1433 MZ_GC_REG();
1434
1435 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1436 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001437
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001438 // make Vim expansion
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001439 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001440 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001441 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001442 scheme_count_lines(pinfo->port); // to get accurate read error location
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001443 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001444
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001445 // Like REPL but print only last result
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001446 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001447 {
1448 result = scheme_eval(expr, environment);
1449 MZ_GC_CHECK();
1450 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001451
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001452 // errors will be caught in do_mzscheme_command and ex_mzfile
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001453 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001454 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001455 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001456 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001457 return result;
1458}
1459
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001460/*
1461 * :mzfile
1462 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001463 void
1464ex_mzfile(exarg_T *eap)
1465{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001466 Port_Info pinfo = {NULL, NULL};
1467
1468 MZ_GC_DECL_REG(1);
1469 MZ_GC_VAR_IN_REG(0, pinfo.port);
1470 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001471
1472 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001473 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001474 && pinfo.port != NULL) // looks like port was not closed
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001475 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001476 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001477 MZ_GC_CHECK();
1478 }
1479 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001480}
1481
1482
1483/*
1484 *========================================================================
1485 * Exception handling code -- cribbed form the MzScheme sources and
1486 * Matthew Flatt's "Inside PLT MzScheme" document.
1487 *========================================================================
1488 */
1489 static void
1490init_exn_catching_apply(void)
1491{
1492 if (!exn_catching_apply)
1493 {
1494 char *e =
1495 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001496 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001497 "(cons #t (thunk))))";
1498
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001499 exn_catching_apply = scheme_eval_string(e, environment);
1500 MZ_GC_CHECK();
1501 exn_p = scheme_builtin_value("exn?");
1502 MZ_GC_CHECK();
1503 exn_message = scheme_builtin_value("exn-message");
1504 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001505 }
1506}
1507
1508/*
1509 * This function applies a thunk, returning the Scheme value if there's
1510 * no exception, otherwise returning NULL and setting *exn to the raised
1511 * value (usually an exn structure).
1512 */
1513 static Scheme_Object *
1514_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1515{
1516 Scheme_Object *v;
1517
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001518 v = _scheme_apply(exn_catching_apply, 1, &f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001519 // v is a pair: (cons #t value) or (cons #f exn)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001520
1521 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1522 return SCHEME_CDR(v);
1523 else
1524 {
1525 *exn = SCHEME_CDR(v);
1526 return NULL;
1527 }
1528}
1529
1530 static Scheme_Object *
1531extract_exn_message(Scheme_Object *v)
1532{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001533 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1534 return _scheme_apply(exn_message, 1, &v);
1535 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001536 return NULL; // Not an exn structure
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001537}
1538
1539 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001540do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001541{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001542 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001543}
1544
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001545/*
1546 *========================================================================
1547 * 3. MzScheme I/O Handlers
1548 *========================================================================
1549 */
1550 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001551do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001552{
1553 char *p, *prev;
1554
1555 prev = mesg;
1556 p = strchr(prev, '\n');
1557 while (p)
1558 {
1559 *p = '\0';
1560 if (error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001561 emsg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001562 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001563 msg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001564 prev = p + 1;
1565 p = strchr(prev, '\n');
1566 }
1567
1568 if (error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001569 emsg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001570 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001571 msg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001572}
1573
1574 static void
Bram Moolenaar75676462013-01-30 14:55:42 +01001575do_output(char *mesg, OUTPUT_LEN_TYPE len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001576{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001577 // TODO: use len, the string may not be NUL terminated
Bram Moolenaar64404472010-06-26 06:24:45 +02001578 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001579}
1580
1581 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001582do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001583{
Bram Moolenaar64404472010-06-26 06:24:45 +02001584 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001585}
1586
1587 static void
1588do_printf(char *format, ...)
1589{
Bram Moolenaar64404472010-06-26 06:24:45 +02001590 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001591}
1592
1593 static void
1594do_flush(void)
1595{
1596 char *buff;
Bram Moolenaar75676462013-01-30 14:55:42 +01001597 OUTPUT_LEN_TYPE length;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001598
Bram Moolenaar75676462013-01-30 14:55:42 +01001599 buff = scheme_get_sized_byte_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001600 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001601 if (length)
1602 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001603 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001604 return;
1605 }
1606
Bram Moolenaar75676462013-01-30 14:55:42 +01001607 buff = scheme_get_sized_byte_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001608 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001609 if (length)
1610 do_output(buff, length);
1611}
1612
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001613/*
1614 *========================================================================
1615 * 4. Implementation of the Vim Features for MzScheme
1616 *========================================================================
1617 */
1618
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001619/*
1620 * (command {command-string})
1621 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001622 static Scheme_Object *
1623vim_command(void *data, int argc, Scheme_Object **argv)
1624{
1625 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001626 Scheme_Object *cmd = NULL;
1627 MZ_GC_DECL_REG(1);
1628 MZ_GC_VAR_IN_REG(0, cmd);
1629 MZ_GC_REG();
1630 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001631
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001632 // may be use do_cmdline_cmd?
Bram Moolenaar75676462013-01-30 14:55:42 +01001633 do_cmdline(BYTE_STRING_VALUE(cmd), NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001634 update_screen(VALID);
1635
Bram Moolenaar75676462013-01-30 14:55:42 +01001636 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001637 raise_if_error();
1638 return scheme_void;
1639}
1640
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001641/*
1642 * (eval {expr-string})
1643 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001644 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001645vim_eval(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001646{
1647#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001648 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001649 Scheme_Object *result = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001650 typval_T *vim_result;
Bram Moolenaar75676462013-01-30 14:55:42 +01001651 Scheme_Object *expr = NULL;
1652 MZ_GC_DECL_REG(2);
1653 MZ_GC_VAR_IN_REG(0, result);
1654 MZ_GC_VAR_IN_REG(1, expr);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001655 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001656 expr = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001657
Bram Moolenaar75676462013-01-30 14:55:42 +01001658 vim_result = eval_expr(BYTE_STRING_VALUE(expr), NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001659
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001660 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001661 raise_vim_exn(_("invalid expression"));
1662
Bram Moolenaar75676462013-01-30 14:55:42 +01001663 result = vim_to_mzscheme(vim_result);
1664 MZ_GC_CHECK();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001665 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001666
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001667 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001668 return result;
1669#else
1670 raise_vim_exn(_("expressions disabled at compile time"));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001671 // unreachable
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001672 return scheme_false;
1673#endif
1674}
1675
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001676/*
1677 * (range-start)
1678 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001679 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001680get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001681{
1682 return scheme_make_integer(range_start);
1683}
1684
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001685/*
1686 * (range-end)
1687 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001688 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001689get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001690{
1691 return scheme_make_integer(range_end);
1692}
1693
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001694/*
1695 * (beep)
1696 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001697 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001698mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001699{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001700 vim_beep(BO_LANG);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001701 return scheme_void;
1702}
1703
1704static Scheme_Object *M_global = NULL;
1705
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001706/*
1707 * (get-option {option-name}) [buffer/window]
1708 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001709 static Scheme_Object *
1710get_option(void *data, int argc, Scheme_Object **argv)
1711{
1712 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001713 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001714 char *strval;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001715 int rc;
Bram Moolenaar75676462013-01-30 14:55:42 +01001716 Scheme_Object *rval = NULL;
1717 Scheme_Object *name = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001718 int opt_flags = 0;
1719 buf_T *save_curb = curbuf;
1720 win_T *save_curw = curwin;
1721
Bram Moolenaar75676462013-01-30 14:55:42 +01001722 MZ_GC_DECL_REG(2);
1723 MZ_GC_VAR_IN_REG(0, rval);
1724 MZ_GC_VAR_IN_REG(1, name);
1725 MZ_GC_REG();
1726
1727 name = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001728
1729 if (argc > 1)
1730 {
1731 if (M_global == NULL)
1732 {
1733 MZ_REGISTER_STATIC(M_global);
1734 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001735 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001736 }
1737
1738 if (argv[1] == M_global)
1739 opt_flags = OPT_GLOBAL;
1740 else if (SCHEME_VIMBUFFERP(argv[1]))
1741 {
1742 curbuf = get_valid_buffer(argv[1]);
1743 opt_flags = OPT_LOCAL;
1744 }
1745 else if (SCHEME_VIMWINDOWP(argv[1]))
1746 {
1747 win_T *win = get_valid_window(argv[1]);
1748
1749 curwin = win;
1750 curbuf = win->w_buffer;
1751 opt_flags = OPT_LOCAL;
1752 }
1753 else
1754 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1755 }
1756
Bram Moolenaar75676462013-01-30 14:55:42 +01001757 rc = get_option_value(BYTE_STRING_VALUE(name), &value, (char_u **)&strval, opt_flags);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001758 curbuf = save_curb;
1759 curwin = save_curw;
1760
1761 switch (rc)
1762 {
1763 case 1:
Bram Moolenaar75676462013-01-30 14:55:42 +01001764 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001765 return scheme_make_integer_value(value);
1766 case 0:
Bram Moolenaar75676462013-01-30 14:55:42 +01001767 rval = scheme_make_byte_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001768 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001769 vim_free(strval);
Bram Moolenaar75676462013-01-30 14:55:42 +01001770 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001771 return rval;
1772 case -1:
1773 case -2:
Bram Moolenaar75676462013-01-30 14:55:42 +01001774 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001775 raise_vim_exn(_("hidden option"));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001776 //NOTREACHED
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001777 case -3:
Bram Moolenaar75676462013-01-30 14:55:42 +01001778 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001779 raise_vim_exn(_("unknown option"));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001780 //NOTREACHED
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001781 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001782 // unreachable
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001783 return scheme_void;
1784}
1785
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001786/*
1787 * (set-option {option-changing-string} [buffer/window])
1788 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001789 static Scheme_Object *
1790set_option(void *data, int argc, Scheme_Object **argv)
1791{
Bram Moolenaar75676462013-01-30 14:55:42 +01001792 char_u *command = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001793 int opt_flags = 0;
1794 buf_T *save_curb = curbuf;
1795 win_T *save_curw = curwin;
1796 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001797 Scheme_Object *cmd = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001798
Bram Moolenaar75676462013-01-30 14:55:42 +01001799 MZ_GC_DECL_REG(1);
1800 MZ_GC_VAR_IN_REG(0, cmd);
1801 MZ_GC_REG();
1802 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
1803
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001804 if (argc > 1)
1805 {
1806 if (M_global == NULL)
1807 {
1808 MZ_REGISTER_STATIC(M_global);
1809 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001810 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001811 }
1812
1813 if (argv[1] == M_global)
1814 opt_flags = OPT_GLOBAL;
1815 else if (SCHEME_VIMBUFFERP(argv[1]))
1816 {
1817 curbuf = get_valid_buffer(argv[1]);
1818 opt_flags = OPT_LOCAL;
1819 }
1820 else if (SCHEME_VIMWINDOWP(argv[1]))
1821 {
1822 win_T *win = get_valid_window(argv[1]);
1823 curwin = win;
1824 curbuf = win->w_buffer;
1825 opt_flags = OPT_LOCAL;
1826 }
1827 else
1828 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1829 }
1830
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001831 // do_set can modify cmd, make copy
Bram Moolenaar75676462013-01-30 14:55:42 +01001832 command = vim_strsave(BYTE_STRING_VALUE(cmd));
1833 MZ_GC_UNREG();
1834 do_set(command, opt_flags);
1835 vim_free(command);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001836 update_screen(NOT_VALID);
1837 curbuf = save_curb;
1838 curwin = save_curw;
1839 raise_if_error();
1840 return scheme_void;
1841}
1842
1843/*
1844 *===========================================================================
1845 * 5. Vim Window-related Manipulation Functions
1846 *===========================================================================
1847 */
1848
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001849/*
1850 * (curr-win)
1851 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001852 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001853get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001854{
1855 return (Scheme_Object *)get_vim_curr_window();
1856}
1857
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001858/*
1859 * (win-count)
1860 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001861 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001862get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001863{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001864 int n = 0;
Bram Moolenaard2142212013-01-30 17:41:50 +01001865 win_T *w;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001866
Bram Moolenaar29323592016-07-24 22:04:11 +02001867 FOR_ALL_WINDOWS(w)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001868 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001869 return scheme_make_integer(n);
1870}
1871
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001872/*
1873 * (get-win-list [buffer])
1874 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001875 static Scheme_Object *
1876get_window_list(void *data, int argc, Scheme_Object **argv)
1877{
1878 Vim_Prim *prim = (Vim_Prim *)data;
1879 vim_mz_buffer *buf;
1880 Scheme_Object *list;
Bram Moolenaard2142212013-01-30 17:41:50 +01001881 win_T *w = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001882
1883 buf = get_buffer_arg(prim->name, 0, argc, argv);
1884 list = scheme_null;
1885
Bram Moolenaard2142212013-01-30 17:41:50 +01001886 for ( ; w != NULL; w = w->w_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001887 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001888 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001889 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001890 MZ_GC_CHECK();
1891 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001892
1893 return list;
1894}
1895
1896 static Scheme_Object *
1897window_new(win_T *win)
1898{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001899 vim_mz_window *self = NULL;
1900
1901 MZ_GC_DECL_REG(1);
1902 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001903
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001904 // We need to handle deletion of windows underneath us.
1905 // If we add a "w_mzscheme_ref" field to the win_T structure,
1906 // then we can get at it in win_free() in vim.
1907 //
1908 // On a win_free() we set the Scheme object's win_T *field
1909 // to an invalid value. We trap all uses of a window
1910 // object, and reject them if the win_T *field is invalid.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001911 if (win->w_mzscheme_ref != NULL)
Bram Moolenaar75676462013-01-30 14:55:42 +01001912 return (Scheme_Object *)WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001913
Bram Moolenaar75676462013-01-30 14:55:42 +01001914 MZ_GC_REG();
1915 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_window));
Bram Moolenaara80faa82020-04-12 19:37:17 +02001916 CLEAR_POINTER(self);
Bram Moolenaar75676462013-01-30 14:55:42 +01001917#ifndef MZ_PRECISE_GC
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001918 scheme_dont_gc_ptr(self); // because win isn't visible to GC
Bram Moolenaar75676462013-01-30 14:55:42 +01001919#else
1920 win->w_mzscheme_ref = scheme_malloc_immobile_box(NULL);
1921#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001922 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001923 WINDOW_REF(win) = self;
1924 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001925 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001926 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001927
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001928 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001929 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001930}
1931
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001932/*
1933 * (get-win-num [window])
1934 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001935 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001936get_window_num(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001937{
Bram Moolenaard2142212013-01-30 17:41:50 +01001938 int nr = 1;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001939 Vim_Prim *prim = (Vim_Prim *)data;
1940 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001941 win_T *wp;
1942
1943 for (wp = firstwin; wp != win; wp = wp->w_next)
1944 ++nr;
1945
1946 return scheme_make_integer(nr);
1947}
1948
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001949/*
1950 * (get-win-by-num {windownum})
1951 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001952 static Scheme_Object *
1953get_window_by_num(void *data, int argc, Scheme_Object **argv)
1954{
1955 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaard2142212013-01-30 17:41:50 +01001956 win_T *win = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001957 int fnum;
1958
1959 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1960 if (fnum < 1)
1961 scheme_signal_error(_("window index is out of range"));
1962
Bram Moolenaard2142212013-01-30 17:41:50 +01001963 for ( ; win != NULL; win = win->w_next, --fnum)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001964 if (fnum == 1) // to be 1-based
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001965 return window_new(win);
1966
1967 return scheme_false;
1968}
1969
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001970/*
1971 * (get-win-buffer [window])
1972 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001973 static Scheme_Object *
1974get_window_buffer(void *data, int argc, Scheme_Object **argv)
1975{
1976 Vim_Prim *prim = (Vim_Prim *)data;
1977 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1978
1979 return buffer_new(win->win->w_buffer);
1980}
1981
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001982/*
1983 * (get-win-height [window])
1984 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001985 static Scheme_Object *
1986get_window_height(void *data, int argc, Scheme_Object **argv)
1987{
1988 Vim_Prim *prim = (Vim_Prim *)data;
1989 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1990
1991 return scheme_make_integer(win->win->w_height);
1992}
1993
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001994/*
1995 * (set-win-height {height} [window])
1996 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001997 static Scheme_Object *
1998set_window_height(void *data, int argc, Scheme_Object **argv)
1999{
2000 Vim_Prim *prim = (Vim_Prim *)data;
2001 vim_mz_window *win;
2002 win_T *savewin;
2003 int height;
2004
2005 win = get_window_arg(prim->name, 1, argc, argv);
2006 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2007
2008#ifdef FEAT_GUI
2009 need_mouse_correct = TRUE;
2010#endif
2011
2012 savewin = curwin;
2013 curwin = win->win;
2014 win_setheight(height);
2015 curwin = savewin;
2016
2017 raise_if_error();
2018 return scheme_void;
2019}
2020
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002021/*
2022 * (get-win-width [window])
2023 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002024 static Scheme_Object *
2025get_window_width(void *data, int argc, Scheme_Object **argv)
2026{
2027 Vim_Prim *prim = (Vim_Prim *)data;
2028 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2029
Bram Moolenaar02631462017-09-22 15:20:32 +02002030 return scheme_make_integer(win->win->w_width);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002031}
2032
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002033/*
2034 * (set-win-width {width} [window])
2035 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002036 static Scheme_Object *
2037set_window_width(void *data, int argc, Scheme_Object **argv)
2038{
2039 Vim_Prim *prim = (Vim_Prim *)data;
2040 vim_mz_window *win;
2041 win_T *savewin;
2042 int width = 0;
2043
2044 win = get_window_arg(prim->name, 1, argc, argv);
2045 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2046
2047# ifdef FEAT_GUI
2048 need_mouse_correct = TRUE;
2049# endif
2050
2051 savewin = curwin;
2052 curwin = win->win;
2053 win_setwidth(width);
2054 curwin = savewin;
2055
2056 raise_if_error();
2057 return scheme_void;
2058}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002059
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002060/*
2061 * (get-cursor [window]) -> (line . col)
2062 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002063 static Scheme_Object *
2064get_cursor(void *data, int argc, Scheme_Object **argv)
2065{
2066 Vim_Prim *prim = (Vim_Prim *)data;
2067 vim_mz_window *win;
2068 pos_T pos;
2069
2070 win = get_window_arg(prim->name, 0, argc, argv);
2071 pos = win->win->w_cursor;
2072 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
2073 scheme_make_integer_value((long)pos.col + 1));
2074}
2075
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002076/*
2077 * (set-cursor (line . col) [window])
2078 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002079 static Scheme_Object *
2080set_cursor(void *data, int argc, Scheme_Object **argv)
2081{
2082 Vim_Prim *prim = (Vim_Prim *)data;
2083 vim_mz_window *win;
2084 long lnum = 0;
2085 long col = 0;
2086
Bram Moolenaar555b2802005-05-19 21:08:39 +00002087#ifdef HAVE_SANDBOX
2088 sandbox_check();
2089#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002090 win = get_window_arg(prim->name, 1, argc, argv);
2091 GUARANTEE_PAIR(prim->name, 0);
2092
2093 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
2094 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
2095 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
2096
2097 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
2098 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
2099
2100 check_line_range(lnum, win->win->w_buffer);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002101 // don't know how to catch invalid column value
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002102
2103 win->win->w_cursor.lnum = lnum;
2104 win->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02002105 win->win->w_set_curswant = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002106 update_screen(VALID);
2107
2108 raise_if_error();
2109 return scheme_void;
2110}
2111/*
2112 *===========================================================================
2113 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002114 *===========================================================================
2115 */
2116
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002117/*
2118 * (open-buff {filename})
2119 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002120 static Scheme_Object *
2121mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
2122{
2123 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002124 int num = 0;
Bram Moolenaar75676462013-01-30 14:55:42 +01002125 Scheme_Object *onum = NULL;
2126 Scheme_Object *buf = NULL;
2127 Scheme_Object *fname;
2128
2129 MZ_GC_DECL_REG(3);
2130 MZ_GC_VAR_IN_REG(0, onum);
2131 MZ_GC_VAR_IN_REG(1, buf);
2132 MZ_GC_VAR_IN_REG(2, fname);
2133 MZ_GC_REG();
2134 fname = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002135
Bram Moolenaar555b2802005-05-19 21:08:39 +00002136#ifdef HAVE_SANDBOX
2137 sandbox_check();
2138#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002139 // TODO make open existing file
Bram Moolenaar75676462013-01-30 14:55:42 +01002140 num = buflist_add(BYTE_STRING_VALUE(fname), BLN_LISTED | BLN_CURBUF);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002141
2142 if (num == 0)
2143 raise_vim_exn(_("couldn't open buffer"));
2144
2145 onum = scheme_make_integer(num);
Bram Moolenaar75676462013-01-30 14:55:42 +01002146 buf = get_buffer_by_num(data, 1, &onum);
2147 MZ_GC_UNREG();
2148 return buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002149}
2150
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002151/*
2152 * (get-buff-by-num {buffernum})
2153 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002154 static Scheme_Object *
2155get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
2156{
2157 Vim_Prim *prim = (Vim_Prim *)data;
2158 buf_T *buf;
2159 int fnum;
2160
2161 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2162
Bram Moolenaar29323592016-07-24 22:04:11 +02002163 FOR_ALL_BUFFERS(buf)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002164 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002165 return buffer_new(buf);
2166
2167 return scheme_false;
2168}
2169
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002170/*
2171 * (get-buff-by-name {buffername})
2172 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002173 static Scheme_Object *
2174get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
2175{
2176 Vim_Prim *prim = (Vim_Prim *)data;
2177 buf_T *buf;
Bram Moolenaar75676462013-01-30 14:55:42 +01002178 Scheme_Object *buffer = NULL;
2179 Scheme_Object *fname = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002180
Bram Moolenaar75676462013-01-30 14:55:42 +01002181 MZ_GC_DECL_REG(2);
2182 MZ_GC_VAR_IN_REG(0, buffer);
2183 MZ_GC_VAR_IN_REG(1, fname);
2184 MZ_GC_REG();
2185 fname = GUARANTEED_STRING_ARG(prim->name, 0);
2186 buffer = scheme_false;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002187
Bram Moolenaar29323592016-07-24 22:04:11 +02002188 FOR_ALL_BUFFERS(buf)
Bram Moolenaar75676462013-01-30 14:55:42 +01002189 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002190 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002191 // empty string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002192 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002193 if (BYTE_STRING_VALUE(fname)[0] == NUL)
2194 buffer = buffer_new(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002195 }
Bram Moolenaar75676462013-01-30 14:55:42 +01002196 else if (!fnamecmp(buf->b_ffname, BYTE_STRING_VALUE(fname))
2197 || !fnamecmp(buf->b_sfname, BYTE_STRING_VALUE(fname)))
2198 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002199 // either short or long filename matches
Bram Moolenaar75676462013-01-30 14:55:42 +01002200 buffer = buffer_new(buf);
2201 }
2202 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002203
Bram Moolenaar75676462013-01-30 14:55:42 +01002204 MZ_GC_UNREG();
2205 return buffer;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002206}
2207
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002208/*
2209 * (get-next-buff [buffer])
2210 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002211 static Scheme_Object *
2212get_next_buffer(void *data, int argc, Scheme_Object **argv)
2213{
2214 Vim_Prim *prim = (Vim_Prim *)data;
2215 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2216
2217 if (buf->b_next == NULL)
2218 return scheme_false;
2219 else
2220 return buffer_new(buf->b_next);
2221}
2222
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002223/*
2224 * (get-prev-buff [buffer])
2225 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002226 static Scheme_Object *
2227get_prev_buffer(void *data, int argc, Scheme_Object **argv)
2228{
2229 Vim_Prim *prim = (Vim_Prim *)data;
2230 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2231
2232 if (buf->b_prev == NULL)
2233 return scheme_false;
2234 else
2235 return buffer_new(buf->b_prev);
2236}
2237
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002238/*
2239 * (get-buff-num [buffer])
2240 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002241 static Scheme_Object *
2242get_buffer_num(void *data, int argc, Scheme_Object **argv)
2243{
2244 Vim_Prim *prim = (Vim_Prim *)data;
2245 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2246
2247 return scheme_make_integer(buf->buf->b_fnum);
2248}
2249
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002250/*
2251 * (buff-count)
2252 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002253 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002254get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002255{
2256 buf_T *b;
2257 int n = 0;
2258
Bram Moolenaar29323592016-07-24 22:04:11 +02002259 FOR_ALL_BUFFERS(b) ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002260 return scheme_make_integer(n);
2261}
2262
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002263/*
2264 * (get-buff-name [buffer])
2265 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002266 static Scheme_Object *
2267get_buffer_name(void *data, int argc, Scheme_Object **argv)
2268{
2269 Vim_Prim *prim = (Vim_Prim *)data;
2270 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2271
Bram Moolenaar75676462013-01-30 14:55:42 +01002272 return scheme_make_byte_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002273}
2274
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002275/*
2276 * (curr-buff)
2277 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002278 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002279get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002280{
2281 return (Scheme_Object *)get_vim_curr_buffer();
2282}
2283
2284 static Scheme_Object *
2285buffer_new(buf_T *buf)
2286{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002287 vim_mz_buffer *self = NULL;
2288
2289 MZ_GC_DECL_REG(1);
2290 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002291
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002292 // We need to handle deletion of buffers underneath us.
2293 // If we add a "b_mzscheme_ref" field to the buf_T structure,
2294 // then we can get at it in buf_freeall() in vim.
Bram Moolenaare344bea2005-09-01 20:46:49 +00002295 if (buf->b_mzscheme_ref)
Bram Moolenaar75676462013-01-30 14:55:42 +01002296 return (Scheme_Object *)BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002297
Bram Moolenaar75676462013-01-30 14:55:42 +01002298 MZ_GC_REG();
2299 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_buffer));
Bram Moolenaara80faa82020-04-12 19:37:17 +02002300 CLEAR_POINTER(self);
Bram Moolenaar75676462013-01-30 14:55:42 +01002301#ifndef MZ_PRECISE_GC
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002302 scheme_dont_gc_ptr(self); // because buf isn't visible to GC
Bram Moolenaar75676462013-01-30 14:55:42 +01002303#else
2304 buf->b_mzscheme_ref = scheme_malloc_immobile_box(NULL);
2305#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002306 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01002307 BUFFER_REF(buf) = self;
2308 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002309 self->buf = buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002310 self->so.type = mz_buffer_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002311
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002312 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01002313 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002314}
2315
2316/*
2317 * (get-buff-size [buffer])
2318 *
2319 * Get the size (number of lines) in the current buffer.
2320 */
2321 static Scheme_Object *
2322get_buffer_size(void *data, int argc, Scheme_Object **argv)
2323{
2324 Vim_Prim *prim = (Vim_Prim *)data;
2325 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2326
2327 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
2328}
2329
2330/*
2331 * (get-buff-line {linenr} [buffer])
2332 *
2333 * Get a line from the specified buffer. The line number is
2334 * in Vim format (1-based). The line is returned as a MzScheme
2335 * string object.
2336 */
2337 static Scheme_Object *
2338get_buffer_line(void *data, int argc, Scheme_Object **argv)
2339{
2340 Vim_Prim *prim = (Vim_Prim *)data;
2341 vim_mz_buffer *buf;
2342 int linenr;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002343 char_u *line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002344
2345 buf = get_buffer_arg(prim->name, 1, argc, argv);
2346 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2347 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2348
2349 raise_if_error();
Bram Moolenaar75676462013-01-30 14:55:42 +01002350 return scheme_make_byte_string((char *)line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002351}
2352
2353
2354/*
2355 * (get-buff-line-list {start} {end} [buffer])
2356 *
2357 * Get a list of lines from the specified buffer. The line numbers
2358 * are in Vim format (1-based). The range is from lo up to, but not
2359 * including, hi. The list is returned as a list of string objects.
2360 */
2361 static Scheme_Object *
2362get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2363{
2364 Vim_Prim *prim = (Vim_Prim *)data;
2365 vim_mz_buffer *buf;
2366 int i, hi, lo, n;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002367 Scheme_Object *list = NULL;
2368
2369 MZ_GC_DECL_REG(1);
2370 MZ_GC_VAR_IN_REG(0, list);
2371 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002372
2373 buf = get_buffer_arg(prim->name, 2, argc, argv);
2374 list = scheme_null;
2375 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2376 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2377
2378 /*
2379 * Handle some error conditions
2380 */
2381 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002382 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002383
2384 if (hi < 0)
2385 hi = 0;
2386 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002387 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002388
2389 n = hi - lo;
2390
2391 for (i = n; i >= 0; --i)
2392 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002393 Scheme_Object *str = scheme_make_byte_string(
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002394 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2395 raise_if_error();
2396
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002397 // Set the list item
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002398 list = scheme_make_pair(str, list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002399 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002400 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002401 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002402 return list;
2403}
2404
2405/*
2406 * (set-buff-line {linenr} {string/#f} [buffer])
2407 *
2408 * Replace a line in the specified buffer. The line number is
2409 * in Vim format (1-based). The replacement line is given as
2410 * an MzScheme string object. The object is checked for validity
2411 * and correct format. An exception is thrown if the values are not
2412 * the correct format.
2413 *
2414 * It returns a Scheme Object that indicates the length of the
2415 * string changed.
2416 */
2417 static Scheme_Object *
2418set_buffer_line(void *data, int argc, Scheme_Object **argv)
2419{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002420 // First of all, we check the value of the supplied MzScheme object.
2421 // There are three cases:
2422 // 1. #f - this is a deletion.
2423 // 2. A string - this is a replacement.
2424 // 3. Anything else - this is an error.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002425 Vim_Prim *prim = (Vim_Prim *)data;
2426 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002427 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002428 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002429 int n;
2430
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002431 MZ_GC_DECL_REG(1);
2432 MZ_GC_VAR_IN_REG(0, line);
2433 MZ_GC_REG();
2434
Bram Moolenaar555b2802005-05-19 21:08:39 +00002435#ifdef HAVE_SANDBOX
2436 sandbox_check();
2437#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002438 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2439 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002440 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002441 line = argv[1];
2442 buf = get_buffer_arg(prim->name, 2, argc, argv);
2443
2444 check_line_range(n, buf->buf);
2445
2446 if (SCHEME_FALSEP(line))
2447 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002448 buf_T *savebuf = curbuf;
2449
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002450 curbuf = buf->buf;
2451
2452 if (u_savedel((linenr_T)n, 1L) == FAIL)
2453 {
2454 curbuf = savebuf;
2455 raise_vim_exn(_("cannot save undo information"));
2456 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02002457 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002458 {
2459 curbuf = savebuf;
2460 raise_vim_exn(_("cannot delete line"));
2461 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002462 if (buf->buf == curwin->w_buffer)
2463 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002464 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002465
2466 curbuf = savebuf;
2467
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002468 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002469 raise_if_error();
2470 return scheme_void;
2471 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002472 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002473 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002474 // Otherwise it's a line
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002475 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002476
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002477 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002478
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002479 curbuf = buf->buf;
2480
2481 if (u_savesub((linenr_T)n) == FAIL)
2482 {
2483 curbuf = savebuf;
2484 vim_free(save);
2485 raise_vim_exn(_("cannot save undo information"));
2486 }
2487 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2488 {
2489 curbuf = savebuf;
2490 vim_free(save);
2491 raise_vim_exn(_("cannot replace line"));
2492 }
2493 else
2494 {
2495 vim_free(save);
2496 changed_bytes((linenr_T)n, 0);
2497 }
2498
2499 curbuf = savebuf;
2500
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002501 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002502 if (buf->buf == curwin->w_buffer)
2503 check_cursor_col();
2504
2505 MZ_GC_UNREG();
2506 raise_if_error();
2507 return scheme_void;
2508 }
2509}
2510
2511 static void
2512free_array(char **array)
2513{
2514 char **curr = array;
2515 while (*curr != NULL)
2516 vim_free(*curr++);
2517 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002518}
2519
2520/*
2521 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2522 *
2523 * Replace a range of lines in the specified buffer. The line numbers are in
2524 * Vim format (1-based). The range is from lo up to, but not including, hi.
2525 * The replacement lines are given as a Scheme list of string objects. The
2526 * list is checked for validity and correct format.
2527 *
2528 * Errors are returned as a value of FAIL. The return value is OK on success.
2529 * If OK is returned and len_change is not NULL, *len_change is set to the
2530 * change in the buffer length.
2531 */
2532 static Scheme_Object *
2533set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2534{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002535 // First of all, we check the type of the supplied MzScheme object.
2536 // There are three cases:
2537 // 1. #f - this is a deletion.
2538 // 2. A list - this is a replacement.
2539 // 3. Anything else - this is an error.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002540 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002541 vim_mz_buffer *buf = NULL;
2542 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002543 int i, old_len, new_len, hi, lo;
2544 long extra;
2545
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002546 MZ_GC_DECL_REG(1);
2547 MZ_GC_VAR_IN_REG(0, line_list);
2548 MZ_GC_REG();
2549
Bram Moolenaar555b2802005-05-19 21:08:39 +00002550#ifdef HAVE_SANDBOX
2551 sandbox_check();
2552#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002553 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2554 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2555 if (!SCHEME_PAIRP(argv[2])
2556 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2557 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2558 line_list = argv[2];
2559 buf = get_buffer_arg(prim->name, 3, argc, argv);
2560 old_len = hi - lo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002561 if (old_len < 0) // process inverse values wisely
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002562 {
2563 i = lo;
2564 lo = hi;
2565 hi = i;
2566 old_len = -old_len;
2567 }
2568 extra = 0;
2569
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002570 check_line_range(lo, buf->buf); // inclusive
2571 check_line_range(hi - 1, buf->buf); // exclusive
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002572
2573 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2574 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002575 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002576 curbuf = buf->buf;
2577
2578 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2579 {
2580 curbuf = savebuf;
2581 raise_vim_exn(_("cannot save undo information"));
2582 }
2583 else
2584 {
2585 for (i = 0; i < old_len; i++)
Bram Moolenaarca70c072020-05-30 20:30:46 +02002586 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002587 {
2588 curbuf = savebuf;
2589 raise_vim_exn(_("cannot delete line"));
2590 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002591 if (buf->buf == curwin->w_buffer)
2592 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002593 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002594 }
2595
2596 curbuf = savebuf;
2597
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002598 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002599 raise_if_error();
2600 return scheme_void;
2601 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002602 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002603 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002604 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002605
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002606 // List
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002607 new_len = scheme_proper_list_length(line_list);
2608 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002609 if (new_len < 0) // improper or cyclic list
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002610 scheme_wrong_type(prim->name, "proper list",
2611 2, argc, argv);
2612 else
2613 {
2614 char **array = NULL;
2615 Scheme_Object *line = NULL;
2616 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002617
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002618 MZ_GC_DECL_REG(2);
2619 MZ_GC_VAR_IN_REG(0, line);
2620 MZ_GC_VAR_IN_REG(1, rest);
2621 MZ_GC_REG();
2622
Bram Moolenaara80faa82020-04-12 19:37:17 +02002623 array = ALLOC_CLEAR_MULT(char *, new_len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002624
2625 rest = line_list;
2626 for (i = 0; i < new_len; ++i)
2627 {
2628 line = SCHEME_CAR(rest);
2629 rest = SCHEME_CDR(rest);
2630 if (!SCHEME_STRINGP(line))
2631 {
2632 free_array(array);
2633 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2634 }
2635 array[i] = string_to_line(line);
2636 }
2637
2638 curbuf = buf->buf;
2639
2640 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2641 {
2642 curbuf = savebuf;
2643 free_array(array);
2644 raise_vim_exn(_("cannot save undo information"));
2645 }
2646
2647 /*
2648 * If the size of the range is reducing (ie, new_len < old_len) we
2649 * need to delete some old_len. We do this at the start, by
2650 * repeatedly deleting line "lo".
2651 */
2652 for (i = 0; i < old_len - new_len; ++i)
2653 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02002654 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002655 {
2656 curbuf = savebuf;
2657 free_array(array);
2658 raise_vim_exn(_("cannot delete line"));
2659 }
2660 extra--;
2661 }
2662
2663 /*
2664 * For as long as possible, replace the existing old_len with the
2665 * new old_len. This is a more efficient operation, as it requires
2666 * less memory allocation and freeing.
2667 */
2668 for (i = 0; i < old_len && i < new_len; i++)
2669 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2670 {
2671 curbuf = savebuf;
2672 free_array(array);
2673 raise_vim_exn(_("cannot replace line"));
2674 }
2675
2676 /*
2677 * Now we may need to insert the remaining new_len. We don't need to
2678 * free the string passed back because MzScheme has control of that
2679 * memory.
2680 */
2681 while (i < new_len)
2682 {
2683 if (ml_append((linenr_T)(lo + i - 1),
2684 (char_u *)array[i], 0, FALSE) == FAIL)
2685 {
2686 curbuf = savebuf;
2687 free_array(array);
2688 raise_vim_exn(_("cannot insert line"));
2689 }
2690 ++i;
2691 ++extra;
2692 }
2693 MZ_GC_UNREG();
2694 free_array(array);
2695 }
2696
2697 /*
2698 * Adjust marks. Invalidate any which lie in the
2699 * changed range, and move any in the remainder of the buffer.
2700 */
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002701 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2702 (long)MAXLNUM, (long)extra);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002703 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2704
2705 if (buf->buf == curwin->w_buffer)
2706 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002707 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002708
2709 MZ_GC_UNREG();
2710 raise_if_error();
2711 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002712 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002713}
2714
2715/*
2716 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2717 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002718 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002719 * The line number is in Vim format (1-based). The lines to be inserted are
2720 * given as an MzScheme list of string objects or as a single string. The lines
2721 * to be added are checked for validity and correct format. Errors are
2722 * returned as a value of FAIL. The return value is OK on success.
2723 * If OK is returned and len_change is not NULL, *len_change
2724 * is set to the change in the buffer length.
2725 */
2726 static Scheme_Object *
2727insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2728{
2729 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002730 vim_mz_buffer *buf = NULL;
2731 Scheme_Object *list = NULL;
2732 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002733 int i, n, size;
2734
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002735 MZ_GC_DECL_REG(1);
2736 MZ_GC_VAR_IN_REG(0, list);
2737 MZ_GC_REG();
2738
Bram Moolenaar555b2802005-05-19 21:08:39 +00002739#ifdef HAVE_SANDBOX
2740 sandbox_check();
2741#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002742 /*
2743 * First of all, we check the type of the supplied MzScheme object.
2744 * It must be a string or a list, or the call is in error.
2745 */
2746 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2747 list = argv[1];
2748
2749 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2750 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2751 buf = get_buffer_arg(prim->name, 2, argc, argv);
2752
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002753 if (n != 0) // 0 can be used in insert
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002754 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002755 if (SCHEME_STRINGP(list))
2756 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002757 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002758
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002759 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002760 curbuf = buf->buf;
2761
2762 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2763 {
2764 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002765 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002766 raise_vim_exn(_("cannot save undo information"));
2767 }
2768 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2769 {
2770 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002771 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002772 raise_vim_exn(_("cannot insert line"));
2773 }
2774 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002775 {
2776 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002777 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002778 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002779
2780 curbuf = savebuf;
2781 update_screen(VALID);
2782
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002783 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002784 raise_if_error();
2785 return scheme_void;
2786 }
2787
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002788 // List
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002789 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002790 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002791 if (size < 0) // improper or cyclic list
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002792 scheme_wrong_type(prim->name, "proper list",
2793 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002794 else
2795 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002796 Scheme_Object *line = NULL;
2797 Scheme_Object *rest = NULL;
2798 char **array;
2799 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002800
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002801 MZ_GC_DECL_REG(2);
2802 MZ_GC_VAR_IN_REG(0, line);
2803 MZ_GC_VAR_IN_REG(1, rest);
2804 MZ_GC_REG();
2805
Bram Moolenaara80faa82020-04-12 19:37:17 +02002806 array = ALLOC_CLEAR_MULT(char *, size + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002807
2808 rest = list;
2809 for (i = 0; i < size; ++i)
2810 {
2811 line = SCHEME_CAR(rest);
2812 rest = SCHEME_CDR(rest);
2813 array[i] = string_to_line(line);
2814 }
2815
2816 curbuf = buf->buf;
2817
2818 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2819 {
2820 curbuf = savebuf;
2821 free_array(array);
2822 raise_vim_exn(_("cannot save undo information"));
2823 }
2824 else
2825 {
2826 for (i = 0; i < size; ++i)
2827 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2828 0, FALSE) == FAIL)
2829 {
2830 curbuf = savebuf;
2831 free_array(array);
2832 raise_vim_exn(_("cannot insert line"));
2833 }
2834
2835 if (i > 0)
2836 appended_lines_mark((linenr_T)n, (long)i);
2837 }
2838 free_array(array);
2839 MZ_GC_UNREG();
2840 curbuf = savebuf;
2841 update_screen(VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002842 }
2843
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002844 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002845 raise_if_error();
2846 return scheme_void;
2847}
2848
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002849/*
2850 * Predicates
2851 */
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002852/*
2853 * (buff? obj)
2854 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002855 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002856vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002857{
2858 if (SCHEME_VIMBUFFERP(argv[0]))
2859 return scheme_true;
2860 else
2861 return scheme_false;
2862}
2863
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002864/*
2865 * (win? obj)
2866 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002867 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002868vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002869{
2870 if (SCHEME_VIMWINDOWP(argv[0]))
2871 return scheme_true;
2872 else
2873 return scheme_false;
2874}
2875
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002876/*
2877 * (buff-valid? obj)
2878 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002879 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002880vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002881{
2882 if (SCHEME_VIMBUFFERP(argv[0])
2883 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2884 return scheme_true;
2885 else
2886 return scheme_false;
2887}
2888
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002889/*
2890 * (win-valid? obj)
2891 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002892 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002893vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002894{
2895 if (SCHEME_VIMWINDOWP(argv[0])
2896 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2897 return scheme_true;
2898 else
2899 return scheme_false;
2900}
2901
2902/*
2903 *===========================================================================
2904 * Utilities
2905 *===========================================================================
2906 */
2907
2908/*
2909 * Convert an MzScheme string into a Vim line.
2910 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002911 * All internal nulls are replaced by newline characters.
2912 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002913 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002914 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002915 */
2916 static char *
2917string_to_line(Scheme_Object *obj)
2918{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002919 char *scheme_str = NULL;
2920 char *vim_str = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01002921 OUTPUT_LEN_TYPE len;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002922 int i;
2923
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002924 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002925
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002926 // Error checking: String must not contain newlines, as we
2927 // are replacing a single line, and we must replace it with
2928 // a single line.
Bram Moolenaar75676462013-01-30 14:55:42 +01002929 if (memchr(scheme_str, '\n', len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002930 scheme_signal_error(_("string cannot contain newlines"));
2931
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002932 vim_str = alloc(len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002933
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002934 // Create a copy of the string, with internal nulls replaced by
2935 // newline characters, as is the vim convention.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002936 for (i = 0; i < len; ++i)
2937 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002938 if (scheme_str[i] == '\0')
2939 vim_str[i] = '\n';
2940 else
2941 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002942 }
2943
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002944 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002945
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002946 MZ_GC_CHECK();
2947 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002948}
2949
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002950#ifdef FEAT_EVAL
2951/*
2952 * Convert Vim value into MzScheme, adopted from if_python.c
2953 */
2954 static Scheme_Object *
Bram Moolenaar75676462013-01-30 14:55:42 +01002955vim_to_mzscheme(typval_T *vim_value)
2956{
2957 Scheme_Object *result = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002958 // hash table to store visited values to avoid infinite loops
Bram Moolenaar75676462013-01-30 14:55:42 +01002959 Scheme_Hash_Table *visited = NULL;
2960
2961 MZ_GC_DECL_REG(2);
2962 MZ_GC_VAR_IN_REG(0, result);
2963 MZ_GC_VAR_IN_REG(1, visited);
2964 MZ_GC_REG();
2965
2966 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2967 MZ_GC_CHECK();
2968
2969 result = vim_to_mzscheme_impl(vim_value, 1, visited);
2970
2971 MZ_GC_UNREG();
2972 return result;
2973}
2974
2975 static Scheme_Object *
2976vim_to_mzscheme_impl(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002977{
2978 Scheme_Object *result = NULL;
2979 int new_value = TRUE;
2980
Bram Moolenaar75676462013-01-30 14:55:42 +01002981 MZ_GC_DECL_REG(2);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002982 MZ_GC_VAR_IN_REG(0, result);
Bram Moolenaar75676462013-01-30 14:55:42 +01002983 MZ_GC_VAR_IN_REG(1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002984 MZ_GC_REG();
2985
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002986 // Avoid infinite recursion
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002987 if (depth > 100)
2988 {
2989 MZ_GC_UNREG();
2990 return scheme_void;
2991 }
2992
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002993 // Check if we run into a recursive loop. The item must be in visited
2994 // then and we can use it again.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002995 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
2996 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002997 if (result != NULL) // found, do nothing
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002998 new_value = FALSE;
2999 else if (vim_value->v_type == VAR_STRING)
3000 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003001 result = scheme_make_byte_string((char *)vim_value->vval.v_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003002 MZ_GC_CHECK();
3003 }
3004 else if (vim_value->v_type == VAR_NUMBER)
3005 {
3006 result = scheme_make_integer((long)vim_value->vval.v_number);
3007 MZ_GC_CHECK();
3008 }
3009# ifdef FEAT_FLOAT
3010 else if (vim_value->v_type == VAR_FLOAT)
3011 {
3012 result = scheme_make_double((double)vim_value->vval.v_float);
3013 MZ_GC_CHECK();
3014 }
3015# endif
3016 else if (vim_value->v_type == VAR_LIST)
3017 {
3018 list_T *list = vim_value->vval.v_list;
3019 listitem_T *curr;
3020
3021 if (list == NULL || list->lv_first == NULL)
3022 result = scheme_null;
3023 else
3024 {
3025 Scheme_Object *obj = NULL;
3026
3027 MZ_GC_DECL_REG(1);
3028 MZ_GC_VAR_IN_REG(0, obj);
3029 MZ_GC_REG();
3030
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003031 curr = list->lv_u.mat.lv_last;
Bram Moolenaar75676462013-01-30 14:55:42 +01003032 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003033 result = scheme_make_pair(obj, scheme_null);
3034 MZ_GC_CHECK();
3035
3036 while (curr != list->lv_first)
3037 {
3038 curr = curr->li_prev;
Bram Moolenaar75676462013-01-30 14:55:42 +01003039 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003040 result = scheme_make_pair(obj, result);
3041 MZ_GC_CHECK();
3042 }
3043 }
3044 MZ_GC_UNREG();
3045 }
3046 else if (vim_value->v_type == VAR_DICT)
3047 {
3048 Scheme_Object *key = NULL;
3049 Scheme_Object *obj = NULL;
3050
3051 MZ_GC_DECL_REG(2);
3052 MZ_GC_VAR_IN_REG(0, key);
3053 MZ_GC_VAR_IN_REG(1, obj);
3054 MZ_GC_REG();
3055
3056 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
3057 MZ_GC_CHECK();
3058 if (vim_value->vval.v_dict != NULL)
3059 {
3060 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
3061 long_u todo = ht->ht_used;
3062 hashitem_T *hi;
3063 dictitem_T *di;
3064
3065 for (hi = ht->ht_array; todo > 0; ++hi)
3066 {
3067 if (!HASHITEM_EMPTY(hi))
3068 {
3069 --todo;
3070
3071 di = dict_lookup(hi);
Bram Moolenaar75676462013-01-30 14:55:42 +01003072 obj = vim_to_mzscheme_impl(&di->di_tv, depth + 1, visited);
3073 key = scheme_make_byte_string((char *)hi->hi_key);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003074 MZ_GC_CHECK();
3075 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
3076 MZ_GC_CHECK();
3077 }
3078 }
3079 }
3080 MZ_GC_UNREG();
3081 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003082 else if (vim_value->v_type == VAR_FUNC)
3083 {
3084 Scheme_Object *funcname = NULL;
3085
3086 MZ_GC_DECL_REG(1);
3087 MZ_GC_VAR_IN_REG(0, funcname);
3088 MZ_GC_REG();
3089
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003090 // FIXME: func_ref() and func_unref() are needed.
Bram Moolenaar75676462013-01-30 14:55:42 +01003091 funcname = scheme_make_byte_string((char *)vim_value->vval.v_string);
3092 MZ_GC_CHECK();
3093 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3094 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3095 MZ_GC_CHECK();
3096
3097 MZ_GC_UNREG();
3098 }
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003099 else if (vim_value->v_type == VAR_PARTIAL)
3100 {
3101 if (vim_value->vval.v_partial == NULL)
3102 result = scheme_null;
3103 else
3104 {
3105 Scheme_Object *funcname = NULL;
3106
3107 MZ_GC_DECL_REG(1);
3108 MZ_GC_VAR_IN_REG(0, funcname);
3109 MZ_GC_REG();
3110
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003111 // FIXME: func_ref() and func_unref() are needed.
3112 // TODO: Support pt_dict and pt_argv.
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003113 funcname = scheme_make_byte_string(
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003114 (char *)partial_name(vim_value->vval.v_partial));
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003115 MZ_GC_CHECK();
3116 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3117 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3118 MZ_GC_CHECK();
3119
3120 MZ_GC_UNREG();
3121 }
3122 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003123 else if (vim_value->v_type == VAR_BOOL || vim_value->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003124 {
3125 if (vim_value->vval.v_number <= VVAL_TRUE)
3126 result = scheme_make_integer((long)vim_value->vval.v_number);
3127 else
3128 result = scheme_null;
3129 MZ_GC_CHECK();
3130 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003131 else
3132 {
3133 result = scheme_void;
3134 new_value = FALSE;
3135 }
3136 if (new_value)
3137 {
3138 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
3139 MZ_GC_CHECK();
3140 }
3141 MZ_GC_UNREG();
3142 return result;
3143}
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003144
3145 static int
Bram Moolenaar75676462013-01-30 14:55:42 +01003146mzscheme_to_vim(Scheme_Object *obj, typval_T *tv)
3147{
3148 int i, status;
3149 Scheme_Hash_Table *visited = NULL;
3150
3151 MZ_GC_DECL_REG(2);
3152 MZ_GC_VAR_IN_REG(0, obj);
3153 MZ_GC_VAR_IN_REG(1, visited);
3154 MZ_GC_REG();
3155
3156 visited = scheme_make_hash_table(SCHEME_hash_ptr);
3157 MZ_GC_CHECK();
3158
3159 status = mzscheme_to_vim_impl(obj, tv, 1, visited);
3160 for (i = 0; i < visited->size; ++i)
3161 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003162 // free up remembered objects
Bram Moolenaar75676462013-01-30 14:55:42 +01003163 if (visited->vals[i] != NULL)
3164 free_tv((typval_T *)visited->vals[i]);
3165 }
3166
3167 MZ_GC_UNREG();
3168 return status;
3169}
3170 static int
3171mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003172 Scheme_Hash_Table *visited)
3173{
3174 int status = OK;
3175 typval_T *found;
Bram Moolenaar75676462013-01-30 14:55:42 +01003176
3177 MZ_GC_DECL_REG(2);
3178 MZ_GC_VAR_IN_REG(0, obj);
3179 MZ_GC_VAR_IN_REG(1, visited);
3180 MZ_GC_REG();
3181
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003182 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003183 if (depth > 100) // limit the deepest recursion level
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003184 {
3185 tv->v_type = VAR_NUMBER;
3186 tv->vval.v_number = 0;
3187 return FAIL;
3188 }
3189
3190 found = (typval_T *)scheme_hash_get(visited, obj);
3191 if (found != NULL)
3192 copy_tv(found, tv);
3193 else if (SCHEME_VOIDP(obj))
3194 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003195 tv->v_type = VAR_SPECIAL;
3196 tv->vval.v_number = VVAL_NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003197 }
3198 else if (SCHEME_INTP(obj))
3199 {
3200 tv->v_type = VAR_NUMBER;
3201 tv->vval.v_number = SCHEME_INT_VAL(obj);
3202 }
3203 else if (SCHEME_BOOLP(obj))
3204 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003205 tv->v_type = VAR_BOOL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003206 tv->vval.v_number = SCHEME_TRUEP(obj);
3207 }
3208# ifdef FEAT_FLOAT
3209 else if (SCHEME_DBLP(obj))
3210 {
3211 tv->v_type = VAR_FLOAT;
3212 tv->vval.v_float = SCHEME_DBL_VAL(obj);
3213 }
3214# endif
Bram Moolenaar75676462013-01-30 14:55:42 +01003215 else if (SCHEME_BYTE_STRINGP(obj))
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003216 {
3217 tv->v_type = VAR_STRING;
Bram Moolenaar75676462013-01-30 14:55:42 +01003218 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(obj));
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003219 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003220# if MZSCHEME_VERSION_MAJOR >= 299
3221 else if (SCHEME_CHAR_STRINGP(obj))
3222 {
3223 Scheme_Object *tmp = NULL;
3224 MZ_GC_DECL_REG(1);
3225 MZ_GC_VAR_IN_REG(0, tmp);
3226 MZ_GC_REG();
3227
3228 tmp = scheme_char_string_to_byte_string(obj);
3229 tv->v_type = VAR_STRING;
3230 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(tmp));
3231 MZ_GC_UNREG();
3232 }
3233#endif
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003234 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
3235 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3236 {
3237 list_T *list = list_alloc();
3238 if (list == NULL)
3239 status = FAIL;
3240 else
3241 {
3242 int i;
3243 Scheme_Object *curr = NULL;
3244 Scheme_Object *cval = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003245 // temporary var to hold current element of vectors and pairs
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003246 typval_T *v;
3247
3248 MZ_GC_DECL_REG(2);
3249 MZ_GC_VAR_IN_REG(0, curr);
3250 MZ_GC_VAR_IN_REG(1, cval);
3251 MZ_GC_REG();
3252
3253 tv->v_type = VAR_LIST;
3254 tv->vval.v_list = list;
3255 ++list->lv_refcount;
3256
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003257 v = ALLOC_ONE(typval_T);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003258 if (v == NULL)
3259 status = FAIL;
3260 else
3261 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003262 // add the value in advance to allow handling of self-referential
3263 // data structures
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003264 typval_T *visited_tv = ALLOC_ONE(typval_T);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003265 copy_tv(tv, visited_tv);
3266 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3267
3268 if (SCHEME_VECTORP(obj))
3269 {
3270 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
3271 {
3272 cval = SCHEME_VEC_ELS(obj)[i];
Bram Moolenaar75676462013-01-30 14:55:42 +01003273 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003274 if (status == FAIL)
3275 break;
3276 status = list_append_tv(list, v);
3277 clear_tv(v);
3278 if (status == FAIL)
3279 break;
3280 }
3281 }
3282 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3283 {
3284 for (curr = obj;
3285 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
3286 curr = SCHEME_CDR(curr))
3287 {
3288 cval = SCHEME_CAR(curr);
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 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003297 // improper list not terminated with null
3298 // need to handle the last element
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003299 if (status == OK && !SCHEME_NULLP(curr))
3300 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003301 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003302 if (status == OK)
3303 {
3304 status = list_append_tv(list, v);
3305 clear_tv(v);
3306 }
3307 }
3308 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003309 // nothing to do for scheme_null
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003310 vim_free(v);
3311 }
3312 MZ_GC_UNREG();
3313 }
3314 }
3315 else if (SCHEME_HASHTP(obj))
3316 {
3317 int i;
3318 dict_T *dict;
3319 Scheme_Object *key = NULL;
3320 Scheme_Object *val = NULL;
3321
3322 MZ_GC_DECL_REG(2);
3323 MZ_GC_VAR_IN_REG(0, key);
3324 MZ_GC_VAR_IN_REG(1, val);
3325 MZ_GC_REG();
3326
3327 dict = dict_alloc();
3328 if (dict == NULL)
3329 status = FAIL;
3330 else
3331 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003332 typval_T *visited_tv = ALLOC_ONE(typval_T);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003333
3334 tv->v_type = VAR_DICT;
3335 tv->vval.v_dict = dict;
3336 ++dict->dv_refcount;
3337
3338 copy_tv(tv, visited_tv);
3339 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3340
3341 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
3342 {
3343 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
3344 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003345 // generate item for `display'ed Scheme key
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003346 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
3347 ((Scheme_Hash_Table *) obj)->keys[i]));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003348 // convert Scheme val to Vim and add it to the dict
Bram Moolenaar75676462013-01-30 14:55:42 +01003349 if (mzscheme_to_vim_impl(((Scheme_Hash_Table *) obj)->vals[i],
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003350 &item->di_tv, depth + 1, visited) == FAIL
3351 || dict_add(dict, item) == FAIL)
3352 {
3353 dictitem_free(item);
3354 status = FAIL;
3355 break;
3356 }
3357 }
3358
3359 }
3360 }
3361 MZ_GC_UNREG();
3362 }
3363 else
3364 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003365 // `display' any other value to string
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003366 tv->v_type = VAR_STRING;
3367 tv->vval.v_string = (char_u *)string_to_line(obj);
3368 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003369 MZ_GC_UNREG();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003370 return status;
3371}
3372
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003373/*
3374 * Scheme prim procedure wrapping Vim funcref
3375 */
Bram Moolenaar75676462013-01-30 14:55:42 +01003376 static Scheme_Object *
3377vim_funcref(void *name, int argc, Scheme_Object **argv)
3378{
3379 int i;
3380 typval_T args;
3381 int status = OK;
3382 Scheme_Object *result = NULL;
3383 list_T *list = list_alloc();
3384
3385 MZ_GC_DECL_REG(1);
3386 MZ_GC_VAR_IN_REG(0, result);
3387 MZ_GC_REG();
3388
3389 result = scheme_void;
3390 if (list == NULL)
3391 status = FAIL;
3392 else
3393 {
3394 args.v_type = VAR_LIST;
3395 args.vval.v_list = list;
3396 ++list->lv_refcount;
3397 for (i = 0; status == OK && i < argc; ++i)
3398 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003399 typval_T *v = ALLOC_ONE(typval_T);
Bram Moolenaar75676462013-01-30 14:55:42 +01003400 if (v == NULL)
3401 status = FAIL;
3402 else
3403 {
3404 status = mzscheme_to_vim(argv[i], v);
3405 if (status == OK)
3406 {
3407 status = list_append_tv(list, v);
3408 clear_tv(v);
3409 }
3410 vim_free(v);
3411 }
3412 }
3413 if (status == OK)
3414 {
3415 typval_T ret;
3416 ret.v_type = VAR_UNKNOWN;
3417
3418 mzscheme_call_vim(BYTE_STRING_VALUE((Scheme_Object *)name), &args, &ret);
3419 MZ_GC_CHECK();
3420 result = vim_to_mzscheme(&ret);
3421 clear_tv(&ret);
3422 MZ_GC_CHECK();
3423 }
3424 }
3425 clear_tv(&args);
3426 MZ_GC_UNREG();
3427 if (status != OK)
3428 raise_vim_exn(_("error converting Scheme values to Vim"));
3429 else
3430 raise_if_error();
3431 return result;
3432}
3433
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003434 void
3435do_mzeval(char_u *str, typval_T *rettv)
3436{
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003437 Scheme_Object *ret = NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003438
Bram Moolenaar75676462013-01-30 14:55:42 +01003439 MZ_GC_DECL_REG(1);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003440 MZ_GC_VAR_IN_REG(0, ret);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003441 MZ_GC_REG();
3442
3443 if (mzscheme_init())
3444 {
3445 MZ_GC_UNREG();
3446 return;
3447 }
3448
3449 MZ_GC_CHECK();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003450 if (eval_with_exn_handling(str, do_eval, &ret) == OK)
Bram Moolenaar75676462013-01-30 14:55:42 +01003451 mzscheme_to_vim(ret, rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003452
3453 MZ_GC_UNREG();
3454}
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003455#endif
3456
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003457/*
3458 * Check to see whether a Vim error has been reported, or a keyboard
3459 * interrupt (from vim --> got_int) has been detected.
3460 */
3461 static int
3462vim_error_check(void)
3463{
3464 return (got_int || did_emsg);
3465}
3466
3467/*
3468 * register Scheme exn:vim
3469 */
3470 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003471register_vim_exn(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003472{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003473 int nc = 0;
3474 int i;
3475 Scheme_Object *struct_exn = NULL;
3476 Scheme_Object *exn_name = NULL;
3477
3478 MZ_GC_DECL_REG(2);
3479 MZ_GC_VAR_IN_REG(0, struct_exn);
3480 MZ_GC_VAR_IN_REG(1, exn_name);
3481 MZ_GC_REG();
3482
3483 exn_name = scheme_intern_symbol("exn:vim");
3484 MZ_GC_CHECK();
3485 struct_exn = scheme_builtin_value("struct:exn");
3486 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003487
3488 if (vim_exn == NULL)
3489 vim_exn = scheme_make_struct_type(exn_name,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003490 struct_exn, NULL, 0, 0, NULL, NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003491#if MZSCHEME_VERSION_MAJOR >= 299
3492 , NULL
3493#endif
3494 );
3495
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003496
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003497 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003498 Scheme_Object **tmp = NULL;
3499 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
3500 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
3501 MZ_GC_DECL_REG(6);
3502 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
3503 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
3504 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003505
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003506 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003507 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
3508 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003509
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003510 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
3511 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
3512 MZ_GC_CHECK();
3513
3514 for (i = 0; i < nc; i++)
3515 {
3516 scheme_add_global_symbol(exn_names[i],
3517 exn_values[i], environment);
3518 MZ_GC_CHECK();
3519 }
3520 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003521 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003522 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003523}
3524
3525/*
3526 * raise exn:vim, may be with additional info string
3527 */
3528 void
3529raise_vim_exn(const char *add_info)
3530{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003531 char *fmt = _("Vim error: ~a");
3532 Scheme_Object *argv[2] = {NULL, NULL};
3533 Scheme_Object *exn = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01003534 Scheme_Object *byte_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003535
Bram Moolenaar75676462013-01-30 14:55:42 +01003536 MZ_GC_DECL_REG(5);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003537 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
3538 MZ_GC_VAR_IN_REG(3, exn);
Bram Moolenaar75676462013-01-30 14:55:42 +01003539 MZ_GC_VAR_IN_REG(4, byte_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003540 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003541
3542 if (add_info != NULL)
3543 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003544 char *c_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003545 Scheme_Object *info = NULL;
3546
3547 MZ_GC_DECL_REG(3);
3548 MZ_GC_VAR_IN_REG(0, c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003549 MZ_GC_VAR_IN_REG(2, info);
3550 MZ_GC_REG();
3551
Bram Moolenaar75676462013-01-30 14:55:42 +01003552 info = scheme_make_byte_string(add_info);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003553 MZ_GC_CHECK();
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02003554 c_string = scheme_format_utf8(fmt, (int)STRLEN(fmt), 1, &info, NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003555 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01003556 byte_string = scheme_make_byte_string(c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003557 MZ_GC_CHECK();
3558 argv[0] = scheme_byte_string_to_char_string(byte_string);
Bram Moolenaar555b2802005-05-19 21:08:39 +00003559 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003560 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003561 }
3562 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003563 {
3564 byte_string = scheme_make_byte_string(_("Vim error"));
3565 MZ_GC_CHECK();
3566 argv[0] = scheme_byte_string_to_char_string(byte_string);
3567 MZ_GC_CHECK();
3568 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003569 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003570
Bram Moolenaar049377e2007-05-12 15:32:12 +00003571#if MZSCHEME_VERSION_MAJOR < 360
3572 argv[1] = scheme_current_continuation_marks();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003573 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003574#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003575 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003576 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003577#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003578
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003579 exn = scheme_make_struct_instance(vim_exn, 2, argv);
3580 MZ_GC_CHECK();
3581 scheme_raise(exn);
3582 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003583}
3584
3585 void
3586raise_if_error(void)
3587{
3588 if (vim_error_check())
3589 raise_vim_exn(NULL);
3590}
3591
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003592/*
3593 * get buffer:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003594 * either current
3595 * or passed as argv[argnum] with checks
3596 */
3597 static vim_mz_buffer *
3598get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3599{
3600 vim_mz_buffer *b;
3601
3602 if (argc < argnum + 1)
3603 return get_vim_curr_buffer();
3604 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3605 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3606 b = (vim_mz_buffer *)argv[argnum];
3607 (void)get_valid_buffer(argv[argnum]);
3608 return b;
3609}
3610
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003611/*
3612 * get window:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003613 * either current
3614 * or passed as argv[argnum] with checks
3615 */
3616 static vim_mz_window *
3617get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3618{
3619 vim_mz_window *w;
3620
3621 if (argc < argnum + 1)
3622 return get_vim_curr_window();
3623 w = (vim_mz_window *)argv[argnum];
3624 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3625 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3626 (void)get_valid_window(argv[argnum]);
3627 return w;
3628}
3629
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003630/*
3631 * get valid Vim buffer from Scheme_Object*
3632 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003633buf_T *get_valid_buffer(void *obj)
3634{
3635 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3636
3637 if (buf == INVALID_BUFFER_VALUE)
3638 scheme_signal_error(_("buffer is invalid"));
3639 return buf;
3640}
3641
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003642/*
3643 * get valid Vim window from Scheme_Object*
3644 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003645win_T *get_valid_window(void *obj)
3646{
3647 win_T *win = ((vim_mz_window *)obj)->win;
3648 if (win == INVALID_WINDOW_VALUE)
3649 scheme_signal_error(_("window is invalid"));
3650 return win;
3651}
3652
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003653 int
3654mzthreads_allowed(void)
3655{
3656 return mz_threads_allow;
3657}
3658
3659 static int
3660line_in_range(linenr_T lnum, buf_T *buf)
3661{
3662 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3663}
3664
3665 static void
3666check_line_range(linenr_T lnum, buf_T *buf)
3667{
3668 if (!line_in_range(lnum, buf))
3669 scheme_signal_error(_("linenr out of range"));
3670}
3671
3672/*
3673 * Check if deleting lines made the cursor position invalid
3674 * (or you'll get msg from Vim about invalid linenr).
3675 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3676 * deleted). Got from if_python.c
3677 */
3678 static void
3679mz_fix_cursor(int lo, int hi, int extra)
3680{
3681 if (curwin->w_cursor.lnum >= lo)
3682 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003683 // Adjust the cursor position if it's in/after the changed
3684 // lines.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003685 if (curwin->w_cursor.lnum >= hi)
3686 {
3687 curwin->w_cursor.lnum += extra;
3688 check_cursor_col();
3689 }
3690 else if (extra < 0)
3691 {
3692 curwin->w_cursor.lnum = lo;
3693 check_cursor();
3694 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003695 else
3696 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003697 changed_cline_bef_curs();
3698 }
3699 invalidate_botline();
3700}
3701
3702static Vim_Prim prims[]=
3703{
3704 /*
3705 * Buffer-related commands
3706 */
3707 {get_buffer_line, "get-buff-line", 1, 2},
3708 {set_buffer_line, "set-buff-line", 2, 3},
3709 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3710 {get_buffer_name, "get-buff-name", 0, 1},
3711 {get_buffer_num, "get-buff-num", 0, 1},
3712 {get_buffer_size, "get-buff-size", 0, 1},
3713 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3714 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3715 {get_curr_buffer, "curr-buff", 0, 0},
3716 {get_buffer_count, "buff-count", 0, 0},
3717 {get_next_buffer, "get-next-buff", 0, 1},
3718 {get_prev_buffer, "get-prev-buff", 0, 1},
3719 {mzscheme_open_buffer, "open-buff", 1, 1},
3720 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3721 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003722 /*
3723 * Window-related commands
3724 */
3725 {get_curr_win, "curr-win", 0, 0},
3726 {get_window_count, "win-count", 0, 0},
3727 {get_window_by_num, "get-win-by-num", 1, 1},
3728 {get_window_num, "get-win-num", 0, 1},
3729 {get_window_buffer, "get-win-buffer", 0, 1},
3730 {get_window_height, "get-win-height", 0, 1},
3731 {set_window_height, "set-win-height", 1, 2},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003732 {get_window_width, "get-win-width", 0, 1},
3733 {set_window_width, "set-win-width", 1, 2},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003734 {get_cursor, "get-cursor", 0, 1},
3735 {set_cursor, "set-cursor", 1, 2},
3736 {get_window_list, "get-win-list", 0, 1},
3737 /*
3738 * Vim-related commands
3739 */
3740 {vim_command, "command", 1, 1},
3741 {vim_eval, "eval", 1, 1},
3742 {get_range_start, "range-start", 0, 0},
3743 {get_range_end, "range-end", 0, 0},
3744 {mzscheme_beep, "beep", 0, 0},
3745 {get_option, "get-option", 1, 2},
3746 {set_option, "set-option", 1, 2},
3747 /*
3748 * small utilities
3749 */
3750 {vim_bufferp, "buff?", 1, 1},
3751 {vim_windowp, "win?", 1, 1},
3752 {vim_buffer_validp, "buff-valid?", 1, 1},
3753 {vim_window_validp, "win-valid?", 1, 1}
3754};
3755
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003756/*
3757 * return MzScheme wrapper for curbuf
3758 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003759 static vim_mz_buffer *
3760get_vim_curr_buffer(void)
3761{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003762 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003763 return (vim_mz_buffer *)buffer_new(curbuf);
3764 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003765 return BUFFER_REF(curbuf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003766}
3767
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003768/*
3769 * return MzScheme wrapper for curwin
3770 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003771 static vim_mz_window *
3772get_vim_curr_window(void)
3773{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003774 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003775 return (vim_mz_window *)window_new(curwin);
3776 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003777 return WINDOW_REF(curwin);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003778}
3779
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003780 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003781make_modules(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003782{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003783 int i;
3784 Scheme_Env *mod = NULL;
3785 Scheme_Object *vimext_symbol = NULL;
3786 Scheme_Object *closed_prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003787
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003788 MZ_GC_DECL_REG(3);
3789 MZ_GC_VAR_IN_REG(0, mod);
3790 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3791 MZ_GC_VAR_IN_REG(2, closed_prim);
3792 MZ_GC_REG();
3793
3794 vimext_symbol = scheme_intern_symbol("vimext");
3795 MZ_GC_CHECK();
3796 mod = scheme_primitive_module(vimext_symbol, environment);
3797 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003798 // all prims made closed so they can access their own names
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003799 for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003800 {
3801 Vim_Prim *prim = prims + i;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003802 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3803 prim->mina, prim->maxa);
3804 scheme_add_global(prim->name, closed_prim, mod);
3805 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003806 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003807 scheme_finish_primitive_module(mod);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003808 MZ_GC_CHECK();
3809 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003810}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003811
Bram Moolenaar555b2802005-05-19 21:08:39 +00003812#ifdef HAVE_SANDBOX
3813static Scheme_Object *M_write = NULL;
3814static Scheme_Object *M_read = NULL;
3815static Scheme_Object *M_execute = NULL;
3816static Scheme_Object *M_delete = NULL;
3817
3818 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003819sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003820{
3821 if (sandbox)
3822 raise_vim_exn(_("not allowed in the Vim sandbox"));
3823}
3824
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003825/*
3826 * security guards to force Vim's sandbox restrictions on MzScheme level
3827 */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003828 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003829sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003830{
3831 if (sandbox)
3832 {
3833 Scheme_Object *requested_access = argv[2];
3834
3835 if (M_write == NULL)
3836 {
3837 MZ_REGISTER_STATIC(M_write);
3838 M_write = scheme_intern_symbol("write");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003839 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003840 }
3841 if (M_read == NULL)
3842 {
3843 MZ_REGISTER_STATIC(M_read);
3844 M_read = scheme_intern_symbol("read");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003845 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003846 }
3847 if (M_execute == NULL)
3848 {
3849 MZ_REGISTER_STATIC(M_execute);
3850 M_execute = scheme_intern_symbol("execute");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003851 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003852 }
3853 if (M_delete == NULL)
3854 {
3855 MZ_REGISTER_STATIC(M_delete);
3856 M_delete = scheme_intern_symbol("delete");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003857 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003858 }
3859
3860 while (!SCHEME_NULLP(requested_access))
3861 {
3862 Scheme_Object *item = SCHEME_CAR(requested_access);
3863 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3864 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
Bram Moolenaar555b2802005-05-19 21:08:39 +00003865 raise_vim_exn(_("not allowed in the Vim sandbox"));
Bram Moolenaar555b2802005-05-19 21:08:39 +00003866 requested_access = SCHEME_CDR(requested_access);
3867 }
3868 }
3869 return scheme_void;
3870}
3871
3872 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003873sandbox_network_guard(int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003874{
3875 return scheme_void;
3876}
3877#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00003878
3879#endif