blob: 9a92fee87ad385a6d119df7a9ffaaa2ab88118b2 [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 Moolenaar325b7a22004-07-05 15:58:32 +00004 * Original work by Brent Fulgham <bfulgham@debian.org>
5 * (Based on lots of help from Matthew Flatt)
6 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00007 * TODO Convert byte-strings to char strings?
8 *
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009 * This consists of six parts:
10 * 1. MzScheme interpreter main program
11 * 2. Routines that handle the external interface between MzScheme and
12 * Vim.
13 * 3. MzScheme input/output handlers: writes output via [e]msg().
14 * 4. Implementation of the Vim Features for MzScheme
15 * 5. Vim Window-related Manipulation Functions.
16 * 6. Vim Buffer-related Manipulation Functions
17 *
18 * NOTES
19 * 1. Memory, allocated with scheme_malloc*, need not to be freed explicitly,
20 * garbage collector will do it self
21 * 2. Requires at least NORMAL features. I can't imagine why one may want
22 * to build with SMALL or TINY features but with MzScheme interface.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000023 * 3. I don't use K&R-style functions. Anyways, MzScheme headers are ANSI.
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024 */
25
Bram Moolenaar325b7a22004-07-05 15:58:32 +000026#include "vim.h"
Bram Moolenaar049377e2007-05-12 15:32:12 +000027
Bram Moolenaar325b7a22004-07-05 15:58:32 +000028#include "if_mzsch.h"
29
Bram Moolenaar76b92b22006-03-24 22:46:53 +000030/* Only do the following when the feature is enabled. Needed for "make
31 * depend". */
32#if defined(FEAT_MZSCHEME) || defined(PROTO)
33
Bram Moolenaar325b7a22004-07-05 15:58:32 +000034/* Base data structures */
35#define SCHEME_VIMBUFFERP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_buffer_type)
36#define SCHEME_VIMWINDOWP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_window_type)
37
38typedef struct
39{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000040 Scheme_Object so;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000041 buf_T *buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000042} vim_mz_buffer;
43
44#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
45
46typedef struct
47{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000048 Scheme_Object so;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000049 win_T *win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000050} vim_mz_window;
51
52#define INVALID_WINDOW_VALUE ((win_T *)(-1))
53
54/*
55 * Prims that form MzScheme Vim interface
56 */
57typedef struct
58{
59 Scheme_Closed_Prim *prim;
60 char *name;
61 int mina; /* arity information */
62 int maxa;
63} Vim_Prim;
64
65typedef struct
66{
67 char *name;
68 Scheme_Object *port;
69} Port_Info;
70
Bram Moolenaar325b7a22004-07-05 15:58:32 +000071/*
72 *========================================================================
73 * Vim-Control Commands
74 *========================================================================
75 */
76/*
77 *========================================================================
78 * Utility functions for the vim/mzscheme interface
79 *========================================================================
80 */
Bram Moolenaar555b2802005-05-19 21:08:39 +000081#ifdef HAVE_SANDBOX
82static Scheme_Object *sandbox_file_guard(int, Scheme_Object **);
83static Scheme_Object *sandbox_network_guard(int, Scheme_Object **);
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000084static void sandbox_check(void);
Bram Moolenaar555b2802005-05-19 21:08:39 +000085#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000086/* Buffer-related commands */
87static Scheme_Object *buffer_new(buf_T *buf);
88static Scheme_Object *get_buffer_by_name(void *, int, Scheme_Object **);
89static Scheme_Object *get_buffer_by_num(void *, int, Scheme_Object **);
90static Scheme_Object *get_buffer_count(void *, int, Scheme_Object **);
91static Scheme_Object *get_buffer_line(void *, int, Scheme_Object **);
92static Scheme_Object *get_buffer_line_list(void *, int, Scheme_Object **);
93static Scheme_Object *get_buffer_name(void *, int, Scheme_Object **);
94static Scheme_Object *get_buffer_num(void *, int, Scheme_Object **);
95static Scheme_Object *get_buffer_size(void *, int, Scheme_Object **);
96static Scheme_Object *get_curr_buffer(void *, int, Scheme_Object **);
97static Scheme_Object *get_next_buffer(void *, int, Scheme_Object **);
98static Scheme_Object *get_prev_buffer(void *, int, Scheme_Object **);
99static Scheme_Object *mzscheme_open_buffer(void *, int, Scheme_Object **);
100static Scheme_Object *set_buffer_line(void *, int, Scheme_Object **);
101static Scheme_Object *set_buffer_line_list(void *, int, Scheme_Object **);
102static Scheme_Object *insert_buffer_line_list(void *, int, Scheme_Object **);
103static Scheme_Object *get_range_start(void *, int, Scheme_Object **);
104static Scheme_Object *get_range_end(void *, int, Scheme_Object **);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000105static vim_mz_buffer *get_vim_curr_buffer(void);
106
107/* Window-related commands */
108static Scheme_Object *window_new(win_T *win);
109static Scheme_Object *get_curr_win(void *, int, Scheme_Object **);
110static Scheme_Object *get_window_count(void *, int, Scheme_Object **);
111static Scheme_Object *get_window_by_num(void *, int, Scheme_Object **);
112static Scheme_Object *get_window_num(void *, int, Scheme_Object **);
113static Scheme_Object *get_window_buffer(void *, int, Scheme_Object **);
114static Scheme_Object *get_window_height(void *, int, Scheme_Object **);
115static Scheme_Object *set_window_height(void *, int, Scheme_Object **);
116#ifdef FEAT_VERTSPLIT
117static Scheme_Object *get_window_width(void *, int, Scheme_Object **);
118static Scheme_Object *set_window_width(void *, int, Scheme_Object **);
119#endif
120static Scheme_Object *get_cursor(void *, int, Scheme_Object **);
121static Scheme_Object *set_cursor(void *, int, Scheme_Object **);
122static Scheme_Object *get_window_list(void *, int, Scheme_Object **);
123static vim_mz_window *get_vim_curr_window(void);
124
125/* Vim-related commands */
126static Scheme_Object *mzscheme_beep(void *, int, Scheme_Object **);
127static Scheme_Object *get_option(void *, int, Scheme_Object **);
128static Scheme_Object *set_option(void *, int, Scheme_Object **);
129static Scheme_Object *vim_command(void *, int, Scheme_Object **);
130static Scheme_Object *vim_eval(void *, int, Scheme_Object **);
131static Scheme_Object *vim_bufferp(void *data, int, Scheme_Object **);
132static Scheme_Object *vim_windowp(void *data, int, Scheme_Object **);
133static Scheme_Object *vim_buffer_validp(void *data, int, Scheme_Object **);
134static Scheme_Object *vim_window_validp(void *data, int, Scheme_Object **);
135
136/*
137 *========================================================================
138 * Internal Function Prototypes
139 *========================================================================
140 */
141static int vim_error_check(void);
142static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
143static void startup_mzscheme(void);
144static char *string_to_line(Scheme_Object *obj);
Bram Moolenaar02e14d62012-11-28 15:37:51 +0100145static void do_output(char *mesg, intptr_t len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000146static void do_printf(char *format, ...);
147static void do_flush(void);
148static Scheme_Object *_apply_thunk_catch_exceptions(
149 Scheme_Object *, Scheme_Object **);
150static Scheme_Object *extract_exn_message(Scheme_Object *v);
151static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
152static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000153static void register_vim_exn(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000154static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
155 int argc, Scheme_Object **argv);
156static vim_mz_window *get_window_arg(const char *fname, int argnum,
157 int argc, Scheme_Object **argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000158static int line_in_range(linenr_T, buf_T *);
159static void check_line_range(linenr_T, buf_T *);
160static void mz_fix_cursor(int lo, int hi, int extra);
161
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000162static int eval_with_exn_handling(void *, Scheme_Closed_Prim *,
163 Scheme_Object **ret);
164static void make_modules(void);
165static void init_exn_catching_apply(void);
166static int mzscheme_env_main(Scheme_Env *env, int argc, char **argv);
167static int mzscheme_init(void);
168#ifdef FEAT_EVAL
169static Scheme_Object *vim_to_mzscheme(typval_T *vim_value, int depth,
170 Scheme_Hash_Table *visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100171static int mzscheme_to_vim(Scheme_Object *obj, typval_T *tv, int depth,
172 Scheme_Hash_Table *visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000173#endif
174
175#ifdef MZ_PRECISE_GC
Bram Moolenaar64404472010-06-26 06:24:45 +0200176static int buffer_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000177{
178 return gcBYTES_TO_WORDS(sizeof(vim_mz_buffer));
179}
180static int buffer_mark_proc(void *obj)
181{
182 return buffer_size_proc(obj);
183}
184static int buffer_fixup_proc(void *obj)
185{
186 return buffer_size_proc(obj);
187}
Bram Moolenaar64404472010-06-26 06:24:45 +0200188static int window_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000189{
190 return gcBYTES_TO_WORDS(sizeof(vim_mz_window));
191}
192static int window_mark_proc(void *obj)
193{
194 return window_size_proc(obj);
195}
196static int window_fixup_proc(void *obj)
197{
198 return window_size_proc(obj);
199}
200#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000201
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#ifdef DYNAMIC_MZSCHEME
203
204static Scheme_Object *dll_scheme_eof;
205static Scheme_Object *dll_scheme_false;
206static Scheme_Object *dll_scheme_void;
207static Scheme_Object *dll_scheme_null;
208static Scheme_Object *dll_scheme_true;
209
210static Scheme_Thread **dll_scheme_current_thread_ptr;
211
212static void (**dll_scheme_console_printf_ptr)(char *str, ...);
213static void (**dll_scheme_console_output_ptr)(char *str, long len);
214static void (**dll_scheme_notify_multithread_ptr)(int on);
215
216static void *(*dll_GC_malloc)(size_t size_in_bytes);
217static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
218static Scheme_Env *(*dll_scheme_basic_env)(void);
219static void (*dll_scheme_check_threads)(void);
220static void (*dll_scheme_register_static)(void *ptr, long size);
221static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
222static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
223 Scheme_Env *env);
224static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
225 Scheme_Object *val, Scheme_Env *env);
226static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
227 Scheme_Object **rands);
228static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000229# if MZSCHEME_VERSION_MAJOR >= 299
230static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
231# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000232static void (*dll_scheme_close_input_port)(Scheme_Object *port);
233static void (*dll_scheme_count_lines)(Scheme_Object *port);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000234#if MZSCHEME_VERSION_MAJOR < 360
Bram Moolenaar33570922005-01-25 22:26:29 +0000235static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000236#else
237static Scheme_Object *(*dll_scheme_current_continuation_marks)(Scheme_Object *prompt_tag);
238#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000239static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
240static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, long *len);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000241static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
Bram Moolenaar33570922005-01-25 22:26:29 +0000242static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
243 int _num_rands, Scheme_Object **rands, int val);
244static void (*dll_scheme_dont_gc_ptr)(void *p);
245static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
246static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
247 Scheme_Env *env);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000248static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
Bram Moolenaar33570922005-01-25 22:26:29 +0000249 Scheme_Env *env, int all);
250static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000251# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000252static char *(*dll_scheme_format)(char *format, int flen, int argc,
253 Scheme_Object **argv, long *rlen);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000254# else
255static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
256 Scheme_Object **argv, long *rlen);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000257static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000258# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000259static void (*dll_scheme_gc_ptr_ok)(void *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000260# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000261static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
262 long *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000263# else
264static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
265 long *len);
266# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000267static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
268static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
269 Scheme_Env *env);
270static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
271 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
272 mzshort maxa);
273static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000274static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
Bram Moolenaar33570922005-01-25 22:26:29 +0000275 Scheme_Object *cdr);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000276static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
277 const char *name, mzshort mina, mzshort maxa);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000278# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000279static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
280static Scheme_Object *(*dll_scheme_make_string_output_port)();
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000281# else
282static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
283static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
284# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000285static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
286 int argc, Scheme_Object **argv);
287static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
288 Scheme_Object *field_names, int flags, int *count_out);
289static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
290 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
291 int num_uninit_fields, Scheme_Object *uninit_val,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000292 Scheme_Object *properties
293# if MZSCHEME_VERSION_MAJOR >= 299
294 , Scheme_Object *guard
295# endif
296 );
Bram Moolenaar33570922005-01-25 22:26:29 +0000297static Scheme_Object **(*dll_scheme_make_struct_values)(
298 Scheme_Object *struct_type, Scheme_Object **names, int count,
299 int flags);
300static Scheme_Type (*dll_scheme_make_type)(const char *name);
301static Scheme_Object *(*dll_scheme_make_vector)(int size,
302 Scheme_Object *fill);
303static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
304static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
305 const char *who);
306static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
307 Scheme_Env *for_env);
308static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
309static void (*dll_scheme_raise)(Scheme_Object *exn);
310static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
311static void (*dll_scheme_signal_error)(const char *msg, ...);
312static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
313 int which, int argc, Scheme_Object **argv);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000314# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000315static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000316 Scheme_Object *o);
317static Scheme_Config *(*dll_scheme_current_config)(void);
318static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
319 (Scheme_Object *s);
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000320static Scheme_Object *(*dll_scheme_char_string_to_path)
321 (Scheme_Object *s);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000322# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000323static Scheme_Hash_Table *(*dll_scheme_make_hash_table)(int type);
324static void (*dll_scheme_hash_set)(Scheme_Hash_Table *table,
325 Scheme_Object *key, Scheme_Object *value);
326static Scheme_Object *(*dll_scheme_hash_get)(Scheme_Hash_Table *table,
327 Scheme_Object *key);
328static Scheme_Object *(*dll_scheme_make_double)(double d);
329# ifdef INCLUDE_MZSCHEME_BASE
330static Scheme_Object *(*dll_scheme_make_sized_byte_string)(char *chars,
331 long len, int copy);
332static Scheme_Object *(*dll_scheme_namespace_require)(Scheme_Object *req);
333# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000334
335/* arrays are imported directly */
336# define scheme_eof dll_scheme_eof
337# define scheme_false dll_scheme_false
338# define scheme_void dll_scheme_void
339# define scheme_null dll_scheme_null
340# define scheme_true dll_scheme_true
341
342/* pointers are GetProceAddress'ed as pointers to pointer */
343# define scheme_current_thread (*dll_scheme_current_thread_ptr)
344# define scheme_console_printf (*dll_scheme_console_printf_ptr)
345# define scheme_console_output (*dll_scheme_console_output_ptr)
346# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
347
348/* and functions in a usual way */
349# define GC_malloc dll_GC_malloc
350# define GC_malloc_atomic dll_GC_malloc_atomic
351
352# define scheme_add_global dll_scheme_add_global
353# define scheme_add_global_symbol dll_scheme_add_global_symbol
354# define scheme_apply dll_scheme_apply
355# define scheme_basic_env dll_scheme_basic_env
356# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000357# if MZSCHEME_VERSION_MAJOR >= 299
358# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
359# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000360# define scheme_check_threads dll_scheme_check_threads
361# define scheme_close_input_port dll_scheme_close_input_port
362# define scheme_count_lines dll_scheme_count_lines
363# define scheme_current_continuation_marks \
364 dll_scheme_current_continuation_marks
365# define scheme_display dll_scheme_display
366# define scheme_display_to_string dll_scheme_display_to_string
367# define scheme_do_eval dll_scheme_do_eval
368# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000369# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000370# define scheme_eval dll_scheme_eval
371# define scheme_eval_string dll_scheme_eval_string
372# define scheme_eval_string_all dll_scheme_eval_string_all
373# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000374# if MZSCHEME_VERSION_MAJOR < 299
375# define scheme_format dll_scheme_format
376# else
377# define scheme_format_utf8 dll_scheme_format_utf8
378# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000379# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000380# if MZSCHEME_VERSION_MAJOR < 299
381# define scheme_get_sized_string_output dll_scheme_get_sized_string_output
382# else
383# define scheme_get_sized_byte_string_output \
384 dll_scheme_get_sized_byte_string_output
Bram Moolenaar555b2802005-05-19 21:08:39 +0000385# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000386# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000387# define scheme_intern_symbol dll_scheme_intern_symbol
388# define scheme_lookup_global dll_scheme_lookup_global
389# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
390# define scheme_make_integer_value dll_scheme_make_integer_value
Bram Moolenaar33570922005-01-25 22:26:29 +0000391# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000392# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000393# if MZSCHEME_VERSION_MAJOR < 299
394# define scheme_make_string dll_scheme_make_string
395# define scheme_make_string_output_port dll_scheme_make_string_output_port
396# else
397# define scheme_make_byte_string dll_scheme_make_byte_string
398# define scheme_make_byte_string_output_port \
399 dll_scheme_make_byte_string_output_port
400# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000401# define scheme_make_struct_instance dll_scheme_make_struct_instance
402# define scheme_make_struct_names dll_scheme_make_struct_names
403# define scheme_make_struct_type dll_scheme_make_struct_type
404# define scheme_make_struct_values dll_scheme_make_struct_values
405# define scheme_make_type dll_scheme_make_type
406# define scheme_make_vector dll_scheme_make_vector
407# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
408# define scheme_open_input_file dll_scheme_open_input_file
409# define scheme_primitive_module dll_scheme_primitive_module
410# define scheme_proper_list_length dll_scheme_proper_list_length
411# define scheme_raise dll_scheme_raise
412# define scheme_read dll_scheme_read
413# define scheme_register_static dll_scheme_register_static
414# define scheme_set_stack_base dll_scheme_set_stack_base
415# define scheme_signal_error dll_scheme_signal_error
416# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000417# if MZSCHEME_VERSION_MAJOR >= 299
418# define scheme_set_param dll_scheme_set_param
419# define scheme_current_config dll_scheme_current_config
420# define scheme_char_string_to_byte_string \
421 dll_scheme_char_string_to_byte_string
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000422# define scheme_char_string_to_path \
423 dll_scheme_char_string_to_path
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000424# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000425# define scheme_make_hash_table dll_scheme_make_hash_table
426# define scheme_hash_set dll_scheme_hash_set
427# define scheme_hash_get dll_scheme_hash_get
428# define scheme_make_double dll_scheme_make_double
429# ifdef INCLUDE_MZSCHEME_BASE
430# define scheme_make_sized_byte_string dll_scheme_make_sized_byte_string
431# define scheme_namespace_require dll_scheme_namespace_require
432# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000433
434typedef struct
435{
436 char *name;
437 void **ptr;
438} Thunk_Info;
439
440static Thunk_Info mzgc_imports[] = {
441 {"GC_malloc", (void **)&dll_GC_malloc},
442 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
443 {NULL, NULL}};
444
445static Thunk_Info mzsch_imports[] = {
446 {"scheme_eof", (void **)&dll_scheme_eof},
447 {"scheme_false", (void **)&dll_scheme_false},
448 {"scheme_void", (void **)&dll_scheme_void},
449 {"scheme_null", (void **)&dll_scheme_null},
450 {"scheme_true", (void **)&dll_scheme_true},
451 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
452 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
453 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000454 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000455 (void **)&dll_scheme_notify_multithread_ptr},
456 {"scheme_add_global", (void **)&dll_scheme_add_global},
457 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
458 {"scheme_apply", (void **)&dll_scheme_apply},
459 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000460# if MZSCHEME_VERSION_MAJOR >= 299
461 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
462# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
464 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
465 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
466 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000467 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000468 (void **)&dll_scheme_current_continuation_marks},
469 {"scheme_display", (void **)&dll_scheme_display},
470 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
471 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
472 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000473 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000474 {"scheme_eval", (void **)&dll_scheme_eval},
475 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
476 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000477 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000478 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000479# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000480 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000481# else
482 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000483 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000486# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000487 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000488 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000489# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000490 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000491 (void **)&dll_scheme_get_sized_byte_string_output},
492#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000493 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
494 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000495 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000496 (void **)&dll_scheme_make_closed_prim_w_arity},
497 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
Bram Moolenaar33570922005-01-25 22:26:29 +0000498 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000499 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000500# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000501 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000502 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000503 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000504# else
505 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000506 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000507 (void **)&dll_scheme_make_byte_string_output_port},
508# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000509 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000510 (void **)&dll_scheme_make_struct_instance},
511 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
512 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
513 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
514 {"scheme_make_type", (void **)&dll_scheme_make_type},
515 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
516 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
517 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
518 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
519 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
520 {"scheme_raise", (void **)&dll_scheme_raise},
521 {"scheme_read", (void **)&dll_scheme_read},
522 {"scheme_register_static", (void **)&dll_scheme_register_static},
523 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
524 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
525 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000526# if MZSCHEME_VERSION_MAJOR >= 299
527 {"scheme_set_param", (void **)&dll_scheme_set_param},
528 {"scheme_current_config", (void **)&dll_scheme_current_config},
529 {"scheme_char_string_to_byte_string",
530 (void **)&dll_scheme_char_string_to_byte_string},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000531 {"scheme_char_string_to_path", (void **)&dll_scheme_char_string_to_path},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000532# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000533 {"scheme_make_hash_table", (void **)&dll_scheme_make_hash_table},
534 {"scheme_hash_set", (void **)&dll_scheme_hash_set},
535 {"scheme_hash_get", (void **)&dll_scheme_hash_get},
536 {"scheme_make_double", (void **)&dll_scheme_make_double},
537# ifdef INCLUDE_MZSCHEME_BASE
538 {"scheme_make_sized_byte_string", (void **)&dll_scheme_make_sized_byte_string},
539 {"scheme_namespace_require", (void **)&dll_scheme_namespace_require},
540#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000541 {NULL, NULL}};
542
543static HINSTANCE hMzGC = 0;
544static HINSTANCE hMzSch = 0;
545
546static void dynamic_mzscheme_end(void);
547static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
548 int verbose);
549
550 static int
551mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
552{
553 Thunk_Info *thunk = NULL;
554
555 if (hMzGC && hMzSch)
556 return OK;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200557 hMzSch = vimLoadLib(sch_dll);
558 hMzGC = vimLoadLib(gc_dll);
Bram Moolenaar33570922005-01-25 22:26:29 +0000559
Bram Moolenaar33570922005-01-25 22:26:29 +0000560 if (!hMzGC)
561 {
562 if (verbose)
563 EMSG2(_(e_loadlib), gc_dll);
564 return FAIL;
565 }
566
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100567 if (!hMzSch)
568 {
569 if (verbose)
570 EMSG2(_(e_loadlib), sch_dll);
571 return FAIL;
572 }
573
Bram Moolenaar33570922005-01-25 22:26:29 +0000574 for (thunk = mzsch_imports; thunk->name; thunk++)
575 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000576 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000577 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
578 {
579 FreeLibrary(hMzSch);
580 hMzSch = 0;
581 FreeLibrary(hMzGC);
582 hMzGC = 0;
583 if (verbose)
584 EMSG2(_(e_loadfunc), thunk->name);
585 return FAIL;
586 }
587 }
588 for (thunk = mzgc_imports; thunk->name; thunk++)
589 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000590 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000591 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
592 {
593 FreeLibrary(hMzSch);
594 hMzSch = 0;
595 FreeLibrary(hMzGC);
596 hMzGC = 0;
597 if (verbose)
598 EMSG2(_(e_loadfunc), thunk->name);
599 return FAIL;
600 }
601 }
602 return OK;
603}
604
605 int
606mzscheme_enabled(int verbose)
607{
608 return mzscheme_runtime_link_init(
609 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
610}
611
612 static void
613dynamic_mzscheme_end(void)
614{
615 if (hMzSch)
616 {
617 FreeLibrary(hMzSch);
618 hMzSch = 0;
619 }
620 if (hMzGC)
621 {
622 FreeLibrary(hMzGC);
623 hMzGC = 0;
624 }
625}
626#endif /* DYNAMIC_MZSCHEME */
627
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000628/* need to put it here for dynamic stuff to work */
Bram Moolenaare484c942009-09-11 10:21:41 +0000629#if defined(INCLUDE_MZSCHEME_BASE)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000630# include "mzscheme_base.c"
Bram Moolenaare484c942009-09-11 10:21:41 +0000631#elif MZSCHEME_VERSION_MAJOR >= 400
632# error MzScheme 4.x must include mzscheme_base.c, for MinGW32 you need to define MZSCHEME_GENERATE_BASE=yes
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000633#endif
634
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000635/*
636 *========================================================================
637 * 1. MzScheme interpreter startup
638 *========================================================================
639 */
640
641static Scheme_Type mz_buffer_type;
642static Scheme_Type mz_window_type;
643
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000644static int initialized = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000645
646/* global environment */
647static Scheme_Env *environment = NULL;
648/* output/error handlers */
649static Scheme_Object *curout = NULL;
650static Scheme_Object *curerr = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000651/* exn:vim exception */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000652static Scheme_Object *exn_catching_apply = NULL;
653static Scheme_Object *exn_p = NULL;
654static Scheme_Object *exn_message = NULL;
655static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000656
657#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
658static void *stack_base = NULL;
659#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000660
661static long range_start;
662static long range_end;
663
664/* MzScheme threads scheduling stuff */
665static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000666
667#if defined(FEAT_GUI_W32)
668static void CALLBACK timer_proc(HWND, UINT, UINT, DWORD);
669static UINT timer_id = 0;
670#elif defined(FEAT_GUI_GTK)
671static gint timer_proc(gpointer);
672static guint timer_id = 0;
673#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
674static void timer_proc(XtPointer, XtIntervalId *);
675static XtIntervalId timer_id = (XtIntervalId)0;
676#elif defined(FEAT_GUI_MAC)
677pascal void timer_proc(EventLoopTimerRef, void *);
678static EventLoopTimerRef timer_id = NULL;
679static EventLoopTimerUPP timerUPP;
680#endif
681
682#ifndef FEAT_GUI_W32 /* Win32 console and Unix */
683 void
684mzvim_check_threads(void)
685{
686 /* Last time MzScheme threads were scheduled */
687 static time_t mz_last_time = 0;
688
689 if (mz_threads_allow && p_mzq > 0)
690 {
691 time_t now = time(NULL);
692
693 if ((now - mz_last_time) * 1000 > p_mzq)
694 {
695 mz_last_time = now;
696 scheme_check_threads();
697 }
698 }
699}
700#endif
701
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000702#ifdef MZSCHEME_GUI_THREADS
703static void setup_timer(void);
704static void remove_timer(void);
705
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000706/* timers are presented in GUI only */
707# if defined(FEAT_GUI_W32)
708 static void CALLBACK
Bram Moolenaar64404472010-06-26 06:24:45 +0200709timer_proc(HWND hwnd UNUSED, UINT uMsg UNUSED, UINT idEvent UNUSED, DWORD dwTime UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000710# elif defined(FEAT_GUI_GTK)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000711 static gint
Bram Moolenaar64404472010-06-26 06:24:45 +0200712timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000713# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000714 static void
Bram Moolenaar64404472010-06-26 06:24:45 +0200715timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000716# elif defined(FEAT_GUI_MAC)
717 pascal void
Bram Moolenaar64404472010-06-26 06:24:45 +0200718timer_proc(EventLoopTimerRef theTimer UNUSED, void *userData UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000719# endif
720{
721 scheme_check_threads();
722# if defined(FEAT_GUI_GTK)
723 return TRUE; /* continue receiving notifications */
724# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
725 /* renew timeout */
726 if (mz_threads_allow && p_mzq > 0)
727 timer_id = XtAppAddTimeOut(app_context, p_mzq,
728 timer_proc, NULL);
729# endif
730}
731
732 static void
733setup_timer(void)
734{
735# if defined(FEAT_GUI_W32)
736 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
737# elif defined(FEAT_GUI_GTK)
738 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
739# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
740 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
741# elif defined(FEAT_GUI_MAC)
742 timerUPP = NewEventLoopTimerUPP(timer_proc);
743 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
744 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
745# endif
746}
747
748 static void
749remove_timer(void)
750{
751# if defined(FEAT_GUI_W32)
752 KillTimer(NULL, timer_id);
753# elif defined(FEAT_GUI_GTK)
754 gtk_timeout_remove(timer_id);
755# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
756 XtRemoveTimeOut(timer_id);
757# elif defined(FEAT_GUI_MAC)
758 RemoveEventLoopTimer(timer_id);
759 DisposeEventLoopTimerUPP(timerUPP);
760# endif
761 timer_id = 0;
762}
763
764 void
765mzvim_reset_timer(void)
766{
767 if (timer_id != 0)
768 remove_timer();
769 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
770 setup_timer();
771}
772
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000773#endif /* MZSCHEME_GUI_THREADS */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000774
775 static void
776notify_multithread(int on)
777{
778 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000779#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000780 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
781 setup_timer();
782 if (!on && timer_id != 0)
783 remove_timer();
784#endif
785}
786
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000787 void
788mzscheme_end(void)
789{
Bram Moolenaar33570922005-01-25 22:26:29 +0000790#ifdef DYNAMIC_MZSCHEME
791 dynamic_mzscheme_end();
792#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000793}
794
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100795#if MZSCHEME_VERSION_MAJOR >= 500 && defined(WIN32) && defined(USE_THREAD_LOCAL)
796static __declspec(thread) void *tls_space;
797#endif
798
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100799/*
800 * Since version 4.x precise GC requires trampolined startup.
801 * Futures and places in version 5.x need it too.
802 */
803#if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR >= 400 \
804 || MZSCHEME_VERSION_MAJOR >= 500 && (defined(MZ_USE_FUTURES) || defined(MZ_USE_PLACES))
805# ifdef DYNAMIC_MZSCHEME
806# error Precise GC v.4+ or Racket with futures/places do not support dynamic MzScheme
807# endif
808# define TRAMPOLINED_MZVIM_STARTUP
809#endif
810
811 int
812mzscheme_main(int argc, char** argv)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000813{
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100814#if MZSCHEME_VERSION_MAJOR >= 500 && defined(WIN32) && defined(USE_THREAD_LOCAL)
815 scheme_register_tls_space(&tls_space, 0);
816#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100817#ifdef TRAMPOLINED_MZVIM_STARTUP
818 return scheme_main_setup(TRUE, mzscheme_env_main, argc, argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000819#else
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100820 return mzscheme_env_main(NULL, argc, argv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000821#endif
822}
823
824 static int
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100825mzscheme_env_main(Scheme_Env *env, int argc, char **argv)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000826{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100827 int vim_main_result;
828#ifdef TRAMPOLINED_MZVIM_STARTUP
829 /* Scheme has created the environment for us */
830 environment = env;
831#else
832# ifdef MZ_PRECISE_GC
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000833 Scheme_Object *dummy = NULL;
834 MZ_GC_DECL_REG(1);
835 MZ_GC_VAR_IN_REG(0, dummy);
836
837 stack_base = &__gc_var_stack__;
838# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000839 int dummy = 0;
840 stack_base = (void *)&dummy;
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100841# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000842#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100843
844 /* mzscheme_main is called as a trampoline from main.
845 * We trampoline into vim_main2
846 * Passing argc, argv through from mzscheme_main
847 */
848 vim_main_result = vim_main2(argc, argv);
849#if !defined(TRAMPOLINED_MZVIM_STARTUP) && defined(MZ_PRECISE_GC)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000850 /* releasing dummy */
851 MZ_GC_REG();
852 MZ_GC_UNREG();
853#endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100854 return vim_main_result;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000855}
856
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000857 static void
858startup_mzscheme(void)
859{
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100860#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000861 scheme_set_stack_base(stack_base, 1);
862#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000863
864 MZ_REGISTER_STATIC(environment);
865 MZ_REGISTER_STATIC(curout);
866 MZ_REGISTER_STATIC(curerr);
867 MZ_REGISTER_STATIC(exn_catching_apply);
868 MZ_REGISTER_STATIC(exn_p);
869 MZ_REGISTER_STATIC(exn_message);
870 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000871
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100872#ifndef TRAMPOLINED_MZVIM_STARTUP
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000873 /* in newer versions of precise GC the initial env has been created */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000874 environment = scheme_basic_env();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000875#endif
876 MZ_GC_CHECK();
877
878#ifdef INCLUDE_MZSCHEME_BASE
879 {
880 /*
Bram Moolenaare484c942009-09-11 10:21:41 +0000881 * versions 4.x do not provide Scheme bindings by default
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000882 * we need to add them explicitly
883 */
884 Scheme_Object *scheme_base_symbol = NULL;
885 MZ_GC_DECL_REG(1);
886 MZ_GC_VAR_IN_REG(0, scheme_base_symbol);
887 MZ_GC_REG();
Bram Moolenaare484c942009-09-11 10:21:41 +0000888 /* invoke function from generated and included mzscheme_base.c */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000889 declare_modules(environment);
890 scheme_base_symbol = scheme_intern_symbol("scheme/base");
891 MZ_GC_CHECK();
892 scheme_namespace_require(scheme_base_symbol);
893 MZ_GC_CHECK();
894 MZ_GC_UNREG();
895 }
896#endif
897 register_vim_exn();
898 /* use new environment to initialise exception handling */
899 init_exn_catching_apply();
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000900
901 /* redirect output */
902 scheme_console_output = do_output;
903 scheme_console_printf = do_printf;
904
905#ifdef MZSCHEME_COLLECTS
906 /* setup 'current-library-collection-paths' parameter */
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000907# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000908 {
909 Scheme_Object *coll_byte_string = NULL;
910 Scheme_Object *coll_char_string = NULL;
911 Scheme_Object *coll_path = NULL;
912 Scheme_Object *coll_pair = NULL;
913 Scheme_Config *config = NULL;
914
915 MZ_GC_DECL_REG(5);
916 MZ_GC_VAR_IN_REG(0, coll_byte_string);
917 MZ_GC_VAR_IN_REG(1, coll_char_string);
918 MZ_GC_VAR_IN_REG(2, coll_path);
919 MZ_GC_VAR_IN_REG(3, coll_pair);
920 MZ_GC_VAR_IN_REG(4, config);
921 MZ_GC_REG();
922 coll_byte_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
923 MZ_GC_CHECK();
924 coll_char_string = scheme_byte_string_to_char_string(coll_byte_string);
925 MZ_GC_CHECK();
926 coll_path = scheme_char_string_to_path(coll_char_string);
927 MZ_GC_CHECK();
928 coll_pair = scheme_make_pair(coll_path, scheme_null);
929 MZ_GC_CHECK();
930 config = scheme_config;
931 MZ_GC_CHECK();
932 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
933 MZ_GC_CHECK();
934 MZ_GC_UNREG();
935 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000936# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000937 {
938 Scheme_Object *coll_string = NULL;
939 Scheme_Object *coll_pair = NULL;
940 Scheme_Config *config = NULL;
941
942 MZ_GC_DECL_REG(3);
943 MZ_GC_VAR_IN_REG(0, coll_string);
944 MZ_GC_VAR_IN_REG(1, coll_pair);
945 MZ_GC_VAR_IN_REG(2, config);
946 MZ_GC_REG();
947 coll_string = scheme_make_string(MZSCHEME_COLLECTS);
948 MZ_GC_CHECK();
949 coll_pair = scheme_make_pair(coll_string, scheme_null);
950 MZ_GC_CHECK();
951 config = scheme_config;
952 MZ_GC_CHECK();
953 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
954 MZ_GC_CHECK();
955 MZ_GC_UNREG();
956 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000957# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000958#endif
Bram Moolenaar555b2802005-05-19 21:08:39 +0000959#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +0000960 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000961 Scheme_Object *make_security_guard = NULL;
962 MZ_GC_DECL_REG(1);
963 MZ_GC_VAR_IN_REG(0, make_security_guard);
964 MZ_GC_REG();
965
966#if MZSCHEME_VERSION_MAJOR < 400
967 {
968 Scheme_Object *make_security_guard_symbol = NULL;
969 MZ_GC_DECL_REG(1);
970 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
971 MZ_GC_REG();
972 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
973 MZ_GC_CHECK();
974 make_security_guard = scheme_lookup_global(
975 make_security_guard_symbol, environment);
976 MZ_GC_UNREG();
977 }
978#else
979 make_security_guard = scheme_builtin_value("make-security-guard");
980 MZ_GC_CHECK();
981#endif
982
983 /* setup sandbox guards */
984 if (make_security_guard != NULL)
985 {
986 Scheme_Object *args[3] = {NULL, NULL, NULL};
987 Scheme_Object *guard = NULL;
988 Scheme_Config *config = NULL;
989 MZ_GC_DECL_REG(5);
990 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
991 MZ_GC_VAR_IN_REG(3, guard);
992 MZ_GC_VAR_IN_REG(4, config);
993 MZ_GC_REG();
994 config = scheme_config;
995 MZ_GC_CHECK();
996 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
997 MZ_GC_CHECK();
998 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
999 "sandbox-file-guard", 3, 3);
1000 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1001 "sandbox-network-guard", 4, 4);
1002 guard = scheme_apply(make_security_guard, 3, args);
1003 MZ_GC_CHECK();
1004 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1005 MZ_GC_CHECK();
1006 MZ_GC_UNREG();
1007 }
1008 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001009 }
1010#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001011 /* Create buffer and window types for use in Scheme code */
1012 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001013 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001014 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001015 MZ_GC_CHECK();
1016#ifdef MZ_PRECISE_GC
1017 GC_register_traversers(mz_buffer_type,
1018 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1019 TRUE, TRUE);
1020 GC_register_traversers(mz_window_type,
1021 window_size_proc, window_mark_proc, window_fixup_proc,
1022 TRUE, TRUE);
1023#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001024
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001025 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001026
1027 /*
1028 * setup callback to receive notifications
1029 * whether thread scheduling is (or not) required
1030 */
1031 scheme_notify_multithread = notify_multithread;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001032}
1033
1034/*
1035 * This routine is called for each new invocation of MzScheme
1036 * to make sure things are properly initialized.
1037 */
1038 static int
1039mzscheme_init(void)
1040{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001041 if (!initialized)
1042 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001043#ifdef DYNAMIC_MZSCHEME
1044 if (!mzscheme_enabled(TRUE))
1045 {
Bram Moolenaarb849e712009-06-24 15:51:37 +00001046 EMSG(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
Bram Moolenaar33570922005-01-25 22:26:29 +00001047 return -1;
1048 }
1049#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001050 startup_mzscheme();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001051 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001052 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001053 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001054 Scheme_Config *config = NULL;
1055 MZ_GC_DECL_REG(1);
1056 MZ_GC_VAR_IN_REG(0, config);
1057 MZ_GC_REG();
1058 config = scheme_config;
1059 MZ_GC_CHECK();
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001060 /* recreate ports each call effectively clearing these ones */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001061 curout = scheme_make_string_output_port();
1062 MZ_GC_CHECK();
1063 curerr = scheme_make_string_output_port();
1064 MZ_GC_CHECK();
1065 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1066 MZ_GC_CHECK();
1067 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1068 MZ_GC_CHECK();
1069 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001070 }
1071
1072 return 0;
1073}
1074
1075/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001076 *========================================================================
1077 * 2. External Interface
1078 *========================================================================
1079 */
1080
1081/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001082 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001083 */
1084 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001085eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001086{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001087 Scheme_Object *value = NULL;
1088 Scheme_Object *exn = NULL;
1089 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001090
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001091 MZ_GC_DECL_REG(3);
1092 MZ_GC_VAR_IN_REG(0, value);
1093 MZ_GC_VAR_IN_REG(1, exn);
1094 MZ_GC_VAR_IN_REG(2, prim);
1095 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001096
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001097 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1098 MZ_GC_CHECK();
1099 value = _apply_thunk_catch_exceptions(prim, &exn);
1100 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001101
1102 if (!value)
1103 {
1104 value = extract_exn_message(exn);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001105 /* Got an exn? */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001106 if (value)
1107 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001108 scheme_display(value, curerr); /* Send to stderr-vim */
1109 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001110 do_flush();
1111 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001112 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001113 /* `raise' was called on some arbitrary value */
1114 return FAIL;
1115 }
1116
1117 if (ret != NULL) /* if pointer to retval supported give it up */
1118 *ret = value;
1119 /* Print any result, as long as it's not a void */
1120 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001121 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001122 scheme_display(value, curout); /* Send to stdout-vim */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001123 MZ_GC_CHECK();
1124 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001125
1126 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001127 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001128 return OK;
1129}
1130
1131/* :mzscheme */
1132 static int
1133do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1134{
1135 if (mzscheme_init())
1136 return FAIL;
1137
1138 range_start = eap->line1;
1139 range_end = eap->line2;
1140
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001141 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001142}
1143
1144/*
1145 * Routine called by VIM when deleting a buffer
1146 */
1147 void
1148mzscheme_buffer_free(buf_T *buf)
1149{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001150 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001151 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001152 vim_mz_buffer *bp;
1153
Bram Moolenaare344bea2005-09-01 20:46:49 +00001154 bp = buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001155 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001156 buf->b_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001157 scheme_gc_ptr_ok(bp);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001158 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001159 }
1160}
1161
1162/*
1163 * Routine called by VIM when deleting a Window
1164 */
1165 void
1166mzscheme_window_free(win_T *win)
1167{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001168 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001169 {
1170 vim_mz_window *wp;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001171 wp = win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001172 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001173 win->w_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001174 scheme_gc_ptr_ok(wp);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001175 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001176 }
1177}
1178
1179/*
1180 * ":mzscheme" (or ":mz")
1181 */
1182 void
1183ex_mzscheme(exarg_T *eap)
1184{
1185 char_u *script;
1186
1187 script = script_get(eap, eap->arg);
1188 if (!eap->skip)
1189 {
1190 if (script == NULL)
1191 do_mzscheme_command(eap, eap->arg, do_eval);
1192 else
1193 {
1194 do_mzscheme_command(eap, script, do_eval);
1195 vim_free(script);
1196 }
1197 }
1198}
1199
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001200 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001201do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001202{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001203 Scheme_Object *expr = NULL;
1204 Scheme_Object *result = NULL;
1205 char *file = NULL;
1206 Port_Info *pinfo = (Port_Info *)data;
1207
1208 MZ_GC_DECL_REG(3);
1209 MZ_GC_VAR_IN_REG(0, expr);
1210 MZ_GC_VAR_IN_REG(1, result);
1211 MZ_GC_VAR_IN_REG(2, file);
1212 MZ_GC_REG();
1213
1214 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1215 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001216
1217 /* make Vim expansion */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001218 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001219 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001220 MZ_GC_CHECK();
1221 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1222 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001223
1224 /* Like REPL but print only last result */
1225 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001226 {
1227 result = scheme_eval(expr, environment);
1228 MZ_GC_CHECK();
1229 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001230
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001231 /* errors will be caught in do_mzscheme_command and ex_mzfile */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001232 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001233 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001234 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001235 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001236 return result;
1237}
1238
1239/* :mzfile */
1240 void
1241ex_mzfile(exarg_T *eap)
1242{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001243 Port_Info pinfo = {NULL, NULL};
1244
1245 MZ_GC_DECL_REG(1);
1246 MZ_GC_VAR_IN_REG(0, pinfo.port);
1247 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001248
1249 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001250 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1251 && pinfo.port != NULL) /* looks like port was not closed */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001252 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001253 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001254 MZ_GC_CHECK();
1255 }
1256 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001257}
1258
1259
1260/*
1261 *========================================================================
1262 * Exception handling code -- cribbed form the MzScheme sources and
1263 * Matthew Flatt's "Inside PLT MzScheme" document.
1264 *========================================================================
1265 */
1266 static void
1267init_exn_catching_apply(void)
1268{
1269 if (!exn_catching_apply)
1270 {
1271 char *e =
1272 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001273 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001274 "(cons #t (thunk))))";
1275
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001276 exn_catching_apply = scheme_eval_string(e, environment);
1277 MZ_GC_CHECK();
1278 exn_p = scheme_builtin_value("exn?");
1279 MZ_GC_CHECK();
1280 exn_message = scheme_builtin_value("exn-message");
1281 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001282 }
1283}
1284
1285/*
1286 * This function applies a thunk, returning the Scheme value if there's
1287 * no exception, otherwise returning NULL and setting *exn to the raised
1288 * value (usually an exn structure).
1289 */
1290 static Scheme_Object *
1291_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1292{
1293 Scheme_Object *v;
1294
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001295 v = _scheme_apply(exn_catching_apply, 1, &f);
1296 /* v is a pair: (cons #t value) or (cons #f exn) */
1297
1298 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1299 return SCHEME_CDR(v);
1300 else
1301 {
1302 *exn = SCHEME_CDR(v);
1303 return NULL;
1304 }
1305}
1306
1307 static Scheme_Object *
1308extract_exn_message(Scheme_Object *v)
1309{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001310 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1311 return _scheme_apply(exn_message, 1, &v);
1312 else
1313 return NULL; /* Not an exn structure */
1314}
1315
1316 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001317do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001318{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001319 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001320}
1321
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001322/*
1323 *========================================================================
1324 * 3. MzScheme I/O Handlers
1325 *========================================================================
1326 */
1327 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001328do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001329{
1330 char *p, *prev;
1331
1332 prev = mesg;
1333 p = strchr(prev, '\n');
1334 while (p)
1335 {
1336 *p = '\0';
1337 if (error)
1338 EMSG(prev);
1339 else
1340 MSG(prev);
1341 prev = p + 1;
1342 p = strchr(prev, '\n');
1343 }
1344
1345 if (error)
1346 EMSG(prev);
1347 else
1348 MSG(prev);
1349}
1350
1351 static void
Bram Moolenaar02e14d62012-11-28 15:37:51 +01001352do_output(char *mesg, intptr_t len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001353{
Bram Moolenaar02e14d62012-11-28 15:37:51 +01001354 /* TODO: use len, the string may not be NUL terminated */
Bram Moolenaar64404472010-06-26 06:24:45 +02001355 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001356}
1357
1358 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001359do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001360{
Bram Moolenaar64404472010-06-26 06:24:45 +02001361 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001362}
1363
1364 static void
1365do_printf(char *format, ...)
1366{
Bram Moolenaar64404472010-06-26 06:24:45 +02001367 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001368}
1369
1370 static void
1371do_flush(void)
1372{
1373 char *buff;
Bram Moolenaar02e14d62012-11-28 15:37:51 +01001374 intptr_t length;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001375
1376 buff = scheme_get_sized_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001377 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001378 if (length)
1379 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001380 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001381 return;
1382 }
1383
1384 buff = scheme_get_sized_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001385 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001386 if (length)
1387 do_output(buff, length);
1388}
1389
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001390/*
1391 *========================================================================
1392 * 4. Implementation of the Vim Features for MzScheme
1393 *========================================================================
1394 */
1395
1396/* (command {command-string}) */
1397 static Scheme_Object *
1398vim_command(void *data, int argc, Scheme_Object **argv)
1399{
1400 Vim_Prim *prim = (Vim_Prim *)data;
1401 char *cmd = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1402
1403 /* may be use do_cmdline_cmd? */
1404 do_cmdline((char_u *)cmd, NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
1405 update_screen(VALID);
1406
1407 raise_if_error();
1408 return scheme_void;
1409}
1410
1411/* (eval {expr-string}) */
1412 static Scheme_Object *
1413vim_eval(void *data, int argc, Scheme_Object **argv)
1414{
1415#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001416 Vim_Prim *prim = (Vim_Prim *)data;
1417 char *expr;
1418 Scheme_Object *result;
1419 /* hash table to store visited values to avoid infinite loops */
1420 Scheme_Hash_Table *visited = NULL;
1421 typval_T *vim_result;
1422
1423 MZ_GC_DECL_REG(1);
1424 MZ_GC_VAR_IN_REG(0, visited);
1425 MZ_GC_REG();
1426
1427 visited = scheme_make_hash_table(SCHEME_hash_ptr);
1428 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001429
1430 expr = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001431 vim_result = eval_expr((char_u *)expr, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001432
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001433 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001434 raise_vim_exn(_("invalid expression"));
1435
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001436 result = vim_to_mzscheme(vim_result, 1, visited);
1437 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001438
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001439 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001440 return result;
1441#else
1442 raise_vim_exn(_("expressions disabled at compile time"));
1443 /* unreachable */
1444 return scheme_false;
1445#endif
1446}
1447
1448/* (range-start) */
1449 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001450get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001451{
1452 return scheme_make_integer(range_start);
1453}
1454
1455/* (range-end) */
1456 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001457get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001458{
1459 return scheme_make_integer(range_end);
1460}
1461
1462/* (beep) */
1463 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001464mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001465{
1466 vim_beep();
1467 return scheme_void;
1468}
1469
1470static Scheme_Object *M_global = NULL;
1471
1472/* (get-option {option-name}) [buffer/window] */
1473 static Scheme_Object *
1474get_option(void *data, int argc, Scheme_Object **argv)
1475{
1476 Vim_Prim *prim = (Vim_Prim *)data;
1477 char_u *name;
1478 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001479 char *strval;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001480 int rc;
1481 Scheme_Object *rval;
1482 int opt_flags = 0;
1483 buf_T *save_curb = curbuf;
1484 win_T *save_curw = curwin;
1485
1486 name = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1487
1488 if (argc > 1)
1489 {
1490 if (M_global == NULL)
1491 {
1492 MZ_REGISTER_STATIC(M_global);
1493 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001494 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001495 }
1496
1497 if (argv[1] == M_global)
1498 opt_flags = OPT_GLOBAL;
1499 else if (SCHEME_VIMBUFFERP(argv[1]))
1500 {
1501 curbuf = get_valid_buffer(argv[1]);
1502 opt_flags = OPT_LOCAL;
1503 }
1504 else if (SCHEME_VIMWINDOWP(argv[1]))
1505 {
1506 win_T *win = get_valid_window(argv[1]);
1507
1508 curwin = win;
1509 curbuf = win->w_buffer;
1510 opt_flags = OPT_LOCAL;
1511 }
1512 else
1513 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1514 }
1515
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001516 rc = get_option_value(name, &value, (char_u **)&strval, opt_flags);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001517 curbuf = save_curb;
1518 curwin = save_curw;
1519
1520 switch (rc)
1521 {
1522 case 1:
1523 return scheme_make_integer_value(value);
1524 case 0:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001525 rval = scheme_make_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001526 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001527 vim_free(strval);
1528 return rval;
1529 case -1:
1530 case -2:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001531 raise_vim_exn(_("hidden option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001532 case -3:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001533 raise_vim_exn(_("unknown option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001534 }
1535 /* unreachable */
1536 return scheme_void;
1537}
1538
1539/* (set-option {option-changing-string} [buffer/window]) */
1540 static Scheme_Object *
1541set_option(void *data, int argc, Scheme_Object **argv)
1542{
1543 char_u *cmd;
1544 int opt_flags = 0;
1545 buf_T *save_curb = curbuf;
1546 win_T *save_curw = curwin;
1547 Vim_Prim *prim = (Vim_Prim *)data;
1548
1549 GUARANTEE_STRING(prim->name, 0);
1550 if (argc > 1)
1551 {
1552 if (M_global == NULL)
1553 {
1554 MZ_REGISTER_STATIC(M_global);
1555 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001556 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001557 }
1558
1559 if (argv[1] == M_global)
1560 opt_flags = OPT_GLOBAL;
1561 else if (SCHEME_VIMBUFFERP(argv[1]))
1562 {
1563 curbuf = get_valid_buffer(argv[1]);
1564 opt_flags = OPT_LOCAL;
1565 }
1566 else if (SCHEME_VIMWINDOWP(argv[1]))
1567 {
1568 win_T *win = get_valid_window(argv[1]);
1569 curwin = win;
1570 curbuf = win->w_buffer;
1571 opt_flags = OPT_LOCAL;
1572 }
1573 else
1574 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1575 }
1576
1577 /* do_set can modify cmd, make copy */
1578 cmd = vim_strsave((char_u *)SCHEME_STR_VAL(argv[0]));
1579 do_set(cmd, opt_flags);
1580 vim_free(cmd);
1581 update_screen(NOT_VALID);
1582 curbuf = save_curb;
1583 curwin = save_curw;
1584 raise_if_error();
1585 return scheme_void;
1586}
1587
1588/*
1589 *===========================================================================
1590 * 5. Vim Window-related Manipulation Functions
1591 *===========================================================================
1592 */
1593
1594/* (curr-win) */
1595 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001596get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001597{
1598 return (Scheme_Object *)get_vim_curr_window();
1599}
1600
1601/* (win-count) */
1602 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001603get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001604{
1605 win_T *w;
1606 int n = 0;
1607
Bram Moolenaarf740b292006-02-16 22:11:02 +00001608 for (w = firstwin; w != NULL; w = w->w_next)
1609 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001610 return scheme_make_integer(n);
1611}
1612
1613/* (get-win-list [buffer]) */
1614 static Scheme_Object *
1615get_window_list(void *data, int argc, Scheme_Object **argv)
1616{
1617 Vim_Prim *prim = (Vim_Prim *)data;
1618 vim_mz_buffer *buf;
1619 Scheme_Object *list;
1620 win_T *w;
1621
1622 buf = get_buffer_arg(prim->name, 0, argc, argv);
1623 list = scheme_null;
1624
Bram Moolenaarf740b292006-02-16 22:11:02 +00001625 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001626 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001627 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001628 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001629 MZ_GC_CHECK();
1630 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001631
1632 return list;
1633}
1634
1635 static Scheme_Object *
1636window_new(win_T *win)
1637{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001638 vim_mz_window *self = NULL;
1639
1640 MZ_GC_DECL_REG(1);
1641 MZ_GC_VAR_IN_REG(0, self);
1642 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001643
1644 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001645 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001646 * then we can get at it in win_free() in vim.
1647 *
1648 * On a win_free() we set the Scheme object's win_T *field
1649 * to an invalid value. We trap all uses of a window
1650 * object, and reject them if the win_T *field is invalid.
1651 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001652 if (win->w_mzscheme_ref != NULL)
1653 return win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001654
1655 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_window));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001656 vim_memset(self, 0, sizeof(vim_mz_window));
1657 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001658 MZ_GC_CHECK();
Bram Moolenaare344bea2005-09-01 20:46:49 +00001659 win->w_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001660 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001661 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001662
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001663 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001664 return (Scheme_Object *)(self);
1665}
1666
1667/* (get-win-num [window]) */
1668 static Scheme_Object *
1669get_window_num(void *data, int argc, Scheme_Object **argv)
1670{
1671 Vim_Prim *prim = (Vim_Prim *)data;
1672 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
1673 int nr = 1;
1674 win_T *wp;
1675
1676 for (wp = firstwin; wp != win; wp = wp->w_next)
1677 ++nr;
1678
1679 return scheme_make_integer(nr);
1680}
1681
1682/* (get-win-by-num {windownum}) */
1683 static Scheme_Object *
1684get_window_by_num(void *data, int argc, Scheme_Object **argv)
1685{
1686 Vim_Prim *prim = (Vim_Prim *)data;
1687 win_T *win;
1688 int fnum;
1689
1690 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1691 if (fnum < 1)
1692 scheme_signal_error(_("window index is out of range"));
1693
Bram Moolenaarf740b292006-02-16 22:11:02 +00001694 for (win = firstwin; win != NULL; win = win->w_next, --fnum)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001695 if (fnum == 1) /* to be 1-based */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001696 return window_new(win);
1697
1698 return scheme_false;
1699}
1700
1701/* (get-win-buffer [window]) */
1702 static Scheme_Object *
1703get_window_buffer(void *data, int argc, Scheme_Object **argv)
1704{
1705 Vim_Prim *prim = (Vim_Prim *)data;
1706 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1707
1708 return buffer_new(win->win->w_buffer);
1709}
1710
1711/* (get-win-height [window]) */
1712 static Scheme_Object *
1713get_window_height(void *data, int argc, Scheme_Object **argv)
1714{
1715 Vim_Prim *prim = (Vim_Prim *)data;
1716 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1717
1718 return scheme_make_integer(win->win->w_height);
1719}
1720
1721/* (set-win-height {height} [window]) */
1722 static Scheme_Object *
1723set_window_height(void *data, int argc, Scheme_Object **argv)
1724{
1725 Vim_Prim *prim = (Vim_Prim *)data;
1726 vim_mz_window *win;
1727 win_T *savewin;
1728 int height;
1729
1730 win = get_window_arg(prim->name, 1, argc, argv);
1731 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1732
1733#ifdef FEAT_GUI
1734 need_mouse_correct = TRUE;
1735#endif
1736
1737 savewin = curwin;
1738 curwin = win->win;
1739 win_setheight(height);
1740 curwin = savewin;
1741
1742 raise_if_error();
1743 return scheme_void;
1744}
1745
1746#ifdef FEAT_VERTSPLIT
1747/* (get-win-width [window]) */
1748 static Scheme_Object *
1749get_window_width(void *data, int argc, Scheme_Object **argv)
1750{
1751 Vim_Prim *prim = (Vim_Prim *)data;
1752 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1753
1754 return scheme_make_integer(W_WIDTH(win->win));
1755}
1756
1757/* (set-win-width {width} [window]) */
1758 static Scheme_Object *
1759set_window_width(void *data, int argc, Scheme_Object **argv)
1760{
1761 Vim_Prim *prim = (Vim_Prim *)data;
1762 vim_mz_window *win;
1763 win_T *savewin;
1764 int width = 0;
1765
1766 win = get_window_arg(prim->name, 1, argc, argv);
1767 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1768
1769# ifdef FEAT_GUI
1770 need_mouse_correct = TRUE;
1771# endif
1772
1773 savewin = curwin;
1774 curwin = win->win;
1775 win_setwidth(width);
1776 curwin = savewin;
1777
1778 raise_if_error();
1779 return scheme_void;
1780}
1781#endif
1782
1783/* (get-cursor [window]) -> (line . col) */
1784 static Scheme_Object *
1785get_cursor(void *data, int argc, Scheme_Object **argv)
1786{
1787 Vim_Prim *prim = (Vim_Prim *)data;
1788 vim_mz_window *win;
1789 pos_T pos;
1790
1791 win = get_window_arg(prim->name, 0, argc, argv);
1792 pos = win->win->w_cursor;
1793 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
1794 scheme_make_integer_value((long)pos.col + 1));
1795}
1796
1797/* (set-cursor (line . col) [window]) */
1798 static Scheme_Object *
1799set_cursor(void *data, int argc, Scheme_Object **argv)
1800{
1801 Vim_Prim *prim = (Vim_Prim *)data;
1802 vim_mz_window *win;
1803 long lnum = 0;
1804 long col = 0;
1805
Bram Moolenaar555b2802005-05-19 21:08:39 +00001806#ifdef HAVE_SANDBOX
1807 sandbox_check();
1808#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001809 win = get_window_arg(prim->name, 1, argc, argv);
1810 GUARANTEE_PAIR(prim->name, 0);
1811
1812 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
1813 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
1814 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
1815
1816 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
1817 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
1818
1819 check_line_range(lnum, win->win->w_buffer);
1820 /* don't know how to catch invalid column value */
1821
1822 win->win->w_cursor.lnum = lnum;
1823 win->win->w_cursor.col = col;
1824 update_screen(VALID);
1825
1826 raise_if_error();
1827 return scheme_void;
1828}
1829/*
1830 *===========================================================================
1831 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001832 *===========================================================================
1833 */
1834
1835/* (open-buff {filename}) */
1836 static Scheme_Object *
1837mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
1838{
1839 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001840 char_u *fname;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001841 int num = 0;
1842 Scheme_Object *onum;
1843
Bram Moolenaar555b2802005-05-19 21:08:39 +00001844#ifdef HAVE_SANDBOX
1845 sandbox_check();
1846#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001847 fname = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001848 /* TODO make open existing file */
1849 num = buflist_add(fname, BLN_LISTED | BLN_CURBUF);
1850
1851 if (num == 0)
1852 raise_vim_exn(_("couldn't open buffer"));
1853
1854 onum = scheme_make_integer(num);
1855 return get_buffer_by_num(data, 1, &onum);
1856}
1857
1858/* (get-buff-by-num {buffernum}) */
1859 static Scheme_Object *
1860get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
1861{
1862 Vim_Prim *prim = (Vim_Prim *)data;
1863 buf_T *buf;
1864 int fnum;
1865
1866 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1867
1868 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001869 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001870 return buffer_new(buf);
1871
1872 return scheme_false;
1873}
1874
1875/* (get-buff-by-name {buffername}) */
1876 static Scheme_Object *
1877get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
1878{
1879 Vim_Prim *prim = (Vim_Prim *)data;
1880 buf_T *buf;
1881 char_u *fname;
1882
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001883 fname = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001884
1885 for (buf = firstbuf; buf; buf = buf->b_next)
1886 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1887 /* empty string */
1888 {
1889 if (fname[0] == NUL)
1890 return buffer_new(buf);
1891 }
1892 else if (!fnamecmp(buf->b_ffname, fname)
1893 || !fnamecmp(buf->b_sfname, fname))
1894 /* either short or long filename matches */
1895 return buffer_new(buf);
1896
1897 return scheme_false;
1898}
1899
1900/* (get-next-buff [buffer]) */
1901 static Scheme_Object *
1902get_next_buffer(void *data, int argc, Scheme_Object **argv)
1903{
1904 Vim_Prim *prim = (Vim_Prim *)data;
1905 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1906
1907 if (buf->b_next == NULL)
1908 return scheme_false;
1909 else
1910 return buffer_new(buf->b_next);
1911}
1912
1913/* (get-prev-buff [buffer]) */
1914 static Scheme_Object *
1915get_prev_buffer(void *data, int argc, Scheme_Object **argv)
1916{
1917 Vim_Prim *prim = (Vim_Prim *)data;
1918 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1919
1920 if (buf->b_prev == NULL)
1921 return scheme_false;
1922 else
1923 return buffer_new(buf->b_prev);
1924}
1925
1926/* (get-buff-num [buffer]) */
1927 static Scheme_Object *
1928get_buffer_num(void *data, int argc, Scheme_Object **argv)
1929{
1930 Vim_Prim *prim = (Vim_Prim *)data;
1931 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1932
1933 return scheme_make_integer(buf->buf->b_fnum);
1934}
1935
1936/* (buff-count) */
1937 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001938get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001939{
1940 buf_T *b;
1941 int n = 0;
1942
1943 for (b = firstbuf; b; b = b->b_next) ++n;
1944 return scheme_make_integer(n);
1945}
1946
1947/* (get-buff-name [buffer]) */
1948 static Scheme_Object *
1949get_buffer_name(void *data, int argc, Scheme_Object **argv)
1950{
1951 Vim_Prim *prim = (Vim_Prim *)data;
1952 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1953
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001954 return scheme_make_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001955}
1956
1957/* (curr-buff) */
1958 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001959get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001960{
1961 return (Scheme_Object *)get_vim_curr_buffer();
1962}
1963
1964 static Scheme_Object *
1965buffer_new(buf_T *buf)
1966{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001967 vim_mz_buffer *self = NULL;
1968
1969 MZ_GC_DECL_REG(1);
1970 MZ_GC_VAR_IN_REG(0, self);
1971 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001972
1973 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001974 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001975 * then we can get at it in buf_freeall() in vim.
1976 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001977 if (buf->b_mzscheme_ref)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001978 return buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001979
1980 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_buffer));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001981 vim_memset(self, 0, sizeof(vim_mz_buffer));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001982 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
1983 MZ_GC_CHECK();
Bram Moolenaare344bea2005-09-01 20:46:49 +00001984 buf->b_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001985 self->buf = buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001986 self->so.type = mz_buffer_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001987
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001988 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001989 return (Scheme_Object *)(self);
1990}
1991
1992/*
1993 * (get-buff-size [buffer])
1994 *
1995 * Get the size (number of lines) in the current buffer.
1996 */
1997 static Scheme_Object *
1998get_buffer_size(void *data, int argc, Scheme_Object **argv)
1999{
2000 Vim_Prim *prim = (Vim_Prim *)data;
2001 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2002
2003 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
2004}
2005
2006/*
2007 * (get-buff-line {linenr} [buffer])
2008 *
2009 * Get a line from the specified buffer. The line number is
2010 * in Vim format (1-based). The line is returned as a MzScheme
2011 * string object.
2012 */
2013 static Scheme_Object *
2014get_buffer_line(void *data, int argc, Scheme_Object **argv)
2015{
2016 Vim_Prim *prim = (Vim_Prim *)data;
2017 vim_mz_buffer *buf;
2018 int linenr;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002019 char_u *line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002020
2021 buf = get_buffer_arg(prim->name, 1, argc, argv);
2022 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2023 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2024
2025 raise_if_error();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002026 return scheme_make_string((char *)line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002027}
2028
2029
2030/*
2031 * (get-buff-line-list {start} {end} [buffer])
2032 *
2033 * Get a list of lines from the specified buffer. The line numbers
2034 * are in Vim format (1-based). The range is from lo up to, but not
2035 * including, hi. The list is returned as a list of string objects.
2036 */
2037 static Scheme_Object *
2038get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2039{
2040 Vim_Prim *prim = (Vim_Prim *)data;
2041 vim_mz_buffer *buf;
2042 int i, hi, lo, n;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002043 Scheme_Object *list = NULL;
2044
2045 MZ_GC_DECL_REG(1);
2046 MZ_GC_VAR_IN_REG(0, list);
2047 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002048
2049 buf = get_buffer_arg(prim->name, 2, argc, argv);
2050 list = scheme_null;
2051 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2052 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2053
2054 /*
2055 * Handle some error conditions
2056 */
2057 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002058 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002059
2060 if (hi < 0)
2061 hi = 0;
2062 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002063 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002064
2065 n = hi - lo;
2066
2067 for (i = n; i >= 0; --i)
2068 {
2069 Scheme_Object *str = scheme_make_string(
2070 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2071 raise_if_error();
2072
2073 /* Set the list item */
2074 list = scheme_make_pair(str, list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002075 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002076 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002077 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002078 return list;
2079}
2080
2081/*
2082 * (set-buff-line {linenr} {string/#f} [buffer])
2083 *
2084 * Replace a line in the specified buffer. The line number is
2085 * in Vim format (1-based). The replacement line is given as
2086 * an MzScheme string object. The object is checked for validity
2087 * and correct format. An exception is thrown if the values are not
2088 * the correct format.
2089 *
2090 * It returns a Scheme Object that indicates the length of the
2091 * string changed.
2092 */
2093 static Scheme_Object *
2094set_buffer_line(void *data, int argc, Scheme_Object **argv)
2095{
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002096 /* First of all, we check the value of the supplied MzScheme object.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002097 * There are three cases:
2098 * 1. #f - this is a deletion.
2099 * 2. A string - this is a replacement.
2100 * 3. Anything else - this is an error.
2101 */
2102 Vim_Prim *prim = (Vim_Prim *)data;
2103 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002104 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002105 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002106 int n;
2107
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002108 MZ_GC_DECL_REG(1);
2109 MZ_GC_VAR_IN_REG(0, line);
2110 MZ_GC_REG();
2111
Bram Moolenaar555b2802005-05-19 21:08:39 +00002112#ifdef HAVE_SANDBOX
2113 sandbox_check();
2114#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002115 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2116 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002117 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002118 line = argv[1];
2119 buf = get_buffer_arg(prim->name, 2, argc, argv);
2120
2121 check_line_range(n, buf->buf);
2122
2123 if (SCHEME_FALSEP(line))
2124 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002125 buf_T *savebuf = curbuf;
2126
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002127 curbuf = buf->buf;
2128
2129 if (u_savedel((linenr_T)n, 1L) == FAIL)
2130 {
2131 curbuf = savebuf;
2132 raise_vim_exn(_("cannot save undo information"));
2133 }
2134 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2135 {
2136 curbuf = savebuf;
2137 raise_vim_exn(_("cannot delete line"));
2138 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002139 if (buf->buf == curwin->w_buffer)
2140 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002141 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002142
2143 curbuf = savebuf;
2144
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002145 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002146 raise_if_error();
2147 return scheme_void;
2148 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002149 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002150 {
2151 /* Otherwise it's a line */
2152 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002153
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002154 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002155
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002156 curbuf = buf->buf;
2157
2158 if (u_savesub((linenr_T)n) == FAIL)
2159 {
2160 curbuf = savebuf;
2161 vim_free(save);
2162 raise_vim_exn(_("cannot save undo information"));
2163 }
2164 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2165 {
2166 curbuf = savebuf;
2167 vim_free(save);
2168 raise_vim_exn(_("cannot replace line"));
2169 }
2170 else
2171 {
2172 vim_free(save);
2173 changed_bytes((linenr_T)n, 0);
2174 }
2175
2176 curbuf = savebuf;
2177
2178 /* Check that the cursor is not beyond the end of the line now. */
2179 if (buf->buf == curwin->w_buffer)
2180 check_cursor_col();
2181
2182 MZ_GC_UNREG();
2183 raise_if_error();
2184 return scheme_void;
2185 }
2186}
2187
2188 static void
2189free_array(char **array)
2190{
2191 char **curr = array;
2192 while (*curr != NULL)
2193 vim_free(*curr++);
2194 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002195}
2196
2197/*
2198 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2199 *
2200 * Replace a range of lines in the specified buffer. The line numbers are in
2201 * Vim format (1-based). The range is from lo up to, but not including, hi.
2202 * The replacement lines are given as a Scheme list of string objects. The
2203 * list is checked for validity and correct format.
2204 *
2205 * Errors are returned as a value of FAIL. The return value is OK on success.
2206 * If OK is returned and len_change is not NULL, *len_change is set to the
2207 * change in the buffer length.
2208 */
2209 static Scheme_Object *
2210set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2211{
2212 /* First of all, we check the type of the supplied MzScheme object.
2213 * There are three cases:
2214 * 1. #f - this is a deletion.
2215 * 2. A list - this is a replacement.
2216 * 3. Anything else - this is an error.
2217 */
2218 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002219 vim_mz_buffer *buf = NULL;
2220 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002221 int i, old_len, new_len, hi, lo;
2222 long extra;
2223
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002224 MZ_GC_DECL_REG(1);
2225 MZ_GC_VAR_IN_REG(0, line_list);
2226 MZ_GC_REG();
2227
Bram Moolenaar555b2802005-05-19 21:08:39 +00002228#ifdef HAVE_SANDBOX
2229 sandbox_check();
2230#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002231 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2232 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2233 if (!SCHEME_PAIRP(argv[2])
2234 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2235 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2236 line_list = argv[2];
2237 buf = get_buffer_arg(prim->name, 3, argc, argv);
2238 old_len = hi - lo;
2239 if (old_len < 0) /* process inverse values wisely */
2240 {
2241 i = lo;
2242 lo = hi;
2243 hi = i;
2244 old_len = -old_len;
2245 }
2246 extra = 0;
2247
2248 check_line_range(lo, buf->buf); /* inclusive */
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002249 check_line_range(hi - 1, buf->buf); /* exclusive */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002250
2251 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2252 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002253 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002254 curbuf = buf->buf;
2255
2256 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2257 {
2258 curbuf = savebuf;
2259 raise_vim_exn(_("cannot save undo information"));
2260 }
2261 else
2262 {
2263 for (i = 0; i < old_len; i++)
2264 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2265 {
2266 curbuf = savebuf;
2267 raise_vim_exn(_("cannot delete line"));
2268 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002269 if (buf->buf == curwin->w_buffer)
2270 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002271 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002272 }
2273
2274 curbuf = savebuf;
2275
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002276 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002277 raise_if_error();
2278 return scheme_void;
2279 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002280 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002281 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002282 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002283
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002284 /* List */
2285 new_len = scheme_proper_list_length(line_list);
2286 MZ_GC_CHECK();
2287 if (new_len < 0) /* improper or cyclic list */
2288 scheme_wrong_type(prim->name, "proper list",
2289 2, argc, argv);
2290 else
2291 {
2292 char **array = NULL;
2293 Scheme_Object *line = NULL;
2294 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002295
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002296 MZ_GC_DECL_REG(2);
2297 MZ_GC_VAR_IN_REG(0, line);
2298 MZ_GC_VAR_IN_REG(1, rest);
2299 MZ_GC_REG();
2300
2301 array = (char **)alloc(new_len * sizeof(char *));
2302 vim_memset(array, 0, new_len * sizeof(char *));
2303
2304 rest = line_list;
2305 for (i = 0; i < new_len; ++i)
2306 {
2307 line = SCHEME_CAR(rest);
2308 rest = SCHEME_CDR(rest);
2309 if (!SCHEME_STRINGP(line))
2310 {
2311 free_array(array);
2312 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2313 }
2314 array[i] = string_to_line(line);
2315 }
2316
2317 curbuf = buf->buf;
2318
2319 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2320 {
2321 curbuf = savebuf;
2322 free_array(array);
2323 raise_vim_exn(_("cannot save undo information"));
2324 }
2325
2326 /*
2327 * If the size of the range is reducing (ie, new_len < old_len) we
2328 * need to delete some old_len. We do this at the start, by
2329 * repeatedly deleting line "lo".
2330 */
2331 for (i = 0; i < old_len - new_len; ++i)
2332 {
2333 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2334 {
2335 curbuf = savebuf;
2336 free_array(array);
2337 raise_vim_exn(_("cannot delete line"));
2338 }
2339 extra--;
2340 }
2341
2342 /*
2343 * For as long as possible, replace the existing old_len with the
2344 * new old_len. This is a more efficient operation, as it requires
2345 * less memory allocation and freeing.
2346 */
2347 for (i = 0; i < old_len && i < new_len; i++)
2348 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2349 {
2350 curbuf = savebuf;
2351 free_array(array);
2352 raise_vim_exn(_("cannot replace line"));
2353 }
2354
2355 /*
2356 * Now we may need to insert the remaining new_len. We don't need to
2357 * free the string passed back because MzScheme has control of that
2358 * memory.
2359 */
2360 while (i < new_len)
2361 {
2362 if (ml_append((linenr_T)(lo + i - 1),
2363 (char_u *)array[i], 0, FALSE) == FAIL)
2364 {
2365 curbuf = savebuf;
2366 free_array(array);
2367 raise_vim_exn(_("cannot insert line"));
2368 }
2369 ++i;
2370 ++extra;
2371 }
2372 MZ_GC_UNREG();
2373 free_array(array);
2374 }
2375
2376 /*
2377 * Adjust marks. Invalidate any which lie in the
2378 * changed range, and move any in the remainder of the buffer.
2379 */
2380 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra);
2381 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2382
2383 if (buf->buf == curwin->w_buffer)
2384 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002385 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002386
2387 MZ_GC_UNREG();
2388 raise_if_error();
2389 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002390 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002391}
2392
2393/*
2394 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2395 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002396 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002397 * The line number is in Vim format (1-based). The lines to be inserted are
2398 * given as an MzScheme list of string objects or as a single string. The lines
2399 * to be added are checked for validity and correct format. Errors are
2400 * returned as a value of FAIL. The return value is OK on success.
2401 * If OK is returned and len_change is not NULL, *len_change
2402 * is set to the change in the buffer length.
2403 */
2404 static Scheme_Object *
2405insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2406{
2407 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002408 vim_mz_buffer *buf = NULL;
2409 Scheme_Object *list = NULL;
2410 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002411 int i, n, size;
2412
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002413 MZ_GC_DECL_REG(1);
2414 MZ_GC_VAR_IN_REG(0, list);
2415 MZ_GC_REG();
2416
Bram Moolenaar555b2802005-05-19 21:08:39 +00002417#ifdef HAVE_SANDBOX
2418 sandbox_check();
2419#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002420 /*
2421 * First of all, we check the type of the supplied MzScheme object.
2422 * It must be a string or a list, or the call is in error.
2423 */
2424 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2425 list = argv[1];
2426
2427 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2428 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2429 buf = get_buffer_arg(prim->name, 2, argc, argv);
2430
2431 if (n != 0) /* 0 can be used in insert */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002432 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002433 if (SCHEME_STRINGP(list))
2434 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002435 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002436
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002437 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002438 curbuf = buf->buf;
2439
2440 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2441 {
2442 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002443 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002444 raise_vim_exn(_("cannot save undo information"));
2445 }
2446 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2447 {
2448 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002449 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002450 raise_vim_exn(_("cannot insert line"));
2451 }
2452 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002453 {
2454 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002455 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002456 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002457
2458 curbuf = savebuf;
2459 update_screen(VALID);
2460
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002461 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002462 raise_if_error();
2463 return scheme_void;
2464 }
2465
2466 /* List */
2467 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002468 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002469 if (size < 0) /* improper or cyclic list */
2470 scheme_wrong_type(prim->name, "proper list",
2471 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002472 else
2473 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002474 Scheme_Object *line = NULL;
2475 Scheme_Object *rest = NULL;
2476 char **array;
2477 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002478
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002479 MZ_GC_DECL_REG(2);
2480 MZ_GC_VAR_IN_REG(0, line);
2481 MZ_GC_VAR_IN_REG(1, rest);
2482 MZ_GC_REG();
2483
2484 array = (char **)alloc(size * sizeof(char *));
2485 vim_memset(array, 0, size * sizeof(char *));
2486
2487 rest = list;
2488 for (i = 0; i < size; ++i)
2489 {
2490 line = SCHEME_CAR(rest);
2491 rest = SCHEME_CDR(rest);
2492 array[i] = string_to_line(line);
2493 }
2494
2495 curbuf = buf->buf;
2496
2497 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2498 {
2499 curbuf = savebuf;
2500 free_array(array);
2501 raise_vim_exn(_("cannot save undo information"));
2502 }
2503 else
2504 {
2505 for (i = 0; i < size; ++i)
2506 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2507 0, FALSE) == FAIL)
2508 {
2509 curbuf = savebuf;
2510 free_array(array);
2511 raise_vim_exn(_("cannot insert line"));
2512 }
2513
2514 if (i > 0)
2515 appended_lines_mark((linenr_T)n, (long)i);
2516 }
2517 free_array(array);
2518 MZ_GC_UNREG();
2519 curbuf = savebuf;
2520 update_screen(VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002521 }
2522
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002523 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002524 raise_if_error();
2525 return scheme_void;
2526}
2527
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002528/*
2529 * Predicates
2530 */
2531/* (buff? obj) */
2532 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002533vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002534{
2535 if (SCHEME_VIMBUFFERP(argv[0]))
2536 return scheme_true;
2537 else
2538 return scheme_false;
2539}
2540
2541/* (win? obj) */
2542 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002543vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002544{
2545 if (SCHEME_VIMWINDOWP(argv[0]))
2546 return scheme_true;
2547 else
2548 return scheme_false;
2549}
2550
2551/* (buff-valid? obj) */
2552 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002553vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002554{
2555 if (SCHEME_VIMBUFFERP(argv[0])
2556 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2557 return scheme_true;
2558 else
2559 return scheme_false;
2560}
2561
2562/* (win-valid? obj) */
2563 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002564vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002565{
2566 if (SCHEME_VIMWINDOWP(argv[0])
2567 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2568 return scheme_true;
2569 else
2570 return scheme_false;
2571}
2572
2573/*
2574 *===========================================================================
2575 * Utilities
2576 *===========================================================================
2577 */
2578
2579/*
2580 * Convert an MzScheme string into a Vim line.
2581 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002582 * All internal nulls are replaced by newline characters.
2583 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002584 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002585 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002586 */
2587 static char *
2588string_to_line(Scheme_Object *obj)
2589{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002590 char *scheme_str = NULL;
2591 char *vim_str = NULL;
Bram Moolenaar02e14d62012-11-28 15:37:51 +01002592 intptr_t len;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002593 int i;
2594
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002595 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002596
2597 /* Error checking: String must not contain newlines, as we
2598 * are replacing a single line, and we must replace it with
2599 * a single line.
2600 */
Bram Moolenaar02e14d62012-11-28 15:37:51 +01002601 if (memchr(scheme_str, '\n', (size_t)len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002602 scheme_signal_error(_("string cannot contain newlines"));
2603
Bram Moolenaar02e14d62012-11-28 15:37:51 +01002604 vim_str = (char *)alloc((int)(len + 1));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002605
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002606 /* Create a copy of the string, with internal nulls replaced by
2607 * newline characters, as is the vim convention.
2608 */
2609 for (i = 0; i < len; ++i)
2610 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002611 if (scheme_str[i] == '\0')
2612 vim_str[i] = '\n';
2613 else
2614 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002615 }
2616
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002617 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002618
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002619 MZ_GC_CHECK();
2620 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002621}
2622
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002623#ifdef FEAT_EVAL
2624/*
2625 * Convert Vim value into MzScheme, adopted from if_python.c
2626 */
2627 static Scheme_Object *
2628vim_to_mzscheme(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
2629{
2630 Scheme_Object *result = NULL;
2631 int new_value = TRUE;
2632
2633 MZ_GC_DECL_REG(1);
2634 MZ_GC_VAR_IN_REG(0, result);
2635 MZ_GC_REG();
2636
2637 /* Avoid infinite recursion */
2638 if (depth > 100)
2639 {
2640 MZ_GC_UNREG();
2641 return scheme_void;
2642 }
2643
2644 /* Check if we run into a recursive loop. The item must be in visited
2645 * then and we can use it again.
2646 */
2647 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
2648 MZ_GC_CHECK();
2649 if (result != NULL) /* found, do nothing */
2650 new_value = FALSE;
2651 else if (vim_value->v_type == VAR_STRING)
2652 {
Bram Moolenaard04da7c2012-10-14 03:41:59 +02002653 result = scheme_make_string(vim_value->vval.v_string == NULL
2654 ? "" : (char *)vim_value->vval.v_string);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002655 MZ_GC_CHECK();
2656 }
2657 else if (vim_value->v_type == VAR_NUMBER)
2658 {
2659 result = scheme_make_integer((long)vim_value->vval.v_number);
2660 MZ_GC_CHECK();
2661 }
2662# ifdef FEAT_FLOAT
2663 else if (vim_value->v_type == VAR_FLOAT)
2664 {
2665 result = scheme_make_double((double)vim_value->vval.v_float);
2666 MZ_GC_CHECK();
2667 }
2668# endif
2669 else if (vim_value->v_type == VAR_LIST)
2670 {
2671 list_T *list = vim_value->vval.v_list;
2672 listitem_T *curr;
2673
2674 if (list == NULL || list->lv_first == NULL)
2675 result = scheme_null;
2676 else
2677 {
2678 Scheme_Object *obj = NULL;
2679
2680 MZ_GC_DECL_REG(1);
2681 MZ_GC_VAR_IN_REG(0, obj);
2682 MZ_GC_REG();
2683
2684 curr = list->lv_last;
2685 obj = vim_to_mzscheme(&curr->li_tv, depth + 1, visited);
2686 result = scheme_make_pair(obj, scheme_null);
2687 MZ_GC_CHECK();
2688
2689 while (curr != list->lv_first)
2690 {
2691 curr = curr->li_prev;
2692 obj = vim_to_mzscheme(&curr->li_tv, depth + 1, visited);
2693 result = scheme_make_pair(obj, result);
2694 MZ_GC_CHECK();
2695 }
2696 }
2697 MZ_GC_UNREG();
2698 }
2699 else if (vim_value->v_type == VAR_DICT)
2700 {
2701 Scheme_Object *key = NULL;
2702 Scheme_Object *obj = NULL;
2703
2704 MZ_GC_DECL_REG(2);
2705 MZ_GC_VAR_IN_REG(0, key);
2706 MZ_GC_VAR_IN_REG(1, obj);
2707 MZ_GC_REG();
2708
2709 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
2710 MZ_GC_CHECK();
2711 if (vim_value->vval.v_dict != NULL)
2712 {
2713 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
2714 long_u todo = ht->ht_used;
2715 hashitem_T *hi;
2716 dictitem_T *di;
2717
2718 for (hi = ht->ht_array; todo > 0; ++hi)
2719 {
2720 if (!HASHITEM_EMPTY(hi))
2721 {
2722 --todo;
2723
2724 di = dict_lookup(hi);
2725 obj = vim_to_mzscheme(&di->di_tv, depth + 1, visited);
2726 key = scheme_make_string((char *)hi->hi_key);
2727 MZ_GC_CHECK();
2728 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
2729 MZ_GC_CHECK();
2730 }
2731 }
2732 }
2733 MZ_GC_UNREG();
2734 }
2735 else
2736 {
2737 result = scheme_void;
2738 new_value = FALSE;
2739 }
2740 if (new_value)
2741 {
2742 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
2743 MZ_GC_CHECK();
2744 }
2745 MZ_GC_UNREG();
2746 return result;
2747}
Bram Moolenaar7e506b62010-01-19 15:55:06 +01002748
2749 static int
2750mzscheme_to_vim(Scheme_Object *obj, typval_T *tv, int depth,
2751 Scheme_Hash_Table *visited)
2752{
2753 int status = OK;
2754 typval_T *found;
2755 MZ_GC_CHECK();
2756 if (depth > 100) /* limit the deepest recursion level */
2757 {
2758 tv->v_type = VAR_NUMBER;
2759 tv->vval.v_number = 0;
2760 return FAIL;
2761 }
2762
2763 found = (typval_T *)scheme_hash_get(visited, obj);
2764 if (found != NULL)
2765 copy_tv(found, tv);
2766 else if (SCHEME_VOIDP(obj))
2767 {
2768 tv->v_type = VAR_NUMBER;
2769 tv->vval.v_number = 0;
2770 }
2771 else if (SCHEME_INTP(obj))
2772 {
2773 tv->v_type = VAR_NUMBER;
2774 tv->vval.v_number = SCHEME_INT_VAL(obj);
2775 }
2776 else if (SCHEME_BOOLP(obj))
2777 {
2778 tv->v_type = VAR_NUMBER;
2779 tv->vval.v_number = SCHEME_TRUEP(obj);
2780 }
2781# ifdef FEAT_FLOAT
2782 else if (SCHEME_DBLP(obj))
2783 {
2784 tv->v_type = VAR_FLOAT;
2785 tv->vval.v_float = SCHEME_DBL_VAL(obj);
2786 }
2787# endif
2788 else if (SCHEME_STRINGP(obj))
2789 {
2790 tv->v_type = VAR_STRING;
2791 tv->vval.v_string = vim_strsave((char_u *)SCHEME_STR_VAL(obj));
2792 }
2793 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
2794 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
2795 {
2796 list_T *list = list_alloc();
2797 if (list == NULL)
2798 status = FAIL;
2799 else
2800 {
2801 int i;
2802 Scheme_Object *curr = NULL;
2803 Scheme_Object *cval = NULL;
2804 /* temporary var to hold current element of vectors and pairs */
2805 typval_T *v;
2806
2807 MZ_GC_DECL_REG(2);
2808 MZ_GC_VAR_IN_REG(0, curr);
2809 MZ_GC_VAR_IN_REG(1, cval);
2810 MZ_GC_REG();
2811
2812 tv->v_type = VAR_LIST;
2813 tv->vval.v_list = list;
2814 ++list->lv_refcount;
2815
2816 v = (typval_T *)alloc(sizeof(typval_T));
2817 if (v == NULL)
2818 status = FAIL;
2819 else
2820 {
2821 /* add the value in advance to allow handling of self-referencial
2822 * data structures */
2823 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
2824 copy_tv(tv, visited_tv);
2825 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
2826
2827 if (SCHEME_VECTORP(obj))
2828 {
2829 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
2830 {
2831 cval = SCHEME_VEC_ELS(obj)[i];
2832 status = mzscheme_to_vim(cval, v, depth + 1, visited);
2833 if (status == FAIL)
2834 break;
2835 status = list_append_tv(list, v);
2836 clear_tv(v);
2837 if (status == FAIL)
2838 break;
2839 }
2840 }
2841 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
2842 {
2843 for (curr = obj;
2844 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
2845 curr = SCHEME_CDR(curr))
2846 {
2847 cval = SCHEME_CAR(curr);
2848 status = mzscheme_to_vim(cval, v, depth + 1, visited);
2849 if (status == FAIL)
2850 break;
2851 status = list_append_tv(list, v);
2852 clear_tv(v);
2853 if (status == FAIL)
2854 break;
2855 }
2856 /* impoper list not terminated with null
2857 * need to handle the last element */
2858 if (status == OK && !SCHEME_NULLP(curr))
2859 {
2860 status = mzscheme_to_vim(cval, v, depth + 1, visited);
2861 if (status == OK)
2862 {
2863 status = list_append_tv(list, v);
2864 clear_tv(v);
2865 }
2866 }
2867 }
2868 /* nothing to do for scheme_null */
2869 vim_free(v);
2870 }
2871 MZ_GC_UNREG();
2872 }
2873 }
2874 else if (SCHEME_HASHTP(obj))
2875 {
2876 int i;
2877 dict_T *dict;
2878 Scheme_Object *key = NULL;
2879 Scheme_Object *val = NULL;
2880
2881 MZ_GC_DECL_REG(2);
2882 MZ_GC_VAR_IN_REG(0, key);
2883 MZ_GC_VAR_IN_REG(1, val);
2884 MZ_GC_REG();
2885
2886 dict = dict_alloc();
2887 if (dict == NULL)
2888 status = FAIL;
2889 else
2890 {
2891 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
2892
2893 tv->v_type = VAR_DICT;
2894 tv->vval.v_dict = dict;
2895 ++dict->dv_refcount;
2896
2897 copy_tv(tv, visited_tv);
2898 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
2899
2900 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
2901 {
2902 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
2903 {
2904 /* generate item for `diplay'ed Scheme key */
2905 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
2906 ((Scheme_Hash_Table *) obj)->keys[i]));
2907 /* convert Scheme val to Vim and add it to the dict */
2908 if (mzscheme_to_vim(((Scheme_Hash_Table *) obj)->vals[i],
2909 &item->di_tv, depth + 1, visited) == FAIL
2910 || dict_add(dict, item) == FAIL)
2911 {
2912 dictitem_free(item);
2913 status = FAIL;
2914 break;
2915 }
2916 }
2917
2918 }
2919 }
2920 MZ_GC_UNREG();
2921 }
2922 else
2923 {
2924 /* `display' any other value to string */
2925 tv->v_type = VAR_STRING;
2926 tv->vval.v_string = (char_u *)string_to_line(obj);
2927 }
2928 return status;
2929}
2930
2931 void
2932do_mzeval(char_u *str, typval_T *rettv)
2933{
2934 int i;
2935 Scheme_Object *ret = NULL;
2936 Scheme_Hash_Table *visited = NULL;
2937
2938 MZ_GC_DECL_REG(2);
2939 MZ_GC_VAR_IN_REG(0, ret);
2940 MZ_GC_VAR_IN_REG(0, visited);
2941 MZ_GC_REG();
2942
2943 if (mzscheme_init())
2944 {
2945 MZ_GC_UNREG();
2946 return;
2947 }
2948
2949 MZ_GC_CHECK();
2950 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2951 MZ_GC_CHECK();
2952
2953 if (eval_with_exn_handling(str, do_eval, &ret) == OK)
2954 mzscheme_to_vim(ret, rettv, 1, visited);
2955
2956 for (i = 0; i < visited->size; ++i)
2957 {
2958 /* free up remembered objects */
2959 if (visited->vals[i] != NULL)
2960 {
2961 free_tv((typval_T *)visited->vals[i]);
2962 }
2963 }
2964
2965 MZ_GC_UNREG();
2966}
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002967#endif
2968
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002969/*
2970 * Check to see whether a Vim error has been reported, or a keyboard
2971 * interrupt (from vim --> got_int) has been detected.
2972 */
2973 static int
2974vim_error_check(void)
2975{
2976 return (got_int || did_emsg);
2977}
2978
2979/*
2980 * register Scheme exn:vim
2981 */
2982 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002983register_vim_exn(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002984{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002985 int nc = 0;
2986 int i;
2987 Scheme_Object *struct_exn = NULL;
2988 Scheme_Object *exn_name = NULL;
2989
2990 MZ_GC_DECL_REG(2);
2991 MZ_GC_VAR_IN_REG(0, struct_exn);
2992 MZ_GC_VAR_IN_REG(1, exn_name);
2993 MZ_GC_REG();
2994
2995 exn_name = scheme_intern_symbol("exn:vim");
2996 MZ_GC_CHECK();
2997 struct_exn = scheme_builtin_value("struct:exn");
2998 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002999
3000 if (vim_exn == NULL)
3001 vim_exn = scheme_make_struct_type(exn_name,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003002 struct_exn, NULL, 0, 0, NULL, NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003003#if MZSCHEME_VERSION_MAJOR >= 299
3004 , NULL
3005#endif
3006 );
3007
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003008
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003009 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003010 Scheme_Object **tmp = NULL;
3011 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
3012 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
3013 MZ_GC_DECL_REG(6);
3014 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
3015 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
3016 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003017
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003018 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003019 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
3020 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003021
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003022 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
3023 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
3024 MZ_GC_CHECK();
3025
3026 for (i = 0; i < nc; i++)
3027 {
3028 scheme_add_global_symbol(exn_names[i],
3029 exn_values[i], environment);
3030 MZ_GC_CHECK();
3031 }
3032 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003033 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003034 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003035}
3036
3037/*
3038 * raise exn:vim, may be with additional info string
3039 */
3040 void
3041raise_vim_exn(const char *add_info)
3042{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003043 char *fmt = _("Vim error: ~a");
3044 Scheme_Object *argv[2] = {NULL, NULL};
3045 Scheme_Object *exn = NULL;
3046
3047 MZ_GC_DECL_REG(4);
3048 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
3049 MZ_GC_VAR_IN_REG(3, exn);
3050 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003051
3052 if (add_info != NULL)
3053 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003054 char *c_string = NULL;
3055 Scheme_Object *byte_string = NULL;
3056 Scheme_Object *info = NULL;
3057
3058 MZ_GC_DECL_REG(3);
3059 MZ_GC_VAR_IN_REG(0, c_string);
3060 MZ_GC_VAR_IN_REG(1, byte_string);
3061 MZ_GC_VAR_IN_REG(2, info);
3062 MZ_GC_REG();
3063
3064 info = scheme_make_string(add_info);
3065 MZ_GC_CHECK();
3066 c_string = scheme_format(fmt, STRLEN(fmt), 1, &info, NULL);
3067 MZ_GC_CHECK();
3068 byte_string = scheme_make_string(c_string);
3069 MZ_GC_CHECK();
3070 argv[0] = scheme_byte_string_to_char_string(byte_string);
3071 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003072 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003073 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003074 }
3075 else
3076 argv[0] = scheme_make_string(_("Vim error"));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003077 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003078
Bram Moolenaar049377e2007-05-12 15:32:12 +00003079#if MZSCHEME_VERSION_MAJOR < 360
3080 argv[1] = scheme_current_continuation_marks();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003081 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003082#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003083 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003084 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003085#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003086
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003087 exn = scheme_make_struct_instance(vim_exn, 2, argv);
3088 MZ_GC_CHECK();
3089 scheme_raise(exn);
3090 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003091}
3092
3093 void
3094raise_if_error(void)
3095{
3096 if (vim_error_check())
3097 raise_vim_exn(NULL);
3098}
3099
3100/* get buffer:
3101 * either current
3102 * or passed as argv[argnum] with checks
3103 */
3104 static vim_mz_buffer *
3105get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3106{
3107 vim_mz_buffer *b;
3108
3109 if (argc < argnum + 1)
3110 return get_vim_curr_buffer();
3111 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3112 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3113 b = (vim_mz_buffer *)argv[argnum];
3114 (void)get_valid_buffer(argv[argnum]);
3115 return b;
3116}
3117
3118/* get window:
3119 * either current
3120 * or passed as argv[argnum] with checks
3121 */
3122 static vim_mz_window *
3123get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3124{
3125 vim_mz_window *w;
3126
3127 if (argc < argnum + 1)
3128 return get_vim_curr_window();
3129 w = (vim_mz_window *)argv[argnum];
3130 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3131 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3132 (void)get_valid_window(argv[argnum]);
3133 return w;
3134}
3135
3136/* get valid Vim buffer from Scheme_Object* */
3137buf_T *get_valid_buffer(void *obj)
3138{
3139 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3140
3141 if (buf == INVALID_BUFFER_VALUE)
3142 scheme_signal_error(_("buffer is invalid"));
3143 return buf;
3144}
3145
3146/* get valid Vim window from Scheme_Object* */
3147win_T *get_valid_window(void *obj)
3148{
3149 win_T *win = ((vim_mz_window *)obj)->win;
3150 if (win == INVALID_WINDOW_VALUE)
3151 scheme_signal_error(_("window is invalid"));
3152 return win;
3153}
3154
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003155 int
3156mzthreads_allowed(void)
3157{
3158 return mz_threads_allow;
3159}
3160
3161 static int
3162line_in_range(linenr_T lnum, buf_T *buf)
3163{
3164 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3165}
3166
3167 static void
3168check_line_range(linenr_T lnum, buf_T *buf)
3169{
3170 if (!line_in_range(lnum, buf))
3171 scheme_signal_error(_("linenr out of range"));
3172}
3173
3174/*
3175 * Check if deleting lines made the cursor position invalid
3176 * (or you'll get msg from Vim about invalid linenr).
3177 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3178 * deleted). Got from if_python.c
3179 */
3180 static void
3181mz_fix_cursor(int lo, int hi, int extra)
3182{
3183 if (curwin->w_cursor.lnum >= lo)
3184 {
3185 /* Adjust the cursor position if it's in/after the changed
3186 * lines. */
3187 if (curwin->w_cursor.lnum >= hi)
3188 {
3189 curwin->w_cursor.lnum += extra;
3190 check_cursor_col();
3191 }
3192 else if (extra < 0)
3193 {
3194 curwin->w_cursor.lnum = lo;
3195 check_cursor();
3196 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003197 else
3198 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003199 changed_cline_bef_curs();
3200 }
3201 invalidate_botline();
3202}
3203
3204static Vim_Prim prims[]=
3205{
3206 /*
3207 * Buffer-related commands
3208 */
3209 {get_buffer_line, "get-buff-line", 1, 2},
3210 {set_buffer_line, "set-buff-line", 2, 3},
3211 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3212 {get_buffer_name, "get-buff-name", 0, 1},
3213 {get_buffer_num, "get-buff-num", 0, 1},
3214 {get_buffer_size, "get-buff-size", 0, 1},
3215 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3216 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3217 {get_curr_buffer, "curr-buff", 0, 0},
3218 {get_buffer_count, "buff-count", 0, 0},
3219 {get_next_buffer, "get-next-buff", 0, 1},
3220 {get_prev_buffer, "get-prev-buff", 0, 1},
3221 {mzscheme_open_buffer, "open-buff", 1, 1},
3222 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3223 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003224 /*
3225 * Window-related commands
3226 */
3227 {get_curr_win, "curr-win", 0, 0},
3228 {get_window_count, "win-count", 0, 0},
3229 {get_window_by_num, "get-win-by-num", 1, 1},
3230 {get_window_num, "get-win-num", 0, 1},
3231 {get_window_buffer, "get-win-buffer", 0, 1},
3232 {get_window_height, "get-win-height", 0, 1},
3233 {set_window_height, "set-win-height", 1, 2},
3234#ifdef FEAT_VERTSPLIT
3235 {get_window_width, "get-win-width", 0, 1},
3236 {set_window_width, "set-win-width", 1, 2},
3237#endif
3238 {get_cursor, "get-cursor", 0, 1},
3239 {set_cursor, "set-cursor", 1, 2},
3240 {get_window_list, "get-win-list", 0, 1},
3241 /*
3242 * Vim-related commands
3243 */
3244 {vim_command, "command", 1, 1},
3245 {vim_eval, "eval", 1, 1},
3246 {get_range_start, "range-start", 0, 0},
3247 {get_range_end, "range-end", 0, 0},
3248 {mzscheme_beep, "beep", 0, 0},
3249 {get_option, "get-option", 1, 2},
3250 {set_option, "set-option", 1, 2},
3251 /*
3252 * small utilities
3253 */
3254 {vim_bufferp, "buff?", 1, 1},
3255 {vim_windowp, "win?", 1, 1},
3256 {vim_buffer_validp, "buff-valid?", 1, 1},
3257 {vim_window_validp, "win-valid?", 1, 1}
3258};
3259
3260/* return MzScheme wrapper for curbuf */
3261 static vim_mz_buffer *
3262get_vim_curr_buffer(void)
3263{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003264 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003265 return (vim_mz_buffer *)buffer_new(curbuf);
3266 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00003267 return (vim_mz_buffer *)curbuf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003268}
3269
3270/* return MzScheme wrapper for curwin */
3271 static vim_mz_window *
3272get_vim_curr_window(void)
3273{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003274 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003275 return (vim_mz_window *)window_new(curwin);
3276 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00003277 return (vim_mz_window *)curwin->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003278}
3279
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003280 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003281make_modules()
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003282{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003283 int i;
3284 Scheme_Env *mod = NULL;
3285 Scheme_Object *vimext_symbol = NULL;
3286 Scheme_Object *closed_prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003287
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003288 MZ_GC_DECL_REG(3);
3289 MZ_GC_VAR_IN_REG(0, mod);
3290 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3291 MZ_GC_VAR_IN_REG(2, closed_prim);
3292 MZ_GC_REG();
3293
3294 vimext_symbol = scheme_intern_symbol("vimext");
3295 MZ_GC_CHECK();
3296 mod = scheme_primitive_module(vimext_symbol, environment);
3297 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003298 /* all prims made closed so they can access their own names */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003299 for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003300 {
3301 Vim_Prim *prim = prims + i;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003302 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3303 prim->mina, prim->maxa);
3304 scheme_add_global(prim->name, closed_prim, mod);
3305 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003306 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003307 scheme_finish_primitive_module(mod);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003308 MZ_GC_CHECK();
3309 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003310}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003311
Bram Moolenaar555b2802005-05-19 21:08:39 +00003312#ifdef HAVE_SANDBOX
3313static Scheme_Object *M_write = NULL;
3314static Scheme_Object *M_read = NULL;
3315static Scheme_Object *M_execute = NULL;
3316static Scheme_Object *M_delete = NULL;
3317
3318 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003319sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003320{
3321 if (sandbox)
3322 raise_vim_exn(_("not allowed in the Vim sandbox"));
3323}
3324
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003325/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003326 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003327sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003328{
3329 if (sandbox)
3330 {
3331 Scheme_Object *requested_access = argv[2];
3332
3333 if (M_write == NULL)
3334 {
3335 MZ_REGISTER_STATIC(M_write);
3336 M_write = scheme_intern_symbol("write");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003337 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003338 }
3339 if (M_read == NULL)
3340 {
3341 MZ_REGISTER_STATIC(M_read);
3342 M_read = scheme_intern_symbol("read");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003343 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003344 }
3345 if (M_execute == NULL)
3346 {
3347 MZ_REGISTER_STATIC(M_execute);
3348 M_execute = scheme_intern_symbol("execute");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003349 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003350 }
3351 if (M_delete == NULL)
3352 {
3353 MZ_REGISTER_STATIC(M_delete);
3354 M_delete = scheme_intern_symbol("delete");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003355 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003356 }
3357
3358 while (!SCHEME_NULLP(requested_access))
3359 {
3360 Scheme_Object *item = SCHEME_CAR(requested_access);
3361 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3362 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
3363 {
3364 raise_vim_exn(_("not allowed in the Vim sandbox"));
3365 }
3366 requested_access = SCHEME_CDR(requested_access);
3367 }
3368 }
3369 return scheme_void;
3370}
3371
3372 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003373sandbox_network_guard(int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003374{
3375 return scheme_void;
3376}
3377#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00003378
3379#endif