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