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