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
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
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.
30 /* Only do the following when the feature is enabled. Needed for "make
32 #if defined(FEAT_MZSCHEME) || defined(PROTO)
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)
46 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
54 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
57 * Prims that form MzScheme Vim interface
61 Scheme_Closed_Prim
*prim
;
63 int mina
; /* arity information */
74 *========================================================================
75 * Vim-Control Commands
76 *========================================================================
79 *========================================================================
80 * Utility functions for the vim/mzscheme interface
81 *========================================================================
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);
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
**);
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);
171 static Scheme_Object
*vim_to_mzscheme(typval_T
*vim_value
, int depth
,
172 Scheme_Hash_Table
*visited
);
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
);
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
,
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
);
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);
237 static Scheme_Object
*(*dll_scheme_current_continuation_marks
)(Scheme_Object
*prompt_tag
);
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
,
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
);
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
);
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
*,
264 static char *(*dll_scheme_get_sized_byte_string_output
)(Scheme_Object
*,
267 static Scheme_Object
*(*dll_scheme_intern_symbol
)(const char *name
);
268 static Scheme_Object
*(*dll_scheme_lookup_global
)(Scheme_Object
*symbol
,
270 static Scheme_Object
*(*dll_scheme_make_closed_prim_w_arity
)
271 (Scheme_Closed_Prim
*prim
, void *data
, const char *name
, mzshort mina
,
273 static Scheme_Object
*(*dll_scheme_make_integer_value
)(long i
);
274 static Scheme_Object
*(*dll_scheme_make_pair
)(Scheme_Object
*car
,
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
)();
282 static Scheme_Object
*(*dll_scheme_make_byte_string
)(const char *chars
);
283 static Scheme_Object
*(*dll_scheme_make_byte_string_output_port
)();
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
297 static Scheme_Object
**(*dll_scheme_make_struct_values
)(
298 Scheme_Object
*struct_type
, Scheme_Object
**names
, int count
,
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
,
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
,
317 static Scheme_Config
*(*dll_scheme_current_config
)(void);
318 static Scheme_Object
*(*dll_scheme_char_string_to_byte_string
)
320 static Scheme_Object
*(*dll_scheme_char_string_to_path
)
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
,
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
,
332 static Scheme_Object
*(*dll_scheme_namespace_require
)(Scheme_Object
*req
);
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
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
377 # define scheme_format_utf8 dll_scheme_format_utf8
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
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
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
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
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
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
440 static Thunk_Info mzgc_imports
[] = {
441 {"GC_malloc", (void **)&dll_GC_malloc
},
442 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic
},
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
},
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
},
482 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8
},
483 {"scheme_get_param", (void **)&dll_scheme_get_param
},
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
},
490 {"scheme_get_sized_byte_string_output",
491 (void **)&dll_scheme_get_sized_byte_string_output
},
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
},
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
},
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
},
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
},
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
,
551 mzscheme_runtime_link_init(char *sch_dll
, char *gc_dll
, int verbose
)
553 Thunk_Info
*thunk
= NULL
;
557 hMzSch
= LoadLibrary(sch_dll
);
558 hMzGC
= LoadLibrary(gc_dll
);
563 EMSG2(_(e_loadlib
), sch_dll
);
570 EMSG2(_(e_loadlib
), gc_dll
);
574 for (thunk
= mzsch_imports
; thunk
->name
; thunk
++)
577 (void *)GetProcAddress(hMzSch
, thunk
->name
)) == NULL
)
584 EMSG2(_(e_loadfunc
), thunk
->name
);
588 for (thunk
= mzgc_imports
; thunk
->name
; thunk
++)
591 (void *)GetProcAddress(hMzGC
, thunk
->name
)) == NULL
)
598 EMSG2(_(e_loadfunc
), thunk
->name
);
606 mzscheme_enabled(int verbose
)
608 return mzscheme_runtime_link_init(
609 DYNAMIC_MZSCH_DLL
, DYNAMIC_MZGC_DLL
, verbose
) == OK
;
613 dynamic_mzscheme_end(void)
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
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
;
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
;
682 #ifndef FEAT_GUI_W32 /* Win32 console and Unix */
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
)
696 scheme_check_threads();
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)
709 timer_proc(HWND hwnd
, UINT uMsg
, UINT idEvent
, DWORD dwTime
)
710 # elif defined(FEAT_GUI_GTK)
712 timer_proc(gpointer data
)
713 # elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
715 timer_proc(XtPointer timed_out
, XtIntervalId
*interval_id
)
716 # elif defined(FEAT_GUI_MAC)
718 timer_proc(EventLoopTimerRef theTimer
, void *userData
)
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)
726 if (mz_threads_allow
&& p_mzq
> 0)
727 timer_id
= XtAppAddTimeOut(app_context
, p_mzq
,
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
);
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
);
765 mzvim_reset_timer(void)
769 if (mz_threads_allow
&& p_mzq
> 0 && gui
.in_use
)
773 #endif /* MZSCHEME_GUI_THREADS */
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
)
782 if (!on
&& timer_id
!= 0)
790 #ifdef DYNAMIC_MZSCHEME
791 dynamic_mzscheme_end();
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
);
802 mzscheme_env_main(NULL
, 0, NULL
);
807 mzscheme_env_main(Scheme_Env
*env
, int argc
, char **argv
)
809 /* neither argument nor return values are used */
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
;
819 MZ_GC_VAR_IN_REG(0, dummy
);
821 stack_base
= &__gc_var_stack__
;
823 /* environment has been created by us by Scheme */
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
836 stack_base
= (void *)&dummy
;
838 main_loop(FALSE
, FALSE
);
839 #if defined(MZ_PRECISE_GC) && MZSCHEME_VERSION_MAJOR < 400
840 /* releasing dummy */
848 startup_mzscheme(void)
850 #if !defined(MZ_PRECISE_GC) || MZSCHEME_VERSION_MAJOR < 400
851 scheme_set_stack_base(stack_base
, 1);
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();
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
;
876 MZ_GC_VAR_IN_REG(0, scheme_base_symbol
);
878 /* invoke function from generated and included mzscheme_base.c */
879 declare_modules(environment
);
880 scheme_base_symbol
= scheme_intern_symbol("scheme/base");
882 scheme_namespace_require(scheme_base_symbol
);
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
;
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
);
912 coll_byte_string
= scheme_make_byte_string(MZSCHEME_COLLECTS
);
914 coll_char_string
= scheme_byte_string_to_char_string(coll_byte_string
);
916 coll_path
= scheme_char_string_to_path(coll_char_string
);
918 coll_pair
= scheme_make_pair(coll_path
, scheme_null
);
920 config
= scheme_config
;
922 scheme_set_param(config
, MZCONFIG_COLLECTION_PATHS
, coll_pair
);
928 Scheme_Object
*coll_string
= NULL
;
929 Scheme_Object
*coll_pair
= NULL
;
930 Scheme_Config
*config
= NULL
;
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
);
937 coll_string
= scheme_make_string(MZSCHEME_COLLECTS
);
939 coll_pair
= scheme_make_pair(coll_string
, scheme_null
);
941 config
= scheme_config
;
943 scheme_set_param(config
, MZCONFIG_COLLECTION_PATHS
, coll_pair
);
951 Scheme_Object
*make_security_guard
= NULL
;
953 MZ_GC_VAR_IN_REG(0, make_security_guard
);
956 #if MZSCHEME_VERSION_MAJOR < 400
958 Scheme_Object
*make_security_guard_symbol
= NULL
;
960 MZ_GC_VAR_IN_REG(0, make_security_guard_symbol
);
962 make_security_guard_symbol
= scheme_intern_symbol("make-security-guard");
964 make_security_guard
= scheme_lookup_global(
965 make_security_guard_symbol
, environment
);
969 make_security_guard
= scheme_builtin_value("make-security-guard");
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
;
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
);
984 config
= scheme_config
;
986 args
[0] = scheme_get_param(config
, MZCONFIG_SECURITY_GUARD
);
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
);
994 scheme_set_param(config
, MZCONFIG_SECURITY_GUARD
, guard
);
1001 /* Create buffer and window types for use in Scheme code */
1002 mz_buffer_type
= scheme_make_type("<vim-buffer>");
1004 mz_window_type
= scheme_make_type("<vim-window>");
1006 #ifdef MZ_PRECISE_GC
1007 GC_register_traversers(mz_buffer_type
,
1008 buffer_size_proc
, buffer_mark_proc
, buffer_fixup_proc
,
1010 GC_register_traversers(mz_window_type
,
1011 window_size_proc
, window_mark_proc
, window_fixup_proc
,
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.
1033 #ifdef DYNAMIC_MZSCHEME
1034 if (!mzscheme_enabled(TRUE
))
1036 EMSG(_("E815: Sorry, this command is disabled, the MzScheme libraries could not be loaded."));
1044 Scheme_Config
*config
= NULL
;
1046 MZ_GC_VAR_IN_REG(0, config
);
1048 config
= scheme_config
;
1050 /* recreate ports each call effectively clearing these ones */
1051 curout
= scheme_make_string_output_port();
1053 curerr
= scheme_make_string_output_port();
1055 scheme_set_param(config
, MZCONFIG_OUTPUT_PORT
, curout
);
1057 scheme_set_param(config
, MZCONFIG_ERROR_PORT
, curerr
);
1066 *========================================================================
1067 * 2. External Interface
1068 *========================================================================
1072 * Evaluate command with exception handling
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
;
1082 MZ_GC_VAR_IN_REG(0, value
);
1083 MZ_GC_VAR_IN_REG(1, exn
);
1084 MZ_GC_VAR_IN_REG(2, prim
);
1087 prim
= scheme_make_closed_prim_w_arity(what
, data
, "mzvim", 0, 0);
1089 value
= _apply_thunk_catch_exceptions(prim
, &exn
);
1094 value
= extract_exn_message(exn
);
1098 scheme_display(value
, curerr
); /* Send to stderr-vim */
1103 /* `raise' was called on some arbitrary value */
1107 if (ret
!= NULL
) /* if pointer to retval supported give it up */
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 */
1123 do_mzscheme_command(exarg_T
*eap
, void *data
, Scheme_Closed_Prim
*what
)
1125 if (mzscheme_init())
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
1138 mzscheme_buffer_free(buf_T
*buf
)
1140 if (buf
->b_mzscheme_ref
)
1144 bp
= buf
->b_mzscheme_ref
;
1145 bp
->buf
= INVALID_BUFFER_VALUE
;
1146 buf
->b_mzscheme_ref
= NULL
;
1147 scheme_gc_ptr_ok(bp
);
1153 * Routine called by VIM when deleting a Window
1156 mzscheme_window_free(win_T
*win
)
1158 if (win
->w_mzscheme_ref
)
1161 wp
= win
->w_mzscheme_ref
;
1162 wp
->win
= INVALID_WINDOW_VALUE
;
1163 win
->w_mzscheme_ref
= NULL
;
1164 scheme_gc_ptr_ok(wp
);
1170 * ":mzscheme" (or ":mz")
1173 ex_mzscheme(exarg_T
*eap
)
1177 script
= script_get(eap
, eap
->arg
);
1181 do_mzscheme_command(eap
, eap
->arg
, do_eval
);
1184 do_mzscheme_command(eap
, script
, do_eval
);
1190 static Scheme_Object
*
1191 do_load(void *data
, int noargc
, Scheme_Object
**noargv
)
1193 Scheme_Object
*expr
= NULL
;
1194 Scheme_Object
*result
= NULL
;
1196 Port_Info
*pinfo
= (Port_Info
*)data
;
1199 MZ_GC_VAR_IN_REG(0, expr
);
1200 MZ_GC_VAR_IN_REG(1, result
);
1201 MZ_GC_VAR_IN_REG(2, file
);
1204 file
= (char *)scheme_malloc_fail_ok(scheme_malloc_atomic
, MAXPATHL
+ 1);
1207 /* make Vim expansion */
1208 expand_env((char_u
*)pinfo
->name
, (char_u
*)file
, MAXPATHL
);
1209 pinfo
->port
= scheme_open_input_file(file
, "mzfile");
1211 scheme_count_lines(pinfo
->port
); /* to get accurate read error location*/
1214 /* Like REPL but print only last result */
1215 while (!SCHEME_EOFP(expr
= scheme_read(pinfo
->port
)))
1217 result
= scheme_eval(expr
, environment
);
1221 /* errors will be caught in do_mzscheme_command and ex_mzfile */
1222 scheme_close_input_port(pinfo
->port
);
1231 ex_mzfile(exarg_T
*eap
)
1233 Port_Info pinfo
= {NULL
, NULL
};
1236 MZ_GC_VAR_IN_REG(0, pinfo
.port
);
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
);
1251 *========================================================================
1252 * Exception handling code -- cribbed form the MzScheme sources and
1253 * Matthew Flatt's "Inside PLT MzScheme" document.
1254 *========================================================================
1257 init_exn_catching_apply(void)
1259 if (!exn_catching_apply
)
1263 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
1264 "(cons #t (thunk))))";
1266 exn_catching_apply
= scheme_eval_string(e
, environment
);
1268 exn_p
= scheme_builtin_value("exn?");
1270 exn_message
= scheme_builtin_value("exn-message");
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
)
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
);
1292 *exn
= SCHEME_CDR(v
);
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
);
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 *========================================================================
1318 do_intrnl_output(char *mesg
, long len
, int error
)
1323 p
= strchr(prev
, '\n');
1332 p
= strchr(prev
, '\n');
1342 do_output(char *mesg
, long len
)
1344 do_intrnl_output(mesg
, len
, 0);
1348 do_err_output(char *mesg
, long len
)
1350 do_intrnl_output(mesg
, len
, 1);
1354 do_printf(char *format
, ...)
1356 do_intrnl_output(format
, STRLEN(format
), 1);
1365 buff
= scheme_get_sized_string_output(curerr
, &length
);
1369 do_err_output(buff
, length
);
1373 buff
= scheme_get_sized_string_output(curout
, &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
);
1400 /* (eval {expr-string}) */
1401 static Scheme_Object
*
1402 vim_eval(void *data
, int argc
, Scheme_Object
**argv
)
1405 Vim_Prim
*prim
= (Vim_Prim
*)data
;
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
;
1413 MZ_GC_VAR_IN_REG(0, visited
);
1416 visited
= scheme_make_hash_table(SCHEME_hash_ptr
);
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
);
1431 raise_vim_exn(_("expressions disabled at compile time"));
1433 return scheme_false
;
1438 static Scheme_Object
*
1439 get_range_start(void *data
, int argc
, Scheme_Object
**argv
)
1441 return scheme_make_integer(range_start
);
1445 static Scheme_Object
*
1446 get_range_end(void *data
, int argc
, Scheme_Object
**argv
)
1448 return scheme_make_integer(range_end
);
1452 static Scheme_Object
*
1453 mzscheme_beep(void *data
, int argc
, Scheme_Object
**argv
)
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
;
1470 Scheme_Object
*rval
;
1472 buf_T
*save_curb
= curbuf
;
1473 win_T
*save_curw
= curwin
;
1475 name
= (char_u
*)SCHEME_STR_VAL(GUARANTEE_STRING(prim
->name
, 0));
1479 if (M_global
== NULL
)
1481 MZ_REGISTER_STATIC(M_global
);
1482 M_global
= scheme_intern_symbol("global");
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]);
1498 curbuf
= win
->w_buffer
;
1499 opt_flags
= OPT_LOCAL
;
1502 scheme_wrong_type(prim
->name
, "vim-buffer/window", 1, argc
, argv
);
1505 rc
= get_option_value(name
, &value
, (char_u
**)&strval
, opt_flags
);
1512 return scheme_make_integer_value(value
);
1514 rval
= scheme_make_string(strval
);
1520 raise_vim_exn(_("hidden option"));
1522 raise_vim_exn(_("unknown option"));
1528 /* (set-option {option-changing-string} [buffer/window]) */
1529 static Scheme_Object
*
1530 set_option(void *data
, int argc
, Scheme_Object
**argv
)
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);
1541 if (M_global
== NULL
)
1543 MZ_REGISTER_STATIC(M_global
);
1544 M_global
= scheme_intern_symbol("global");
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]);
1559 curbuf
= win
->w_buffer
;
1560 opt_flags
= OPT_LOCAL
;
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
);
1570 update_screen(NOT_VALID
);
1578 *===========================================================================
1579 * 5. Vim Window-related Manipulation Functions
1580 *===========================================================================
1584 static Scheme_Object
*
1585 get_curr_win(void *data
, int argc
, Scheme_Object
**argv
)
1587 return (Scheme_Object
*)get_vim_curr_window();
1591 static Scheme_Object
*
1592 get_window_count(void *data
, int argc
, Scheme_Object
**argv
)
1597 for (w
= firstwin
; w
!= NULL
; w
= w
->w_next
)
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
;
1608 Scheme_Object
*list
;
1611 buf
= get_buffer_arg(prim
->name
, 0, argc
, argv
);
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
);
1624 static Scheme_Object
*
1625 window_new(win_T
*win
)
1627 vim_mz_window
*self
= NULL
;
1630 MZ_GC_VAR_IN_REG(0, self
);
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 */
1648 win
->w_mzscheme_ref
= self
;
1650 self
->so
.type
= mz_window_type
;
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
;
1665 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
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
;
1679 fnum
= SCHEME_INT_VAL(GUARANTEE_INTEGER(prim
->name
, 0));
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
;
1719 win
= get_window_arg(prim
->name
, 1, argc
, argv
);
1720 height
= SCHEME_INT_VAL(GUARANTEE_INTEGER(prim
->name
, 0));
1723 need_mouse_correct
= TRUE
;
1728 win_setheight(height
);
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
;
1755 win
= get_window_arg(prim
->name
, 1, argc
, argv
);
1756 width
= SCHEME_INT_VAL(GUARANTEE_INTEGER(prim
->name
, 0));
1759 need_mouse_correct
= TRUE
;
1764 win_setwidth(width
);
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
;
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
;
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
);
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
;
1831 Scheme_Object
*onum
;
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
);
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
;
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
;
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
)
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
;
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
;
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
);
1926 static Scheme_Object
*
1927 get_buffer_count(void *data
, int argc
, Scheme_Object
**argv
)
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
);
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
;
1959 MZ_GC_VAR_IN_REG(0, self
);
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 */
1973 buf
->b_mzscheme_ref
= self
;
1975 self
->so
.type
= mz_buffer_type
;
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
2002 static Scheme_Object
*
2003 get_buffer_line(void *data
, int argc
, Scheme_Object
**argv
)
2005 Vim_Prim
*prim
= (Vim_Prim
*)data
;
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
);
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
;
2032 Scheme_Object
*list
= NULL
;
2035 MZ_GC_VAR_IN_REG(0, list
);
2038 buf
= get_buffer_arg(prim
->name
, 2, argc
, argv
);
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
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
));
2062 /* Set the list item */
2063 list
= scheme_make_pair(str
, 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
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
;
2093 Scheme_Object
*line
= NULL
;
2098 MZ_GC_VAR_IN_REG(0, line
);
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
);
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
;
2118 if (u_savedel((linenr_T
)n
, 1L) == FAIL
)
2121 raise_vim_exn(_("cannot save undo information"));
2123 else if (ml_delete((linenr_T
)n
, FALSE
) == FAIL
)
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);
2140 /* Otherwise it's a line */
2141 buf_T
*savebuf
= curbuf
;
2143 save
= string_to_line(line
);
2147 if (u_savesub((linenr_T
)n
) == FAIL
)
2151 raise_vim_exn(_("cannot save undo information"));
2153 else if (ml_replace((linenr_T
)n
, (char_u
*)save
, TRUE
) == FAIL
)
2157 raise_vim_exn(_("cannot replace line"));
2162 changed_bytes((linenr_T
)n
, 0);
2167 /* Check that the cursor is not beyond the end of the line now. */
2168 if (buf
->buf
== curwin
->w_buffer
)
2178 free_array(char **array
)
2180 char **curr
= array
;
2181 while (*curr
!= NULL
)
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
;
2214 MZ_GC_VAR_IN_REG(0, line_list
);
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
);
2228 if (old_len
< 0) /* process inverse values wisely */
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
;
2245 if (u_savedel((linenr_T
)lo
, (long)old_len
) == FAIL
)
2248 raise_vim_exn(_("cannot save undo information"));
2252 for (i
= 0; i
< old_len
; i
++)
2253 if (ml_delete((linenr_T
)lo
, FALSE
) == FAIL
)
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
);
2271 buf_T
*savebuf
= curbuf
;
2274 new_len
= scheme_proper_list_length(line_list
);
2276 if (new_len
< 0) /* improper or cyclic list */
2277 scheme_wrong_type(prim
->name
, "proper list",
2281 char **array
= NULL
;
2282 Scheme_Object
*line
= NULL
;
2283 Scheme_Object
*rest
= NULL
;
2286 MZ_GC_VAR_IN_REG(0, line
);
2287 MZ_GC_VAR_IN_REG(1, rest
);
2290 array
= (char **)alloc(new_len
* sizeof(char *));
2291 vim_memset(array
, 0, new_len
* sizeof(char *));
2294 for (i
= 0; i
< new_len
; ++i
)
2296 line
= SCHEME_CAR(rest
);
2297 rest
= SCHEME_CDR(rest
);
2298 if (!SCHEME_STRINGP(line
))
2301 scheme_wrong_type(prim
->name
, "string-list", 2, argc
, argv
);
2303 array
[i
] = string_to_line(line
);
2308 if (u_save((linenr_T
)(lo
-1), (linenr_T
)hi
) == FAIL
)
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
)
2326 raise_vim_exn(_("cannot delete line"));
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
)
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
2351 if (ml_append((linenr_T
)(lo
+ i
- 1),
2352 (char_u
*)array
[i
], 0, FALSE
) == FAIL
)
2356 raise_vim_exn(_("cannot insert line"));
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
);
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
;
2403 MZ_GC_VAR_IN_REG(0, list
);
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));
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
);
2429 if (u_save((linenr_T
)n
, (linenr_T
)(n
+1)) == FAIL
)
2433 raise_vim_exn(_("cannot save undo information"));
2435 else if (ml_append((linenr_T
)n
, (char_u
*)str
, 0, FALSE
) == FAIL
)
2439 raise_vim_exn(_("cannot insert line"));
2444 appended_lines_mark((linenr_T
)n
, 1L);
2448 update_screen(VALID
);
2456 size
= scheme_proper_list_length(list
);
2458 if (size
< 0) /* improper or cyclic list */
2459 scheme_wrong_type(prim
->name
, "proper list",
2463 Scheme_Object
*line
= NULL
;
2464 Scheme_Object
*rest
= NULL
;
2466 buf_T
*savebuf
= curbuf
;
2469 MZ_GC_VAR_IN_REG(0, line
);
2470 MZ_GC_VAR_IN_REG(1, rest
);
2473 array
= (char **)alloc(size
* sizeof(char *));
2474 vim_memset(array
, 0, size
* sizeof(char *));
2477 for (i
= 0; i
< size
; ++i
)
2479 line
= SCHEME_CAR(rest
);
2480 rest
= SCHEME_CDR(rest
);
2481 array
[i
] = string_to_line(line
);
2486 if (u_save((linenr_T
)n
, (linenr_T
)(n
+ 1)) == FAIL
)
2490 raise_vim_exn(_("cannot save undo information"));
2494 for (i
= 0; i
< size
; ++i
)
2495 if (ml_append((linenr_T
)(n
+ i
), (char_u
*)array
[i
],
2500 raise_vim_exn(_("cannot insert line"));
2504 appended_lines_mark((linenr_T
)n
, (long)i
);
2509 update_screen(VALID
);
2521 static Scheme_Object
*
2522 vim_bufferp(void *data
, int argc
, Scheme_Object
**argv
)
2524 if (SCHEME_VIMBUFFERP(argv
[0]))
2527 return scheme_false
;
2531 static Scheme_Object
*
2532 vim_windowp(void *data
, int argc
, Scheme_Object
**argv
)
2534 if (SCHEME_VIMWINDOWP(argv
[0]))
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
)
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
)
2559 return scheme_false
;
2563 *===========================================================================
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
2577 string_to_line(Scheme_Object
*obj
)
2579 char *scheme_str
= NULL
;
2580 char *vim_str
= NULL
;
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
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')
2603 vim_str
[i
] = scheme_str
[i
];
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
;
2623 MZ_GC_VAR_IN_REG(0, result
);
2626 /* Avoid infinite recursion */
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
);
2638 if (result
!= NULL
) /* found, do nothing */
2640 else if (vim_value
->v_type
== VAR_STRING
)
2642 result
= scheme_make_string((char *)vim_value
->vval
.v_string
);
2645 else if (vim_value
->v_type
== VAR_NUMBER
)
2647 result
= scheme_make_integer((long)vim_value
->vval
.v_number
);
2651 else if (vim_value
->v_type
== VAR_FLOAT
)
2653 result
= scheme_make_double((double)vim_value
->vval
.v_float
);
2657 else if (vim_value
->v_type
== VAR_LIST
)
2659 list_T
*list
= vim_value
->vval
.v_list
;
2662 if (list
== NULL
|| list
->lv_first
== NULL
)
2663 result
= scheme_null
;
2666 Scheme_Object
*obj
= NULL
;
2669 MZ_GC_VAR_IN_REG(0, obj
);
2672 curr
= list
->lv_last
;
2673 obj
= vim_to_mzscheme(&curr
->li_tv
, depth
+ 1, visited
);
2674 result
= scheme_make_pair(obj
, scheme_null
);
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
);
2687 else if (vim_value
->v_type
== VAR_DICT
)
2689 Scheme_Object
*key
= NULL
;
2690 Scheme_Object
*obj
= NULL
;
2693 MZ_GC_VAR_IN_REG(0, key
);
2694 MZ_GC_VAR_IN_REG(1, obj
);
2697 result
= (Scheme_Object
*)scheme_make_hash_table(SCHEME_hash_ptr
);
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
;
2706 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
2708 if (!HASHITEM_EMPTY(hi
))
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
);
2716 scheme_hash_set((Scheme_Hash_Table
*)result
, key
, obj
);
2725 result
= scheme_void
;
2730 scheme_hash_set(visited
, (Scheme_Object
*)vim_value
, result
);
2739 * Check to see whether a Vim error has been reported, or a keyboard
2740 * interrupt (from vim --> got_int) has been detected.
2743 vim_error_check(void)
2745 return (got_int
|| did_emsg
);
2749 * register Scheme exn:vim
2752 register_vim_exn(void)
2756 Scheme_Object
*struct_exn
= NULL
;
2757 Scheme_Object
*exn_name
= NULL
;
2760 MZ_GC_VAR_IN_REG(0, struct_exn
);
2761 MZ_GC_VAR_IN_REG(1, exn_name
);
2764 exn_name
= scheme_intern_symbol("exn:vim");
2766 struct_exn
= scheme_builtin_value("struct:exn");
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
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
};
2783 MZ_GC_ARRAY_VAR_IN_REG(0, exn_names
, 5);
2784 MZ_GC_ARRAY_VAR_IN_REG(3, exn_values
, 5);
2787 tmp
= scheme_make_struct_names(exn_name
, scheme_null
, 0, &nc
);
2789 mch_memmove(exn_names
, tmp
, nc
* sizeof(Scheme_Object
*));
2792 tmp
= scheme_make_struct_values(vim_exn
, exn_names
, nc
, 0);
2793 mch_memmove(exn_values
, tmp
, nc
* sizeof(Scheme_Object
*));
2796 for (i
= 0; i
< nc
; i
++)
2798 scheme_add_global_symbol(exn_names
[i
],
2799 exn_values
[i
], environment
);
2808 * raise exn:vim, may be with additional info string
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
;
2818 MZ_GC_ARRAY_VAR_IN_REG(0, argv
, 2);
2819 MZ_GC_VAR_IN_REG(3, exn
);
2822 if (add_info
!= NULL
)
2824 char *c_string
= NULL
;
2825 Scheme_Object
*byte_string
= NULL
;
2826 Scheme_Object
*info
= NULL
;
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
);
2834 info
= scheme_make_string(add_info
);
2836 c_string
= scheme_format(fmt
, STRLEN(fmt
), 1, &info
, NULL
);
2838 byte_string
= scheme_make_string(c_string
);
2840 argv
[0] = scheme_byte_string_to_char_string(byte_string
);
2842 SCHEME_SET_IMMUTABLE(argv
[0]);
2846 argv
[0] = scheme_make_string(_("Vim error"));
2849 #if MZSCHEME_VERSION_MAJOR < 360
2850 argv
[1] = scheme_current_continuation_marks();
2853 argv
[1] = scheme_current_continuation_marks(NULL
);
2857 exn
= scheme_make_struct_instance(vim_exn
, 2, argv
);
2864 raise_if_error(void)
2866 if (vim_error_check())
2867 raise_vim_exn(NULL
);
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
)
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
]);
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
)
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
]);
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"));
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"));
2926 mzthreads_allowed(void)
2928 return mz_threads_allow
;
2932 line_in_range(linenr_T lnum
, buf_T
*buf
)
2934 return (lnum
> 0 && lnum
<= buf
->b_ml
.ml_line_count
);
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
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
2957 if (curwin
->w_cursor
.lnum
>= hi
)
2959 curwin
->w_cursor
.lnum
+= extra
;
2964 curwin
->w_cursor
.lnum
= lo
;
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},
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},
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
);
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
);
3047 return (vim_mz_window
*)curwin
->w_mzscheme_ref
;
3054 Scheme_Env
*mod
= NULL
;
3055 Scheme_Object
*vimext_symbol
= NULL
;
3056 Scheme_Object
*closed_prim
= NULL
;
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
);
3064 vimext_symbol
= scheme_intern_symbol("vimext");
3066 mod
= scheme_primitive_module(vimext_symbol
, environment
);
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
);
3077 scheme_finish_primitive_module(mod
);
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
;
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
)
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");
3111 MZ_REGISTER_STATIC(M_read
);
3112 M_read
= scheme_intern_symbol("read");
3115 if (M_execute
== NULL
)
3117 MZ_REGISTER_STATIC(M_execute
);
3118 M_execute
= scheme_intern_symbol("execute");
3121 if (M_delete
== NULL
)
3123 MZ_REGISTER_STATIC(M_delete
);
3124 M_delete
= scheme_intern_symbol("delete");
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
);
3142 static Scheme_Object
*
3143 sandbox_network_guard(int argc
, Scheme_Object
**argv
)