blob: 4874851353f925e91a72245c3ebcb43f5fa93bc6 [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 *
7 * This consists of six parts:
8 * 1. MzScheme interpreter main program
9 * 2. Routines that handle the external interface between MzScheme and
10 * Vim.
11 * 3. MzScheme input/output handlers: writes output via [e]msg().
12 * 4. Implementation of the Vim Features for MzScheme
13 * 5. Vim Window-related Manipulation Functions.
14 * 6. Vim Buffer-related Manipulation Functions
15 *
16 * NOTES
17 * 1. Memory, allocated with scheme_malloc*, need not to be freed explicitly,
18 * garbage collector will do it self
19 * 2. Requires at least NORMAL features. I can't imagine why one may want
20 * to build with SMALL or TINY features but with MzScheme interface.
21 * 3. I don't use K&R-style functions. Anyway, MzScheme headers are ANSI.
22 */
23
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024#include "vim.h"
25#include "if_mzsch.h"
26
Bram Moolenaar76b92b22006-03-24 22:46:53 +000027/* Only do the following when the feature is enabled. Needed for "make
28 * depend". */
29#if defined(FEAT_MZSCHEME) || defined(PROTO)
30
Bram Moolenaar325b7a22004-07-05 15:58:32 +000031/* Base data structures */
32#define SCHEME_VIMBUFFERP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_buffer_type)
33#define SCHEME_VIMWINDOWP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_window_type)
34
35typedef struct
36{
37 Scheme_Type tag;
38 Scheme_Env *env;
39 buf_T *buf;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000040} vim_mz_buffer;
41
42#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
43
44typedef struct
45{
46 Scheme_Type tag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000047 win_T *win;
Bram Moolenaar325b7a22004-07-05 15:58:32 +000048} vim_mz_window;
49
50#define INVALID_WINDOW_VALUE ((win_T *)(-1))
51
52/*
53 * Prims that form MzScheme Vim interface
54 */
55typedef struct
56{
57 Scheme_Closed_Prim *prim;
58 char *name;
59 int mina; /* arity information */
60 int maxa;
61} Vim_Prim;
62
63typedef struct
64{
65 char *name;
66 Scheme_Object *port;
67} Port_Info;
68
69/* info for closed prim */
70/*
71 * data have different means:
72 * for do_eval it is char*
73 * for do_apply is Apply_Onfo*
74 * for do_load is Port_Info*
75 */
76typedef struct
77{
78 void *data;
79 Scheme_Env *env;
80} Cmd_Info;
81
82/* info for do_apply */
83typedef struct
84{
85 Scheme_Object *proc;
86 int argc;
87 Scheme_Object **argv;
88} Apply_Info;
89
90/*
91 *========================================================================
92 * Vim-Control Commands
93 *========================================================================
94 */
95/*
96 *========================================================================
97 * Utility functions for the vim/mzscheme interface
98 *========================================================================
99 */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000100#ifdef HAVE_SANDBOX
101static Scheme_Object *sandbox_file_guard(int, Scheme_Object **);
102static Scheme_Object *sandbox_network_guard(int, Scheme_Object **);
103static void sandbox_check();
104#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000105/* Buffer-related commands */
106static Scheme_Object *buffer_new(buf_T *buf);
107static Scheme_Object *get_buffer_by_name(void *, int, Scheme_Object **);
108static Scheme_Object *get_buffer_by_num(void *, int, Scheme_Object **);
109static Scheme_Object *get_buffer_count(void *, int, Scheme_Object **);
110static Scheme_Object *get_buffer_line(void *, int, Scheme_Object **);
111static Scheme_Object *get_buffer_line_list(void *, int, Scheme_Object **);
112static Scheme_Object *get_buffer_name(void *, int, Scheme_Object **);
113static Scheme_Object *get_buffer_num(void *, int, Scheme_Object **);
114static Scheme_Object *get_buffer_size(void *, int, Scheme_Object **);
115static Scheme_Object *get_curr_buffer(void *, int, Scheme_Object **);
116static Scheme_Object *get_next_buffer(void *, int, Scheme_Object **);
117static Scheme_Object *get_prev_buffer(void *, int, Scheme_Object **);
118static Scheme_Object *mzscheme_open_buffer(void *, int, Scheme_Object **);
119static Scheme_Object *set_buffer_line(void *, int, Scheme_Object **);
120static Scheme_Object *set_buffer_line_list(void *, int, Scheme_Object **);
121static Scheme_Object *insert_buffer_line_list(void *, int, Scheme_Object **);
122static Scheme_Object *get_range_start(void *, int, Scheme_Object **);
123static Scheme_Object *get_range_end(void *, int, Scheme_Object **);
124static Scheme_Object *get_buffer_namespace(void *, int, Scheme_Object **);
125static vim_mz_buffer *get_vim_curr_buffer(void);
126
127/* Window-related commands */
128static Scheme_Object *window_new(win_T *win);
129static Scheme_Object *get_curr_win(void *, int, Scheme_Object **);
130static Scheme_Object *get_window_count(void *, int, Scheme_Object **);
131static Scheme_Object *get_window_by_num(void *, int, Scheme_Object **);
132static Scheme_Object *get_window_num(void *, int, Scheme_Object **);
133static Scheme_Object *get_window_buffer(void *, int, Scheme_Object **);
134static Scheme_Object *get_window_height(void *, int, Scheme_Object **);
135static Scheme_Object *set_window_height(void *, int, Scheme_Object **);
136#ifdef FEAT_VERTSPLIT
137static Scheme_Object *get_window_width(void *, int, Scheme_Object **);
138static Scheme_Object *set_window_width(void *, int, Scheme_Object **);
139#endif
140static Scheme_Object *get_cursor(void *, int, Scheme_Object **);
141static Scheme_Object *set_cursor(void *, int, Scheme_Object **);
142static Scheme_Object *get_window_list(void *, int, Scheme_Object **);
143static vim_mz_window *get_vim_curr_window(void);
144
145/* Vim-related commands */
146static Scheme_Object *mzscheme_beep(void *, int, Scheme_Object **);
147static Scheme_Object *get_option(void *, int, Scheme_Object **);
148static Scheme_Object *set_option(void *, int, Scheme_Object **);
149static Scheme_Object *vim_command(void *, int, Scheme_Object **);
150static Scheme_Object *vim_eval(void *, int, Scheme_Object **);
151static Scheme_Object *vim_bufferp(void *data, int, Scheme_Object **);
152static Scheme_Object *vim_windowp(void *data, int, Scheme_Object **);
153static Scheme_Object *vim_buffer_validp(void *data, int, Scheme_Object **);
154static Scheme_Object *vim_window_validp(void *data, int, Scheme_Object **);
155
156/*
157 *========================================================================
158 * Internal Function Prototypes
159 *========================================================================
160 */
161static int vim_error_check(void);
162static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
163static void startup_mzscheme(void);
164static char *string_to_line(Scheme_Object *obj);
165static int mzscheme_io_init(void);
166static void mzscheme_interface_init(vim_mz_buffer *self);
167static void do_output(char *mesg, long len);
168static void do_printf(char *format, ...);
169static void do_flush(void);
170static Scheme_Object *_apply_thunk_catch_exceptions(
171 Scheme_Object *, Scheme_Object **);
172static Scheme_Object *extract_exn_message(Scheme_Object *v);
173static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
174static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
175static Scheme_Object *do_apply(void *, int noargc, Scheme_Object **noargv);
176static void register_vim_exn(Scheme_Env *env);
177static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
178 int argc, Scheme_Object **argv);
179static vim_mz_window *get_window_arg(const char *fname, int argnum,
180 int argc, Scheme_Object **argv);
181static void add_vim_exn(Scheme_Env *env);
182static int line_in_range(linenr_T, buf_T *);
183static void check_line_range(linenr_T, buf_T *);
184static void mz_fix_cursor(int lo, int hi, int extra);
185
186static int eval_in_namespace(void *, Scheme_Closed_Prim *, Scheme_Env *,
187 Scheme_Object **ret);
188static void make_modules(Scheme_Env *);
189
Bram Moolenaar33570922005-01-25 22:26:29 +0000190#ifdef DYNAMIC_MZSCHEME
191
192static Scheme_Object *dll_scheme_eof;
193static Scheme_Object *dll_scheme_false;
194static Scheme_Object *dll_scheme_void;
195static Scheme_Object *dll_scheme_null;
196static Scheme_Object *dll_scheme_true;
197
198static Scheme_Thread **dll_scheme_current_thread_ptr;
199
200static void (**dll_scheme_console_printf_ptr)(char *str, ...);
201static void (**dll_scheme_console_output_ptr)(char *str, long len);
202static void (**dll_scheme_notify_multithread_ptr)(int on);
203
204static void *(*dll_GC_malloc)(size_t size_in_bytes);
205static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
206static Scheme_Env *(*dll_scheme_basic_env)(void);
207static void (*dll_scheme_check_threads)(void);
208static void (*dll_scheme_register_static)(void *ptr, long size);
209static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
210static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
211 Scheme_Env *env);
212static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
213 Scheme_Object *val, Scheme_Env *env);
214static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
215 Scheme_Object **rands);
216static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000217# if MZSCHEME_VERSION_MAJOR >= 299
218static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
219# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000220static void (*dll_scheme_close_input_port)(Scheme_Object *port);
221static void (*dll_scheme_count_lines)(Scheme_Object *port);
222static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
223static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
224static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, long *len);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000225static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
Bram Moolenaar33570922005-01-25 22:26:29 +0000226static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
227 int _num_rands, Scheme_Object **rands, int val);
228static void (*dll_scheme_dont_gc_ptr)(void *p);
229static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
230static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
231 Scheme_Env *env);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000232static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
Bram Moolenaar33570922005-01-25 22:26:29 +0000233 Scheme_Env *env, int all);
234static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000235# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000236static char *(*dll_scheme_format)(char *format, int flen, int argc,
237 Scheme_Object **argv, long *rlen);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000238# else
239static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
240 Scheme_Object **argv, long *rlen);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000241static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000242# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000243static void (*dll_scheme_gc_ptr_ok)(void *p);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000244# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000245static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
246 long *len);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000247# else
248static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
249 long *len);
250# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000251static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
252static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
253 Scheme_Env *env);
254static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
255 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
256 mzshort maxa);
257static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
258static Scheme_Object *(*dll_scheme_make_namespace)(int argc,
259 Scheme_Object *argv[]);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000260static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
Bram Moolenaar33570922005-01-25 22:26:29 +0000261 Scheme_Object *cdr);
Bram Moolenaar555b2802005-05-19 21:08:39 +0000262static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
263 const char *name, mzshort mina, mzshort maxa);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000264# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000265static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
266static Scheme_Object *(*dll_scheme_make_string_output_port)();
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000267# else
268static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
269static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
270# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000271static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
272 int argc, Scheme_Object **argv);
273static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
274 Scheme_Object *field_names, int flags, int *count_out);
275static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
276 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
277 int num_uninit_fields, Scheme_Object *uninit_val,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000278 Scheme_Object *properties
279# if MZSCHEME_VERSION_MAJOR >= 299
280 , Scheme_Object *guard
281# endif
282 );
Bram Moolenaar33570922005-01-25 22:26:29 +0000283static Scheme_Object **(*dll_scheme_make_struct_values)(
284 Scheme_Object *struct_type, Scheme_Object **names, int count,
285 int flags);
286static Scheme_Type (*dll_scheme_make_type)(const char *name);
287static Scheme_Object *(*dll_scheme_make_vector)(int size,
288 Scheme_Object *fill);
289static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
290static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
291 const char *who);
292static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
293 Scheme_Env *for_env);
294static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
295static void (*dll_scheme_raise)(Scheme_Object *exn);
296static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
297static void (*dll_scheme_signal_error)(const char *msg, ...);
298static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
299 int which, int argc, Scheme_Object **argv);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000300# if MZSCHEME_VERSION_MAJOR >= 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000301static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000302 Scheme_Object *o);
303static Scheme_Config *(*dll_scheme_current_config)(void);
304static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
305 (Scheme_Object *s);
306# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000307
308/* arrays are imported directly */
309# define scheme_eof dll_scheme_eof
310# define scheme_false dll_scheme_false
311# define scheme_void dll_scheme_void
312# define scheme_null dll_scheme_null
313# define scheme_true dll_scheme_true
314
315/* pointers are GetProceAddress'ed as pointers to pointer */
316# define scheme_current_thread (*dll_scheme_current_thread_ptr)
317# define scheme_console_printf (*dll_scheme_console_printf_ptr)
318# define scheme_console_output (*dll_scheme_console_output_ptr)
319# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
320
321/* and functions in a usual way */
322# define GC_malloc dll_GC_malloc
323# define GC_malloc_atomic dll_GC_malloc_atomic
324
325# define scheme_add_global dll_scheme_add_global
326# define scheme_add_global_symbol dll_scheme_add_global_symbol
327# define scheme_apply dll_scheme_apply
328# define scheme_basic_env dll_scheme_basic_env
329# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000330# if MZSCHEME_VERSION_MAJOR >= 299
331# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
332# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000333# define scheme_check_threads dll_scheme_check_threads
334# define scheme_close_input_port dll_scheme_close_input_port
335# define scheme_count_lines dll_scheme_count_lines
336# define scheme_current_continuation_marks \
337 dll_scheme_current_continuation_marks
338# define scheme_display dll_scheme_display
339# define scheme_display_to_string dll_scheme_display_to_string
340# define scheme_do_eval dll_scheme_do_eval
341# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000342# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000343# define scheme_eval dll_scheme_eval
344# define scheme_eval_string dll_scheme_eval_string
345# define scheme_eval_string_all dll_scheme_eval_string_all
346# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000347# if MZSCHEME_VERSION_MAJOR < 299
348# define scheme_format dll_scheme_format
349# else
350# define scheme_format_utf8 dll_scheme_format_utf8
351# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000352# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000353# if MZSCHEME_VERSION_MAJOR < 299
354# define scheme_get_sized_string_output dll_scheme_get_sized_string_output
355# else
356# define scheme_get_sized_byte_string_output \
357 dll_scheme_get_sized_byte_string_output
Bram Moolenaar555b2802005-05-19 21:08:39 +0000358# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000359# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000360# define scheme_intern_symbol dll_scheme_intern_symbol
361# define scheme_lookup_global dll_scheme_lookup_global
362# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
363# define scheme_make_integer_value dll_scheme_make_integer_value
364# define scheme_make_namespace dll_scheme_make_namespace
365# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000366# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000367# if MZSCHEME_VERSION_MAJOR < 299
368# define scheme_make_string dll_scheme_make_string
369# define scheme_make_string_output_port dll_scheme_make_string_output_port
370# else
371# define scheme_make_byte_string dll_scheme_make_byte_string
372# define scheme_make_byte_string_output_port \
373 dll_scheme_make_byte_string_output_port
374# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000375# define scheme_make_struct_instance dll_scheme_make_struct_instance
376# define scheme_make_struct_names dll_scheme_make_struct_names
377# define scheme_make_struct_type dll_scheme_make_struct_type
378# define scheme_make_struct_values dll_scheme_make_struct_values
379# define scheme_make_type dll_scheme_make_type
380# define scheme_make_vector dll_scheme_make_vector
381# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
382# define scheme_open_input_file dll_scheme_open_input_file
383# define scheme_primitive_module dll_scheme_primitive_module
384# define scheme_proper_list_length dll_scheme_proper_list_length
385# define scheme_raise dll_scheme_raise
386# define scheme_read dll_scheme_read
387# define scheme_register_static dll_scheme_register_static
388# define scheme_set_stack_base dll_scheme_set_stack_base
389# define scheme_signal_error dll_scheme_signal_error
390# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000391# if MZSCHEME_VERSION_MAJOR >= 299
392# define scheme_set_param dll_scheme_set_param
393# define scheme_current_config dll_scheme_current_config
394# define scheme_char_string_to_byte_string \
395 dll_scheme_char_string_to_byte_string
396# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000397
398typedef struct
399{
400 char *name;
401 void **ptr;
402} Thunk_Info;
403
404static Thunk_Info mzgc_imports[] = {
405 {"GC_malloc", (void **)&dll_GC_malloc},
406 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
407 {NULL, NULL}};
408
409static Thunk_Info mzsch_imports[] = {
410 {"scheme_eof", (void **)&dll_scheme_eof},
411 {"scheme_false", (void **)&dll_scheme_false},
412 {"scheme_void", (void **)&dll_scheme_void},
413 {"scheme_null", (void **)&dll_scheme_null},
414 {"scheme_true", (void **)&dll_scheme_true},
415 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
416 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
417 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000418 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000419 (void **)&dll_scheme_notify_multithread_ptr},
420 {"scheme_add_global", (void **)&dll_scheme_add_global},
421 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
422 {"scheme_apply", (void **)&dll_scheme_apply},
423 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000424# if MZSCHEME_VERSION_MAJOR >= 299
425 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
426# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000427 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
428 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
429 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
430 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000431 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000432 (void **)&dll_scheme_current_continuation_marks},
433 {"scheme_display", (void **)&dll_scheme_display},
434 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
435 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
436 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000437 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000438 {"scheme_eval", (void **)&dll_scheme_eval},
439 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
440 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000441 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000442 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000443# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000444 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000445# else
446 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000447 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000450# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000451 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000452 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000453# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000454 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000455 (void **)&dll_scheme_get_sized_byte_string_output},
456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
458 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000459 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000460 (void **)&dll_scheme_make_closed_prim_w_arity},
461 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
462 {"scheme_make_namespace", (void **)&dll_scheme_make_namespace},
463 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000464 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000465# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000466 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000467 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000468 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000469# else
470 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000471 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000472 (void **)&dll_scheme_make_byte_string_output_port},
473# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000474 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000475 (void **)&dll_scheme_make_struct_instance},
476 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
477 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
478 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
479 {"scheme_make_type", (void **)&dll_scheme_make_type},
480 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
481 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
482 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
483 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
484 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
485 {"scheme_raise", (void **)&dll_scheme_raise},
486 {"scheme_read", (void **)&dll_scheme_read},
487 {"scheme_register_static", (void **)&dll_scheme_register_static},
488 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
489 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
490 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000491# if MZSCHEME_VERSION_MAJOR >= 299
492 {"scheme_set_param", (void **)&dll_scheme_set_param},
493 {"scheme_current_config", (void **)&dll_scheme_current_config},
494 {"scheme_char_string_to_byte_string",
495 (void **)&dll_scheme_char_string_to_byte_string},
496# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000497 {NULL, NULL}};
498
499static HINSTANCE hMzGC = 0;
500static HINSTANCE hMzSch = 0;
501
502static void dynamic_mzscheme_end(void);
503static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
504 int verbose);
505
506 static int
507mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
508{
509 Thunk_Info *thunk = NULL;
510
511 if (hMzGC && hMzSch)
512 return OK;
513 hMzSch = LoadLibrary(sch_dll);
514 hMzGC = LoadLibrary(gc_dll);
515
516 if (!hMzSch)
517 {
518 if (verbose)
519 EMSG2(_(e_loadlib), sch_dll);
520 return FAIL;
521 }
522
523 if (!hMzGC)
524 {
525 if (verbose)
526 EMSG2(_(e_loadlib), gc_dll);
527 return FAIL;
528 }
529
530 for (thunk = mzsch_imports; thunk->name; thunk++)
531 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000532 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000533 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
534 {
535 FreeLibrary(hMzSch);
536 hMzSch = 0;
537 FreeLibrary(hMzGC);
538 hMzGC = 0;
539 if (verbose)
540 EMSG2(_(e_loadfunc), thunk->name);
541 return FAIL;
542 }
543 }
544 for (thunk = mzgc_imports; thunk->name; thunk++)
545 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000546 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000547 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
548 {
549 FreeLibrary(hMzSch);
550 hMzSch = 0;
551 FreeLibrary(hMzGC);
552 hMzGC = 0;
553 if (verbose)
554 EMSG2(_(e_loadfunc), thunk->name);
555 return FAIL;
556 }
557 }
558 return OK;
559}
560
561 int
562mzscheme_enabled(int verbose)
563{
564 return mzscheme_runtime_link_init(
565 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
566}
567
568 static void
569dynamic_mzscheme_end(void)
570{
571 if (hMzSch)
572 {
573 FreeLibrary(hMzSch);
574 hMzSch = 0;
575 }
576 if (hMzGC)
577 {
578 FreeLibrary(hMzGC);
579 hMzGC = 0;
580 }
581}
582#endif /* DYNAMIC_MZSCHEME */
583
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000584/*
585 *========================================================================
586 * 1. MzScheme interpreter startup
587 *========================================================================
588 */
589
590static Scheme_Type mz_buffer_type;
591static Scheme_Type mz_window_type;
592
593static int initialized = 0;
594
595/* global environment */
596static Scheme_Env *environment = NULL;
597/* output/error handlers */
598static Scheme_Object *curout = NULL;
599static Scheme_Object *curerr = NULL;
600/* vim:exn exception */
601static Scheme_Object *exn_catching_apply = NULL;
602static Scheme_Object *exn_p = NULL;
603static Scheme_Object *exn_message = NULL;
604static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
605 /* values for exn:vim - constructor, predicate, accessors etc */
606static Scheme_Object *vim_exn_names = NULL;
607static Scheme_Object *vim_exn_values = NULL;
608
609static long range_start;
610static long range_end;
611
612/* MzScheme threads scheduling stuff */
613static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000614
615#if defined(FEAT_GUI_W32)
616static void CALLBACK timer_proc(HWND, UINT, UINT, DWORD);
617static UINT timer_id = 0;
618#elif defined(FEAT_GUI_GTK)
619static gint timer_proc(gpointer);
620static guint timer_id = 0;
621#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
622static void timer_proc(XtPointer, XtIntervalId *);
623static XtIntervalId timer_id = (XtIntervalId)0;
624#elif defined(FEAT_GUI_MAC)
625pascal void timer_proc(EventLoopTimerRef, void *);
626static EventLoopTimerRef timer_id = NULL;
627static EventLoopTimerUPP timerUPP;
628#endif
629
630#ifndef FEAT_GUI_W32 /* Win32 console and Unix */
631 void
632mzvim_check_threads(void)
633{
634 /* Last time MzScheme threads were scheduled */
635 static time_t mz_last_time = 0;
636
637 if (mz_threads_allow && p_mzq > 0)
638 {
639 time_t now = time(NULL);
640
641 if ((now - mz_last_time) * 1000 > p_mzq)
642 {
643 mz_last_time = now;
644 scheme_check_threads();
645 }
646 }
647}
648#endif
649
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000650#ifdef MZSCHEME_GUI_THREADS
651static void setup_timer(void);
652static void remove_timer(void);
653
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000654/* timers are presented in GUI only */
655# if defined(FEAT_GUI_W32)
656 static void CALLBACK
657timer_proc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
658# elif defined(FEAT_GUI_GTK)
659/*ARGSUSED*/
660 static gint
661timer_proc(gpointer data)
662# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
663/* ARGSUSED */
664 static void
665timer_proc(XtPointer timed_out, XtIntervalId *interval_id)
666# elif defined(FEAT_GUI_MAC)
667 pascal void
668timer_proc(EventLoopTimerRef theTimer, void *userData)
669# endif
670{
671 scheme_check_threads();
672# if defined(FEAT_GUI_GTK)
673 return TRUE; /* continue receiving notifications */
674# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
675 /* renew timeout */
676 if (mz_threads_allow && p_mzq > 0)
677 timer_id = XtAppAddTimeOut(app_context, p_mzq,
678 timer_proc, NULL);
679# endif
680}
681
682 static void
683setup_timer(void)
684{
685# if defined(FEAT_GUI_W32)
686 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
687# elif defined(FEAT_GUI_GTK)
688 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
689# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
690 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
691# elif defined(FEAT_GUI_MAC)
692 timerUPP = NewEventLoopTimerUPP(timer_proc);
693 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
694 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
695# endif
696}
697
698 static void
699remove_timer(void)
700{
701# if defined(FEAT_GUI_W32)
702 KillTimer(NULL, timer_id);
703# elif defined(FEAT_GUI_GTK)
704 gtk_timeout_remove(timer_id);
705# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
706 XtRemoveTimeOut(timer_id);
707# elif defined(FEAT_GUI_MAC)
708 RemoveEventLoopTimer(timer_id);
709 DisposeEventLoopTimerUPP(timerUPP);
710# endif
711 timer_id = 0;
712}
713
714 void
715mzvim_reset_timer(void)
716{
717 if (timer_id != 0)
718 remove_timer();
719 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
720 setup_timer();
721}
722
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000723#endif /* MZSCHEME_GUI_THREADS */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000724
725 static void
726notify_multithread(int on)
727{
728 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000729#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000730 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
731 setup_timer();
732 if (!on && timer_id != 0)
733 remove_timer();
734#endif
735}
736
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000737 void
738mzscheme_end(void)
739{
Bram Moolenaar33570922005-01-25 22:26:29 +0000740#ifdef DYNAMIC_MZSCHEME
741 dynamic_mzscheme_end();
742#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000743}
744
745 static void
746startup_mzscheme(void)
747{
Bram Moolenaar555b2802005-05-19 21:08:39 +0000748 Scheme_Object *proc_make_security_guard;
749
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000750 scheme_set_stack_base(NULL, 1);
751
752 MZ_REGISTER_STATIC(environment);
753 MZ_REGISTER_STATIC(curout);
754 MZ_REGISTER_STATIC(curerr);
755 MZ_REGISTER_STATIC(exn_catching_apply);
756 MZ_REGISTER_STATIC(exn_p);
757 MZ_REGISTER_STATIC(exn_message);
758 MZ_REGISTER_STATIC(vim_exn);
759 MZ_REGISTER_STATIC(vim_exn_names);
760 MZ_REGISTER_STATIC(vim_exn_values);
761
762 environment = scheme_basic_env();
763
764 /* redirect output */
765 scheme_console_output = do_output;
766 scheme_console_printf = do_printf;
767
768#ifdef MZSCHEME_COLLECTS
769 /* setup 'current-library-collection-paths' parameter */
770 scheme_set_param(scheme_config, MZCONFIG_COLLECTION_PATHS,
771 scheme_make_pair(scheme_make_string(MZSCHEME_COLLECTS),
772 scheme_null));
773#endif
Bram Moolenaar555b2802005-05-19 21:08:39 +0000774#ifdef HAVE_SANDBOX
775 /* setup sandbox guards */
776 proc_make_security_guard = scheme_lookup_global(
777 scheme_intern_symbol("make-security-guard"),
778 environment);
779 if (proc_make_security_guard != NULL)
780 {
781 Scheme_Object *args[3];
782 Scheme_Object *guard;
783 args[0] = scheme_get_param(scheme_config, MZCONFIG_SECURITY_GUARD);
784 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
785 "sandbox-file-guard", 3, 3);
786 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
787 "sandbox-network-guard", 4, 4);
788 guard = scheme_apply(proc_make_security_guard, 3, args);
789 scheme_set_param(scheme_config, MZCONFIG_SECURITY_GUARD, guard);
790 }
791#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000792 /* Create buffer and window types for use in Scheme code */
793 mz_buffer_type = scheme_make_type("<vim-buffer>");
794 mz_window_type = scheme_make_type("<vim-window>");
795
796 register_vim_exn(environment);
797 make_modules(environment);
798
799 /*
800 * setup callback to receive notifications
801 * whether thread scheduling is (or not) required
802 */
803 scheme_notify_multithread = notify_multithread;
804 initialized = 1;
805}
806
807/*
808 * This routine is called for each new invocation of MzScheme
809 * to make sure things are properly initialized.
810 */
811 static int
812mzscheme_init(void)
813{
814 int do_require = FALSE;
815
816 if (!initialized)
817 {
818 do_require = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000819#ifdef DYNAMIC_MZSCHEME
820 if (!mzscheme_enabled(TRUE))
821 {
822 EMSG(_("???: Sorry, this command is disabled, the MzScheme library could not be loaded."));
823 return -1;
824 }
825#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000826 startup_mzscheme();
827
828 if (mzscheme_io_init())
829 return -1;
830
831 }
832 /* recreate ports each call effectivelly clearing these ones */
833 curout = scheme_make_string_output_port();
834 curerr = scheme_make_string_output_port();
835 scheme_set_param(scheme_config, MZCONFIG_OUTPUT_PORT, curout);
836 scheme_set_param(scheme_config, MZCONFIG_ERROR_PORT, curerr);
837
838 if (do_require)
839 {
840 /* auto-instantiate in basic env */
841 eval_in_namespace("(require (prefix vimext: vimext))", do_eval,
842 environment, NULL);
843 }
844
845 return 0;
846}
847
848/*
849 * This routine fills the namespace with various important routines that can
850 * be used within MzScheme.
851 */
852 static void
853mzscheme_interface_init(vim_mz_buffer *mzbuff)
854{
855 Scheme_Object *attach;
856
857 mzbuff->env = (Scheme_Env *)scheme_make_namespace(0, NULL);
858
859 /*
860 * attach instantiated modules from global namespace
861 * so they can be easily instantiated in the buffer namespace
862 */
863 attach = scheme_lookup_global(
864 scheme_intern_symbol("namespace-attach-module"),
865 environment);
866
867 if (attach != NULL)
868 {
869 Scheme_Object *ret;
870 Scheme_Object *args[2];
871
872 args[0] = (Scheme_Object *)environment;
873 args[1] = scheme_intern_symbol("vimext");
874
875 ret = (Scheme_Object *)mzvim_apply(attach, 2, args);
876 }
877
878 add_vim_exn(mzbuff->env);
879}
880
881/*
882 *========================================================================
883 * 2. External Interface
884 *========================================================================
885 */
886
887/*
888 * Evaluate command in namespace with exception handling
889 */
890 static int
891eval_in_namespace(void *data, Scheme_Closed_Prim *what, Scheme_Env *env,
892 Scheme_Object **ret)
893{
894 Scheme_Object *value;
895 Scheme_Object *exn;
896 Cmd_Info info; /* closure info */
897
898 info.data = data;
899 info.env = env;
900
901 scheme_set_param(scheme_config, MZCONFIG_ENV,
902 (Scheme_Object *) env);
903 /*
904 * ensure all evaluations will be in current buffer namespace,
905 * the second argument to scheme_eval_string isn't enough!
906 */
907 value = _apply_thunk_catch_exceptions(
908 scheme_make_closed_prim_w_arity(what, &info, "mzvim", 0, 0),
909 &exn);
910
911 if (!value)
912 {
913 value = extract_exn_message(exn);
914 /* Got an exn? */
915 if (value)
916 {
917 scheme_display(value, curerr); /* Send to stderr-vim */
918 do_flush();
919 }
920 /* `raise' was called on some arbitrary value */
921 return FAIL;
922 }
923
924 if (ret != NULL) /* if pointer to retval supported give it up */
925 *ret = value;
926 /* Print any result, as long as it's not a void */
927 else if (!SCHEME_VOIDP(value))
928 scheme_display(value, curout); /* Send to stdout-vim */
929
930 do_flush();
931 return OK;
932}
933
934/* :mzscheme */
935 static int
936do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
937{
938 if (mzscheme_init())
939 return FAIL;
940
941 range_start = eap->line1;
942 range_end = eap->line2;
943
944 return eval_in_namespace(data, what, get_vim_curr_buffer()->env, NULL);
945}
946
947/*
948 * Routine called by VIM when deleting a buffer
949 */
950 void
951mzscheme_buffer_free(buf_T *buf)
952{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000953 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000954 {
955 vim_mz_buffer *bp;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000956 bp = buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000957 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000958 buf->b_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000959 scheme_gc_ptr_ok(bp);
960 }
961}
962
963/*
964 * Routine called by VIM when deleting a Window
965 */
966 void
967mzscheme_window_free(win_T *win)
968{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000969 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000970 {
971 vim_mz_window *wp;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000972 wp = win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000973 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000974 win->w_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000975 scheme_gc_ptr_ok(wp);
976 }
977}
978
979/*
980 * ":mzscheme" (or ":mz")
981 */
982 void
983ex_mzscheme(exarg_T *eap)
984{
985 char_u *script;
986
987 script = script_get(eap, eap->arg);
988 if (!eap->skip)
989 {
990 if (script == NULL)
991 do_mzscheme_command(eap, eap->arg, do_eval);
992 else
993 {
994 do_mzscheme_command(eap, script, do_eval);
995 vim_free(script);
996 }
997 }
998}
999
1000/* eval MzScheme string */
1001 void *
1002mzvim_eval_string(char_u *str)
1003{
1004 Scheme_Object *ret = NULL;
1005 if (mzscheme_init())
1006 return FAIL;
1007
1008 eval_in_namespace(str, do_eval, get_vim_curr_buffer()->env, &ret);
1009 return ret;
1010}
1011
1012/*
1013 * apply MzScheme procedure with arguments,
1014 * handling errors
1015 */
1016 Scheme_Object *
1017mzvim_apply(Scheme_Object *proc, int argc, Scheme_Object **argv)
1018{
1019 Apply_Info data;
1020 Scheme_Object *ret = NULL;
1021
1022 if (mzscheme_init())
1023 return FAIL;
1024
1025 data.proc = proc;
1026 data.argc = argc;
1027 data.argv = argv;
1028
1029 eval_in_namespace(&data, do_apply, get_vim_curr_buffer()->env, &ret);
1030 return ret;
1031}
1032
1033 static Scheme_Object *
1034do_load(void *data, int noargc, Scheme_Object **noargv)
1035{
1036 Cmd_Info *info = (Cmd_Info *)data;
1037 Scheme_Object *result = scheme_void;
1038 Scheme_Object *expr;
1039 char_u *file = scheme_malloc_fail_ok(
1040 scheme_malloc_atomic, MAXPATHL + 1);
1041 Port_Info *pinfo = (Port_Info *)(info->data);
1042
1043 /* make Vim expansion */
1044 expand_env((char_u *)pinfo->name, file, MAXPATHL);
1045 /* scheme_load looks strange working with namespaces and error handling*/
1046 pinfo->port = scheme_open_input_file(file, "mzfile");
1047 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1048
1049 /* Like REPL but print only last result */
1050 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
1051 result = scheme_eval(expr, info->env);
1052
1053 /* errors will be caught in do_mzscheme_comamnd and ex_mzfile */
1054 scheme_close_input_port(pinfo->port);
1055 pinfo->port = NULL;
1056 return result;
1057}
1058
1059/* :mzfile */
1060 void
1061ex_mzfile(exarg_T *eap)
1062{
1063 Port_Info pinfo;
1064
1065 pinfo.name = (char *)eap->arg;
1066 pinfo.port = NULL;
1067 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1068 && pinfo.port != NULL) /* looks like port was not closed */
1069 scheme_close_input_port(pinfo.port);
1070}
1071
1072
1073/*
1074 *========================================================================
1075 * Exception handling code -- cribbed form the MzScheme sources and
1076 * Matthew Flatt's "Inside PLT MzScheme" document.
1077 *========================================================================
1078 */
1079 static void
1080init_exn_catching_apply(void)
1081{
1082 if (!exn_catching_apply)
1083 {
1084 char *e =
1085 "(lambda (thunk) "
1086 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
1087 "(cons #t (thunk))))";
1088
1089 /* make sure we have a namespace with the standard syntax: */
1090 Scheme_Env *env = (Scheme_Env *)scheme_make_namespace(0, NULL);
1091 add_vim_exn(env);
1092
1093 exn_catching_apply = scheme_eval_string(e, env);
1094 exn_p = scheme_lookup_global(scheme_intern_symbol("exn?"), env);
1095 exn_message = scheme_lookup_global(
1096 scheme_intern_symbol("exn-message"), env);
1097 }
1098}
1099
1100/*
1101 * This function applies a thunk, returning the Scheme value if there's
1102 * no exception, otherwise returning NULL and setting *exn to the raised
1103 * value (usually an exn structure).
1104 */
1105 static Scheme_Object *
1106_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1107{
1108 Scheme_Object *v;
1109
1110 init_exn_catching_apply();
1111
1112 v = _scheme_apply(exn_catching_apply, 1, &f);
1113 /* v is a pair: (cons #t value) or (cons #f exn) */
1114
1115 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1116 return SCHEME_CDR(v);
1117 else
1118 {
1119 *exn = SCHEME_CDR(v);
1120 return NULL;
1121 }
1122}
1123
1124 static Scheme_Object *
1125extract_exn_message(Scheme_Object *v)
1126{
1127 init_exn_catching_apply();
1128
1129 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1130 return _scheme_apply(exn_message, 1, &v);
1131 else
1132 return NULL; /* Not an exn structure */
1133}
1134
1135 static Scheme_Object *
1136do_eval(void *s, int noargc, Scheme_Object **noargv)
1137{
1138 Cmd_Info *info = (Cmd_Info *)s;
1139
1140 return scheme_eval_string_all((char *)(info->data), info->env, TRUE);
1141}
1142
1143 static Scheme_Object *
1144do_apply(void *a, int noargc, Scheme_Object **noargv)
1145{
1146 Apply_Info *info = (Apply_Info *)(((Cmd_Info *)a)->data);
1147
1148 return scheme_apply(info->proc, info->argc, info->argv);
1149}
1150
1151/*
1152 *========================================================================
1153 * 3. MzScheme I/O Handlers
1154 *========================================================================
1155 */
1156 static void
1157do_intrnl_output(char *mesg, long len, int error)
1158{
1159 char *p, *prev;
1160
1161 prev = mesg;
1162 p = strchr(prev, '\n');
1163 while (p)
1164 {
1165 *p = '\0';
1166 if (error)
1167 EMSG(prev);
1168 else
1169 MSG(prev);
1170 prev = p + 1;
1171 p = strchr(prev, '\n');
1172 }
1173
1174 if (error)
1175 EMSG(prev);
1176 else
1177 MSG(prev);
1178}
1179
1180 static void
1181do_output(char *mesg, long len)
1182{
1183 do_intrnl_output(mesg, len, 0);
1184}
1185
1186 static void
1187do_err_output(char *mesg, long len)
1188{
1189 do_intrnl_output(mesg, len, 1);
1190}
1191
1192 static void
1193do_printf(char *format, ...)
1194{
1195 do_intrnl_output(format, STRLEN(format), 1);
1196}
1197
1198 static void
1199do_flush(void)
1200{
1201 char *buff;
1202 long length;
1203
1204 buff = scheme_get_sized_string_output(curerr, &length);
1205 if (length)
1206 {
1207 do_err_output(buff, length);
1208 return;
1209 }
1210
1211 buff = scheme_get_sized_string_output(curout, &length);
1212 if (length)
1213 do_output(buff, length);
1214}
1215
1216 static int
1217mzscheme_io_init(void)
1218{
1219 /* Nothing needed so far... */
1220 return 0;
1221}
1222
1223/*
1224 *========================================================================
1225 * 4. Implementation of the Vim Features for MzScheme
1226 *========================================================================
1227 */
1228
1229/* (command {command-string}) */
1230 static Scheme_Object *
1231vim_command(void *data, int argc, Scheme_Object **argv)
1232{
1233 Vim_Prim *prim = (Vim_Prim *)data;
1234 char *cmd = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1235
1236 /* may be use do_cmdline_cmd? */
1237 do_cmdline((char_u *)cmd, NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
1238 update_screen(VALID);
1239
1240 raise_if_error();
1241 return scheme_void;
1242}
1243
1244/* (eval {expr-string}) */
1245 static Scheme_Object *
1246vim_eval(void *data, int argc, Scheme_Object **argv)
1247{
1248#ifdef FEAT_EVAL
1249 Vim_Prim *prim = (Vim_Prim *)data;
1250 char *expr;
1251 char *str;
1252 Scheme_Object *result;
1253
1254 expr = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1255
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001256 str = (char *)eval_to_string((char_u *)expr, NULL, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001257
1258 if (str == NULL)
1259 raise_vim_exn(_("invalid expression"));
1260
1261 result = scheme_make_string(str);
1262
1263 vim_free(str);
1264
1265 return result;
1266#else
1267 raise_vim_exn(_("expressions disabled at compile time"));
1268 /* unreachable */
1269 return scheme_false;
1270#endif
1271}
1272
1273/* (range-start) */
1274 static Scheme_Object *
1275get_range_start(void *data, int argc, Scheme_Object **argv)
1276{
1277 return scheme_make_integer(range_start);
1278}
1279
1280/* (range-end) */
1281 static Scheme_Object *
1282get_range_end(void *data, int argc, Scheme_Object **argv)
1283{
1284 return scheme_make_integer(range_end);
1285}
1286
1287/* (beep) */
1288 static Scheme_Object *
1289mzscheme_beep(void *data, int argc, Scheme_Object **argv)
1290{
1291 vim_beep();
1292 return scheme_void;
1293}
1294
1295static Scheme_Object *M_global = NULL;
1296
1297/* (get-option {option-name}) [buffer/window] */
1298 static Scheme_Object *
1299get_option(void *data, int argc, Scheme_Object **argv)
1300{
1301 Vim_Prim *prim = (Vim_Prim *)data;
1302 char_u *name;
1303 long value;
1304 char_u *strval;
1305 int rc;
1306 Scheme_Object *rval;
1307 int opt_flags = 0;
1308 buf_T *save_curb = curbuf;
1309 win_T *save_curw = curwin;
1310
1311 name = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1312
1313 if (argc > 1)
1314 {
1315 if (M_global == NULL)
1316 {
1317 MZ_REGISTER_STATIC(M_global);
1318 M_global = scheme_intern_symbol("global");
1319 }
1320
1321 if (argv[1] == M_global)
1322 opt_flags = OPT_GLOBAL;
1323 else if (SCHEME_VIMBUFFERP(argv[1]))
1324 {
1325 curbuf = get_valid_buffer(argv[1]);
1326 opt_flags = OPT_LOCAL;
1327 }
1328 else if (SCHEME_VIMWINDOWP(argv[1]))
1329 {
1330 win_T *win = get_valid_window(argv[1]);
1331
1332 curwin = win;
1333 curbuf = win->w_buffer;
1334 opt_flags = OPT_LOCAL;
1335 }
1336 else
1337 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1338 }
1339
1340 rc = get_option_value(name, &value, &strval, opt_flags);
1341 curbuf = save_curb;
1342 curwin = save_curw;
1343
1344 switch (rc)
1345 {
1346 case 1:
1347 return scheme_make_integer_value(value);
1348 case 0:
1349 rval = scheme_make_string(strval);
1350 vim_free(strval);
1351 return rval;
1352 case -1:
1353 case -2:
1354 raise_vim_exn(_("hidden option"));
1355 case -3:
1356 raise_vim_exn(_("unknown option"));
1357 }
1358 /* unreachable */
1359 return scheme_void;
1360}
1361
1362/* (set-option {option-changing-string} [buffer/window]) */
1363 static Scheme_Object *
1364set_option(void *data, int argc, Scheme_Object **argv)
1365{
1366 char_u *cmd;
1367 int opt_flags = 0;
1368 buf_T *save_curb = curbuf;
1369 win_T *save_curw = curwin;
1370 Vim_Prim *prim = (Vim_Prim *)data;
1371
1372 GUARANTEE_STRING(prim->name, 0);
1373 if (argc > 1)
1374 {
1375 if (M_global == NULL)
1376 {
1377 MZ_REGISTER_STATIC(M_global);
1378 M_global = scheme_intern_symbol("global");
1379 }
1380
1381 if (argv[1] == M_global)
1382 opt_flags = OPT_GLOBAL;
1383 else if (SCHEME_VIMBUFFERP(argv[1]))
1384 {
1385 curbuf = get_valid_buffer(argv[1]);
1386 opt_flags = OPT_LOCAL;
1387 }
1388 else if (SCHEME_VIMWINDOWP(argv[1]))
1389 {
1390 win_T *win = get_valid_window(argv[1]);
1391 curwin = win;
1392 curbuf = win->w_buffer;
1393 opt_flags = OPT_LOCAL;
1394 }
1395 else
1396 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1397 }
1398
1399 /* do_set can modify cmd, make copy */
1400 cmd = vim_strsave((char_u *)SCHEME_STR_VAL(argv[0]));
1401 do_set(cmd, opt_flags);
1402 vim_free(cmd);
1403 update_screen(NOT_VALID);
1404 curbuf = save_curb;
1405 curwin = save_curw;
1406 raise_if_error();
1407 return scheme_void;
1408}
1409
1410/*
1411 *===========================================================================
1412 * 5. Vim Window-related Manipulation Functions
1413 *===========================================================================
1414 */
1415
1416/* (curr-win) */
1417 static Scheme_Object *
1418get_curr_win(void *data, int argc, Scheme_Object **argv)
1419{
1420 return (Scheme_Object *)get_vim_curr_window();
1421}
1422
1423/* (win-count) */
1424 static Scheme_Object *
1425get_window_count(void *data, int argc, Scheme_Object **argv)
1426{
1427 win_T *w;
1428 int n = 0;
1429
Bram Moolenaarf740b292006-02-16 22:11:02 +00001430 for (w = firstwin; w != NULL; w = w->w_next)
1431 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001432 return scheme_make_integer(n);
1433}
1434
1435/* (get-win-list [buffer]) */
1436 static Scheme_Object *
1437get_window_list(void *data, int argc, Scheme_Object **argv)
1438{
1439 Vim_Prim *prim = (Vim_Prim *)data;
1440 vim_mz_buffer *buf;
1441 Scheme_Object *list;
1442 win_T *w;
1443
1444 buf = get_buffer_arg(prim->name, 0, argc, argv);
1445 list = scheme_null;
1446
Bram Moolenaarf740b292006-02-16 22:11:02 +00001447 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001448 if (w->w_buffer == buf->buf)
1449 list = scheme_make_pair(window_new(w), list);
1450
1451 return list;
1452}
1453
1454 static Scheme_Object *
1455window_new(win_T *win)
1456{
1457 vim_mz_window *self;
1458
1459 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001460 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001461 * then we can get at it in win_free() in vim.
1462 *
1463 * On a win_free() we set the Scheme object's win_T *field
1464 * to an invalid value. We trap all uses of a window
1465 * object, and reject them if the win_T *field is invalid.
1466 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001467 if (win->w_mzscheme_ref != NULL)
1468 return win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001469
1470 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_window));
1471
1472 vim_memset(self, 0, sizeof(vim_mz_window));
1473 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001474 win->w_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001475 self->win = win;
1476 self->tag = mz_window_type;
1477
1478 return (Scheme_Object *)(self);
1479}
1480
1481/* (get-win-num [window]) */
1482 static Scheme_Object *
1483get_window_num(void *data, int argc, Scheme_Object **argv)
1484{
1485 Vim_Prim *prim = (Vim_Prim *)data;
1486 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
1487 int nr = 1;
1488 win_T *wp;
1489
1490 for (wp = firstwin; wp != win; wp = wp->w_next)
1491 ++nr;
1492
1493 return scheme_make_integer(nr);
1494}
1495
1496/* (get-win-by-num {windownum}) */
1497 static Scheme_Object *
1498get_window_by_num(void *data, int argc, Scheme_Object **argv)
1499{
1500 Vim_Prim *prim = (Vim_Prim *)data;
1501 win_T *win;
1502 int fnum;
1503
1504 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1505 if (fnum < 1)
1506 scheme_signal_error(_("window index is out of range"));
1507
Bram Moolenaarf740b292006-02-16 22:11:02 +00001508 for (win = firstwin; win != NULL; win = win->w_next, --fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001509 if (fnum == 1) /* to be 1-based */
1510 return window_new(win);
1511
1512 return scheme_false;
1513}
1514
1515/* (get-win-buffer [window]) */
1516 static Scheme_Object *
1517get_window_buffer(void *data, int argc, Scheme_Object **argv)
1518{
1519 Vim_Prim *prim = (Vim_Prim *)data;
1520 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1521
1522 return buffer_new(win->win->w_buffer);
1523}
1524
1525/* (get-win-height [window]) */
1526 static Scheme_Object *
1527get_window_height(void *data, int argc, Scheme_Object **argv)
1528{
1529 Vim_Prim *prim = (Vim_Prim *)data;
1530 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1531
1532 return scheme_make_integer(win->win->w_height);
1533}
1534
1535/* (set-win-height {height} [window]) */
1536 static Scheme_Object *
1537set_window_height(void *data, int argc, Scheme_Object **argv)
1538{
1539 Vim_Prim *prim = (Vim_Prim *)data;
1540 vim_mz_window *win;
1541 win_T *savewin;
1542 int height;
1543
1544 win = get_window_arg(prim->name, 1, argc, argv);
1545 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1546
1547#ifdef FEAT_GUI
1548 need_mouse_correct = TRUE;
1549#endif
1550
1551 savewin = curwin;
1552 curwin = win->win;
1553 win_setheight(height);
1554 curwin = savewin;
1555
1556 raise_if_error();
1557 return scheme_void;
1558}
1559
1560#ifdef FEAT_VERTSPLIT
1561/* (get-win-width [window]) */
1562 static Scheme_Object *
1563get_window_width(void *data, int argc, Scheme_Object **argv)
1564{
1565 Vim_Prim *prim = (Vim_Prim *)data;
1566 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1567
1568 return scheme_make_integer(W_WIDTH(win->win));
1569}
1570
1571/* (set-win-width {width} [window]) */
1572 static Scheme_Object *
1573set_window_width(void *data, int argc, Scheme_Object **argv)
1574{
1575 Vim_Prim *prim = (Vim_Prim *)data;
1576 vim_mz_window *win;
1577 win_T *savewin;
1578 int width = 0;
1579
1580 win = get_window_arg(prim->name, 1, argc, argv);
1581 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1582
1583# ifdef FEAT_GUI
1584 need_mouse_correct = TRUE;
1585# endif
1586
1587 savewin = curwin;
1588 curwin = win->win;
1589 win_setwidth(width);
1590 curwin = savewin;
1591
1592 raise_if_error();
1593 return scheme_void;
1594}
1595#endif
1596
1597/* (get-cursor [window]) -> (line . col) */
1598 static Scheme_Object *
1599get_cursor(void *data, int argc, Scheme_Object **argv)
1600{
1601 Vim_Prim *prim = (Vim_Prim *)data;
1602 vim_mz_window *win;
1603 pos_T pos;
1604
1605 win = get_window_arg(prim->name, 0, argc, argv);
1606 pos = win->win->w_cursor;
1607 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
1608 scheme_make_integer_value((long)pos.col + 1));
1609}
1610
1611/* (set-cursor (line . col) [window]) */
1612 static Scheme_Object *
1613set_cursor(void *data, int argc, Scheme_Object **argv)
1614{
1615 Vim_Prim *prim = (Vim_Prim *)data;
1616 vim_mz_window *win;
1617 long lnum = 0;
1618 long col = 0;
1619
Bram Moolenaar555b2802005-05-19 21:08:39 +00001620#ifdef HAVE_SANDBOX
1621 sandbox_check();
1622#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001623 win = get_window_arg(prim->name, 1, argc, argv);
1624 GUARANTEE_PAIR(prim->name, 0);
1625
1626 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
1627 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
1628 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
1629
1630 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
1631 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
1632
1633 check_line_range(lnum, win->win->w_buffer);
1634 /* don't know how to catch invalid column value */
1635
1636 win->win->w_cursor.lnum = lnum;
1637 win->win->w_cursor.col = col;
1638 update_screen(VALID);
1639
1640 raise_if_error();
1641 return scheme_void;
1642}
1643/*
1644 *===========================================================================
1645 * 6. Vim Buffer-related Manipulation Functions
1646 * Note that each buffer should have its own private namespace.
1647 *===========================================================================
1648 */
1649
1650/* (open-buff {filename}) */
1651 static Scheme_Object *
1652mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
1653{
1654 Vim_Prim *prim = (Vim_Prim *)data;
1655 char *fname;
1656 int num = 0;
1657 Scheme_Object *onum;
1658
Bram Moolenaar555b2802005-05-19 21:08:39 +00001659#ifdef HAVE_SANDBOX
1660 sandbox_check();
1661#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001662 fname = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1663 /* TODO make open existing file */
1664 num = buflist_add(fname, BLN_LISTED | BLN_CURBUF);
1665
1666 if (num == 0)
1667 raise_vim_exn(_("couldn't open buffer"));
1668
1669 onum = scheme_make_integer(num);
1670 return get_buffer_by_num(data, 1, &onum);
1671}
1672
1673/* (get-buff-by-num {buffernum}) */
1674 static Scheme_Object *
1675get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
1676{
1677 Vim_Prim *prim = (Vim_Prim *)data;
1678 buf_T *buf;
1679 int fnum;
1680
1681 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1682
1683 for (buf = firstbuf; buf; buf = buf->b_next)
1684 if (buf->b_fnum == fnum)
1685 return buffer_new(buf);
1686
1687 return scheme_false;
1688}
1689
1690/* (get-buff-by-name {buffername}) */
1691 static Scheme_Object *
1692get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
1693{
1694 Vim_Prim *prim = (Vim_Prim *)data;
1695 buf_T *buf;
1696 char_u *fname;
1697
1698 fname = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1699
1700 for (buf = firstbuf; buf; buf = buf->b_next)
1701 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1702 /* empty string */
1703 {
1704 if (fname[0] == NUL)
1705 return buffer_new(buf);
1706 }
1707 else if (!fnamecmp(buf->b_ffname, fname)
1708 || !fnamecmp(buf->b_sfname, fname))
1709 /* either short or long filename matches */
1710 return buffer_new(buf);
1711
1712 return scheme_false;
1713}
1714
1715/* (get-next-buff [buffer]) */
1716 static Scheme_Object *
1717get_next_buffer(void *data, int argc, Scheme_Object **argv)
1718{
1719 Vim_Prim *prim = (Vim_Prim *)data;
1720 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1721
1722 if (buf->b_next == NULL)
1723 return scheme_false;
1724 else
1725 return buffer_new(buf->b_next);
1726}
1727
1728/* (get-prev-buff [buffer]) */
1729 static Scheme_Object *
1730get_prev_buffer(void *data, int argc, Scheme_Object **argv)
1731{
1732 Vim_Prim *prim = (Vim_Prim *)data;
1733 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1734
1735 if (buf->b_prev == NULL)
1736 return scheme_false;
1737 else
1738 return buffer_new(buf->b_prev);
1739}
1740
1741/* (get-buff-num [buffer]) */
1742 static Scheme_Object *
1743get_buffer_num(void *data, int argc, Scheme_Object **argv)
1744{
1745 Vim_Prim *prim = (Vim_Prim *)data;
1746 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1747
1748 return scheme_make_integer(buf->buf->b_fnum);
1749}
1750
1751/* (buff-count) */
1752 static Scheme_Object *
1753get_buffer_count(void *data, int argc, Scheme_Object **argv)
1754{
1755 buf_T *b;
1756 int n = 0;
1757
1758 for (b = firstbuf; b; b = b->b_next) ++n;
1759 return scheme_make_integer(n);
1760}
1761
1762/* (get-buff-name [buffer]) */
1763 static Scheme_Object *
1764get_buffer_name(void *data, int argc, Scheme_Object **argv)
1765{
1766 Vim_Prim *prim = (Vim_Prim *)data;
1767 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1768
1769 return scheme_make_string(buf->buf->b_ffname);
1770}
1771
1772/* (curr-buff) */
1773 static Scheme_Object *
1774get_curr_buffer(void *data, int argc, Scheme_Object **argv)
1775{
1776 return (Scheme_Object *)get_vim_curr_buffer();
1777}
1778
1779 static Scheme_Object *
1780buffer_new(buf_T *buf)
1781{
1782 vim_mz_buffer *self;
1783
1784 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001785 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001786 * then we can get at it in buf_freeall() in vim.
1787 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001788 if (buf->b_mzscheme_ref)
1789 return buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001790
1791 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_buffer));
1792
1793 vim_memset(self, 0, sizeof(vim_mz_buffer));
1794 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001795 buf->b_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001796 self->buf = buf;
1797 self->tag = mz_buffer_type;
1798
1799 mzscheme_interface_init(self); /* Set up namespace */
1800
1801 return (Scheme_Object *)(self);
1802}
1803
1804/*
1805 * (get-buff-size [buffer])
1806 *
1807 * Get the size (number of lines) in the current buffer.
1808 */
1809 static Scheme_Object *
1810get_buffer_size(void *data, int argc, Scheme_Object **argv)
1811{
1812 Vim_Prim *prim = (Vim_Prim *)data;
1813 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1814
1815 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
1816}
1817
1818/*
1819 * (get-buff-line {linenr} [buffer])
1820 *
1821 * Get a line from the specified buffer. The line number is
1822 * in Vim format (1-based). The line is returned as a MzScheme
1823 * string object.
1824 */
1825 static Scheme_Object *
1826get_buffer_line(void *data, int argc, Scheme_Object **argv)
1827{
1828 Vim_Prim *prim = (Vim_Prim *)data;
1829 vim_mz_buffer *buf;
1830 int linenr;
1831 char *line;
1832
1833 buf = get_buffer_arg(prim->name, 1, argc, argv);
1834 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1835 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
1836
1837 raise_if_error();
1838 return scheme_make_string(line);
1839}
1840
1841
1842/*
1843 * (get-buff-line-list {start} {end} [buffer])
1844 *
1845 * Get a list of lines from the specified buffer. The line numbers
1846 * are in Vim format (1-based). The range is from lo up to, but not
1847 * including, hi. The list is returned as a list of string objects.
1848 */
1849 static Scheme_Object *
1850get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
1851{
1852 Vim_Prim *prim = (Vim_Prim *)data;
1853 vim_mz_buffer *buf;
1854 int i, hi, lo, n;
1855 Scheme_Object *list;
1856
1857 buf = get_buffer_arg(prim->name, 2, argc, argv);
1858 list = scheme_null;
1859 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
1860 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1861
1862 /*
1863 * Handle some error conditions
1864 */
1865 if (lo < 0)
1866 lo = 0;
1867
1868 if (hi < 0)
1869 hi = 0;
1870 if (hi < lo)
1871 hi = lo;
1872
1873 n = hi - lo;
1874
1875 for (i = n; i >= 0; --i)
1876 {
1877 Scheme_Object *str = scheme_make_string(
1878 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
1879 raise_if_error();
1880
1881 /* Set the list item */
1882 list = scheme_make_pair(str, list);
1883 }
1884
1885 return list;
1886}
1887
1888/*
1889 * (set-buff-line {linenr} {string/#f} [buffer])
1890 *
1891 * Replace a line in the specified buffer. The line number is
1892 * in Vim format (1-based). The replacement line is given as
1893 * an MzScheme string object. The object is checked for validity
1894 * and correct format. An exception is thrown if the values are not
1895 * the correct format.
1896 *
1897 * It returns a Scheme Object that indicates the length of the
1898 * string changed.
1899 */
1900 static Scheme_Object *
1901set_buffer_line(void *data, int argc, Scheme_Object **argv)
1902{
1903 /* First of all, we check the the of the supplied MzScheme object.
1904 * There are three cases:
1905 * 1. #f - this is a deletion.
1906 * 2. A string - this is a replacement.
1907 * 3. Anything else - this is an error.
1908 */
1909 Vim_Prim *prim = (Vim_Prim *)data;
1910 vim_mz_buffer *buf;
1911 Scheme_Object *line;
1912 char *save;
1913 buf_T *savebuf;
1914 int n;
1915
Bram Moolenaar555b2802005-05-19 21:08:39 +00001916#ifdef HAVE_SANDBOX
1917 sandbox_check();
1918#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001919 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1920 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
1921 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
1922 line = argv[1];
1923 buf = get_buffer_arg(prim->name, 2, argc, argv);
1924
1925 check_line_range(n, buf->buf);
1926
1927 if (SCHEME_FALSEP(line))
1928 {
1929 savebuf = curbuf;
1930 curbuf = buf->buf;
1931
1932 if (u_savedel((linenr_T)n, 1L) == FAIL)
1933 {
1934 curbuf = savebuf;
1935 raise_vim_exn(_("cannot save undo information"));
1936 }
1937 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
1938 {
1939 curbuf = savebuf;
1940 raise_vim_exn(_("cannot delete line"));
1941 }
1942 deleted_lines_mark((linenr_T)n, 1L);
1943 if (buf->buf == curwin->w_buffer)
1944 mz_fix_cursor(n, n + 1, -1);
1945
1946 curbuf = savebuf;
1947
1948 raise_if_error();
1949 return scheme_void;
1950 }
1951
1952 /* Otherwise it's a line */
1953 save = string_to_line(line);
1954 savebuf = curbuf;
1955
1956 curbuf = buf->buf;
1957
1958 if (u_savesub((linenr_T)n) == FAIL)
1959 {
1960 curbuf = savebuf;
1961 raise_vim_exn(_("cannot save undo information"));
1962 }
1963 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
1964 {
1965 curbuf = savebuf;
1966 raise_vim_exn(_("cannot replace line"));
1967 }
1968 else
1969 changed_bytes((linenr_T)n, 0);
1970
1971 curbuf = savebuf;
1972
1973 raise_if_error();
1974 return scheme_void;
1975}
1976
1977/*
1978 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
1979 *
1980 * Replace a range of lines in the specified buffer. The line numbers are in
1981 * Vim format (1-based). The range is from lo up to, but not including, hi.
1982 * The replacement lines are given as a Scheme list of string objects. The
1983 * list is checked for validity and correct format.
1984 *
1985 * Errors are returned as a value of FAIL. The return value is OK on success.
1986 * If OK is returned and len_change is not NULL, *len_change is set to the
1987 * change in the buffer length.
1988 */
1989 static Scheme_Object *
1990set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
1991{
1992 /* First of all, we check the type of the supplied MzScheme object.
1993 * There are three cases:
1994 * 1. #f - this is a deletion.
1995 * 2. A list - this is a replacement.
1996 * 3. Anything else - this is an error.
1997 */
1998 Vim_Prim *prim = (Vim_Prim *)data;
1999 vim_mz_buffer *buf;
2000 Scheme_Object *line_list;
2001 Scheme_Object *line;
2002 Scheme_Object *rest;
2003 char **array;
2004 buf_T *savebuf;
2005 int i, old_len, new_len, hi, lo;
2006 long extra;
2007
Bram Moolenaar555b2802005-05-19 21:08:39 +00002008#ifdef HAVE_SANDBOX
2009 sandbox_check();
2010#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002011 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2012 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2013 if (!SCHEME_PAIRP(argv[2])
2014 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2015 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2016 line_list = argv[2];
2017 buf = get_buffer_arg(prim->name, 3, argc, argv);
2018 old_len = hi - lo;
2019 if (old_len < 0) /* process inverse values wisely */
2020 {
2021 i = lo;
2022 lo = hi;
2023 hi = i;
2024 old_len = -old_len;
2025 }
2026 extra = 0;
2027
2028 check_line_range(lo, buf->buf); /* inclusive */
2029 check_line_range(hi - 1, buf->buf); /* exclisive */
2030
2031 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2032 {
2033 savebuf = curbuf;
2034 curbuf = buf->buf;
2035
2036 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2037 {
2038 curbuf = savebuf;
2039 raise_vim_exn(_("cannot save undo information"));
2040 }
2041 else
2042 {
2043 for (i = 0; i < old_len; i++)
2044 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2045 {
2046 curbuf = savebuf;
2047 raise_vim_exn(_("cannot delete line"));
2048 }
2049 deleted_lines_mark((linenr_T)lo, (long)old_len);
2050 if (buf->buf == curwin->w_buffer)
2051 mz_fix_cursor(lo, hi, -old_len);
2052 }
2053
2054 curbuf = savebuf;
2055
2056 raise_if_error();
2057 return scheme_void;
2058 }
2059
2060 /* List */
2061 new_len = scheme_proper_list_length(line_list);
2062 if (new_len < 0) /* improper or cyclic list */
2063 scheme_wrong_type(prim->name, "proper list",
2064 2, argc, argv);
2065
2066 /* Using MzScheme allocator, so we don't need to free this and
2067 * can safely keep pointers to GC collected strings
2068 */
2069 array = (char **)scheme_malloc_fail_ok(scheme_malloc,
2070 (unsigned)(new_len * sizeof(char *)));
2071
2072 rest = line_list;
2073 for (i = 0; i < new_len; ++i)
2074 {
2075 line = SCHEME_CAR(rest);
2076 rest = SCHEME_CDR(rest);
2077 if (!SCHEME_STRINGP(line))
2078 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2079 array[i] = string_to_line(line);
2080 }
2081
2082 savebuf = curbuf;
2083 curbuf = buf->buf;
2084
2085 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2086 {
2087 curbuf = savebuf;
2088 raise_vim_exn(_("cannot save undo information"));
2089 }
2090
2091 /*
2092 * If the size of the range is reducing (ie, new_len < old_len) we
2093 * need to delete some old_len. We do this at the start, by
2094 * repeatedly deleting line "lo".
2095 */
2096 for (i = 0; i < old_len - new_len; ++i)
2097 {
2098 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2099 {
2100 curbuf = savebuf;
2101 raise_vim_exn(_("cannot delete line"));
2102 }
2103 extra--;
2104 }
2105
2106 /*
2107 * For as long as possible, replace the existing old_len with the
2108 * new old_len. This is a more efficient operation, as it requires
2109 * less memory allocation and freeing.
2110 */
2111 for (i = 0; i < old_len && i < new_len; i++)
2112 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2113 {
2114 curbuf = savebuf;
2115 raise_vim_exn(_("cannot replace line"));
2116 }
2117
2118 /*
2119 * Now we may need to insert the remaining new_len. We don't need to
2120 * free the string passed back because MzScheme has control of that
2121 * memory.
2122 */
2123 while (i < new_len)
2124 {
2125 if (ml_append((linenr_T)(lo + i - 1),
2126 (char_u *)array[i], 0, FALSE) == FAIL)
2127 {
2128 curbuf = savebuf;
2129 raise_vim_exn(_("cannot insert line"));
2130 }
2131 ++i;
2132 ++extra;
2133 }
2134
2135 /*
2136 * Adjust marks. Invalidate any which lie in the
2137 * changed range, and move any in the remainder of the buffer.
2138 */
2139 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra);
2140 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2141
2142 if (buf->buf == curwin->w_buffer)
2143 mz_fix_cursor(lo, hi, extra);
2144 curbuf = savebuf;
2145
2146 raise_if_error();
2147 return scheme_void;
2148}
2149
2150/*
2151 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2152 *
2153 * Insert a number of lines into the specified buffer after the specifed line.
2154 * The line number is in Vim format (1-based). The lines to be inserted are
2155 * given as an MzScheme list of string objects or as a single string. The lines
2156 * to be added are checked for validity and correct format. Errors are
2157 * returned as a value of FAIL. The return value is OK on success.
2158 * If OK is returned and len_change is not NULL, *len_change
2159 * is set to the change in the buffer length.
2160 */
2161 static Scheme_Object *
2162insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2163{
2164 Vim_Prim *prim = (Vim_Prim *)data;
2165 vim_mz_buffer *buf;
2166 Scheme_Object *list;
2167 Scheme_Object *line;
2168 Scheme_Object *rest;
2169 char **array;
2170 char *str;
2171 buf_T *savebuf;
2172 int i, n, size;
2173
Bram Moolenaar555b2802005-05-19 21:08:39 +00002174#ifdef HAVE_SANDBOX
2175 sandbox_check();
2176#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002177 /*
2178 * First of all, we check the type of the supplied MzScheme object.
2179 * It must be a string or a list, or the call is in error.
2180 */
2181 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2182 list = argv[1];
2183
2184 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2185 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2186 buf = get_buffer_arg(prim->name, 2, argc, argv);
2187
2188 if (n != 0) /* 0 can be used in insert */
2189 check_line_range(n, buf->buf);
2190 if (SCHEME_STRINGP(list))
2191 {
2192 str = string_to_line(list);
2193
2194 savebuf = curbuf;
2195 curbuf = buf->buf;
2196
2197 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2198 {
2199 curbuf = savebuf;
2200 raise_vim_exn(_("cannot save undo information"));
2201 }
2202 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2203 {
2204 curbuf = savebuf;
2205 raise_vim_exn(_("cannot insert line"));
2206 }
2207 else
2208 appended_lines_mark((linenr_T)n, 1L);
2209
2210 curbuf = savebuf;
2211 update_screen(VALID);
2212
2213 raise_if_error();
2214 return scheme_void;
2215 }
2216
2217 /* List */
2218 size = scheme_proper_list_length(list);
2219 if (size < 0) /* improper or cyclic list */
2220 scheme_wrong_type(prim->name, "proper list",
2221 2, argc, argv);
2222
2223 /* Using MzScheme allocator, so we don't need to free this and
2224 * can safely keep pointers to GC collected strings
2225 */
2226 array = (char **)scheme_malloc_fail_ok(
2227 scheme_malloc, (unsigned)(size * sizeof(char *)));
2228
2229 rest = list;
2230 for (i = 0; i < size; ++i)
2231 {
2232 line = SCHEME_CAR(rest);
2233 rest = SCHEME_CDR(rest);
2234 array[i] = string_to_line(line);
2235 }
2236
2237 savebuf = curbuf;
2238 curbuf = buf->buf;
2239
2240 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2241 {
2242 curbuf = savebuf;
2243 raise_vim_exn(_("cannot save undo information"));
2244 }
2245 else
2246 {
2247 for (i = 0; i < size; ++i)
2248 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2249 0, FALSE) == FAIL)
2250 {
2251 curbuf = savebuf;
2252 raise_vim_exn(_("cannot insert line"));
2253 }
2254
2255 if (i > 0)
2256 appended_lines_mark((linenr_T)n, (long)i);
2257 }
2258
2259 curbuf = savebuf;
2260 update_screen(VALID);
2261
2262 raise_if_error();
2263 return scheme_void;
2264}
2265
2266/* (get-buff-namespace [buffer]) */
2267 static Scheme_Object *
2268get_buffer_namespace(void *data, int argc, Scheme_Object **argv)
2269{
2270 Vim_Prim *prim = (Vim_Prim *)data;
2271
2272 return (Scheme_Object *)get_buffer_arg(prim->name, 0, argc, argv)->env;
2273}
2274
2275/*
2276 * Predicates
2277 */
2278/* (buff? obj) */
2279 static Scheme_Object *
2280vim_bufferp(void *data, int argc, Scheme_Object **argv)
2281{
2282 if (SCHEME_VIMBUFFERP(argv[0]))
2283 return scheme_true;
2284 else
2285 return scheme_false;
2286}
2287
2288/* (win? obj) */
2289 static Scheme_Object *
2290vim_windowp(void *data, int argc, Scheme_Object **argv)
2291{
2292 if (SCHEME_VIMWINDOWP(argv[0]))
2293 return scheme_true;
2294 else
2295 return scheme_false;
2296}
2297
2298/* (buff-valid? obj) */
2299 static Scheme_Object *
2300vim_buffer_validp(void *data, int argc, Scheme_Object **argv)
2301{
2302 if (SCHEME_VIMBUFFERP(argv[0])
2303 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2304 return scheme_true;
2305 else
2306 return scheme_false;
2307}
2308
2309/* (win-valid? obj) */
2310 static Scheme_Object *
2311vim_window_validp(void *data, int argc, Scheme_Object **argv)
2312{
2313 if (SCHEME_VIMWINDOWP(argv[0])
2314 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2315 return scheme_true;
2316 else
2317 return scheme_false;
2318}
2319
2320/*
2321 *===========================================================================
2322 * Utilities
2323 *===========================================================================
2324 */
2325
2326/*
2327 * Convert an MzScheme string into a Vim line.
2328 *
2329 * The result is in allocated memory. All internal nulls are replaced by
2330 * newline characters. It is an error for the string to contain newline
2331 * characters.
2332 *
2333 */
2334 static char *
2335string_to_line(Scheme_Object *obj)
2336{
2337 char *str;
2338 long len;
2339 int i;
2340
2341 str = scheme_display_to_string(obj, &len);
2342
2343 /* Error checking: String must not contain newlines, as we
2344 * are replacing a single line, and we must replace it with
2345 * a single line.
2346 */
2347 if (memchr(str, '\n', len))
2348 scheme_signal_error(_("string cannot contain newlines"));
2349
2350 /* Create a copy of the string, with internal nulls replaced by
2351 * newline characters, as is the vim convention.
2352 */
2353 for (i = 0; i < len; ++i)
2354 {
2355 if (str[i] == '\0')
2356 str[i] = '\n';
2357 }
2358
2359 str[i] = '\0';
2360
2361 return str;
2362}
2363
2364/*
2365 * Check to see whether a Vim error has been reported, or a keyboard
2366 * interrupt (from vim --> got_int) has been detected.
2367 */
2368 static int
2369vim_error_check(void)
2370{
2371 return (got_int || did_emsg);
2372}
2373
2374/*
2375 * register Scheme exn:vim
2376 */
2377 static void
2378register_vim_exn(Scheme_Env *env)
2379{
2380 Scheme_Object *exn_name = scheme_intern_symbol("exn:vim");
2381
2382 if (vim_exn == NULL)
2383 vim_exn = scheme_make_struct_type(exn_name,
2384 scheme_builtin_value("struct:exn"), NULL, 0, 0, NULL, NULL
2385#if MZSCHEME_VERSION_MAJOR >= 299
2386 , NULL
2387#endif
2388 );
2389
2390 if (vim_exn_values == NULL)
2391 {
2392 int nc = 0;
2393
2394 Scheme_Object **exn_names = scheme_make_struct_names(
2395 exn_name, scheme_null, 0, &nc);
2396 Scheme_Object **exn_values = scheme_make_struct_values(
2397 vim_exn, exn_names, nc, 0);
2398
2399 vim_exn_names = scheme_make_vector(nc, scheme_false);
2400 vim_exn_values = scheme_make_vector(nc, scheme_false);
2401 /* remember names and values */
2402 mch_memmove(SCHEME_VEC_ELS(vim_exn_names), exn_names,
2403 nc * sizeof(Scheme_Object *));
2404 mch_memmove(SCHEME_VEC_ELS(vim_exn_values), exn_values,
2405 nc * sizeof(Scheme_Object *));
2406 }
2407
2408 add_vim_exn(env);
2409}
2410
2411/*
2412 * Add stuff of exn:vim to env
2413 */
2414 static void
2415add_vim_exn(Scheme_Env *env)
2416{
2417 int i;
2418
2419 for (i = 0; i < SCHEME_VEC_SIZE(vim_exn_values); i++)
2420 scheme_add_global_symbol(SCHEME_VEC_ELS(vim_exn_names)[i],
2421 SCHEME_VEC_ELS(vim_exn_values)[i], env);
2422}
2423
2424/*
2425 * raise exn:vim, may be with additional info string
2426 */
2427 void
2428raise_vim_exn(const char *add_info)
2429{
2430 Scheme_Object *argv[2];
2431 char_u *fmt = _("Vim error: ~a");
2432
2433 if (add_info != NULL)
2434 {
2435 Scheme_Object *info = scheme_make_string(add_info);
Bram Moolenaar555b2802005-05-19 21:08:39 +00002436 argv[0] = scheme_byte_string_to_char_string(scheme_make_string(
2437 scheme_format(fmt, strlen(fmt), 1, &info, NULL)));
2438 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002439 }
2440 else
2441 argv[0] = scheme_make_string(_("Vim error"));
2442
2443 argv[1] = scheme_current_continuation_marks();
2444
2445 scheme_raise(scheme_make_struct_instance(vim_exn, 2, argv));
2446}
2447
2448 void
2449raise_if_error(void)
2450{
2451 if (vim_error_check())
2452 raise_vim_exn(NULL);
2453}
2454
2455/* get buffer:
2456 * either current
2457 * or passed as argv[argnum] with checks
2458 */
2459 static vim_mz_buffer *
2460get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
2461{
2462 vim_mz_buffer *b;
2463
2464 if (argc < argnum + 1)
2465 return get_vim_curr_buffer();
2466 if (!SCHEME_VIMBUFFERP(argv[argnum]))
2467 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
2468 b = (vim_mz_buffer *)argv[argnum];
2469 (void)get_valid_buffer(argv[argnum]);
2470 return b;
2471}
2472
2473/* get window:
2474 * either current
2475 * or passed as argv[argnum] with checks
2476 */
2477 static vim_mz_window *
2478get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
2479{
2480 vim_mz_window *w;
2481
2482 if (argc < argnum + 1)
2483 return get_vim_curr_window();
2484 w = (vim_mz_window *)argv[argnum];
2485 if (!SCHEME_VIMWINDOWP(argv[argnum]))
2486 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
2487 (void)get_valid_window(argv[argnum]);
2488 return w;
2489}
2490
2491/* get valid Vim buffer from Scheme_Object* */
2492buf_T *get_valid_buffer(void *obj)
2493{
2494 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
2495
2496 if (buf == INVALID_BUFFER_VALUE)
2497 scheme_signal_error(_("buffer is invalid"));
2498 return buf;
2499}
2500
2501/* get valid Vim window from Scheme_Object* */
2502win_T *get_valid_window(void *obj)
2503{
2504 win_T *win = ((vim_mz_window *)obj)->win;
2505 if (win == INVALID_WINDOW_VALUE)
2506 scheme_signal_error(_("window is invalid"));
2507 return win;
2508}
2509
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002510 int
2511mzthreads_allowed(void)
2512{
2513 return mz_threads_allow;
2514}
2515
2516 static int
2517line_in_range(linenr_T lnum, buf_T *buf)
2518{
2519 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
2520}
2521
2522 static void
2523check_line_range(linenr_T lnum, buf_T *buf)
2524{
2525 if (!line_in_range(lnum, buf))
2526 scheme_signal_error(_("linenr out of range"));
2527}
2528
2529/*
2530 * Check if deleting lines made the cursor position invalid
2531 * (or you'll get msg from Vim about invalid linenr).
2532 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2533 * deleted). Got from if_python.c
2534 */
2535 static void
2536mz_fix_cursor(int lo, int hi, int extra)
2537{
2538 if (curwin->w_cursor.lnum >= lo)
2539 {
2540 /* Adjust the cursor position if it's in/after the changed
2541 * lines. */
2542 if (curwin->w_cursor.lnum >= hi)
2543 {
2544 curwin->w_cursor.lnum += extra;
2545 check_cursor_col();
2546 }
2547 else if (extra < 0)
2548 {
2549 curwin->w_cursor.lnum = lo;
2550 check_cursor();
2551 }
2552 changed_cline_bef_curs();
2553 }
2554 invalidate_botline();
2555}
2556
2557static Vim_Prim prims[]=
2558{
2559 /*
2560 * Buffer-related commands
2561 */
2562 {get_buffer_line, "get-buff-line", 1, 2},
2563 {set_buffer_line, "set-buff-line", 2, 3},
2564 {get_buffer_line_list, "get-buff-line-list", 2, 3},
2565 {get_buffer_name, "get-buff-name", 0, 1},
2566 {get_buffer_num, "get-buff-num", 0, 1},
2567 {get_buffer_size, "get-buff-size", 0, 1},
2568 {set_buffer_line_list, "set-buff-line-list", 3, 4},
2569 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
2570 {get_curr_buffer, "curr-buff", 0, 0},
2571 {get_buffer_count, "buff-count", 0, 0},
2572 {get_next_buffer, "get-next-buff", 0, 1},
2573 {get_prev_buffer, "get-prev-buff", 0, 1},
2574 {mzscheme_open_buffer, "open-buff", 1, 1},
2575 {get_buffer_by_name, "get-buff-by-name", 1, 1},
2576 {get_buffer_by_num, "get-buff-by-num", 1, 1},
2577 {get_buffer_namespace, "get-buff-namespace", 0, 1},
2578 /*
2579 * Window-related commands
2580 */
2581 {get_curr_win, "curr-win", 0, 0},
2582 {get_window_count, "win-count", 0, 0},
2583 {get_window_by_num, "get-win-by-num", 1, 1},
2584 {get_window_num, "get-win-num", 0, 1},
2585 {get_window_buffer, "get-win-buffer", 0, 1},
2586 {get_window_height, "get-win-height", 0, 1},
2587 {set_window_height, "set-win-height", 1, 2},
2588#ifdef FEAT_VERTSPLIT
2589 {get_window_width, "get-win-width", 0, 1},
2590 {set_window_width, "set-win-width", 1, 2},
2591#endif
2592 {get_cursor, "get-cursor", 0, 1},
2593 {set_cursor, "set-cursor", 1, 2},
2594 {get_window_list, "get-win-list", 0, 1},
2595 /*
2596 * Vim-related commands
2597 */
2598 {vim_command, "command", 1, 1},
2599 {vim_eval, "eval", 1, 1},
2600 {get_range_start, "range-start", 0, 0},
2601 {get_range_end, "range-end", 0, 0},
2602 {mzscheme_beep, "beep", 0, 0},
2603 {get_option, "get-option", 1, 2},
2604 {set_option, "set-option", 1, 2},
2605 /*
2606 * small utilities
2607 */
2608 {vim_bufferp, "buff?", 1, 1},
2609 {vim_windowp, "win?", 1, 1},
2610 {vim_buffer_validp, "buff-valid?", 1, 1},
2611 {vim_window_validp, "win-valid?", 1, 1}
2612};
2613
2614/* return MzScheme wrapper for curbuf */
2615 static vim_mz_buffer *
2616get_vim_curr_buffer(void)
2617{
Bram Moolenaare344bea2005-09-01 20:46:49 +00002618 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002619 return (vim_mz_buffer *)buffer_new(curbuf);
2620 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00002621 return (vim_mz_buffer *)curbuf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002622}
2623
2624/* return MzScheme wrapper for curwin */
2625 static vim_mz_window *
2626get_vim_curr_window(void)
2627{
Bram Moolenaare344bea2005-09-01 20:46:49 +00002628 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002629 return (vim_mz_window *)window_new(curwin);
2630 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00002631 return (vim_mz_window *)curwin->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002632}
2633
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002634 static void
2635make_modules(Scheme_Env *env)
2636{
2637 int i;
2638 Scheme_Env *mod;
2639
2640 mod = scheme_primitive_module(scheme_intern_symbol("vimext"), env);
2641 /* all prims made closed so they can access their own names */
2642 for (i = 0; i < sizeof(prims)/sizeof(prims[0]); i++)
2643 {
2644 Vim_Prim *prim = prims + i;
2645 scheme_add_global(prim->name,
2646 scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
2647 prim->mina, prim->maxa),
2648 mod);
2649 }
2650 scheme_add_global("global-namespace", (Scheme_Object *)environment, mod);
2651 scheme_finish_primitive_module(mod);
2652}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002653
Bram Moolenaar555b2802005-05-19 21:08:39 +00002654#ifdef HAVE_SANDBOX
2655static Scheme_Object *M_write = NULL;
2656static Scheme_Object *M_read = NULL;
2657static Scheme_Object *M_execute = NULL;
2658static Scheme_Object *M_delete = NULL;
2659
2660 static void
2661sandbox_check()
2662{
2663 if (sandbox)
2664 raise_vim_exn(_("not allowed in the Vim sandbox"));
2665}
2666
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002667/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00002668 static Scheme_Object *
2669sandbox_file_guard(int argc, Scheme_Object **argv)
2670{
2671 if (sandbox)
2672 {
2673 Scheme_Object *requested_access = argv[2];
2674
2675 if (M_write == NULL)
2676 {
2677 MZ_REGISTER_STATIC(M_write);
2678 M_write = scheme_intern_symbol("write");
2679 }
2680 if (M_read == NULL)
2681 {
2682 MZ_REGISTER_STATIC(M_read);
2683 M_read = scheme_intern_symbol("read");
2684 }
2685 if (M_execute == NULL)
2686 {
2687 MZ_REGISTER_STATIC(M_execute);
2688 M_execute = scheme_intern_symbol("execute");
2689 }
2690 if (M_delete == NULL)
2691 {
2692 MZ_REGISTER_STATIC(M_delete);
2693 M_delete = scheme_intern_symbol("delete");
2694 }
2695
2696 while (!SCHEME_NULLP(requested_access))
2697 {
2698 Scheme_Object *item = SCHEME_CAR(requested_access);
2699 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
2700 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
2701 {
2702 raise_vim_exn(_("not allowed in the Vim sandbox"));
2703 }
2704 requested_access = SCHEME_CDR(requested_access);
2705 }
2706 }
2707 return scheme_void;
2708}
2709
2710 static Scheme_Object *
2711sandbox_network_guard(int argc, Scheme_Object **argv)
2712{
2713 return scheme_void;
2714}
2715#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00002716
2717#endif