Merge branch 'vim' into fix/fast-join
[vim_extended.git] / src / if_mzsch.c
blob349945183e62780df119776d8ee16c06e60a90fd
1 /* vi:set ts=8 sts=4 sw=4:
3 * MzScheme interface by Sergey Khorev <sergey.khorev@gmail.com>
4 * Original work by Brent Fulgham <bfulgham@debian.org>
5 * (Based on lots of help from Matthew Flatt)
7 * TODO Convert byte-strings to char strings?
9 * This consists of six parts:
10 * 1. MzScheme interpreter main program
11 * 2. Routines that handle the external interface between MzScheme and
12 * Vim.
13 * 3. MzScheme input/output handlers: writes output via [e]msg().
14 * 4. Implementation of the Vim Features for MzScheme
15 * 5. Vim Window-related Manipulation Functions.
16 * 6. Vim Buffer-related Manipulation Functions
18 * NOTES
19 * 1. Memory, allocated with scheme_malloc*, need not to be freed explicitly,
20 * garbage collector will do it self
21 * 2. Requires at least NORMAL features. I can't imagine why one may want
22 * to build with SMALL or TINY features but with MzScheme interface.
23 * 3. I don't use K&R-style functions. Anyways, MzScheme headers are ANSI.
26 #include "vim.h"
28 #include "if_mzsch.h"
30 /* Only do the following when the feature is enabled. Needed for "make
31 * depend". */
32 #if defined(FEAT_MZSCHEME) || defined(PROTO)
34 #include <assert.h>
36 /* Base data structures */
37 #define SCHEME_VIMBUFFERP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_buffer_type)
38 #define SCHEME_VIMWINDOWP(obj) SAME_TYPE(SCHEME_TYPE(obj), mz_window_type)
40 typedef struct
42 Scheme_Object so;
43 buf_T *buf;
44 } vim_mz_buffer;
46 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
48 typedef struct
50 Scheme_Object so;
51 win_T *win;
52 } vim_mz_window;
54 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
57 * Prims that form MzScheme Vim interface
59 typedef struct
61 Scheme_Closed_Prim *prim;
62 char *name;
63 int mina; /* arity information */
64 int maxa;
65 } Vim_Prim;
67 typedef struct
69 char *name;
70 Scheme_Object *port;
71 } Port_Info;
74 *========================================================================
75 * Vim-Control Commands
76 *========================================================================
79 *========================================================================
80 * Utility functions for the vim/mzscheme interface
81 *========================================================================
83 #ifdef HAVE_SANDBOX
84 static Scheme_Object *sandbox_file_guard(int, Scheme_Object **);
85 static Scheme_Object *sandbox_network_guard(int, Scheme_Object **);
86 static void sandbox_check(void);
87 #endif
88 /* Buffer-related commands */
89 static Scheme_Object *buffer_new(buf_T *buf);
90 static Scheme_Object *get_buffer_by_name(void *, int, Scheme_Object **);
91 static Scheme_Object *get_buffer_by_num(void *, int, Scheme_Object **);
92 static Scheme_Object *get_buffer_count(void *, int, Scheme_Object **);
93 static Scheme_Object *get_buffer_line(void *, int, Scheme_Object **);
94 static Scheme_Object *get_buffer_line_list(void *, int, Scheme_Object **);
95 static Scheme_Object *get_buffer_name(void *, int, Scheme_Object **);
96 static Scheme_Object *get_buffer_num(void *, int, Scheme_Object **);
97 static Scheme_Object *get_buffer_size(void *, int, Scheme_Object **);
98 static Scheme_Object *get_curr_buffer(void *, int, Scheme_Object **);
99 static Scheme_Object *get_next_buffer(void *, int, Scheme_Object **);
100 static Scheme_Object *get_prev_buffer(void *, int, Scheme_Object **);
101 static Scheme_Object *mzscheme_open_buffer(void *, int, Scheme_Object **);
102 static Scheme_Object *set_buffer_line(void *, int, Scheme_Object **);
103 static Scheme_Object *set_buffer_line_list(void *, int, Scheme_Object **);
104 static Scheme_Object *insert_buffer_line_list(void *, int, Scheme_Object **);
105 static Scheme_Object *get_range_start(void *, int, Scheme_Object **);
106 static Scheme_Object *get_range_end(void *, int, Scheme_Object **);
107 static vim_mz_buffer *get_vim_curr_buffer(void);
109 /* Window-related commands */
110 static Scheme_Object *window_new(win_T *win);
111 static Scheme_Object *get_curr_win(void *, int, Scheme_Object **);
112 static Scheme_Object *get_window_count(void *, int, Scheme_Object **);
113 static Scheme_Object *get_window_by_num(void *, int, Scheme_Object **);
114 static Scheme_Object *get_window_num(void *, int, Scheme_Object **);
115 static Scheme_Object *get_window_buffer(void *, int, Scheme_Object **);
116 static Scheme_Object *get_window_height(void *, int, Scheme_Object **);
117 static Scheme_Object *set_window_height(void *, int, Scheme_Object **);
118 #ifdef FEAT_VERTSPLIT
119 static Scheme_Object *get_window_width(void *, int, Scheme_Object **);
120 static Scheme_Object *set_window_width(void *, int, Scheme_Object **);
121 #endif
122 static Scheme_Object *get_cursor(void *, int, Scheme_Object **);
123 static Scheme_Object *set_cursor(void *, int, Scheme_Object **);
124 static Scheme_Object *get_window_list(void *, int, Scheme_Object **);
125 static vim_mz_window *get_vim_curr_window(void);
127 /* Vim-related commands */
128 static Scheme_Object *mzscheme_beep(void *, int, Scheme_Object **);
129 static Scheme_Object *get_option(void *, int, Scheme_Object **);
130 static Scheme_Object *set_option(void *, int, Scheme_Object **);
131 static Scheme_Object *vim_command(void *, int, Scheme_Object **);
132 static Scheme_Object *vim_eval(void *, int, Scheme_Object **);
133 static Scheme_Object *vim_bufferp(void *data, int, Scheme_Object **);
134 static Scheme_Object *vim_windowp(void *data, int, Scheme_Object **);
135 static Scheme_Object *vim_buffer_validp(void *data, int, Scheme_Object **);
136 static Scheme_Object *vim_window_validp(void *data, int, Scheme_Object **);
139 *========================================================================
140 * Internal Function Prototypes
141 *========================================================================
143 static int vim_error_check(void);
144 static int do_mzscheme_command(exarg_T *, void *, Scheme_Closed_Prim *what);
145 static void startup_mzscheme(void);
146 static char *string_to_line(Scheme_Object *obj);
147 static void do_output(char *mesg, long len);
148 static void do_printf(char *format, ...);
149 static void do_flush(void);
150 static Scheme_Object *_apply_thunk_catch_exceptions(
151 Scheme_Object *, Scheme_Object **);
152 static Scheme_Object *extract_exn_message(Scheme_Object *v);
153 static Scheme_Object *do_eval(void *, int noargc, Scheme_Object **noargv);
154 static Scheme_Object *do_load(void *, int noargc, Scheme_Object **noargv);
155 static void register_vim_exn(void);
156 static vim_mz_buffer *get_buffer_arg(const char *fname, int argnum,
157 int argc, Scheme_Object **argv);
158 static vim_mz_window *get_window_arg(const char *fname, int argnum,
159 int argc, Scheme_Object **argv);
160 static int line_in_range(linenr_T, buf_T *);
161 static void check_line_range(linenr_T, buf_T *);
162 static void mz_fix_cursor(int lo, int hi, int extra);
164 static int eval_with_exn_handling(void *, Scheme_Closed_Prim *,
165 Scheme_Object **ret);
166 static void make_modules(void);
167 static void init_exn_catching_apply(void);
168 static int mzscheme_env_main(Scheme_Env *env, int argc, char **argv);
169 static int mzscheme_init(void);
170 #ifdef FEAT_EVAL
171 static Scheme_Object *vim_to_mzscheme(typval_T *vim_value, int depth,
172 Scheme_Hash_Table *visited);
173 #endif
175 #ifdef MZ_PRECISE_GC
176 static int buffer_size_proc(void *obj)
178 return gcBYTES_TO_WORDS(sizeof(vim_mz_buffer));
180 static int buffer_mark_proc(void *obj)
182 return buffer_size_proc(obj);
184 static int buffer_fixup_proc(void *obj)
186 return buffer_size_proc(obj);
188 static int window_size_proc(void *obj)
190 return gcBYTES_TO_WORDS(sizeof(vim_mz_window));
192 static int window_mark_proc(void *obj)
194 return window_size_proc(obj);
196 static int window_fixup_proc(void *obj)
198 return window_size_proc(obj);
200 #endif
202 #ifdef DYNAMIC_MZSCHEME
204 static Scheme_Object *dll_scheme_eof;
205 static Scheme_Object *dll_scheme_false;
206 static Scheme_Object *dll_scheme_void;
207 static Scheme_Object *dll_scheme_null;
208 static Scheme_Object *dll_scheme_true;
210 static Scheme_Thread **dll_scheme_current_thread_ptr;
212 static void (**dll_scheme_console_printf_ptr)(char *str, ...);
213 static void (**dll_scheme_console_output_ptr)(char *str, long len);
214 static void (**dll_scheme_notify_multithread_ptr)(int on);
216 static void *(*dll_GC_malloc)(size_t size_in_bytes);
217 static void *(*dll_GC_malloc_atomic)(size_t size_in_bytes);
218 static Scheme_Env *(*dll_scheme_basic_env)(void);
219 static void (*dll_scheme_check_threads)(void);
220 static void (*dll_scheme_register_static)(void *ptr, long size);
221 static void (*dll_scheme_set_stack_base)(void *base, int no_auto_statics);
222 static void (*dll_scheme_add_global)(const char *name, Scheme_Object *val,
223 Scheme_Env *env);
224 static void (*dll_scheme_add_global_symbol)(Scheme_Object *name,
225 Scheme_Object *val, Scheme_Env *env);
226 static Scheme_Object *(*dll_scheme_apply)(Scheme_Object *rator, int num_rands,
227 Scheme_Object **rands);
228 static Scheme_Object *(*dll_scheme_builtin_value)(const char *name);
229 # if MZSCHEME_VERSION_MAJOR >= 299
230 static Scheme_Object *(*dll_scheme_byte_string_to_char_string)(Scheme_Object *s);
231 # endif
232 static void (*dll_scheme_close_input_port)(Scheme_Object *port);
233 static void (*dll_scheme_count_lines)(Scheme_Object *port);
234 #if MZSCHEME_VERSION_MAJOR < 360
235 static Scheme_Object *(*dll_scheme_current_continuation_marks)(void);
236 #else
237 static Scheme_Object *(*dll_scheme_current_continuation_marks)(Scheme_Object *prompt_tag);
238 #endif
239 static void (*dll_scheme_display)(Scheme_Object *obj, Scheme_Object *port);
240 static char *(*dll_scheme_display_to_string)(Scheme_Object *obj, long *len);
241 static int (*dll_scheme_eq)(Scheme_Object *obj1, Scheme_Object *obj2);
242 static Scheme_Object *(*dll_scheme_do_eval)(Scheme_Object *obj,
243 int _num_rands, Scheme_Object **rands, int val);
244 static void (*dll_scheme_dont_gc_ptr)(void *p);
245 static Scheme_Object *(*dll_scheme_eval)(Scheme_Object *obj, Scheme_Env *env);
246 static Scheme_Object *(*dll_scheme_eval_string)(const char *str,
247 Scheme_Env *env);
248 static Scheme_Object *(*dll_scheme_eval_string_all)(const char *str,
249 Scheme_Env *env, int all);
250 static void (*dll_scheme_finish_primitive_module)(Scheme_Env *env);
251 # if MZSCHEME_VERSION_MAJOR < 299
252 static char *(*dll_scheme_format)(char *format, int flen, int argc,
253 Scheme_Object **argv, long *rlen);
254 # else
255 static char *(*dll_scheme_format_utf8)(char *format, int flen, int argc,
256 Scheme_Object **argv, long *rlen);
257 static Scheme_Object *(*dll_scheme_get_param)(Scheme_Config *c, int pos);
258 # endif
259 static void (*dll_scheme_gc_ptr_ok)(void *p);
260 # if MZSCHEME_VERSION_MAJOR < 299
261 static char *(*dll_scheme_get_sized_string_output)(Scheme_Object *,
262 long *len);
263 # else
264 static char *(*dll_scheme_get_sized_byte_string_output)(Scheme_Object *,
265 long *len);
266 # endif
267 static Scheme_Object *(*dll_scheme_intern_symbol)(const char *name);
268 static Scheme_Object *(*dll_scheme_lookup_global)(Scheme_Object *symbol,
269 Scheme_Env *env);
270 static Scheme_Object *(*dll_scheme_make_closed_prim_w_arity)
271 (Scheme_Closed_Prim *prim, void *data, const char *name, mzshort mina,
272 mzshort maxa);
273 static Scheme_Object *(*dll_scheme_make_integer_value)(long i);
274 static Scheme_Object *(*dll_scheme_make_pair)(Scheme_Object *car,
275 Scheme_Object *cdr);
276 static Scheme_Object *(*dll_scheme_make_prim_w_arity)(Scheme_Prim *prim,
277 const char *name, mzshort mina, mzshort maxa);
278 # if MZSCHEME_VERSION_MAJOR < 299
279 static Scheme_Object *(*dll_scheme_make_string)(const char *chars);
280 static Scheme_Object *(*dll_scheme_make_string_output_port)();
281 # else
282 static Scheme_Object *(*dll_scheme_make_byte_string)(const char *chars);
283 static Scheme_Object *(*dll_scheme_make_byte_string_output_port)();
284 # endif
285 static Scheme_Object *(*dll_scheme_make_struct_instance)(Scheme_Object *stype,
286 int argc, Scheme_Object **argv);
287 static Scheme_Object **(*dll_scheme_make_struct_names)(Scheme_Object *base,
288 Scheme_Object *field_names, int flags, int *count_out);
289 static Scheme_Object *(*dll_scheme_make_struct_type)(Scheme_Object *base,
290 Scheme_Object *parent, Scheme_Object *inspector, int num_fields,
291 int num_uninit_fields, Scheme_Object *uninit_val,
292 Scheme_Object *properties
293 # if MZSCHEME_VERSION_MAJOR >= 299
294 , Scheme_Object *guard
295 # endif
297 static Scheme_Object **(*dll_scheme_make_struct_values)(
298 Scheme_Object *struct_type, Scheme_Object **names, int count,
299 int flags);
300 static Scheme_Type (*dll_scheme_make_type)(const char *name);
301 static Scheme_Object *(*dll_scheme_make_vector)(int size,
302 Scheme_Object *fill);
303 static void *(*dll_scheme_malloc_fail_ok)(void *(*f)(size_t), size_t);
304 static Scheme_Object *(*dll_scheme_open_input_file)(const char *name,
305 const char *who);
306 static Scheme_Env *(*dll_scheme_primitive_module)(Scheme_Object *name,
307 Scheme_Env *for_env);
308 static int (*dll_scheme_proper_list_length)(Scheme_Object *list);
309 static void (*dll_scheme_raise)(Scheme_Object *exn);
310 static Scheme_Object *(*dll_scheme_read)(Scheme_Object *port);
311 static void (*dll_scheme_signal_error)(const char *msg, ...);
312 static void (*dll_scheme_wrong_type)(const char *name, const char *expected,
313 int which, int argc, Scheme_Object **argv);
314 # if MZSCHEME_VERSION_MAJOR >= 299
315 static void (*dll_scheme_set_param)(Scheme_Config *c, int pos,
316 Scheme_Object *o);
317 static Scheme_Config *(*dll_scheme_current_config)(void);
318 static Scheme_Object *(*dll_scheme_char_string_to_byte_string)
319 (Scheme_Object *s);
320 static Scheme_Object *(*dll_scheme_char_string_to_path)
321 (Scheme_Object *s);
322 # endif
323 static Scheme_Hash_Table *(*dll_scheme_make_hash_table)(int type);
324 static void (*dll_scheme_hash_set)(Scheme_Hash_Table *table,
325 Scheme_Object *key, Scheme_Object *value);
326 static Scheme_Object *(*dll_scheme_hash_get)(Scheme_Hash_Table *table,
327 Scheme_Object *key);
328 static Scheme_Object *(*dll_scheme_make_double)(double d);
329 # ifdef INCLUDE_MZSCHEME_BASE
330 static Scheme_Object *(*dll_scheme_make_sized_byte_string)(char *chars,
331 long len, int copy);
332 static Scheme_Object *(*dll_scheme_namespace_require)(Scheme_Object *req);
333 # endif
335 /* arrays are imported directly */
336 # define scheme_eof dll_scheme_eof
337 # define scheme_false dll_scheme_false
338 # define scheme_void dll_scheme_void
339 # define scheme_null dll_scheme_null
340 # define scheme_true dll_scheme_true
342 /* pointers are GetProceAddress'ed as pointers to pointer */
343 # define scheme_current_thread (*dll_scheme_current_thread_ptr)
344 # define scheme_console_printf (*dll_scheme_console_printf_ptr)
345 # define scheme_console_output (*dll_scheme_console_output_ptr)
346 # define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
348 /* and functions in a usual way */
349 # define GC_malloc dll_GC_malloc
350 # define GC_malloc_atomic dll_GC_malloc_atomic
352 # define scheme_add_global dll_scheme_add_global
353 # define scheme_add_global_symbol dll_scheme_add_global_symbol
354 # define scheme_apply dll_scheme_apply
355 # define scheme_basic_env dll_scheme_basic_env
356 # define scheme_builtin_value dll_scheme_builtin_value
357 # if MZSCHEME_VERSION_MAJOR >= 299
358 # define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
359 # endif
360 # define scheme_check_threads dll_scheme_check_threads
361 # define scheme_close_input_port dll_scheme_close_input_port
362 # define scheme_count_lines dll_scheme_count_lines
363 # define scheme_current_continuation_marks \
364 dll_scheme_current_continuation_marks
365 # define scheme_display dll_scheme_display
366 # define scheme_display_to_string dll_scheme_display_to_string
367 # define scheme_do_eval dll_scheme_do_eval
368 # define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
369 # define scheme_eq dll_scheme_eq
370 # define scheme_eval dll_scheme_eval
371 # define scheme_eval_string dll_scheme_eval_string
372 # define scheme_eval_string_all dll_scheme_eval_string_all
373 # define scheme_finish_primitive_module dll_scheme_finish_primitive_module
374 # if MZSCHEME_VERSION_MAJOR < 299
375 # define scheme_format dll_scheme_format
376 # else
377 # define scheme_format_utf8 dll_scheme_format_utf8
378 # endif
379 # define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
380 # if MZSCHEME_VERSION_MAJOR < 299
381 # define scheme_get_sized_string_output dll_scheme_get_sized_string_output
382 # else
383 # define scheme_get_sized_byte_string_output \
384 dll_scheme_get_sized_byte_string_output
385 # define scheme_get_param dll_scheme_get_param
386 # endif
387 # define scheme_intern_symbol dll_scheme_intern_symbol
388 # define scheme_lookup_global dll_scheme_lookup_global
389 # define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
390 # define scheme_make_integer_value dll_scheme_make_integer_value
391 # define scheme_make_pair dll_scheme_make_pair
392 # define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
393 # if MZSCHEME_VERSION_MAJOR < 299
394 # define scheme_make_string dll_scheme_make_string
395 # define scheme_make_string_output_port dll_scheme_make_string_output_port
396 # else
397 # define scheme_make_byte_string dll_scheme_make_byte_string
398 # define scheme_make_byte_string_output_port \
399 dll_scheme_make_byte_string_output_port
400 # endif
401 # define scheme_make_struct_instance dll_scheme_make_struct_instance
402 # define scheme_make_struct_names dll_scheme_make_struct_names
403 # define scheme_make_struct_type dll_scheme_make_struct_type
404 # define scheme_make_struct_values dll_scheme_make_struct_values
405 # define scheme_make_type dll_scheme_make_type
406 # define scheme_make_vector dll_scheme_make_vector
407 # define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
408 # define scheme_open_input_file dll_scheme_open_input_file
409 # define scheme_primitive_module dll_scheme_primitive_module
410 # define scheme_proper_list_length dll_scheme_proper_list_length
411 # define scheme_raise dll_scheme_raise
412 # define scheme_read dll_scheme_read
413 # define scheme_register_static dll_scheme_register_static
414 # define scheme_set_stack_base dll_scheme_set_stack_base
415 # define scheme_signal_error dll_scheme_signal_error
416 # define scheme_wrong_type dll_scheme_wrong_type
417 # if MZSCHEME_VERSION_MAJOR >= 299
418 # define scheme_set_param dll_scheme_set_param
419 # define scheme_current_config dll_scheme_current_config
420 # define scheme_char_string_to_byte_string \
421 dll_scheme_char_string_to_byte_string
422 # define scheme_char_string_to_path \
423 dll_scheme_char_string_to_path
424 # endif
425 # define scheme_make_hash_table dll_scheme_make_hash_table
426 # define scheme_hash_set dll_scheme_hash_set
427 # define scheme_hash_get dll_scheme_hash_get
428 # define scheme_make_double dll_scheme_make_double
429 # ifdef INCLUDE_MZSCHEME_BASE
430 # define scheme_make_sized_byte_string dll_scheme_make_sized_byte_string
431 # define scheme_namespace_require dll_scheme_namespace_require
432 # endif
434 typedef struct
436 char *name;
437 void **ptr;
438 } Thunk_Info;
440 static Thunk_Info mzgc_imports[] = {
441 {"GC_malloc", (void **)&dll_GC_malloc},
442 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
443 {NULL, NULL}};
445 static Thunk_Info mzsch_imports[] = {
446 {"scheme_eof", (void **)&dll_scheme_eof},
447 {"scheme_false", (void **)&dll_scheme_false},
448 {"scheme_void", (void **)&dll_scheme_void},
449 {"scheme_null", (void **)&dll_scheme_null},
450 {"scheme_true", (void **)&dll_scheme_true},
451 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
452 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
453 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
454 {"scheme_notify_multithread",
455 (void **)&dll_scheme_notify_multithread_ptr},
456 {"scheme_add_global", (void **)&dll_scheme_add_global},
457 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
458 {"scheme_apply", (void **)&dll_scheme_apply},
459 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
460 # if MZSCHEME_VERSION_MAJOR >= 299
461 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
462 # endif
463 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
464 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
465 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
466 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
467 {"scheme_current_continuation_marks",
468 (void **)&dll_scheme_current_continuation_marks},
469 {"scheme_display", (void **)&dll_scheme_display},
470 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
471 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
472 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
473 {"scheme_eq", (void **)&dll_scheme_eq},
474 {"scheme_eval", (void **)&dll_scheme_eval},
475 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
476 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
477 {"scheme_finish_primitive_module",
478 (void **)&dll_scheme_finish_primitive_module},
479 # if MZSCHEME_VERSION_MAJOR < 299
480 {"scheme_format", (void **)&dll_scheme_format},
481 # else
482 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
483 {"scheme_get_param", (void **)&dll_scheme_get_param},
484 #endif
485 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
486 # if MZSCHEME_VERSION_MAJOR < 299
487 {"scheme_get_sized_string_output",
488 (void **)&dll_scheme_get_sized_string_output},
489 # else
490 {"scheme_get_sized_byte_string_output",
491 (void **)&dll_scheme_get_sized_byte_string_output},
492 #endif
493 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
494 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
495 {"scheme_make_closed_prim_w_arity",
496 (void **)&dll_scheme_make_closed_prim_w_arity},
497 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
498 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
499 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
500 # if MZSCHEME_VERSION_MAJOR < 299
501 {"scheme_make_string", (void **)&dll_scheme_make_string},
502 {"scheme_make_string_output_port",
503 (void **)&dll_scheme_make_string_output_port},
504 # else
505 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
506 {"scheme_make_byte_string_output_port",
507 (void **)&dll_scheme_make_byte_string_output_port},
508 # endif
509 {"scheme_make_struct_instance",
510 (void **)&dll_scheme_make_struct_instance},
511 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
512 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
513 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
514 {"scheme_make_type", (void **)&dll_scheme_make_type},
515 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
516 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
517 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
518 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
519 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
520 {"scheme_raise", (void **)&dll_scheme_raise},
521 {"scheme_read", (void **)&dll_scheme_read},
522 {"scheme_register_static", (void **)&dll_scheme_register_static},
523 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
524 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
525 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
526 # if MZSCHEME_VERSION_MAJOR >= 299
527 {"scheme_set_param", (void **)&dll_scheme_set_param},
528 {"scheme_current_config", (void **)&dll_scheme_current_config},
529 {"scheme_char_string_to_byte_string",
530 (void **)&dll_scheme_char_string_to_byte_string},
531 {"scheme_char_string_to_path", (void **)&dll_scheme_char_string_to_path},
532 # endif
533 {"scheme_make_hash_table", (void **)&dll_scheme_make_hash_table},
534 {"scheme_hash_set", (void **)&dll_scheme_hash_set},
535 {"scheme_hash_get", (void **)&dll_scheme_hash_get},
536 {"scheme_make_double", (void **)&dll_scheme_make_double},
537 # ifdef INCLUDE_MZSCHEME_BASE
538 {"scheme_make_sized_byte_string", (void **)&dll_scheme_make_sized_byte_string},
539 {"scheme_namespace_require", (void **)&dll_scheme_namespace_require},
540 #endif
541 {NULL, NULL}};
543 static HINSTANCE hMzGC = 0;
544 static HINSTANCE hMzSch = 0;
546 static void dynamic_mzscheme_end(void);
547 static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
548 int verbose);
550 static int
551 mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
553 Thunk_Info *thunk = NULL;
555 if (hMzGC && hMzSch)
556 return OK;
557 hMzSch = LoadLibrary(sch_dll);
558 hMzGC = LoadLibrary(gc_dll);
560 if (!hMzSch)
562 if (verbose)
563 EMSG2(_(e_loadlib), sch_dll);
564 return FAIL;
567 if (!hMzGC)
569 if (verbose)
570 EMSG2(_(e_loadlib), gc_dll);
571 return FAIL;
574 for (thunk = mzsch_imports; thunk->name; thunk++)
576 if ((*thunk->ptr =
577 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
579 FreeLibrary(hMzSch);
580 hMzSch = 0;
581 FreeLibrary(hMzGC);
582 hMzGC = 0;
583 if (verbose)
584 EMSG2(_(e_loadfunc), thunk->name);
585 return FAIL;
588 for (thunk = mzgc_imports; thunk->name; thunk++)
590 if ((*thunk->ptr =
591 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
593 FreeLibrary(hMzSch);
594 hMzSch = 0;
595 FreeLibrary(hMzGC);
596 hMzGC = 0;
597 if (verbose)
598 EMSG2(_(e_loadfunc), thunk->name);
599 return FAIL;
602 return OK;
606 mzscheme_enabled(int verbose)
608 return mzscheme_runtime_link_init(
609 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
612 static void
613 dynamic_mzscheme_end(void)
615 if (hMzSch)
617 FreeLibrary(hMzSch);
618 hMzSch = 0;
620 if (hMzGC)
622 FreeLibrary(hMzGC);
623 hMzGC = 0;
626 #endif /* DYNAMIC_MZSCHEME */
628 /* need to put it here for dynamic stuff to work */
629 #if defined(INCLUDE_MZSCHEME_BASE)
630 # include "mzscheme_base.c"
631 #elif MZSCHEME_VERSION_MAJOR >= 400
632 # error MzScheme 4.x must include mzscheme_base.c, for MinGW32 you need to define MZSCHEME_GENERATE_BASE=yes
633 #endif
636 *========================================================================
637 * 1. MzScheme interpreter startup
638 *========================================================================
641 static Scheme_Type mz_buffer_type;
642 static Scheme_Type mz_window_type;
644 static int initialized = FALSE;
646 /* global environment */
647 static Scheme_Env *environment = NULL;
648 /* output/error handlers */
649 static Scheme_Object *curout = NULL;
650 static Scheme_Object *curerr = NULL;
651 /* exn:vim exception */
652 static Scheme_Object *exn_catching_apply = NULL;
653 static Scheme_Object *exn_p = NULL;
654 static Scheme_Object *exn_message = NULL;
655 static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
657 #if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
658 static void *stack_base = NULL;
659 #endif
661 static long range_start;
662 static long range_end;
664 /* MzScheme threads scheduling stuff */
665 static int mz_threads_allow = 0;
667 #if defined(FEAT_GUI_W32)
668 static void CALLBACK timer_proc(HWND, UINT, UINT, DWORD);
669 static UINT timer_id = 0;
670 #elif defined(FEAT_GUI_GTK)
671 static gint timer_proc(gpointer);
672 static guint timer_id = 0;
673 #elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
674 static void timer_proc(XtPointer, XtIntervalId *);
675 static XtIntervalId timer_id = (XtIntervalId)0;
676 #elif defined(FEAT_GUI_MAC)
677 pascal void timer_proc(EventLoopTimerRef, void *);
678 static EventLoopTimerRef timer_id = NULL;
679 static EventLoopTimerUPP timerUPP;
680 #endif
682 #ifndef FEAT_GUI_W32 /* Win32 console and Unix */
683 void
684 mzvim_check_threads(void)
686 /* Last time MzScheme threads were scheduled */
687 static time_t mz_last_time = 0;
689 if (mz_threads_allow && p_mzq > 0)
691 time_t now = time(NULL);
693 if ((now - mz_last_time) * 1000 > p_mzq)
695 mz_last_time = now;
696 scheme_check_threads();
700 #endif
702 #ifdef MZSCHEME_GUI_THREADS
703 static void setup_timer(void);
704 static void remove_timer(void);
706 /* timers are presented in GUI only */
707 # if defined(FEAT_GUI_W32)
708 static void CALLBACK
709 timer_proc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
710 # elif defined(FEAT_GUI_GTK)
711 static gint
712 timer_proc(gpointer data)
713 # elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
714 static void
715 timer_proc(XtPointer timed_out, XtIntervalId *interval_id)
716 # elif defined(FEAT_GUI_MAC)
717 pascal void
718 timer_proc(EventLoopTimerRef theTimer, void *userData)
719 # endif
721 scheme_check_threads();
722 # if defined(FEAT_GUI_GTK)
723 return TRUE; /* continue receiving notifications */
724 # elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
725 /* renew timeout */
726 if (mz_threads_allow && p_mzq > 0)
727 timer_id = XtAppAddTimeOut(app_context, p_mzq,
728 timer_proc, NULL);
729 # endif
732 static void
733 setup_timer(void)
735 # if defined(FEAT_GUI_W32)
736 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
737 # elif defined(FEAT_GUI_GTK)
738 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
739 # elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
740 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
741 # elif defined(FEAT_GUI_MAC)
742 timerUPP = NewEventLoopTimerUPP(timer_proc);
743 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
744 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
745 # endif
748 static void
749 remove_timer(void)
751 # if defined(FEAT_GUI_W32)
752 KillTimer(NULL, timer_id);
753 # elif defined(FEAT_GUI_GTK)
754 gtk_timeout_remove(timer_id);
755 # elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
756 XtRemoveTimeOut(timer_id);
757 # elif defined(FEAT_GUI_MAC)
758 RemoveEventLoopTimer(timer_id);
759 DisposeEventLoopTimerUPP(timerUPP);
760 # endif
761 timer_id = 0;
764 void
765 mzvim_reset_timer(void)
767 if (timer_id != 0)
768 remove_timer();
769 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
770 setup_timer();
773 #endif /* MZSCHEME_GUI_THREADS */
775 static void
776 notify_multithread(int on)
778 mz_threads_allow = on;
779 #ifdef MZSCHEME_GUI_THREADS
780 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
781 setup_timer();
782 if (!on && timer_id != 0)
783 remove_timer();
784 #endif
787 void
788 mzscheme_end(void)
790 #ifdef DYNAMIC_MZSCHEME
791 dynamic_mzscheme_end();
792 #endif
795 void
796 mzscheme_main(void)
798 #if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR >= 400
799 /* use trampoline for precise GC in MzScheme >= 4.x */
800 scheme_main_setup(TRUE, mzscheme_env_main, 0, NULL);
801 #else
802 mzscheme_env_main(NULL, 0, NULL);
803 #endif
806 static int
807 mzscheme_env_main(Scheme_Env *env, int argc, char **argv)
809 /* neither argument nor return values are used */
810 #ifdef MZ_PRECISE_GC
811 # if MZSCHEME_VERSION_MAJOR < 400
813 * Starting from version 4.x, embedding applications must use
814 * scheme_main_setup/scheme_main_stack_setup trampolines
815 * rather than setting stack base directly with scheme_set_stack_base
817 Scheme_Object *dummy = NULL;
818 MZ_GC_DECL_REG(1);
819 MZ_GC_VAR_IN_REG(0, dummy);
821 stack_base = &__gc_var_stack__;
822 # else
823 /* environment has been created by us by Scheme */
824 environment = env;
825 # endif
827 * In 4.x, all activities must be performed inside trampoline
828 * so we are forced to initialise GC immediately
829 * This can be postponed in 3.x but I see no point in implementing
830 * a feature which will work in older versions only.
831 * One would better use conservative GC if he needs dynamic MzScheme
833 mzscheme_init();
834 #else
835 int dummy = 0;
836 stack_base = (void *)&dummy;
837 #endif
838 main_loop(FALSE, FALSE);
839 #if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR < 400
840 /* releasing dummy */
841 MZ_GC_REG();
842 MZ_GC_UNREG();
843 #endif
844 return 0;
847 static void
848 startup_mzscheme(void)
850 #if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
851 scheme_set_stack_base(stack_base, 1);
852 #endif
854 MZ_REGISTER_STATIC(environment);
855 MZ_REGISTER_STATIC(curout);
856 MZ_REGISTER_STATIC(curerr);
857 MZ_REGISTER_STATIC(exn_catching_apply);
858 MZ_REGISTER_STATIC(exn_p);
859 MZ_REGISTER_STATIC(exn_message);
860 MZ_REGISTER_STATIC(vim_exn);
862 #if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
863 /* in newer versions of precise GC the initial env has been created */
864 environment = scheme_basic_env();
865 #endif
866 MZ_GC_CHECK();
868 #ifdef INCLUDE_MZSCHEME_BASE
871 * versions 4.x do not provide Scheme bindings by default
872 * we need to add them explicitly
874 Scheme_Object *scheme_base_symbol = NULL;
875 MZ_GC_DECL_REG(1);
876 MZ_GC_VAR_IN_REG(0, scheme_base_symbol);
877 MZ_GC_REG();
878 /* invoke function from generated and included mzscheme_base.c */
879 declare_modules(environment);
880 scheme_base_symbol = scheme_intern_symbol("scheme/base");
881 MZ_GC_CHECK();
882 scheme_namespace_require(scheme_base_symbol);
883 MZ_GC_CHECK();
884 MZ_GC_UNREG();
886 #endif
887 register_vim_exn();
888 /* use new environment to initialise exception handling */
889 init_exn_catching_apply();
891 /* redirect output */
892 scheme_console_output = do_output;
893 scheme_console_printf = do_printf;
895 #ifdef MZSCHEME_COLLECTS
896 /* setup 'current-library-collection-paths' parameter */
897 # if MZSCHEME_VERSION_MAJOR >= 299
899 Scheme_Object *coll_byte_string = NULL;
900 Scheme_Object *coll_char_string = NULL;
901 Scheme_Object *coll_path = NULL;
902 Scheme_Object *coll_pair = NULL;
903 Scheme_Config *config = NULL;
905 MZ_GC_DECL_REG(5);
906 MZ_GC_VAR_IN_REG(0, coll_byte_string);
907 MZ_GC_VAR_IN_REG(1, coll_char_string);
908 MZ_GC_VAR_IN_REG(2, coll_path);
909 MZ_GC_VAR_IN_REG(3, coll_pair);
910 MZ_GC_VAR_IN_REG(4, config);
911 MZ_GC_REG();
912 coll_byte_string = scheme_make_byte_string(MZSCHEME_COLLECTS);
913 MZ_GC_CHECK();
914 coll_char_string = scheme_byte_string_to_char_string(coll_byte_string);
915 MZ_GC_CHECK();
916 coll_path = scheme_char_string_to_path(coll_char_string);
917 MZ_GC_CHECK();
918 coll_pair = scheme_make_pair(coll_path, scheme_null);
919 MZ_GC_CHECK();
920 config = scheme_config;
921 MZ_GC_CHECK();
922 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
923 MZ_GC_CHECK();
924 MZ_GC_UNREG();
926 # else
928 Scheme_Object *coll_string = NULL;
929 Scheme_Object *coll_pair = NULL;
930 Scheme_Config *config = NULL;
932 MZ_GC_DECL_REG(3);
933 MZ_GC_VAR_IN_REG(0, coll_string);
934 MZ_GC_VAR_IN_REG(1, coll_pair);
935 MZ_GC_VAR_IN_REG(2, config);
936 MZ_GC_REG();
937 coll_string = scheme_make_string(MZSCHEME_COLLECTS);
938 MZ_GC_CHECK();
939 coll_pair = scheme_make_pair(coll_string, scheme_null);
940 MZ_GC_CHECK();
941 config = scheme_config;
942 MZ_GC_CHECK();
943 scheme_set_param(config, MZCONFIG_COLLECTION_PATHS, coll_pair);
944 MZ_GC_CHECK();
945 MZ_GC_UNREG();
947 # endif
948 #endif
949 #ifdef HAVE_SANDBOX
951 Scheme_Object *make_security_guard = NULL;
952 MZ_GC_DECL_REG(1);
953 MZ_GC_VAR_IN_REG(0, make_security_guard);
954 MZ_GC_REG();
956 #if MZSCHEME_VERSION_MAJOR < 400
958 Scheme_Object *make_security_guard_symbol = NULL;
959 MZ_GC_DECL_REG(1);
960 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol);
961 MZ_GC_REG();
962 make_security_guard_symbol = scheme_intern_symbol("make-security-guard");
963 MZ_GC_CHECK();
964 make_security_guard = scheme_lookup_global(
965 make_security_guard_symbol, environment);
966 MZ_GC_UNREG();
968 #else
969 make_security_guard = scheme_builtin_value("make-security-guard");
970 MZ_GC_CHECK();
971 #endif
973 /* setup sandbox guards */
974 if (make_security_guard != NULL)
976 Scheme_Object *args[3] = {NULL, NULL, NULL};
977 Scheme_Object *guard = NULL;
978 Scheme_Config *config = NULL;
979 MZ_GC_DECL_REG(5);
980 MZ_GC_ARRAY_VAR_IN_REG(0, args, 3);
981 MZ_GC_VAR_IN_REG(3, guard);
982 MZ_GC_VAR_IN_REG(4, config);
983 MZ_GC_REG();
984 config = scheme_config;
985 MZ_GC_CHECK();
986 args[0] = scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
987 MZ_GC_CHECK();
988 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
989 "sandbox-file-guard", 3, 3);
990 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
991 "sandbox-network-guard", 4, 4);
992 guard = scheme_apply(make_security_guard, 3, args);
993 MZ_GC_CHECK();
994 scheme_set_param(config, MZCONFIG_SECURITY_GUARD, guard);
995 MZ_GC_CHECK();
996 MZ_GC_UNREG();
998 MZ_GC_UNREG();
1000 #endif
1001 /* Create buffer and window types for use in Scheme code */
1002 mz_buffer_type = scheme_make_type("<vim-buffer>");
1003 MZ_GC_CHECK();
1004 mz_window_type = scheme_make_type("<vim-window>");
1005 MZ_GC_CHECK();
1006 #ifdef MZ_PRECISE_GC
1007 GC_register_traversers(mz_buffer_type,
1008 buffer_size_proc, buffer_mark_proc, buffer_fixup_proc,
1009 TRUE, TRUE);
1010 GC_register_traversers(mz_window_type,
1011 window_size_proc, window_mark_proc, window_fixup_proc,
1012 TRUE, TRUE);
1013 #endif
1015 make_modules();
1018 * setup callback to receive notifications
1019 * whether thread scheduling is (or not) required
1021 scheme_notify_multithread = notify_multithread;
1025 * This routine is called for each new invocation of MzScheme
1026 * to make sure things are properly initialized.
1028 static int
1029 mzscheme_init(void)
1031 if (!initialized)
1033 #ifdef DYNAMIC_MZSCHEME
1034 if (!mzscheme_enabled(TRUE))
1036 EMSG(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
1037 return -1;
1039 #endif
1040 startup_mzscheme();
1041 initialized = TRUE;
1044 Scheme_Config *config = NULL;
1045 MZ_GC_DECL_REG(1);
1046 MZ_GC_VAR_IN_REG(0, config);
1047 MZ_GC_REG();
1048 config = scheme_config;
1049 MZ_GC_CHECK();
1050 /* recreate ports each call effectively clearing these ones */
1051 curout = scheme_make_string_output_port();
1052 MZ_GC_CHECK();
1053 curerr = scheme_make_string_output_port();
1054 MZ_GC_CHECK();
1055 scheme_set_param(config, MZCONFIG_OUTPUT_PORT, curout);
1056 MZ_GC_CHECK();
1057 scheme_set_param(config, MZCONFIG_ERROR_PORT, curerr);
1058 MZ_GC_CHECK();
1059 MZ_GC_UNREG();
1062 return 0;
1066 *========================================================================
1067 * 2. External Interface
1068 *========================================================================
1072 * Evaluate command with exception handling
1074 static int
1075 eval_with_exn_handling(void *data, Scheme_Closed_Prim *what, Scheme_Object **ret)
1077 Scheme_Object *value = NULL;
1078 Scheme_Object *exn = NULL;
1079 Scheme_Object *prim = NULL;
1081 MZ_GC_DECL_REG(3);
1082 MZ_GC_VAR_IN_REG(0, value);
1083 MZ_GC_VAR_IN_REG(1, exn);
1084 MZ_GC_VAR_IN_REG(2, prim);
1085 MZ_GC_REG();
1087 prim = scheme_make_closed_prim_w_arity(what, data, "mzvim", 0, 0);
1088 MZ_GC_CHECK();
1089 value = _apply_thunk_catch_exceptions(prim, &exn);
1090 MZ_GC_CHECK();
1092 if (!value)
1094 value = extract_exn_message(exn);
1095 /* Got an exn? */
1096 if (value)
1098 scheme_display(value, curerr); /* Send to stderr-vim */
1099 MZ_GC_CHECK();
1100 do_flush();
1102 MZ_GC_UNREG();
1103 /* `raise' was called on some arbitrary value */
1104 return FAIL;
1107 if (ret != NULL) /* if pointer to retval supported give it up */
1108 *ret = value;
1109 /* Print any result, as long as it's not a void */
1110 else if (!SCHEME_VOIDP(value))
1112 scheme_display(value, curout); /* Send to stdout-vim */
1113 MZ_GC_CHECK();
1116 do_flush();
1117 MZ_GC_UNREG();
1118 return OK;
1121 /* :mzscheme */
1122 static int
1123 do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
1125 if (mzscheme_init())
1126 return FAIL;
1128 range_start = eap->line1;
1129 range_end = eap->line2;
1131 return eval_with_exn_handling(data, what, NULL);
1135 * Routine called by VIM when deleting a buffer
1137 void
1138 mzscheme_buffer_free(buf_T *buf)
1140 if (buf->b_mzscheme_ref)
1142 vim_mz_buffer *bp;
1144 bp = buf->b_mzscheme_ref;
1145 bp->buf = INVALID_BUFFER_VALUE;
1146 buf->b_mzscheme_ref = NULL;
1147 scheme_gc_ptr_ok(bp);
1148 MZ_GC_CHECK();
1153 * Routine called by VIM when deleting a Window
1155 void
1156 mzscheme_window_free(win_T *win)
1158 if (win->w_mzscheme_ref)
1160 vim_mz_window *wp;
1161 wp = win->w_mzscheme_ref;
1162 wp->win = INVALID_WINDOW_VALUE;
1163 win->w_mzscheme_ref = NULL;
1164 scheme_gc_ptr_ok(wp);
1165 MZ_GC_CHECK();
1170 * ":mzscheme" (or ":mz")
1172 void
1173 ex_mzscheme(exarg_T *eap)
1175 char_u *script;
1177 script = script_get(eap, eap->arg);
1178 if (!eap->skip)
1180 if (script == NULL)
1181 do_mzscheme_command(eap, eap->arg, do_eval);
1182 else
1184 do_mzscheme_command(eap, script, do_eval);
1185 vim_free(script);
1190 static Scheme_Object *
1191 do_load(void *data, int noargc, Scheme_Object **noargv)
1193 Scheme_Object *expr = NULL;
1194 Scheme_Object *result = NULL;
1195 char *file = NULL;
1196 Port_Info *pinfo = (Port_Info *)data;
1198 MZ_GC_DECL_REG(3);
1199 MZ_GC_VAR_IN_REG(0, expr);
1200 MZ_GC_VAR_IN_REG(1, result);
1201 MZ_GC_VAR_IN_REG(2, file);
1202 MZ_GC_REG();
1204 file = (char *)scheme_malloc_fail_ok(scheme_malloc_atomic, MAXPATHL + 1);
1205 MZ_GC_CHECK();
1207 /* make Vim expansion */
1208 expand_env((char_u *)pinfo->name, (char_u *)file, MAXPATHL);
1209 pinfo->port = scheme_open_input_file(file, "mzfile");
1210 MZ_GC_CHECK();
1211 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1212 MZ_GC_CHECK();
1214 /* Like REPL but print only last result */
1215 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
1217 result = scheme_eval(expr, environment);
1218 MZ_GC_CHECK();
1221 /* errors will be caught in do_mzscheme_command and ex_mzfile */
1222 scheme_close_input_port(pinfo->port);
1223 MZ_GC_CHECK();
1224 pinfo->port = NULL;
1225 MZ_GC_UNREG();
1226 return result;
1229 /* :mzfile */
1230 void
1231 ex_mzfile(exarg_T *eap)
1233 Port_Info pinfo = {NULL, NULL};
1235 MZ_GC_DECL_REG(1);
1236 MZ_GC_VAR_IN_REG(0, pinfo.port);
1237 MZ_GC_REG();
1239 pinfo.name = (char *)eap->arg;
1240 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1241 && pinfo.port != NULL) /* looks like port was not closed */
1243 scheme_close_input_port(pinfo.port);
1244 MZ_GC_CHECK();
1246 MZ_GC_UNREG();
1251 *========================================================================
1252 * Exception handling code -- cribbed form the MzScheme sources and
1253 * Matthew Flatt's "Inside PLT MzScheme" document.
1254 *========================================================================
1256 static void
1257 init_exn_catching_apply(void)
1259 if (!exn_catching_apply)
1261 char *e =
1262 "(lambda (thunk) "
1263 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
1264 "(cons #t (thunk))))";
1266 exn_catching_apply = scheme_eval_string(e, environment);
1267 MZ_GC_CHECK();
1268 exn_p = scheme_builtin_value("exn?");
1269 MZ_GC_CHECK();
1270 exn_message = scheme_builtin_value("exn-message");
1271 MZ_GC_CHECK();
1276 * This function applies a thunk, returning the Scheme value if there's
1277 * no exception, otherwise returning NULL and setting *exn to the raised
1278 * value (usually an exn structure).
1280 static Scheme_Object *
1281 _apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1283 Scheme_Object *v;
1285 v = _scheme_apply(exn_catching_apply, 1, &f);
1286 /* v is a pair: (cons #t value) or (cons #f exn) */
1288 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1289 return SCHEME_CDR(v);
1290 else
1292 *exn = SCHEME_CDR(v);
1293 return NULL;
1297 static Scheme_Object *
1298 extract_exn_message(Scheme_Object *v)
1300 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1301 return _scheme_apply(exn_message, 1, &v);
1302 else
1303 return NULL; /* Not an exn structure */
1306 static Scheme_Object *
1307 do_eval(void *s, int noargc, Scheme_Object **noargv)
1309 return scheme_eval_string_all((char *)s, environment, TRUE);
1313 *========================================================================
1314 * 3. MzScheme I/O Handlers
1315 *========================================================================
1317 static void
1318 do_intrnl_output(char *mesg, long len, int error)
1320 char *p, *prev;
1322 prev = mesg;
1323 p = strchr(prev, '\n');
1324 while (p)
1326 *p = '\0';
1327 if (error)
1328 EMSG(prev);
1329 else
1330 MSG(prev);
1331 prev = p + 1;
1332 p = strchr(prev, '\n');
1335 if (error)
1336 EMSG(prev);
1337 else
1338 MSG(prev);
1341 static void
1342 do_output(char *mesg, long len)
1344 do_intrnl_output(mesg, len, 0);
1347 static void
1348 do_err_output(char *mesg, long len)
1350 do_intrnl_output(mesg, len, 1);
1353 static void
1354 do_printf(char *format, ...)
1356 do_intrnl_output(format, STRLEN(format), 1);
1359 static void
1360 do_flush(void)
1362 char *buff;
1363 long length;
1365 buff = scheme_get_sized_string_output(curerr, &length);
1366 MZ_GC_CHECK();
1367 if (length)
1369 do_err_output(buff, length);
1370 return;
1373 buff = scheme_get_sized_string_output(curout, &length);
1374 MZ_GC_CHECK();
1375 if (length)
1376 do_output(buff, length);
1380 *========================================================================
1381 * 4. Implementation of the Vim Features for MzScheme
1382 *========================================================================
1385 /* (command {command-string}) */
1386 static Scheme_Object *
1387 vim_command(void *data, int argc, Scheme_Object **argv)
1389 Vim_Prim *prim = (Vim_Prim *)data;
1390 char *cmd = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1392 /* may be use do_cmdline_cmd? */
1393 do_cmdline((char_u *)cmd, NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
1394 update_screen(VALID);
1396 raise_if_error();
1397 return scheme_void;
1400 /* (eval {expr-string}) */
1401 static Scheme_Object *
1402 vim_eval(void *data, int argc, Scheme_Object **argv)
1404 #ifdef FEAT_EVAL
1405 Vim_Prim *prim = (Vim_Prim *)data;
1406 char *expr;
1407 Scheme_Object *result;
1408 /* hash table to store visited values to avoid infinite loops */
1409 Scheme_Hash_Table *visited = NULL;
1410 typval_T *vim_result;
1412 MZ_GC_DECL_REG(1);
1413 MZ_GC_VAR_IN_REG(0, visited);
1414 MZ_GC_REG();
1416 visited = scheme_make_hash_table(SCHEME_hash_ptr);
1417 MZ_GC_CHECK();
1419 expr = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1420 vim_result = eval_expr((char_u *)expr, NULL);
1422 if (vim_result == NULL)
1423 raise_vim_exn(_("invalid expression"));
1425 result = vim_to_mzscheme(vim_result, 1, visited);
1426 free_tv(vim_result);
1428 MZ_GC_UNREG();
1429 return result;
1430 #else
1431 raise_vim_exn(_("expressions disabled at compile time"));
1432 /* unreachable */
1433 return scheme_false;
1434 #endif
1437 /* (range-start) */
1438 static Scheme_Object *
1439 get_range_start(void *data, int argc, Scheme_Object **argv)
1441 return scheme_make_integer(range_start);
1444 /* (range-end) */
1445 static Scheme_Object *
1446 get_range_end(void *data, int argc, Scheme_Object **argv)
1448 return scheme_make_integer(range_end);
1451 /* (beep) */
1452 static Scheme_Object *
1453 mzscheme_beep(void *data, int argc, Scheme_Object **argv)
1455 vim_beep();
1456 return scheme_void;
1459 static Scheme_Object *M_global = NULL;
1461 /* (get-option {option-name}) [buffer/window] */
1462 static Scheme_Object *
1463 get_option(void *data, int argc, Scheme_Object **argv)
1465 Vim_Prim *prim = (Vim_Prim *)data;
1466 char_u *name;
1467 long value;
1468 char *strval;
1469 int rc;
1470 Scheme_Object *rval;
1471 int opt_flags = 0;
1472 buf_T *save_curb = curbuf;
1473 win_T *save_curw = curwin;
1475 name = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1477 if (argc > 1)
1479 if (M_global == NULL)
1481 MZ_REGISTER_STATIC(M_global);
1482 M_global = scheme_intern_symbol("global");
1483 MZ_GC_CHECK();
1486 if (argv[1] == M_global)
1487 opt_flags = OPT_GLOBAL;
1488 else if (SCHEME_VIMBUFFERP(argv[1]))
1490 curbuf = get_valid_buffer(argv[1]);
1491 opt_flags = OPT_LOCAL;
1493 else if (SCHEME_VIMWINDOWP(argv[1]))
1495 win_T *win = get_valid_window(argv[1]);
1497 curwin = win;
1498 curbuf = win->w_buffer;
1499 opt_flags = OPT_LOCAL;
1501 else
1502 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1505 rc = get_option_value(name, &value, (char_u **)&strval, opt_flags);
1506 curbuf = save_curb;
1507 curwin = save_curw;
1509 switch (rc)
1511 case 1:
1512 return scheme_make_integer_value(value);
1513 case 0:
1514 rval = scheme_make_string(strval);
1515 MZ_GC_CHECK();
1516 vim_free(strval);
1517 return rval;
1518 case -1:
1519 case -2:
1520 raise_vim_exn(_("hidden option"));
1521 case -3:
1522 raise_vim_exn(_("unknown option"));
1524 /* unreachable */
1525 return scheme_void;
1528 /* (set-option {option-changing-string} [buffer/window]) */
1529 static Scheme_Object *
1530 set_option(void *data, int argc, Scheme_Object **argv)
1532 char_u *cmd;
1533 int opt_flags = 0;
1534 buf_T *save_curb = curbuf;
1535 win_T *save_curw = curwin;
1536 Vim_Prim *prim = (Vim_Prim *)data;
1538 GUARANTEE_STRING(prim->name, 0);
1539 if (argc > 1)
1541 if (M_global == NULL)
1543 MZ_REGISTER_STATIC(M_global);
1544 M_global = scheme_intern_symbol("global");
1545 MZ_GC_CHECK();
1548 if (argv[1] == M_global)
1549 opt_flags = OPT_GLOBAL;
1550 else if (SCHEME_VIMBUFFERP(argv[1]))
1552 curbuf = get_valid_buffer(argv[1]);
1553 opt_flags = OPT_LOCAL;
1555 else if (SCHEME_VIMWINDOWP(argv[1]))
1557 win_T *win = get_valid_window(argv[1]);
1558 curwin = win;
1559 curbuf = win->w_buffer;
1560 opt_flags = OPT_LOCAL;
1562 else
1563 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1566 /* do_set can modify cmd, make copy */
1567 cmd = vim_strsave((char_u *)SCHEME_STR_VAL(argv[0]));
1568 do_set(cmd, opt_flags);
1569 vim_free(cmd);
1570 update_screen(NOT_VALID);
1571 curbuf = save_curb;
1572 curwin = save_curw;
1573 raise_if_error();
1574 return scheme_void;
1578 *===========================================================================
1579 * 5. Vim Window-related Manipulation Functions
1580 *===========================================================================
1583 /* (curr-win) */
1584 static Scheme_Object *
1585 get_curr_win(void *data, int argc, Scheme_Object **argv)
1587 return (Scheme_Object *)get_vim_curr_window();
1590 /* (win-count) */
1591 static Scheme_Object *
1592 get_window_count(void *data, int argc, Scheme_Object **argv)
1594 win_T *w;
1595 int n = 0;
1597 for (w = firstwin; w != NULL; w = w->w_next)
1598 ++n;
1599 return scheme_make_integer(n);
1602 /* (get-win-list [buffer]) */
1603 static Scheme_Object *
1604 get_window_list(void *data, int argc, Scheme_Object **argv)
1606 Vim_Prim *prim = (Vim_Prim *)data;
1607 vim_mz_buffer *buf;
1608 Scheme_Object *list;
1609 win_T *w;
1611 buf = get_buffer_arg(prim->name, 0, argc, argv);
1612 list = scheme_null;
1614 for (w = firstwin; w != NULL; w = w->w_next)
1615 if (w->w_buffer == buf->buf)
1617 list = scheme_make_pair(window_new(w), list);
1618 MZ_GC_CHECK();
1621 return list;
1624 static Scheme_Object *
1625 window_new(win_T *win)
1627 vim_mz_window *self = NULL;
1629 MZ_GC_DECL_REG(1);
1630 MZ_GC_VAR_IN_REG(0, self);
1631 MZ_GC_REG();
1633 /* We need to handle deletion of windows underneath us.
1634 * If we add a "w_mzscheme_ref" field to the win_T structure,
1635 * then we can get at it in win_free() in vim.
1637 * On a win_free() we set the Scheme object's win_T *field
1638 * to an invalid value. We trap all uses of a window
1639 * object, and reject them if the win_T *field is invalid.
1641 if (win->w_mzscheme_ref != NULL)
1642 return win->w_mzscheme_ref;
1644 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_window));
1645 vim_memset(self, 0, sizeof(vim_mz_window));
1646 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
1647 MZ_GC_CHECK();
1648 win->w_mzscheme_ref = self;
1649 self->win = win;
1650 self->so.type = mz_window_type;
1652 MZ_GC_UNREG();
1653 return (Scheme_Object *)(self);
1656 /* (get-win-num [window]) */
1657 static Scheme_Object *
1658 get_window_num(void *data, int argc, Scheme_Object **argv)
1660 Vim_Prim *prim = (Vim_Prim *)data;
1661 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
1662 int nr = 1;
1663 win_T *wp;
1665 for (wp = firstwin; wp != win; wp = wp->w_next)
1666 ++nr;
1668 return scheme_make_integer(nr);
1671 /* (get-win-by-num {windownum}) */
1672 static Scheme_Object *
1673 get_window_by_num(void *data, int argc, Scheme_Object **argv)
1675 Vim_Prim *prim = (Vim_Prim *)data;
1676 win_T *win;
1677 int fnum;
1679 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1680 if (fnum < 1)
1681 scheme_signal_error(_("window index is out of range"));
1683 for (win = firstwin; win != NULL; win = win->w_next, --fnum)
1684 if (fnum == 1) /* to be 1-based */
1685 return window_new(win);
1687 return scheme_false;
1690 /* (get-win-buffer [window]) */
1691 static Scheme_Object *
1692 get_window_buffer(void *data, int argc, Scheme_Object **argv)
1694 Vim_Prim *prim = (Vim_Prim *)data;
1695 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1697 return buffer_new(win->win->w_buffer);
1700 /* (get-win-height [window]) */
1701 static Scheme_Object *
1702 get_window_height(void *data, int argc, Scheme_Object **argv)
1704 Vim_Prim *prim = (Vim_Prim *)data;
1705 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1707 return scheme_make_integer(win->win->w_height);
1710 /* (set-win-height {height} [window]) */
1711 static Scheme_Object *
1712 set_window_height(void *data, int argc, Scheme_Object **argv)
1714 Vim_Prim *prim = (Vim_Prim *)data;
1715 vim_mz_window *win;
1716 win_T *savewin;
1717 int height;
1719 win = get_window_arg(prim->name, 1, argc, argv);
1720 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1722 #ifdef FEAT_GUI
1723 need_mouse_correct = TRUE;
1724 #endif
1726 savewin = curwin;
1727 curwin = win->win;
1728 win_setheight(height);
1729 curwin = savewin;
1731 raise_if_error();
1732 return scheme_void;
1735 #ifdef FEAT_VERTSPLIT
1736 /* (get-win-width [window]) */
1737 static Scheme_Object *
1738 get_window_width(void *data, int argc, Scheme_Object **argv)
1740 Vim_Prim *prim = (Vim_Prim *)data;
1741 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1743 return scheme_make_integer(W_WIDTH(win->win));
1746 /* (set-win-width {width} [window]) */
1747 static Scheme_Object *
1748 set_window_width(void *data, int argc, Scheme_Object **argv)
1750 Vim_Prim *prim = (Vim_Prim *)data;
1751 vim_mz_window *win;
1752 win_T *savewin;
1753 int width = 0;
1755 win = get_window_arg(prim->name, 1, argc, argv);
1756 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1758 # ifdef FEAT_GUI
1759 need_mouse_correct = TRUE;
1760 # endif
1762 savewin = curwin;
1763 curwin = win->win;
1764 win_setwidth(width);
1765 curwin = savewin;
1767 raise_if_error();
1768 return scheme_void;
1770 #endif
1772 /* (get-cursor [window]) -> (line . col) */
1773 static Scheme_Object *
1774 get_cursor(void *data, int argc, Scheme_Object **argv)
1776 Vim_Prim *prim = (Vim_Prim *)data;
1777 vim_mz_window *win;
1778 pos_T pos;
1780 win = get_window_arg(prim->name, 0, argc, argv);
1781 pos = win->win->w_cursor;
1782 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
1783 scheme_make_integer_value((long)pos.col + 1));
1786 /* (set-cursor (line . col) [window]) */
1787 static Scheme_Object *
1788 set_cursor(void *data, int argc, Scheme_Object **argv)
1790 Vim_Prim *prim = (Vim_Prim *)data;
1791 vim_mz_window *win;
1792 long lnum = 0;
1793 long col = 0;
1795 #ifdef HAVE_SANDBOX
1796 sandbox_check();
1797 #endif
1798 win = get_window_arg(prim->name, 1, argc, argv);
1799 GUARANTEE_PAIR(prim->name, 0);
1801 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
1802 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
1803 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
1805 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
1806 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
1808 check_line_range(lnum, win->win->w_buffer);
1809 /* don't know how to catch invalid column value */
1811 win->win->w_cursor.lnum = lnum;
1812 win->win->w_cursor.col = col;
1813 update_screen(VALID);
1815 raise_if_error();
1816 return scheme_void;
1819 *===========================================================================
1820 * 6. Vim Buffer-related Manipulation Functions
1821 *===========================================================================
1824 /* (open-buff {filename}) */
1825 static Scheme_Object *
1826 mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
1828 Vim_Prim *prim = (Vim_Prim *)data;
1829 char_u *fname;
1830 int num = 0;
1831 Scheme_Object *onum;
1833 #ifdef HAVE_SANDBOX
1834 sandbox_check();
1835 #endif
1836 fname = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1837 /* TODO make open existing file */
1838 num = buflist_add(fname, BLN_LISTED | BLN_CURBUF);
1840 if (num == 0)
1841 raise_vim_exn(_("couldn't open buffer"));
1843 onum = scheme_make_integer(num);
1844 return get_buffer_by_num(data, 1, &onum);
1847 /* (get-buff-by-num {buffernum}) */
1848 static Scheme_Object *
1849 get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
1851 Vim_Prim *prim = (Vim_Prim *)data;
1852 buf_T *buf;
1853 int fnum;
1855 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1857 for (buf = firstbuf; buf; buf = buf->b_next)
1858 if (buf->b_fnum == fnum)
1859 return buffer_new(buf);
1861 return scheme_false;
1864 /* (get-buff-by-name {buffername}) */
1865 static Scheme_Object *
1866 get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
1868 Vim_Prim *prim = (Vim_Prim *)data;
1869 buf_T *buf;
1870 char_u *fname;
1872 fname = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1874 for (buf = firstbuf; buf; buf = buf->b_next)
1875 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1876 /* empty string */
1878 if (fname[0] == NUL)
1879 return buffer_new(buf);
1881 else if (!fnamecmp(buf->b_ffname, fname)
1882 || !fnamecmp(buf->b_sfname, fname))
1883 /* either short or long filename matches */
1884 return buffer_new(buf);
1886 return scheme_false;
1889 /* (get-next-buff [buffer]) */
1890 static Scheme_Object *
1891 get_next_buffer(void *data, int argc, Scheme_Object **argv)
1893 Vim_Prim *prim = (Vim_Prim *)data;
1894 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1896 if (buf->b_next == NULL)
1897 return scheme_false;
1898 else
1899 return buffer_new(buf->b_next);
1902 /* (get-prev-buff [buffer]) */
1903 static Scheme_Object *
1904 get_prev_buffer(void *data, int argc, Scheme_Object **argv)
1906 Vim_Prim *prim = (Vim_Prim *)data;
1907 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1909 if (buf->b_prev == NULL)
1910 return scheme_false;
1911 else
1912 return buffer_new(buf->b_prev);
1915 /* (get-buff-num [buffer]) */
1916 static Scheme_Object *
1917 get_buffer_num(void *data, int argc, Scheme_Object **argv)
1919 Vim_Prim *prim = (Vim_Prim *)data;
1920 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1922 return scheme_make_integer(buf->buf->b_fnum);
1925 /* (buff-count) */
1926 static Scheme_Object *
1927 get_buffer_count(void *data, int argc, Scheme_Object **argv)
1929 buf_T *b;
1930 int n = 0;
1932 for (b = firstbuf; b; b = b->b_next) ++n;
1933 return scheme_make_integer(n);
1936 /* (get-buff-name [buffer]) */
1937 static Scheme_Object *
1938 get_buffer_name(void *data, int argc, Scheme_Object **argv)
1940 Vim_Prim *prim = (Vim_Prim *)data;
1941 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1943 return scheme_make_string((char *)buf->buf->b_ffname);
1946 /* (curr-buff) */
1947 static Scheme_Object *
1948 get_curr_buffer(void *data, int argc, Scheme_Object **argv)
1950 return (Scheme_Object *)get_vim_curr_buffer();
1953 static Scheme_Object *
1954 buffer_new(buf_T *buf)
1956 vim_mz_buffer *self = NULL;
1958 MZ_GC_DECL_REG(1);
1959 MZ_GC_VAR_IN_REG(0, self);
1960 MZ_GC_REG();
1962 /* We need to handle deletion of buffers underneath us.
1963 * If we add a "b_mzscheme_ref" field to the buf_T structure,
1964 * then we can get at it in buf_freeall() in vim.
1966 if (buf->b_mzscheme_ref)
1967 return buf->b_mzscheme_ref;
1969 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_buffer));
1970 vim_memset(self, 0, sizeof(vim_mz_buffer));
1971 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
1972 MZ_GC_CHECK();
1973 buf->b_mzscheme_ref = self;
1974 self->buf = buf;
1975 self->so.type = mz_buffer_type;
1977 MZ_GC_UNREG();
1978 return (Scheme_Object *)(self);
1982 * (get-buff-size [buffer])
1984 * Get the size (number of lines) in the current buffer.
1986 static Scheme_Object *
1987 get_buffer_size(void *data, int argc, Scheme_Object **argv)
1989 Vim_Prim *prim = (Vim_Prim *)data;
1990 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1992 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
1996 * (get-buff-line {linenr} [buffer])
1998 * Get a line from the specified buffer. The line number is
1999 * in Vim format (1-based). The line is returned as a MzScheme
2000 * string object.
2002 static Scheme_Object *
2003 get_buffer_line(void *data, int argc, Scheme_Object **argv)
2005 Vim_Prim *prim = (Vim_Prim *)data;
2006 vim_mz_buffer *buf;
2007 int linenr;
2008 char_u *line;
2010 buf = get_buffer_arg(prim->name, 1, argc, argv);
2011 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2012 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
2014 raise_if_error();
2015 return scheme_make_string((char *)line);
2020 * (get-buff-line-list {start} {end} [buffer])
2022 * Get a list of lines from the specified buffer. The line numbers
2023 * are in Vim format (1-based). The range is from lo up to, but not
2024 * including, hi. The list is returned as a list of string objects.
2026 static Scheme_Object *
2027 get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2029 Vim_Prim *prim = (Vim_Prim *)data;
2030 vim_mz_buffer *buf;
2031 int i, hi, lo, n;
2032 Scheme_Object *list = NULL;
2034 MZ_GC_DECL_REG(1);
2035 MZ_GC_VAR_IN_REG(0, list);
2036 MZ_GC_REG();
2038 buf = get_buffer_arg(prim->name, 2, argc, argv);
2039 list = scheme_null;
2040 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2041 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2044 * Handle some error conditions
2046 if (lo < 0)
2047 lo = 0;
2049 if (hi < 0)
2050 hi = 0;
2051 if (hi < lo)
2052 hi = lo;
2054 n = hi - lo;
2056 for (i = n; i >= 0; --i)
2058 Scheme_Object *str = scheme_make_string(
2059 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
2060 raise_if_error();
2062 /* Set the list item */
2063 list = scheme_make_pair(str, list);
2064 MZ_GC_CHECK();
2066 MZ_GC_UNREG();
2067 return list;
2071 * (set-buff-line {linenr} {string/#f} [buffer])
2073 * Replace a line in the specified buffer. The line number is
2074 * in Vim format (1-based). The replacement line is given as
2075 * an MzScheme string object. The object is checked for validity
2076 * and correct format. An exception is thrown if the values are not
2077 * the correct format.
2079 * It returns a Scheme Object that indicates the length of the
2080 * string changed.
2082 static Scheme_Object *
2083 set_buffer_line(void *data, int argc, Scheme_Object **argv)
2085 /* First of all, we check the value of the supplied MzScheme object.
2086 * There are three cases:
2087 * 1. #f - this is a deletion.
2088 * 2. A string - this is a replacement.
2089 * 3. Anything else - this is an error.
2091 Vim_Prim *prim = (Vim_Prim *)data;
2092 vim_mz_buffer *buf;
2093 Scheme_Object *line = NULL;
2094 char *save;
2095 int n;
2097 MZ_GC_DECL_REG(1);
2098 MZ_GC_VAR_IN_REG(0, line);
2099 MZ_GC_REG();
2101 #ifdef HAVE_SANDBOX
2102 sandbox_check();
2103 #endif
2104 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2105 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
2106 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
2107 line = argv[1];
2108 buf = get_buffer_arg(prim->name, 2, argc, argv);
2110 check_line_range(n, buf->buf);
2112 if (SCHEME_FALSEP(line))
2114 buf_T *savebuf = curbuf;
2116 curbuf = buf->buf;
2118 if (u_savedel((linenr_T)n, 1L) == FAIL)
2120 curbuf = savebuf;
2121 raise_vim_exn(_("cannot save undo information"));
2123 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2125 curbuf = savebuf;
2126 raise_vim_exn(_("cannot delete line"));
2128 if (buf->buf == curwin->w_buffer)
2129 mz_fix_cursor(n, n + 1, -1);
2130 deleted_lines_mark((linenr_T)n, 1L);
2132 curbuf = savebuf;
2134 MZ_GC_UNREG();
2135 raise_if_error();
2136 return scheme_void;
2138 else
2140 /* Otherwise it's a line */
2141 buf_T *savebuf = curbuf;
2143 save = string_to_line(line);
2145 curbuf = buf->buf;
2147 if (u_savesub((linenr_T)n) == FAIL)
2149 curbuf = savebuf;
2150 vim_free(save);
2151 raise_vim_exn(_("cannot save undo information"));
2153 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
2155 curbuf = savebuf;
2156 vim_free(save);
2157 raise_vim_exn(_("cannot replace line"));
2159 else
2161 vim_free(save);
2162 changed_bytes((linenr_T)n, 0);
2165 curbuf = savebuf;
2167 /* Check that the cursor is not beyond the end of the line now. */
2168 if (buf->buf == curwin->w_buffer)
2169 check_cursor_col();
2171 MZ_GC_UNREG();
2172 raise_if_error();
2173 return scheme_void;
2177 static void
2178 free_array(char **array)
2180 char **curr = array;
2181 while (*curr != NULL)
2182 vim_free(*curr++);
2183 vim_free(array);
2187 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
2189 * Replace a range of lines in the specified buffer. The line numbers are in
2190 * Vim format (1-based). The range is from lo up to, but not including, hi.
2191 * The replacement lines are given as a Scheme list of string objects. The
2192 * list is checked for validity and correct format.
2194 * Errors are returned as a value of FAIL. The return value is OK on success.
2195 * If OK is returned and len_change is not NULL, *len_change is set to the
2196 * change in the buffer length.
2198 static Scheme_Object *
2199 set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2201 /* First of all, we check the type of the supplied MzScheme object.
2202 * There are three cases:
2203 * 1. #f - this is a deletion.
2204 * 2. A list - this is a replacement.
2205 * 3. Anything else - this is an error.
2207 Vim_Prim *prim = (Vim_Prim *)data;
2208 vim_mz_buffer *buf = NULL;
2209 Scheme_Object *line_list = NULL;
2210 int i, old_len, new_len, hi, lo;
2211 long extra;
2213 MZ_GC_DECL_REG(1);
2214 MZ_GC_VAR_IN_REG(0, line_list);
2215 MZ_GC_REG();
2217 #ifdef HAVE_SANDBOX
2218 sandbox_check();
2219 #endif
2220 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2221 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2222 if (!SCHEME_PAIRP(argv[2])
2223 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2224 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2225 line_list = argv[2];
2226 buf = get_buffer_arg(prim->name, 3, argc, argv);
2227 old_len = hi - lo;
2228 if (old_len < 0) /* process inverse values wisely */
2230 i = lo;
2231 lo = hi;
2232 hi = i;
2233 old_len = -old_len;
2235 extra = 0;
2237 check_line_range(lo, buf->buf); /* inclusive */
2238 check_line_range(hi - 1, buf->buf); /* exclusive */
2240 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2242 buf_T *savebuf = curbuf;
2243 curbuf = buf->buf;
2245 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2247 curbuf = savebuf;
2248 raise_vim_exn(_("cannot save undo information"));
2250 else
2252 for (i = 0; i < old_len; i++)
2253 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2255 curbuf = savebuf;
2256 raise_vim_exn(_("cannot delete line"));
2258 if (buf->buf == curwin->w_buffer)
2259 mz_fix_cursor(lo, hi, -old_len);
2260 deleted_lines_mark((linenr_T)lo, (long)old_len);
2263 curbuf = savebuf;
2265 MZ_GC_UNREG();
2266 raise_if_error();
2267 return scheme_void;
2269 else
2271 buf_T *savebuf = curbuf;
2273 /* List */
2274 new_len = scheme_proper_list_length(line_list);
2275 MZ_GC_CHECK();
2276 if (new_len < 0) /* improper or cyclic list */
2277 scheme_wrong_type(prim->name, "proper list",
2278 2, argc, argv);
2279 else
2281 char **array = NULL;
2282 Scheme_Object *line = NULL;
2283 Scheme_Object *rest = NULL;
2285 MZ_GC_DECL_REG(2);
2286 MZ_GC_VAR_IN_REG(0, line);
2287 MZ_GC_VAR_IN_REG(1, rest);
2288 MZ_GC_REG();
2290 array = (char **)alloc(new_len * sizeof(char *));
2291 vim_memset(array, 0, new_len * sizeof(char *));
2293 rest = line_list;
2294 for (i = 0; i < new_len; ++i)
2296 line = SCHEME_CAR(rest);
2297 rest = SCHEME_CDR(rest);
2298 if (!SCHEME_STRINGP(line))
2300 free_array(array);
2301 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
2303 array[i] = string_to_line(line);
2306 curbuf = buf->buf;
2308 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2310 curbuf = savebuf;
2311 free_array(array);
2312 raise_vim_exn(_("cannot save undo information"));
2316 * If the size of the range is reducing (ie, new_len < old_len) we
2317 * need to delete some old_len. We do this at the start, by
2318 * repeatedly deleting line "lo".
2320 for (i = 0; i < old_len - new_len; ++i)
2322 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2324 curbuf = savebuf;
2325 free_array(array);
2326 raise_vim_exn(_("cannot delete line"));
2328 extra--;
2332 * For as long as possible, replace the existing old_len with the
2333 * new old_len. This is a more efficient operation, as it requires
2334 * less memory allocation and freeing.
2336 for (i = 0; i < old_len && i < new_len; i++)
2337 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2339 curbuf = savebuf;
2340 free_array(array);
2341 raise_vim_exn(_("cannot replace line"));
2345 * Now we may need to insert the remaining new_len. We don't need to
2346 * free the string passed back because MzScheme has control of that
2347 * memory.
2349 while (i < new_len)
2351 if (ml_append((linenr_T)(lo + i - 1),
2352 (char_u *)array[i], 0, FALSE) == FAIL)
2354 curbuf = savebuf;
2355 free_array(array);
2356 raise_vim_exn(_("cannot insert line"));
2358 ++i;
2359 ++extra;
2361 MZ_GC_UNREG();
2362 free_array(array);
2366 * Adjust marks. Invalidate any which lie in the
2367 * changed range, and move any in the remainder of the buffer.
2369 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra);
2370 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2372 if (buf->buf == curwin->w_buffer)
2373 mz_fix_cursor(lo, hi, extra);
2374 curbuf = savebuf;
2376 MZ_GC_UNREG();
2377 raise_if_error();
2378 return scheme_void;
2383 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2385 * Insert a number of lines into the specified buffer after the specified line.
2386 * The line number is in Vim format (1-based). The lines to be inserted are
2387 * given as an MzScheme list of string objects or as a single string. The lines
2388 * to be added are checked for validity and correct format. Errors are
2389 * returned as a value of FAIL. The return value is OK on success.
2390 * If OK is returned and len_change is not NULL, *len_change
2391 * is set to the change in the buffer length.
2393 static Scheme_Object *
2394 insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2396 Vim_Prim *prim = (Vim_Prim *)data;
2397 vim_mz_buffer *buf = NULL;
2398 Scheme_Object *list = NULL;
2399 char *str = NULL;
2400 int i, n, size;
2402 MZ_GC_DECL_REG(1);
2403 MZ_GC_VAR_IN_REG(0, list);
2404 MZ_GC_REG();
2406 #ifdef HAVE_SANDBOX
2407 sandbox_check();
2408 #endif
2410 * First of all, we check the type of the supplied MzScheme object.
2411 * It must be a string or a list, or the call is in error.
2413 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2414 list = argv[1];
2416 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2417 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2418 buf = get_buffer_arg(prim->name, 2, argc, argv);
2420 if (n != 0) /* 0 can be used in insert */
2421 check_line_range(n, buf->buf);
2422 if (SCHEME_STRINGP(list))
2424 buf_T *savebuf = curbuf;
2426 str = string_to_line(list);
2427 curbuf = buf->buf;
2429 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2431 curbuf = savebuf;
2432 vim_free(str);
2433 raise_vim_exn(_("cannot save undo information"));
2435 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2437 curbuf = savebuf;
2438 vim_free(str);
2439 raise_vim_exn(_("cannot insert line"));
2441 else
2443 vim_free(str);
2444 appended_lines_mark((linenr_T)n, 1L);
2447 curbuf = savebuf;
2448 update_screen(VALID);
2450 MZ_GC_UNREG();
2451 raise_if_error();
2452 return scheme_void;
2455 /* List */
2456 size = scheme_proper_list_length(list);
2457 MZ_GC_CHECK();
2458 if (size < 0) /* improper or cyclic list */
2459 scheme_wrong_type(prim->name, "proper list",
2460 2, argc, argv);
2461 else
2463 Scheme_Object *line = NULL;
2464 Scheme_Object *rest = NULL;
2465 char **array;
2466 buf_T *savebuf = curbuf;
2468 MZ_GC_DECL_REG(2);
2469 MZ_GC_VAR_IN_REG(0, line);
2470 MZ_GC_VAR_IN_REG(1, rest);
2471 MZ_GC_REG();
2473 array = (char **)alloc(size * sizeof(char *));
2474 vim_memset(array, 0, size * sizeof(char *));
2476 rest = list;
2477 for (i = 0; i < size; ++i)
2479 line = SCHEME_CAR(rest);
2480 rest = SCHEME_CDR(rest);
2481 array[i] = string_to_line(line);
2484 curbuf = buf->buf;
2486 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2488 curbuf = savebuf;
2489 free_array(array);
2490 raise_vim_exn(_("cannot save undo information"));
2492 else
2494 for (i = 0; i < size; ++i)
2495 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
2496 0, FALSE) == FAIL)
2498 curbuf = savebuf;
2499 free_array(array);
2500 raise_vim_exn(_("cannot insert line"));
2503 if (i > 0)
2504 appended_lines_mark((linenr_T)n, (long)i);
2506 free_array(array);
2507 MZ_GC_UNREG();
2508 curbuf = savebuf;
2509 update_screen(VALID);
2512 MZ_GC_UNREG();
2513 raise_if_error();
2514 return scheme_void;
2518 * Predicates
2520 /* (buff? obj) */
2521 static Scheme_Object *
2522 vim_bufferp(void *data, int argc, Scheme_Object **argv)
2524 if (SCHEME_VIMBUFFERP(argv[0]))
2525 return scheme_true;
2526 else
2527 return scheme_false;
2530 /* (win? obj) */
2531 static Scheme_Object *
2532 vim_windowp(void *data, int argc, Scheme_Object **argv)
2534 if (SCHEME_VIMWINDOWP(argv[0]))
2535 return scheme_true;
2536 else
2537 return scheme_false;
2540 /* (buff-valid? obj) */
2541 static Scheme_Object *
2542 vim_buffer_validp(void *data, int argc, Scheme_Object **argv)
2544 if (SCHEME_VIMBUFFERP(argv[0])
2545 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2546 return scheme_true;
2547 else
2548 return scheme_false;
2551 /* (win-valid? obj) */
2552 static Scheme_Object *
2553 vim_window_validp(void *data, int argc, Scheme_Object **argv)
2555 if (SCHEME_VIMWINDOWP(argv[0])
2556 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2557 return scheme_true;
2558 else
2559 return scheme_false;
2563 *===========================================================================
2564 * Utilities
2565 *===========================================================================
2569 * Convert an MzScheme string into a Vim line.
2571 * All internal nulls are replaced by newline characters.
2572 * It is an error for the string to contain newline characters.
2574 * Returns pointer to Vim allocated memory
2576 static char *
2577 string_to_line(Scheme_Object *obj)
2579 char *scheme_str = NULL;
2580 char *vim_str = NULL;
2581 long len;
2582 int i;
2584 scheme_str = scheme_display_to_string(obj, &len);
2586 /* Error checking: String must not contain newlines, as we
2587 * are replacing a single line, and we must replace it with
2588 * a single line.
2590 if (memchr(scheme_str, '\n', len))
2591 scheme_signal_error(_("string cannot contain newlines"));
2593 vim_str = (char *)alloc(len + 1);
2595 /* Create a copy of the string, with internal nulls replaced by
2596 * newline characters, as is the vim convention.
2598 for (i = 0; i < len; ++i)
2600 if (scheme_str[i] == '\0')
2601 vim_str[i] = '\n';
2602 else
2603 vim_str[i] = scheme_str[i];
2606 vim_str[i] = '\0';
2608 MZ_GC_CHECK();
2609 return vim_str;
2612 #ifdef FEAT_EVAL
2614 * Convert Vim value into MzScheme, adopted from if_python.c
2616 static Scheme_Object *
2617 vim_to_mzscheme(typval_T *vim_value, int depth, Scheme_Hash_Table *visited)
2619 Scheme_Object *result = NULL;
2620 int new_value = TRUE;
2622 MZ_GC_DECL_REG(1);
2623 MZ_GC_VAR_IN_REG(0, result);
2624 MZ_GC_REG();
2626 /* Avoid infinite recursion */
2627 if (depth > 100)
2629 MZ_GC_UNREG();
2630 return scheme_void;
2633 /* Check if we run into a recursive loop. The item must be in visited
2634 * then and we can use it again.
2636 result = scheme_hash_get(visited, (Scheme_Object *)vim_value);
2637 MZ_GC_CHECK();
2638 if (result != NULL) /* found, do nothing */
2639 new_value = FALSE;
2640 else if (vim_value->v_type == VAR_STRING)
2642 result = scheme_make_string((char *)vim_value->vval.v_string);
2643 MZ_GC_CHECK();
2645 else if (vim_value->v_type == VAR_NUMBER)
2647 result = scheme_make_integer((long)vim_value->vval.v_number);
2648 MZ_GC_CHECK();
2650 # ifdef FEAT_FLOAT
2651 else if (vim_value->v_type == VAR_FLOAT)
2653 result = scheme_make_double((double)vim_value->vval.v_float);
2654 MZ_GC_CHECK();
2656 # endif
2657 else if (vim_value->v_type == VAR_LIST)
2659 list_T *list = vim_value->vval.v_list;
2660 listitem_T *curr;
2662 if (list == NULL || list->lv_first == NULL)
2663 result = scheme_null;
2664 else
2666 Scheme_Object *obj = NULL;
2668 MZ_GC_DECL_REG(1);
2669 MZ_GC_VAR_IN_REG(0, obj);
2670 MZ_GC_REG();
2672 curr = list->lv_last;
2673 obj = vim_to_mzscheme(&curr->li_tv, depth + 1, visited);
2674 result = scheme_make_pair(obj, scheme_null);
2675 MZ_GC_CHECK();
2677 while (curr != list->lv_first)
2679 curr = curr->li_prev;
2680 obj = vim_to_mzscheme(&curr->li_tv, depth + 1, visited);
2681 result = scheme_make_pair(obj, result);
2682 MZ_GC_CHECK();
2685 MZ_GC_UNREG();
2687 else if (vim_value->v_type == VAR_DICT)
2689 Scheme_Object *key = NULL;
2690 Scheme_Object *obj = NULL;
2692 MZ_GC_DECL_REG(2);
2693 MZ_GC_VAR_IN_REG(0, key);
2694 MZ_GC_VAR_IN_REG(1, obj);
2695 MZ_GC_REG();
2697 result = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr);
2698 MZ_GC_CHECK();
2699 if (vim_value->vval.v_dict != NULL)
2701 hashtab_T *ht = &vim_value->vval.v_dict->dv_hashtab;
2702 long_u todo = ht->ht_used;
2703 hashitem_T *hi;
2704 dictitem_T *di;
2706 for (hi = ht->ht_array; todo > 0; ++hi)
2708 if (!HASHITEM_EMPTY(hi))
2710 --todo;
2712 di = dict_lookup(hi);
2713 obj = vim_to_mzscheme(&di->di_tv, depth + 1, visited);
2714 key = scheme_make_string((char *)hi->hi_key);
2715 MZ_GC_CHECK();
2716 scheme_hash_set((Scheme_Hash_Table *)result, key, obj);
2717 MZ_GC_CHECK();
2721 MZ_GC_UNREG();
2723 else
2725 result = scheme_void;
2726 new_value = FALSE;
2728 if (new_value)
2730 scheme_hash_set(visited, (Scheme_Object *)vim_value, result);
2731 MZ_GC_CHECK();
2733 MZ_GC_UNREG();
2734 return result;
2736 #endif
2739 * Check to see whether a Vim error has been reported, or a keyboard
2740 * interrupt (from vim --> got_int) has been detected.
2742 static int
2743 vim_error_check(void)
2745 return (got_int || did_emsg);
2749 * register Scheme exn:vim
2751 static void
2752 register_vim_exn(void)
2754 int nc = 0;
2755 int i;
2756 Scheme_Object *struct_exn = NULL;
2757 Scheme_Object *exn_name = NULL;
2759 MZ_GC_DECL_REG(2);
2760 MZ_GC_VAR_IN_REG(0, struct_exn);
2761 MZ_GC_VAR_IN_REG(1, exn_name);
2762 MZ_GC_REG();
2764 exn_name = scheme_intern_symbol("exn:vim");
2765 MZ_GC_CHECK();
2766 struct_exn = scheme_builtin_value("struct:exn");
2767 MZ_GC_CHECK();
2769 if (vim_exn == NULL)
2770 vim_exn = scheme_make_struct_type(exn_name,
2771 struct_exn, NULL, 0, 0, NULL, NULL
2772 #if MZSCHEME_VERSION_MAJOR >= 299
2773 , NULL
2774 #endif
2779 Scheme_Object **tmp = NULL;
2780 Scheme_Object *exn_names[5] = {NULL, NULL, NULL, NULL, NULL};
2781 Scheme_Object *exn_values[5] = {NULL, NULL, NULL, NULL, NULL};
2782 MZ_GC_DECL_REG(6);
2783 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names, 5);
2784 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values, 5);
2785 MZ_GC_REG();
2787 tmp = scheme_make_struct_names(exn_name, scheme_null, 0, &nc);
2788 assert(nc <= 5);
2789 mch_memmove(exn_names, tmp, nc * sizeof(Scheme_Object *));
2790 MZ_GC_CHECK();
2792 tmp = scheme_make_struct_values(vim_exn, exn_names, nc, 0);
2793 mch_memmove(exn_values, tmp, nc * sizeof(Scheme_Object *));
2794 MZ_GC_CHECK();
2796 for (i = 0; i < nc; i++)
2798 scheme_add_global_symbol(exn_names[i],
2799 exn_values[i], environment);
2800 MZ_GC_CHECK();
2802 MZ_GC_UNREG();
2804 MZ_GC_UNREG();
2808 * raise exn:vim, may be with additional info string
2810 void
2811 raise_vim_exn(const char *add_info)
2813 char *fmt = _("Vim error: ~a");
2814 Scheme_Object *argv[2] = {NULL, NULL};
2815 Scheme_Object *exn = NULL;
2817 MZ_GC_DECL_REG(4);
2818 MZ_GC_ARRAY_VAR_IN_REG(0, argv, 2);
2819 MZ_GC_VAR_IN_REG(3, exn);
2820 MZ_GC_REG();
2822 if (add_info != NULL)
2824 char *c_string = NULL;
2825 Scheme_Object *byte_string = NULL;
2826 Scheme_Object *info = NULL;
2828 MZ_GC_DECL_REG(3);
2829 MZ_GC_VAR_IN_REG(0, c_string);
2830 MZ_GC_VAR_IN_REG(1, byte_string);
2831 MZ_GC_VAR_IN_REG(2, info);
2832 MZ_GC_REG();
2834 info = scheme_make_string(add_info);
2835 MZ_GC_CHECK();
2836 c_string = scheme_format(fmt, STRLEN(fmt), 1, &info, NULL);
2837 MZ_GC_CHECK();
2838 byte_string = scheme_make_string(c_string);
2839 MZ_GC_CHECK();
2840 argv[0] = scheme_byte_string_to_char_string(byte_string);
2841 MZ_GC_CHECK();
2842 SCHEME_SET_IMMUTABLE(argv[0]);
2843 MZ_GC_UNREG();
2845 else
2846 argv[0] = scheme_make_string(_("Vim error"));
2847 MZ_GC_CHECK();
2849 #if MZSCHEME_VERSION_MAJOR < 360
2850 argv[1] = scheme_current_continuation_marks();
2851 MZ_GC_CHECK();
2852 #else
2853 argv[1] = scheme_current_continuation_marks(NULL);
2854 MZ_GC_CHECK();
2855 #endif
2857 exn = scheme_make_struct_instance(vim_exn, 2, argv);
2858 MZ_GC_CHECK();
2859 scheme_raise(exn);
2860 MZ_GC_UNREG();
2863 void
2864 raise_if_error(void)
2866 if (vim_error_check())
2867 raise_vim_exn(NULL);
2870 /* get buffer:
2871 * either current
2872 * or passed as argv[argnum] with checks
2874 static vim_mz_buffer *
2875 get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
2877 vim_mz_buffer *b;
2879 if (argc < argnum + 1)
2880 return get_vim_curr_buffer();
2881 if (!SCHEME_VIMBUFFERP(argv[argnum]))
2882 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
2883 b = (vim_mz_buffer *)argv[argnum];
2884 (void)get_valid_buffer(argv[argnum]);
2885 return b;
2888 /* get window:
2889 * either current
2890 * or passed as argv[argnum] with checks
2892 static vim_mz_window *
2893 get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
2895 vim_mz_window *w;
2897 if (argc < argnum + 1)
2898 return get_vim_curr_window();
2899 w = (vim_mz_window *)argv[argnum];
2900 if (!SCHEME_VIMWINDOWP(argv[argnum]))
2901 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
2902 (void)get_valid_window(argv[argnum]);
2903 return w;
2906 /* get valid Vim buffer from Scheme_Object* */
2907 buf_T *get_valid_buffer(void *obj)
2909 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
2911 if (buf == INVALID_BUFFER_VALUE)
2912 scheme_signal_error(_("buffer is invalid"));
2913 return buf;
2916 /* get valid Vim window from Scheme_Object* */
2917 win_T *get_valid_window(void *obj)
2919 win_T *win = ((vim_mz_window *)obj)->win;
2920 if (win == INVALID_WINDOW_VALUE)
2921 scheme_signal_error(_("window is invalid"));
2922 return win;
2926 mzthreads_allowed(void)
2928 return mz_threads_allow;
2931 static int
2932 line_in_range(linenr_T lnum, buf_T *buf)
2934 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
2937 static void
2938 check_line_range(linenr_T lnum, buf_T *buf)
2940 if (!line_in_range(lnum, buf))
2941 scheme_signal_error(_("linenr out of range"));
2945 * Check if deleting lines made the cursor position invalid
2946 * (or you'll get msg from Vim about invalid linenr).
2947 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2948 * deleted). Got from if_python.c
2950 static void
2951 mz_fix_cursor(int lo, int hi, int extra)
2953 if (curwin->w_cursor.lnum >= lo)
2955 /* Adjust the cursor position if it's in/after the changed
2956 * lines. */
2957 if (curwin->w_cursor.lnum >= hi)
2959 curwin->w_cursor.lnum += extra;
2960 check_cursor_col();
2962 else if (extra < 0)
2964 curwin->w_cursor.lnum = lo;
2965 check_cursor();
2967 else
2968 check_cursor_col();
2969 changed_cline_bef_curs();
2971 invalidate_botline();
2974 static Vim_Prim prims[]=
2977 * Buffer-related commands
2979 {get_buffer_line, "get-buff-line", 1, 2},
2980 {set_buffer_line, "set-buff-line", 2, 3},
2981 {get_buffer_line_list, "get-buff-line-list", 2, 3},
2982 {get_buffer_name, "get-buff-name", 0, 1},
2983 {get_buffer_num, "get-buff-num", 0, 1},
2984 {get_buffer_size, "get-buff-size", 0, 1},
2985 {set_buffer_line_list, "set-buff-line-list", 3, 4},
2986 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
2987 {get_curr_buffer, "curr-buff", 0, 0},
2988 {get_buffer_count, "buff-count", 0, 0},
2989 {get_next_buffer, "get-next-buff", 0, 1},
2990 {get_prev_buffer, "get-prev-buff", 0, 1},
2991 {mzscheme_open_buffer, "open-buff", 1, 1},
2992 {get_buffer_by_name, "get-buff-by-name", 1, 1},
2993 {get_buffer_by_num, "get-buff-by-num", 1, 1},
2995 * Window-related commands
2997 {get_curr_win, "curr-win", 0, 0},
2998 {get_window_count, "win-count", 0, 0},
2999 {get_window_by_num, "get-win-by-num", 1, 1},
3000 {get_window_num, "get-win-num", 0, 1},
3001 {get_window_buffer, "get-win-buffer", 0, 1},
3002 {get_window_height, "get-win-height", 0, 1},
3003 {set_window_height, "set-win-height", 1, 2},
3004 #ifdef FEAT_VERTSPLIT
3005 {get_window_width, "get-win-width", 0, 1},
3006 {set_window_width, "set-win-width", 1, 2},
3007 #endif
3008 {get_cursor, "get-cursor", 0, 1},
3009 {set_cursor, "set-cursor", 1, 2},
3010 {get_window_list, "get-win-list", 0, 1},
3012 * Vim-related commands
3014 {vim_command, "command", 1, 1},
3015 {vim_eval, "eval", 1, 1},
3016 {get_range_start, "range-start", 0, 0},
3017 {get_range_end, "range-end", 0, 0},
3018 {mzscheme_beep, "beep", 0, 0},
3019 {get_option, "get-option", 1, 2},
3020 {set_option, "set-option", 1, 2},
3022 * small utilities
3024 {vim_bufferp, "buff?", 1, 1},
3025 {vim_windowp, "win?", 1, 1},
3026 {vim_buffer_validp, "buff-valid?", 1, 1},
3027 {vim_window_validp, "win-valid?", 1, 1}
3030 /* return MzScheme wrapper for curbuf */
3031 static vim_mz_buffer *
3032 get_vim_curr_buffer(void)
3034 if (curbuf->b_mzscheme_ref == NULL)
3035 return (vim_mz_buffer *)buffer_new(curbuf);
3036 else
3037 return (vim_mz_buffer *)curbuf->b_mzscheme_ref;
3040 /* return MzScheme wrapper for curwin */
3041 static vim_mz_window *
3042 get_vim_curr_window(void)
3044 if (curwin->w_mzscheme_ref == NULL)
3045 return (vim_mz_window *)window_new(curwin);
3046 else
3047 return (vim_mz_window *)curwin->w_mzscheme_ref;
3050 static void
3051 make_modules()
3053 int i;
3054 Scheme_Env *mod = NULL;
3055 Scheme_Object *vimext_symbol = NULL;
3056 Scheme_Object *closed_prim = NULL;
3058 MZ_GC_DECL_REG(3);
3059 MZ_GC_VAR_IN_REG(0, mod);
3060 MZ_GC_VAR_IN_REG(1, vimext_symbol);
3061 MZ_GC_VAR_IN_REG(2, closed_prim);
3062 MZ_GC_REG();
3064 vimext_symbol = scheme_intern_symbol("vimext");
3065 MZ_GC_CHECK();
3066 mod = scheme_primitive_module(vimext_symbol, environment);
3067 MZ_GC_CHECK();
3068 /* all prims made closed so they can access their own names */
3069 for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
3071 Vim_Prim *prim = prims + i;
3072 closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
3073 prim->mina, prim->maxa);
3074 scheme_add_global(prim->name, closed_prim, mod);
3075 MZ_GC_CHECK();
3077 scheme_finish_primitive_module(mod);
3078 MZ_GC_CHECK();
3079 MZ_GC_UNREG();
3082 #ifdef HAVE_SANDBOX
3083 static Scheme_Object *M_write = NULL;
3084 static Scheme_Object *M_read = NULL;
3085 static Scheme_Object *M_execute = NULL;
3086 static Scheme_Object *M_delete = NULL;
3088 static void
3089 sandbox_check(void)
3091 if (sandbox)
3092 raise_vim_exn(_("not allowed in the Vim sandbox"));
3095 /* security guards to force Vim's sandbox restrictions on MzScheme level */
3096 static Scheme_Object *
3097 sandbox_file_guard(int argc, Scheme_Object **argv)
3099 if (sandbox)
3101 Scheme_Object *requested_access = argv[2];
3103 if (M_write == NULL)
3105 MZ_REGISTER_STATIC(M_write);
3106 M_write = scheme_intern_symbol("write");
3107 MZ_GC_CHECK();
3109 if (M_read == NULL)
3111 MZ_REGISTER_STATIC(M_read);
3112 M_read = scheme_intern_symbol("read");
3113 MZ_GC_CHECK();
3115 if (M_execute == NULL)
3117 MZ_REGISTER_STATIC(M_execute);
3118 M_execute = scheme_intern_symbol("execute");
3119 MZ_GC_CHECK();
3121 if (M_delete == NULL)
3123 MZ_REGISTER_STATIC(M_delete);
3124 M_delete = scheme_intern_symbol("delete");
3125 MZ_GC_CHECK();
3128 while (!SCHEME_NULLP(requested_access))
3130 Scheme_Object *item = SCHEME_CAR(requested_access);
3131 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
3132 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
3134 raise_vim_exn(_("not allowed in the Vim sandbox"));
3136 requested_access = SCHEME_CDR(requested_access);
3139 return scheme_void;
3142 static Scheme_Object *
3143 sandbox_network_guard(int argc, Scheme_Object **argv)
3145 return scheme_void;
3147 #endif
3149 #endif