blob: 9efb621843f50aa3769eac56bfea524daee7a402 [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,
Bram Moolenaarf15f9432007-06-28 11:07:21 +0000776 scheme_build_list(0, scheme_make_string(MZSCHEME_COLLECTS)));
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000777#endif
Bram Moolenaar555b2802005-05-19 21:08:39 +0000778#ifdef HAVE_SANDBOX
779 /* setup sandbox guards */
780 proc_make_security_guard = scheme_lookup_global(
781 scheme_intern_symbol("make-security-guard"),
782 environment);
783 if (proc_make_security_guard != NULL)
784 {
785 Scheme_Object *args[3];
786 Scheme_Object *guard;
787 args[0] = scheme_get_param(scheme_config, MZCONFIG_SECURITY_GUARD);
788 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
789 "sandbox-file-guard", 3, 3);
790 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
791 "sandbox-network-guard", 4, 4);
792 guard = scheme_apply(proc_make_security_guard, 3, args);
793 scheme_set_param(scheme_config, MZCONFIG_SECURITY_GUARD, guard);
794 }
795#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000796 /* Create buffer and window types for use in Scheme code */
797 mz_buffer_type = scheme_make_type("<vim-buffer>");
798 mz_window_type = scheme_make_type("<vim-window>");
799
800 register_vim_exn(environment);
801 make_modules(environment);
802
803 /*
804 * setup callback to receive notifications
805 * whether thread scheduling is (or not) required
806 */
807 scheme_notify_multithread = notify_multithread;
808 initialized = 1;
809}
810
811/*
812 * This routine is called for each new invocation of MzScheme
813 * to make sure things are properly initialized.
814 */
815 static int
816mzscheme_init(void)
817{
818 int do_require = FALSE;
819
820 if (!initialized)
821 {
822 do_require = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000823#ifdef DYNAMIC_MZSCHEME
824 if (!mzscheme_enabled(TRUE))
825 {
826 EMSG(_("???: Sorry, this command is disabled, the MzScheme library could not be loaded."));
827 return -1;
828 }
829#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000830 startup_mzscheme();
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000831
832 if (mzscheme_io_init())
833 return -1;
834
835 }
836 /* recreate ports each call effectivelly clearing these ones */
837 curout = scheme_make_string_output_port();
838 curerr = scheme_make_string_output_port();
839 scheme_set_param(scheme_config, MZCONFIG_OUTPUT_PORT, curout);
840 scheme_set_param(scheme_config, MZCONFIG_ERROR_PORT, curerr);
841
842 if (do_require)
843 {
844 /* auto-instantiate in basic env */
845 eval_in_namespace("(require (prefix vimext: vimext))", do_eval,
846 environment, NULL);
847 }
848
849 return 0;
850}
851
852/*
853 * This routine fills the namespace with various important routines that can
854 * be used within MzScheme.
855 */
856 static void
857mzscheme_interface_init(vim_mz_buffer *mzbuff)
858{
859 Scheme_Object *attach;
860
861 mzbuff->env = (Scheme_Env *)scheme_make_namespace(0, NULL);
862
863 /*
864 * attach instantiated modules from global namespace
865 * so they can be easily instantiated in the buffer namespace
866 */
867 attach = scheme_lookup_global(
868 scheme_intern_symbol("namespace-attach-module"),
869 environment);
870
871 if (attach != NULL)
872 {
873 Scheme_Object *ret;
874 Scheme_Object *args[2];
875
876 args[0] = (Scheme_Object *)environment;
877 args[1] = scheme_intern_symbol("vimext");
878
879 ret = (Scheme_Object *)mzvim_apply(attach, 2, args);
880 }
881
882 add_vim_exn(mzbuff->env);
883}
884
885/*
886 *========================================================================
887 * 2. External Interface
888 *========================================================================
889 */
890
891/*
892 * Evaluate command in namespace with exception handling
893 */
894 static int
895eval_in_namespace(void *data, Scheme_Closed_Prim *what, Scheme_Env *env,
896 Scheme_Object **ret)
897{
898 Scheme_Object *value;
899 Scheme_Object *exn;
900 Cmd_Info info; /* closure info */
901
902 info.data = data;
903 info.env = env;
904
905 scheme_set_param(scheme_config, MZCONFIG_ENV,
906 (Scheme_Object *) env);
907 /*
908 * ensure all evaluations will be in current buffer namespace,
909 * the second argument to scheme_eval_string isn't enough!
910 */
911 value = _apply_thunk_catch_exceptions(
912 scheme_make_closed_prim_w_arity(what, &info, "mzvim", 0, 0),
913 &exn);
914
915 if (!value)
916 {
917 value = extract_exn_message(exn);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000918 /* Got an exn? */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000919 if (value)
920 {
921 scheme_display(value, curerr); /* Send to stderr-vim */
922 do_flush();
923 }
924 /* `raise' was called on some arbitrary value */
925 return FAIL;
926 }
927
928 if (ret != NULL) /* if pointer to retval supported give it up */
929 *ret = value;
930 /* Print any result, as long as it's not a void */
931 else if (!SCHEME_VOIDP(value))
932 scheme_display(value, curout); /* Send to stdout-vim */
933
934 do_flush();
935 return OK;
936}
937
938/* :mzscheme */
939 static int
940do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
941{
942 if (mzscheme_init())
943 return FAIL;
944
945 range_start = eap->line1;
946 range_end = eap->line2;
947
948 return eval_in_namespace(data, what, get_vim_curr_buffer()->env, NULL);
949}
950
951/*
952 * Routine called by VIM when deleting a buffer
953 */
954 void
955mzscheme_buffer_free(buf_T *buf)
956{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000957 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000958 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000959 vim_mz_buffer *bp;
960
Bram Moolenaare344bea2005-09-01 20:46:49 +0000961 bp = buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000962 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000963 buf->b_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000964 scheme_gc_ptr_ok(bp);
965 }
966}
967
968/*
969 * Routine called by VIM when deleting a Window
970 */
971 void
972mzscheme_window_free(win_T *win)
973{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000974 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000975 {
976 vim_mz_window *wp;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000977 wp = win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000978 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000979 win->w_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000980 scheme_gc_ptr_ok(wp);
981 }
982}
983
984/*
985 * ":mzscheme" (or ":mz")
986 */
987 void
988ex_mzscheme(exarg_T *eap)
989{
990 char_u *script;
991
992 script = script_get(eap, eap->arg);
993 if (!eap->skip)
994 {
995 if (script == NULL)
996 do_mzscheme_command(eap, eap->arg, do_eval);
997 else
998 {
999 do_mzscheme_command(eap, script, do_eval);
1000 vim_free(script);
1001 }
1002 }
1003}
1004
1005/* eval MzScheme string */
1006 void *
1007mzvim_eval_string(char_u *str)
1008{
1009 Scheme_Object *ret = NULL;
1010 if (mzscheme_init())
1011 return FAIL;
1012
1013 eval_in_namespace(str, do_eval, get_vim_curr_buffer()->env, &ret);
1014 return ret;
1015}
1016
1017/*
1018 * apply MzScheme procedure with arguments,
1019 * handling errors
1020 */
1021 Scheme_Object *
1022mzvim_apply(Scheme_Object *proc, int argc, Scheme_Object **argv)
1023{
1024 Apply_Info data;
1025 Scheme_Object *ret = NULL;
1026
1027 if (mzscheme_init())
1028 return FAIL;
1029
1030 data.proc = proc;
1031 data.argc = argc;
1032 data.argv = argv;
1033
1034 eval_in_namespace(&data, do_apply, get_vim_curr_buffer()->env, &ret);
1035 return ret;
1036}
1037
1038 static Scheme_Object *
1039do_load(void *data, int noargc, Scheme_Object **noargv)
1040{
1041 Cmd_Info *info = (Cmd_Info *)data;
1042 Scheme_Object *result = scheme_void;
1043 Scheme_Object *expr;
1044 char_u *file = scheme_malloc_fail_ok(
1045 scheme_malloc_atomic, MAXPATHL + 1);
1046 Port_Info *pinfo = (Port_Info *)(info->data);
1047
1048 /* make Vim expansion */
1049 expand_env((char_u *)pinfo->name, file, MAXPATHL);
1050 /* scheme_load looks strange working with namespaces and error handling*/
1051 pinfo->port = scheme_open_input_file(file, "mzfile");
1052 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1053
1054 /* Like REPL but print only last result */
1055 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
1056 result = scheme_eval(expr, info->env);
1057
1058 /* errors will be caught in do_mzscheme_comamnd and ex_mzfile */
1059 scheme_close_input_port(pinfo->port);
1060 pinfo->port = NULL;
1061 return result;
1062}
1063
1064/* :mzfile */
1065 void
1066ex_mzfile(exarg_T *eap)
1067{
1068 Port_Info pinfo;
1069
1070 pinfo.name = (char *)eap->arg;
1071 pinfo.port = NULL;
1072 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1073 && pinfo.port != NULL) /* looks like port was not closed */
1074 scheme_close_input_port(pinfo.port);
1075}
1076
1077
1078/*
1079 *========================================================================
1080 * Exception handling code -- cribbed form the MzScheme sources and
1081 * Matthew Flatt's "Inside PLT MzScheme" document.
1082 *========================================================================
1083 */
1084 static void
1085init_exn_catching_apply(void)
1086{
1087 if (!exn_catching_apply)
1088 {
1089 char *e =
1090 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001091 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001092 "(cons #t (thunk))))";
1093
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001094 /* make sure we have a namespace with the standard syntax: */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001095 Scheme_Env *env = (Scheme_Env *)scheme_make_namespace(0, NULL);
1096 add_vim_exn(env);
1097
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001098 exn_catching_apply = scheme_eval_string(e, env);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001099 exn_p = scheme_lookup_global(scheme_intern_symbol("exn?"), env);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001100 exn_message = scheme_lookup_global(
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001101 scheme_intern_symbol("exn-message"), env);
1102 }
1103}
1104
1105/*
1106 * This function applies a thunk, returning the Scheme value if there's
1107 * no exception, otherwise returning NULL and setting *exn to the raised
1108 * value (usually an exn structure).
1109 */
1110 static Scheme_Object *
1111_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1112{
1113 Scheme_Object *v;
1114
1115 init_exn_catching_apply();
1116
1117 v = _scheme_apply(exn_catching_apply, 1, &f);
1118 /* v is a pair: (cons #t value) or (cons #f exn) */
1119
1120 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1121 return SCHEME_CDR(v);
1122 else
1123 {
1124 *exn = SCHEME_CDR(v);
1125 return NULL;
1126 }
1127}
1128
1129 static Scheme_Object *
1130extract_exn_message(Scheme_Object *v)
1131{
1132 init_exn_catching_apply();
1133
1134 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1135 return _scheme_apply(exn_message, 1, &v);
1136 else
1137 return NULL; /* Not an exn structure */
1138}
1139
1140 static Scheme_Object *
1141do_eval(void *s, int noargc, Scheme_Object **noargv)
1142{
1143 Cmd_Info *info = (Cmd_Info *)s;
1144
1145 return scheme_eval_string_all((char *)(info->data), info->env, TRUE);
1146}
1147
1148 static Scheme_Object *
1149do_apply(void *a, int noargc, Scheme_Object **noargv)
1150{
1151 Apply_Info *info = (Apply_Info *)(((Cmd_Info *)a)->data);
1152
1153 return scheme_apply(info->proc, info->argc, info->argv);
1154}
1155
1156/*
1157 *========================================================================
1158 * 3. MzScheme I/O Handlers
1159 *========================================================================
1160 */
1161 static void
1162do_intrnl_output(char *mesg, long len, int error)
1163{
1164 char *p, *prev;
1165
1166 prev = mesg;
1167 p = strchr(prev, '\n');
1168 while (p)
1169 {
1170 *p = '\0';
1171 if (error)
1172 EMSG(prev);
1173 else
1174 MSG(prev);
1175 prev = p + 1;
1176 p = strchr(prev, '\n');
1177 }
1178
1179 if (error)
1180 EMSG(prev);
1181 else
1182 MSG(prev);
1183}
1184
1185 static void
1186do_output(char *mesg, long len)
1187{
1188 do_intrnl_output(mesg, len, 0);
1189}
1190
1191 static void
1192do_err_output(char *mesg, long len)
1193{
1194 do_intrnl_output(mesg, len, 1);
1195}
1196
1197 static void
1198do_printf(char *format, ...)
1199{
1200 do_intrnl_output(format, STRLEN(format), 1);
1201}
1202
1203 static void
1204do_flush(void)
1205{
1206 char *buff;
1207 long length;
1208
1209 buff = scheme_get_sized_string_output(curerr, &length);
1210 if (length)
1211 {
1212 do_err_output(buff, length);
1213 return;
1214 }
1215
1216 buff = scheme_get_sized_string_output(curout, &length);
1217 if (length)
1218 do_output(buff, length);
1219}
1220
1221 static int
1222mzscheme_io_init(void)
1223{
1224 /* Nothing needed so far... */
1225 return 0;
1226}
1227
1228/*
1229 *========================================================================
1230 * 4. Implementation of the Vim Features for MzScheme
1231 *========================================================================
1232 */
1233
1234/* (command {command-string}) */
1235 static Scheme_Object *
1236vim_command(void *data, int argc, Scheme_Object **argv)
1237{
1238 Vim_Prim *prim = (Vim_Prim *)data;
1239 char *cmd = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1240
1241 /* may be use do_cmdline_cmd? */
1242 do_cmdline((char_u *)cmd, NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
1243 update_screen(VALID);
1244
1245 raise_if_error();
1246 return scheme_void;
1247}
1248
1249/* (eval {expr-string}) */
1250 static Scheme_Object *
1251vim_eval(void *data, int argc, Scheme_Object **argv)
1252{
1253#ifdef FEAT_EVAL
1254 Vim_Prim *prim = (Vim_Prim *)data;
1255 char *expr;
1256 char *str;
1257 Scheme_Object *result;
1258
1259 expr = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1260
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001261 str = (char *)eval_to_string((char_u *)expr, NULL, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001262
1263 if (str == NULL)
1264 raise_vim_exn(_("invalid expression"));
1265
1266 result = scheme_make_string(str);
1267
1268 vim_free(str);
1269
1270 return result;
1271#else
1272 raise_vim_exn(_("expressions disabled at compile time"));
1273 /* unreachable */
1274 return scheme_false;
1275#endif
1276}
1277
1278/* (range-start) */
1279 static Scheme_Object *
1280get_range_start(void *data, int argc, Scheme_Object **argv)
1281{
1282 return scheme_make_integer(range_start);
1283}
1284
1285/* (range-end) */
1286 static Scheme_Object *
1287get_range_end(void *data, int argc, Scheme_Object **argv)
1288{
1289 return scheme_make_integer(range_end);
1290}
1291
1292/* (beep) */
1293 static Scheme_Object *
1294mzscheme_beep(void *data, int argc, Scheme_Object **argv)
1295{
1296 vim_beep();
1297 return scheme_void;
1298}
1299
1300static Scheme_Object *M_global = NULL;
1301
1302/* (get-option {option-name}) [buffer/window] */
1303 static Scheme_Object *
1304get_option(void *data, int argc, Scheme_Object **argv)
1305{
1306 Vim_Prim *prim = (Vim_Prim *)data;
1307 char_u *name;
1308 long value;
1309 char_u *strval;
1310 int rc;
1311 Scheme_Object *rval;
1312 int opt_flags = 0;
1313 buf_T *save_curb = curbuf;
1314 win_T *save_curw = curwin;
1315
1316 name = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1317
1318 if (argc > 1)
1319 {
1320 if (M_global == NULL)
1321 {
1322 MZ_REGISTER_STATIC(M_global);
1323 M_global = scheme_intern_symbol("global");
1324 }
1325
1326 if (argv[1] == M_global)
1327 opt_flags = OPT_GLOBAL;
1328 else if (SCHEME_VIMBUFFERP(argv[1]))
1329 {
1330 curbuf = get_valid_buffer(argv[1]);
1331 opt_flags = OPT_LOCAL;
1332 }
1333 else if (SCHEME_VIMWINDOWP(argv[1]))
1334 {
1335 win_T *win = get_valid_window(argv[1]);
1336
1337 curwin = win;
1338 curbuf = win->w_buffer;
1339 opt_flags = OPT_LOCAL;
1340 }
1341 else
1342 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1343 }
1344
1345 rc = get_option_value(name, &value, &strval, opt_flags);
1346 curbuf = save_curb;
1347 curwin = save_curw;
1348
1349 switch (rc)
1350 {
1351 case 1:
1352 return scheme_make_integer_value(value);
1353 case 0:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001354 rval = scheme_make_string(strval);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001355 vim_free(strval);
1356 return rval;
1357 case -1:
1358 case -2:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001359 raise_vim_exn(_("hidden option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001360 case -3:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001361 raise_vim_exn(_("unknown option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001362 }
1363 /* unreachable */
1364 return scheme_void;
1365}
1366
1367/* (set-option {option-changing-string} [buffer/window]) */
1368 static Scheme_Object *
1369set_option(void *data, int argc, Scheme_Object **argv)
1370{
1371 char_u *cmd;
1372 int opt_flags = 0;
1373 buf_T *save_curb = curbuf;
1374 win_T *save_curw = curwin;
1375 Vim_Prim *prim = (Vim_Prim *)data;
1376
1377 GUARANTEE_STRING(prim->name, 0);
1378 if (argc > 1)
1379 {
1380 if (M_global == NULL)
1381 {
1382 MZ_REGISTER_STATIC(M_global);
1383 M_global = scheme_intern_symbol("global");
1384 }
1385
1386 if (argv[1] == M_global)
1387 opt_flags = OPT_GLOBAL;
1388 else if (SCHEME_VIMBUFFERP(argv[1]))
1389 {
1390 curbuf = get_valid_buffer(argv[1]);
1391 opt_flags = OPT_LOCAL;
1392 }
1393 else if (SCHEME_VIMWINDOWP(argv[1]))
1394 {
1395 win_T *win = get_valid_window(argv[1]);
1396 curwin = win;
1397 curbuf = win->w_buffer;
1398 opt_flags = OPT_LOCAL;
1399 }
1400 else
1401 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1402 }
1403
1404 /* do_set can modify cmd, make copy */
1405 cmd = vim_strsave((char_u *)SCHEME_STR_VAL(argv[0]));
1406 do_set(cmd, opt_flags);
1407 vim_free(cmd);
1408 update_screen(NOT_VALID);
1409 curbuf = save_curb;
1410 curwin = save_curw;
1411 raise_if_error();
1412 return scheme_void;
1413}
1414
1415/*
1416 *===========================================================================
1417 * 5. Vim Window-related Manipulation Functions
1418 *===========================================================================
1419 */
1420
1421/* (curr-win) */
1422 static Scheme_Object *
1423get_curr_win(void *data, int argc, Scheme_Object **argv)
1424{
1425 return (Scheme_Object *)get_vim_curr_window();
1426}
1427
1428/* (win-count) */
1429 static Scheme_Object *
1430get_window_count(void *data, int argc, Scheme_Object **argv)
1431{
1432 win_T *w;
1433 int n = 0;
1434
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435 for (w = firstwin; w != NULL; w = w->w_next)
1436 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001437 return scheme_make_integer(n);
1438}
1439
1440/* (get-win-list [buffer]) */
1441 static Scheme_Object *
1442get_window_list(void *data, int argc, Scheme_Object **argv)
1443{
1444 Vim_Prim *prim = (Vim_Prim *)data;
1445 vim_mz_buffer *buf;
1446 Scheme_Object *list;
1447 win_T *w;
1448
1449 buf = get_buffer_arg(prim->name, 0, argc, argv);
1450 list = scheme_null;
1451
Bram Moolenaarf740b292006-02-16 22:11:02 +00001452 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001453 if (w->w_buffer == buf->buf)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001454 list = scheme_make_pair(window_new(w), list);
1455
1456 return list;
1457}
1458
1459 static Scheme_Object *
1460window_new(win_T *win)
1461{
1462 vim_mz_window *self;
1463
1464 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001465 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001466 * then we can get at it in win_free() in vim.
1467 *
1468 * On a win_free() we set the Scheme object's win_T *field
1469 * to an invalid value. We trap all uses of a window
1470 * object, and reject them if the win_T *field is invalid.
1471 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001472 if (win->w_mzscheme_ref != NULL)
1473 return win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001474
1475 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_window));
1476
1477 vim_memset(self, 0, sizeof(vim_mz_window));
1478 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001479 win->w_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001480 self->win = win;
1481 self->tag = mz_window_type;
1482
1483 return (Scheme_Object *)(self);
1484}
1485
1486/* (get-win-num [window]) */
1487 static Scheme_Object *
1488get_window_num(void *data, int argc, Scheme_Object **argv)
1489{
1490 Vim_Prim *prim = (Vim_Prim *)data;
1491 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
1492 int nr = 1;
1493 win_T *wp;
1494
1495 for (wp = firstwin; wp != win; wp = wp->w_next)
1496 ++nr;
1497
1498 return scheme_make_integer(nr);
1499}
1500
1501/* (get-win-by-num {windownum}) */
1502 static Scheme_Object *
1503get_window_by_num(void *data, int argc, Scheme_Object **argv)
1504{
1505 Vim_Prim *prim = (Vim_Prim *)data;
1506 win_T *win;
1507 int fnum;
1508
1509 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1510 if (fnum < 1)
1511 scheme_signal_error(_("window index is out of range"));
1512
Bram Moolenaarf740b292006-02-16 22:11:02 +00001513 for (win = firstwin; win != NULL; win = win->w_next, --fnum)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001514 if (fnum == 1) /* to be 1-based */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001515 return window_new(win);
1516
1517 return scheme_false;
1518}
1519
1520/* (get-win-buffer [window]) */
1521 static Scheme_Object *
1522get_window_buffer(void *data, int argc, Scheme_Object **argv)
1523{
1524 Vim_Prim *prim = (Vim_Prim *)data;
1525 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1526
1527 return buffer_new(win->win->w_buffer);
1528}
1529
1530/* (get-win-height [window]) */
1531 static Scheme_Object *
1532get_window_height(void *data, int argc, Scheme_Object **argv)
1533{
1534 Vim_Prim *prim = (Vim_Prim *)data;
1535 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1536
1537 return scheme_make_integer(win->win->w_height);
1538}
1539
1540/* (set-win-height {height} [window]) */
1541 static Scheme_Object *
1542set_window_height(void *data, int argc, Scheme_Object **argv)
1543{
1544 Vim_Prim *prim = (Vim_Prim *)data;
1545 vim_mz_window *win;
1546 win_T *savewin;
1547 int height;
1548
1549 win = get_window_arg(prim->name, 1, argc, argv);
1550 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1551
1552#ifdef FEAT_GUI
1553 need_mouse_correct = TRUE;
1554#endif
1555
1556 savewin = curwin;
1557 curwin = win->win;
1558 win_setheight(height);
1559 curwin = savewin;
1560
1561 raise_if_error();
1562 return scheme_void;
1563}
1564
1565#ifdef FEAT_VERTSPLIT
1566/* (get-win-width [window]) */
1567 static Scheme_Object *
1568get_window_width(void *data, int argc, Scheme_Object **argv)
1569{
1570 Vim_Prim *prim = (Vim_Prim *)data;
1571 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1572
1573 return scheme_make_integer(W_WIDTH(win->win));
1574}
1575
1576/* (set-win-width {width} [window]) */
1577 static Scheme_Object *
1578set_window_width(void *data, int argc, Scheme_Object **argv)
1579{
1580 Vim_Prim *prim = (Vim_Prim *)data;
1581 vim_mz_window *win;
1582 win_T *savewin;
1583 int width = 0;
1584
1585 win = get_window_arg(prim->name, 1, argc, argv);
1586 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1587
1588# ifdef FEAT_GUI
1589 need_mouse_correct = TRUE;
1590# endif
1591
1592 savewin = curwin;
1593 curwin = win->win;
1594 win_setwidth(width);
1595 curwin = savewin;
1596
1597 raise_if_error();
1598 return scheme_void;
1599}
1600#endif
1601
1602/* (get-cursor [window]) -> (line . col) */
1603 static Scheme_Object *
1604get_cursor(void *data, int argc, Scheme_Object **argv)
1605{
1606 Vim_Prim *prim = (Vim_Prim *)data;
1607 vim_mz_window *win;
1608 pos_T pos;
1609
1610 win = get_window_arg(prim->name, 0, argc, argv);
1611 pos = win->win->w_cursor;
1612 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
1613 scheme_make_integer_value((long)pos.col + 1));
1614}
1615
1616/* (set-cursor (line . col) [window]) */
1617 static Scheme_Object *
1618set_cursor(void *data, int argc, Scheme_Object **argv)
1619{
1620 Vim_Prim *prim = (Vim_Prim *)data;
1621 vim_mz_window *win;
1622 long lnum = 0;
1623 long col = 0;
1624
Bram Moolenaar555b2802005-05-19 21:08:39 +00001625#ifdef HAVE_SANDBOX
1626 sandbox_check();
1627#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001628 win = get_window_arg(prim->name, 1, argc, argv);
1629 GUARANTEE_PAIR(prim->name, 0);
1630
1631 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
1632 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
1633 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
1634
1635 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
1636 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
1637
1638 check_line_range(lnum, win->win->w_buffer);
1639 /* don't know how to catch invalid column value */
1640
1641 win->win->w_cursor.lnum = lnum;
1642 win->win->w_cursor.col = col;
1643 update_screen(VALID);
1644
1645 raise_if_error();
1646 return scheme_void;
1647}
1648/*
1649 *===========================================================================
1650 * 6. Vim Buffer-related Manipulation Functions
1651 * Note that each buffer should have its own private namespace.
1652 *===========================================================================
1653 */
1654
1655/* (open-buff {filename}) */
1656 static Scheme_Object *
1657mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
1658{
1659 Vim_Prim *prim = (Vim_Prim *)data;
1660 char *fname;
1661 int num = 0;
1662 Scheme_Object *onum;
1663
Bram Moolenaar555b2802005-05-19 21:08:39 +00001664#ifdef HAVE_SANDBOX
1665 sandbox_check();
1666#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001667 fname = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1668 /* TODO make open existing file */
1669 num = buflist_add(fname, BLN_LISTED | BLN_CURBUF);
1670
1671 if (num == 0)
1672 raise_vim_exn(_("couldn't open buffer"));
1673
1674 onum = scheme_make_integer(num);
1675 return get_buffer_by_num(data, 1, &onum);
1676}
1677
1678/* (get-buff-by-num {buffernum}) */
1679 static Scheme_Object *
1680get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
1681{
1682 Vim_Prim *prim = (Vim_Prim *)data;
1683 buf_T *buf;
1684 int fnum;
1685
1686 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1687
1688 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001689 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001690 return buffer_new(buf);
1691
1692 return scheme_false;
1693}
1694
1695/* (get-buff-by-name {buffername}) */
1696 static Scheme_Object *
1697get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
1698{
1699 Vim_Prim *prim = (Vim_Prim *)data;
1700 buf_T *buf;
1701 char_u *fname;
1702
1703 fname = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1704
1705 for (buf = firstbuf; buf; buf = buf->b_next)
1706 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1707 /* empty string */
1708 {
1709 if (fname[0] == NUL)
1710 return buffer_new(buf);
1711 }
1712 else if (!fnamecmp(buf->b_ffname, fname)
1713 || !fnamecmp(buf->b_sfname, fname))
1714 /* either short or long filename matches */
1715 return buffer_new(buf);
1716
1717 return scheme_false;
1718}
1719
1720/* (get-next-buff [buffer]) */
1721 static Scheme_Object *
1722get_next_buffer(void *data, int argc, Scheme_Object **argv)
1723{
1724 Vim_Prim *prim = (Vim_Prim *)data;
1725 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1726
1727 if (buf->b_next == NULL)
1728 return scheme_false;
1729 else
1730 return buffer_new(buf->b_next);
1731}
1732
1733/* (get-prev-buff [buffer]) */
1734 static Scheme_Object *
1735get_prev_buffer(void *data, int argc, Scheme_Object **argv)
1736{
1737 Vim_Prim *prim = (Vim_Prim *)data;
1738 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1739
1740 if (buf->b_prev == NULL)
1741 return scheme_false;
1742 else
1743 return buffer_new(buf->b_prev);
1744}
1745
1746/* (get-buff-num [buffer]) */
1747 static Scheme_Object *
1748get_buffer_num(void *data, int argc, Scheme_Object **argv)
1749{
1750 Vim_Prim *prim = (Vim_Prim *)data;
1751 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1752
1753 return scheme_make_integer(buf->buf->b_fnum);
1754}
1755
1756/* (buff-count) */
1757 static Scheme_Object *
1758get_buffer_count(void *data, int argc, Scheme_Object **argv)
1759{
1760 buf_T *b;
1761 int n = 0;
1762
1763 for (b = firstbuf; b; b = b->b_next) ++n;
1764 return scheme_make_integer(n);
1765}
1766
1767/* (get-buff-name [buffer]) */
1768 static Scheme_Object *
1769get_buffer_name(void *data, int argc, Scheme_Object **argv)
1770{
1771 Vim_Prim *prim = (Vim_Prim *)data;
1772 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1773
1774 return scheme_make_string(buf->buf->b_ffname);
1775}
1776
1777/* (curr-buff) */
1778 static Scheme_Object *
1779get_curr_buffer(void *data, int argc, Scheme_Object **argv)
1780{
1781 return (Scheme_Object *)get_vim_curr_buffer();
1782}
1783
1784 static Scheme_Object *
1785buffer_new(buf_T *buf)
1786{
1787 vim_mz_buffer *self;
1788
1789 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001790 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001791 * then we can get at it in buf_freeall() in vim.
1792 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001793 if (buf->b_mzscheme_ref)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001794 return buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001795
1796 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_buffer));
1797
1798 vim_memset(self, 0, sizeof(vim_mz_buffer));
1799 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001800 buf->b_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001801 self->buf = buf;
1802 self->tag = mz_buffer_type;
1803
1804 mzscheme_interface_init(self); /* Set up namespace */
1805
1806 return (Scheme_Object *)(self);
1807}
1808
1809/*
1810 * (get-buff-size [buffer])
1811 *
1812 * Get the size (number of lines) in the current buffer.
1813 */
1814 static Scheme_Object *
1815get_buffer_size(void *data, int argc, Scheme_Object **argv)
1816{
1817 Vim_Prim *prim = (Vim_Prim *)data;
1818 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1819
1820 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
1821}
1822
1823/*
1824 * (get-buff-line {linenr} [buffer])
1825 *
1826 * Get a line from the specified buffer. The line number is
1827 * in Vim format (1-based). The line is returned as a MzScheme
1828 * string object.
1829 */
1830 static Scheme_Object *
1831get_buffer_line(void *data, int argc, Scheme_Object **argv)
1832{
1833 Vim_Prim *prim = (Vim_Prim *)data;
1834 vim_mz_buffer *buf;
1835 int linenr;
1836 char *line;
1837
1838 buf = get_buffer_arg(prim->name, 1, argc, argv);
1839 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1840 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
1841
1842 raise_if_error();
1843 return scheme_make_string(line);
1844}
1845
1846
1847/*
1848 * (get-buff-line-list {start} {end} [buffer])
1849 *
1850 * Get a list of lines from the specified buffer. The line numbers
1851 * are in Vim format (1-based). The range is from lo up to, but not
1852 * including, hi. The list is returned as a list of string objects.
1853 */
1854 static Scheme_Object *
1855get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
1856{
1857 Vim_Prim *prim = (Vim_Prim *)data;
1858 vim_mz_buffer *buf;
1859 int i, hi, lo, n;
1860 Scheme_Object *list;
1861
1862 buf = get_buffer_arg(prim->name, 2, argc, argv);
1863 list = scheme_null;
1864 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
1865 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1866
1867 /*
1868 * Handle some error conditions
1869 */
1870 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001871 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001872
1873 if (hi < 0)
1874 hi = 0;
1875 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001876 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001877
1878 n = hi - lo;
1879
1880 for (i = n; i >= 0; --i)
1881 {
1882 Scheme_Object *str = scheme_make_string(
1883 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
1884 raise_if_error();
1885
1886 /* Set the list item */
1887 list = scheme_make_pair(str, list);
1888 }
1889
1890 return list;
1891}
1892
1893/*
1894 * (set-buff-line {linenr} {string/#f} [buffer])
1895 *
1896 * Replace a line in the specified buffer. The line number is
1897 * in Vim format (1-based). The replacement line is given as
1898 * an MzScheme string object. The object is checked for validity
1899 * and correct format. An exception is thrown if the values are not
1900 * the correct format.
1901 *
1902 * It returns a Scheme Object that indicates the length of the
1903 * string changed.
1904 */
1905 static Scheme_Object *
1906set_buffer_line(void *data, int argc, Scheme_Object **argv)
1907{
1908 /* First of all, we check the the of the supplied MzScheme object.
1909 * There are three cases:
1910 * 1. #f - this is a deletion.
1911 * 2. A string - this is a replacement.
1912 * 3. Anything else - this is an error.
1913 */
1914 Vim_Prim *prim = (Vim_Prim *)data;
1915 vim_mz_buffer *buf;
1916 Scheme_Object *line;
1917 char *save;
1918 buf_T *savebuf;
1919 int n;
1920
Bram Moolenaar555b2802005-05-19 21:08:39 +00001921#ifdef HAVE_SANDBOX
1922 sandbox_check();
1923#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001924 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1925 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001926 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001927 line = argv[1];
1928 buf = get_buffer_arg(prim->name, 2, argc, argv);
1929
1930 check_line_range(n, buf->buf);
1931
1932 if (SCHEME_FALSEP(line))
1933 {
1934 savebuf = curbuf;
1935 curbuf = buf->buf;
1936
1937 if (u_savedel((linenr_T)n, 1L) == FAIL)
1938 {
1939 curbuf = savebuf;
1940 raise_vim_exn(_("cannot save undo information"));
1941 }
1942 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
1943 {
1944 curbuf = savebuf;
1945 raise_vim_exn(_("cannot delete line"));
1946 }
1947 deleted_lines_mark((linenr_T)n, 1L);
1948 if (buf->buf == curwin->w_buffer)
1949 mz_fix_cursor(n, n + 1, -1);
1950
1951 curbuf = savebuf;
1952
1953 raise_if_error();
1954 return scheme_void;
1955 }
1956
1957 /* Otherwise it's a line */
1958 save = string_to_line(line);
1959 savebuf = curbuf;
1960
1961 curbuf = buf->buf;
1962
1963 if (u_savesub((linenr_T)n) == FAIL)
1964 {
1965 curbuf = savebuf;
1966 raise_vim_exn(_("cannot save undo information"));
1967 }
1968 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
1969 {
1970 curbuf = savebuf;
1971 raise_vim_exn(_("cannot replace line"));
1972 }
1973 else
1974 changed_bytes((linenr_T)n, 0);
1975
1976 curbuf = savebuf;
1977
1978 raise_if_error();
1979 return scheme_void;
1980}
1981
1982/*
1983 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
1984 *
1985 * Replace a range of lines in the specified buffer. The line numbers are in
1986 * Vim format (1-based). The range is from lo up to, but not including, hi.
1987 * The replacement lines are given as a Scheme list of string objects. The
1988 * list is checked for validity and correct format.
1989 *
1990 * Errors are returned as a value of FAIL. The return value is OK on success.
1991 * If OK is returned and len_change is not NULL, *len_change is set to the
1992 * change in the buffer length.
1993 */
1994 static Scheme_Object *
1995set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
1996{
1997 /* First of all, we check the type of the supplied MzScheme object.
1998 * There are three cases:
1999 * 1. #f - this is a deletion.
2000 * 2. A list - this is a replacement.
2001 * 3. Anything else - this is an error.
2002 */
2003 Vim_Prim *prim = (Vim_Prim *)data;
2004 vim_mz_buffer *buf;
2005 Scheme_Object *line_list;
2006 Scheme_Object *line;
2007 Scheme_Object *rest;
2008 char **array;
2009 buf_T *savebuf;
2010 int i, old_len, new_len, hi, lo;
2011 long extra;
2012
Bram Moolenaar555b2802005-05-19 21:08:39 +00002013#ifdef HAVE_SANDBOX
2014 sandbox_check();
2015#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002016 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2017 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2018 if (!SCHEME_PAIRP(argv[2])
2019 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2020 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2021 line_list = argv[2];
2022 buf = get_buffer_arg(prim->name, 3, argc, argv);
2023 old_len = hi - lo;
2024 if (old_len < 0) /* process inverse values wisely */
2025 {
2026 i = lo;
2027 lo = hi;
2028 hi = i;
2029 old_len = -old_len;
2030 }
2031 extra = 0;
2032
2033 check_line_range(lo, buf->buf); /* inclusive */
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002034 check_line_range(hi - 1, buf->buf); /* exclusive */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002035
2036 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2037 {
2038 savebuf = curbuf;
2039 curbuf = buf->buf;
2040
2041 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2042 {
2043 curbuf = savebuf;
2044 raise_vim_exn(_("cannot save undo information"));
2045 }
2046 else
2047 {
2048 for (i = 0; i < old_len; i++)
2049 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2050 {
2051 curbuf = savebuf;
2052 raise_vim_exn(_("cannot delete line"));
2053 }
2054 deleted_lines_mark((linenr_T)lo, (long)old_len);
2055 if (buf->buf == curwin->w_buffer)
2056 mz_fix_cursor(lo, hi, -old_len);
2057 }
2058
2059 curbuf = savebuf;
2060
2061 raise_if_error();
2062 return scheme_void;
2063 }
2064
2065 /* List */
2066 new_len = scheme_proper_list_length(line_list);
2067 if (new_len < 0) /* improper or cyclic list */
2068 scheme_wrong_type(prim->name, "proper list",
2069 2, argc, argv);
2070
2071 /* Using MzScheme allocator, so we don't need to free this and
2072 * can safely keep pointers to GC collected strings
2073 */
2074 array = (char **)scheme_malloc_fail_ok(scheme_malloc,
2075 (unsigned)(new_len * sizeof(char *)));
2076
2077 rest = line_list;
2078 for (i = 0; i < new_len; ++i)
2079 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002080 line = SCHEME_CAR(rest);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002081 rest = SCHEME_CDR(rest);
2082 if (!SCHEME_STRINGP(line))
2083 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002084 array[i] = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002085 }
2086
2087 savebuf = curbuf;
2088 curbuf = buf->buf;
2089
2090 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2091 {
2092 curbuf = savebuf;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002093 raise_vim_exn(_("cannot save undo information"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002094 }
2095
2096 /*
2097 * If the size of the range is reducing (ie, new_len < old_len) we
2098 * need to delete some old_len. We do this at the start, by
2099 * repeatedly deleting line "lo".
2100 */
2101 for (i = 0; i < old_len - new_len; ++i)
2102 {
2103 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2104 {
2105 curbuf = savebuf;
2106 raise_vim_exn(_("cannot delete line"));
2107 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002108 extra--;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002109 }
2110
2111 /*
2112 * For as long as possible, replace the existing old_len with the
2113 * new old_len. This is a more efficient operation, as it requires
2114 * less memory allocation and freeing.
2115 */
2116 for (i = 0; i < old_len && i < new_len; i++)
2117 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2118 {
2119 curbuf = savebuf;
2120 raise_vim_exn(_("cannot replace line"));
2121 }
2122
2123 /*
2124 * Now we may need to insert the remaining new_len. We don't need to
2125 * free the string passed back because MzScheme has control of that
2126 * memory.
2127 */
2128 while (i < new_len)
2129 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002130 if (ml_append((linenr_T)(lo + i - 1),
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002131 (char_u *)array[i], 0, FALSE) == FAIL)
2132 {
2133 curbuf = savebuf;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002134 raise_vim_exn(_("cannot insert line"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002135 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002136 ++i;
2137 ++extra;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002138 }
2139
2140 /*
2141 * Adjust marks. Invalidate any which lie in the
2142 * changed range, and move any in the remainder of the buffer.
2143 */
2144 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra);
2145 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2146
2147 if (buf->buf == curwin->w_buffer)
2148 mz_fix_cursor(lo, hi, extra);
2149 curbuf = savebuf;
2150
2151 raise_if_error();
2152 return scheme_void;
2153}
2154
2155/*
2156 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2157 *
2158 * Insert a number of lines into the specified buffer after the specifed line.
2159 * The line number is in Vim format (1-based). The lines to be inserted are
2160 * given as an MzScheme list of string objects or as a single string. The lines
2161 * to be added are checked for validity and correct format. Errors are
2162 * returned as a value of FAIL. The return value is OK on success.
2163 * If OK is returned and len_change is not NULL, *len_change
2164 * is set to the change in the buffer length.
2165 */
2166 static Scheme_Object *
2167insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2168{
2169 Vim_Prim *prim = (Vim_Prim *)data;
2170 vim_mz_buffer *buf;
2171 Scheme_Object *list;
2172 Scheme_Object *line;
2173 Scheme_Object *rest;
2174 char **array;
2175 char *str;
2176 buf_T *savebuf;
2177 int i, n, size;
2178
Bram Moolenaar555b2802005-05-19 21:08:39 +00002179#ifdef HAVE_SANDBOX
2180 sandbox_check();
2181#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002182 /*
2183 * First of all, we check the type of the supplied MzScheme object.
2184 * It must be a string or a list, or the call is in error.
2185 */
2186 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2187 list = argv[1];
2188
2189 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2190 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2191 buf = get_buffer_arg(prim->name, 2, argc, argv);
2192
2193 if (n != 0) /* 0 can be used in insert */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002194 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002195 if (SCHEME_STRINGP(list))
2196 {
2197 str = string_to_line(list);
2198
2199 savebuf = curbuf;
2200 curbuf = buf->buf;
2201
2202 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2203 {
2204 curbuf = savebuf;
2205 raise_vim_exn(_("cannot save undo information"));
2206 }
2207 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2208 {
2209 curbuf = savebuf;
2210 raise_vim_exn(_("cannot insert line"));
2211 }
2212 else
2213 appended_lines_mark((linenr_T)n, 1L);
2214
2215 curbuf = savebuf;
2216 update_screen(VALID);
2217
2218 raise_if_error();
2219 return scheme_void;
2220 }
2221
2222 /* List */
2223 size = scheme_proper_list_length(list);
2224 if (size < 0) /* improper or cyclic list */
2225 scheme_wrong_type(prim->name, "proper list",
2226 2, argc, argv);
2227
2228 /* Using MzScheme allocator, so we don't need to free this and
2229 * can safely keep pointers to GC collected strings
2230 */
2231 array = (char **)scheme_malloc_fail_ok(
2232 scheme_malloc, (unsigned)(size * sizeof(char *)));
2233
2234 rest = list;
2235 for (i = 0; i < size; ++i)
2236 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002237 line = SCHEME_CAR(rest);
2238 rest = SCHEME_CDR(rest);
2239 array[i] = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002240 }
2241
2242 savebuf = curbuf;
2243 curbuf = buf->buf;
2244
2245 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2246 {
2247 curbuf = savebuf;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002248 raise_vim_exn(_("cannot save undo information"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002249 }
2250 else
2251 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002252 for (i = 0; i < size; ++i)
2253 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002254 0, FALSE) == FAIL)
2255 {
2256 curbuf = savebuf;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002257 raise_vim_exn(_("cannot insert line"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002258 }
2259
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002260 if (i > 0)
2261 appended_lines_mark((linenr_T)n, (long)i);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002262 }
2263
2264 curbuf = savebuf;
2265 update_screen(VALID);
2266
2267 raise_if_error();
2268 return scheme_void;
2269}
2270
2271/* (get-buff-namespace [buffer]) */
2272 static Scheme_Object *
2273get_buffer_namespace(void *data, int argc, Scheme_Object **argv)
2274{
2275 Vim_Prim *prim = (Vim_Prim *)data;
2276
2277 return (Scheme_Object *)get_buffer_arg(prim->name, 0, argc, argv)->env;
2278}
2279
2280/*
2281 * Predicates
2282 */
2283/* (buff? obj) */
2284 static Scheme_Object *
2285vim_bufferp(void *data, int argc, Scheme_Object **argv)
2286{
2287 if (SCHEME_VIMBUFFERP(argv[0]))
2288 return scheme_true;
2289 else
2290 return scheme_false;
2291}
2292
2293/* (win? obj) */
2294 static Scheme_Object *
2295vim_windowp(void *data, int argc, Scheme_Object **argv)
2296{
2297 if (SCHEME_VIMWINDOWP(argv[0]))
2298 return scheme_true;
2299 else
2300 return scheme_false;
2301}
2302
2303/* (buff-valid? obj) */
2304 static Scheme_Object *
2305vim_buffer_validp(void *data, int argc, Scheme_Object **argv)
2306{
2307 if (SCHEME_VIMBUFFERP(argv[0])
2308 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2309 return scheme_true;
2310 else
2311 return scheme_false;
2312}
2313
2314/* (win-valid? obj) */
2315 static Scheme_Object *
2316vim_window_validp(void *data, int argc, Scheme_Object **argv)
2317{
2318 if (SCHEME_VIMWINDOWP(argv[0])
2319 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2320 return scheme_true;
2321 else
2322 return scheme_false;
2323}
2324
2325/*
2326 *===========================================================================
2327 * Utilities
2328 *===========================================================================
2329 */
2330
2331/*
2332 * Convert an MzScheme string into a Vim line.
2333 *
2334 * The result is in allocated memory. All internal nulls are replaced by
2335 * newline characters. It is an error for the string to contain newline
2336 * characters.
2337 *
2338 */
2339 static char *
2340string_to_line(Scheme_Object *obj)
2341{
2342 char *str;
2343 long len;
2344 int i;
2345
2346 str = scheme_display_to_string(obj, &len);
2347
2348 /* Error checking: String must not contain newlines, as we
2349 * are replacing a single line, and we must replace it with
2350 * a single line.
2351 */
2352 if (memchr(str, '\n', len))
2353 scheme_signal_error(_("string cannot contain newlines"));
2354
2355 /* Create a copy of the string, with internal nulls replaced by
2356 * newline characters, as is the vim convention.
2357 */
2358 for (i = 0; i < len; ++i)
2359 {
2360 if (str[i] == '\0')
2361 str[i] = '\n';
2362 }
2363
2364 str[i] = '\0';
2365
2366 return str;
2367}
2368
2369/*
2370 * Check to see whether a Vim error has been reported, or a keyboard
2371 * interrupt (from vim --> got_int) has been detected.
2372 */
2373 static int
2374vim_error_check(void)
2375{
2376 return (got_int || did_emsg);
2377}
2378
2379/*
2380 * register Scheme exn:vim
2381 */
2382 static void
2383register_vim_exn(Scheme_Env *env)
2384{
2385 Scheme_Object *exn_name = scheme_intern_symbol("exn:vim");
2386
2387 if (vim_exn == NULL)
2388 vim_exn = scheme_make_struct_type(exn_name,
2389 scheme_builtin_value("struct:exn"), NULL, 0, 0, NULL, NULL
2390#if MZSCHEME_VERSION_MAJOR >= 299
2391 , NULL
2392#endif
2393 );
2394
2395 if (vim_exn_values == NULL)
2396 {
2397 int nc = 0;
2398
2399 Scheme_Object **exn_names = scheme_make_struct_names(
2400 exn_name, scheme_null, 0, &nc);
2401 Scheme_Object **exn_values = scheme_make_struct_values(
2402 vim_exn, exn_names, nc, 0);
2403
2404 vim_exn_names = scheme_make_vector(nc, scheme_false);
2405 vim_exn_values = scheme_make_vector(nc, scheme_false);
2406 /* remember names and values */
2407 mch_memmove(SCHEME_VEC_ELS(vim_exn_names), exn_names,
2408 nc * sizeof(Scheme_Object *));
2409 mch_memmove(SCHEME_VEC_ELS(vim_exn_values), exn_values,
2410 nc * sizeof(Scheme_Object *));
2411 }
2412
2413 add_vim_exn(env);
2414}
2415
2416/*
2417 * Add stuff of exn:vim to env
2418 */
2419 static void
2420add_vim_exn(Scheme_Env *env)
2421{
2422 int i;
2423
2424 for (i = 0; i < SCHEME_VEC_SIZE(vim_exn_values); i++)
2425 scheme_add_global_symbol(SCHEME_VEC_ELS(vim_exn_names)[i],
2426 SCHEME_VEC_ELS(vim_exn_values)[i], env);
2427}
2428
2429/*
2430 * raise exn:vim, may be with additional info string
2431 */
2432 void
2433raise_vim_exn(const char *add_info)
2434{
2435 Scheme_Object *argv[2];
2436 char_u *fmt = _("Vim error: ~a");
2437
2438 if (add_info != NULL)
2439 {
2440 Scheme_Object *info = scheme_make_string(add_info);
Bram Moolenaar555b2802005-05-19 21:08:39 +00002441 argv[0] = scheme_byte_string_to_char_string(scheme_make_string(
2442 scheme_format(fmt, strlen(fmt), 1, &info, NULL)));
2443 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002444 }
2445 else
2446 argv[0] = scheme_make_string(_("Vim error"));
2447
Bram Moolenaar049377e2007-05-12 15:32:12 +00002448#if MZSCHEME_VERSION_MAJOR < 360
2449 argv[1] = scheme_current_continuation_marks();
2450#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00002451 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar049377e2007-05-12 15:32:12 +00002452#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002453
2454 scheme_raise(scheme_make_struct_instance(vim_exn, 2, argv));
2455}
2456
2457 void
2458raise_if_error(void)
2459{
2460 if (vim_error_check())
2461 raise_vim_exn(NULL);
2462}
2463
2464/* get buffer:
2465 * either current
2466 * or passed as argv[argnum] with checks
2467 */
2468 static vim_mz_buffer *
2469get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
2470{
2471 vim_mz_buffer *b;
2472
2473 if (argc < argnum + 1)
2474 return get_vim_curr_buffer();
2475 if (!SCHEME_VIMBUFFERP(argv[argnum]))
2476 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
2477 b = (vim_mz_buffer *)argv[argnum];
2478 (void)get_valid_buffer(argv[argnum]);
2479 return b;
2480}
2481
2482/* get window:
2483 * either current
2484 * or passed as argv[argnum] with checks
2485 */
2486 static vim_mz_window *
2487get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
2488{
2489 vim_mz_window *w;
2490
2491 if (argc < argnum + 1)
2492 return get_vim_curr_window();
2493 w = (vim_mz_window *)argv[argnum];
2494 if (!SCHEME_VIMWINDOWP(argv[argnum]))
2495 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
2496 (void)get_valid_window(argv[argnum]);
2497 return w;
2498}
2499
2500/* get valid Vim buffer from Scheme_Object* */
2501buf_T *get_valid_buffer(void *obj)
2502{
2503 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
2504
2505 if (buf == INVALID_BUFFER_VALUE)
2506 scheme_signal_error(_("buffer is invalid"));
2507 return buf;
2508}
2509
2510/* get valid Vim window from Scheme_Object* */
2511win_T *get_valid_window(void *obj)
2512{
2513 win_T *win = ((vim_mz_window *)obj)->win;
2514 if (win == INVALID_WINDOW_VALUE)
2515 scheme_signal_error(_("window is invalid"));
2516 return win;
2517}
2518
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002519 int
2520mzthreads_allowed(void)
2521{
2522 return mz_threads_allow;
2523}
2524
2525 static int
2526line_in_range(linenr_T lnum, buf_T *buf)
2527{
2528 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
2529}
2530
2531 static void
2532check_line_range(linenr_T lnum, buf_T *buf)
2533{
2534 if (!line_in_range(lnum, buf))
2535 scheme_signal_error(_("linenr out of range"));
2536}
2537
2538/*
2539 * Check if deleting lines made the cursor position invalid
2540 * (or you'll get msg from Vim about invalid linenr).
2541 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2542 * deleted). Got from if_python.c
2543 */
2544 static void
2545mz_fix_cursor(int lo, int hi, int extra)
2546{
2547 if (curwin->w_cursor.lnum >= lo)
2548 {
2549 /* Adjust the cursor position if it's in/after the changed
2550 * lines. */
2551 if (curwin->w_cursor.lnum >= hi)
2552 {
2553 curwin->w_cursor.lnum += extra;
2554 check_cursor_col();
2555 }
2556 else if (extra < 0)
2557 {
2558 curwin->w_cursor.lnum = lo;
2559 check_cursor();
2560 }
2561 changed_cline_bef_curs();
2562 }
2563 invalidate_botline();
2564}
2565
2566static Vim_Prim prims[]=
2567{
2568 /*
2569 * Buffer-related commands
2570 */
2571 {get_buffer_line, "get-buff-line", 1, 2},
2572 {set_buffer_line, "set-buff-line", 2, 3},
2573 {get_buffer_line_list, "get-buff-line-list", 2, 3},
2574 {get_buffer_name, "get-buff-name", 0, 1},
2575 {get_buffer_num, "get-buff-num", 0, 1},
2576 {get_buffer_size, "get-buff-size", 0, 1},
2577 {set_buffer_line_list, "set-buff-line-list", 3, 4},
2578 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
2579 {get_curr_buffer, "curr-buff", 0, 0},
2580 {get_buffer_count, "buff-count", 0, 0},
2581 {get_next_buffer, "get-next-buff", 0, 1},
2582 {get_prev_buffer, "get-prev-buff", 0, 1},
2583 {mzscheme_open_buffer, "open-buff", 1, 1},
2584 {get_buffer_by_name, "get-buff-by-name", 1, 1},
2585 {get_buffer_by_num, "get-buff-by-num", 1, 1},
2586 {get_buffer_namespace, "get-buff-namespace", 0, 1},
2587 /*
2588 * Window-related commands
2589 */
2590 {get_curr_win, "curr-win", 0, 0},
2591 {get_window_count, "win-count", 0, 0},
2592 {get_window_by_num, "get-win-by-num", 1, 1},
2593 {get_window_num, "get-win-num", 0, 1},
2594 {get_window_buffer, "get-win-buffer", 0, 1},
2595 {get_window_height, "get-win-height", 0, 1},
2596 {set_window_height, "set-win-height", 1, 2},
2597#ifdef FEAT_VERTSPLIT
2598 {get_window_width, "get-win-width", 0, 1},
2599 {set_window_width, "set-win-width", 1, 2},
2600#endif
2601 {get_cursor, "get-cursor", 0, 1},
2602 {set_cursor, "set-cursor", 1, 2},
2603 {get_window_list, "get-win-list", 0, 1},
2604 /*
2605 * Vim-related commands
2606 */
2607 {vim_command, "command", 1, 1},
2608 {vim_eval, "eval", 1, 1},
2609 {get_range_start, "range-start", 0, 0},
2610 {get_range_end, "range-end", 0, 0},
2611 {mzscheme_beep, "beep", 0, 0},
2612 {get_option, "get-option", 1, 2},
2613 {set_option, "set-option", 1, 2},
2614 /*
2615 * small utilities
2616 */
2617 {vim_bufferp, "buff?", 1, 1},
2618 {vim_windowp, "win?", 1, 1},
2619 {vim_buffer_validp, "buff-valid?", 1, 1},
2620 {vim_window_validp, "win-valid?", 1, 1}
2621};
2622
2623/* return MzScheme wrapper for curbuf */
2624 static vim_mz_buffer *
2625get_vim_curr_buffer(void)
2626{
Bram Moolenaare344bea2005-09-01 20:46:49 +00002627 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002628 return (vim_mz_buffer *)buffer_new(curbuf);
2629 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00002630 return (vim_mz_buffer *)curbuf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002631}
2632
2633/* return MzScheme wrapper for curwin */
2634 static vim_mz_window *
2635get_vim_curr_window(void)
2636{
Bram Moolenaare344bea2005-09-01 20:46:49 +00002637 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002638 return (vim_mz_window *)window_new(curwin);
2639 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00002640 return (vim_mz_window *)curwin->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002641}
2642
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002643 static void
2644make_modules(Scheme_Env *env)
2645{
2646 int i;
2647 Scheme_Env *mod;
2648
2649 mod = scheme_primitive_module(scheme_intern_symbol("vimext"), env);
2650 /* all prims made closed so they can access their own names */
2651 for (i = 0; i < sizeof(prims)/sizeof(prims[0]); i++)
2652 {
2653 Vim_Prim *prim = prims + i;
2654 scheme_add_global(prim->name,
2655 scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
2656 prim->mina, prim->maxa),
2657 mod);
2658 }
2659 scheme_add_global("global-namespace", (Scheme_Object *)environment, mod);
2660 scheme_finish_primitive_module(mod);
2661}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002662
Bram Moolenaar555b2802005-05-19 21:08:39 +00002663#ifdef HAVE_SANDBOX
2664static Scheme_Object *M_write = NULL;
2665static Scheme_Object *M_read = NULL;
2666static Scheme_Object *M_execute = NULL;
2667static Scheme_Object *M_delete = NULL;
2668
2669 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00002670sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002671{
2672 if (sandbox)
2673 raise_vim_exn(_("not allowed in the Vim sandbox"));
2674}
2675
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002676/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00002677 static Scheme_Object *
2678sandbox_file_guard(int argc, Scheme_Object **argv)
2679{
2680 if (sandbox)
2681 {
2682 Scheme_Object *requested_access = argv[2];
2683
2684 if (M_write == NULL)
2685 {
2686 MZ_REGISTER_STATIC(M_write);
2687 M_write = scheme_intern_symbol("write");
2688 }
2689 if (M_read == NULL)
2690 {
2691 MZ_REGISTER_STATIC(M_read);
2692 M_read = scheme_intern_symbol("read");
2693 }
2694 if (M_execute == NULL)
2695 {
2696 MZ_REGISTER_STATIC(M_execute);
2697 M_execute = scheme_intern_symbol("execute");
2698 }
2699 if (M_delete == NULL)
2700 {
2701 MZ_REGISTER_STATIC(M_delete);
2702 M_delete = scheme_intern_symbol("delete");
2703 }
2704
2705 while (!SCHEME_NULLP(requested_access))
2706 {
2707 Scheme_Object *item = SCHEME_CAR(requested_access);
2708 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
2709 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
2710 {
2711 raise_vim_exn(_("not allowed in the Vim sandbox"));
2712 }
2713 requested_access = SCHEME_CDR(requested_access);
2714 }
2715 }
2716 return scheme_void;
2717}
2718
2719 static Scheme_Object *
2720sandbox_network_guard(int argc, Scheme_Object **argv)
2721{
2722 return scheme_void;
2723}
2724#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00002725
2726#endif