blob: 65873c91392d212bbc2dabaccb859ff9b74a9df8 [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 Moolenaar9e70cf12009-05-26 20:59:55 +000034#include <assert.h>
35
Bram Moolenaar325b7a22004-07-05 15:58:32 +000036/* Base data structures */
37#define SCHEME_VIMBUFFERP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_buffer_type)
38#define SCHEME_VIMWINDOWP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_window_type)
39
40typedef struct
41{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000042 Scheme_Object so;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000043 buf_T *buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000044} vim_mz_buffer;
45
46#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
47
48typedef struct
49{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +000050 Scheme_Object so;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000051 win_T *win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000052} vim_mz_window;
53
54#define INVALID_WINDOW_VALUE ((win_T *)(-1))
55
56/*
57 * Prims that form MzScheme Vim interface
58 */
59typedef struct
60{
61 Scheme_Closed_Prim *prim;
62 char *name;
63 int mina; /* arity information */
64 int maxa;
65} Vim_Prim;
66
67typedef struct
68{
69 char *name;
70 Scheme_Object *port;
71} Port_Info;
72
Bram Moolenaar325b7a22004-07-05 15:58:32 +000073/*
74 *========================================================================
75 * Vim-Control Commands
76 *========================================================================
77 */
78/*
79 *========================================================================
80 * Utility functions for the vim/mzscheme interface
81 *========================================================================
82 */
Bram Moolenaar555b2802005-05-19 21:08:39 +000083#ifdef HAVE_SANDBOX
84static Scheme_Object *sandbox_file_guard(int, Scheme_Object **);
85static Scheme_Object *sandbox_network_guard(int, Scheme_Object **);
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000086static void sandbox_check(void);
Bram Moolenaar555b2802005-05-19 21:08:39 +000087#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000088/* Buffer-related commands */
89static Scheme_Object *buffer_new(buf_T *buf);
90static Scheme_Object *get_buffer_by_name(void *, int, Scheme_Object **);
91static Scheme_Object *get_buffer_by_num(void *, int, Scheme_Object **);
92static Scheme_Object *get_buffer_count(void *, int, Scheme_Object **);
93static Scheme_Object *get_buffer_line(void *, int, Scheme_Object **);
94static Scheme_Object *get_buffer_line_list(void *, int, Scheme_Object **);
95static Scheme_Object *get_buffer_name(void *, int, Scheme_Object **);
96static Scheme_Object *get_buffer_num(void *, int, Scheme_Object **);
97static Scheme_Object *get_buffer_size(void *, int, Scheme_Object **);
98static Scheme_Object *get_curr_buffer(void *, int, Scheme_Object **);
99static Scheme_Object *get_next_buffer(void *, int, Scheme_Object **);
100static Scheme_Object *get_prev_buffer(void *, int, Scheme_Object **);
101static Scheme_Object *mzscheme_open_buffer(void *, int, Scheme_Object **);
102static Scheme_Object *set_buffer_line(void *, int, Scheme_Object **);
103static Scheme_Object *set_buffer_line_list(void *, int, Scheme_Object **);
104static Scheme_Object *insert_buffer_line_list(void *, int, Scheme_Object **);
105static Scheme_Object *get_range_start(void *, int, Scheme_Object **);
106static Scheme_Object *get_range_end(void *, int, Scheme_Object **);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000107static vim_mz_buffer *get_vim_curr_buffer(void);
108
109/* Window-related commands */
110static Scheme_Object *window_new(win_T *win);
111static Scheme_Object *get_curr_win(void *, int, Scheme_Object **);
112static Scheme_Object *get_window_count(void *, int, Scheme_Object **);
113static Scheme_Object *get_window_by_num(void *, int, Scheme_Object **);
114static Scheme_Object *get_window_num(void *, int, Scheme_Object **);
115static Scheme_Object *get_window_buffer(void *, int, Scheme_Object **);
116static Scheme_Object *get_window_height(void *, int, Scheme_Object **);
117static Scheme_Object *set_window_height(void *, int, Scheme_Object **);
118#ifdef FEAT_VERTSPLIT
119static Scheme_Object *get_window_width(void *, int, Scheme_Object **);
120static Scheme_Object *set_window_width(void *, int, Scheme_Object **);
121#endif
122static Scheme_Object *get_cursor(void *, int, Scheme_Object **);
123static Scheme_Object *set_cursor(void *, int, Scheme_Object **);
124static Scheme_Object *get_window_list(void *, int, Scheme_Object **);
125static vim_mz_window *get_vim_curr_window(void);
126
127/* Vim-related commands */
128static Scheme_Object *mzscheme_beep(void *, int, Scheme_Object **);
129static Scheme_Object *get_option(void *, int, Scheme_Object **);
130static Scheme_Object *set_option(void *, int, Scheme_Object **);
131static Scheme_Object *vim_command(void *, int, Scheme_Object **);
132static Scheme_Object *vim_eval(void *, int, Scheme_Object **);
133static Scheme_Object *vim_bufferp(void *data, int, Scheme_Object **);
134static Scheme_Object *vim_windowp(void *data, int, Scheme_Object **);
135static Scheme_Object *vim_buffer_validp(void *data, int, Scheme_Object **);
136static Scheme_Object *vim_window_validp(void *data, int, Scheme_Object **);
137
138/*
139 *========================================================================
140 * Internal Function Prototypes
141 *========================================================================
142 */
143static int vim_error_check(void);
144static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
145static void startup_mzscheme(void);
146static char *string_to_line(Scheme_Object *obj);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000147static void do_output(char *mesg, long len);
148static void do_printf(char *format, ...);
149static void do_flush(void);
150static Scheme_Object *_apply_thunk_catch_exceptions(
151 Scheme_Object *, Scheme_Object **);
152static Scheme_Object *extract_exn_message(Scheme_Object *v);
153static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
154static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000155static void register_vim_exn(void);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000156static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
157 int argc, Scheme_Object **argv);
158static vim_mz_window *get_window_arg(const char *fname, int argnum,
159 int argc, Scheme_Object **argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000160static int line_in_range(linenr_T, buf_T *);
161static void check_line_range(linenr_T, buf_T *);
162static void mz_fix_cursor(int lo, int hi, int extra);
163
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000164static int eval_with_exn_handling(void *, Scheme_Closed_Prim *,
165 Scheme_Object **ret);
166static void make_modules(void);
167static void init_exn_catching_apply(void);
168static int mzscheme_env_main(Scheme_Env *env, int argc, char **argv);
169static int mzscheme_init(void);
170#ifdef FEAT_EVAL
171static Scheme_Object *vim_to_mzscheme(typval_T *vim_value, int depth,
172 Scheme_Hash_Table *visited);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100173static int mzscheme_to_vim(Scheme_Object *obj, typval_T *tv, int depth,
174 Scheme_Hash_Table *visited);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000175#endif
176
177#ifdef MZ_PRECISE_GC
Bram Moolenaar64404472010-06-26 06:24:45 +0200178static int buffer_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000179{
180 return gcBYTES_TO_WORDS(sizeof(vim_mz_buffer));
181}
182static int buffer_mark_proc(void *obj)
183{
184 return buffer_size_proc(obj);
185}
186static int buffer_fixup_proc(void *obj)
187{
188 return buffer_size_proc(obj);
189}
Bram Moolenaar64404472010-06-26 06:24:45 +0200190static int window_size_proc(void *obj UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000191{
192 return gcBYTES_TO_WORDS(sizeof(vim_mz_window));
193}
194static int window_mark_proc(void *obj)
195{
196 return window_size_proc(obj);
197}
198static int window_fixup_proc(void *obj)
199{
200 return window_size_proc(obj);
201}
202#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000203
Bram Moolenaar33570922005-01-25 22:26:29 +0000204#ifdef DYNAMIC_MZSCHEME
205
206static Scheme_Object *dll_scheme_eof;
207static Scheme_Object *dll_scheme_false;
208static Scheme_Object *dll_scheme_void;
209static Scheme_Object *dll_scheme_null;
210static Scheme_Object *dll_scheme_true;
211
212static Scheme_Thread **dll_scheme_current_thread_ptr;
213
214static void (**dll_scheme_console_printf_ptr)(char *str, ...);
215static void (**dll_scheme_console_output_ptr)(char *str, long len);
216static void (**dll_scheme_notify_multithread_ptr)(int on);
217
218static void *(*dll_GC_malloc)(size_t size_in_bytes);
219static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
220static Scheme_Env *(*dll_scheme_basic_env)(void);
221static void (*dll_scheme_check_threads)(void);
222static void (*dll_scheme_register_static)(void *ptr, long size);
223static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
224static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
225 Scheme_Env *env);
226static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
227 Scheme_Object *val, Scheme_Env *env);
228static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
229 Scheme_Object **rands);
230static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000231# if MZSCHEME_VERSION_MAJOR >= 299
232static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
233# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000234static void (*dll_scheme_close_input_port)(Scheme_Object *port);
235static void (*dll_scheme_count_lines)(Scheme_Object *port);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000236#if MZSCHEME_VERSION_MAJOR < 360
Bram Moolenaar33570922005-01-25 22:26:29 +0000237static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
Bram Moolenaar049377e2007-05-12 15:32:12 +0000238#else
239static Scheme_Object *(*dll_scheme_current_continuation_marks)(Scheme_Object *prompt_tag);
240#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000241static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
242static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, long *len);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000243static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
Bram Moolenaar33570922005-01-25 22:26:29 +0000244static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
245 int _num_rands, Scheme_Object **rands, int val);
246static void (*dll_scheme_dont_gc_ptr)(void *p);
247static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
248static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
249 Scheme_Env *env);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000250static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
Bram Moolenaar33570922005-01-25 22:26:29 +0000251 Scheme_Env *env, int all);
252static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000253# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000254static char *(*dll_scheme_format)(char *format, int flen, int argc,
255 Scheme_Object **argv, long *rlen);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000256# else
257static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
258 Scheme_Object **argv, long *rlen);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000259static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000260# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000261static void (*dll_scheme_gc_ptr_ok)(void *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000262# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000263static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
264 long *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000265# else
266static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
267 long *len);
268# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000269static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
270static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
271 Scheme_Env *env);
272static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
273 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
274 mzshort maxa);
275static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000276static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 Scheme_Object *cdr);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000278static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
279 const char *name, mzshort mina, mzshort maxa);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000280# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000281static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
282static Scheme_Object *(*dll_scheme_make_string_output_port)();
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000283# else
284static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
285static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
286# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000287static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
288 int argc, Scheme_Object **argv);
289static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
290 Scheme_Object *field_names, int flags, int *count_out);
291static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
292 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
293 int num_uninit_fields, Scheme_Object *uninit_val,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000294 Scheme_Object *properties
295# if MZSCHEME_VERSION_MAJOR >= 299
296 , Scheme_Object *guard
297# endif
298 );
Bram Moolenaar33570922005-01-25 22:26:29 +0000299static Scheme_Object **(*dll_scheme_make_struct_values)(
300 Scheme_Object *struct_type, Scheme_Object **names, int count,
301 int flags);
302static Scheme_Type (*dll_scheme_make_type)(const char *name);
303static Scheme_Object *(*dll_scheme_make_vector)(int size,
304 Scheme_Object *fill);
305static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
306static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
307 const char *who);
308static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
309 Scheme_Env *for_env);
310static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
311static void (*dll_scheme_raise)(Scheme_Object *exn);
312static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
313static void (*dll_scheme_signal_error)(const char *msg, ...);
314static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
315 int which, int argc, Scheme_Object **argv);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000316# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000317static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000318 Scheme_Object *o);
319static Scheme_Config *(*dll_scheme_current_config)(void);
320static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
321 (Scheme_Object *s);
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000322static Scheme_Object *(*dll_scheme_char_string_to_path)
323 (Scheme_Object *s);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000324# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000325static Scheme_Hash_Table *(*dll_scheme_make_hash_table)(int type);
326static void (*dll_scheme_hash_set)(Scheme_Hash_Table *table,
327 Scheme_Object *key, Scheme_Object *value);
328static Scheme_Object *(*dll_scheme_hash_get)(Scheme_Hash_Table *table,
329 Scheme_Object *key);
330static Scheme_Object *(*dll_scheme_make_double)(double d);
331# ifdef INCLUDE_MZSCHEME_BASE
332static Scheme_Object *(*dll_scheme_make_sized_byte_string)(char *chars,
333 long len, int copy);
334static Scheme_Object *(*dll_scheme_namespace_require)(Scheme_Object *req);
335# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000336
337/* arrays are imported directly */
338# define scheme_eof dll_scheme_eof
339# define scheme_false dll_scheme_false
340# define scheme_void dll_scheme_void
341# define scheme_null dll_scheme_null
342# define scheme_true dll_scheme_true
343
344/* pointers are GetProceAddress'ed as pointers to pointer */
345# define scheme_current_thread (*dll_scheme_current_thread_ptr)
346# define scheme_console_printf (*dll_scheme_console_printf_ptr)
347# define scheme_console_output (*dll_scheme_console_output_ptr)
348# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
349
350/* and functions in a usual way */
351# define GC_malloc dll_GC_malloc
352# define GC_malloc_atomic dll_GC_malloc_atomic
353
354# define scheme_add_global dll_scheme_add_global
355# define scheme_add_global_symbol dll_scheme_add_global_symbol
356# define scheme_apply dll_scheme_apply
357# define scheme_basic_env dll_scheme_basic_env
358# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000359# if MZSCHEME_VERSION_MAJOR >= 299
360# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
361# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000362# define scheme_check_threads dll_scheme_check_threads
363# define scheme_close_input_port dll_scheme_close_input_port
364# define scheme_count_lines dll_scheme_count_lines
365# define scheme_current_continuation_marks \
366 dll_scheme_current_continuation_marks
367# define scheme_display dll_scheme_display
368# define scheme_display_to_string dll_scheme_display_to_string
369# define scheme_do_eval dll_scheme_do_eval
370# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000371# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000372# define scheme_eval dll_scheme_eval
373# define scheme_eval_string dll_scheme_eval_string
374# define scheme_eval_string_all dll_scheme_eval_string_all
375# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000376# if MZSCHEME_VERSION_MAJOR < 299
377# define scheme_format dll_scheme_format
378# else
379# define scheme_format_utf8 dll_scheme_format_utf8
380# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000381# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000382# if MZSCHEME_VERSION_MAJOR < 299
383# define scheme_get_sized_string_output dll_scheme_get_sized_string_output
384# else
385# define scheme_get_sized_byte_string_output \
386 dll_scheme_get_sized_byte_string_output
Bram Moolenaar555b2802005-05-19 21:08:39 +0000387# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000388# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000389# define scheme_intern_symbol dll_scheme_intern_symbol
390# define scheme_lookup_global dll_scheme_lookup_global
391# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
392# define scheme_make_integer_value dll_scheme_make_integer_value
Bram Moolenaar33570922005-01-25 22:26:29 +0000393# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000394# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000395# if MZSCHEME_VERSION_MAJOR < 299
396# define scheme_make_string dll_scheme_make_string
397# define scheme_make_string_output_port dll_scheme_make_string_output_port
398# else
399# define scheme_make_byte_string dll_scheme_make_byte_string
400# define scheme_make_byte_string_output_port \
401 dll_scheme_make_byte_string_output_port
402# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000403# define scheme_make_struct_instance dll_scheme_make_struct_instance
404# define scheme_make_struct_names dll_scheme_make_struct_names
405# define scheme_make_struct_type dll_scheme_make_struct_type
406# define scheme_make_struct_values dll_scheme_make_struct_values
407# define scheme_make_type dll_scheme_make_type
408# define scheme_make_vector dll_scheme_make_vector
409# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
410# define scheme_open_input_file dll_scheme_open_input_file
411# define scheme_primitive_module dll_scheme_primitive_module
412# define scheme_proper_list_length dll_scheme_proper_list_length
413# define scheme_raise dll_scheme_raise
414# define scheme_read dll_scheme_read
415# define scheme_register_static dll_scheme_register_static
416# define scheme_set_stack_base dll_scheme_set_stack_base
417# define scheme_signal_error dll_scheme_signal_error
418# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000419# if MZSCHEME_VERSION_MAJOR >= 299
420# define scheme_set_param dll_scheme_set_param
421# define scheme_current_config dll_scheme_current_config
422# define scheme_char_string_to_byte_string \
423 dll_scheme_char_string_to_byte_string
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000424# define scheme_char_string_to_path \
425 dll_scheme_char_string_to_path
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000426# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000427# define scheme_make_hash_table dll_scheme_make_hash_table
428# define scheme_hash_set dll_scheme_hash_set
429# define scheme_hash_get dll_scheme_hash_get
430# define scheme_make_double dll_scheme_make_double
431# ifdef INCLUDE_MZSCHEME_BASE
432# define scheme_make_sized_byte_string dll_scheme_make_sized_byte_string
433# define scheme_namespace_require dll_scheme_namespace_require
434# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000435
436typedef struct
437{
438 char *name;
439 void **ptr;
440} Thunk_Info;
441
442static Thunk_Info mzgc_imports[] = {
443 {"GC_malloc", (void **)&dll_GC_malloc},
444 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
445 {NULL, NULL}};
446
447static Thunk_Info mzsch_imports[] = {
448 {"scheme_eof", (void **)&dll_scheme_eof},
449 {"scheme_false", (void **)&dll_scheme_false},
450 {"scheme_void", (void **)&dll_scheme_void},
451 {"scheme_null", (void **)&dll_scheme_null},
452 {"scheme_true", (void **)&dll_scheme_true},
453 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
454 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
455 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000456 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000457 (void **)&dll_scheme_notify_multithread_ptr},
458 {"scheme_add_global", (void **)&dll_scheme_add_global},
459 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
460 {"scheme_apply", (void **)&dll_scheme_apply},
461 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000462# if MZSCHEME_VERSION_MAJOR >= 299
463 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
464# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000465 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
466 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
467 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
468 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000469 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000470 (void **)&dll_scheme_current_continuation_marks},
471 {"scheme_display", (void **)&dll_scheme_display},
472 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
473 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
474 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000475 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000476 {"scheme_eval", (void **)&dll_scheme_eval},
477 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
478 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000479 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000480 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000481# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000482 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000483# else
484 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000485 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000486#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000487 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000488# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000489 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000490 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000491# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000492 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000493 (void **)&dll_scheme_get_sized_byte_string_output},
494#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000495 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
496 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000497 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000498 (void **)&dll_scheme_make_closed_prim_w_arity},
499 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
Bram Moolenaar33570922005-01-25 22:26:29 +0000500 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000501 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000502# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000503 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000504 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000505 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000506# else
507 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000508 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000509 (void **)&dll_scheme_make_byte_string_output_port},
510# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000511 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000512 (void **)&dll_scheme_make_struct_instance},
513 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
514 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
515 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
516 {"scheme_make_type", (void **)&dll_scheme_make_type},
517 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
518 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
519 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
520 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
521 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
522 {"scheme_raise", (void **)&dll_scheme_raise},
523 {"scheme_read", (void **)&dll_scheme_read},
524 {"scheme_register_static", (void **)&dll_scheme_register_static},
525 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
526 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
527 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000528# if MZSCHEME_VERSION_MAJOR >= 299
529 {"scheme_set_param", (void **)&dll_scheme_set_param},
530 {"scheme_current_config", (void **)&dll_scheme_current_config},
531 {"scheme_char_string_to_byte_string",
532 (void **)&dll_scheme_char_string_to_byte_string},
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000533 {"scheme_char_string_to_path", (void **)&dll_scheme_char_string_to_path},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000534# endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000535 {"scheme_make_hash_table", (void **)&dll_scheme_make_hash_table},
536 {"scheme_hash_set", (void **)&dll_scheme_hash_set},
537 {"scheme_hash_get", (void **)&dll_scheme_hash_get},
538 {"scheme_make_double", (void **)&dll_scheme_make_double},
539# ifdef INCLUDE_MZSCHEME_BASE
540 {"scheme_make_sized_byte_string", (void **)&dll_scheme_make_sized_byte_string},
541 {"scheme_namespace_require", (void **)&dll_scheme_namespace_require},
542#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000543 {NULL, NULL}};
544
545static HINSTANCE hMzGC = 0;
546static HINSTANCE hMzSch = 0;
547
548static void dynamic_mzscheme_end(void);
549static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
550 int verbose);
551
552 static int
553mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
554{
555 Thunk_Info *thunk = NULL;
556
557 if (hMzGC && hMzSch)
558 return OK;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200559 hMzSch = vimLoadLib(sch_dll);
560 hMzGC = vimLoadLib(gc_dll);
Bram Moolenaar33570922005-01-25 22:26:29 +0000561
562 if (!hMzSch)
563 {
564 if (verbose)
565 EMSG2(_(e_loadlib), sch_dll);
566 return FAIL;
567 }
568
569 if (!hMzGC)
570 {
571 if (verbose)
572 EMSG2(_(e_loadlib), gc_dll);
573 return FAIL;
574 }
575
576 for (thunk = mzsch_imports; thunk->name; thunk++)
577 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000578 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000579 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
580 {
581 FreeLibrary(hMzSch);
582 hMzSch = 0;
583 FreeLibrary(hMzGC);
584 hMzGC = 0;
585 if (verbose)
586 EMSG2(_(e_loadfunc), thunk->name);
587 return FAIL;
588 }
589 }
590 for (thunk = mzgc_imports; thunk->name; thunk++)
591 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000592 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000593 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
594 {
595 FreeLibrary(hMzSch);
596 hMzSch = 0;
597 FreeLibrary(hMzGC);
598 hMzGC = 0;
599 if (verbose)
600 EMSG2(_(e_loadfunc), thunk->name);
601 return FAIL;
602 }
603 }
604 return OK;
605}
606
607 int
608mzscheme_enabled(int verbose)
609{
610 return mzscheme_runtime_link_init(
611 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
612}
613
614 static void
615dynamic_mzscheme_end(void)
616{
617 if (hMzSch)
618 {
619 FreeLibrary(hMzSch);
620 hMzSch = 0;
621 }
622 if (hMzGC)
623 {
624 FreeLibrary(hMzGC);
625 hMzGC = 0;
626 }
627}
628#endif /* DYNAMIC_MZSCHEME */
629
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000630/* need to put it here for dynamic stuff to work */
Bram Moolenaare484c942009-09-11 10:21:41 +0000631#if defined(INCLUDE_MZSCHEME_BASE)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000632# include "mzscheme_base.c"
Bram Moolenaare484c942009-09-11 10:21:41 +0000633#elif MZSCHEME_VERSION_MAJOR >= 400
634# 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 +0000635#endif
636
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000637/*
638 *========================================================================
639 * 1. MzScheme interpreter startup
640 *========================================================================
641 */
642
643static Scheme_Type mz_buffer_type;
644static Scheme_Type mz_window_type;
645
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000646static int initialized = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000647
648/* global environment */
649static Scheme_Env *environment = NULL;
650/* output/error handlers */
651static Scheme_Object *curout = NULL;
652static Scheme_Object *curerr = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000653/* exn:vim exception */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000654static Scheme_Object *exn_catching_apply = NULL;
655static Scheme_Object *exn_p = NULL;
656static Scheme_Object *exn_message = NULL;
657static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000658
659#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
660static void *stack_base = NULL;
661#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000662
663static long range_start;
664static long range_end;
665
666/* MzScheme threads scheduling stuff */
667static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000668
669#if defined(FEAT_GUI_W32)
670static void CALLBACK timer_proc(HWND, UINT, UINT, DWORD);
671static UINT timer_id = 0;
672#elif defined(FEAT_GUI_GTK)
673static gint timer_proc(gpointer);
674static guint timer_id = 0;
675#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
676static void timer_proc(XtPointer, XtIntervalId *);
677static XtIntervalId timer_id = (XtIntervalId)0;
678#elif defined(FEAT_GUI_MAC)
679pascal void timer_proc(EventLoopTimerRef, void *);
680static EventLoopTimerRef timer_id = NULL;
681static EventLoopTimerUPP timerUPP;
682#endif
683
684#ifndef FEAT_GUI_W32 /* Win32 console and Unix */
685 void
686mzvim_check_threads(void)
687{
688 /* Last time MzScheme threads were scheduled */
689 static time_t mz_last_time = 0;
690
691 if (mz_threads_allow && p_mzq > 0)
692 {
693 time_t now = time(NULL);
694
695 if ((now - mz_last_time) * 1000 > p_mzq)
696 {
697 mz_last_time = now;
698 scheme_check_threads();
699 }
700 }
701}
702#endif
703
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000704#ifdef MZSCHEME_GUI_THREADS
705static void setup_timer(void);
706static void remove_timer(void);
707
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000708/* timers are presented in GUI only */
709# if defined(FEAT_GUI_W32)
710 static void CALLBACK
Bram Moolenaar64404472010-06-26 06:24:45 +0200711timer_proc(HWND hwnd UNUSED, UINT uMsg UNUSED, UINT idEvent UNUSED, DWORD dwTime UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000712# elif defined(FEAT_GUI_GTK)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000713 static gint
Bram Moolenaar64404472010-06-26 06:24:45 +0200714timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000715# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000716 static void
Bram Moolenaar64404472010-06-26 06:24:45 +0200717timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000718# elif defined(FEAT_GUI_MAC)
719 pascal void
Bram Moolenaar64404472010-06-26 06:24:45 +0200720timer_proc(EventLoopTimerRef theTimer UNUSED, void *userData UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000721# endif
722{
723 scheme_check_threads();
724# if defined(FEAT_GUI_GTK)
725 return TRUE; /* continue receiving notifications */
726# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
727 /* renew timeout */
728 if (mz_threads_allow && p_mzq > 0)
729 timer_id = XtAppAddTimeOut(app_context, p_mzq,
730 timer_proc, NULL);
731# endif
732}
733
734 static void
735setup_timer(void)
736{
737# if defined(FEAT_GUI_W32)
738 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
739# elif defined(FEAT_GUI_GTK)
740 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
741# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
742 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
743# elif defined(FEAT_GUI_MAC)
744 timerUPP = NewEventLoopTimerUPP(timer_proc);
745 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
746 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
747# endif
748}
749
750 static void
751remove_timer(void)
752{
753# if defined(FEAT_GUI_W32)
754 KillTimer(NULL, timer_id);
755# elif defined(FEAT_GUI_GTK)
756 gtk_timeout_remove(timer_id);
757# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
758 XtRemoveTimeOut(timer_id);
759# elif defined(FEAT_GUI_MAC)
760 RemoveEventLoopTimer(timer_id);
761 DisposeEventLoopTimerUPP(timerUPP);
762# endif
763 timer_id = 0;
764}
765
766 void
767mzvim_reset_timer(void)
768{
769 if (timer_id != 0)
770 remove_timer();
771 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
772 setup_timer();
773}
774
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000775#endif /* MZSCHEME_GUI_THREADS */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000776
777 static void
778notify_multithread(int on)
779{
780 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000781#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000782 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
783 setup_timer();
784 if (!on && timer_id != 0)
785 remove_timer();
786#endif
787}
788
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000789 void
790mzscheme_end(void)
791{
Bram Moolenaar33570922005-01-25 22:26:29 +0000792#ifdef DYNAMIC_MZSCHEME
793 dynamic_mzscheme_end();
794#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000795}
796
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100797#if MZSCHEME_VERSION_MAJOR >= 500 && defined(WIN32) && defined(USE_THREAD_LOCAL)
798static __declspec(thread) void *tls_space;
799#endif
800
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000801 void
802mzscheme_main(void)
803{
Bram Moolenaar2d0860d2010-11-03 21:59:30 +0100804#if MZSCHEME_VERSION_MAJOR >= 500 && defined(WIN32) && defined(USE_THREAD_LOCAL)
805 scheme_register_tls_space(&tls_space, 0);
806#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000807#if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR >= 400
808 /* use trampoline for precise GC in MzScheme >= 4.x */
809 scheme_main_setup(TRUE, mzscheme_env_main, 0, NULL);
810#else
811 mzscheme_env_main(NULL, 0, NULL);
812#endif
813}
814
815 static int
Bram Moolenaar64404472010-06-26 06:24:45 +0200816mzscheme_env_main(Scheme_Env *env, int argc UNUSED, char **argv UNUSED)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000817{
818 /* neither argument nor return values are used */
819#ifdef MZ_PRECISE_GC
820# if MZSCHEME_VERSION_MAJOR < 400
821 /*
822 * Starting from version 4.x, embedding applications must use
823 * scheme_main_setup/scheme_main_stack_setup trampolines
824 * rather than setting stack base directly with scheme_set_stack_base
825 */
826 Scheme_Object *dummy = NULL;
827 MZ_GC_DECL_REG(1);
828 MZ_GC_VAR_IN_REG(0, dummy);
829
830 stack_base = &__gc_var_stack__;
831# else
832 /* environment has been created by us by Scheme */
833 environment = env;
834# endif
835 /*
836 * In 4.x, all activities must be performed inside trampoline
837 * so we are forced to initialise GC immediately
838 * This can be postponed in 3.x but I see no point in implementing
839 * a feature which will work in older versions only.
840 * One would better use conservative GC if he needs dynamic MzScheme
841 */
842 mzscheme_init();
843#else
844 int dummy = 0;
845 stack_base = (void *)&dummy;
846#endif
847 main_loop(FALSE, FALSE);
848#if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR < 400
849 /* releasing dummy */
850 MZ_GC_REG();
851 MZ_GC_UNREG();
852#endif
853 return 0;
854}
855
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000856 static void
857startup_mzscheme(void)
858{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000859#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
860 scheme_set_stack_base(stack_base, 1);
861#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000862
863 MZ_REGISTER_STATIC(environment);
864 MZ_REGISTER_STATIC(curout);
865 MZ_REGISTER_STATIC(curerr);
866 MZ_REGISTER_STATIC(exn_catching_apply);
867 MZ_REGISTER_STATIC(exn_p);
868 MZ_REGISTER_STATIC(exn_message);
869 MZ_REGISTER_STATIC(vim_exn);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000870
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000871#if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
872 /* in newer versions of precise GC the initial env has been created */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000873 environment = scheme_basic_env();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000874#endif
875 MZ_GC_CHECK();
876
877#ifdef INCLUDE_MZSCHEME_BASE
878 {
879 /*
Bram Moolenaare484c942009-09-11 10:21:41 +0000880 * versions 4.x do not provide Scheme bindings by default
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000881 * we need to add them explicitly
882 */
883 Scheme_Object *scheme_base_symbol = NULL;
884 MZ_GC_DECL_REG(1);
885 MZ_GC_VAR_IN_REG(0, scheme_base_symbol);
886 MZ_GC_REG();
Bram Moolenaare484c942009-09-11 10:21:41 +0000887 /* invoke function from generated and included mzscheme_base.c */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000888 declare_modules(environment);
889 scheme_base_symbol = scheme_intern_symbol("scheme/base");
890 MZ_GC_CHECK();
891 scheme_namespace_require(scheme_base_symbol);
892 MZ_GC_CHECK();
893 MZ_GC_UNREG();
894 }
895#endif
896 register_vim_exn();
897 /* use new environment to initialise exception handling */
898 init_exn_catching_apply();
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000899
900 /* redirect output */
901 scheme_console_output = do_output;
902 scheme_console_printf = do_printf;
903
904#ifdef MZSCHEME_COLLECTS
905 /* setup 'current-library-collection-paths' parameter */
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000906# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000907 {
908 Scheme_Object *coll_byte_string = NULL;
909 Scheme_Object *coll_char_string = NULL;
910 Scheme_Object *coll_path = NULL;
911 Scheme_Object *coll_pair = NULL;
912 Scheme_Config *config = NULL;
913
914 MZ_GC_DECL_REG(5);
915 MZ_GC_VAR_IN_REG(0, coll_byte_string);
916 MZ_GC_VAR_IN_REG(1, coll_char_string);
917 MZ_GC_VAR_IN_REG(2, coll_path);
918 MZ_GC_VAR_IN_REG(3, coll_pair);
919 MZ_GC_VAR_IN_REG(4, config);
920 MZ_GC_REG();
921 coll_byte_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
922 MZ_GC_CHECK();
923 coll_char_string = scheme_byte_string_to_char_string(coll_byte_string);
924 MZ_GC_CHECK();
925 coll_path = scheme_char_string_to_path(coll_char_string);
926 MZ_GC_CHECK();
927 coll_pair = scheme_make_pair(coll_path, scheme_null);
928 MZ_GC_CHECK();
929 config = scheme_config;
930 MZ_GC_CHECK();
931 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
932 MZ_GC_CHECK();
933 MZ_GC_UNREG();
934 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000935# else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000936 {
937 Scheme_Object *coll_string = NULL;
938 Scheme_Object *coll_pair = NULL;
939 Scheme_Config *config = NULL;
940
941 MZ_GC_DECL_REG(3);
942 MZ_GC_VAR_IN_REG(0, coll_string);
943 MZ_GC_VAR_IN_REG(1, coll_pair);
944 MZ_GC_VAR_IN_REG(2, config);
945 MZ_GC_REG();
946 coll_string = scheme_make_string(MZSCHEME_COLLECTS);
947 MZ_GC_CHECK();
948 coll_pair = scheme_make_pair(coll_string, scheme_null);
949 MZ_GC_CHECK();
950 config = scheme_config;
951 MZ_GC_CHECK();
952 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
953 MZ_GC_CHECK();
954 MZ_GC_UNREG();
955 }
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000956# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000957#endif
Bram Moolenaar555b2802005-05-19 21:08:39 +0000958#ifdef HAVE_SANDBOX
Bram Moolenaar555b2802005-05-19 21:08:39 +0000959 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000960 Scheme_Object *make_security_guard = NULL;
961 MZ_GC_DECL_REG(1);
962 MZ_GC_VAR_IN_REG(0, make_security_guard);
963 MZ_GC_REG();
964
965#if MZSCHEME_VERSION_MAJOR < 400
966 {
967 Scheme_Object *make_security_guard_symbol = NULL;
968 MZ_GC_DECL_REG(1);
969 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
970 MZ_GC_REG();
971 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
972 MZ_GC_CHECK();
973 make_security_guard = scheme_lookup_global(
974 make_security_guard_symbol, environment);
975 MZ_GC_UNREG();
976 }
977#else
978 make_security_guard = scheme_builtin_value("make-security-guard");
979 MZ_GC_CHECK();
980#endif
981
982 /* setup sandbox guards */
983 if (make_security_guard != NULL)
984 {
985 Scheme_Object *args[3] = {NULL, NULL, NULL};
986 Scheme_Object *guard = NULL;
987 Scheme_Config *config = NULL;
988 MZ_GC_DECL_REG(5);
989 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
990 MZ_GC_VAR_IN_REG(3, guard);
991 MZ_GC_VAR_IN_REG(4, config);
992 MZ_GC_REG();
993 config = scheme_config;
994 MZ_GC_CHECK();
995 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
996 MZ_GC_CHECK();
997 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
998 "sandbox-file-guard", 3, 3);
999 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
1000 "sandbox-network-guard", 4, 4);
1001 guard = scheme_apply(make_security_guard, 3, args);
1002 MZ_GC_CHECK();
1003 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
1004 MZ_GC_CHECK();
1005 MZ_GC_UNREG();
1006 }
1007 MZ_GC_UNREG();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001008 }
1009#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001010 /* Create buffer and window types for use in Scheme code */
1011 mz_buffer_type = scheme_make_type("<vim-buffer>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001012 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001013 mz_window_type = scheme_make_type("<vim-window>");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001014 MZ_GC_CHECK();
1015#ifdef MZ_PRECISE_GC
1016 GC_register_traversers(mz_buffer_type,
1017 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1018 TRUE, TRUE);
1019 GC_register_traversers(mz_window_type,
1020 window_size_proc, window_mark_proc, window_fixup_proc,
1021 TRUE, TRUE);
1022#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001023
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001024 make_modules();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001025
1026 /*
1027 * setup callback to receive notifications
1028 * whether thread scheduling is (or not) required
1029 */
1030 scheme_notify_multithread = notify_multithread;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001031}
1032
1033/*
1034 * This routine is called for each new invocation of MzScheme
1035 * to make sure things are properly initialized.
1036 */
1037 static int
1038mzscheme_init(void)
1039{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001040 if (!initialized)
1041 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001042#ifdef DYNAMIC_MZSCHEME
1043 if (!mzscheme_enabled(TRUE))
1044 {
Bram Moolenaarb849e712009-06-24 15:51:37 +00001045 EMSG(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
Bram Moolenaar33570922005-01-25 22:26:29 +00001046 return -1;
1047 }
1048#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001049 startup_mzscheme();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001050 initialized = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001051 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001052 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001053 Scheme_Config *config = NULL;
1054 MZ_GC_DECL_REG(1);
1055 MZ_GC_VAR_IN_REG(0, config);
1056 MZ_GC_REG();
1057 config = scheme_config;
1058 MZ_GC_CHECK();
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001059 /* recreate ports each call effectively clearing these ones */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001060 curout = scheme_make_string_output_port();
1061 MZ_GC_CHECK();
1062 curerr = scheme_make_string_output_port();
1063 MZ_GC_CHECK();
1064 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1065 MZ_GC_CHECK();
1066 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1067 MZ_GC_CHECK();
1068 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001069 }
1070
1071 return 0;
1072}
1073
1074/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001075 *========================================================================
1076 * 2. External Interface
1077 *========================================================================
1078 */
1079
1080/*
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001081 * Evaluate command with exception handling
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001082 */
1083 static int
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001084eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001085{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001086 Scheme_Object *value = NULL;
1087 Scheme_Object *exn = NULL;
1088 Scheme_Object *prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001089
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001090 MZ_GC_DECL_REG(3);
1091 MZ_GC_VAR_IN_REG(0, value);
1092 MZ_GC_VAR_IN_REG(1, exn);
1093 MZ_GC_VAR_IN_REG(2, prim);
1094 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001095
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001096 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1097 MZ_GC_CHECK();
1098 value = _apply_thunk_catch_exceptions(prim, &exn);
1099 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001100
1101 if (!value)
1102 {
1103 value = extract_exn_message(exn);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001104 /* Got an exn? */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001105 if (value)
1106 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001107 scheme_display(value, curerr); /* Send to stderr-vim */
1108 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001109 do_flush();
1110 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001111 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001112 /* `raise' was called on some arbitrary value */
1113 return FAIL;
1114 }
1115
1116 if (ret != NULL) /* if pointer to retval supported give it up */
1117 *ret = value;
1118 /* Print any result, as long as it's not a void */
1119 else if (!SCHEME_VOIDP(value))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001120 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001121 scheme_display(value, curout); /* Send to stdout-vim */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001122 MZ_GC_CHECK();
1123 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001124
1125 do_flush();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001126 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001127 return OK;
1128}
1129
1130/* :mzscheme */
1131 static int
1132do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1133{
1134 if (mzscheme_init())
1135 return FAIL;
1136
1137 range_start = eap->line1;
1138 range_end = eap->line2;
1139
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001140 return eval_with_exn_handling(data, what, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001141}
1142
1143/*
1144 * Routine called by VIM when deleting a buffer
1145 */
1146 void
1147mzscheme_buffer_free(buf_T *buf)
1148{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001149 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001150 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001151 vim_mz_buffer *bp;
1152
Bram Moolenaare344bea2005-09-01 20:46:49 +00001153 bp = buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001154 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001155 buf->b_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001156 scheme_gc_ptr_ok(bp);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001157 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001158 }
1159}
1160
1161/*
1162 * Routine called by VIM when deleting a Window
1163 */
1164 void
1165mzscheme_window_free(win_T *win)
1166{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001167 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001168 {
1169 vim_mz_window *wp;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001170 wp = win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001171 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001172 win->w_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001173 scheme_gc_ptr_ok(wp);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001174 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001175 }
1176}
1177
1178/*
1179 * ":mzscheme" (or ":mz")
1180 */
1181 void
1182ex_mzscheme(exarg_T *eap)
1183{
1184 char_u *script;
1185
1186 script = script_get(eap, eap->arg);
1187 if (!eap->skip)
1188 {
1189 if (script == NULL)
1190 do_mzscheme_command(eap, eap->arg, do_eval);
1191 else
1192 {
1193 do_mzscheme_command(eap, script, do_eval);
1194 vim_free(script);
1195 }
1196 }
1197}
1198
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001199 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001200do_load(void *data, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001201{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001202 Scheme_Object *expr = NULL;
1203 Scheme_Object *result = NULL;
1204 char *file = NULL;
1205 Port_Info *pinfo = (Port_Info *)data;
1206
1207 MZ_GC_DECL_REG(3);
1208 MZ_GC_VAR_IN_REG(0, expr);
1209 MZ_GC_VAR_IN_REG(1, result);
1210 MZ_GC_VAR_IN_REG(2, file);
1211 MZ_GC_REG();
1212
1213 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1214 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001215
1216 /* make Vim expansion */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001217 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001218 pinfo->port = scheme_open_input_file(file, "mzfile");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001219 MZ_GC_CHECK();
1220 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1221 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001222
1223 /* Like REPL but print only last result */
1224 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001225 {
1226 result = scheme_eval(expr, environment);
1227 MZ_GC_CHECK();
1228 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001229
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00001230 /* errors will be caught in do_mzscheme_command and ex_mzfile */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001231 scheme_close_input_port(pinfo->port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001232 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001233 pinfo->port = NULL;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001234 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001235 return result;
1236}
1237
1238/* :mzfile */
1239 void
1240ex_mzfile(exarg_T *eap)
1241{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001242 Port_Info pinfo = {NULL, NULL};
1243
1244 MZ_GC_DECL_REG(1);
1245 MZ_GC_VAR_IN_REG(0, pinfo.port);
1246 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001247
1248 pinfo.name = (char *)eap->arg;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001249 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1250 && pinfo.port != NULL) /* looks like port was not closed */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001251 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001252 scheme_close_input_port(pinfo.port);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001253 MZ_GC_CHECK();
1254 }
1255 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001256}
1257
1258
1259/*
1260 *========================================================================
1261 * Exception handling code -- cribbed form the MzScheme sources and
1262 * Matthew Flatt's "Inside PLT MzScheme" document.
1263 *========================================================================
1264 */
1265 static void
1266init_exn_catching_apply(void)
1267{
1268 if (!exn_catching_apply)
1269 {
1270 char *e =
1271 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001272 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001273 "(cons #t (thunk))))";
1274
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001275 exn_catching_apply = scheme_eval_string(e, environment);
1276 MZ_GC_CHECK();
1277 exn_p = scheme_builtin_value("exn?");
1278 MZ_GC_CHECK();
1279 exn_message = scheme_builtin_value("exn-message");
1280 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001281 }
1282}
1283
1284/*
1285 * This function applies a thunk, returning the Scheme value if there's
1286 * no exception, otherwise returning NULL and setting *exn to the raised
1287 * value (usually an exn structure).
1288 */
1289 static Scheme_Object *
1290_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1291{
1292 Scheme_Object *v;
1293
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001294 v = _scheme_apply(exn_catching_apply, 1, &f);
1295 /* v is a pair: (cons #t value) or (cons #f exn) */
1296
1297 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1298 return SCHEME_CDR(v);
1299 else
1300 {
1301 *exn = SCHEME_CDR(v);
1302 return NULL;
1303 }
1304}
1305
1306 static Scheme_Object *
1307extract_exn_message(Scheme_Object *v)
1308{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001309 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1310 return _scheme_apply(exn_message, 1, &v);
1311 else
1312 return NULL; /* Not an exn structure */
1313}
1314
1315 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001316do_eval(void *s, int noargc UNUSED, Scheme_Object **noargv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001317{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001318 return scheme_eval_string_all((char *)s, environment, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001319}
1320
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001321/*
1322 *========================================================================
1323 * 3. MzScheme I/O Handlers
1324 *========================================================================
1325 */
1326 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001327do_intrnl_output(char *mesg, int error)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001328{
1329 char *p, *prev;
1330
1331 prev = mesg;
1332 p = strchr(prev, '\n');
1333 while (p)
1334 {
1335 *p = '\0';
1336 if (error)
1337 EMSG(prev);
1338 else
1339 MSG(prev);
1340 prev = p + 1;
1341 p = strchr(prev, '\n');
1342 }
1343
1344 if (error)
1345 EMSG(prev);
1346 else
1347 MSG(prev);
1348}
1349
1350 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001351do_output(char *mesg, long len UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001352{
Bram Moolenaar64404472010-06-26 06:24:45 +02001353 do_intrnl_output(mesg, 0);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001354}
1355
1356 static void
Bram Moolenaar64404472010-06-26 06:24:45 +02001357do_err_output(char *mesg)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001358{
Bram Moolenaar64404472010-06-26 06:24:45 +02001359 do_intrnl_output(mesg, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001360}
1361
1362 static void
1363do_printf(char *format, ...)
1364{
Bram Moolenaar64404472010-06-26 06:24:45 +02001365 do_intrnl_output(format, 1);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001366}
1367
1368 static void
1369do_flush(void)
1370{
1371 char *buff;
1372 long length;
1373
1374 buff = scheme_get_sized_string_output(curerr, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001375 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001376 if (length)
1377 {
Bram Moolenaar64404472010-06-26 06:24:45 +02001378 do_err_output(buff);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001379 return;
1380 }
1381
1382 buff = scheme_get_sized_string_output(curout, &length);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001383 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001384 if (length)
1385 do_output(buff, length);
1386}
1387
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001388/*
1389 *========================================================================
1390 * 4. Implementation of the Vim Features for MzScheme
1391 *========================================================================
1392 */
1393
1394/* (command {command-string}) */
1395 static Scheme_Object *
1396vim_command(void *data, int argc, Scheme_Object **argv)
1397{
1398 Vim_Prim *prim = (Vim_Prim *)data;
1399 char *cmd = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1400
1401 /* may be use do_cmdline_cmd? */
1402 do_cmdline((char_u *)cmd, NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
1403 update_screen(VALID);
1404
1405 raise_if_error();
1406 return scheme_void;
1407}
1408
1409/* (eval {expr-string}) */
1410 static Scheme_Object *
1411vim_eval(void *data, int argc, Scheme_Object **argv)
1412{
1413#ifdef FEAT_EVAL
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001414 Vim_Prim *prim = (Vim_Prim *)data;
1415 char *expr;
1416 Scheme_Object *result;
1417 /* hash table to store visited values to avoid infinite loops */
1418 Scheme_Hash_Table *visited = NULL;
1419 typval_T *vim_result;
1420
1421 MZ_GC_DECL_REG(1);
1422 MZ_GC_VAR_IN_REG(0, visited);
1423 MZ_GC_REG();
1424
1425 visited = scheme_make_hash_table(SCHEME_hash_ptr);
1426 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001427
1428 expr = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001429 vim_result = eval_expr((char_u *)expr, NULL);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001430
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001431 if (vim_result == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001432 raise_vim_exn(_("invalid expression"));
1433
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001434 result = vim_to_mzscheme(vim_result, 1, visited);
1435 free_tv(vim_result);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001436
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001437 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001438 return result;
1439#else
1440 raise_vim_exn(_("expressions disabled at compile time"));
1441 /* unreachable */
1442 return scheme_false;
1443#endif
1444}
1445
1446/* (range-start) */
1447 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001448get_range_start(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001449{
1450 return scheme_make_integer(range_start);
1451}
1452
1453/* (range-end) */
1454 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001455get_range_end(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001456{
1457 return scheme_make_integer(range_end);
1458}
1459
1460/* (beep) */
1461 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001462mzscheme_beep(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001463{
1464 vim_beep();
1465 return scheme_void;
1466}
1467
1468static Scheme_Object *M_global = NULL;
1469
1470/* (get-option {option-name}) [buffer/window] */
1471 static Scheme_Object *
1472get_option(void *data, int argc, Scheme_Object **argv)
1473{
1474 Vim_Prim *prim = (Vim_Prim *)data;
1475 char_u *name;
1476 long value;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001477 char *strval;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001478 int rc;
1479 Scheme_Object *rval;
1480 int opt_flags = 0;
1481 buf_T *save_curb = curbuf;
1482 win_T *save_curw = curwin;
1483
1484 name = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1485
1486 if (argc > 1)
1487 {
1488 if (M_global == NULL)
1489 {
1490 MZ_REGISTER_STATIC(M_global);
1491 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001492 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001493 }
1494
1495 if (argv[1] == M_global)
1496 opt_flags = OPT_GLOBAL;
1497 else if (SCHEME_VIMBUFFERP(argv[1]))
1498 {
1499 curbuf = get_valid_buffer(argv[1]);
1500 opt_flags = OPT_LOCAL;
1501 }
1502 else if (SCHEME_VIMWINDOWP(argv[1]))
1503 {
1504 win_T *win = get_valid_window(argv[1]);
1505
1506 curwin = win;
1507 curbuf = win->w_buffer;
1508 opt_flags = OPT_LOCAL;
1509 }
1510 else
1511 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1512 }
1513
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001514 rc = get_option_value(name, &value, (char_u **)&strval, opt_flags);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001515 curbuf = save_curb;
1516 curwin = save_curw;
1517
1518 switch (rc)
1519 {
1520 case 1:
1521 return scheme_make_integer_value(value);
1522 case 0:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001523 rval = scheme_make_string(strval);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001524 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001525 vim_free(strval);
1526 return rval;
1527 case -1:
1528 case -2:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001529 raise_vim_exn(_("hidden option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001530 case -3:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001531 raise_vim_exn(_("unknown option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001532 }
1533 /* unreachable */
1534 return scheme_void;
1535}
1536
1537/* (set-option {option-changing-string} [buffer/window]) */
1538 static Scheme_Object *
1539set_option(void *data, int argc, Scheme_Object **argv)
1540{
1541 char_u *cmd;
1542 int opt_flags = 0;
1543 buf_T *save_curb = curbuf;
1544 win_T *save_curw = curwin;
1545 Vim_Prim *prim = (Vim_Prim *)data;
1546
1547 GUARANTEE_STRING(prim->name, 0);
1548 if (argc > 1)
1549 {
1550 if (M_global == NULL)
1551 {
1552 MZ_REGISTER_STATIC(M_global);
1553 M_global = scheme_intern_symbol("global");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001554 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001555 }
1556
1557 if (argv[1] == M_global)
1558 opt_flags = OPT_GLOBAL;
1559 else if (SCHEME_VIMBUFFERP(argv[1]))
1560 {
1561 curbuf = get_valid_buffer(argv[1]);
1562 opt_flags = OPT_LOCAL;
1563 }
1564 else if (SCHEME_VIMWINDOWP(argv[1]))
1565 {
1566 win_T *win = get_valid_window(argv[1]);
1567 curwin = win;
1568 curbuf = win->w_buffer;
1569 opt_flags = OPT_LOCAL;
1570 }
1571 else
1572 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1573 }
1574
1575 /* do_set can modify cmd, make copy */
1576 cmd = vim_strsave((char_u *)SCHEME_STR_VAL(argv[0]));
1577 do_set(cmd, opt_flags);
1578 vim_free(cmd);
1579 update_screen(NOT_VALID);
1580 curbuf = save_curb;
1581 curwin = save_curw;
1582 raise_if_error();
1583 return scheme_void;
1584}
1585
1586/*
1587 *===========================================================================
1588 * 5. Vim Window-related Manipulation Functions
1589 *===========================================================================
1590 */
1591
1592/* (curr-win) */
1593 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001594get_curr_win(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001595{
1596 return (Scheme_Object *)get_vim_curr_window();
1597}
1598
1599/* (win-count) */
1600 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001601get_window_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001602{
1603 win_T *w;
1604 int n = 0;
1605
Bram Moolenaarf740b292006-02-16 22:11:02 +00001606 for (w = firstwin; w != NULL; w = w->w_next)
1607 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001608 return scheme_make_integer(n);
1609}
1610
1611/* (get-win-list [buffer]) */
1612 static Scheme_Object *
1613get_window_list(void *data, int argc, Scheme_Object **argv)
1614{
1615 Vim_Prim *prim = (Vim_Prim *)data;
1616 vim_mz_buffer *buf;
1617 Scheme_Object *list;
1618 win_T *w;
1619
1620 buf = get_buffer_arg(prim->name, 0, argc, argv);
1621 list = scheme_null;
1622
Bram Moolenaarf740b292006-02-16 22:11:02 +00001623 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001624 if (w->w_buffer == buf->buf)
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001625 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001626 list = scheme_make_pair(window_new(w), list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001627 MZ_GC_CHECK();
1628 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001629
1630 return list;
1631}
1632
1633 static Scheme_Object *
1634window_new(win_T *win)
1635{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001636 vim_mz_window *self = NULL;
1637
1638 MZ_GC_DECL_REG(1);
1639 MZ_GC_VAR_IN_REG(0, self);
1640 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001641
1642 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001643 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001644 * then we can get at it in win_free() in vim.
1645 *
1646 * On a win_free() we set the Scheme object's win_T *field
1647 * to an invalid value. We trap all uses of a window
1648 * object, and reject them if the win_T *field is invalid.
1649 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001650 if (win->w_mzscheme_ref != NULL)
1651 return win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001652
1653 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_window));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001654 vim_memset(self, 0, sizeof(vim_mz_window));
1655 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001656 MZ_GC_CHECK();
Bram Moolenaare344bea2005-09-01 20:46:49 +00001657 win->w_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001658 self->win = win;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001659 self->so.type = mz_window_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001660
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001661 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001662 return (Scheme_Object *)(self);
1663}
1664
1665/* (get-win-num [window]) */
1666 static Scheme_Object *
1667get_window_num(void *data, int argc, Scheme_Object **argv)
1668{
1669 Vim_Prim *prim = (Vim_Prim *)data;
1670 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
1671 int nr = 1;
1672 win_T *wp;
1673
1674 for (wp = firstwin; wp != win; wp = wp->w_next)
1675 ++nr;
1676
1677 return scheme_make_integer(nr);
1678}
1679
1680/* (get-win-by-num {windownum}) */
1681 static Scheme_Object *
1682get_window_by_num(void *data, int argc, Scheme_Object **argv)
1683{
1684 Vim_Prim *prim = (Vim_Prim *)data;
1685 win_T *win;
1686 int fnum;
1687
1688 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1689 if (fnum < 1)
1690 scheme_signal_error(_("window index is out of range"));
1691
Bram Moolenaarf740b292006-02-16 22:11:02 +00001692 for (win = firstwin; win != NULL; win = win->w_next, --fnum)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001693 if (fnum == 1) /* to be 1-based */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001694 return window_new(win);
1695
1696 return scheme_false;
1697}
1698
1699/* (get-win-buffer [window]) */
1700 static Scheme_Object *
1701get_window_buffer(void *data, int argc, Scheme_Object **argv)
1702{
1703 Vim_Prim *prim = (Vim_Prim *)data;
1704 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1705
1706 return buffer_new(win->win->w_buffer);
1707}
1708
1709/* (get-win-height [window]) */
1710 static Scheme_Object *
1711get_window_height(void *data, int argc, Scheme_Object **argv)
1712{
1713 Vim_Prim *prim = (Vim_Prim *)data;
1714 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1715
1716 return scheme_make_integer(win->win->w_height);
1717}
1718
1719/* (set-win-height {height} [window]) */
1720 static Scheme_Object *
1721set_window_height(void *data, int argc, Scheme_Object **argv)
1722{
1723 Vim_Prim *prim = (Vim_Prim *)data;
1724 vim_mz_window *win;
1725 win_T *savewin;
1726 int height;
1727
1728 win = get_window_arg(prim->name, 1, argc, argv);
1729 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1730
1731#ifdef FEAT_GUI
1732 need_mouse_correct = TRUE;
1733#endif
1734
1735 savewin = curwin;
1736 curwin = win->win;
1737 win_setheight(height);
1738 curwin = savewin;
1739
1740 raise_if_error();
1741 return scheme_void;
1742}
1743
1744#ifdef FEAT_VERTSPLIT
1745/* (get-win-width [window]) */
1746 static Scheme_Object *
1747get_window_width(void *data, int argc, Scheme_Object **argv)
1748{
1749 Vim_Prim *prim = (Vim_Prim *)data;
1750 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1751
1752 return scheme_make_integer(W_WIDTH(win->win));
1753}
1754
1755/* (set-win-width {width} [window]) */
1756 static Scheme_Object *
1757set_window_width(void *data, int argc, Scheme_Object **argv)
1758{
1759 Vim_Prim *prim = (Vim_Prim *)data;
1760 vim_mz_window *win;
1761 win_T *savewin;
1762 int width = 0;
1763
1764 win = get_window_arg(prim->name, 1, argc, argv);
1765 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1766
1767# ifdef FEAT_GUI
1768 need_mouse_correct = TRUE;
1769# endif
1770
1771 savewin = curwin;
1772 curwin = win->win;
1773 win_setwidth(width);
1774 curwin = savewin;
1775
1776 raise_if_error();
1777 return scheme_void;
1778}
1779#endif
1780
1781/* (get-cursor [window]) -> (line . col) */
1782 static Scheme_Object *
1783get_cursor(void *data, int argc, Scheme_Object **argv)
1784{
1785 Vim_Prim *prim = (Vim_Prim *)data;
1786 vim_mz_window *win;
1787 pos_T pos;
1788
1789 win = get_window_arg(prim->name, 0, argc, argv);
1790 pos = win->win->w_cursor;
1791 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
1792 scheme_make_integer_value((long)pos.col + 1));
1793}
1794
1795/* (set-cursor (line . col) [window]) */
1796 static Scheme_Object *
1797set_cursor(void *data, int argc, Scheme_Object **argv)
1798{
1799 Vim_Prim *prim = (Vim_Prim *)data;
1800 vim_mz_window *win;
1801 long lnum = 0;
1802 long col = 0;
1803
Bram Moolenaar555b2802005-05-19 21:08:39 +00001804#ifdef HAVE_SANDBOX
1805 sandbox_check();
1806#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001807 win = get_window_arg(prim->name, 1, argc, argv);
1808 GUARANTEE_PAIR(prim->name, 0);
1809
1810 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
1811 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
1812 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
1813
1814 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
1815 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
1816
1817 check_line_range(lnum, win->win->w_buffer);
1818 /* don't know how to catch invalid column value */
1819
1820 win->win->w_cursor.lnum = lnum;
1821 win->win->w_cursor.col = col;
1822 update_screen(VALID);
1823
1824 raise_if_error();
1825 return scheme_void;
1826}
1827/*
1828 *===========================================================================
1829 * 6. Vim Buffer-related Manipulation Functions
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001830 *===========================================================================
1831 */
1832
1833/* (open-buff {filename}) */
1834 static Scheme_Object *
1835mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
1836{
1837 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001838 char_u *fname;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001839 int num = 0;
1840 Scheme_Object *onum;
1841
Bram Moolenaar555b2802005-05-19 21:08:39 +00001842#ifdef HAVE_SANDBOX
1843 sandbox_check();
1844#endif
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001845 fname = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001846 /* TODO make open existing file */
1847 num = buflist_add(fname, BLN_LISTED | BLN_CURBUF);
1848
1849 if (num == 0)
1850 raise_vim_exn(_("couldn't open buffer"));
1851
1852 onum = scheme_make_integer(num);
1853 return get_buffer_by_num(data, 1, &onum);
1854}
1855
1856/* (get-buff-by-num {buffernum}) */
1857 static Scheme_Object *
1858get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
1859{
1860 Vim_Prim *prim = (Vim_Prim *)data;
1861 buf_T *buf;
1862 int fnum;
1863
1864 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1865
1866 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001867 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001868 return buffer_new(buf);
1869
1870 return scheme_false;
1871}
1872
1873/* (get-buff-by-name {buffername}) */
1874 static Scheme_Object *
1875get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
1876{
1877 Vim_Prim *prim = (Vim_Prim *)data;
1878 buf_T *buf;
1879 char_u *fname;
1880
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001881 fname = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001882
1883 for (buf = firstbuf; buf; buf = buf->b_next)
1884 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1885 /* empty string */
1886 {
1887 if (fname[0] == NUL)
1888 return buffer_new(buf);
1889 }
1890 else if (!fnamecmp(buf->b_ffname, fname)
1891 || !fnamecmp(buf->b_sfname, fname))
1892 /* either short or long filename matches */
1893 return buffer_new(buf);
1894
1895 return scheme_false;
1896}
1897
1898/* (get-next-buff [buffer]) */
1899 static Scheme_Object *
1900get_next_buffer(void *data, int argc, Scheme_Object **argv)
1901{
1902 Vim_Prim *prim = (Vim_Prim *)data;
1903 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1904
1905 if (buf->b_next == NULL)
1906 return scheme_false;
1907 else
1908 return buffer_new(buf->b_next);
1909}
1910
1911/* (get-prev-buff [buffer]) */
1912 static Scheme_Object *
1913get_prev_buffer(void *data, int argc, Scheme_Object **argv)
1914{
1915 Vim_Prim *prim = (Vim_Prim *)data;
1916 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1917
1918 if (buf->b_prev == NULL)
1919 return scheme_false;
1920 else
1921 return buffer_new(buf->b_prev);
1922}
1923
1924/* (get-buff-num [buffer]) */
1925 static Scheme_Object *
1926get_buffer_num(void *data, int argc, Scheme_Object **argv)
1927{
1928 Vim_Prim *prim = (Vim_Prim *)data;
1929 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1930
1931 return scheme_make_integer(buf->buf->b_fnum);
1932}
1933
1934/* (buff-count) */
1935 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001936get_buffer_count(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001937{
1938 buf_T *b;
1939 int n = 0;
1940
1941 for (b = firstbuf; b; b = b->b_next) ++n;
1942 return scheme_make_integer(n);
1943}
1944
1945/* (get-buff-name [buffer]) */
1946 static Scheme_Object *
1947get_buffer_name(void *data, int argc, Scheme_Object **argv)
1948{
1949 Vim_Prim *prim = (Vim_Prim *)data;
1950 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1951
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001952 return scheme_make_string((char *)buf->buf->b_ffname);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001953}
1954
1955/* (curr-buff) */
1956 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02001957get_curr_buffer(void *data UNUSED, int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001958{
1959 return (Scheme_Object *)get_vim_curr_buffer();
1960}
1961
1962 static Scheme_Object *
1963buffer_new(buf_T *buf)
1964{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001965 vim_mz_buffer *self = NULL;
1966
1967 MZ_GC_DECL_REG(1);
1968 MZ_GC_VAR_IN_REG(0, self);
1969 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001970
1971 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001972 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001973 * then we can get at it in buf_freeall() in vim.
1974 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001975 if (buf->b_mzscheme_ref)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001976 return buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001977
1978 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_buffer));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001979 vim_memset(self, 0, sizeof(vim_mz_buffer));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001980 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
1981 MZ_GC_CHECK();
Bram Moolenaare344bea2005-09-01 20:46:49 +00001982 buf->b_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001983 self->buf = buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001984 self->so.type = mz_buffer_type;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001985
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00001986 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001987 return (Scheme_Object *)(self);
1988}
1989
1990/*
1991 * (get-buff-size [buffer])
1992 *
1993 * Get the size (number of lines) in the current buffer.
1994 */
1995 static Scheme_Object *
1996get_buffer_size(void *data, int argc, Scheme_Object **argv)
1997{
1998 Vim_Prim *prim = (Vim_Prim *)data;
1999 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
2000
2001 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
2002}
2003
2004/*
2005 * (get-buff-line {linenr} [buffer])
2006 *
2007 * Get a line from the specified buffer. The line number is
2008 * in Vim format (1-based). The line is returned as a MzScheme
2009 * string object.
2010 */
2011 static Scheme_Object *
2012get_buffer_line(void *data, int argc, Scheme_Object **argv)
2013{
2014 Vim_Prim *prim = (Vim_Prim *)data;
2015 vim_mz_buffer *buf;
2016 int linenr;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002017 char_u *line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002018
2019 buf = get_buffer_arg(prim->name, 1, argc, argv);
2020 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2021 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2022
2023 raise_if_error();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002024 return scheme_make_string((char *)line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002025}
2026
2027
2028/*
2029 * (get-buff-line-list {start} {end} [buffer])
2030 *
2031 * Get a list of lines from the specified buffer. The line numbers
2032 * are in Vim format (1-based). The range is from lo up to, but not
2033 * including, hi. The list is returned as a list of string objects.
2034 */
2035 static Scheme_Object *
2036get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2037{
2038 Vim_Prim *prim = (Vim_Prim *)data;
2039 vim_mz_buffer *buf;
2040 int i, hi, lo, n;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002041 Scheme_Object *list = NULL;
2042
2043 MZ_GC_DECL_REG(1);
2044 MZ_GC_VAR_IN_REG(0, list);
2045 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002046
2047 buf = get_buffer_arg(prim->name, 2, argc, argv);
2048 list = scheme_null;
2049 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2050 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2051
2052 /*
2053 * Handle some error conditions
2054 */
2055 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002056 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002057
2058 if (hi < 0)
2059 hi = 0;
2060 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002061 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002062
2063 n = hi - lo;
2064
2065 for (i = n; i >= 0; --i)
2066 {
2067 Scheme_Object *str = scheme_make_string(
2068 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2069 raise_if_error();
2070
2071 /* Set the list item */
2072 list = scheme_make_pair(str, list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002073 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002074 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002075 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002076 return list;
2077}
2078
2079/*
2080 * (set-buff-line {linenr} {string/#f} [buffer])
2081 *
2082 * Replace a line in the specified buffer. The line number is
2083 * in Vim format (1-based). The replacement line is given as
2084 * an MzScheme string object. The object is checked for validity
2085 * and correct format. An exception is thrown if the values are not
2086 * the correct format.
2087 *
2088 * It returns a Scheme Object that indicates the length of the
2089 * string changed.
2090 */
2091 static Scheme_Object *
2092set_buffer_line(void *data, int argc, Scheme_Object **argv)
2093{
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002094 /* First of all, we check the value of the supplied MzScheme object.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002095 * There are three cases:
2096 * 1. #f - this is a deletion.
2097 * 2. A string - this is a replacement.
2098 * 3. Anything else - this is an error.
2099 */
2100 Vim_Prim *prim = (Vim_Prim *)data;
2101 vim_mz_buffer *buf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002102 Scheme_Object *line = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002103 char *save;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002104 int n;
2105
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002106 MZ_GC_DECL_REG(1);
2107 MZ_GC_VAR_IN_REG(0, line);
2108 MZ_GC_REG();
2109
Bram Moolenaar555b2802005-05-19 21:08:39 +00002110#ifdef HAVE_SANDBOX
2111 sandbox_check();
2112#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002113 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2114 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002115 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002116 line = argv[1];
2117 buf = get_buffer_arg(prim->name, 2, argc, argv);
2118
2119 check_line_range(n, buf->buf);
2120
2121 if (SCHEME_FALSEP(line))
2122 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002123 buf_T *savebuf = curbuf;
2124
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002125 curbuf = buf->buf;
2126
2127 if (u_savedel((linenr_T)n, 1L) == FAIL)
2128 {
2129 curbuf = savebuf;
2130 raise_vim_exn(_("cannot save undo information"));
2131 }
2132 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2133 {
2134 curbuf = savebuf;
2135 raise_vim_exn(_("cannot delete line"));
2136 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002137 if (buf->buf == curwin->w_buffer)
2138 mz_fix_cursor(n, n + 1, -1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002139 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002140
2141 curbuf = savebuf;
2142
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002143 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002144 raise_if_error();
2145 return scheme_void;
2146 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002147 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002148 {
2149 /* Otherwise it's a line */
2150 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002151
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002152 save = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002153
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002154 curbuf = buf->buf;
2155
2156 if (u_savesub((linenr_T)n) == FAIL)
2157 {
2158 curbuf = savebuf;
2159 vim_free(save);
2160 raise_vim_exn(_("cannot save undo information"));
2161 }
2162 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2163 {
2164 curbuf = savebuf;
2165 vim_free(save);
2166 raise_vim_exn(_("cannot replace line"));
2167 }
2168 else
2169 {
2170 vim_free(save);
2171 changed_bytes((linenr_T)n, 0);
2172 }
2173
2174 curbuf = savebuf;
2175
2176 /* Check that the cursor is not beyond the end of the line now. */
2177 if (buf->buf == curwin->w_buffer)
2178 check_cursor_col();
2179
2180 MZ_GC_UNREG();
2181 raise_if_error();
2182 return scheme_void;
2183 }
2184}
2185
2186 static void
2187free_array(char **array)
2188{
2189 char **curr = array;
2190 while (*curr != NULL)
2191 vim_free(*curr++);
2192 vim_free(array);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002193}
2194
2195/*
2196 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2197 *
2198 * Replace a range of lines in the specified buffer. The line numbers are in
2199 * Vim format (1-based). The range is from lo up to, but not including, hi.
2200 * The replacement lines are given as a Scheme list of string objects. The
2201 * list is checked for validity and correct format.
2202 *
2203 * Errors are returned as a value of FAIL. The return value is OK on success.
2204 * If OK is returned and len_change is not NULL, *len_change is set to the
2205 * change in the buffer length.
2206 */
2207 static Scheme_Object *
2208set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2209{
2210 /* First of all, we check the type of the supplied MzScheme object.
2211 * There are three cases:
2212 * 1. #f - this is a deletion.
2213 * 2. A list - this is a replacement.
2214 * 3. Anything else - this is an error.
2215 */
2216 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002217 vim_mz_buffer *buf = NULL;
2218 Scheme_Object *line_list = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002219 int i, old_len, new_len, hi, lo;
2220 long extra;
2221
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002222 MZ_GC_DECL_REG(1);
2223 MZ_GC_VAR_IN_REG(0, line_list);
2224 MZ_GC_REG();
2225
Bram Moolenaar555b2802005-05-19 21:08:39 +00002226#ifdef HAVE_SANDBOX
2227 sandbox_check();
2228#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002229 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2230 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2231 if (!SCHEME_PAIRP(argv[2])
2232 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2233 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2234 line_list = argv[2];
2235 buf = get_buffer_arg(prim->name, 3, argc, argv);
2236 old_len = hi - lo;
2237 if (old_len < 0) /* process inverse values wisely */
2238 {
2239 i = lo;
2240 lo = hi;
2241 hi = i;
2242 old_len = -old_len;
2243 }
2244 extra = 0;
2245
2246 check_line_range(lo, buf->buf); /* inclusive */
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002247 check_line_range(hi - 1, buf->buf); /* exclusive */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002248
2249 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2250 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002251 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002252 curbuf = buf->buf;
2253
2254 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2255 {
2256 curbuf = savebuf;
2257 raise_vim_exn(_("cannot save undo information"));
2258 }
2259 else
2260 {
2261 for (i = 0; i < old_len; i++)
2262 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2263 {
2264 curbuf = savebuf;
2265 raise_vim_exn(_("cannot delete line"));
2266 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002267 if (buf->buf == curwin->w_buffer)
2268 mz_fix_cursor(lo, hi, -old_len);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002269 deleted_lines_mark((linenr_T)lo, (long)old_len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002270 }
2271
2272 curbuf = savebuf;
2273
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002274 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002275 raise_if_error();
2276 return scheme_void;
2277 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002278 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002279 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002280 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002281
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002282 /* List */
2283 new_len = scheme_proper_list_length(line_list);
2284 MZ_GC_CHECK();
2285 if (new_len < 0) /* improper or cyclic list */
2286 scheme_wrong_type(prim->name, "proper list",
2287 2, argc, argv);
2288 else
2289 {
2290 char **array = NULL;
2291 Scheme_Object *line = NULL;
2292 Scheme_Object *rest = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002293
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002294 MZ_GC_DECL_REG(2);
2295 MZ_GC_VAR_IN_REG(0, line);
2296 MZ_GC_VAR_IN_REG(1, rest);
2297 MZ_GC_REG();
2298
2299 array = (char **)alloc(new_len * sizeof(char *));
2300 vim_memset(array, 0, new_len * sizeof(char *));
2301
2302 rest = line_list;
2303 for (i = 0; i < new_len; ++i)
2304 {
2305 line = SCHEME_CAR(rest);
2306 rest = SCHEME_CDR(rest);
2307 if (!SCHEME_STRINGP(line))
2308 {
2309 free_array(array);
2310 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2311 }
2312 array[i] = string_to_line(line);
2313 }
2314
2315 curbuf = buf->buf;
2316
2317 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2318 {
2319 curbuf = savebuf;
2320 free_array(array);
2321 raise_vim_exn(_("cannot save undo information"));
2322 }
2323
2324 /*
2325 * If the size of the range is reducing (ie, new_len < old_len) we
2326 * need to delete some old_len. We do this at the start, by
2327 * repeatedly deleting line "lo".
2328 */
2329 for (i = 0; i < old_len - new_len; ++i)
2330 {
2331 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2332 {
2333 curbuf = savebuf;
2334 free_array(array);
2335 raise_vim_exn(_("cannot delete line"));
2336 }
2337 extra--;
2338 }
2339
2340 /*
2341 * For as long as possible, replace the existing old_len with the
2342 * new old_len. This is a more efficient operation, as it requires
2343 * less memory allocation and freeing.
2344 */
2345 for (i = 0; i < old_len && i < new_len; i++)
2346 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2347 {
2348 curbuf = savebuf;
2349 free_array(array);
2350 raise_vim_exn(_("cannot replace line"));
2351 }
2352
2353 /*
2354 * Now we may need to insert the remaining new_len. We don't need to
2355 * free the string passed back because MzScheme has control of that
2356 * memory.
2357 */
2358 while (i < new_len)
2359 {
2360 if (ml_append((linenr_T)(lo + i - 1),
2361 (char_u *)array[i], 0, FALSE) == FAIL)
2362 {
2363 curbuf = savebuf;
2364 free_array(array);
2365 raise_vim_exn(_("cannot insert line"));
2366 }
2367 ++i;
2368 ++extra;
2369 }
2370 MZ_GC_UNREG();
2371 free_array(array);
2372 }
2373
2374 /*
2375 * Adjust marks. Invalidate any which lie in the
2376 * changed range, and move any in the remainder of the buffer.
2377 */
2378 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra);
2379 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2380
2381 if (buf->buf == curwin->w_buffer)
2382 mz_fix_cursor(lo, hi, extra);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002383 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002384
2385 MZ_GC_UNREG();
2386 raise_if_error();
2387 return scheme_void;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002388 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002389}
2390
2391/*
2392 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2393 *
Bram Moolenaar0a1c0ec2009-12-16 18:02:47 +00002394 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002395 * The line number is in Vim format (1-based). The lines to be inserted are
2396 * given as an MzScheme list of string objects or as a single string. The lines
2397 * to be added are checked for validity and correct format. Errors are
2398 * returned as a value of FAIL. The return value is OK on success.
2399 * If OK is returned and len_change is not NULL, *len_change
2400 * is set to the change in the buffer length.
2401 */
2402 static Scheme_Object *
2403insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2404{
2405 Vim_Prim *prim = (Vim_Prim *)data;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002406 vim_mz_buffer *buf = NULL;
2407 Scheme_Object *list = NULL;
2408 char *str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002409 int i, n, size;
2410
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002411 MZ_GC_DECL_REG(1);
2412 MZ_GC_VAR_IN_REG(0, list);
2413 MZ_GC_REG();
2414
Bram Moolenaar555b2802005-05-19 21:08:39 +00002415#ifdef HAVE_SANDBOX
2416 sandbox_check();
2417#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002418 /*
2419 * First of all, we check the type of the supplied MzScheme object.
2420 * It must be a string or a list, or the call is in error.
2421 */
2422 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2423 list = argv[1];
2424
2425 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2426 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2427 buf = get_buffer_arg(prim->name, 2, argc, argv);
2428
2429 if (n != 0) /* 0 can be used in insert */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002430 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002431 if (SCHEME_STRINGP(list))
2432 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002433 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002434
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002435 str = string_to_line(list);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002436 curbuf = buf->buf;
2437
2438 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2439 {
2440 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002441 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002442 raise_vim_exn(_("cannot save undo information"));
2443 }
2444 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2445 {
2446 curbuf = savebuf;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002447 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002448 raise_vim_exn(_("cannot insert line"));
2449 }
2450 else
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002451 {
2452 vim_free(str);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002453 appended_lines_mark((linenr_T)n, 1L);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002454 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002455
2456 curbuf = savebuf;
2457 update_screen(VALID);
2458
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002459 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002460 raise_if_error();
2461 return scheme_void;
2462 }
2463
2464 /* List */
2465 size = scheme_proper_list_length(list);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002466 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002467 if (size < 0) /* improper or cyclic list */
2468 scheme_wrong_type(prim->name, "proper list",
2469 2, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002470 else
2471 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002472 Scheme_Object *line = NULL;
2473 Scheme_Object *rest = NULL;
2474 char **array;
2475 buf_T *savebuf = curbuf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002476
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002477 MZ_GC_DECL_REG(2);
2478 MZ_GC_VAR_IN_REG(0, line);
2479 MZ_GC_VAR_IN_REG(1, rest);
2480 MZ_GC_REG();
2481
2482 array = (char **)alloc(size * sizeof(char *));
2483 vim_memset(array, 0, size * sizeof(char *));
2484
2485 rest = list;
2486 for (i = 0; i < size; ++i)
2487 {
2488 line = SCHEME_CAR(rest);
2489 rest = SCHEME_CDR(rest);
2490 array[i] = string_to_line(line);
2491 }
2492
2493 curbuf = buf->buf;
2494
2495 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2496 {
2497 curbuf = savebuf;
2498 free_array(array);
2499 raise_vim_exn(_("cannot save undo information"));
2500 }
2501 else
2502 {
2503 for (i = 0; i < size; ++i)
2504 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2505 0, FALSE) == FAIL)
2506 {
2507 curbuf = savebuf;
2508 free_array(array);
2509 raise_vim_exn(_("cannot insert line"));
2510 }
2511
2512 if (i > 0)
2513 appended_lines_mark((linenr_T)n, (long)i);
2514 }
2515 free_array(array);
2516 MZ_GC_UNREG();
2517 curbuf = savebuf;
2518 update_screen(VALID);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002519 }
2520
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002521 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002522 raise_if_error();
2523 return scheme_void;
2524}
2525
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002526/*
2527 * Predicates
2528 */
2529/* (buff? obj) */
2530 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002531vim_bufferp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002532{
2533 if (SCHEME_VIMBUFFERP(argv[0]))
2534 return scheme_true;
2535 else
2536 return scheme_false;
2537}
2538
2539/* (win? obj) */
2540 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002541vim_windowp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002542{
2543 if (SCHEME_VIMWINDOWP(argv[0]))
2544 return scheme_true;
2545 else
2546 return scheme_false;
2547}
2548
2549/* (buff-valid? obj) */
2550 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002551vim_buffer_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002552{
2553 if (SCHEME_VIMBUFFERP(argv[0])
2554 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2555 return scheme_true;
2556 else
2557 return scheme_false;
2558}
2559
2560/* (win-valid? obj) */
2561 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02002562vim_window_validp(void *data UNUSED, int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002563{
2564 if (SCHEME_VIMWINDOWP(argv[0])
2565 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2566 return scheme_true;
2567 else
2568 return scheme_false;
2569}
2570
2571/*
2572 *===========================================================================
2573 * Utilities
2574 *===========================================================================
2575 */
2576
2577/*
2578 * Convert an MzScheme string into a Vim line.
2579 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002580 * All internal nulls are replaced by newline characters.
2581 * It is an error for the string to contain newline characters.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002582 *
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002583 * Returns pointer to Vim allocated memory
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002584 */
2585 static char *
2586string_to_line(Scheme_Object *obj)
2587{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002588 char *scheme_str = NULL;
2589 char *vim_str = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002590 long len;
2591 int i;
2592
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002593 scheme_str = scheme_display_to_string(obj, &len);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002594
2595 /* Error checking: String must not contain newlines, as we
2596 * are replacing a single line, and we must replace it with
2597 * a single line.
2598 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002599 if (memchr(scheme_str, '\n', len))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002600 scheme_signal_error(_("string cannot contain newlines"));
2601
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002602 vim_str = (char *)alloc(len + 1);
2603
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002604 /* Create a copy of the string, with internal nulls replaced by
2605 * newline characters, as is the vim convention.
2606 */
2607 for (i = 0; i < len; ++i)
2608 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002609 if (scheme_str[i] == '\0')
2610 vim_str[i] = '\n';
2611 else
2612 vim_str[i] = scheme_str[i];
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002613 }
2614
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002615 vim_str[i] = '\0';
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002616
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002617 MZ_GC_CHECK();
2618 return vim_str;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002619}
2620
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002621#ifdef FEAT_EVAL
2622/*
2623 * Convert Vim value into MzScheme, adopted from if_python.c
2624 */
2625 static Scheme_Object *
2626vim_to_mzscheme(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
2627{
2628 Scheme_Object *result = NULL;
2629 int new_value = TRUE;
2630
2631 MZ_GC_DECL_REG(1);
2632 MZ_GC_VAR_IN_REG(0, result);
2633 MZ_GC_REG();
2634
2635 /* Avoid infinite recursion */
2636 if (depth > 100)
2637 {
2638 MZ_GC_UNREG();
2639 return scheme_void;
2640 }
2641
2642 /* Check if we run into a recursive loop. The item must be in visited
2643 * then and we can use it again.
2644 */
2645 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
2646 MZ_GC_CHECK();
2647 if (result != NULL) /* found, do nothing */
2648 new_value = FALSE;
2649 else if (vim_value->v_type == VAR_STRING)
2650 {
2651 result = scheme_make_string((char *)vim_value->vval.v_string);
2652 MZ_GC_CHECK();
2653 }
2654 else if (vim_value->v_type == VAR_NUMBER)
2655 {
2656 result = scheme_make_integer((long)vim_value->vval.v_number);
2657 MZ_GC_CHECK();
2658 }
2659# ifdef FEAT_FLOAT
2660 else if (vim_value->v_type == VAR_FLOAT)
2661 {
2662 result = scheme_make_double((double)vim_value->vval.v_float);
2663 MZ_GC_CHECK();
2664 }
2665# endif
2666 else if (vim_value->v_type == VAR_LIST)
2667 {
2668 list_T *list = vim_value->vval.v_list;
2669 listitem_T *curr;
2670
2671 if (list == NULL || list->lv_first == NULL)
2672 result = scheme_null;
2673 else
2674 {
2675 Scheme_Object *obj = NULL;
2676
2677 MZ_GC_DECL_REG(1);
2678 MZ_GC_VAR_IN_REG(0, obj);
2679 MZ_GC_REG();
2680
2681 curr = list->lv_last;
2682 obj = vim_to_mzscheme(&curr->li_tv, depth + 1, visited);
2683 result = scheme_make_pair(obj, scheme_null);
2684 MZ_GC_CHECK();
2685
2686 while (curr != list->lv_first)
2687 {
2688 curr = curr->li_prev;
2689 obj = vim_to_mzscheme(&curr->li_tv, depth + 1, visited);
2690 result = scheme_make_pair(obj, result);
2691 MZ_GC_CHECK();
2692 }
2693 }
2694 MZ_GC_UNREG();
2695 }
2696 else if (vim_value->v_type == VAR_DICT)
2697 {
2698 Scheme_Object *key = NULL;
2699 Scheme_Object *obj = NULL;
2700
2701 MZ_GC_DECL_REG(2);
2702 MZ_GC_VAR_IN_REG(0, key);
2703 MZ_GC_VAR_IN_REG(1, obj);
2704 MZ_GC_REG();
2705
2706 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
2707 MZ_GC_CHECK();
2708 if (vim_value->vval.v_dict != NULL)
2709 {
2710 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
2711 long_u todo = ht->ht_used;
2712 hashitem_T *hi;
2713 dictitem_T *di;
2714
2715 for (hi = ht->ht_array; todo > 0; ++hi)
2716 {
2717 if (!HASHITEM_EMPTY(hi))
2718 {
2719 --todo;
2720
2721 di = dict_lookup(hi);
2722 obj = vim_to_mzscheme(&di->di_tv, depth + 1, visited);
2723 key = scheme_make_string((char *)hi->hi_key);
2724 MZ_GC_CHECK();
2725 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
2726 MZ_GC_CHECK();
2727 }
2728 }
2729 }
2730 MZ_GC_UNREG();
2731 }
2732 else
2733 {
2734 result = scheme_void;
2735 new_value = FALSE;
2736 }
2737 if (new_value)
2738 {
2739 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
2740 MZ_GC_CHECK();
2741 }
2742 MZ_GC_UNREG();
2743 return result;
2744}
Bram Moolenaar7e506b62010-01-19 15:55:06 +01002745
2746 static int
2747mzscheme_to_vim(Scheme_Object *obj, typval_T *tv, int depth,
2748 Scheme_Hash_Table *visited)
2749{
2750 int status = OK;
2751 typval_T *found;
2752 MZ_GC_CHECK();
2753 if (depth > 100) /* limit the deepest recursion level */
2754 {
2755 tv->v_type = VAR_NUMBER;
2756 tv->vval.v_number = 0;
2757 return FAIL;
2758 }
2759
2760 found = (typval_T *)scheme_hash_get(visited, obj);
2761 if (found != NULL)
2762 copy_tv(found, tv);
2763 else if (SCHEME_VOIDP(obj))
2764 {
2765 tv->v_type = VAR_NUMBER;
2766 tv->vval.v_number = 0;
2767 }
2768 else if (SCHEME_INTP(obj))
2769 {
2770 tv->v_type = VAR_NUMBER;
2771 tv->vval.v_number = SCHEME_INT_VAL(obj);
2772 }
2773 else if (SCHEME_BOOLP(obj))
2774 {
2775 tv->v_type = VAR_NUMBER;
2776 tv->vval.v_number = SCHEME_TRUEP(obj);
2777 }
2778# ifdef FEAT_FLOAT
2779 else if (SCHEME_DBLP(obj))
2780 {
2781 tv->v_type = VAR_FLOAT;
2782 tv->vval.v_float = SCHEME_DBL_VAL(obj);
2783 }
2784# endif
2785 else if (SCHEME_STRINGP(obj))
2786 {
2787 tv->v_type = VAR_STRING;
2788 tv->vval.v_string = vim_strsave((char_u *)SCHEME_STR_VAL(obj));
2789 }
2790 else if (SCHEME_VECTORP(obj) || SCHEME_NULLP(obj)
2791 || SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
2792 {
2793 list_T *list = list_alloc();
2794 if (list == NULL)
2795 status = FAIL;
2796 else
2797 {
2798 int i;
2799 Scheme_Object *curr = NULL;
2800 Scheme_Object *cval = NULL;
2801 /* temporary var to hold current element of vectors and pairs */
2802 typval_T *v;
2803
2804 MZ_GC_DECL_REG(2);
2805 MZ_GC_VAR_IN_REG(0, curr);
2806 MZ_GC_VAR_IN_REG(1, cval);
2807 MZ_GC_REG();
2808
2809 tv->v_type = VAR_LIST;
2810 tv->vval.v_list = list;
2811 ++list->lv_refcount;
2812
2813 v = (typval_T *)alloc(sizeof(typval_T));
2814 if (v == NULL)
2815 status = FAIL;
2816 else
2817 {
2818 /* add the value in advance to allow handling of self-referencial
2819 * data structures */
2820 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
2821 copy_tv(tv, visited_tv);
2822 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
2823
2824 if (SCHEME_VECTORP(obj))
2825 {
2826 for (i = 0; i < SCHEME_VEC_SIZE(obj); ++i)
2827 {
2828 cval = SCHEME_VEC_ELS(obj)[i];
2829 status = mzscheme_to_vim(cval, v, depth + 1, visited);
2830 if (status == FAIL)
2831 break;
2832 status = list_append_tv(list, v);
2833 clear_tv(v);
2834 if (status == FAIL)
2835 break;
2836 }
2837 }
2838 else if (SCHEME_PAIRP(obj) || SCHEME_MUTABLE_PAIRP(obj))
2839 {
2840 for (curr = obj;
2841 SCHEME_PAIRP(curr) || SCHEME_MUTABLE_PAIRP(curr);
2842 curr = SCHEME_CDR(curr))
2843 {
2844 cval = SCHEME_CAR(curr);
2845 status = mzscheme_to_vim(cval, v, depth + 1, visited);
2846 if (status == FAIL)
2847 break;
2848 status = list_append_tv(list, v);
2849 clear_tv(v);
2850 if (status == FAIL)
2851 break;
2852 }
2853 /* impoper list not terminated with null
2854 * need to handle the last element */
2855 if (status == OK && !SCHEME_NULLP(curr))
2856 {
2857 status = mzscheme_to_vim(cval, v, depth + 1, visited);
2858 if (status == OK)
2859 {
2860 status = list_append_tv(list, v);
2861 clear_tv(v);
2862 }
2863 }
2864 }
2865 /* nothing to do for scheme_null */
2866 vim_free(v);
2867 }
2868 MZ_GC_UNREG();
2869 }
2870 }
2871 else if (SCHEME_HASHTP(obj))
2872 {
2873 int i;
2874 dict_T *dict;
2875 Scheme_Object *key = NULL;
2876 Scheme_Object *val = NULL;
2877
2878 MZ_GC_DECL_REG(2);
2879 MZ_GC_VAR_IN_REG(0, key);
2880 MZ_GC_VAR_IN_REG(1, val);
2881 MZ_GC_REG();
2882
2883 dict = dict_alloc();
2884 if (dict == NULL)
2885 status = FAIL;
2886 else
2887 {
2888 typval_T *visited_tv = (typval_T *)alloc(sizeof(typval_T));
2889
2890 tv->v_type = VAR_DICT;
2891 tv->vval.v_dict = dict;
2892 ++dict->dv_refcount;
2893
2894 copy_tv(tv, visited_tv);
2895 scheme_hash_set(visited, obj, (Scheme_Object *)visited_tv);
2896
2897 for (i = 0; i < ((Scheme_Hash_Table *)obj)->size; ++i)
2898 {
2899 if (((Scheme_Hash_Table *) obj)->vals[i] != NULL)
2900 {
2901 /* generate item for `diplay'ed Scheme key */
2902 dictitem_T *item = dictitem_alloc((char_u *)string_to_line(
2903 ((Scheme_Hash_Table *) obj)->keys[i]));
2904 /* convert Scheme val to Vim and add it to the dict */
2905 if (mzscheme_to_vim(((Scheme_Hash_Table *) obj)->vals[i],
2906 &item->di_tv, depth + 1, visited) == FAIL
2907 || dict_add(dict, item) == FAIL)
2908 {
2909 dictitem_free(item);
2910 status = FAIL;
2911 break;
2912 }
2913 }
2914
2915 }
2916 }
2917 MZ_GC_UNREG();
2918 }
2919 else
2920 {
2921 /* `display' any other value to string */
2922 tv->v_type = VAR_STRING;
2923 tv->vval.v_string = (char_u *)string_to_line(obj);
2924 }
2925 return status;
2926}
2927
2928 void
2929do_mzeval(char_u *str, typval_T *rettv)
2930{
2931 int i;
2932 Scheme_Object *ret = NULL;
2933 Scheme_Hash_Table *visited = NULL;
2934
2935 MZ_GC_DECL_REG(2);
2936 MZ_GC_VAR_IN_REG(0, ret);
2937 MZ_GC_VAR_IN_REG(0, visited);
2938 MZ_GC_REG();
2939
2940 if (mzscheme_init())
2941 {
2942 MZ_GC_UNREG();
2943 return;
2944 }
2945
2946 MZ_GC_CHECK();
2947 visited = scheme_make_hash_table(SCHEME_hash_ptr);
2948 MZ_GC_CHECK();
2949
2950 if (eval_with_exn_handling(str, do_eval, &ret) == OK)
2951 mzscheme_to_vim(ret, rettv, 1, visited);
2952
2953 for (i = 0; i < visited->size; ++i)
2954 {
2955 /* free up remembered objects */
2956 if (visited->vals[i] != NULL)
2957 {
2958 free_tv((typval_T *)visited->vals[i]);
2959 }
2960 }
2961
2962 MZ_GC_UNREG();
2963}
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002964#endif
2965
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002966/*
2967 * Check to see whether a Vim error has been reported, or a keyboard
2968 * interrupt (from vim --> got_int) has been detected.
2969 */
2970 static int
2971vim_error_check(void)
2972{
2973 return (got_int || did_emsg);
2974}
2975
2976/*
2977 * register Scheme exn:vim
2978 */
2979 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002980register_vim_exn(void)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002981{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002982 int nc = 0;
2983 int i;
2984 Scheme_Object *struct_exn = NULL;
2985 Scheme_Object *exn_name = NULL;
2986
2987 MZ_GC_DECL_REG(2);
2988 MZ_GC_VAR_IN_REG(0, struct_exn);
2989 MZ_GC_VAR_IN_REG(1, exn_name);
2990 MZ_GC_REG();
2991
2992 exn_name = scheme_intern_symbol("exn:vim");
2993 MZ_GC_CHECK();
2994 struct_exn = scheme_builtin_value("struct:exn");
2995 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002996
2997 if (vim_exn == NULL)
2998 vim_exn = scheme_make_struct_type(exn_name,
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00002999 struct_exn, NULL, 0, 0, NULL, NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003000#if MZSCHEME_VERSION_MAJOR >= 299
3001 , NULL
3002#endif
3003 );
3004
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003005
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003006 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003007 Scheme_Object **tmp = NULL;
3008 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
3009 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
3010 MZ_GC_DECL_REG(6);
3011 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
3012 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
3013 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003014
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003015 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
3016 assert(nc <= 5);
3017 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
3018 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003019
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003020 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
3021 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
3022 MZ_GC_CHECK();
3023
3024 for (i = 0; i < nc; i++)
3025 {
3026 scheme_add_global_symbol(exn_names[i],
3027 exn_values[i], environment);
3028 MZ_GC_CHECK();
3029 }
3030 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003031 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003032 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003033}
3034
3035/*
3036 * raise exn:vim, may be with additional info string
3037 */
3038 void
3039raise_vim_exn(const char *add_info)
3040{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003041 char *fmt = _("Vim error: ~a");
3042 Scheme_Object *argv[2] = {NULL, NULL};
3043 Scheme_Object *exn = NULL;
3044
3045 MZ_GC_DECL_REG(4);
3046 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
3047 MZ_GC_VAR_IN_REG(3, exn);
3048 MZ_GC_REG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003049
3050 if (add_info != NULL)
3051 {
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003052 char *c_string = NULL;
3053 Scheme_Object *byte_string = NULL;
3054 Scheme_Object *info = NULL;
3055
3056 MZ_GC_DECL_REG(3);
3057 MZ_GC_VAR_IN_REG(0, c_string);
3058 MZ_GC_VAR_IN_REG(1, byte_string);
3059 MZ_GC_VAR_IN_REG(2, info);
3060 MZ_GC_REG();
3061
3062 info = scheme_make_string(add_info);
3063 MZ_GC_CHECK();
3064 c_string = scheme_format(fmt, STRLEN(fmt), 1, &info, NULL);
3065 MZ_GC_CHECK();
3066 byte_string = scheme_make_string(c_string);
3067 MZ_GC_CHECK();
3068 argv[0] = scheme_byte_string_to_char_string(byte_string);
3069 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003070 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003071 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003072 }
3073 else
3074 argv[0] = scheme_make_string(_("Vim error"));
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003075 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003076
Bram Moolenaar049377e2007-05-12 15:32:12 +00003077#if MZSCHEME_VERSION_MAJOR < 360
3078 argv[1] = scheme_current_continuation_marks();
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003079 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003080#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003081 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003082 MZ_GC_CHECK();
Bram Moolenaar049377e2007-05-12 15:32:12 +00003083#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003084
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003085 exn = scheme_make_struct_instance(vim_exn, 2, argv);
3086 MZ_GC_CHECK();
3087 scheme_raise(exn);
3088 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003089}
3090
3091 void
3092raise_if_error(void)
3093{
3094 if (vim_error_check())
3095 raise_vim_exn(NULL);
3096}
3097
3098/* get buffer:
3099 * either current
3100 * or passed as argv[argnum] with checks
3101 */
3102 static vim_mz_buffer *
3103get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3104{
3105 vim_mz_buffer *b;
3106
3107 if (argc < argnum + 1)
3108 return get_vim_curr_buffer();
3109 if (!SCHEME_VIMBUFFERP(argv[argnum]))
3110 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
3111 b = (vim_mz_buffer *)argv[argnum];
3112 (void)get_valid_buffer(argv[argnum]);
3113 return b;
3114}
3115
3116/* get window:
3117 * either current
3118 * or passed as argv[argnum] with checks
3119 */
3120 static vim_mz_window *
3121get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
3122{
3123 vim_mz_window *w;
3124
3125 if (argc < argnum + 1)
3126 return get_vim_curr_window();
3127 w = (vim_mz_window *)argv[argnum];
3128 if (!SCHEME_VIMWINDOWP(argv[argnum]))
3129 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
3130 (void)get_valid_window(argv[argnum]);
3131 return w;
3132}
3133
3134/* get valid Vim buffer from Scheme_Object* */
3135buf_T *get_valid_buffer(void *obj)
3136{
3137 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
3138
3139 if (buf == INVALID_BUFFER_VALUE)
3140 scheme_signal_error(_("buffer is invalid"));
3141 return buf;
3142}
3143
3144/* get valid Vim window from Scheme_Object* */
3145win_T *get_valid_window(void *obj)
3146{
3147 win_T *win = ((vim_mz_window *)obj)->win;
3148 if (win == INVALID_WINDOW_VALUE)
3149 scheme_signal_error(_("window is invalid"));
3150 return win;
3151}
3152
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003153 int
3154mzthreads_allowed(void)
3155{
3156 return mz_threads_allow;
3157}
3158
3159 static int
3160line_in_range(linenr_T lnum, buf_T *buf)
3161{
3162 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
3163}
3164
3165 static void
3166check_line_range(linenr_T lnum, buf_T *buf)
3167{
3168 if (!line_in_range(lnum, buf))
3169 scheme_signal_error(_("linenr out of range"));
3170}
3171
3172/*
3173 * Check if deleting lines made the cursor position invalid
3174 * (or you'll get msg from Vim about invalid linenr).
3175 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3176 * deleted). Got from if_python.c
3177 */
3178 static void
3179mz_fix_cursor(int lo, int hi, int extra)
3180{
3181 if (curwin->w_cursor.lnum >= lo)
3182 {
3183 /* Adjust the cursor position if it's in/after the changed
3184 * lines. */
3185 if (curwin->w_cursor.lnum >= hi)
3186 {
3187 curwin->w_cursor.lnum += extra;
3188 check_cursor_col();
3189 }
3190 else if (extra < 0)
3191 {
3192 curwin->w_cursor.lnum = lo;
3193 check_cursor();
3194 }
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003195 else
3196 check_cursor_col();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003197 changed_cline_bef_curs();
3198 }
3199 invalidate_botline();
3200}
3201
3202static Vim_Prim prims[]=
3203{
3204 /*
3205 * Buffer-related commands
3206 */
3207 {get_buffer_line, "get-buff-line", 1, 2},
3208 {set_buffer_line, "set-buff-line", 2, 3},
3209 {get_buffer_line_list, "get-buff-line-list", 2, 3},
3210 {get_buffer_name, "get-buff-name", 0, 1},
3211 {get_buffer_num, "get-buff-num", 0, 1},
3212 {get_buffer_size, "get-buff-size", 0, 1},
3213 {set_buffer_line_list, "set-buff-line-list", 3, 4},
3214 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
3215 {get_curr_buffer, "curr-buff", 0, 0},
3216 {get_buffer_count, "buff-count", 0, 0},
3217 {get_next_buffer, "get-next-buff", 0, 1},
3218 {get_prev_buffer, "get-prev-buff", 0, 1},
3219 {mzscheme_open_buffer, "open-buff", 1, 1},
3220 {get_buffer_by_name, "get-buff-by-name", 1, 1},
3221 {get_buffer_by_num, "get-buff-by-num", 1, 1},
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003222 /*
3223 * Window-related commands
3224 */
3225 {get_curr_win, "curr-win", 0, 0},
3226 {get_window_count, "win-count", 0, 0},
3227 {get_window_by_num, "get-win-by-num", 1, 1},
3228 {get_window_num, "get-win-num", 0, 1},
3229 {get_window_buffer, "get-win-buffer", 0, 1},
3230 {get_window_height, "get-win-height", 0, 1},
3231 {set_window_height, "set-win-height", 1, 2},
3232#ifdef FEAT_VERTSPLIT
3233 {get_window_width, "get-win-width", 0, 1},
3234 {set_window_width, "set-win-width", 1, 2},
3235#endif
3236 {get_cursor, "get-cursor", 0, 1},
3237 {set_cursor, "set-cursor", 1, 2},
3238 {get_window_list, "get-win-list", 0, 1},
3239 /*
3240 * Vim-related commands
3241 */
3242 {vim_command, "command", 1, 1},
3243 {vim_eval, "eval", 1, 1},
3244 {get_range_start, "range-start", 0, 0},
3245 {get_range_end, "range-end", 0, 0},
3246 {mzscheme_beep, "beep", 0, 0},
3247 {get_option, "get-option", 1, 2},
3248 {set_option, "set-option", 1, 2},
3249 /*
3250 * small utilities
3251 */
3252 {vim_bufferp, "buff?", 1, 1},
3253 {vim_windowp, "win?", 1, 1},
3254 {vim_buffer_validp, "buff-valid?", 1, 1},
3255 {vim_window_validp, "win-valid?", 1, 1}
3256};
3257
3258/* return MzScheme wrapper for curbuf */
3259 static vim_mz_buffer *
3260get_vim_curr_buffer(void)
3261{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003262 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003263 return (vim_mz_buffer *)buffer_new(curbuf);
3264 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00003265 return (vim_mz_buffer *)curbuf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003266}
3267
3268/* return MzScheme wrapper for curwin */
3269 static vim_mz_window *
3270get_vim_curr_window(void)
3271{
Bram Moolenaare344bea2005-09-01 20:46:49 +00003272 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003273 return (vim_mz_window *)window_new(curwin);
3274 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00003275 return (vim_mz_window *)curwin->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003276}
3277
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003278 static void
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003279make_modules()
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003280{
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003281 int i;
3282 Scheme_Env *mod = NULL;
3283 Scheme_Object *vimext_symbol = NULL;
3284 Scheme_Object *closed_prim = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003285
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003286 MZ_GC_DECL_REG(3);
3287 MZ_GC_VAR_IN_REG(0, mod);
3288 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3289 MZ_GC_VAR_IN_REG(2, closed_prim);
3290 MZ_GC_REG();
3291
3292 vimext_symbol = scheme_intern_symbol("vimext");
3293 MZ_GC_CHECK();
3294 mod = scheme_primitive_module(vimext_symbol, environment);
3295 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003296 /* all prims made closed so they can access their own names */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003297 for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003298 {
3299 Vim_Prim *prim = prims + i;
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003300 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3301 prim->mina, prim->maxa);
3302 scheme_add_global(prim->name, closed_prim, mod);
3303 MZ_GC_CHECK();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003304 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003305 scheme_finish_primitive_module(mod);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003306 MZ_GC_CHECK();
3307 MZ_GC_UNREG();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003308}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003309
Bram Moolenaar555b2802005-05-19 21:08:39 +00003310#ifdef HAVE_SANDBOX
3311static Scheme_Object *M_write = NULL;
3312static Scheme_Object *M_read = NULL;
3313static Scheme_Object *M_execute = NULL;
3314static Scheme_Object *M_delete = NULL;
3315
3316 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00003317sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003318{
3319 if (sandbox)
3320 raise_vim_exn(_("not allowed in the Vim sandbox"));
3321}
3322
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003323/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00003324 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003325sandbox_file_guard(int argc UNUSED, Scheme_Object **argv)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003326{
3327 if (sandbox)
3328 {
3329 Scheme_Object *requested_access = argv[2];
3330
3331 if (M_write == NULL)
3332 {
3333 MZ_REGISTER_STATIC(M_write);
3334 M_write = scheme_intern_symbol("write");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003335 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003336 }
3337 if (M_read == NULL)
3338 {
3339 MZ_REGISTER_STATIC(M_read);
3340 M_read = scheme_intern_symbol("read");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003341 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003342 }
3343 if (M_execute == NULL)
3344 {
3345 MZ_REGISTER_STATIC(M_execute);
3346 M_execute = scheme_intern_symbol("execute");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003347 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003348 }
3349 if (M_delete == NULL)
3350 {
3351 MZ_REGISTER_STATIC(M_delete);
3352 M_delete = scheme_intern_symbol("delete");
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00003353 MZ_GC_CHECK();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003354 }
3355
3356 while (!SCHEME_NULLP(requested_access))
3357 {
3358 Scheme_Object *item = SCHEME_CAR(requested_access);
3359 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3360 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
3361 {
3362 raise_vim_exn(_("not allowed in the Vim sandbox"));
3363 }
3364 requested_access = SCHEME_CDR(requested_access);
3365 }
3366 }
3367 return scheme_void;
3368}
3369
3370 static Scheme_Object *
Bram Moolenaar64404472010-06-26 06:24:45 +02003371sandbox_network_guard(int argc UNUSED, Scheme_Object **argv UNUSED)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003372{
3373 return scheme_void;
3374}
3375#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00003376
3377#endif