blob: 37cebc553a48c3338071ce369cdefe1d0253d768 [file] [log] [blame]
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003 * MzScheme interface by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar75676462013-01-30 14:55:42 +01004 * Based on work by Brent Fulgham <bfulgham@debian.org>
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005 * (Based on lots of help from Matthew Flatt)
6 *
7 * This consists of six parts:
8 * 1. MzScheme interpreter main program
9 * 2. Routines that handle the external interface between MzScheme and
10 * Vim.
11 * 3. MzScheme input/output handlers: writes output via [e]msg().
12 * 4. Implementation of the Vim Features for MzScheme
13 * 5. Vim Window-related Manipulation Functions.
14 * 6. Vim Buffer-related Manipulation Functions
15 *
16 * NOTES
17 * 1. Memory, allocated with scheme_malloc*, need not to be freed explicitly,
18 * garbage collector will do it self
19 * 2. Requires at least NORMAL features. I can't imagine why one may want
20 * to build with SMALL or TINY features but with MzScheme interface.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000021 * 3. I don't use K&R-style functions. Anyways, MzScheme headers are ANSI.
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022 */
23
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024#include "vim.h"
Bram Moolenaar049377e2007-05-12 15:32:12 +000025
Bram Moolenaar325b7a22004-07-05 15:58:32 +000026#include "if_mzsch.h"
27
Bram Moolenaar76b92b22006-03-24 22:46:53 +000028/* Only do the following when the feature is enabled. Needed for "make
29 * depend". */
30#if defined(FEAT_MZSCHEME) || defined(PROTO)
31
Bram Moolenaar4349c572016-01-30 13:28:28 +010032#ifdef PROTO
33typedef int Scheme_Object;
34typedef int Scheme_Closed_Prim;
35typedef int Scheme_Env;
36typedef int Scheme_Hash_Table;
37typedef int Scheme_Type;
38typedef int Scheme_Thread;
39typedef int Scheme_Closed_Prim;
40typedef int mzshort;
41typedef int Scheme_Prim;
42typedef int HINSTANCE;
43#endif
44
Bram Moolenaar4e640bd2016-01-16 16:20:38 +010045/*
46 * scheme_register_tls_space is only available on 32-bit Windows until
47 * racket-6.3. See
48 * http://docs.racket-lang.org/inside/im_memoryalloc.html?q=scheme_register_tls_space
49 */
50#if MZSCHEME_VERSION_MAJOR >= 500 && defined(WIN32) \
51 && defined(USE_THREAD_LOCAL) \
52 && (!defined(_WIN64) || MZSCHEME_VERSION_MAJOR >= 603)
53# define HAVE_TLS_SPACE 1
54#endif
55
56/*
57 * Since version 4.x precise GC requires trampolined startup.
58 * Futures and places in version 5.x need it too.
59 */
60#if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR >= 400 \
61 || MZSCHEME_VERSION_MAJOR >= 500 \
62 && (defined(MZ_USE_FUTURES) || defined(MZ_USE_PLACES))
63# define TRAMPOLINED_MZVIM_STARTUP
64#endif
65
Bram Moolenaar325b7a22004-07-05 15:58:32 +000066/* Base data structures */
67#define SCHEME_VIMBUFFERP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_buffer_type)
68#define SCHEME_VIMWINDOWP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_window_type)
69
70typedef struct
71{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000072 Scheme_Object so;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000073 buf_T *buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000074} vim_mz_buffer;
75
76#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
77
78typedef struct
79{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000080 Scheme_Object so;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000081 win_T *win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000082} vim_mz_window;
83
84#define INVALID_WINDOW_VALUE ((win_T *)(-1))
85
86/*
87 * Prims that form MzScheme Vim interface
88 */
89typedef struct
90{
91 Scheme_Closed_Prim *prim;
92 char *name;
93 int mina; /* arity information */
94 int maxa;
95} Vim_Prim;
96
97typedef struct
98{
99 char *name;
100 Scheme_Object *port;
101} Port_Info;
102
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000103/*
104 *========================================================================
105 * Vim-Control Commands
106 *========================================================================
107 */
108/*
109 *========================================================================
110 * Utility functions for the vim/mzscheme interface
111 *========================================================================
112 */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000113#ifdef HAVE_SANDBOX
114static Scheme_Object *sandbox_file_guard(int, Scheme_Object **);
115static Scheme_Object *sandbox_network_guard(int, Scheme_Object **);
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000116static void sandbox_check(void);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000117#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000118/* Buffer-related commands */
119static Scheme_Object *buffer_new(buf_T *buf);
120static Scheme_Object *get_buffer_by_name(void *, int, Scheme_Object **);
121static Scheme_Object *get_buffer_by_num(void *, int, Scheme_Object **);
122static Scheme_Object *get_buffer_count(void *, int, Scheme_Object **);
123static Scheme_Object *get_buffer_line(void *, int, Scheme_Object **);
124static Scheme_Object *get_buffer_line_list(void *, int, Scheme_Object **);
125static Scheme_Object *get_buffer_name(void *, int, Scheme_Object **);
126static Scheme_Object *get_buffer_num(void *, int, Scheme_Object **);
127static Scheme_Object *get_buffer_size(void *, int, Scheme_Object **);
128static Scheme_Object *get_curr_buffer(void *, int, Scheme_Object **);
129static Scheme_Object *get_next_buffer(void *, int, Scheme_Object **);
130static Scheme_Object *get_prev_buffer(void *, int, Scheme_Object **);
131static Scheme_Object *mzscheme_open_buffer(void *, int, Scheme_Object **);
132static Scheme_Object *set_buffer_line(void *, int, Scheme_Object **);
133static Scheme_Object *set_buffer_line_list(void *, int, Scheme_Object **);
134static Scheme_Object *insert_buffer_line_list(void *, int, Scheme_Object **);
135static Scheme_Object *get_range_start(void *, int, Scheme_Object **);
136static Scheme_Object *get_range_end(void *, int, Scheme_Object **);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000137static vim_mz_buffer *get_vim_curr_buffer(void);
138
139/* Window-related commands */
140static Scheme_Object *window_new(win_T *win);
141static Scheme_Object *get_curr_win(void *, int, Scheme_Object **);
142static Scheme_Object *get_window_count(void *, int, Scheme_Object **);
143static Scheme_Object *get_window_by_num(void *, int, Scheme_Object **);
144static Scheme_Object *get_window_num(void *, int, Scheme_Object **);
145static Scheme_Object *get_window_buffer(void *, int, Scheme_Object **);
146static Scheme_Object *get_window_height(void *, int, Scheme_Object **);
147static Scheme_Object *set_window_height(void *, int, Scheme_Object **);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100148#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000149static Scheme_Object *get_window_width(void *, int, Scheme_Object **);
150static Scheme_Object *set_window_width(void *, int, Scheme_Object **);
151#endif
152static Scheme_Object *get_cursor(void *, int, Scheme_Object **);
153static Scheme_Object *set_cursor(void *, int, Scheme_Object **);
154static Scheme_Object *get_window_list(void *, int, Scheme_Object **);
155static vim_mz_window *get_vim_curr_window(void);
156
157/* Vim-related commands */
158static Scheme_Object *mzscheme_beep(void *, int, Scheme_Object **);
159static Scheme_Object *get_option(void *, int, Scheme_Object **);
160static Scheme_Object *set_option(void *, int, Scheme_Object **);
161static Scheme_Object *vim_command(void *, int, Scheme_Object **);
162static Scheme_Object *vim_eval(void *, int, Scheme_Object **);
163static Scheme_Object *vim_bufferp(void *data, int, Scheme_Object **);
164static Scheme_Object *vim_windowp(void *data, int, Scheme_Object **);
165static Scheme_Object *vim_buffer_validp(void *data, int, Scheme_Object **);
166static Scheme_Object *vim_window_validp(void *data, int, Scheme_Object **);
167
168/*
169 *========================================================================
170 * Internal Function Prototypes
171 *========================================================================
172 */
173static int vim_error_check(void);
174static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100175static int startup_mzscheme(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000176static char *string_to_line(Scheme_Object *obj);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100177#if MZSCHEME_VERSION_MAJOR >= 501
Bram Moolenaar75676462013-01-30 14:55:42 +0100178# define OUTPUT_LEN_TYPE intptr_t
179#else
180# define OUTPUT_LEN_TYPE long
181#endif
182static void do_output(char *mesg, OUTPUT_LEN_TYPE len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000183static void do_printf(char *format, ...);
184static void do_flush(void);
185static Scheme_Object *_apply_thunk_catch_exceptions(
186 Scheme_Object *, Scheme_Object **);
187static Scheme_Object *extract_exn_message(Scheme_Object *v);
188static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
189static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000190static void register_vim_exn(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000191static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
192 int argc, Scheme_Object **argv);
193static vim_mz_window *get_window_arg(const char *fname, int argnum,
194 int argc, Scheme_Object **argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000195static int line_in_range(linenr_T, buf_T *);
196static void check_line_range(linenr_T, buf_T *);
197static void mz_fix_cursor(int lo, int hi, int extra);
198
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000199static int eval_with_exn_handling(void *, Scheme_Closed_Prim *,
200 Scheme_Object **ret);
201static void make_modules(void);
202static void init_exn_catching_apply(void);
203static int mzscheme_env_main(Scheme_Env *env, int argc, char **argv);
204static int mzscheme_init(void);
205#ifdef FEAT_EVAL
Bram Moolenaar75676462013-01-30 14:55:42 +0100206static Scheme_Object *vim_to_mzscheme(typval_T *vim_value);
207static Scheme_Object *vim_to_mzscheme_impl(typval_T *vim_value, int depth,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000208 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100209static int mzscheme_to_vim(Scheme_Object *obj, typval_T *tv);
210static int mzscheme_to_vim_impl(Scheme_Object *obj, typval_T *tv, int depth,
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100211 Scheme_Hash_Table *visited);
Bram Moolenaar75676462013-01-30 14:55:42 +0100212static Scheme_Object *vim_funcref(void *data, int argc, Scheme_Object **argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000213#endif
214
215#ifdef MZ_PRECISE_GC
Bram Moolenaar64404472010-06-26 06:24:45 +0200216static int buffer_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000217{
218 return gcBYTES_TO_WORDS(sizeof(vim_mz_buffer));
219}
220static int buffer_mark_proc(void *obj)
221{
222 return buffer_size_proc(obj);
223}
224static int buffer_fixup_proc(void *obj)
225{
Bram Moolenaar75676462013-01-30 14:55:42 +0100226 /* apparently not needed as the object will be uncollectable while
227 * the buffer is alive
228 */
229 /*
230 vim_mz_buffer* buf = (vim_mz_buffer*) obj;
231 buf->buf->b_mzscheme_ref = GC_fixup_self(obj);
232 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000233 return buffer_size_proc(obj);
234}
Bram Moolenaar64404472010-06-26 06:24:45 +0200235static int window_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000236{
237 return gcBYTES_TO_WORDS(sizeof(vim_mz_window));
238}
239static int window_mark_proc(void *obj)
240{
241 return window_size_proc(obj);
242}
243static int window_fixup_proc(void *obj)
244{
Bram Moolenaar75676462013-01-30 14:55:42 +0100245 /* apparently not needed as the object will be uncollectable while
246 * the window is alive
247 */
248 /*
249 vim_mz_window* win = (vim_mz_window*) obj;
250 win->win->w_mzscheme_ref = GC_fixup_self(obj);
251 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000252 return window_size_proc(obj);
253}
Bram Moolenaar75676462013-01-30 14:55:42 +0100254/* with precise GC, w_mzscheme_ref and b_mzscheme_ref are immobile boxes
255 * containing pointers to a window/buffer
256 * with conservative GC these are simply pointers*/
257# define WINDOW_REF(win) *(vim_mz_window **)((win)->w_mzscheme_ref)
258# define BUFFER_REF(buf) *(vim_mz_buffer **)((buf)->b_mzscheme_ref)
259#else
260# define WINDOW_REF(win) (vim_mz_window *)((win)->w_mzscheme_ref)
261# define BUFFER_REF(buf) (vim_mz_buffer *)((buf)->b_mzscheme_ref)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000262#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000263
Bram Moolenaar4349c572016-01-30 13:28:28 +0100264#if defined(DYNAMIC_MZSCHEME) || defined(PROTO)
Bram Moolenaar33570922005-01-25 22:26:29 +0000265static Scheme_Object *dll_scheme_eof;
266static Scheme_Object *dll_scheme_false;
267static Scheme_Object *dll_scheme_void;
268static Scheme_Object *dll_scheme_null;
269static Scheme_Object *dll_scheme_true;
270
271static Scheme_Thread **dll_scheme_current_thread_ptr;
272
273static void (**dll_scheme_console_printf_ptr)(char *str, ...);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100274static void (**dll_scheme_console_output_ptr)(char *str, OUTPUT_LEN_TYPE len);
Bram Moolenaar33570922005-01-25 22:26:29 +0000275static void (**dll_scheme_notify_multithread_ptr)(int on);
276
277static void *(*dll_GC_malloc)(size_t size_in_bytes);
278static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
279static Scheme_Env *(*dll_scheme_basic_env)(void);
280static void (*dll_scheme_check_threads)(void);
281static void (*dll_scheme_register_static)(void *ptr, long size);
282static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
283static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
284 Scheme_Env *env);
285static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
286 Scheme_Object *val, Scheme_Env *env);
287static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
288 Scheme_Object **rands);
289static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000290# if MZSCHEME_VERSION_MAJOR >= 299
291static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100292static Scheme_Object *(*dll_scheme_make_path)(const char *chars);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000293# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000294static void (*dll_scheme_close_input_port)(Scheme_Object *port);
295static void (*dll_scheme_count_lines)(Scheme_Object *port);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000296#if MZSCHEME_VERSION_MAJOR < 360
Bram Moolenaar33570922005-01-25 22:26:29 +0000297static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000298#else
299static Scheme_Object *(*dll_scheme_current_continuation_marks)(Scheme_Object *prompt_tag);
300#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000301static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100302static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, OUTPUT_LEN_TYPE *len);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000303static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
Bram Moolenaar33570922005-01-25 22:26:29 +0000304static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
305 int _num_rands, Scheme_Object **rands, int val);
306static void (*dll_scheme_dont_gc_ptr)(void *p);
307static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
308static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
309 Scheme_Env *env);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000310static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
Bram Moolenaar33570922005-01-25 22:26:29 +0000311 Scheme_Env *env, int all);
312static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000313# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000314static char *(*dll_scheme_format)(char *format, int flen, int argc,
315 Scheme_Object **argv, long *rlen);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000316# else
317static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100318 Scheme_Object **argv, OUTPUT_LEN_TYPE *rlen);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000319static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000320# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000321static void (*dll_scheme_gc_ptr_ok)(void *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000322# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000323static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
324 long *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000325# else
326static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100327 OUTPUT_LEN_TYPE *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000328# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000329static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
330static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
331 Scheme_Env *env);
332static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
333 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
334 mzshort maxa);
335static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000336static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
Bram Moolenaar33570922005-01-25 22:26:29 +0000337 Scheme_Object *cdr);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000338static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
339 const char *name, mzshort mina, mzshort maxa);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000340# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000341static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
342static Scheme_Object *(*dll_scheme_make_string_output_port)();
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000343# else
344static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
345static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
346# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000347static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
348 int argc, Scheme_Object **argv);
349static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
350 Scheme_Object *field_names, int flags, int *count_out);
351static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
352 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
353 int num_uninit_fields, Scheme_Object *uninit_val,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000354 Scheme_Object *properties
355# if MZSCHEME_VERSION_MAJOR >= 299
356 , Scheme_Object *guard
357# endif
358 );
Bram Moolenaar33570922005-01-25 22:26:29 +0000359static Scheme_Object **(*dll_scheme_make_struct_values)(
360 Scheme_Object *struct_type, Scheme_Object **names, int count,
361 int flags);
362static Scheme_Type (*dll_scheme_make_type)(const char *name);
363static Scheme_Object *(*dll_scheme_make_vector)(int size,
364 Scheme_Object *fill);
365static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
366static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
367 const char *who);
368static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
369 Scheme_Env *for_env);
370static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
371static void (*dll_scheme_raise)(Scheme_Object *exn);
372static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
373static void (*dll_scheme_signal_error)(const char *msg, ...);
374static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
375 int which, int argc, Scheme_Object **argv);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000376# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000377static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000378 Scheme_Object *o);
379static Scheme_Config *(*dll_scheme_current_config)(void);
380static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
381 (Scheme_Object *s);
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000382static Scheme_Object *(*dll_scheme_char_string_to_path)
383 (Scheme_Object *s);
Bram Moolenaar75676462013-01-30 14:55:42 +0100384static void *(*dll_scheme_set_collects_path)(Scheme_Object *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000385# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000386static Scheme_Hash_Table *(*dll_scheme_make_hash_table)(int type);
387static void (*dll_scheme_hash_set)(Scheme_Hash_Table *table,
388 Scheme_Object *key, Scheme_Object *value);
389static Scheme_Object *(*dll_scheme_hash_get)(Scheme_Hash_Table *table,
390 Scheme_Object *key);
391static Scheme_Object *(*dll_scheme_make_double)(double d);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000392static Scheme_Object *(*dll_scheme_make_sized_byte_string)(char *chars,
393 long len, int copy);
394static Scheme_Object *(*dll_scheme_namespace_require)(Scheme_Object *req);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100395static Scheme_Object *(*dll_scheme_dynamic_wind)(void (*pre)(void *), Scheme_Object *(* volatile act)(void *), void (* volatile post)(void *), Scheme_Object *(*jmp_handler)(void *), void * volatile data);
396# ifdef MZ_PRECISE_GC
397static void *(*dll_GC_malloc_one_tagged)(size_t size_in_bytes);
398static void (*dll_GC_register_traversers)(short tag, Size_Proc size, Mark_Proc mark, Fixup_Proc fixup, int is_constant_size, int is_atomic);
399# endif
400# if MZSCHEME_VERSION_MAJOR >= 400
401static void (*dll_scheme_init_collection_paths)(Scheme_Env *global_env, Scheme_Object *extra_dirs);
402static void **(*dll_scheme_malloc_immobile_box)(void *p);
403static void (*dll_scheme_free_immobile_box)(void **b);
404# endif
405# if MZSCHEME_VERSION_MAJOR >= 500
406# ifdef TRAMPOLINED_MZVIM_STARTUP
407static int (*dll_scheme_main_setup)(int no_auto_statics, Scheme_Env_Main _main, int argc, char **argv);
408# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
409static void (*dll_scheme_register_tls_space)(void *tls_space, int _tls_index);
410# endif
411# endif
412# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
413static Thread_Local_Variables *(*dll_scheme_external_get_thread_local_variables)(void);
414# endif
415# endif
416# if MZSCHEME_VERSION_MAJOR >= 600
417static void (*dll_scheme_embedded_load)(intptr_t len, const char *s, int predefined);
418static void (*dll_scheme_register_embedded_load)(intptr_t len, const char *s);
419static void (*dll_scheme_set_config_path)(Scheme_Object *p);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000420# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000421
Bram Moolenaar4349c572016-01-30 13:28:28 +0100422#if defined(DYNAMIC_MZSCHEME) /* not when defined(PROTO) */
423
Bram Moolenaar33570922005-01-25 22:26:29 +0000424/* arrays are imported directly */
425# define scheme_eof dll_scheme_eof
426# define scheme_false dll_scheme_false
427# define scheme_void dll_scheme_void
428# define scheme_null dll_scheme_null
429# define scheme_true dll_scheme_true
430
431/* pointers are GetProceAddress'ed as pointers to pointer */
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100432#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
433# define scheme_current_thread (*dll_scheme_current_thread_ptr)
434# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000435# define scheme_console_printf (*dll_scheme_console_printf_ptr)
436# define scheme_console_output (*dll_scheme_console_output_ptr)
437# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
438
439/* and functions in a usual way */
440# define GC_malloc dll_GC_malloc
441# define GC_malloc_atomic dll_GC_malloc_atomic
442
443# define scheme_add_global dll_scheme_add_global
444# define scheme_add_global_symbol dll_scheme_add_global_symbol
445# define scheme_apply dll_scheme_apply
446# define scheme_basic_env dll_scheme_basic_env
447# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000448# if MZSCHEME_VERSION_MAJOR >= 299
449# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100450# define scheme_make_path dll_scheme_make_path
Bram Moolenaar555b2802005-05-19 21:08:39 +0000451# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000452# define scheme_check_threads dll_scheme_check_threads
453# define scheme_close_input_port dll_scheme_close_input_port
454# define scheme_count_lines dll_scheme_count_lines
455# define scheme_current_continuation_marks \
456 dll_scheme_current_continuation_marks
457# define scheme_display dll_scheme_display
458# define scheme_display_to_string dll_scheme_display_to_string
459# define scheme_do_eval dll_scheme_do_eval
460# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000461# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000462# define scheme_eval dll_scheme_eval
463# define scheme_eval_string dll_scheme_eval_string
464# define scheme_eval_string_all dll_scheme_eval_string_all
465# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000466# if MZSCHEME_VERSION_MAJOR < 299
467# define scheme_format dll_scheme_format
468# else
469# define scheme_format_utf8 dll_scheme_format_utf8
470# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000472# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100473# define scheme_get_sized_byte_string_output dll_scheme_get_sized_string_output
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000474# else
475# define scheme_get_sized_byte_string_output \
476 dll_scheme_get_sized_byte_string_output
Bram Moolenaar75676462013-01-30 14:55:42 +0100477# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000478# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479# define scheme_intern_symbol dll_scheme_intern_symbol
480# define scheme_lookup_global dll_scheme_lookup_global
481# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
482# define scheme_make_integer_value dll_scheme_make_integer_value
Bram Moolenaar33570922005-01-25 22:26:29 +0000483# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000484# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000485# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar75676462013-01-30 14:55:42 +0100486# define scheme_make_byte_string dll_scheme_make_string
487# define scheme_make_byte_string_output_port dll_scheme_make_string_output_port
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000488# else
489# define scheme_make_byte_string dll_scheme_make_byte_string
490# define scheme_make_byte_string_output_port \
491 dll_scheme_make_byte_string_output_port
492# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000493# define scheme_make_struct_instance dll_scheme_make_struct_instance
494# define scheme_make_struct_names dll_scheme_make_struct_names
495# define scheme_make_struct_type dll_scheme_make_struct_type
496# define scheme_make_struct_values dll_scheme_make_struct_values
497# define scheme_make_type dll_scheme_make_type
498# define scheme_make_vector dll_scheme_make_vector
499# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
500# define scheme_open_input_file dll_scheme_open_input_file
501# define scheme_primitive_module dll_scheme_primitive_module
502# define scheme_proper_list_length dll_scheme_proper_list_length
503# define scheme_raise dll_scheme_raise
504# define scheme_read dll_scheme_read
505# define scheme_register_static dll_scheme_register_static
506# define scheme_set_stack_base dll_scheme_set_stack_base
507# define scheme_signal_error dll_scheme_signal_error
508# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000509# if MZSCHEME_VERSION_MAJOR >= 299
510# define scheme_set_param dll_scheme_set_param
511# define scheme_current_config dll_scheme_current_config
512# define scheme_char_string_to_byte_string \
513 dll_scheme_char_string_to_byte_string
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000514# define scheme_char_string_to_path \
515 dll_scheme_char_string_to_path
Bram Moolenaar75676462013-01-30 14:55:42 +0100516# define scheme_set_collects_path dll_scheme_set_collects_path
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000517# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000518# define scheme_make_hash_table dll_scheme_make_hash_table
519# define scheme_hash_set dll_scheme_hash_set
520# define scheme_hash_get dll_scheme_hash_get
521# define scheme_make_double dll_scheme_make_double
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100522# define scheme_make_sized_byte_string dll_scheme_make_sized_byte_string
523# define scheme_namespace_require dll_scheme_namespace_require
524# define scheme_dynamic_wind dll_scheme_dynamic_wind
525# ifdef MZ_PRECISE_GC
526# define GC_malloc_one_tagged dll_GC_malloc_one_tagged
527# define GC_register_traversers dll_GC_register_traversers
528# endif
529# if MZSCHEME_VERSION_MAJOR >= 400
530# ifdef TRAMPOLINED_MZVIM_STARTUP
531# define scheme_main_setup dll_scheme_main_setup
532# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
533# define scheme_register_tls_space dll_scheme_register_tls_space
534# endif
535# endif
536# define scheme_init_collection_paths dll_scheme_init_collection_paths
537# define scheme_malloc_immobile_box dll_scheme_malloc_immobile_box
538# define scheme_free_immobile_box dll_scheme_free_immobile_box
539# endif
540# if MZSCHEME_VERSION_MAJOR >= 600
541# define scheme_embedded_load dll_scheme_embedded_load
542# define scheme_register_embedded_load dll_scheme_register_embedded_load
543# define scheme_set_config_path dll_scheme_set_config_path
544# endif
545
546# if MZSCHEME_VERSION_MAJOR >= 500
547# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200548/* define as function for macro in schthread.h */
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100549Thread_Local_Variables *
550scheme_external_get_thread_local_variables(void)
551{
552 return dll_scheme_external_get_thread_local_variables();
553}
554# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000555# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000556
Bram Moolenaar4349c572016-01-30 13:28:28 +0100557#endif
558
Bram Moolenaar33570922005-01-25 22:26:29 +0000559typedef struct
560{
561 char *name;
562 void **ptr;
563} Thunk_Info;
564
565static Thunk_Info mzgc_imports[] = {
566 {"GC_malloc", (void **)&dll_GC_malloc},
567 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
568 {NULL, NULL}};
569
570static Thunk_Info mzsch_imports[] = {
571 {"scheme_eof", (void **)&dll_scheme_eof},
572 {"scheme_false", (void **)&dll_scheme_false},
573 {"scheme_void", (void **)&dll_scheme_void},
574 {"scheme_null", (void **)&dll_scheme_null},
575 {"scheme_true", (void **)&dll_scheme_true},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100576#if !defined(USE_THREAD_LOCAL) && !defined(LINK_EXTENSIONS_BY_TABLE)
Bram Moolenaar33570922005-01-25 22:26:29 +0000577 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100578#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000579 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
580 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000581 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000582 (void **)&dll_scheme_notify_multithread_ptr},
583 {"scheme_add_global", (void **)&dll_scheme_add_global},
584 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
585 {"scheme_apply", (void **)&dll_scheme_apply},
586 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000587# if MZSCHEME_VERSION_MAJOR >= 299
588 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100589 {"scheme_make_path", (void **)&dll_scheme_make_path},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000590# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000591 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
592 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
593 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
594 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000595 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000596 (void **)&dll_scheme_current_continuation_marks},
597 {"scheme_display", (void **)&dll_scheme_display},
598 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
599 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
600 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000601 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000602 {"scheme_eval", (void **)&dll_scheme_eval},
603 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
604 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000605 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000606 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000607# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000608 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000609# else
610 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000611 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000613 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000614# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000615 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000617# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000618 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000619 (void **)&dll_scheme_get_sized_byte_string_output},
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
622 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000623 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000624 (void **)&dll_scheme_make_closed_prim_w_arity},
625 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
Bram Moolenaar33570922005-01-25 22:26:29 +0000626 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000627 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000628# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000629 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000630 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000631 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000632# else
633 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000634 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000635 (void **)&dll_scheme_make_byte_string_output_port},
636# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000637 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000638 (void **)&dll_scheme_make_struct_instance},
639 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
640 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
641 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
642 {"scheme_make_type", (void **)&dll_scheme_make_type},
643 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
644 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
645 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
646 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
647 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
648 {"scheme_raise", (void **)&dll_scheme_raise},
649 {"scheme_read", (void **)&dll_scheme_read},
650 {"scheme_register_static", (void **)&dll_scheme_register_static},
651 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
652 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
653 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000654# if MZSCHEME_VERSION_MAJOR >= 299
655 {"scheme_set_param", (void **)&dll_scheme_set_param},
656 {"scheme_current_config", (void **)&dll_scheme_current_config},
657 {"scheme_char_string_to_byte_string",
658 (void **)&dll_scheme_char_string_to_byte_string},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000659 {"scheme_char_string_to_path", (void **)&dll_scheme_char_string_to_path},
Bram Moolenaar75676462013-01-30 14:55:42 +0100660 {"scheme_set_collects_path", (void **)&dll_scheme_set_collects_path},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000661# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000662 {"scheme_make_hash_table", (void **)&dll_scheme_make_hash_table},
663 {"scheme_hash_set", (void **)&dll_scheme_hash_set},
664 {"scheme_hash_get", (void **)&dll_scheme_hash_get},
665 {"scheme_make_double", (void **)&dll_scheme_make_double},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000666 {"scheme_make_sized_byte_string", (void **)&dll_scheme_make_sized_byte_string},
667 {"scheme_namespace_require", (void **)&dll_scheme_namespace_require},
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100668 {"scheme_dynamic_wind", (void **)&dll_scheme_dynamic_wind},
669# ifdef MZ_PRECISE_GC
670 {"GC_malloc_one_tagged", (void **)&dll_GC_malloc_one_tagged},
671 {"GC_register_traversers", (void **)&dll_GC_register_traversers},
672# endif
673# if MZSCHEME_VERSION_MAJOR >= 400
674# ifdef TRAMPOLINED_MZVIM_STARTUP
675 {"scheme_main_setup", (void **)&dll_scheme_main_setup},
676# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || MZSCHEME_VERSION_MAJOR >= 603
677 {"scheme_register_tls_space", (void **)&dll_scheme_register_tls_space},
678# endif
679# endif
680 {"scheme_init_collection_paths", (void **)&dll_scheme_init_collection_paths},
681 {"scheme_malloc_immobile_box", (void **)&dll_scheme_malloc_immobile_box},
682 {"scheme_free_immobile_box", (void **)&dll_scheme_free_immobile_box},
683# endif
684# if MZSCHEME_VERSION_MAJOR >= 500
685# if defined(IMPLEMENT_THREAD_LOCAL_VIA_WIN_TLS) || defined(IMPLEMENT_THREAD_LOCAL_EXTERNALLY_VIA_PROC)
686 {"scheme_external_get_thread_local_variables", (void **)&dll_scheme_external_get_thread_local_variables},
687# endif
688# endif
689# if MZSCHEME_VERSION_MAJOR >= 600
690 {"scheme_embedded_load", (void **)&dll_scheme_embedded_load},
691 {"scheme_register_embedded_load", (void **)&dll_scheme_register_embedded_load},
692 {"scheme_set_config_path", (void **)&dll_scheme_set_config_path},
693# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000694 {NULL, NULL}};
695
696static HINSTANCE hMzGC = 0;
697static HINSTANCE hMzSch = 0;
698
699static void dynamic_mzscheme_end(void);
700static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
701 int verbose);
702
703 static int
704mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
705{
706 Thunk_Info *thunk = NULL;
707
708 if (hMzGC && hMzSch)
709 return OK;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200710 hMzSch = vimLoadLib(sch_dll);
711 hMzGC = vimLoadLib(gc_dll);
Bram Moolenaar33570922005-01-25 22:26:29 +0000712
Bram Moolenaar33570922005-01-25 22:26:29 +0000713 if (!hMzGC)
714 {
715 if (verbose)
716 EMSG2(_(e_loadlib), gc_dll);
717 return FAIL;
718 }
719
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100720 if (!hMzSch)
721 {
722 if (verbose)
723 EMSG2(_(e_loadlib), sch_dll);
724 return FAIL;
725 }
726
Bram Moolenaar33570922005-01-25 22:26:29 +0000727 for (thunk = mzsch_imports; thunk->name; thunk++)
728 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000729 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
731 {
732 FreeLibrary(hMzSch);
733 hMzSch = 0;
734 FreeLibrary(hMzGC);
735 hMzGC = 0;
736 if (verbose)
737 EMSG2(_(e_loadfunc), thunk->name);
738 return FAIL;
739 }
740 }
741 for (thunk = mzgc_imports; thunk->name; thunk++)
742 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000743 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000744 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
745 {
746 FreeLibrary(hMzSch);
747 hMzSch = 0;
748 FreeLibrary(hMzGC);
749 hMzGC = 0;
750 if (verbose)
751 EMSG2(_(e_loadfunc), thunk->name);
752 return FAIL;
753 }
754 }
755 return OK;
756}
757
758 int
759mzscheme_enabled(int verbose)
760{
761 return mzscheme_runtime_link_init(
762 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
763}
764
765 static void
766dynamic_mzscheme_end(void)
767{
768 if (hMzSch)
769 {
770 FreeLibrary(hMzSch);
771 hMzSch = 0;
772 }
773 if (hMzGC)
774 {
775 FreeLibrary(hMzGC);
776 hMzGC = 0;
777 }
778}
779#endif /* DYNAMIC_MZSCHEME */
780
Bram Moolenaar75676462013-01-30 14:55:42 +0100781#if MZSCHEME_VERSION_MAJOR < 299
782# define GUARANTEED_STRING_ARG(proc, num) GUARANTEE_STRING(proc, num)
783#else
784 static Scheme_Object *
785guaranteed_byte_string_arg(char *proc, int num, int argc, Scheme_Object **argv)
786{
787 if (SCHEME_BYTE_STRINGP(argv[num]))
788 {
789 return argv[num];
790 }
791 else if (SCHEME_CHAR_STRINGP(argv[num]))
792 {
793 Scheme_Object *tmp = NULL;
794 MZ_GC_DECL_REG(2);
795 MZ_GC_VAR_IN_REG(0, argv[num]);
796 MZ_GC_VAR_IN_REG(1, tmp);
797 MZ_GC_REG();
798 tmp = scheme_char_string_to_byte_string(argv[num]);
799 MZ_GC_UNREG();
800 return tmp;
801 }
802 else
803 scheme_wrong_type(proc, "string", num, argc, argv);
804 /* unreachable */
805 return scheme_void;
806}
807# define GUARANTEED_STRING_ARG(proc, num) guaranteed_byte_string_arg(proc, num, argc, argv)
808#endif
809
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000810/* need to put it here for dynamic stuff to work */
Bram Moolenaare484c942009-09-11 10:21:41 +0000811#if defined(INCLUDE_MZSCHEME_BASE)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000812# include "mzscheme_base.c"
813#endif
814
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000815/*
816 *========================================================================
817 * 1. MzScheme interpreter startup
818 *========================================================================
819 */
820
821static Scheme_Type mz_buffer_type;
822static Scheme_Type mz_window_type;
823
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000824static int initialized = FALSE;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100825#ifdef DYNAMIC_MZSCHEME
826static int disabled = FALSE;
827#endif
828static int load_base_module_failed = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000829
830/* global environment */
831static Scheme_Env *environment = NULL;
832/* output/error handlers */
833static Scheme_Object *curout = NULL;
834static Scheme_Object *curerr = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000835/* exn:vim exception */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000836static Scheme_Object *exn_catching_apply = NULL;
837static Scheme_Object *exn_p = NULL;
838static Scheme_Object *exn_message = NULL;
839static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000840
841#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
842static void *stack_base = NULL;
843#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000844
845static long range_start;
846static long range_end;
847
848/* MzScheme threads scheduling stuff */
849static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000850
851#if defined(FEAT_GUI_W32)
Bram Moolenaardec6c7b2016-06-02 11:57:38 +0200852static void CALLBACK timer_proc(HWND, UINT, UINT_PTR, DWORD);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000853static UINT timer_id = 0;
854#elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100855# if GTK_CHECK_VERSION(3,0,0)
856static gboolean timer_proc(gpointer);
857# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000858static gint timer_proc(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100859# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000860static guint timer_id = 0;
861#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
862static void timer_proc(XtPointer, XtIntervalId *);
863static XtIntervalId timer_id = (XtIntervalId)0;
864#elif defined(FEAT_GUI_MAC)
865pascal void timer_proc(EventLoopTimerRef, void *);
866static EventLoopTimerRef timer_id = NULL;
867static EventLoopTimerUPP timerUPP;
868#endif
869
870#ifndef FEAT_GUI_W32 /* Win32 console and Unix */
871 void
872mzvim_check_threads(void)
873{
874 /* Last time MzScheme threads were scheduled */
875 static time_t mz_last_time = 0;
876
877 if (mz_threads_allow && p_mzq > 0)
878 {
879 time_t now = time(NULL);
880
881 if ((now - mz_last_time) * 1000 > p_mzq)
882 {
883 mz_last_time = now;
884 scheme_check_threads();
885 }
886 }
887}
888#endif
889
Bram Moolenaar4349c572016-01-30 13:28:28 +0100890#if defined(MZSCHEME_GUI_THREADS) || defined(PROTO)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000891static void setup_timer(void);
892static void remove_timer(void);
893
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000894/* timers are presented in GUI only */
895# if defined(FEAT_GUI_W32)
896 static void CALLBACK
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200897timer_proc(HWND hwnd UNUSED, UINT uMsg UNUSED, UINT_PTR idEvent UNUSED, DWORD dwTime UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000898# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100899# if GTK_CHECK_VERSION(3,0,0)
900 static gboolean
901# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000902 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100903# endif
Bram Moolenaar64404472010-06-26 06:24:45 +0200904timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000905# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000906 static void
Bram Moolenaar64404472010-06-26 06:24:45 +0200907timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000908# elif defined(FEAT_GUI_MAC)
909 pascal void
Bram Moolenaar64404472010-06-26 06:24:45 +0200910timer_proc(EventLoopTimerRef theTimer UNUSED, void *userData UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000911# endif
912{
913 scheme_check_threads();
914# if defined(FEAT_GUI_GTK)
915 return TRUE; /* continue receiving notifications */
916# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
917 /* renew timeout */
918 if (mz_threads_allow && p_mzq > 0)
919 timer_id = XtAppAddTimeOut(app_context, p_mzq,
920 timer_proc, NULL);
921# endif
922}
923
924 static void
925setup_timer(void)
926{
927# if defined(FEAT_GUI_W32)
928 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
929# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100930# if GTK_CHECK_VERSION(3,0,0)
931 timer_id = g_timeout_add((guint)p_mzq, (GSourceFunc)timer_proc, NULL);
932# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000933 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100934# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000935# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
936 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
937# elif defined(FEAT_GUI_MAC)
938 timerUPP = NewEventLoopTimerUPP(timer_proc);
939 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
940 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
941# endif
942}
943
944 static void
945remove_timer(void)
946{
947# if defined(FEAT_GUI_W32)
948 KillTimer(NULL, timer_id);
949# elif defined(FEAT_GUI_GTK)
Bram Moolenaar98921892016-02-23 17:14:37 +0100950# if GTK_CHECK_VERSION(3,0,0)
951 g_source_remove(timer_id);
952# else
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000953 gtk_timeout_remove(timer_id);
Bram Moolenaar98921892016-02-23 17:14:37 +0100954# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000955# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
956 XtRemoveTimeOut(timer_id);
957# elif defined(FEAT_GUI_MAC)
958 RemoveEventLoopTimer(timer_id);
959 DisposeEventLoopTimerUPP(timerUPP);
960# endif
961 timer_id = 0;
962}
963
964 void
965mzvim_reset_timer(void)
966{
967 if (timer_id != 0)
968 remove_timer();
969 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
970 setup_timer();
971}
972
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000973#endif /* MZSCHEME_GUI_THREADS */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000974
975 static void
976notify_multithread(int on)
977{
978 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000979#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000980 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
981 setup_timer();
982 if (!on && timer_id != 0)
983 remove_timer();
984#endif
985}
986
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000987 void
988mzscheme_end(void)
989{
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100990 /* We can not unload the DLL. Racket's thread might be still alive. */
991#if 0
Bram Moolenaar33570922005-01-25 22:26:29 +0000992#ifdef DYNAMIC_MZSCHEME
993 dynamic_mzscheme_end();
994#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100995#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000996}
997
Bram Moolenaar4e640bd2016-01-16 16:20:38 +0100998#if HAVE_TLS_SPACE
999# if defined(_MSC_VER)
Bram Moolenaar2d0860d2010-11-03 21:59:30 +01001000static __declspec(thread) void *tls_space;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001001extern intptr_t _tls_index;
1002# elif defined(__MINGW32__)
1003static __thread void *tls_space;
1004extern intptr_t _tls_index;
1005# else
1006static THREAD_LOCAL void *tls_space;
1007static intptr_t _tls_index = 0;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001008# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001009#endif
1010
1011 int
1012mzscheme_main(int argc, char** argv)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001013{
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001014#ifdef DYNAMIC_MZSCHEME
1015 /*
1016 * Racket requires trampolined startup. We can not load it later.
1017 * If dynamic dll loading is failed, disable it.
1018 */
1019 if (!mzscheme_enabled(FALSE))
1020 {
1021 disabled = TRUE;
1022 return vim_main2(argc, argv);
1023 }
1024#endif
Bram Moolenaar5c5c9802015-07-10 16:12:48 +02001025#ifdef HAVE_TLS_SPACE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001026 scheme_register_tls_space(&tls_space, _tls_index);
Bram Moolenaar2d0860d2010-11-03 21:59:30 +01001027#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001028#ifdef TRAMPOLINED_MZVIM_STARTUP
1029 return scheme_main_setup(TRUE, mzscheme_env_main, argc, argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001030#else
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001031 return mzscheme_env_main(NULL, argc, argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001032#endif
1033}
1034
1035 static int
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001036mzscheme_env_main(Scheme_Env *env, int argc, char **argv)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001037{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001038 int vim_main_result;
1039#ifdef TRAMPOLINED_MZVIM_STARTUP
1040 /* Scheme has created the environment for us */
1041 environment = env;
1042#else
1043# ifdef MZ_PRECISE_GC
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001044 Scheme_Object *dummy = NULL;
1045 MZ_GC_DECL_REG(1);
1046 MZ_GC_VAR_IN_REG(0, dummy);
1047
1048 stack_base = &__gc_var_stack__;
1049# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001050 int dummy = 0;
1051 stack_base = (void *)&dummy;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001052# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001053#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001054
1055 /* mzscheme_main is called as a trampoline from main.
1056 * We trampoline into vim_main2
1057 * Passing argc, argv through from mzscheme_main
1058 */
1059 vim_main_result = vim_main2(argc, argv);
1060#if !defined(TRAMPOLINED_MZVIM_STARTUP) && defined(MZ_PRECISE_GC)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001061 /* releasing dummy */
1062 MZ_GC_REG();
1063 MZ_GC_UNREG();
1064#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001065 return vim_main_result;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001066}
1067
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001068 static Scheme_Object*
1069load_base_module(void *data)
1070{
1071 scheme_namespace_require(scheme_intern_symbol((char *)data));
1072 return scheme_null;
1073}
1074
1075 static Scheme_Object *
Bram Moolenaar8b29aba2016-03-28 22:17:16 +02001076load_base_module_on_error(void *data UNUSED)
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001077{
1078 load_base_module_failed = TRUE;
1079 return scheme_null;
1080}
1081
1082 static int
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001083startup_mzscheme(void)
1084{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001085#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001086 scheme_set_stack_base(stack_base, 1);
1087#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001088
Bram Moolenaar75676462013-01-30 14:55:42 +01001089#ifndef TRAMPOLINED_MZVIM_STARTUP
1090 /* in newer versions of precise GC the initial env has been created */
1091 environment = scheme_basic_env();
1092#endif
1093
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001094 MZ_REGISTER_STATIC(environment);
1095 MZ_REGISTER_STATIC(curout);
1096 MZ_REGISTER_STATIC(curerr);
1097 MZ_REGISTER_STATIC(exn_catching_apply);
1098 MZ_REGISTER_STATIC(exn_p);
1099 MZ_REGISTER_STATIC(exn_message);
1100 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001101
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001102 MZ_GC_CHECK();
1103
1104#ifdef INCLUDE_MZSCHEME_BASE
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001105 /* invoke function from generated and included mzscheme_base.c */
1106 declare_modules(environment);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001107#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001108
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001109 /* setup 'current-library-collection-paths' parameter */
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001110# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001111 {
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001112 Scheme_Object *coll_path = NULL;
1113 int mustfree = FALSE;
1114 char_u *s;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001115
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001116 MZ_GC_DECL_REG(1);
1117 MZ_GC_VAR_IN_REG(0, coll_path);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001118 MZ_GC_REG();
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001119 /* workaround for dynamic loading on windows */
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001120 s = vim_getenv((char_u *)"PLTCOLLECTS", &mustfree);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001121 if (s != NULL)
1122 {
Bram Moolenaar74a97b12016-02-18 21:19:21 +01001123 coll_path = scheme_make_path((char *)s);
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001124 MZ_GC_CHECK();
1125 if (mustfree)
1126 vim_free(s);
1127 }
1128# ifdef MZSCHEME_COLLECTS
1129 if (coll_path == NULL)
1130 {
1131 coll_path = scheme_make_path(MZSCHEME_COLLECTS);
1132 MZ_GC_CHECK();
1133 }
Bram Moolenaar39d7d512013-01-31 21:09:15 +01001134# endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001135 if (coll_path != NULL)
1136 {
1137 scheme_set_collects_path(coll_path);
1138 MZ_GC_CHECK();
1139 }
1140 MZ_GC_UNREG();
1141 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001142# else
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001143# ifdef MZSCHEME_COLLECTS
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001144 {
1145 Scheme_Object *coll_string = NULL;
1146 Scheme_Object *coll_pair = NULL;
1147 Scheme_Config *config = NULL;
1148
1149 MZ_GC_DECL_REG(3);
1150 MZ_GC_VAR_IN_REG(0, coll_string);
1151 MZ_GC_VAR_IN_REG(1, coll_pair);
1152 MZ_GC_VAR_IN_REG(2, config);
1153 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001154 coll_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001155 MZ_GC_CHECK();
1156 coll_pair = scheme_make_pair(coll_string, scheme_null);
1157 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001158 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001159 MZ_GC_CHECK();
1160 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
1161 MZ_GC_CHECK();
1162 MZ_GC_UNREG();
1163 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +00001164# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001165#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001166
1167# if MZSCHEME_VERSION_MAJOR >= 600
1168 {
1169 Scheme_Object *config_path = NULL;
1170 int mustfree = FALSE;
1171 char_u *s;
1172
1173 MZ_GC_DECL_REG(1);
1174 MZ_GC_VAR_IN_REG(0, config_path);
1175 MZ_GC_REG();
1176 /* workaround for dynamic loading on windows */
1177 s = vim_getenv("PLTCONFIGDIR", &mustfree);
1178 if (s != NULL)
1179 {
1180 config_path = scheme_make_path(s);
1181 MZ_GC_CHECK();
1182 if (mustfree)
1183 vim_free(s);
1184 }
1185#ifdef MZSCHEME_CONFIGDIR
1186 if (config_path == NULL)
1187 {
1188 config_path = scheme_make_path(MZSCHEME_CONFIGDIR);
1189 MZ_GC_CHECK();
1190 }
1191#endif
1192 if (config_path != NULL)
1193 {
1194 scheme_set_config_path(config_path);
1195 MZ_GC_CHECK();
1196 }
1197 MZ_GC_UNREG();
1198 }
1199# endif
1200
1201#if MZSCHEME_VERSION_MAJOR >= 400
1202 scheme_init_collection_paths(environment, scheme_null);
1203#endif
1204
1205 /*
1206 * versions 4.x do not provide Scheme bindings by default
1207 * we need to add them explicitly
1208 */
1209 {
1210 /* use error handler to avoid abort */
1211 scheme_dynamic_wind(NULL, load_base_module, NULL,
1212 load_base_module_on_error, "racket/base");
1213 if (load_base_module_failed)
1214 {
1215 load_base_module_failed = FALSE;
1216 scheme_dynamic_wind(NULL, load_base_module, NULL,
1217 load_base_module_on_error, "scheme/base");
1218 if (load_base_module_failed)
1219 return -1;
1220 }
1221 }
1222
1223 register_vim_exn();
1224 /* use new environment to initialise exception handling */
1225 init_exn_catching_apply();
1226
1227 /* redirect output */
1228 scheme_console_output = do_output;
1229 scheme_console_printf = do_printf;
1230
Bram Moolenaar555b2802005-05-19 21:08:39 +00001231#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +00001232 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001233 Scheme_Object *make_security_guard = NULL;
1234 MZ_GC_DECL_REG(1);
1235 MZ_GC_VAR_IN_REG(0, make_security_guard);
1236 MZ_GC_REG();
1237
1238#if MZSCHEME_VERSION_MAJOR < 400
1239 {
1240 Scheme_Object *make_security_guard_symbol = NULL;
1241 MZ_GC_DECL_REG(1);
1242 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
1243 MZ_GC_REG();
1244 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
1245 MZ_GC_CHECK();
1246 make_security_guard = scheme_lookup_global(
1247 make_security_guard_symbol, environment);
1248 MZ_GC_UNREG();
1249 }
1250#else
1251 make_security_guard = scheme_builtin_value("make-security-guard");
1252 MZ_GC_CHECK();
1253#endif
1254
1255 /* setup sandbox guards */
1256 if (make_security_guard != NULL)
1257 {
1258 Scheme_Object *args[3] = {NULL, NULL, NULL};
1259 Scheme_Object *guard = NULL;
1260 Scheme_Config *config = NULL;
1261 MZ_GC_DECL_REG(5);
1262 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
1263 MZ_GC_VAR_IN_REG(3, guard);
1264 MZ_GC_VAR_IN_REG(4, config);
1265 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001266 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001267 MZ_GC_CHECK();
1268 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
1269 MZ_GC_CHECK();
1270 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
1271 "sandbox-file-guard", 3, 3);
1272 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1273 "sandbox-network-guard", 4, 4);
1274 guard = scheme_apply(make_security_guard, 3, args);
1275 MZ_GC_CHECK();
1276 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1277 MZ_GC_CHECK();
1278 MZ_GC_UNREG();
1279 }
1280 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001281 }
1282#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001283 /* Create buffer and window types for use in Scheme code */
1284 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001285 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001286 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001287 MZ_GC_CHECK();
1288#ifdef MZ_PRECISE_GC
1289 GC_register_traversers(mz_buffer_type,
1290 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1291 TRUE, TRUE);
1292 GC_register_traversers(mz_window_type,
1293 window_size_proc, window_mark_proc, window_fixup_proc,
1294 TRUE, TRUE);
1295#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001296
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001297 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001298
1299 /*
1300 * setup callback to receive notifications
1301 * whether thread scheduling is (or not) required
1302 */
1303 scheme_notify_multithread = notify_multithread;
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001304
1305 return 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001306}
1307
1308/*
1309 * This routine is called for each new invocation of MzScheme
1310 * to make sure things are properly initialized.
1311 */
1312 static int
1313mzscheme_init(void)
1314{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001315 if (!initialized)
1316 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001317#ifdef DYNAMIC_MZSCHEME
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001318 if (disabled || !mzscheme_enabled(TRUE))
Bram Moolenaar33570922005-01-25 22:26:29 +00001319 {
Bram Moolenaarb849e712009-06-24 15:51:37 +00001320 EMSG(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
Bram Moolenaar33570922005-01-25 22:26:29 +00001321 return -1;
1322 }
1323#endif
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001324 if (load_base_module_failed || startup_mzscheme())
1325 {
Bram Moolenaar9e3be262016-01-24 13:58:40 +01001326 EMSG(_("E895: Sorry, this command is disabled, the MzScheme's racket/base module could not be loaded."));
Bram Moolenaar4e640bd2016-01-16 16:20:38 +01001327 return -1;
1328 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001329 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001330 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001331 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001332 Scheme_Config *config = NULL;
1333 MZ_GC_DECL_REG(1);
1334 MZ_GC_VAR_IN_REG(0, config);
1335 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001336 config = scheme_current_config();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001337 MZ_GC_CHECK();
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001338 /* recreate ports each call effectively clearing these ones */
Bram Moolenaar75676462013-01-30 14:55:42 +01001339 curout = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001340 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001341 curerr = scheme_make_byte_string_output_port();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001342 MZ_GC_CHECK();
1343 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1344 MZ_GC_CHECK();
1345 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1346 MZ_GC_CHECK();
1347 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001348 }
1349
1350 return 0;
1351}
1352
1353/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001354 *========================================================================
1355 * 2. External Interface
1356 *========================================================================
1357 */
1358
1359/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001360 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001361 */
1362 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001363eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001364{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001365 Scheme_Object *value = NULL;
1366 Scheme_Object *exn = NULL;
1367 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001368
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001369 MZ_GC_DECL_REG(3);
1370 MZ_GC_VAR_IN_REG(0, value);
1371 MZ_GC_VAR_IN_REG(1, exn);
1372 MZ_GC_VAR_IN_REG(2, prim);
1373 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001374
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001375 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1376 MZ_GC_CHECK();
1377 value = _apply_thunk_catch_exceptions(prim, &exn);
1378 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001379
1380 if (!value)
1381 {
1382 value = extract_exn_message(exn);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001383 /* Got an exn? */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001384 if (value)
1385 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001386 scheme_display(value, curerr); /* Send to stderr-vim */
1387 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001388 do_flush();
1389 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001390 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001391 /* `raise' was called on some arbitrary value */
1392 return FAIL;
1393 }
1394
1395 if (ret != NULL) /* if pointer to retval supported give it up */
1396 *ret = value;
1397 /* Print any result, as long as it's not a void */
1398 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001399 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001400 scheme_display(value, curout); /* Send to stdout-vim */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001401 MZ_GC_CHECK();
1402 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001403
1404 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001405 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001406 return OK;
1407}
1408
1409/* :mzscheme */
1410 static int
1411do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1412{
1413 if (mzscheme_init())
1414 return FAIL;
1415
1416 range_start = eap->line1;
1417 range_end = eap->line2;
1418
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001419 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001420}
1421
1422/*
1423 * Routine called by VIM when deleting a buffer
1424 */
1425 void
1426mzscheme_buffer_free(buf_T *buf)
1427{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001428 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001429 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001430 vim_mz_buffer *bp = NULL;
1431 MZ_GC_DECL_REG(1);
1432 MZ_GC_VAR_IN_REG(0, bp);
1433 MZ_GC_REG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001434
Bram Moolenaar75676462013-01-30 14:55:42 +01001435 bp = BUFFER_REF(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001436 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001437#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001438 scheme_gc_ptr_ok(bp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001439#else
1440 scheme_free_immobile_box(buf->b_mzscheme_ref);
1441#endif
1442 buf->b_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001443 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001444 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001445 }
1446}
1447
1448/*
1449 * Routine called by VIM when deleting a Window
1450 */
1451 void
1452mzscheme_window_free(win_T *win)
1453{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001454 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001455 {
Bram Moolenaar75676462013-01-30 14:55:42 +01001456 vim_mz_window *wp = NULL;
1457 MZ_GC_DECL_REG(1);
1458 MZ_GC_VAR_IN_REG(0, wp);
1459 MZ_GC_REG();
1460 wp = WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001461 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar75676462013-01-30 14:55:42 +01001462#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001463 scheme_gc_ptr_ok(wp);
Bram Moolenaar75676462013-01-30 14:55:42 +01001464#else
1465 scheme_free_immobile_box(win->w_mzscheme_ref);
1466#endif
1467 win->w_mzscheme_ref = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001468 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001469 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001470 }
1471}
1472
1473/*
1474 * ":mzscheme" (or ":mz")
1475 */
1476 void
1477ex_mzscheme(exarg_T *eap)
1478{
1479 char_u *script;
1480
1481 script = script_get(eap, eap->arg);
1482 if (!eap->skip)
1483 {
1484 if (script == NULL)
1485 do_mzscheme_command(eap, eap->arg, do_eval);
1486 else
1487 {
1488 do_mzscheme_command(eap, script, do_eval);
1489 vim_free(script);
1490 }
1491 }
1492}
1493
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001494 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001495do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001496{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001497 Scheme_Object *expr = NULL;
1498 Scheme_Object *result = NULL;
1499 char *file = NULL;
1500 Port_Info *pinfo = (Port_Info *)data;
1501
1502 MZ_GC_DECL_REG(3);
1503 MZ_GC_VAR_IN_REG(0, expr);
1504 MZ_GC_VAR_IN_REG(1, result);
1505 MZ_GC_VAR_IN_REG(2, file);
1506 MZ_GC_REG();
1507
1508 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1509 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001510
1511 /* make Vim expansion */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001512 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001513 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001514 MZ_GC_CHECK();
1515 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1516 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001517
1518 /* Like REPL but print only last result */
1519 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001520 {
1521 result = scheme_eval(expr, environment);
1522 MZ_GC_CHECK();
1523 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001524
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001525 /* errors will be caught in do_mzscheme_command and ex_mzfile */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001526 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001527 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001528 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001529 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001530 return result;
1531}
1532
1533/* :mzfile */
1534 void
1535ex_mzfile(exarg_T *eap)
1536{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001537 Port_Info pinfo = {NULL, NULL};
1538
1539 MZ_GC_DECL_REG(1);
1540 MZ_GC_VAR_IN_REG(0, pinfo.port);
1541 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001542
1543 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001544 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1545 && pinfo.port != NULL) /* looks like port was not closed */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001546 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001547 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001548 MZ_GC_CHECK();
1549 }
1550 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001551}
1552
1553
1554/*
1555 *========================================================================
1556 * Exception handling code -- cribbed form the MzScheme sources and
1557 * Matthew Flatt's "Inside PLT MzScheme" document.
1558 *========================================================================
1559 */
1560 static void
1561init_exn_catching_apply(void)
1562{
1563 if (!exn_catching_apply)
1564 {
1565 char *e =
1566 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001567 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001568 "(cons #t (thunk))))";
1569
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001570 exn_catching_apply = scheme_eval_string(e, environment);
1571 MZ_GC_CHECK();
1572 exn_p = scheme_builtin_value("exn?");
1573 MZ_GC_CHECK();
1574 exn_message = scheme_builtin_value("exn-message");
1575 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001576 }
1577}
1578
1579/*
1580 * This function applies a thunk, returning the Scheme value if there's
1581 * no exception, otherwise returning NULL and setting *exn to the raised
1582 * value (usually an exn structure).
1583 */
1584 static Scheme_Object *
1585_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1586{
1587 Scheme_Object *v;
1588
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001589 v = _scheme_apply(exn_catching_apply, 1, &f);
1590 /* v is a pair: (cons #t value) or (cons #f exn) */
1591
1592 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1593 return SCHEME_CDR(v);
1594 else
1595 {
1596 *exn = SCHEME_CDR(v);
1597 return NULL;
1598 }
1599}
1600
1601 static Scheme_Object *
1602extract_exn_message(Scheme_Object *v)
1603{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001604 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1605 return _scheme_apply(exn_message, 1, &v);
1606 else
1607 return NULL; /* Not an exn structure */
1608}
1609
1610 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001611do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001612{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001613 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001614}
1615
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001616/*
1617 *========================================================================
1618 * 3. MzScheme I/O Handlers
1619 *========================================================================
1620 */
1621 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001622do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001623{
1624 char *p, *prev;
1625
1626 prev = mesg;
1627 p = strchr(prev, '\n');
1628 while (p)
1629 {
1630 *p = '\0';
1631 if (error)
1632 EMSG(prev);
1633 else
1634 MSG(prev);
1635 prev = p + 1;
1636 p = strchr(prev, '\n');
1637 }
1638
1639 if (error)
1640 EMSG(prev);
1641 else
1642 MSG(prev);
1643}
1644
1645 static void
Bram Moolenaar75676462013-01-30 14:55:42 +01001646do_output(char *mesg, OUTPUT_LEN_TYPE len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001647{
Bram Moolenaar02e14d62012-11-28 15:37:51 +01001648 /* TODO: use len, the string may not be NUL terminated */
Bram Moolenaar64404472010-06-26 06:24:45 +02001649 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001650}
1651
1652 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001653do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001654{
Bram Moolenaar64404472010-06-26 06:24:45 +02001655 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001656}
1657
1658 static void
1659do_printf(char *format, ...)
1660{
Bram Moolenaar64404472010-06-26 06:24:45 +02001661 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001662}
1663
1664 static void
1665do_flush(void)
1666{
1667 char *buff;
Bram Moolenaar75676462013-01-30 14:55:42 +01001668 OUTPUT_LEN_TYPE length;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001669
Bram Moolenaar75676462013-01-30 14:55:42 +01001670 buff = scheme_get_sized_byte_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001671 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001672 if (length)
1673 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001674 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001675 return;
1676 }
1677
Bram Moolenaar75676462013-01-30 14:55:42 +01001678 buff = scheme_get_sized_byte_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001679 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001680 if (length)
1681 do_output(buff, length);
1682}
1683
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001684/*
1685 *========================================================================
1686 * 4. Implementation of the Vim Features for MzScheme
1687 *========================================================================
1688 */
1689
1690/* (command {command-string}) */
1691 static Scheme_Object *
1692vim_command(void *data, int argc, Scheme_Object **argv)
1693{
1694 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001695 Scheme_Object *cmd = NULL;
1696 MZ_GC_DECL_REG(1);
1697 MZ_GC_VAR_IN_REG(0, cmd);
1698 MZ_GC_REG();
1699 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001700
1701 /* may be use do_cmdline_cmd? */
Bram Moolenaar75676462013-01-30 14:55:42 +01001702 do_cmdline(BYTE_STRING_VALUE(cmd), NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001703 update_screen(VALID);
1704
Bram Moolenaar75676462013-01-30 14:55:42 +01001705 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001706 raise_if_error();
1707 return scheme_void;
1708}
1709
1710/* (eval {expr-string}) */
1711 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001712vim_eval(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001713{
1714#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001715 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001716 Scheme_Object *result = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001717 typval_T *vim_result;
Bram Moolenaar75676462013-01-30 14:55:42 +01001718 Scheme_Object *expr = NULL;
1719 MZ_GC_DECL_REG(2);
1720 MZ_GC_VAR_IN_REG(0, result);
1721 MZ_GC_VAR_IN_REG(1, expr);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001722 MZ_GC_REG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001723 expr = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001724
Bram Moolenaar75676462013-01-30 14:55:42 +01001725 vim_result = eval_expr(BYTE_STRING_VALUE(expr), NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001726
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001727 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001728 raise_vim_exn(_("invalid expression"));
1729
Bram Moolenaar75676462013-01-30 14:55:42 +01001730 result = vim_to_mzscheme(vim_result);
1731 MZ_GC_CHECK();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001732 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001733
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001734 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001735 return result;
1736#else
1737 raise_vim_exn(_("expressions disabled at compile time"));
1738 /* unreachable */
1739 return scheme_false;
1740#endif
1741}
1742
1743/* (range-start) */
1744 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001745get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001746{
1747 return scheme_make_integer(range_start);
1748}
1749
1750/* (range-end) */
1751 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001752get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001753{
1754 return scheme_make_integer(range_end);
1755}
1756
1757/* (beep) */
1758 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001759mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001760{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001761 vim_beep(BO_LANG);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001762 return scheme_void;
1763}
1764
1765static Scheme_Object *M_global = NULL;
1766
1767/* (get-option {option-name}) [buffer/window] */
1768 static Scheme_Object *
1769get_option(void *data, int argc, Scheme_Object **argv)
1770{
1771 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001772 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001773 char *strval;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001774 int rc;
Bram Moolenaar75676462013-01-30 14:55:42 +01001775 Scheme_Object *rval = NULL;
1776 Scheme_Object *name = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001777 int opt_flags = 0;
1778 buf_T *save_curb = curbuf;
1779 win_T *save_curw = curwin;
1780
Bram Moolenaar75676462013-01-30 14:55:42 +01001781 MZ_GC_DECL_REG(2);
1782 MZ_GC_VAR_IN_REG(0, rval);
1783 MZ_GC_VAR_IN_REG(1, name);
1784 MZ_GC_REG();
1785
1786 name = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001787
1788 if (argc > 1)
1789 {
1790 if (M_global == NULL)
1791 {
1792 MZ_REGISTER_STATIC(M_global);
1793 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001794 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001795 }
1796
1797 if (argv[1] == M_global)
1798 opt_flags = OPT_GLOBAL;
1799 else if (SCHEME_VIMBUFFERP(argv[1]))
1800 {
1801 curbuf = get_valid_buffer(argv[1]);
1802 opt_flags = OPT_LOCAL;
1803 }
1804 else if (SCHEME_VIMWINDOWP(argv[1]))
1805 {
1806 win_T *win = get_valid_window(argv[1]);
1807
1808 curwin = win;
1809 curbuf = win->w_buffer;
1810 opt_flags = OPT_LOCAL;
1811 }
1812 else
1813 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1814 }
1815
Bram Moolenaar75676462013-01-30 14:55:42 +01001816 rc = get_option_value(BYTE_STRING_VALUE(name), &value, (char_u **)&strval, opt_flags);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001817 curbuf = save_curb;
1818 curwin = save_curw;
1819
1820 switch (rc)
1821 {
1822 case 1:
Bram Moolenaar75676462013-01-30 14:55:42 +01001823 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001824 return scheme_make_integer_value(value);
1825 case 0:
Bram Moolenaar75676462013-01-30 14:55:42 +01001826 rval = scheme_make_byte_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001827 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001828 vim_free(strval);
Bram Moolenaar75676462013-01-30 14:55:42 +01001829 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001830 return rval;
1831 case -1:
1832 case -2:
Bram Moolenaar75676462013-01-30 14:55:42 +01001833 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001834 raise_vim_exn(_("hidden option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001835 case -3:
Bram Moolenaar75676462013-01-30 14:55:42 +01001836 MZ_GC_UNREG();
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001837 raise_vim_exn(_("unknown option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001838 }
1839 /* unreachable */
1840 return scheme_void;
1841}
1842
1843/* (set-option {option-changing-string} [buffer/window]) */
1844 static Scheme_Object *
1845set_option(void *data, int argc, Scheme_Object **argv)
1846{
Bram Moolenaar75676462013-01-30 14:55:42 +01001847 char_u *command = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001848 int opt_flags = 0;
1849 buf_T *save_curb = curbuf;
1850 win_T *save_curw = curwin;
1851 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar75676462013-01-30 14:55:42 +01001852 Scheme_Object *cmd = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001853
Bram Moolenaar75676462013-01-30 14:55:42 +01001854 MZ_GC_DECL_REG(1);
1855 MZ_GC_VAR_IN_REG(0, cmd);
1856 MZ_GC_REG();
1857 cmd = GUARANTEED_STRING_ARG(prim->name, 0);
1858
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001859 if (argc > 1)
1860 {
1861 if (M_global == NULL)
1862 {
1863 MZ_REGISTER_STATIC(M_global);
1864 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001865 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001866 }
1867
1868 if (argv[1] == M_global)
1869 opt_flags = OPT_GLOBAL;
1870 else if (SCHEME_VIMBUFFERP(argv[1]))
1871 {
1872 curbuf = get_valid_buffer(argv[1]);
1873 opt_flags = OPT_LOCAL;
1874 }
1875 else if (SCHEME_VIMWINDOWP(argv[1]))
1876 {
1877 win_T *win = get_valid_window(argv[1]);
1878 curwin = win;
1879 curbuf = win->w_buffer;
1880 opt_flags = OPT_LOCAL;
1881 }
1882 else
1883 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1884 }
1885
1886 /* do_set can modify cmd, make copy */
Bram Moolenaar75676462013-01-30 14:55:42 +01001887 command = vim_strsave(BYTE_STRING_VALUE(cmd));
1888 MZ_GC_UNREG();
1889 do_set(command, opt_flags);
1890 vim_free(command);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001891 update_screen(NOT_VALID);
1892 curbuf = save_curb;
1893 curwin = save_curw;
1894 raise_if_error();
1895 return scheme_void;
1896}
1897
1898/*
1899 *===========================================================================
1900 * 5. Vim Window-related Manipulation Functions
1901 *===========================================================================
1902 */
1903
1904/* (curr-win) */
1905 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001906get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001907{
1908 return (Scheme_Object *)get_vim_curr_window();
1909}
1910
1911/* (win-count) */
1912 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001913get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001914{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001915 int n = 0;
Bram Moolenaard2142212013-01-30 17:41:50 +01001916#ifdef FEAT_WINDOWS
1917 win_T *w;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001918
Bram Moolenaarf740b292006-02-16 22:11:02 +00001919 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaard2142212013-01-30 17:41:50 +01001920#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001921 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001922 return scheme_make_integer(n);
1923}
1924
1925/* (get-win-list [buffer]) */
1926 static Scheme_Object *
1927get_window_list(void *data, int argc, Scheme_Object **argv)
1928{
1929 Vim_Prim *prim = (Vim_Prim *)data;
1930 vim_mz_buffer *buf;
1931 Scheme_Object *list;
Bram Moolenaard2142212013-01-30 17:41:50 +01001932 win_T *w = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001933
1934 buf = get_buffer_arg(prim->name, 0, argc, argv);
1935 list = scheme_null;
1936
Bram Moolenaard2142212013-01-30 17:41:50 +01001937#ifdef FEAT_WINDOWS
1938 for ( ; w != NULL; w = w->w_next)
1939#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001940 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001941 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001942 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001943 MZ_GC_CHECK();
1944 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001945
1946 return list;
1947}
1948
1949 static Scheme_Object *
1950window_new(win_T *win)
1951{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001952 vim_mz_window *self = NULL;
1953
1954 MZ_GC_DECL_REG(1);
1955 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001956
1957 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001958 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001959 * then we can get at it in win_free() in vim.
1960 *
1961 * On a win_free() we set the Scheme object's win_T *field
1962 * to an invalid value. We trap all uses of a window
1963 * object, and reject them if the win_T *field is invalid.
1964 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001965 if (win->w_mzscheme_ref != NULL)
Bram Moolenaar75676462013-01-30 14:55:42 +01001966 return (Scheme_Object *)WINDOW_REF(win);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001967
Bram Moolenaar75676462013-01-30 14:55:42 +01001968 MZ_GC_REG();
1969 self = scheme_malloc_fail_ok(scheme_malloc_tagged, sizeof(vim_mz_window));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001970 vim_memset(self, 0, sizeof(vim_mz_window));
Bram Moolenaar75676462013-01-30 14:55:42 +01001971#ifndef MZ_PRECISE_GC
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001972 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaar75676462013-01-30 14:55:42 +01001973#else
1974 win->w_mzscheme_ref = scheme_malloc_immobile_box(NULL);
1975#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001976 MZ_GC_CHECK();
Bram Moolenaar75676462013-01-30 14:55:42 +01001977 WINDOW_REF(win) = self;
1978 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001979 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001980 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001981
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001982 MZ_GC_UNREG();
Bram Moolenaar75676462013-01-30 14:55:42 +01001983 return (Scheme_Object *)self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001984}
1985
1986/* (get-win-num [window]) */
1987 static Scheme_Object *
Bram Moolenaard2142212013-01-30 17:41:50 +01001988get_window_num(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001989{
Bram Moolenaard2142212013-01-30 17:41:50 +01001990 int nr = 1;
1991#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001992 Vim_Prim *prim = (Vim_Prim *)data;
1993 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001994 win_T *wp;
1995
1996 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaard2142212013-01-30 17:41:50 +01001997#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001998 ++nr;
1999
2000 return scheme_make_integer(nr);
2001}
2002
2003/* (get-win-by-num {windownum}) */
2004 static Scheme_Object *
2005get_window_by_num(void *data, int argc, Scheme_Object **argv)
2006{
2007 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaard2142212013-01-30 17:41:50 +01002008 win_T *win = firstwin;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002009 int fnum;
2010
2011 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2012 if (fnum < 1)
2013 scheme_signal_error(_("window index is out of range"));
2014
Bram Moolenaard2142212013-01-30 17:41:50 +01002015#ifdef FEAT_WINDOWS
2016 for ( ; win != NULL; win = win->w_next, --fnum)
2017#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002018 if (fnum == 1) /* to be 1-based */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002019 return window_new(win);
2020
2021 return scheme_false;
2022}
2023
2024/* (get-win-buffer [window]) */
2025 static Scheme_Object *
2026get_window_buffer(void *data, int argc, Scheme_Object **argv)
2027{
2028 Vim_Prim *prim = (Vim_Prim *)data;
2029 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2030
2031 return buffer_new(win->win->w_buffer);
2032}
2033
2034/* (get-win-height [window]) */
2035 static Scheme_Object *
2036get_window_height(void *data, int argc, Scheme_Object **argv)
2037{
2038 Vim_Prim *prim = (Vim_Prim *)data;
2039 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2040
2041 return scheme_make_integer(win->win->w_height);
2042}
2043
2044/* (set-win-height {height} [window]) */
2045 static Scheme_Object *
2046set_window_height(void *data, int argc, Scheme_Object **argv)
2047{
2048 Vim_Prim *prim = (Vim_Prim *)data;
2049 vim_mz_window *win;
2050 win_T *savewin;
2051 int height;
2052
2053 win = get_window_arg(prim->name, 1, argc, argv);
2054 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2055
2056#ifdef FEAT_GUI
2057 need_mouse_correct = TRUE;
2058#endif
2059
2060 savewin = curwin;
2061 curwin = win->win;
2062 win_setheight(height);
2063 curwin = savewin;
2064
2065 raise_if_error();
2066 return scheme_void;
2067}
2068
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002069#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002070/* (get-win-width [window]) */
2071 static Scheme_Object *
2072get_window_width(void *data, int argc, Scheme_Object **argv)
2073{
2074 Vim_Prim *prim = (Vim_Prim *)data;
2075 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
2076
2077 return scheme_make_integer(W_WIDTH(win->win));
2078}
2079
2080/* (set-win-width {width} [window]) */
2081 static Scheme_Object *
2082set_window_width(void *data, int argc, Scheme_Object **argv)
2083{
2084 Vim_Prim *prim = (Vim_Prim *)data;
2085 vim_mz_window *win;
2086 win_T *savewin;
2087 int width = 0;
2088
2089 win = get_window_arg(prim->name, 1, argc, argv);
2090 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2091
2092# ifdef FEAT_GUI
2093 need_mouse_correct = TRUE;
2094# endif
2095
2096 savewin = curwin;
2097 curwin = win->win;
2098 win_setwidth(width);
2099 curwin = savewin;
2100
2101 raise_if_error();
2102 return scheme_void;
2103}
2104#endif
2105
2106/* (get-cursor [window]) -> (line . col) */
2107 static Scheme_Object *
2108get_cursor(void *data, int argc, Scheme_Object **argv)
2109{
2110 Vim_Prim *prim = (Vim_Prim *)data;
2111 vim_mz_window *win;
2112 pos_T pos;
2113
2114 win = get_window_arg(prim->name, 0, argc, argv);
2115 pos = win->win->w_cursor;
2116 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
2117 scheme_make_integer_value((long)pos.col + 1));
2118}
2119
2120/* (set-cursor (line . col) [window]) */
2121 static Scheme_Object *
2122set_cursor(void *data, int argc, Scheme_Object **argv)
2123{
2124 Vim_Prim *prim = (Vim_Prim *)data;
2125 vim_mz_window *win;
2126 long lnum = 0;
2127 long col = 0;
2128
Bram Moolenaar555b2802005-05-19 21:08:39 +00002129#ifdef HAVE_SANDBOX
2130 sandbox_check();
2131#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002132 win = get_window_arg(prim->name, 1, argc, argv);
2133 GUARANTEE_PAIR(prim->name, 0);
2134
2135 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
2136 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
2137 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
2138
2139 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
2140 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
2141
2142 check_line_range(lnum, win->win->w_buffer);
2143 /* don't know how to catch invalid column value */
2144
2145 win->win->w_cursor.lnum = lnum;
2146 win->win->w_cursor.col = col;
2147 update_screen(VALID);
2148
2149 raise_if_error();
2150 return scheme_void;
2151}
2152/*
2153 *===========================================================================
2154 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002155 *===========================================================================
2156 */
2157
2158/* (open-buff {filename}) */
2159 static Scheme_Object *
2160mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
2161{
2162 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002163 int num = 0;
Bram Moolenaar75676462013-01-30 14:55:42 +01002164 Scheme_Object *onum = NULL;
2165 Scheme_Object *buf = NULL;
2166 Scheme_Object *fname;
2167
2168 MZ_GC_DECL_REG(3);
2169 MZ_GC_VAR_IN_REG(0, onum);
2170 MZ_GC_VAR_IN_REG(1, buf);
2171 MZ_GC_VAR_IN_REG(2, fname);
2172 MZ_GC_REG();
2173 fname = GUARANTEED_STRING_ARG(prim->name, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002174
Bram Moolenaar555b2802005-05-19 21:08:39 +00002175#ifdef HAVE_SANDBOX
2176 sandbox_check();
2177#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002178 /* TODO make open existing file */
Bram Moolenaar75676462013-01-30 14:55:42 +01002179 num = buflist_add(BYTE_STRING_VALUE(fname), BLN_LISTED | BLN_CURBUF);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002180
2181 if (num == 0)
2182 raise_vim_exn(_("couldn't open buffer"));
2183
2184 onum = scheme_make_integer(num);
Bram Moolenaar75676462013-01-30 14:55:42 +01002185 buf = get_buffer_by_num(data, 1, &onum);
2186 MZ_GC_UNREG();
2187 return buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002188}
2189
2190/* (get-buff-by-num {buffernum}) */
2191 static Scheme_Object *
2192get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
2193{
2194 Vim_Prim *prim = (Vim_Prim *)data;
2195 buf_T *buf;
2196 int fnum;
2197
2198 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2199
2200 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002201 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002202 return buffer_new(buf);
2203
2204 return scheme_false;
2205}
2206
2207/* (get-buff-by-name {buffername}) */
2208 static Scheme_Object *
2209get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
2210{
2211 Vim_Prim *prim = (Vim_Prim *)data;
2212 buf_T *buf;
Bram Moolenaar75676462013-01-30 14:55:42 +01002213 Scheme_Object *buffer = NULL;
2214 Scheme_Object *fname = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002215
Bram Moolenaar75676462013-01-30 14:55:42 +01002216 MZ_GC_DECL_REG(2);
2217 MZ_GC_VAR_IN_REG(0, buffer);
2218 MZ_GC_VAR_IN_REG(1, fname);
2219 MZ_GC_REG();
2220 fname = GUARANTEED_STRING_ARG(prim->name, 0);
2221 buffer = scheme_false;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002222
2223 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaar75676462013-01-30 14:55:42 +01002224 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002225 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2226 /* empty string */
2227 {
Bram Moolenaar75676462013-01-30 14:55:42 +01002228 if (BYTE_STRING_VALUE(fname)[0] == NUL)
2229 buffer = buffer_new(buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002230 }
Bram Moolenaar75676462013-01-30 14:55:42 +01002231 else if (!fnamecmp(buf->b_ffname, BYTE_STRING_VALUE(fname))
2232 || !fnamecmp(buf->b_sfname, BYTE_STRING_VALUE(fname)))
2233 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002234 /* either short or long filename matches */
Bram Moolenaar75676462013-01-30 14:55:42 +01002235 buffer = buffer_new(buf);
2236 }
2237 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002238
Bram Moolenaar75676462013-01-30 14:55:42 +01002239 MZ_GC_UNREG();
2240 return buffer;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002241}
2242
2243/* (get-next-buff [buffer]) */
2244 static Scheme_Object *
2245get_next_buffer(void *data, int argc, Scheme_Object **argv)
2246{
2247 Vim_Prim *prim = (Vim_Prim *)data;
2248 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2249
2250 if (buf->b_next == NULL)
2251 return scheme_false;
2252 else
2253 return buffer_new(buf->b_next);
2254}
2255
2256/* (get-prev-buff [buffer]) */
2257 static Scheme_Object *
2258get_prev_buffer(void *data, int argc, Scheme_Object **argv)
2259{
2260 Vim_Prim *prim = (Vim_Prim *)data;
2261 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
2262
2263 if (buf->b_prev == NULL)
2264 return scheme_false;
2265 else
2266 return buffer_new(buf->b_prev);
2267}
2268
2269/* (get-buff-num [buffer]) */
2270 static Scheme_Object *
2271get_buffer_num(void *data, int argc, Scheme_Object **argv)
2272{
2273 Vim_Prim *prim = (Vim_Prim *)data;
2274 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2275
2276 return scheme_make_integer(buf->buf->b_fnum);
2277}
2278
2279/* (buff-count) */
2280 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002281get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002282{
2283 buf_T *b;
2284 int n = 0;
2285
2286 for (b = firstbuf; b; b = b->b_next) ++n;
2287 return scheme_make_integer(n);
2288}
2289
2290/* (get-buff-name [buffer]) */
2291 static Scheme_Object *
2292get_buffer_name(void *data, int argc, Scheme_Object **argv)
2293{
2294 Vim_Prim *prim = (Vim_Prim *)data;
2295 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2296
Bram Moolenaar75676462013-01-30 14:55:42 +01002297 return scheme_make_byte_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002298}
2299
2300/* (curr-buff) */
2301 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002302get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002303{
2304 return (Scheme_Object *)get_vim_curr_buffer();
2305}
2306
2307 static Scheme_Object *
2308buffer_new(buf_T *buf)
2309{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002310 vim_mz_buffer *self = NULL;
2311
2312 MZ_GC_DECL_REG(1);
2313 MZ_GC_VAR_IN_REG(0, self);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002314
2315 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00002316 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002317 * then we can get at it in buf_freeall() in vim.
2318 */
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 Moolenaar325b7a22004-07-05 15:58:32 +00002324 vim_memset(self, 0, sizeof(vim_mz_buffer));
Bram Moolenaar75676462013-01-30 14:55:42 +01002325#ifndef MZ_PRECISE_GC
2326 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
2327#else
2328 buf->b_mzscheme_ref = scheme_malloc_immobile_box(NULL);
2329#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
2421 /* Set the list item */
2422 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 Moolenaar0a1c0ec2009-12-16 18:02:47 +00002444 /* First of all, we check the value of the supplied MzScheme object.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002445 * 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.
2449 */
2450 Vim_Prim *prim = (Vim_Prim *)data;
2451 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002452 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002453 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002454 int n;
2455
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002456 MZ_GC_DECL_REG(1);
2457 MZ_GC_VAR_IN_REG(0, line);
2458 MZ_GC_REG();
2459
Bram Moolenaar555b2802005-05-19 21:08:39 +00002460#ifdef HAVE_SANDBOX
2461 sandbox_check();
2462#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002463 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2464 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002465 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002466 line = argv[1];
2467 buf = get_buffer_arg(prim->name, 2, argc, argv);
2468
2469 check_line_range(n, buf->buf);
2470
2471 if (SCHEME_FALSEP(line))
2472 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002473 buf_T *savebuf = curbuf;
2474
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002475 curbuf = buf->buf;
2476
2477 if (u_savedel((linenr_T)n, 1L) == FAIL)
2478 {
2479 curbuf = savebuf;
2480 raise_vim_exn(_("cannot save undo information"));
2481 }
2482 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2483 {
2484 curbuf = savebuf;
2485 raise_vim_exn(_("cannot delete line"));
2486 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002487 if (buf->buf == curwin->w_buffer)
2488 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002489 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002490
2491 curbuf = savebuf;
2492
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002493 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002494 raise_if_error();
2495 return scheme_void;
2496 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002497 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002498 {
2499 /* Otherwise it's a line */
2500 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002501
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002502 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002503
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002504 curbuf = buf->buf;
2505
2506 if (u_savesub((linenr_T)n) == FAIL)
2507 {
2508 curbuf = savebuf;
2509 vim_free(save);
2510 raise_vim_exn(_("cannot save undo information"));
2511 }
2512 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2513 {
2514 curbuf = savebuf;
2515 vim_free(save);
2516 raise_vim_exn(_("cannot replace line"));
2517 }
2518 else
2519 {
2520 vim_free(save);
2521 changed_bytes((linenr_T)n, 0);
2522 }
2523
2524 curbuf = savebuf;
2525
2526 /* Check that the cursor is not beyond the end of the line now. */
2527 if (buf->buf == curwin->w_buffer)
2528 check_cursor_col();
2529
2530 MZ_GC_UNREG();
2531 raise_if_error();
2532 return scheme_void;
2533 }
2534}
2535
2536 static void
2537free_array(char **array)
2538{
2539 char **curr = array;
2540 while (*curr != NULL)
2541 vim_free(*curr++);
2542 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002543}
2544
2545/*
2546 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2547 *
2548 * Replace a range of lines in the specified buffer. The line numbers are in
2549 * Vim format (1-based). The range is from lo up to, but not including, hi.
2550 * The replacement lines are given as a Scheme list of string objects. The
2551 * list is checked for validity and correct format.
2552 *
2553 * Errors are returned as a value of FAIL. The return value is OK on success.
2554 * If OK is returned and len_change is not NULL, *len_change is set to the
2555 * change in the buffer length.
2556 */
2557 static Scheme_Object *
2558set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2559{
2560 /* First of all, we check the type of the supplied MzScheme object.
2561 * There are three cases:
2562 * 1. #f - this is a deletion.
2563 * 2. A list - this is a replacement.
2564 * 3. Anything else - this is an error.
2565 */
2566 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002567 vim_mz_buffer *buf = NULL;
2568 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002569 int i, old_len, new_len, hi, lo;
2570 long extra;
2571
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002572 MZ_GC_DECL_REG(1);
2573 MZ_GC_VAR_IN_REG(0, line_list);
2574 MZ_GC_REG();
2575
Bram Moolenaar555b2802005-05-19 21:08:39 +00002576#ifdef HAVE_SANDBOX
2577 sandbox_check();
2578#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002579 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2580 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2581 if (!SCHEME_PAIRP(argv[2])
2582 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2583 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2584 line_list = argv[2];
2585 buf = get_buffer_arg(prim->name, 3, argc, argv);
2586 old_len = hi - lo;
2587 if (old_len < 0) /* process inverse values wisely */
2588 {
2589 i = lo;
2590 lo = hi;
2591 hi = i;
2592 old_len = -old_len;
2593 }
2594 extra = 0;
2595
2596 check_line_range(lo, buf->buf); /* inclusive */
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002597 check_line_range(hi - 1, buf->buf); /* exclusive */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002598
2599 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2600 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002601 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002602 curbuf = buf->buf;
2603
2604 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2605 {
2606 curbuf = savebuf;
2607 raise_vim_exn(_("cannot save undo information"));
2608 }
2609 else
2610 {
2611 for (i = 0; i < old_len; i++)
2612 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2613 {
2614 curbuf = savebuf;
2615 raise_vim_exn(_("cannot delete line"));
2616 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002617 if (buf->buf == curwin->w_buffer)
2618 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002619 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002620 }
2621
2622 curbuf = savebuf;
2623
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002624 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002625 raise_if_error();
2626 return scheme_void;
2627 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002628 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002629 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002630 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002631
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002632 /* List */
2633 new_len = scheme_proper_list_length(line_list);
2634 MZ_GC_CHECK();
2635 if (new_len < 0) /* improper or cyclic list */
2636 scheme_wrong_type(prim->name, "proper list",
2637 2, argc, argv);
2638 else
2639 {
2640 char **array = NULL;
2641 Scheme_Object *line = NULL;
2642 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002643
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002644 MZ_GC_DECL_REG(2);
2645 MZ_GC_VAR_IN_REG(0, line);
2646 MZ_GC_VAR_IN_REG(1, rest);
2647 MZ_GC_REG();
2648
Bram Moolenaar75676462013-01-30 14:55:42 +01002649 array = (char **)alloc((new_len+1)* sizeof(char *));
2650 vim_memset(array, 0, (new_len+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002651
2652 rest = line_list;
2653 for (i = 0; i < new_len; ++i)
2654 {
2655 line = SCHEME_CAR(rest);
2656 rest = SCHEME_CDR(rest);
2657 if (!SCHEME_STRINGP(line))
2658 {
2659 free_array(array);
2660 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2661 }
2662 array[i] = string_to_line(line);
2663 }
2664
2665 curbuf = buf->buf;
2666
2667 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2668 {
2669 curbuf = savebuf;
2670 free_array(array);
2671 raise_vim_exn(_("cannot save undo information"));
2672 }
2673
2674 /*
2675 * If the size of the range is reducing (ie, new_len < old_len) we
2676 * need to delete some old_len. We do this at the start, by
2677 * repeatedly deleting line "lo".
2678 */
2679 for (i = 0; i < old_len - new_len; ++i)
2680 {
2681 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2682 {
2683 curbuf = savebuf;
2684 free_array(array);
2685 raise_vim_exn(_("cannot delete line"));
2686 }
2687 extra--;
2688 }
2689
2690 /*
2691 * For as long as possible, replace the existing old_len with the
2692 * new old_len. This is a more efficient operation, as it requires
2693 * less memory allocation and freeing.
2694 */
2695 for (i = 0; i < old_len && i < new_len; i++)
2696 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2697 {
2698 curbuf = savebuf;
2699 free_array(array);
2700 raise_vim_exn(_("cannot replace line"));
2701 }
2702
2703 /*
2704 * Now we may need to insert the remaining new_len. We don't need to
2705 * free the string passed back because MzScheme has control of that
2706 * memory.
2707 */
2708 while (i < new_len)
2709 {
2710 if (ml_append((linenr_T)(lo + i - 1),
2711 (char_u *)array[i], 0, FALSE) == FAIL)
2712 {
2713 curbuf = savebuf;
2714 free_array(array);
2715 raise_vim_exn(_("cannot insert line"));
2716 }
2717 ++i;
2718 ++extra;
2719 }
2720 MZ_GC_UNREG();
2721 free_array(array);
2722 }
2723
2724 /*
2725 * Adjust marks. Invalidate any which lie in the
2726 * changed range, and move any in the remainder of the buffer.
2727 */
2728 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra);
2729 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2730
2731 if (buf->buf == curwin->w_buffer)
2732 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002733 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002734
2735 MZ_GC_UNREG();
2736 raise_if_error();
2737 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002738 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002739}
2740
2741/*
2742 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2743 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002744 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002745 * The line number is in Vim format (1-based). The lines to be inserted are
2746 * given as an MzScheme list of string objects or as a single string. The lines
2747 * to be added are checked for validity and correct format. Errors are
2748 * returned as a value of FAIL. The return value is OK on success.
2749 * If OK is returned and len_change is not NULL, *len_change
2750 * is set to the change in the buffer length.
2751 */
2752 static Scheme_Object *
2753insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2754{
2755 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002756 vim_mz_buffer *buf = NULL;
2757 Scheme_Object *list = NULL;
2758 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002759 int i, n, size;
2760
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002761 MZ_GC_DECL_REG(1);
2762 MZ_GC_VAR_IN_REG(0, list);
2763 MZ_GC_REG();
2764
Bram Moolenaar555b2802005-05-19 21:08:39 +00002765#ifdef HAVE_SANDBOX
2766 sandbox_check();
2767#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002768 /*
2769 * First of all, we check the type of the supplied MzScheme object.
2770 * It must be a string or a list, or the call is in error.
2771 */
2772 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2773 list = argv[1];
2774
2775 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2776 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2777 buf = get_buffer_arg(prim->name, 2, argc, argv);
2778
2779 if (n != 0) /* 0 can be used in insert */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002780 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002781 if (SCHEME_STRINGP(list))
2782 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002783 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002784
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002785 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002786 curbuf = buf->buf;
2787
2788 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2789 {
2790 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002791 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002792 raise_vim_exn(_("cannot save undo information"));
2793 }
2794 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2795 {
2796 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002797 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002798 raise_vim_exn(_("cannot insert line"));
2799 }
2800 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002801 {
2802 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002803 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002804 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002805
2806 curbuf = savebuf;
2807 update_screen(VALID);
2808
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002809 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002810 raise_if_error();
2811 return scheme_void;
2812 }
2813
2814 /* List */
2815 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002816 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002817 if (size < 0) /* improper or cyclic list */
2818 scheme_wrong_type(prim->name, "proper list",
2819 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002820 else
2821 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002822 Scheme_Object *line = NULL;
2823 Scheme_Object *rest = NULL;
2824 char **array;
2825 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002826
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002827 MZ_GC_DECL_REG(2);
2828 MZ_GC_VAR_IN_REG(0, line);
2829 MZ_GC_VAR_IN_REG(1, rest);
2830 MZ_GC_REG();
2831
Bram Moolenaar75676462013-01-30 14:55:42 +01002832 array = (char **)alloc((size+1) * sizeof(char *));
2833 vim_memset(array, 0, (size+1) * sizeof(char *));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002834
2835 rest = list;
2836 for (i = 0; i < size; ++i)
2837 {
2838 line = SCHEME_CAR(rest);
2839 rest = SCHEME_CDR(rest);
2840 array[i] = string_to_line(line);
2841 }
2842
2843 curbuf = buf->buf;
2844
2845 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2846 {
2847 curbuf = savebuf;
2848 free_array(array);
2849 raise_vim_exn(_("cannot save undo information"));
2850 }
2851 else
2852 {
2853 for (i = 0; i < size; ++i)
2854 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2855 0, FALSE) == FAIL)
2856 {
2857 curbuf = savebuf;
2858 free_array(array);
2859 raise_vim_exn(_("cannot insert line"));
2860 }
2861
2862 if (i > 0)
2863 appended_lines_mark((linenr_T)n, (long)i);
2864 }
2865 free_array(array);
2866 MZ_GC_UNREG();
2867 curbuf = savebuf;
2868 update_screen(VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002869 }
2870
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002871 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002872 raise_if_error();
2873 return scheme_void;
2874}
2875
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002876/*
2877 * Predicates
2878 */
2879/* (buff? obj) */
2880 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002881vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002882{
2883 if (SCHEME_VIMBUFFERP(argv[0]))
2884 return scheme_true;
2885 else
2886 return scheme_false;
2887}
2888
2889/* (win? obj) */
2890 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002891vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002892{
2893 if (SCHEME_VIMWINDOWP(argv[0]))
2894 return scheme_true;
2895 else
2896 return scheme_false;
2897}
2898
2899/* (buff-valid? obj) */
2900 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002901vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002902{
2903 if (SCHEME_VIMBUFFERP(argv[0])
2904 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2905 return scheme_true;
2906 else
2907 return scheme_false;
2908}
2909
2910/* (win-valid? obj) */
2911 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002912vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002913{
2914 if (SCHEME_VIMWINDOWP(argv[0])
2915 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2916 return scheme_true;
2917 else
2918 return scheme_false;
2919}
2920
2921/*
2922 *===========================================================================
2923 * Utilities
2924 *===========================================================================
2925 */
2926
2927/*
2928 * Convert an MzScheme string into a Vim line.
2929 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002930 * All internal nulls are replaced by newline characters.
2931 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002932 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002933 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002934 */
2935 static char *
2936string_to_line(Scheme_Object *obj)
2937{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002938 char *scheme_str = NULL;
2939 char *vim_str = NULL;
Bram Moolenaar75676462013-01-30 14:55:42 +01002940 OUTPUT_LEN_TYPE len;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002941 int i;
2942
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002943 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002944
2945 /* Error checking: String must not contain newlines, as we
2946 * are replacing a single line, and we must replace it with
2947 * a single line.
2948 */
Bram Moolenaar75676462013-01-30 14:55:42 +01002949 if (memchr(scheme_str, '\n', len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002950 scheme_signal_error(_("string cannot contain newlines"));
2951
Bram Moolenaar75676462013-01-30 14:55:42 +01002952 vim_str = (char *)alloc(len + 1);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002953
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002954 /* Create a copy of the string, with internal nulls replaced by
2955 * newline characters, as is the vim convention.
2956 */
2957 for (i = 0; i < len; ++i)
2958 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002959 if (scheme_str[i] == '\0')
2960 vim_str[i] = '\n';
2961 else
2962 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002963 }
2964
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002965 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002966
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002967 MZ_GC_CHECK();
2968 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002969}
2970
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002971#ifdef FEAT_EVAL
2972/*
2973 * Convert Vim value into MzScheme, adopted from if_python.c
2974 */
2975 static Scheme_Object *
Bram Moolenaar75676462013-01-30 14:55:42 +01002976vim_to_mzscheme(typval_T *vim_value)
2977{
2978 Scheme_Object *result = NULL;
2979 /* hash table to store visited values to avoid infinite loops */
2980 Scheme_Hash_Table *visited = NULL;
2981
2982 MZ_GC_DECL_REG(2);
2983 MZ_GC_VAR_IN_REG(0, result);
2984 MZ_GC_VAR_IN_REG(1, visited);
2985 MZ_GC_REG();
2986
2987 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2988 MZ_GC_CHECK();
2989
2990 result = vim_to_mzscheme_impl(vim_value, 1, visited);
2991
2992 MZ_GC_UNREG();
2993 return result;
2994}
2995
2996 static Scheme_Object *
2997vim_to_mzscheme_impl(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002998{
2999 Scheme_Object *result = NULL;
3000 int new_value = TRUE;
3001
Bram Moolenaar75676462013-01-30 14:55:42 +01003002 MZ_GC_DECL_REG(2);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003003 MZ_GC_VAR_IN_REG(0, result);
Bram Moolenaar75676462013-01-30 14:55:42 +01003004 MZ_GC_VAR_IN_REG(1, visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003005 MZ_GC_REG();
3006
3007 /* Avoid infinite recursion */
3008 if (depth > 100)
3009 {
3010 MZ_GC_UNREG();
3011 return scheme_void;
3012 }
3013
3014 /* Check if we run into a recursive loop. The item must be in visited
3015 * then and we can use it again.
3016 */
3017 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
3018 MZ_GC_CHECK();
3019 if (result != NULL) /* found, do nothing */
3020 new_value = FALSE;
3021 else if (vim_value->v_type == VAR_STRING)
3022 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003023 result = scheme_make_byte_string((char *)vim_value->vval.v_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003024 MZ_GC_CHECK();
3025 }
3026 else if (vim_value->v_type == VAR_NUMBER)
3027 {
3028 result = scheme_make_integer((long)vim_value->vval.v_number);
3029 MZ_GC_CHECK();
3030 }
3031# ifdef FEAT_FLOAT
3032 else if (vim_value->v_type == VAR_FLOAT)
3033 {
3034 result = scheme_make_double((double)vim_value->vval.v_float);
3035 MZ_GC_CHECK();
3036 }
3037# endif
3038 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
3053 curr = list->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
3087 for (hi = ht->ht_array; todo > 0; ++hi)
3088 {
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 Moolenaar67c2c052016-03-30 22:03:02 +02003112 /* 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
3133 /* FIXME: func_ref() and func_unref() are needed. */
3134 /* TODO: Support pt_dict and pt_argv. */
3135 funcname = scheme_make_byte_string(
3136 (char *)vim_value->vval.v_partial->pt_name);
3137 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 Moolenaar520e1e42016-01-23 19:46:28 +01003145 else if (vim_value->v_type == VAR_SPECIAL)
3146 {
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 {
3184 /* free up remembered objects */
3185 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();
3205 if (depth > 100) /* limit the deepest recursion level */
3206 {
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 Moolenaar520e1e42016-01-23 19:46:28 +01003227 tv->v_type = VAR_SPECIAL;
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003228 tv->vval.v_number = SCHEME_TRUEP(obj);
3229 }
3230# ifdef FEAT_FLOAT
3231 else if (SCHEME_DBLP(obj))
3232 {
3233 tv->v_type = VAR_FLOAT;
3234 tv->vval.v_float = SCHEME_DBL_VAL(obj);
3235 }
3236# endif
Bram Moolenaar75676462013-01-30 14:55:42 +01003237 else if (SCHEME_BYTE_STRINGP(obj))
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003238 {
3239 tv->v_type = VAR_STRING;
Bram Moolenaar75676462013-01-30 14:55:42 +01003240 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(obj));
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003241 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003242# if MZSCHEME_VERSION_MAJOR >= 299
3243 else if (SCHEME_CHAR_STRINGP(obj))
3244 {
3245 Scheme_Object *tmp = NULL;
3246 MZ_GC_DECL_REG(1);
3247 MZ_GC_VAR_IN_REG(0, tmp);
3248 MZ_GC_REG();
3249
3250 tmp = scheme_char_string_to_byte_string(obj);
3251 tv->v_type = VAR_STRING;
3252 tv->vval.v_string = vim_strsave(BYTE_STRING_VALUE(tmp));
3253 MZ_GC_UNREG();
3254 }
3255#endif
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003256 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
3257 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3258 {
3259 list_T *list = list_alloc();
3260 if (list == NULL)
3261 status = FAIL;
3262 else
3263 {
3264 int i;
3265 Scheme_Object *curr = NULL;
3266 Scheme_Object *cval = NULL;
3267 /* temporary var to hold current element of vectors and pairs */
3268 typval_T *v;
3269
3270 MZ_GC_DECL_REG(2);
3271 MZ_GC_VAR_IN_REG(0, curr);
3272 MZ_GC_VAR_IN_REG(1, cval);
3273 MZ_GC_REG();
3274
3275 tv->v_type = VAR_LIST;
3276 tv->vval.v_list = list;
3277 ++list->lv_refcount;
3278
3279 v = (typval_T *)alloc(sizeof(typval_T));
3280 if (v == NULL)
3281 status = FAIL;
3282 else
3283 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003284 /* add the value in advance to allow handling of self-referential
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003285 * data structures */
3286 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3287 copy_tv(tv, visited_tv);
3288 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3289
3290 if (SCHEME_VECTORP(obj))
3291 {
3292 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
3293 {
3294 cval = SCHEME_VEC_ELS(obj)[i];
Bram Moolenaar75676462013-01-30 14:55:42 +01003295 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003296 if (status == FAIL)
3297 break;
3298 status = list_append_tv(list, v);
3299 clear_tv(v);
3300 if (status == FAIL)
3301 break;
3302 }
3303 }
3304 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
3305 {
3306 for (curr = obj;
3307 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
3308 curr = SCHEME_CDR(curr))
3309 {
3310 cval = SCHEME_CAR(curr);
Bram Moolenaar75676462013-01-30 14:55:42 +01003311 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003312 if (status == FAIL)
3313 break;
3314 status = list_append_tv(list, v);
3315 clear_tv(v);
3316 if (status == FAIL)
3317 break;
3318 }
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003319 /* improper list not terminated with null
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003320 * need to handle the last element */
3321 if (status == OK && !SCHEME_NULLP(curr))
3322 {
Bram Moolenaar75676462013-01-30 14:55:42 +01003323 status = mzscheme_to_vim_impl(cval, v, depth + 1, visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003324 if (status == OK)
3325 {
3326 status = list_append_tv(list, v);
3327 clear_tv(v);
3328 }
3329 }
3330 }
3331 /* nothing to do for scheme_null */
3332 vim_free(v);
3333 }
3334 MZ_GC_UNREG();
3335 }
3336 }
3337 else if (SCHEME_HASHTP(obj))
3338 {
3339 int i;
3340 dict_T *dict;
3341 Scheme_Object *key = NULL;
3342 Scheme_Object *val = NULL;
3343
3344 MZ_GC_DECL_REG(2);
3345 MZ_GC_VAR_IN_REG(0, key);
3346 MZ_GC_VAR_IN_REG(1, val);
3347 MZ_GC_REG();
3348
3349 dict = dict_alloc();
3350 if (dict == NULL)
3351 status = FAIL;
3352 else
3353 {
3354 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
3355
3356 tv->v_type = VAR_DICT;
3357 tv->vval.v_dict = dict;
3358 ++dict->dv_refcount;
3359
3360 copy_tv(tv, visited_tv);
3361 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
3362
3363 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
3364 {
3365 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
3366 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003367 /* generate item for `display'ed Scheme key */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003368 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
3369 ((Scheme_Hash_Table *) obj)->keys[i]));
3370 /* convert Scheme val to Vim and add it to the dict */
Bram Moolenaar75676462013-01-30 14:55:42 +01003371 if (mzscheme_to_vim_impl(((Scheme_Hash_Table *) obj)->vals[i],
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003372 &item->di_tv, depth + 1, visited) == FAIL
3373 || dict_add(dict, item) == FAIL)
3374 {
3375 dictitem_free(item);
3376 status = FAIL;
3377 break;
3378 }
3379 }
3380
3381 }
3382 }
3383 MZ_GC_UNREG();
3384 }
3385 else
3386 {
3387 /* `display' any other value to string */
3388 tv->v_type = VAR_STRING;
3389 tv->vval.v_string = (char_u *)string_to_line(obj);
3390 }
Bram Moolenaar75676462013-01-30 14:55:42 +01003391 MZ_GC_UNREG();
Bram Moolenaar7e506b62010-01-19 15:55:06 +01003392 return status;
3393}
3394
Bram Moolenaar75676462013-01-30 14:55:42 +01003395/* Scheme prim procedure wrapping Vim funcref */
3396 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 {
3419 typval_T *v = (typval_T *)alloc(sizeof(typval_T));
3420 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
3612/* get buffer:
3613 * either current
3614 * or passed as argv[argnum] with checks
3615 */
3616 static vim_mz_buffer *
3617get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3618{
3619 vim_mz_buffer *b;
3620
3621 if (argc < argnum + 1)
3622 return get_vim_curr_buffer();
3623 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3624 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3625 b = (vim_mz_buffer *)argv[argnum];
3626 (void)get_valid_buffer(argv[argnum]);
3627 return b;
3628}
3629
3630/* get window:
3631 * either current
3632 * or passed as argv[argnum] with checks
3633 */
3634 static vim_mz_window *
3635get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3636{
3637 vim_mz_window *w;
3638
3639 if (argc < argnum + 1)
3640 return get_vim_curr_window();
3641 w = (vim_mz_window *)argv[argnum];
3642 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3643 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3644 (void)get_valid_window(argv[argnum]);
3645 return w;
3646}
3647
3648/* get valid Vim buffer from Scheme_Object* */
3649buf_T *get_valid_buffer(void *obj)
3650{
3651 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3652
3653 if (buf == INVALID_BUFFER_VALUE)
3654 scheme_signal_error(_("buffer is invalid"));
3655 return buf;
3656}
3657
3658/* get valid Vim window from Scheme_Object* */
3659win_T *get_valid_window(void *obj)
3660{
3661 win_T *win = ((vim_mz_window *)obj)->win;
3662 if (win == INVALID_WINDOW_VALUE)
3663 scheme_signal_error(_("window is invalid"));
3664 return win;
3665}
3666
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003667 int
3668mzthreads_allowed(void)
3669{
3670 return mz_threads_allow;
3671}
3672
3673 static int
3674line_in_range(linenr_T lnum, buf_T *buf)
3675{
3676 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3677}
3678
3679 static void
3680check_line_range(linenr_T lnum, buf_T *buf)
3681{
3682 if (!line_in_range(lnum, buf))
3683 scheme_signal_error(_("linenr out of range"));
3684}
3685
3686/*
3687 * Check if deleting lines made the cursor position invalid
3688 * (or you'll get msg from Vim about invalid linenr).
3689 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3690 * deleted). Got from if_python.c
3691 */
3692 static void
3693mz_fix_cursor(int lo, int hi, int extra)
3694{
3695 if (curwin->w_cursor.lnum >= lo)
3696 {
3697 /* Adjust the cursor position if it's in/after the changed
3698 * lines. */
3699 if (curwin->w_cursor.lnum >= hi)
3700 {
3701 curwin->w_cursor.lnum += extra;
3702 check_cursor_col();
3703 }
3704 else if (extra < 0)
3705 {
3706 curwin->w_cursor.lnum = lo;
3707 check_cursor();
3708 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003709 else
3710 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003711 changed_cline_bef_curs();
3712 }
3713 invalidate_botline();
3714}
3715
3716static Vim_Prim prims[]=
3717{
3718 /*
3719 * Buffer-related commands
3720 */
3721 {get_buffer_line, "get-buff-line", 1, 2},
3722 {set_buffer_line, "set-buff-line", 2, 3},
3723 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3724 {get_buffer_name, "get-buff-name", 0, 1},
3725 {get_buffer_num, "get-buff-num", 0, 1},
3726 {get_buffer_size, "get-buff-size", 0, 1},
3727 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3728 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3729 {get_curr_buffer, "curr-buff", 0, 0},
3730 {get_buffer_count, "buff-count", 0, 0},
3731 {get_next_buffer, "get-next-buff", 0, 1},
3732 {get_prev_buffer, "get-prev-buff", 0, 1},
3733 {mzscheme_open_buffer, "open-buff", 1, 1},
3734 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3735 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003736 /*
3737 * Window-related commands
3738 */
3739 {get_curr_win, "curr-win", 0, 0},
3740 {get_window_count, "win-count", 0, 0},
3741 {get_window_by_num, "get-win-by-num", 1, 1},
3742 {get_window_num, "get-win-num", 0, 1},
3743 {get_window_buffer, "get-win-buffer", 0, 1},
3744 {get_window_height, "get-win-height", 0, 1},
3745 {set_window_height, "set-win-height", 1, 2},
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003746#ifdef FEAT_WINDOWS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003747 {get_window_width, "get-win-width", 0, 1},
3748 {set_window_width, "set-win-width", 1, 2},
3749#endif
3750 {get_cursor, "get-cursor", 0, 1},
3751 {set_cursor, "set-cursor", 1, 2},
3752 {get_window_list, "get-win-list", 0, 1},
3753 /*
3754 * Vim-related commands
3755 */
3756 {vim_command, "command", 1, 1},
3757 {vim_eval, "eval", 1, 1},
3758 {get_range_start, "range-start", 0, 0},
3759 {get_range_end, "range-end", 0, 0},
3760 {mzscheme_beep, "beep", 0, 0},
3761 {get_option, "get-option", 1, 2},
3762 {set_option, "set-option", 1, 2},
3763 /*
3764 * small utilities
3765 */
3766 {vim_bufferp, "buff?", 1, 1},
3767 {vim_windowp, "win?", 1, 1},
3768 {vim_buffer_validp, "buff-valid?", 1, 1},
3769 {vim_window_validp, "win-valid?", 1, 1}
3770};
3771
3772/* return MzScheme wrapper for curbuf */
3773 static vim_mz_buffer *
3774get_vim_curr_buffer(void)
3775{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003776 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003777 return (vim_mz_buffer *)buffer_new(curbuf);
3778 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003779 return BUFFER_REF(curbuf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003780}
3781
3782/* return MzScheme wrapper for curwin */
3783 static vim_mz_window *
3784get_vim_curr_window(void)
3785{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003786 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003787 return (vim_mz_window *)window_new(curwin);
3788 else
Bram Moolenaar75676462013-01-30 14:55:42 +01003789 return WINDOW_REF(curwin);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003790}
3791
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003792 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003793make_modules(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003794{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003795 int i;
3796 Scheme_Env *mod = NULL;
3797 Scheme_Object *vimext_symbol = NULL;
3798 Scheme_Object *closed_prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003799
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003800 MZ_GC_DECL_REG(3);
3801 MZ_GC_VAR_IN_REG(0, mod);
3802 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3803 MZ_GC_VAR_IN_REG(2, closed_prim);
3804 MZ_GC_REG();
3805
3806 vimext_symbol = scheme_intern_symbol("vimext");
3807 MZ_GC_CHECK();
3808 mod = scheme_primitive_module(vimext_symbol, environment);
3809 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003810 /* all prims made closed so they can access their own names */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003811 for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003812 {
3813 Vim_Prim *prim = prims + i;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003814 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3815 prim->mina, prim->maxa);
3816 scheme_add_global(prim->name, closed_prim, mod);
3817 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003818 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003819 scheme_finish_primitive_module(mod);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003820 MZ_GC_CHECK();
3821 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003822}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003823
Bram Moolenaar555b2802005-05-19 21:08:39 +00003824#ifdef HAVE_SANDBOX
3825static Scheme_Object *M_write = NULL;
3826static Scheme_Object *M_read = NULL;
3827static Scheme_Object *M_execute = NULL;
3828static Scheme_Object *M_delete = NULL;
3829
3830 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003831sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003832{
3833 if (sandbox)
3834 raise_vim_exn(_("not allowed in the Vim sandbox"));
3835}
3836
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003837/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003838 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003839sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003840{
3841 if (sandbox)
3842 {
3843 Scheme_Object *requested_access = argv[2];
3844
3845 if (M_write == NULL)
3846 {
3847 MZ_REGISTER_STATIC(M_write);
3848 M_write = scheme_intern_symbol("write");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003849 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003850 }
3851 if (M_read == NULL)
3852 {
3853 MZ_REGISTER_STATIC(M_read);
3854 M_read = scheme_intern_symbol("read");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003855 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003856 }
3857 if (M_execute == NULL)
3858 {
3859 MZ_REGISTER_STATIC(M_execute);
3860 M_execute = scheme_intern_symbol("execute");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003861 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003862 }
3863 if (M_delete == NULL)
3864 {
3865 MZ_REGISTER_STATIC(M_delete);
3866 M_delete = scheme_intern_symbol("delete");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003867 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003868 }
3869
3870 while (!SCHEME_NULLP(requested_access))
3871 {
3872 Scheme_Object *item = SCHEME_CAR(requested_access);
3873 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3874 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
3875 {
3876 raise_vim_exn(_("not allowed in the Vim sandbox"));
3877 }
3878 requested_access = SCHEME_CDR(requested_access);
3879 }
3880 }
3881 return scheme_void;
3882}
3883
3884 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003885sandbox_network_guard(int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003886{
3887 return scheme_void;
3888}
3889#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00003890
3891#endif