blob: 4a8644c50f3292d7e7398a401f72398763f5f1f8 [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
Martin Tournoij7904fa42022-10-04 16:28:45 +010020 * to build with 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 Moolenaar952d9d82021-08-02 18:07:18 +0200142static void do_printf(char *format, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000143static 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
Ken Takataf0ce1762024-07-27 13:25:34 +0200386// pointers are GetProcAddress'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 Moolenaar460ae5d2022-01-01 14:19:49 +0000671 semsg(_(e_could_not_load_library_str_str), gc_dll, GetWin32Error());
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 Moolenaar460ae5d2022-01-01 14:19:49 +0000678 semsg(_(e_could_not_load_library_str_str), sch_dll, GetWin32Error());
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 Moolenaar460ae5d2022-01-01 14:19:49 +0000692 semsg(_(e_could_not_load_library_function_str), 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 Moolenaar460ae5d2022-01-01 14:19:49 +0000706 semsg(_(e_could_not_load_library_function_str), 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;
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100812#elif defined(FEAT_GUI_MOTIF)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000813static 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 Moolenaar0b962e52022-04-03 18:02:37 +0100848# elif defined(FEAT_GUI_MOTIF)
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 Moolenaar0b962e52022-04-03 18:02:37 +0100856# elif defined(FEAT_GUI_MOTIF)
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 Moolenaar0b962e52022-04-03 18:02:37 +0100871# elif defined(FEAT_GUI_MOTIF)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000872 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 Moolenaar0b962e52022-04-03 18:02:37 +0100883# elif defined(FEAT_GUI_MOTIF)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000884 XtRemoveTimeOut(timer_id);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000885# endif
886 timer_id = 0;
887}
888
K.Takata4ee083e2023-02-22 13:14:36 +0000889#endif // MZSCHEME_GUI_THREADS
890
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000891 char *
K.Takata4ee083e2023-02-22 13:14:36 +0000892did_set_mzquantum(optset_T *args UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000893{
K.Takata4ee083e2023-02-22 13:14:36 +0000894#if defined(MZSCHEME_GUI_THREADS)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000895 // reset timer
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000896 if (timer_id != 0)
897 remove_timer();
898 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
899 setup_timer();
K.Takata4ee083e2023-02-22 13:14:36 +0000900#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000901 return NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000902}
903
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000904 static void
905notify_multithread(int on)
906{
907 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000908#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000909 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
910 setup_timer();
911 if (!on && timer_id != 0)
912 remove_timer();
913#endif
914}
915
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000916 void
917mzscheme_end(void)
918{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100919 // We can not unload the DLL. Racket's thread might be still alive.
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100920#if 0
Bram Moolenaar33570922005-01-25 22:26:29 +0000921#ifdef DYNAMIC_MZSCHEME
922 dynamic_mzscheme_end();
923#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100924#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000925}
926
Ken Takataf0ce1762024-07-27 13:25:34 +0200927#if HAVE_TLS_SPACE && !defined(VIMDLL)
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100928# if defined(_MSC_VER)
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100929static __declspec(thread) void *tls_space;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100930extern intptr_t _tls_index;
931# elif defined(__MINGW32__)
932static __thread void *tls_space;
933extern intptr_t _tls_index;
934# else
935static THREAD_LOCAL void *tls_space;
936static intptr_t _tls_index = 0;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100937# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100938#endif
939
Bram Moolenaar54b63522016-08-26 12:55:09 +0200940/*
941 * mzscheme_main() is called early in main().
942 * We may call scheme_main_setup() which calls mzscheme_env_main() which then
943 * trampolines into vim_main2(), which never returns.
944 */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100945 int
Bram Moolenaar54b63522016-08-26 12:55:09 +0200946mzscheme_main(void)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000947{
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200948 int argc = 0;
949 char *argv = NULL;
950
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100951#ifdef DYNAMIC_MZSCHEME
952 /*
953 * Racket requires trampolined startup. We can not load it later.
954 * If dynamic dll loading is failed, disable it.
955 */
956 if (!mzscheme_enabled(FALSE))
957 {
958 disabled = TRUE;
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200959 return vim_main2();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100960 }
961#endif
Bram Moolenaar5c5c9802015-07-10 16:12:48 +0200962#ifdef HAVE_TLS_SPACE
Ken Takataf0ce1762024-07-27 13:25:34 +0200963# ifdef VIMDLL
964 void **ptls_space;
965 intptr_t tls_index;
966 void (*pget_tls_info)(void ***ptls_space, intptr_t *ptls_index);
967
968 // Get the address of get_tls_info() from (g)vim.exe.
969 pget_tls_info = (void *)GetProcAddress(
970 GetModuleHandle(NULL), "get_tls_info");
971 if (pget_tls_info == NULL)
972 {
973 disabled = TRUE;
974 return vim_main2();
975 }
976 pget_tls_info(&ptls_space, &tls_index);
977 scheme_register_tls_space(ptls_space, tls_index);
978# else
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100979 scheme_register_tls_space(&tls_space, _tls_index);
Ken Takataf0ce1762024-07-27 13:25:34 +0200980# endif
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100981#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100982#ifdef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200983 return scheme_main_setup(TRUE, mzscheme_env_main, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000984#else
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200985 return mzscheme_env_main(NULL, argc, &argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000986#endif
987}
988
989 static int
Bram Moolenaar54b63522016-08-26 12:55:09 +0200990mzscheme_env_main(Scheme_Env *env, int argc UNUSED, char **argv UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000991{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100992#ifdef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100993 // Scheme has created the environment for us
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100994 environment = env;
995#else
996# ifdef MZ_PRECISE_GC
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000997 Scheme_Object *dummy = NULL;
998 MZ_GC_DECL_REG(1);
999 MZ_GC_VAR_IN_REG(0, dummy);
1000
1001 stack_base = &__gc_var_stack__;
1002# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001003 int dummy = 0;
1004 stack_base = (void *)&dummy;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001005# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001006#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001007
Bram Moolenaar54b63522016-08-26 12:55:09 +02001008 vim_main2();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001009 // not reached, vim_main2() will loop until exit()
Bram Moolenaar54b63522016-08-26 12:55:09 +02001010
1011 return 0;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001012}
1013
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001014 static Scheme_Object*
1015load_base_module(void *data)
1016{
1017 scheme_namespace_require(scheme_intern_symbol((char *)data));
1018 return scheme_null;
1019}
1020
1021 static Scheme_Object *
Bram Moolenaar8b29aba2016-03-28 22:17:16 +02001022load_base_module_on_error(void *data UNUSED)
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001023{
1024 load_base_module_failed = TRUE;
1025 return scheme_null;
1026}
1027
1028 static int
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001029startup_mzscheme(void)
1030{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001031#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001032 scheme_set_stack_base(stack_base, 1);
1033#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001034
Bram Moolenaar75676462013-01-30 14:55:42 +01001035#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001036 // in newer versions of precise GC the initial env has been created
Bram Moolenaar75676462013-01-30 14:55:42 +01001037 environment = scheme_basic_env();
1038#endif
1039
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001040 MZ_REGISTER_STATIC(environment);
1041 MZ_REGISTER_STATIC(curout);
1042 MZ_REGISTER_STATIC(curerr);
1043 MZ_REGISTER_STATIC(exn_catching_apply);
1044 MZ_REGISTER_STATIC(exn_p);
1045 MZ_REGISTER_STATIC(exn_message);
1046 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001047
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001048 MZ_GC_CHECK();
1049
1050#ifdef INCLUDE_MZSCHEME_BASE
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001051 // invoke function from generated and included mzscheme_base.c
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001052 declare_modules(environment);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001053#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001054
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001055 // setup 'current-library-collection-paths' parameter
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001056# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001057 {
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001058 Scheme_Object *coll_path = NULL;
1059 int mustfree = FALSE;
1060 char_u *s;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001061
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001062 MZ_GC_DECL_REG(1);
1063 MZ_GC_VAR_IN_REG(0, coll_path);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001064 MZ_GC_REG();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001065 // workaround for dynamic loading on windows
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001066 s = vim_getenv((char_u *)"PLTCOLLECTS", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001067 if (s != NULL)
1068 {
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001069 coll_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001070 MZ_GC_CHECK();
1071 if (mustfree)
1072 vim_free(s);
1073 }
1074# ifdef MZSCHEME_COLLECTS
1075 if (coll_path == NULL)
1076 {
1077 coll_path = scheme_make_path(MZSCHEME_COLLECTS);
1078 MZ_GC_CHECK();
1079 }
Bram Moolenaar39d7d512013-01-31 21:09:15 +01001080# endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001081 if (coll_path != NULL)
1082 {
1083 scheme_set_collects_path(coll_path);
1084 MZ_GC_CHECK();
1085 }
1086 MZ_GC_UNREG();
1087 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001088# else
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001089# ifdef MZSCHEME_COLLECTS
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001090 {
1091 Scheme_Object *coll_string = NULL;
1092 Scheme_Object *coll_pair = NULL;
1093 Scheme_Config *config = NULL;
1094
1095 MZ_GC_DECL_REG(3);
1096 MZ_GC_VAR_IN_REG(0, coll_string);
1097 MZ_GC_VAR_IN_REG(1, coll_pair);
1098 MZ_GC_VAR_IN_REG(2, config);
1099 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001100 coll_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001101 MZ_GC_CHECK();
1102 coll_pair = scheme_make_pair(coll_string, scheme_null);
1103 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001104 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001105 MZ_GC_CHECK();
1106 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
1107 MZ_GC_CHECK();
1108 MZ_GC_UNREG();
1109 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001110# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001111#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001112
1113# if MZSCHEME_VERSION_MAJOR >= 600
1114 {
1115 Scheme_Object *config_path = NULL;
1116 int mustfree = FALSE;
1117 char_u *s;
1118
1119 MZ_GC_DECL_REG(1);
1120 MZ_GC_VAR_IN_REG(0, config_path);
1121 MZ_GC_REG();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001122 // workaround for dynamic loading on windows
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001123 s = vim_getenv((char_u *)"PLTCONFIGDIR", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001124 if (s != NULL)
1125 {
Bram Moolenaar5b7d1772016-06-13 19:54:22 +02001126 config_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001127 MZ_GC_CHECK();
1128 if (mustfree)
1129 vim_free(s);
1130 }
1131#ifdef MZSCHEME_CONFIGDIR
1132 if (config_path == NULL)
1133 {
1134 config_path = scheme_make_path(MZSCHEME_CONFIGDIR);
1135 MZ_GC_CHECK();
1136 }
1137#endif
1138 if (config_path != NULL)
1139 {
1140 scheme_set_config_path(config_path);
1141 MZ_GC_CHECK();
1142 }
1143 MZ_GC_UNREG();
1144 }
1145# endif
1146
1147#if MZSCHEME_VERSION_MAJOR >= 400
1148 scheme_init_collection_paths(environment, scheme_null);
1149#endif
1150
1151 /*
1152 * versions 4.x do not provide Scheme bindings by default
1153 * we need to add them explicitly
1154 */
1155 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001156 // use error handler to avoid abort
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001157 scheme_dynamic_wind(NULL, load_base_module, NULL,
1158 load_base_module_on_error, "racket/base");
1159 if (load_base_module_failed)
1160 {
1161 load_base_module_failed = FALSE;
1162 scheme_dynamic_wind(NULL, load_base_module, NULL,
1163 load_base_module_on_error, "scheme/base");
1164 if (load_base_module_failed)
1165 return -1;
1166 }
1167 }
1168
1169 register_vim_exn();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001170 // use new environment to initialise exception handling
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001171 init_exn_catching_apply();
1172
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001173 // redirect output
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001174 scheme_console_output = do_output;
1175 scheme_console_printf = do_printf;
1176
Bram Moolenaar555b2802005-05-19 21:08:39 +00001177#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +00001178 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001179 Scheme_Object *make_security_guard = NULL;
1180 MZ_GC_DECL_REG(1);
1181 MZ_GC_VAR_IN_REG(0, make_security_guard);
1182 MZ_GC_REG();
1183
1184#if MZSCHEME_VERSION_MAJOR < 400
1185 {
1186 Scheme_Object *make_security_guard_symbol = NULL;
1187 MZ_GC_DECL_REG(1);
1188 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
1189 MZ_GC_REG();
1190 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
1191 MZ_GC_CHECK();
1192 make_security_guard = scheme_lookup_global(
1193 make_security_guard_symbol, environment);
1194 MZ_GC_UNREG();
1195 }
1196#else
1197 make_security_guard = scheme_builtin_value("make-security-guard");
1198 MZ_GC_CHECK();
1199#endif
1200
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001201 // setup sandbox guards
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001202 if (make_security_guard != NULL)
1203 {
1204 Scheme_Object *args[3] = {NULL, NULL, NULL};
1205 Scheme_Object *guard = NULL;
1206 Scheme_Config *config = NULL;
1207 MZ_GC_DECL_REG(5);
1208 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
1209 MZ_GC_VAR_IN_REG(3, guard);
1210 MZ_GC_VAR_IN_REG(4, config);
1211 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001212 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001213 MZ_GC_CHECK();
1214 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
1215 MZ_GC_CHECK();
1216 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
1217 "sandbox-file-guard", 3, 3);
1218 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1219 "sandbox-network-guard", 4, 4);
1220 guard = scheme_apply(make_security_guard, 3, args);
1221 MZ_GC_CHECK();
1222 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1223 MZ_GC_CHECK();
1224 MZ_GC_UNREG();
1225 }
1226 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001227 }
1228#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001229 // Create buffer and window types for use in Scheme code
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001230 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001231 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001232 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001233 MZ_GC_CHECK();
1234#ifdef MZ_PRECISE_GC
1235 GC_register_traversers(mz_buffer_type,
1236 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1237 TRUE, TRUE);
1238 GC_register_traversers(mz_window_type,
1239 window_size_proc, window_mark_proc, window_fixup_proc,
1240 TRUE, TRUE);
1241#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001242
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001243 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001244
1245 /*
1246 * setup callback to receive notifications
1247 * whether thread scheduling is (or not) required
1248 */
1249 scheme_notify_multithread = notify_multithread;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001250
1251 return 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001252}
1253
1254/*
1255 * This routine is called for each new invocation of MzScheme
1256 * to make sure things are properly initialized.
1257 */
1258 static int
1259mzscheme_init(void)
1260{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001261 if (!initialized)
1262 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001263#ifdef DYNAMIC_MZSCHEME
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001264 if (disabled || !mzscheme_enabled(TRUE))
Bram Moolenaar33570922005-01-25 22:26:29 +00001265 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001266 emsg(_(e_sorry_this_command_is_disabled_the_mzscheme_libraries_could_not_be_loaded));
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 return -1;
1268 }
1269#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001270 if (load_base_module_failed || startup_mzscheme())
1271 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001272 emsg(_(e_sorry_this_command_is_disabled_the_mzscheme_racket_base_module_could_not_be_loaded));
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001273 return -1;
1274 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001275 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001276 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001277 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001278 Scheme_Config *config = NULL;
1279 MZ_GC_DECL_REG(1);
1280 MZ_GC_VAR_IN_REG(0, config);
1281 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001282 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001283 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001284 // recreate ports each call effectively clearing these ones
Bram Moolenaar75676462013-01-30 14:55:42 +01001285 curout = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001286 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001287 curerr = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001288 MZ_GC_CHECK();
1289 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1290 MZ_GC_CHECK();
1291 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1292 MZ_GC_CHECK();
1293 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001294 }
1295
1296 return 0;
1297}
1298
1299/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001300 *========================================================================
1301 * 2. External Interface
1302 *========================================================================
1303 */
1304
1305/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001306 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001307 */
1308 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001309eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001310{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001311 Scheme_Object *value = NULL;
1312 Scheme_Object *exn = NULL;
1313 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001314
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001315 MZ_GC_DECL_REG(3);
1316 MZ_GC_VAR_IN_REG(0, value);
1317 MZ_GC_VAR_IN_REG(1, exn);
1318 MZ_GC_VAR_IN_REG(2, prim);
1319 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001320
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001321 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1322 MZ_GC_CHECK();
1323 value = _apply_thunk_catch_exceptions(prim, &exn);
1324 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001325
1326 if (!value)
1327 {
1328 value = extract_exn_message(exn);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001329 // Got an exn?
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001330 if (value)
1331 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001332 scheme_display(value, curerr); // Send to stderr-vim
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001333 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001334 do_flush();
1335 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001336 MZ_GC_UNREG();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001337 // `raise' was called on some arbitrary value
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001338 return FAIL;
1339 }
1340
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001341 if (ret != NULL) // if pointer to retval supported give it up
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001342 *ret = value;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001343 // Print any result, as long as it's not a void
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001344 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001345 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001346 scheme_display(value, curout); // Send to stdout-vim
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001347 MZ_GC_CHECK();
1348 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001349
1350 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001351 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001352 return OK;
1353}
1354
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001355/*
1356 * :mzscheme
1357 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001358 static int
1359do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1360{
1361 if (mzscheme_init())
1362 return FAIL;
1363
1364 range_start = eap->line1;
1365 range_end = eap->line2;
1366
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001367 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001368}
1369
1370/*
1371 * Routine called by VIM when deleting a buffer
1372 */
1373 void
1374mzscheme_buffer_free(buf_T *buf)
1375{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001376 if (buf->b_mzscheme_ref == NULL)
1377 return;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001378
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001379 vim_mz_buffer *bp = NULL;
1380 MZ_GC_DECL_REG(1);
1381 MZ_GC_VAR_IN_REG(0, bp);
1382 MZ_GC_REG();
1383
1384 bp = BUFFER_REF(buf);
1385 bp->buf = INVALID_BUFFER_VALUE;
Ken Takatac97b3fe2023-10-11 21:27:06 +02001386#ifdef MZ_PRECISE_GC
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001387 scheme_free_immobile_box(buf->b_mzscheme_ref);
Ken Takatac97b3fe2023-10-11 21:27:06 +02001388#else
1389 scheme_gc_ptr_ok(bp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001390#endif
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001391 buf->b_mzscheme_ref = NULL;
1392 MZ_GC_CHECK();
1393 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001394}
1395
1396/*
1397 * Routine called by VIM when deleting a Window
1398 */
1399 void
1400mzscheme_window_free(win_T *win)
1401{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001402 if (win->w_mzscheme_ref == NULL)
1403 return;
1404
1405 vim_mz_window *wp = NULL;
1406 MZ_GC_DECL_REG(1);
1407 MZ_GC_VAR_IN_REG(0, wp);
1408 MZ_GC_REG();
1409 wp = WINDOW_REF(win);
1410 wp->win = INVALID_WINDOW_VALUE;
Ken Takatac97b3fe2023-10-11 21:27:06 +02001411#ifdef MZ_PRECISE_GC
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001412 scheme_free_immobile_box(win->w_mzscheme_ref);
Ken Takatac97b3fe2023-10-11 21:27:06 +02001413#else
1414 scheme_gc_ptr_ok(wp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001415#endif
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001416 win->w_mzscheme_ref = NULL;
1417 MZ_GC_CHECK();
1418 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001419}
1420
1421/*
1422 * ":mzscheme" (or ":mz")
1423 */
1424 void
1425ex_mzscheme(exarg_T *eap)
1426{
1427 char_u *script;
1428
1429 script = script_get(eap, eap->arg);
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001430 if (eap->skip)
1431 return;
1432
1433 if (script == NULL)
1434 do_mzscheme_command(eap, eap->arg, do_eval);
1435 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001436 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001437 do_mzscheme_command(eap, script, do_eval);
1438 vim_free(script);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001439 }
1440}
1441
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001442 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001443do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001444{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001445 Scheme_Object *expr = NULL;
1446 Scheme_Object *result = NULL;
1447 char *file = NULL;
1448 Port_Info *pinfo = (Port_Info *)data;
1449
1450 MZ_GC_DECL_REG(3);
1451 MZ_GC_VAR_IN_REG(0, expr);
1452 MZ_GC_VAR_IN_REG(1, result);
1453 MZ_GC_VAR_IN_REG(2, file);
1454 MZ_GC_REG();
1455
1456 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1457 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001458
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001459 // make Vim expansion
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001460 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001461 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001462 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001463 scheme_count_lines(pinfo->port); // to get accurate read error location
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001464 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001465
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001466 // Like REPL but print only last result
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001467 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001468 {
1469 result = scheme_eval(expr, environment);
1470 MZ_GC_CHECK();
1471 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001472
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001473 // errors will be caught in do_mzscheme_command and ex_mzfile
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001474 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001475 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001476 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001477 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001478 return result;
1479}
1480
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001481/*
1482 * :mzfile
1483 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001484 void
1485ex_mzfile(exarg_T *eap)
1486{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001487 Port_Info pinfo = {NULL, NULL};
1488
1489 MZ_GC_DECL_REG(1);
1490 MZ_GC_VAR_IN_REG(0, pinfo.port);
1491 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001492
1493 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001494 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001495 && pinfo.port != NULL) // looks like port was not closed
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001496 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001497 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001498 MZ_GC_CHECK();
1499 }
1500 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001501}
1502
1503
1504/*
1505 *========================================================================
1506 * Exception handling code -- cribbed form the MzScheme sources and
1507 * Matthew Flatt's "Inside PLT MzScheme" document.
1508 *========================================================================
1509 */
1510 static void
1511init_exn_catching_apply(void)
1512{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001513 if (exn_catching_apply)
1514 return;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001515
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001516 char *e =
1517 "(lambda (thunk) "
1518 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
1519 "(cons #t (thunk))))";
1520
1521 exn_catching_apply = scheme_eval_string(e, environment);
1522 MZ_GC_CHECK();
1523 exn_p = scheme_builtin_value("exn?");
1524 MZ_GC_CHECK();
1525 exn_message = scheme_builtin_value("exn-message");
1526 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001527}
1528
1529/*
1530 * This function applies a thunk, returning the Scheme value if there's
1531 * no exception, otherwise returning NULL and setting *exn to the raised
1532 * value (usually an exn structure).
1533 */
1534 static Scheme_Object *
1535_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1536{
1537 Scheme_Object *v;
1538
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001539 v = _scheme_apply(exn_catching_apply, 1, &f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001540 // v is a pair: (cons #t value) or (cons #f exn)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001541
1542 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1543 return SCHEME_CDR(v);
1544 else
1545 {
1546 *exn = SCHEME_CDR(v);
1547 return NULL;
1548 }
1549}
1550
1551 static Scheme_Object *
1552extract_exn_message(Scheme_Object *v)
1553{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001554 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1555 return _scheme_apply(exn_message, 1, &v);
1556 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001557 return NULL; // Not an exn structure
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001558}
1559
1560 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001561do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001562{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001563 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001564}
1565
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001566/*
1567 *========================================================================
1568 * 3. MzScheme I/O Handlers
1569 *========================================================================
1570 */
1571 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001572do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001573{
1574 char *p, *prev;
1575
1576 prev = mesg;
1577 p = strchr(prev, '\n');
1578 while (p)
1579 {
1580 *p = '\0';
1581 if (error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001582 emsg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001583 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001584 msg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001585 prev = p + 1;
1586 p = strchr(prev, '\n');
1587 }
1588
1589 if (error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001590 emsg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001591 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001592 msg(prev);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001593}
1594
1595 static void
Bram Moolenaar75676462013-01-30 14:55:42 +01001596do_output(char *mesg, OUTPUT_LEN_TYPE len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001597{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001598 // TODO: use len, the string may not be NUL terminated
Bram Moolenaar64404472010-06-26 06:24:45 +02001599 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001600}
1601
1602 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001603do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001604{
Bram Moolenaar64404472010-06-26 06:24:45 +02001605 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001606}
1607
1608 static void
1609do_printf(char *format, ...)
1610{
Bram Moolenaar64404472010-06-26 06:24:45 +02001611 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001612}
1613
1614 static void
1615do_flush(void)
1616{
1617 char *buff;
Bram Moolenaar75676462013-01-30 14:55:42 +01001618 OUTPUT_LEN_TYPE length;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001619
Bram Moolenaar75676462013-01-30 14:55:42 +01001620 buff = scheme_get_sized_byte_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001621 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001622 if (length)
1623 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001624 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001625 return;
1626 }
1627
Bram Moolenaar75676462013-01-30 14:55:42 +01001628 buff = scheme_get_sized_byte_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001629 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001630 if (length)
1631 do_output(buff, length);
1632}
1633
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001634/*
1635 *========================================================================
1636 * 4. Implementation of the Vim Features for MzScheme
1637 *========================================================================
1638 */
1639
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001640/*
1641 * (command {command-string})
1642 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001643 static Scheme_Object *
1644vim_command(void *data, int argc, Scheme_Object **argv)
1645{
1646 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001647 Scheme_Object *cmd = NULL;
1648 MZ_GC_DECL_REG(1);
1649 MZ_GC_VAR_IN_REG(0, cmd);
1650 MZ_GC_REG();
1651 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001652
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001653 // may be use do_cmdline_cmd?
Bram Moolenaar75676462013-01-30 14:55:42 +01001654 do_cmdline(BYTE_STRING_VALUE(cmd), NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001655 update_screen(UPD_VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001656
Bram Moolenaar75676462013-01-30 14:55:42 +01001657 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001658 raise_if_error();
1659 return scheme_void;
1660}
1661
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001662/*
1663 * (eval {expr-string})
1664 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001665 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001666vim_eval(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001667{
1668#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001669 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001670 Scheme_Object *result = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001671 typval_T *vim_result;
Bram Moolenaar75676462013-01-30 14:55:42 +01001672 Scheme_Object *expr = NULL;
1673 MZ_GC_DECL_REG(2);
1674 MZ_GC_VAR_IN_REG(0, result);
1675 MZ_GC_VAR_IN_REG(1, expr);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001676 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001677 expr = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001678
Bram Moolenaar75676462013-01-30 14:55:42 +01001679 vim_result = eval_expr(BYTE_STRING_VALUE(expr), NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001680
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001681 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001682 raise_vim_exn(_("invalid expression"));
1683
Bram Moolenaar75676462013-01-30 14:55:42 +01001684 result = vim_to_mzscheme(vim_result);
1685 MZ_GC_CHECK();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001686 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001687
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001688 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001689 return result;
1690#else
1691 raise_vim_exn(_("expressions disabled at compile time"));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001692 // unreachable
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001693 return scheme_false;
1694#endif
1695}
1696
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001697/*
1698 * (range-start)
1699 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001700 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001701get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001702{
1703 return scheme_make_integer(range_start);
1704}
1705
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001706/*
1707 * (range-end)
1708 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001709 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001710get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001711{
1712 return scheme_make_integer(range_end);
1713}
1714
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001715/*
1716 * (beep)
1717 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001718 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001719mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001720{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001721 vim_beep(BO_LANG);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001722 return scheme_void;
1723}
1724
1725static Scheme_Object *M_global = NULL;
1726
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001727/*
1728 * (get-option {option-name}) [buffer/window]
1729 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001730 static Scheme_Object *
1731get_option(void *data, int argc, Scheme_Object **argv)
1732{
1733 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001734 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001735 char *strval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001736 getoption_T rc;
Bram Moolenaar75676462013-01-30 14:55:42 +01001737 Scheme_Object *rval = NULL;
1738 Scheme_Object *name = NULL;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001739 int scope = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001740 buf_T *save_curb = curbuf;
1741 win_T *save_curw = curwin;
1742
Bram Moolenaar75676462013-01-30 14:55:42 +01001743 MZ_GC_DECL_REG(2);
1744 MZ_GC_VAR_IN_REG(0, rval);
1745 MZ_GC_VAR_IN_REG(1, name);
1746 MZ_GC_REG();
1747
1748 name = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001749
1750 if (argc > 1)
1751 {
1752 if (M_global == NULL)
1753 {
1754 MZ_REGISTER_STATIC(M_global);
1755 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001756 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001757 }
1758
1759 if (argv[1] == M_global)
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001760 scope = OPT_GLOBAL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001761 else if (SCHEME_VIMBUFFERP(argv[1]))
1762 {
1763 curbuf = get_valid_buffer(argv[1]);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001764 scope = OPT_LOCAL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001765 }
1766 else if (SCHEME_VIMWINDOWP(argv[1]))
1767 {
1768 win_T *win = get_valid_window(argv[1]);
1769
1770 curwin = win;
1771 curbuf = win->w_buffer;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001772 scope = OPT_LOCAL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001773 }
1774 else
1775 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1776 }
1777
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001778 rc = get_option_value(BYTE_STRING_VALUE(name), &value, (char_u **)&strval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001779 NULL, scope);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001780 curbuf = save_curb;
1781 curwin = save_curw;
1782
1783 switch (rc)
1784 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001785 case gov_bool:
1786 case gov_number:
Bram Moolenaar75676462013-01-30 14:55:42 +01001787 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001788 return scheme_make_integer_value(value);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001789 case gov_string:
Bram Moolenaar75676462013-01-30 14:55:42 +01001790 rval = scheme_make_byte_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001791 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001792 vim_free(strval);
Bram Moolenaar75676462013-01-30 14:55:42 +01001793 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001794 return rval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001795 case gov_hidden_bool:
1796 case gov_hidden_number:
1797 case gov_hidden_string:
Bram Moolenaar75676462013-01-30 14:55:42 +01001798 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001799 raise_vim_exn(_("hidden option"));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001800 //NOTREACHED
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001801 case gov_unknown:
Bram Moolenaar75676462013-01-30 14:55:42 +01001802 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001803 raise_vim_exn(_("unknown option"));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001804 //NOTREACHED
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001805 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001806 // unreachable
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001807 return scheme_void;
1808}
1809
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001810/*
1811 * (set-option {option-changing-string} [buffer/window])
1812 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001813 static Scheme_Object *
1814set_option(void *data, int argc, Scheme_Object **argv)
1815{
Bram Moolenaar75676462013-01-30 14:55:42 +01001816 char_u *command = NULL;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001817 int scope = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001818 buf_T *save_curb = curbuf;
1819 win_T *save_curw = curwin;
1820 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001821 Scheme_Object *cmd = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001822
Bram Moolenaar75676462013-01-30 14:55:42 +01001823 MZ_GC_DECL_REG(1);
1824 MZ_GC_VAR_IN_REG(0, cmd);
1825 MZ_GC_REG();
1826 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
1827
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001828 if (argc > 1)
1829 {
1830 if (M_global == NULL)
1831 {
1832 MZ_REGISTER_STATIC(M_global);
1833 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001834 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001835 }
1836
1837 if (argv[1] == M_global)
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001838 scope = OPT_GLOBAL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001839 else if (SCHEME_VIMBUFFERP(argv[1]))
1840 {
1841 curbuf = get_valid_buffer(argv[1]);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001842 scope = OPT_LOCAL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001843 }
1844 else if (SCHEME_VIMWINDOWP(argv[1]))
1845 {
1846 win_T *win = get_valid_window(argv[1]);
1847 curwin = win;
1848 curbuf = win->w_buffer;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001849 scope = OPT_LOCAL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001850 }
1851 else
1852 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1853 }
1854
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001855 // do_set can modify cmd, make copy
Bram Moolenaar75676462013-01-30 14:55:42 +01001856 command = vim_strsave(BYTE_STRING_VALUE(cmd));
1857 MZ_GC_UNREG();
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001858 do_set(command, scope);
Bram Moolenaar75676462013-01-30 14:55:42 +01001859 vim_free(command);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001860 update_screen(UPD_NOT_VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001861 curbuf = save_curb;
1862 curwin = save_curw;
1863 raise_if_error();
1864 return scheme_void;
1865}
1866
1867/*
1868 *===========================================================================
1869 * 5. Vim Window-related Manipulation Functions
1870 *===========================================================================
1871 */
1872
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001873/*
1874 * (curr-win)
1875 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001876 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001877get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001878{
1879 return (Scheme_Object *)get_vim_curr_window();
1880}
1881
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001882/*
1883 * (win-count)
1884 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001885 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001886get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001887{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001888 int n = 0;
Bram Moolenaard2142212013-01-30 17:41:50 +01001889 win_T *w;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001890
Bram Moolenaar29323592016-07-24 22:04:11 +02001891 FOR_ALL_WINDOWS(w)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001892 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001893 return scheme_make_integer(n);
1894}
1895
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001896/*
1897 * (get-win-list [buffer])
1898 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001899 static Scheme_Object *
1900get_window_list(void *data, int argc, Scheme_Object **argv)
1901{
1902 Vim_Prim *prim = (Vim_Prim *)data;
1903 vim_mz_buffer *buf;
1904 Scheme_Object *list;
Bram Moolenaard2142212013-01-30 17:41:50 +01001905 win_T *w = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001906
1907 buf = get_buffer_arg(prim->name, 0, argc, argv);
1908 list = scheme_null;
1909
Bram Moolenaard2142212013-01-30 17:41:50 +01001910 for ( ; w != NULL; w = w->w_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001911 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001912 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001913 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001914 MZ_GC_CHECK();
1915 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001916
1917 return list;
1918}
1919
1920 static Scheme_Object *
1921window_new(win_T *win)
1922{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001923 vim_mz_window *self = NULL;
1924
1925 MZ_GC_DECL_REG(1);
1926 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001927
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001928 // We need to handle deletion of windows underneath us.
1929 // If we add a "w_mzscheme_ref" field to the win_T structure,
1930 // then we can get at it in win_free() in vim.
1931 //
1932 // On a win_free() we set the Scheme object's win_T *field
1933 // to an invalid value. We trap all uses of a window
1934 // object, and reject them if the win_T *field is invalid.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001935 if (win->w_mzscheme_ref != NULL)
Bram Moolenaar75676462013-01-30 14:55:42 +01001936 return (Scheme_Object *)WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001937
Bram Moolenaar75676462013-01-30 14:55:42 +01001938 MZ_GC_REG();
1939 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_window));
Bram Moolenaara80faa82020-04-12 19:37:17 +02001940 CLEAR_POINTER(self);
Ken Takatac97b3fe2023-10-11 21:27:06 +02001941#ifdef MZ_PRECISE_GC
Bram Moolenaar75676462013-01-30 14:55:42 +01001942 win->w_mzscheme_ref = scheme_malloc_immobile_box(NULL);
Ken Takatac97b3fe2023-10-11 21:27:06 +02001943#else
1944 scheme_dont_gc_ptr(self); // because win isn't visible to GC
Bram Moolenaar75676462013-01-30 14:55:42 +01001945#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001946 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001947 WINDOW_REF(win) = self;
1948 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001949 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001950 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001951
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001952 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001953 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001954}
1955
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001956/*
1957 * (get-win-num [window])
1958 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001959 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001960get_window_num(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001961{
Bram Moolenaard2142212013-01-30 17:41:50 +01001962 int nr = 1;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001963 Vim_Prim *prim = (Vim_Prim *)data;
1964 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001965 win_T *wp;
1966
1967 for (wp = firstwin; wp != win; wp = wp->w_next)
1968 ++nr;
1969
1970 return scheme_make_integer(nr);
1971}
1972
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001973/*
1974 * (get-win-by-num {windownum})
1975 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001976 static Scheme_Object *
1977get_window_by_num(void *data, int argc, Scheme_Object **argv)
1978{
1979 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaard2142212013-01-30 17:41:50 +01001980 win_T *win = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001981 int fnum;
1982
1983 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1984 if (fnum < 1)
1985 scheme_signal_error(_("window index is out of range"));
1986
Bram Moolenaard2142212013-01-30 17:41:50 +01001987 for ( ; win != NULL; win = win->w_next, --fnum)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001988 if (fnum == 1) // to be 1-based
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001989 return window_new(win);
1990
1991 return scheme_false;
1992}
1993
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001994/*
1995 * (get-win-buffer [window])
1996 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001997 static Scheme_Object *
1998get_window_buffer(void *data, int argc, Scheme_Object **argv)
1999{
2000 Vim_Prim *prim = (Vim_Prim *)data;
2001 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2002
2003 return buffer_new(win->win->w_buffer);
2004}
2005
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002006/*
2007 * (get-win-height [window])
2008 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002009 static Scheme_Object *
2010get_window_height(void *data, int argc, Scheme_Object **argv)
2011{
2012 Vim_Prim *prim = (Vim_Prim *)data;
2013 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2014
2015 return scheme_make_integer(win->win->w_height);
2016}
2017
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002018/*
2019 * (set-win-height {height} [window])
2020 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002021 static Scheme_Object *
2022set_window_height(void *data, int argc, Scheme_Object **argv)
2023{
2024 Vim_Prim *prim = (Vim_Prim *)data;
2025 vim_mz_window *win;
2026 win_T *savewin;
2027 int height;
2028
2029 win = get_window_arg(prim->name, 1, argc, argv);
2030 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2031
2032#ifdef FEAT_GUI
2033 need_mouse_correct = TRUE;
2034#endif
2035
2036 savewin = curwin;
2037 curwin = win->win;
2038 win_setheight(height);
2039 curwin = savewin;
2040
2041 raise_if_error();
2042 return scheme_void;
2043}
2044
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002045/*
2046 * (get-win-width [window])
2047 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002048 static Scheme_Object *
2049get_window_width(void *data, int argc, Scheme_Object **argv)
2050{
2051 Vim_Prim *prim = (Vim_Prim *)data;
2052 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2053
Bram Moolenaar02631462017-09-22 15:20:32 +02002054 return scheme_make_integer(win->win->w_width);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002055}
2056
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002057/*
2058 * (set-win-width {width} [window])
2059 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002060 static Scheme_Object *
2061set_window_width(void *data, int argc, Scheme_Object **argv)
2062{
2063 Vim_Prim *prim = (Vim_Prim *)data;
2064 vim_mz_window *win;
2065 win_T *savewin;
2066 int width = 0;
2067
2068 win = get_window_arg(prim->name, 1, argc, argv);
2069 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2070
2071# ifdef FEAT_GUI
2072 need_mouse_correct = TRUE;
2073# endif
2074
2075 savewin = curwin;
2076 curwin = win->win;
2077 win_setwidth(width);
2078 curwin = savewin;
2079
2080 raise_if_error();
2081 return scheme_void;
2082}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002083
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002084/*
2085 * (get-cursor [window]) -> (line . col)
2086 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002087 static Scheme_Object *
2088get_cursor(void *data, int argc, Scheme_Object **argv)
2089{
2090 Vim_Prim *prim = (Vim_Prim *)data;
2091 vim_mz_window *win;
2092 pos_T pos;
2093
2094 win = get_window_arg(prim->name, 0, argc, argv);
2095 pos = win->win->w_cursor;
2096 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
2097 scheme_make_integer_value((long)pos.col + 1));
2098}
2099
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002100/*
2101 * (set-cursor (line . col) [window])
2102 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002103 static Scheme_Object *
2104set_cursor(void *data, int argc, Scheme_Object **argv)
2105{
2106 Vim_Prim *prim = (Vim_Prim *)data;
2107 vim_mz_window *win;
2108 long lnum = 0;
2109 long col = 0;
2110
Bram Moolenaar555b2802005-05-19 21:08:39 +00002111#ifdef HAVE_SANDBOX
2112 sandbox_check();
2113#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002114 win = get_window_arg(prim->name, 1, argc, argv);
2115 GUARANTEE_PAIR(prim->name, 0);
2116
2117 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
2118 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
2119 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
2120
2121 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
2122 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
2123
2124 check_line_range(lnum, win->win->w_buffer);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002125 // don't know how to catch invalid column value
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002126
2127 win->win->w_cursor.lnum = lnum;
2128 win->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02002129 win->win->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002130 update_screen(UPD_VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002131
2132 raise_if_error();
2133 return scheme_void;
2134}
2135/*
2136 *===========================================================================
2137 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002138 *===========================================================================
2139 */
2140
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002141/*
2142 * (open-buff {filename})
2143 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002144 static Scheme_Object *
2145mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
2146{
2147 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002148 int num = 0;
Bram Moolenaar75676462013-01-30 14:55:42 +01002149 Scheme_Object *onum = NULL;
2150 Scheme_Object *buf = NULL;
2151 Scheme_Object *fname;
2152
2153 MZ_GC_DECL_REG(3);
2154 MZ_GC_VAR_IN_REG(0, onum);
2155 MZ_GC_VAR_IN_REG(1, buf);
2156 MZ_GC_VAR_IN_REG(2, fname);
2157 MZ_GC_REG();
2158 fname = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002159
Bram Moolenaar555b2802005-05-19 21:08:39 +00002160#ifdef HAVE_SANDBOX
2161 sandbox_check();
2162#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002163 // TODO make open existing file
Bram Moolenaar75676462013-01-30 14:55:42 +01002164 num = buflist_add(BYTE_STRING_VALUE(fname), BLN_LISTED | BLN_CURBUF);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002165
2166 if (num == 0)
2167 raise_vim_exn(_("couldn't open buffer"));
2168
2169 onum = scheme_make_integer(num);
Bram Moolenaar75676462013-01-30 14:55:42 +01002170 buf = get_buffer_by_num(data, 1, &onum);
2171 MZ_GC_UNREG();
2172 return buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002173}
2174
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002175/*
2176 * (get-buff-by-num {buffernum})
2177 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002178 static Scheme_Object *
2179get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
2180{
2181 Vim_Prim *prim = (Vim_Prim *)data;
2182 buf_T *buf;
2183 int fnum;
2184
2185 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2186
Bram Moolenaar29323592016-07-24 22:04:11 +02002187 FOR_ALL_BUFFERS(buf)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002188 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002189 return buffer_new(buf);
2190
2191 return scheme_false;
2192}
2193
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002194/*
2195 * (get-buff-by-name {buffername})
2196 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002197 static Scheme_Object *
2198get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
2199{
2200 Vim_Prim *prim = (Vim_Prim *)data;
2201 buf_T *buf;
Bram Moolenaar75676462013-01-30 14:55:42 +01002202 Scheme_Object *buffer = NULL;
2203 Scheme_Object *fname = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002204
Bram Moolenaar75676462013-01-30 14:55:42 +01002205 MZ_GC_DECL_REG(2);
2206 MZ_GC_VAR_IN_REG(0, buffer);
2207 MZ_GC_VAR_IN_REG(1, fname);
2208 MZ_GC_REG();
2209 fname = GUARANTEED_STRING_ARG(prim->name, 0);
2210 buffer = scheme_false;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002211
Bram Moolenaar29323592016-07-24 22:04:11 +02002212 FOR_ALL_BUFFERS(buf)
Bram Moolenaar75676462013-01-30 14:55:42 +01002213 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002214 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002215 // empty string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002216 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002217 if (BYTE_STRING_VALUE(fname)[0] == NUL)
2218 buffer = buffer_new(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002219 }
Bram Moolenaar75676462013-01-30 14:55:42 +01002220 else if (!fnamecmp(buf->b_ffname, BYTE_STRING_VALUE(fname))
2221 || !fnamecmp(buf->b_sfname, BYTE_STRING_VALUE(fname)))
2222 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002223 // either short or long filename matches
Bram Moolenaar75676462013-01-30 14:55:42 +01002224 buffer = buffer_new(buf);
2225 }
2226 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002227
Bram Moolenaar75676462013-01-30 14:55:42 +01002228 MZ_GC_UNREG();
2229 return buffer;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002230}
2231
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002232/*
2233 * (get-next-buff [buffer])
2234 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002235 static Scheme_Object *
2236get_next_buffer(void *data, int argc, Scheme_Object **argv)
2237{
2238 Vim_Prim *prim = (Vim_Prim *)data;
2239 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2240
2241 if (buf->b_next == NULL)
2242 return scheme_false;
2243 else
2244 return buffer_new(buf->b_next);
2245}
2246
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002247/*
2248 * (get-prev-buff [buffer])
2249 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002250 static Scheme_Object *
2251get_prev_buffer(void *data, int argc, Scheme_Object **argv)
2252{
2253 Vim_Prim *prim = (Vim_Prim *)data;
2254 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2255
2256 if (buf->b_prev == NULL)
2257 return scheme_false;
2258 else
2259 return buffer_new(buf->b_prev);
2260}
2261
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002262/*
2263 * (get-buff-num [buffer])
2264 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002265 static Scheme_Object *
2266get_buffer_num(void *data, int argc, Scheme_Object **argv)
2267{
2268 Vim_Prim *prim = (Vim_Prim *)data;
2269 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2270
2271 return scheme_make_integer(buf->buf->b_fnum);
2272}
2273
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002274/*
2275 * (buff-count)
2276 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002277 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002278get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002279{
2280 buf_T *b;
2281 int n = 0;
2282
Bram Moolenaar29323592016-07-24 22:04:11 +02002283 FOR_ALL_BUFFERS(b) ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002284 return scheme_make_integer(n);
2285}
2286
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002287/*
2288 * (get-buff-name [buffer])
2289 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002290 static Scheme_Object *
2291get_buffer_name(void *data, int argc, Scheme_Object **argv)
2292{
2293 Vim_Prim *prim = (Vim_Prim *)data;
2294 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2295
Bram Moolenaar75676462013-01-30 14:55:42 +01002296 return scheme_make_byte_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002297}
2298
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002299/*
2300 * (curr-buff)
2301 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002302 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002303get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002304{
2305 return (Scheme_Object *)get_vim_curr_buffer();
2306}
2307
2308 static Scheme_Object *
2309buffer_new(buf_T *buf)
2310{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002311 vim_mz_buffer *self = NULL;
2312
2313 MZ_GC_DECL_REG(1);
2314 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002315
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002316 // We need to handle deletion of buffers underneath us.
2317 // If we add a "b_mzscheme_ref" field to the buf_T structure,
2318 // then we can get at it in buf_freeall() in vim.
Bram Moolenaare344bea2005-09-01 20:46:49 +00002319 if (buf->b_mzscheme_ref)
Bram Moolenaar75676462013-01-30 14:55:42 +01002320 return (Scheme_Object *)BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002321
Bram Moolenaar75676462013-01-30 14:55:42 +01002322 MZ_GC_REG();
2323 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_buffer));
Bram Moolenaara80faa82020-04-12 19:37:17 +02002324 CLEAR_POINTER(self);
Ken Takatac97b3fe2023-10-11 21:27:06 +02002325#ifdef MZ_PRECISE_GC
Bram Moolenaar75676462013-01-30 14:55:42 +01002326 buf->b_mzscheme_ref = scheme_malloc_immobile_box(NULL);
Ken Takatac97b3fe2023-10-11 21:27:06 +02002327#else
2328 scheme_dont_gc_ptr(self); // because buf isn't visible to GC
Bram Moolenaar75676462013-01-30 14:55:42 +01002329#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002330 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01002331 BUFFER_REF(buf) = self;
2332 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002333 self->buf = buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002334 self->so.type = mz_buffer_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002335
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002336 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01002337 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002338}
2339
2340/*
2341 * (get-buff-size [buffer])
2342 *
2343 * Get the size (number of lines) in the current buffer.
2344 */
2345 static Scheme_Object *
2346get_buffer_size(void *data, int argc, Scheme_Object **argv)
2347{
2348 Vim_Prim *prim = (Vim_Prim *)data;
2349 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2350
2351 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
2352}
2353
2354/*
2355 * (get-buff-line {linenr} [buffer])
2356 *
2357 * Get a line from the specified buffer. The line number is
2358 * in Vim format (1-based). The line is returned as a MzScheme
2359 * string object.
2360 */
2361 static Scheme_Object *
2362get_buffer_line(void *data, int argc, Scheme_Object **argv)
2363{
2364 Vim_Prim *prim = (Vim_Prim *)data;
2365 vim_mz_buffer *buf;
2366 int linenr;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002367 char_u *line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002368
2369 buf = get_buffer_arg(prim->name, 1, argc, argv);
2370 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2371 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2372
2373 raise_if_error();
Bram Moolenaar75676462013-01-30 14:55:42 +01002374 return scheme_make_byte_string((char *)line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002375}
2376
2377
2378/*
2379 * (get-buff-line-list {start} {end} [buffer])
2380 *
2381 * Get a list of lines from the specified buffer. The line numbers
2382 * are in Vim format (1-based). The range is from lo up to, but not
2383 * including, hi. The list is returned as a list of string objects.
2384 */
2385 static Scheme_Object *
2386get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2387{
2388 Vim_Prim *prim = (Vim_Prim *)data;
2389 vim_mz_buffer *buf;
2390 int i, hi, lo, n;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002391 Scheme_Object *list = NULL;
2392
2393 MZ_GC_DECL_REG(1);
2394 MZ_GC_VAR_IN_REG(0, list);
2395 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002396
2397 buf = get_buffer_arg(prim->name, 2, argc, argv);
2398 list = scheme_null;
2399 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2400 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2401
2402 /*
2403 * Handle some error conditions
2404 */
2405 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002406 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002407
2408 if (hi < 0)
2409 hi = 0;
2410 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002411 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002412
2413 n = hi - lo;
2414
2415 for (i = n; i >= 0; --i)
2416 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002417 Scheme_Object *str = scheme_make_byte_string(
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002418 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2419 raise_if_error();
2420
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002421 // Set the list item
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002422 list = scheme_make_pair(str, list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002423 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002424 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002425 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002426 return list;
2427}
2428
2429/*
2430 * (set-buff-line {linenr} {string/#f} [buffer])
2431 *
2432 * Replace a line in the specified buffer. The line number is
2433 * in Vim format (1-based). The replacement line is given as
2434 * an MzScheme string object. The object is checked for validity
2435 * and correct format. An exception is thrown if the values are not
2436 * the correct format.
2437 *
2438 * It returns a Scheme Object that indicates the length of the
2439 * string changed.
2440 */
2441 static Scheme_Object *
2442set_buffer_line(void *data, int argc, Scheme_Object **argv)
2443{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002444 // First of all, we check the value of the supplied MzScheme object.
2445 // There are three cases:
2446 // 1. #f - this is a deletion.
2447 // 2. A string - this is a replacement.
2448 // 3. Anything else - this is an error.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002449 Vim_Prim *prim = (Vim_Prim *)data;
2450 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002451 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002452 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002453 int n;
2454
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002455 MZ_GC_DECL_REG(1);
2456 MZ_GC_VAR_IN_REG(0, line);
2457 MZ_GC_REG();
2458
Bram Moolenaar555b2802005-05-19 21:08:39 +00002459#ifdef HAVE_SANDBOX
2460 sandbox_check();
2461#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002462 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2463 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002464 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002465 line = argv[1];
2466 buf = get_buffer_arg(prim->name, 2, argc, argv);
2467
2468 check_line_range(n, buf->buf);
2469
2470 if (SCHEME_FALSEP(line))
2471 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002472 buf_T *savebuf = curbuf;
2473
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002474 curbuf = buf->buf;
2475
2476 if (u_savedel((linenr_T)n, 1L) == FAIL)
2477 {
2478 curbuf = savebuf;
2479 raise_vim_exn(_("cannot save undo information"));
2480 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02002481 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002482 {
2483 curbuf = savebuf;
2484 raise_vim_exn(_("cannot delete line"));
2485 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002486 if (buf->buf == curwin->w_buffer)
2487 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002488 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002489
2490 curbuf = savebuf;
2491
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002492 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002493 raise_if_error();
2494 return scheme_void;
2495 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002496 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002497 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002498 // Otherwise it's a line
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002499 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002500
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002501 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002502
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002503 curbuf = buf->buf;
2504
2505 if (u_savesub((linenr_T)n) == FAIL)
2506 {
2507 curbuf = savebuf;
2508 vim_free(save);
2509 raise_vim_exn(_("cannot save undo information"));
2510 }
2511 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2512 {
2513 curbuf = savebuf;
2514 vim_free(save);
2515 raise_vim_exn(_("cannot replace line"));
2516 }
2517 else
2518 {
2519 vim_free(save);
2520 changed_bytes((linenr_T)n, 0);
2521 }
2522
2523 curbuf = savebuf;
2524
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002525 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002526 if (buf->buf == curwin->w_buffer)
2527 check_cursor_col();
2528
2529 MZ_GC_UNREG();
2530 raise_if_error();
2531 return scheme_void;
2532 }
2533}
2534
2535 static void
2536free_array(char **array)
2537{
2538 char **curr = array;
2539 while (*curr != NULL)
2540 vim_free(*curr++);
2541 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002542}
2543
2544/*
2545 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2546 *
2547 * Replace a range of lines in the specified buffer. The line numbers are in
2548 * Vim format (1-based). The range is from lo up to, but not including, hi.
2549 * The replacement lines are given as a Scheme list of string objects. The
2550 * list is checked for validity and correct format.
2551 *
2552 * Errors are returned as a value of FAIL. The return value is OK on success.
2553 * If OK is returned and len_change is not NULL, *len_change is set to the
2554 * change in the buffer length.
2555 */
2556 static Scheme_Object *
2557set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2558{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002559 // First of all, we check the type of the supplied MzScheme object.
2560 // There are three cases:
2561 // 1. #f - this is a deletion.
2562 // 2. A list - this is a replacement.
2563 // 3. Anything else - this is an error.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002564 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002565 vim_mz_buffer *buf = NULL;
2566 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002567 int i, old_len, new_len, hi, lo;
2568 long extra;
2569
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002570 MZ_GC_DECL_REG(1);
2571 MZ_GC_VAR_IN_REG(0, line_list);
2572 MZ_GC_REG();
2573
Bram Moolenaar555b2802005-05-19 21:08:39 +00002574#ifdef HAVE_SANDBOX
2575 sandbox_check();
2576#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002577 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2578 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2579 if (!SCHEME_PAIRP(argv[2])
2580 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2581 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2582 line_list = argv[2];
2583 buf = get_buffer_arg(prim->name, 3, argc, argv);
2584 old_len = hi - lo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002585 if (old_len < 0) // process inverse values wisely
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002586 {
2587 i = lo;
2588 lo = hi;
2589 hi = i;
2590 old_len = -old_len;
2591 }
2592 extra = 0;
2593
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002594 check_line_range(lo, buf->buf); // inclusive
2595 check_line_range(hi - 1, buf->buf); // exclusive
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002596
2597 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2598 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002599 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002600 curbuf = buf->buf;
2601
2602 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2603 {
2604 curbuf = savebuf;
2605 raise_vim_exn(_("cannot save undo information"));
2606 }
2607 else
2608 {
2609 for (i = 0; i < old_len; i++)
Bram Moolenaarca70c072020-05-30 20:30:46 +02002610 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002611 {
2612 curbuf = savebuf;
2613 raise_vim_exn(_("cannot delete line"));
2614 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002615 if (buf->buf == curwin->w_buffer)
2616 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002617 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002618 }
2619
2620 curbuf = savebuf;
2621
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002622 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002623 raise_if_error();
2624 return scheme_void;
2625 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002626 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002627 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002628 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002629
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002630 // List
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002631 new_len = scheme_proper_list_length(line_list);
2632 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002633 if (new_len < 0) // improper or cyclic list
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002634 scheme_wrong_type(prim->name, "proper list",
2635 2, argc, argv);
2636 else
2637 {
2638 char **array = NULL;
2639 Scheme_Object *line = NULL;
2640 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002641
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002642 MZ_GC_DECL_REG(2);
2643 MZ_GC_VAR_IN_REG(0, line);
2644 MZ_GC_VAR_IN_REG(1, rest);
2645 MZ_GC_REG();
2646
Bram Moolenaara80faa82020-04-12 19:37:17 +02002647 array = ALLOC_CLEAR_MULT(char *, new_len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002648
2649 rest = line_list;
2650 for (i = 0; i < new_len; ++i)
2651 {
2652 line = SCHEME_CAR(rest);
2653 rest = SCHEME_CDR(rest);
2654 if (!SCHEME_STRINGP(line))
2655 {
2656 free_array(array);
2657 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2658 }
2659 array[i] = string_to_line(line);
2660 }
2661
2662 curbuf = buf->buf;
2663
2664 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2665 {
2666 curbuf = savebuf;
2667 free_array(array);
2668 raise_vim_exn(_("cannot save undo information"));
2669 }
2670
2671 /*
2672 * If the size of the range is reducing (ie, new_len < old_len) we
2673 * need to delete some old_len. We do this at the start, by
2674 * repeatedly deleting line "lo".
2675 */
2676 for (i = 0; i < old_len - new_len; ++i)
2677 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02002678 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002679 {
2680 curbuf = savebuf;
2681 free_array(array);
2682 raise_vim_exn(_("cannot delete line"));
2683 }
2684 extra--;
2685 }
2686
2687 /*
2688 * For as long as possible, replace the existing old_len with the
2689 * new old_len. This is a more efficient operation, as it requires
2690 * less memory allocation and freeing.
2691 */
2692 for (i = 0; i < old_len && i < new_len; i++)
2693 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2694 {
2695 curbuf = savebuf;
2696 free_array(array);
2697 raise_vim_exn(_("cannot replace line"));
2698 }
2699
2700 /*
2701 * Now we may need to insert the remaining new_len. We don't need to
2702 * free the string passed back because MzScheme has control of that
2703 * memory.
2704 */
2705 while (i < new_len)
2706 {
2707 if (ml_append((linenr_T)(lo + i - 1),
2708 (char_u *)array[i], 0, FALSE) == FAIL)
2709 {
2710 curbuf = savebuf;
2711 free_array(array);
2712 raise_vim_exn(_("cannot insert line"));
2713 }
2714 ++i;
2715 ++extra;
2716 }
2717 MZ_GC_UNREG();
2718 free_array(array);
2719 }
2720
2721 /*
2722 * Adjust marks. Invalidate any which lie in the
2723 * changed range, and move any in the remainder of the buffer.
2724 */
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002725 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2726 (long)MAXLNUM, (long)extra);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002727 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2728
2729 if (buf->buf == curwin->w_buffer)
2730 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002731 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002732
2733 MZ_GC_UNREG();
2734 raise_if_error();
2735 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002736 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002737}
2738
2739/*
2740 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2741 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002742 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002743 * The line number is in Vim format (1-based). The lines to be inserted are
2744 * given as an MzScheme list of string objects or as a single string. The lines
2745 * to be added are checked for validity and correct format. Errors are
2746 * returned as a value of FAIL. The return value is OK on success.
2747 * If OK is returned and len_change is not NULL, *len_change
2748 * is set to the change in the buffer length.
2749 */
2750 static Scheme_Object *
2751insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2752{
2753 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002754 vim_mz_buffer *buf = NULL;
2755 Scheme_Object *list = NULL;
2756 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002757 int i, n, size;
2758
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002759 MZ_GC_DECL_REG(1);
2760 MZ_GC_VAR_IN_REG(0, list);
2761 MZ_GC_REG();
2762
Bram Moolenaar555b2802005-05-19 21:08:39 +00002763#ifdef HAVE_SANDBOX
2764 sandbox_check();
2765#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002766 /*
2767 * First of all, we check the type of the supplied MzScheme object.
2768 * It must be a string or a list, or the call is in error.
2769 */
2770 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2771 list = argv[1];
2772
2773 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2774 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2775 buf = get_buffer_arg(prim->name, 2, argc, argv);
2776
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002777 if (n != 0) // 0 can be used in insert
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002778 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002779 if (SCHEME_STRINGP(list))
2780 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002781 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002782
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002783 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002784 curbuf = buf->buf;
2785
2786 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2787 {
2788 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002789 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002790 raise_vim_exn(_("cannot save undo information"));
2791 }
2792 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2793 {
2794 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002795 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002796 raise_vim_exn(_("cannot insert line"));
2797 }
2798 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002799 {
2800 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002801 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002802 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002803
2804 curbuf = savebuf;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002805 update_screen(UPD_VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002806
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002807 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002808 raise_if_error();
2809 return scheme_void;
2810 }
2811
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002812 // List
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002813 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002814 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002815 if (size < 0) // improper or cyclic list
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002816 scheme_wrong_type(prim->name, "proper list",
2817 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002818 else
2819 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002820 Scheme_Object *line = NULL;
2821 Scheme_Object *rest = NULL;
2822 char **array;
2823 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002824
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002825 MZ_GC_DECL_REG(2);
2826 MZ_GC_VAR_IN_REG(0, line);
2827 MZ_GC_VAR_IN_REG(1, rest);
2828 MZ_GC_REG();
2829
Bram Moolenaara80faa82020-04-12 19:37:17 +02002830 array = ALLOC_CLEAR_MULT(char *, size + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002831
2832 rest = list;
2833 for (i = 0; i < size; ++i)
2834 {
2835 line = SCHEME_CAR(rest);
2836 rest = SCHEME_CDR(rest);
2837 array[i] = string_to_line(line);
2838 }
2839
2840 curbuf = buf->buf;
2841
2842 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2843 {
2844 curbuf = savebuf;
2845 free_array(array);
2846 raise_vim_exn(_("cannot save undo information"));
2847 }
2848 else
2849 {
2850 for (i = 0; i < size; ++i)
2851 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2852 0, FALSE) == FAIL)
2853 {
2854 curbuf = savebuf;
2855 free_array(array);
2856 raise_vim_exn(_("cannot insert line"));
2857 }
2858
2859 if (i > 0)
2860 appended_lines_mark((linenr_T)n, (long)i);
2861 }
2862 free_array(array);
2863 MZ_GC_UNREG();
2864 curbuf = savebuf;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002865 update_screen(UPD_VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002866 }
2867
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002868 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002869 raise_if_error();
2870 return scheme_void;
2871}
2872
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002873/*
2874 * Predicates
2875 */
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002876/*
2877 * (buff? obj)
2878 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002879 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002880vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002881{
2882 if (SCHEME_VIMBUFFERP(argv[0]))
2883 return scheme_true;
2884 else
2885 return scheme_false;
2886}
2887
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002888/*
2889 * (win? obj)
2890 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002891 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002892vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002893{
2894 if (SCHEME_VIMWINDOWP(argv[0]))
2895 return scheme_true;
2896 else
2897 return scheme_false;
2898}
2899
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002900/*
2901 * (buff-valid? obj)
2902 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002903 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002904vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002905{
2906 if (SCHEME_VIMBUFFERP(argv[0])
2907 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2908 return scheme_true;
2909 else
2910 return scheme_false;
2911}
2912
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002913/*
2914 * (win-valid? obj)
2915 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002916 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002917vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002918{
2919 if (SCHEME_VIMWINDOWP(argv[0])
2920 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2921 return scheme_true;
2922 else
2923 return scheme_false;
2924}
2925
2926/*
2927 *===========================================================================
2928 * Utilities
2929 *===========================================================================
2930 */
2931
2932/*
2933 * Convert an MzScheme string into a Vim line.
2934 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002935 * All internal nulls are replaced by newline characters.
2936 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002937 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002938 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002939 */
2940 static char *
2941string_to_line(Scheme_Object *obj)
2942{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002943 char *scheme_str = NULL;
2944 char *vim_str = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01002945 OUTPUT_LEN_TYPE len;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002946 int i;
2947
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002948 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002949
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002950 // Error checking: String must not contain newlines, as we
2951 // are replacing a single line, and we must replace it with
2952 // a single line.
Bram Moolenaar75676462013-01-30 14:55:42 +01002953 if (memchr(scheme_str, '\n', len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002954 scheme_signal_error(_("string cannot contain newlines"));
2955
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002956 vim_str = alloc(len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002957
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002958 // Create a copy of the string, with internal nulls replaced by
2959 // newline characters, as is the vim convention.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002960 for (i = 0; i < len; ++i)
2961 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002962 if (scheme_str[i] == '\0')
2963 vim_str[i] = '\n';
2964 else
2965 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002966 }
2967
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002968 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002969
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002970 MZ_GC_CHECK();
2971 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002972}
2973
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002974#ifdef FEAT_EVAL
2975/*
2976 * Convert Vim value into MzScheme, adopted from if_python.c
2977 */
2978 static Scheme_Object *
Bram Moolenaar75676462013-01-30 14:55:42 +01002979vim_to_mzscheme(typval_T *vim_value)
2980{
2981 Scheme_Object *result = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002982 // hash table to store visited values to avoid infinite loops
Bram Moolenaar75676462013-01-30 14:55:42 +01002983 Scheme_Hash_Table *visited = NULL;
2984
2985 MZ_GC_DECL_REG(2);
2986 MZ_GC_VAR_IN_REG(0, result);
2987 MZ_GC_VAR_IN_REG(1, visited);
2988 MZ_GC_REG();
2989
2990 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2991 MZ_GC_CHECK();
2992
2993 result = vim_to_mzscheme_impl(vim_value, 1, visited);
2994
2995 MZ_GC_UNREG();
2996 return result;
2997}
2998
2999 static Scheme_Object *
3000vim_to_mzscheme_impl(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003001{
3002 Scheme_Object *result = NULL;
3003 int new_value = TRUE;
3004
Bram Moolenaar75676462013-01-30 14:55:42 +01003005 MZ_GC_DECL_REG(2);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003006 MZ_GC_VAR_IN_REG(0, result);
Bram Moolenaar75676462013-01-30 14:55:42 +01003007 MZ_GC_VAR_IN_REG(1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003008 MZ_GC_REG();
3009
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003010 // Avoid infinite recursion
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003011 if (depth > 100)
3012 {
3013 MZ_GC_UNREG();
3014 return scheme_void;
3015 }
3016
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003017 // Check if we run into a recursive loop. The item must be in visited
3018 // then and we can use it again.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003019 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
3020 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003021 if (result != NULL) // found, do nothing
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003022 new_value = FALSE;
3023 else if (vim_value->v_type == VAR_STRING)
3024 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003025 result = scheme_make_byte_string((char *)vim_value->vval.v_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003026 MZ_GC_CHECK();
3027 }
3028 else if (vim_value->v_type == VAR_NUMBER)
3029 {
3030 result = scheme_make_integer((long)vim_value->vval.v_number);
3031 MZ_GC_CHECK();
3032 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003033 else if (vim_value->v_type == VAR_FLOAT)
3034 {
3035 result = scheme_make_double((double)vim_value->vval.v_float);
3036 MZ_GC_CHECK();
3037 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003038 else if (vim_value->v_type == VAR_LIST)
3039 {
3040 list_T *list = vim_value->vval.v_list;
3041 listitem_T *curr;
3042
3043 if (list == NULL || list->lv_first == NULL)
3044 result = scheme_null;
3045 else
3046 {
3047 Scheme_Object *obj = NULL;
3048
3049 MZ_GC_DECL_REG(1);
3050 MZ_GC_VAR_IN_REG(0, obj);
3051 MZ_GC_REG();
3052
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003053 curr = list->lv_u.mat.lv_last;
Bram Moolenaar75676462013-01-30 14:55:42 +01003054 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003055 result = scheme_make_pair(obj, scheme_null);
3056 MZ_GC_CHECK();
3057
3058 while (curr != list->lv_first)
3059 {
3060 curr = curr->li_prev;
Bram Moolenaar75676462013-01-30 14:55:42 +01003061 obj = vim_to_mzscheme_impl(&curr->li_tv, depth + 1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003062 result = scheme_make_pair(obj, result);
3063 MZ_GC_CHECK();
3064 }
3065 }
3066 MZ_GC_UNREG();
3067 }
3068 else if (vim_value->v_type == VAR_DICT)
3069 {
3070 Scheme_Object *key = NULL;
3071 Scheme_Object *obj = NULL;
3072
3073 MZ_GC_DECL_REG(2);
3074 MZ_GC_VAR_IN_REG(0, key);
3075 MZ_GC_VAR_IN_REG(1, obj);
3076 MZ_GC_REG();
3077
3078 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
3079 MZ_GC_CHECK();
3080 if (vim_value->vval.v_dict != NULL)
3081 {
3082 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
3083 long_u todo = ht->ht_used;
3084 hashitem_T *hi;
3085 dictitem_T *di;
3086
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003087 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003088 {
3089 if (!HASHITEM_EMPTY(hi))
3090 {
3091 --todo;
3092
3093 di = dict_lookup(hi);
Bram Moolenaar75676462013-01-30 14:55:42 +01003094 obj = vim_to_mzscheme_impl(&di->di_tv, depth + 1, visited);
3095 key = scheme_make_byte_string((char *)hi->hi_key);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003096 MZ_GC_CHECK();
3097 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
3098 MZ_GC_CHECK();
3099 }
3100 }
3101 }
3102 MZ_GC_UNREG();
3103 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003104 else if (vim_value->v_type == VAR_FUNC)
3105 {
3106 Scheme_Object *funcname = NULL;
3107
3108 MZ_GC_DECL_REG(1);
3109 MZ_GC_VAR_IN_REG(0, funcname);
3110 MZ_GC_REG();
3111
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003112 // FIXME: func_ref() and func_unref() are needed.
Bram Moolenaar75676462013-01-30 14:55:42 +01003113 funcname = scheme_make_byte_string((char *)vim_value->vval.v_string);
3114 MZ_GC_CHECK();
3115 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3116 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3117 MZ_GC_CHECK();
3118
3119 MZ_GC_UNREG();
3120 }
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003121 else if (vim_value->v_type == VAR_PARTIAL)
3122 {
3123 if (vim_value->vval.v_partial == NULL)
3124 result = scheme_null;
3125 else
3126 {
3127 Scheme_Object *funcname = NULL;
3128
3129 MZ_GC_DECL_REG(1);
3130 MZ_GC_VAR_IN_REG(0, funcname);
3131 MZ_GC_REG();
3132
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003133 // FIXME: func_ref() and func_unref() are needed.
3134 // TODO: Support pt_dict and pt_argv.
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003135 funcname = scheme_make_byte_string(
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003136 (char *)partial_name(vim_value->vval.v_partial));
Bram Moolenaar67c2c052016-03-30 22:03:02 +02003137 MZ_GC_CHECK();
3138 result = scheme_make_closed_prim_w_arity(vim_funcref, funcname,
3139 (const char *)BYTE_STRING_VALUE(funcname), 0, -1);
3140 MZ_GC_CHECK();
3141
3142 MZ_GC_UNREG();
3143 }
3144 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003145 else if (vim_value->v_type == VAR_BOOL || vim_value->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003146 {
3147 if (vim_value->vval.v_number <= VVAL_TRUE)
3148 result = scheme_make_integer((long)vim_value->vval.v_number);
3149 else
3150 result = scheme_null;
3151 MZ_GC_CHECK();
3152 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003153 else
3154 {
3155 result = scheme_void;
3156 new_value = FALSE;
3157 }
3158 if (new_value)
3159 {
3160 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
3161 MZ_GC_CHECK();
3162 }
3163 MZ_GC_UNREG();
3164 return result;
3165}
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003166
3167 static int
Bram Moolenaar75676462013-01-30 14:55:42 +01003168mzscheme_to_vim(Scheme_Object *obj, typval_T *tv)
3169{
3170 int i, status;
3171 Scheme_Hash_Table *visited = NULL;
3172
3173 MZ_GC_DECL_REG(2);
3174 MZ_GC_VAR_IN_REG(0, obj);
3175 MZ_GC_VAR_IN_REG(1, visited);
3176 MZ_GC_REG();
3177
3178 visited = scheme_make_hash_table(SCHEME_hash_ptr);
3179 MZ_GC_CHECK();
3180
3181 status = mzscheme_to_vim_impl(obj, tv, 1, visited);
3182 for (i = 0; i < visited->size; ++i)
3183 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003184 // free up remembered objects
Bram Moolenaar75676462013-01-30 14:55:42 +01003185 if (visited->vals[i] != NULL)
3186 free_tv((typval_T *)visited->vals[i]);
3187 }
3188
3189 MZ_GC_UNREG();
3190 return status;
3191}
3192 static int
3193mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003194 Scheme_Hash_Table *visited)
3195{
3196 int status = OK;
3197 typval_T *found;
Bram Moolenaar75676462013-01-30 14:55:42 +01003198
3199 MZ_GC_DECL_REG(2);
3200 MZ_GC_VAR_IN_REG(0, obj);
3201 MZ_GC_VAR_IN_REG(1, visited);
3202 MZ_GC_REG();
3203
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003204 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003205 if (depth > 100) // limit the deepest recursion level
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003206 {
3207 tv->v_type = VAR_NUMBER;
3208 tv->vval.v_number = 0;
3209 return FAIL;
3210 }
3211
3212 found = (typval_T *)scheme_hash_get(visited, obj);
3213 if (found != NULL)
3214 copy_tv(found, tv);
3215 else if (SCHEME_VOIDP(obj))
3216 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003217 tv->v_type = VAR_SPECIAL;
3218 tv->vval.v_number = VVAL_NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003219 }
3220 else if (SCHEME_INTP(obj))
3221 {
3222 tv->v_type = VAR_NUMBER;
3223 tv->vval.v_number = SCHEME_INT_VAL(obj);
3224 }
3225 else if (SCHEME_BOOLP(obj))
3226 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003227 tv->v_type = VAR_BOOL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003228 tv->vval.v_number = SCHEME_TRUEP(obj);
3229 }
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003230 else if (SCHEME_DBLP(obj))
3231 {
3232 tv->v_type = VAR_FLOAT;
3233 tv->vval.v_float = SCHEME_DBL_VAL(obj);
3234 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003235 else if (SCHEME_BYTE_STRINGP(obj))
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003236 {
3237 tv->v_type = VAR_STRING;
Bram Moolenaar75676462013-01-30 14:55:42 +01003238 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(obj));
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003239 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003240# if MZSCHEME_VERSION_MAJOR >= 299
3241 else if (SCHEME_CHAR_STRINGP(obj))
3242 {
3243 Scheme_Object *tmp = NULL;
3244 MZ_GC_DECL_REG(1);
3245 MZ_GC_VAR_IN_REG(0, tmp);
3246 MZ_GC_REG();
3247
3248 tmp = scheme_char_string_to_byte_string(obj);
3249 tv->v_type = VAR_STRING;
3250 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(tmp));
3251 MZ_GC_UNREG();
3252 }
3253#endif
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003254 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
3255 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3256 {
3257 list_T *list = list_alloc();
3258 if (list == NULL)
3259 status = FAIL;
3260 else
3261 {
3262 int i;
3263 Scheme_Object *curr = NULL;
3264 Scheme_Object *cval = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003265 // temporary var to hold current element of vectors and pairs
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003266 typval_T *v;
3267
3268 MZ_GC_DECL_REG(2);
3269 MZ_GC_VAR_IN_REG(0, curr);
3270 MZ_GC_VAR_IN_REG(1, cval);
3271 MZ_GC_REG();
3272
3273 tv->v_type = VAR_LIST;
3274 tv->vval.v_list = list;
3275 ++list->lv_refcount;
3276
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003277 v = ALLOC_ONE(typval_T);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003278 if (v == NULL)
3279 status = FAIL;
3280 else
3281 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003282 // add the value in advance to allow handling of self-referential
3283 // data structures
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003284 typval_T *visited_tv = ALLOC_ONE(typval_T);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003285 copy_tv(tv, visited_tv);
3286 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3287
3288 if (SCHEME_VECTORP(obj))
3289 {
3290 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
3291 {
3292 cval = SCHEME_VEC_ELS(obj)[i];
Bram Moolenaar75676462013-01-30 14:55:42 +01003293 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003294 if (status == FAIL)
3295 break;
3296 status = list_append_tv(list, v);
3297 clear_tv(v);
3298 if (status == FAIL)
3299 break;
3300 }
3301 }
3302 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3303 {
3304 for (curr = obj;
3305 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
3306 curr = SCHEME_CDR(curr))
3307 {
3308 cval = SCHEME_CAR(curr);
Bram Moolenaar75676462013-01-30 14:55:42 +01003309 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003310 if (status == FAIL)
3311 break;
3312 status = list_append_tv(list, v);
3313 clear_tv(v);
3314 if (status == FAIL)
3315 break;
3316 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003317 // improper list not terminated with null
3318 // need to handle the last element
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003319 if (status == OK && !SCHEME_NULLP(curr))
3320 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003321 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003322 if (status == OK)
3323 {
3324 status = list_append_tv(list, v);
3325 clear_tv(v);
3326 }
3327 }
3328 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003329 // nothing to do for scheme_null
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003330 vim_free(v);
3331 }
3332 MZ_GC_UNREG();
3333 }
3334 }
3335 else if (SCHEME_HASHTP(obj))
3336 {
3337 int i;
3338 dict_T *dict;
3339 Scheme_Object *key = NULL;
3340 Scheme_Object *val = NULL;
3341
3342 MZ_GC_DECL_REG(2);
3343 MZ_GC_VAR_IN_REG(0, key);
3344 MZ_GC_VAR_IN_REG(1, val);
3345 MZ_GC_REG();
3346
3347 dict = dict_alloc();
3348 if (dict == NULL)
3349 status = FAIL;
3350 else
3351 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003352 typval_T *visited_tv = ALLOC_ONE(typval_T);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003353
3354 tv->v_type = VAR_DICT;
3355 tv->vval.v_dict = dict;
3356 ++dict->dv_refcount;
3357
3358 copy_tv(tv, visited_tv);
3359 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3360
3361 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
3362 {
3363 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
3364 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003365 // generate item for `display'ed Scheme key
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003366 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
3367 ((Scheme_Hash_Table *) obj)->keys[i]));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003368 // convert Scheme val to Vim and add it to the dict
Bram Moolenaar75676462013-01-30 14:55:42 +01003369 if (mzscheme_to_vim_impl(((Scheme_Hash_Table *) obj)->vals[i],
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003370 &item->di_tv, depth + 1, visited) == FAIL
3371 || dict_add(dict, item) == FAIL)
3372 {
3373 dictitem_free(item);
3374 status = FAIL;
3375 break;
3376 }
3377 }
3378
3379 }
3380 }
3381 MZ_GC_UNREG();
3382 }
3383 else
3384 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003385 // `display' any other value to string
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003386 tv->v_type = VAR_STRING;
3387 tv->vval.v_string = (char_u *)string_to_line(obj);
3388 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003389 MZ_GC_UNREG();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003390 return status;
3391}
3392
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003393/*
3394 * Scheme prim procedure wrapping Vim funcref
3395 */
Bram Moolenaar75676462013-01-30 14:55:42 +01003396 static Scheme_Object *
3397vim_funcref(void *name, int argc, Scheme_Object **argv)
3398{
3399 int i;
3400 typval_T args;
3401 int status = OK;
3402 Scheme_Object *result = NULL;
3403 list_T *list = list_alloc();
3404
3405 MZ_GC_DECL_REG(1);
3406 MZ_GC_VAR_IN_REG(0, result);
3407 MZ_GC_REG();
3408
3409 result = scheme_void;
3410 if (list == NULL)
3411 status = FAIL;
3412 else
3413 {
3414 args.v_type = VAR_LIST;
3415 args.vval.v_list = list;
3416 ++list->lv_refcount;
3417 for (i = 0; status == OK && i < argc; ++i)
3418 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003419 typval_T *v = ALLOC_ONE(typval_T);
Bram Moolenaar75676462013-01-30 14:55:42 +01003420 if (v == NULL)
3421 status = FAIL;
3422 else
3423 {
3424 status = mzscheme_to_vim(argv[i], v);
3425 if (status == OK)
3426 {
3427 status = list_append_tv(list, v);
3428 clear_tv(v);
3429 }
3430 vim_free(v);
3431 }
3432 }
3433 if (status == OK)
3434 {
3435 typval_T ret;
3436 ret.v_type = VAR_UNKNOWN;
3437
3438 mzscheme_call_vim(BYTE_STRING_VALUE((Scheme_Object *)name), &args, &ret);
3439 MZ_GC_CHECK();
3440 result = vim_to_mzscheme(&ret);
3441 clear_tv(&ret);
3442 MZ_GC_CHECK();
3443 }
3444 }
3445 clear_tv(&args);
3446 MZ_GC_UNREG();
3447 if (status != OK)
3448 raise_vim_exn(_("error converting Scheme values to Vim"));
3449 else
3450 raise_if_error();
3451 return result;
3452}
3453
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003454 void
3455do_mzeval(char_u *str, typval_T *rettv)
3456{
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003457 Scheme_Object *ret = NULL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003458
Bram Moolenaar75676462013-01-30 14:55:42 +01003459 MZ_GC_DECL_REG(1);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003460 MZ_GC_VAR_IN_REG(0, ret);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003461 MZ_GC_REG();
3462
3463 if (mzscheme_init())
3464 {
3465 MZ_GC_UNREG();
3466 return;
3467 }
3468
3469 MZ_GC_CHECK();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003470 if (eval_with_exn_handling(str, do_eval, &ret) == OK)
Bram Moolenaar75676462013-01-30 14:55:42 +01003471 mzscheme_to_vim(ret, rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003472
3473 MZ_GC_UNREG();
3474}
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003475#endif
3476
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003477/*
3478 * Check to see whether a Vim error has been reported, or a keyboard
3479 * interrupt (from vim --> got_int) has been detected.
3480 */
3481 static int
3482vim_error_check(void)
3483{
3484 return (got_int || did_emsg);
3485}
3486
3487/*
3488 * register Scheme exn:vim
3489 */
3490 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003491register_vim_exn(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003492{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003493 int nc = 0;
3494 int i;
3495 Scheme_Object *struct_exn = NULL;
3496 Scheme_Object *exn_name = NULL;
3497
3498 MZ_GC_DECL_REG(2);
3499 MZ_GC_VAR_IN_REG(0, struct_exn);
3500 MZ_GC_VAR_IN_REG(1, exn_name);
3501 MZ_GC_REG();
3502
3503 exn_name = scheme_intern_symbol("exn:vim");
3504 MZ_GC_CHECK();
3505 struct_exn = scheme_builtin_value("struct:exn");
3506 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003507
3508 if (vim_exn == NULL)
3509 vim_exn = scheme_make_struct_type(exn_name,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003510 struct_exn, NULL, 0, 0, NULL, NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003511#if MZSCHEME_VERSION_MAJOR >= 299
3512 , NULL
3513#endif
3514 );
3515
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003516
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003517 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003518 Scheme_Object **tmp = NULL;
3519 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
3520 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
3521 MZ_GC_DECL_REG(6);
3522 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
3523 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
3524 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003525
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003526 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003527 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
3528 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003529
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003530 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
3531 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
3532 MZ_GC_CHECK();
3533
3534 for (i = 0; i < nc; i++)
3535 {
3536 scheme_add_global_symbol(exn_names[i],
3537 exn_values[i], environment);
3538 MZ_GC_CHECK();
3539 }
3540 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003541 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003542 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003543}
3544
3545/*
3546 * raise exn:vim, may be with additional info string
3547 */
3548 void
3549raise_vim_exn(const char *add_info)
3550{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003551 char *fmt = _("Vim error: ~a");
3552 Scheme_Object *argv[2] = {NULL, NULL};
3553 Scheme_Object *exn = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01003554 Scheme_Object *byte_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003555
Bram Moolenaar75676462013-01-30 14:55:42 +01003556 MZ_GC_DECL_REG(5);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003557 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
3558 MZ_GC_VAR_IN_REG(3, exn);
Bram Moolenaar75676462013-01-30 14:55:42 +01003559 MZ_GC_VAR_IN_REG(4, byte_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003560 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003561
3562 if (add_info != NULL)
3563 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003564 char *c_string = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003565 Scheme_Object *info = NULL;
3566
3567 MZ_GC_DECL_REG(3);
3568 MZ_GC_VAR_IN_REG(0, c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003569 MZ_GC_VAR_IN_REG(2, info);
3570 MZ_GC_REG();
3571
Bram Moolenaar75676462013-01-30 14:55:42 +01003572 info = scheme_make_byte_string(add_info);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003573 MZ_GC_CHECK();
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02003574 c_string = scheme_format_utf8(fmt, (int)STRLEN(fmt), 1, &info, NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003575 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01003576 byte_string = scheme_make_byte_string(c_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003577 MZ_GC_CHECK();
3578 argv[0] = scheme_byte_string_to_char_string(byte_string);
Bram Moolenaar555b2802005-05-19 21:08:39 +00003579 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003580 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003581 }
3582 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003583 {
3584 byte_string = scheme_make_byte_string(_("Vim error"));
3585 MZ_GC_CHECK();
3586 argv[0] = scheme_byte_string_to_char_string(byte_string);
3587 MZ_GC_CHECK();
3588 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003589 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003590
Bram Moolenaar049377e2007-05-12 15:32:12 +00003591#if MZSCHEME_VERSION_MAJOR < 360
3592 argv[1] = scheme_current_continuation_marks();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003593 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003594#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003595 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003596 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003597#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003598
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003599 exn = scheme_make_struct_instance(vim_exn, 2, argv);
3600 MZ_GC_CHECK();
3601 scheme_raise(exn);
3602 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003603}
3604
3605 void
3606raise_if_error(void)
3607{
3608 if (vim_error_check())
3609 raise_vim_exn(NULL);
3610}
3611
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003612/*
3613 * get buffer:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003614 * either current
3615 * or passed as argv[argnum] with checks
3616 */
3617 static vim_mz_buffer *
3618get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3619{
3620 vim_mz_buffer *b;
3621
3622 if (argc < argnum + 1)
3623 return get_vim_curr_buffer();
3624 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3625 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3626 b = (vim_mz_buffer *)argv[argnum];
3627 (void)get_valid_buffer(argv[argnum]);
3628 return b;
3629}
3630
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003631/*
3632 * get window:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003633 * either current
3634 * or passed as argv[argnum] with checks
3635 */
3636 static vim_mz_window *
3637get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3638{
3639 vim_mz_window *w;
3640
3641 if (argc < argnum + 1)
3642 return get_vim_curr_window();
3643 w = (vim_mz_window *)argv[argnum];
3644 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3645 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3646 (void)get_valid_window(argv[argnum]);
3647 return w;
3648}
3649
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003650/*
3651 * get valid Vim buffer from Scheme_Object*
3652 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003653buf_T *get_valid_buffer(void *obj)
3654{
3655 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3656
3657 if (buf == INVALID_BUFFER_VALUE)
3658 scheme_signal_error(_("buffer is invalid"));
3659 return buf;
3660}
3661
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003662/*
3663 * get valid Vim window from Scheme_Object*
3664 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003665win_T *get_valid_window(void *obj)
3666{
3667 win_T *win = ((vim_mz_window *)obj)->win;
3668 if (win == INVALID_WINDOW_VALUE)
3669 scheme_signal_error(_("window is invalid"));
3670 return win;
3671}
3672
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003673 int
3674mzthreads_allowed(void)
3675{
3676 return mz_threads_allow;
3677}
3678
3679 static int
3680line_in_range(linenr_T lnum, buf_T *buf)
3681{
3682 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3683}
3684
3685 static void
3686check_line_range(linenr_T lnum, buf_T *buf)
3687{
3688 if (!line_in_range(lnum, buf))
3689 scheme_signal_error(_("linenr out of range"));
3690}
3691
3692/*
3693 * Check if deleting lines made the cursor position invalid
3694 * (or you'll get msg from Vim about invalid linenr).
3695 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3696 * deleted). Got from if_python.c
3697 */
3698 static void
3699mz_fix_cursor(int lo, int hi, int extra)
3700{
3701 if (curwin->w_cursor.lnum >= lo)
3702 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003703 // Adjust the cursor position if it's in/after the changed
3704 // lines.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003705 if (curwin->w_cursor.lnum >= hi)
3706 {
3707 curwin->w_cursor.lnum += extra;
3708 check_cursor_col();
3709 }
3710 else if (extra < 0)
3711 {
3712 curwin->w_cursor.lnum = lo;
3713 check_cursor();
3714 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003715 else
3716 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003717 changed_cline_bef_curs();
3718 }
3719 invalidate_botline();
3720}
3721
3722static Vim_Prim prims[]=
3723{
3724 /*
3725 * Buffer-related commands
3726 */
3727 {get_buffer_line, "get-buff-line", 1, 2},
3728 {set_buffer_line, "set-buff-line", 2, 3},
3729 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3730 {get_buffer_name, "get-buff-name", 0, 1},
3731 {get_buffer_num, "get-buff-num", 0, 1},
3732 {get_buffer_size, "get-buff-size", 0, 1},
3733 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3734 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3735 {get_curr_buffer, "curr-buff", 0, 0},
3736 {get_buffer_count, "buff-count", 0, 0},
3737 {get_next_buffer, "get-next-buff", 0, 1},
3738 {get_prev_buffer, "get-prev-buff", 0, 1},
3739 {mzscheme_open_buffer, "open-buff", 1, 1},
3740 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3741 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003742 /*
3743 * Window-related commands
3744 */
3745 {get_curr_win, "curr-win", 0, 0},
3746 {get_window_count, "win-count", 0, 0},
3747 {get_window_by_num, "get-win-by-num", 1, 1},
3748 {get_window_num, "get-win-num", 0, 1},
3749 {get_window_buffer, "get-win-buffer", 0, 1},
3750 {get_window_height, "get-win-height", 0, 1},
3751 {set_window_height, "set-win-height", 1, 2},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003752 {get_window_width, "get-win-width", 0, 1},
3753 {set_window_width, "set-win-width", 1, 2},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003754 {get_cursor, "get-cursor", 0, 1},
3755 {set_cursor, "set-cursor", 1, 2},
3756 {get_window_list, "get-win-list", 0, 1},
3757 /*
3758 * Vim-related commands
3759 */
3760 {vim_command, "command", 1, 1},
3761 {vim_eval, "eval", 1, 1},
3762 {get_range_start, "range-start", 0, 0},
3763 {get_range_end, "range-end", 0, 0},
3764 {mzscheme_beep, "beep", 0, 0},
3765 {get_option, "get-option", 1, 2},
3766 {set_option, "set-option", 1, 2},
3767 /*
3768 * small utilities
3769 */
3770 {vim_bufferp, "buff?", 1, 1},
3771 {vim_windowp, "win?", 1, 1},
3772 {vim_buffer_validp, "buff-valid?", 1, 1},
3773 {vim_window_validp, "win-valid?", 1, 1}
3774};
3775
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003776/*
3777 * return MzScheme wrapper for curbuf
3778 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003779 static vim_mz_buffer *
3780get_vim_curr_buffer(void)
3781{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003782 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003783 return (vim_mz_buffer *)buffer_new(curbuf);
3784 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003785 return BUFFER_REF(curbuf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003786}
3787
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003788/*
3789 * return MzScheme wrapper for curwin
3790 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003791 static vim_mz_window *
3792get_vim_curr_window(void)
3793{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003794 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003795 return (vim_mz_window *)window_new(curwin);
3796 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003797 return WINDOW_REF(curwin);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003798}
3799
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003800 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003801make_modules(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003802{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003803 int i;
3804 Scheme_Env *mod = NULL;
3805 Scheme_Object *vimext_symbol = NULL;
3806 Scheme_Object *closed_prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003807
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003808 MZ_GC_DECL_REG(3);
3809 MZ_GC_VAR_IN_REG(0, mod);
3810 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3811 MZ_GC_VAR_IN_REG(2, closed_prim);
3812 MZ_GC_REG();
3813
3814 vimext_symbol = scheme_intern_symbol("vimext");
3815 MZ_GC_CHECK();
3816 mod = scheme_primitive_module(vimext_symbol, environment);
3817 MZ_GC_CHECK();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003818 // all prims made closed so they can access their own names
K.Takataeeec2542021-06-02 13:28:16 +02003819 for (i = 0; i < (int)ARRAY_LENGTH(prims); i++)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003820 {
3821 Vim_Prim *prim = prims + i;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003822 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3823 prim->mina, prim->maxa);
3824 scheme_add_global(prim->name, closed_prim, mod);
3825 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003826 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003827 scheme_finish_primitive_module(mod);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003828 MZ_GC_CHECK();
3829 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003830}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003831
Bram Moolenaar555b2802005-05-19 21:08:39 +00003832#ifdef HAVE_SANDBOX
3833static Scheme_Object *M_write = NULL;
3834static Scheme_Object *M_read = NULL;
3835static Scheme_Object *M_execute = NULL;
3836static Scheme_Object *M_delete = NULL;
3837
3838 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003839sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003840{
3841 if (sandbox)
3842 raise_vim_exn(_("not allowed in the Vim sandbox"));
3843}
3844
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003845/*
3846 * security guards to force Vim's sandbox restrictions on MzScheme level
3847 */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003848 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003849sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003850{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00003851 if (!sandbox)
3852 return scheme_void;
3853
3854 Scheme_Object *requested_access = argv[2];
3855
3856 if (M_write == NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003857 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00003858 MZ_REGISTER_STATIC(M_write);
3859 M_write = scheme_intern_symbol("write");
3860 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003861 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00003862 if (M_read == NULL)
3863 {
3864 MZ_REGISTER_STATIC(M_read);
3865 M_read = scheme_intern_symbol("read");
3866 MZ_GC_CHECK();
3867 }
3868 if (M_execute == NULL)
3869 {
3870 MZ_REGISTER_STATIC(M_execute);
3871 M_execute = scheme_intern_symbol("execute");
3872 MZ_GC_CHECK();
3873 }
3874 if (M_delete == NULL)
3875 {
3876 MZ_REGISTER_STATIC(M_delete);
3877 M_delete = scheme_intern_symbol("delete");
3878 MZ_GC_CHECK();
3879 }
3880
3881 while (!SCHEME_NULLP(requested_access))
3882 {
3883 Scheme_Object *item = SCHEME_CAR(requested_access);
3884 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3885 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
3886 raise_vim_exn(_("not allowed in the Vim sandbox"));
3887 requested_access = SCHEME_CDR(requested_access);
3888 }
3889
Bram Moolenaar555b2802005-05-19 21:08:39 +00003890 return scheme_void;
3891}
3892
3893 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003894sandbox_network_guard(int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003895{
3896 return scheme_void;
3897}
3898#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00003899
3900#endif