blob: 7f43cab6ed04891e5a17e1a1c02f7fb0f2f8a782 [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);
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000311static Scheme_Object *(*dll_scheme_char_string_to_path)
312 (Scheme_Object *s);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000313# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000314
315/* arrays are imported directly */
316# define scheme_eof dll_scheme_eof
317# define scheme_false dll_scheme_false
318# define scheme_void dll_scheme_void
319# define scheme_null dll_scheme_null
320# define scheme_true dll_scheme_true
321
322/* pointers are GetProceAddress'ed as pointers to pointer */
323# define scheme_current_thread (*dll_scheme_current_thread_ptr)
324# define scheme_console_printf (*dll_scheme_console_printf_ptr)
325# define scheme_console_output (*dll_scheme_console_output_ptr)
326# define scheme_notify_multithread (*dll_scheme_notify_multithread_ptr)
327
328/* and functions in a usual way */
329# define GC_malloc dll_GC_malloc
330# define GC_malloc_atomic dll_GC_malloc_atomic
331
332# define scheme_add_global dll_scheme_add_global
333# define scheme_add_global_symbol dll_scheme_add_global_symbol
334# define scheme_apply dll_scheme_apply
335# define scheme_basic_env dll_scheme_basic_env
336# define scheme_builtin_value dll_scheme_builtin_value
Bram Moolenaar555b2802005-05-19 21:08:39 +0000337# if MZSCHEME_VERSION_MAJOR >= 299
338# define scheme_byte_string_to_char_string dll_scheme_byte_string_to_char_string
339# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000340# define scheme_check_threads dll_scheme_check_threads
341# define scheme_close_input_port dll_scheme_close_input_port
342# define scheme_count_lines dll_scheme_count_lines
343# define scheme_current_continuation_marks \
344 dll_scheme_current_continuation_marks
345# define scheme_display dll_scheme_display
346# define scheme_display_to_string dll_scheme_display_to_string
347# define scheme_do_eval dll_scheme_do_eval
348# define scheme_dont_gc_ptr dll_scheme_dont_gc_ptr
Bram Moolenaar555b2802005-05-19 21:08:39 +0000349# define scheme_eq dll_scheme_eq
Bram Moolenaar33570922005-01-25 22:26:29 +0000350# define scheme_eval dll_scheme_eval
351# define scheme_eval_string dll_scheme_eval_string
352# define scheme_eval_string_all dll_scheme_eval_string_all
353# define scheme_finish_primitive_module dll_scheme_finish_primitive_module
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000354# if MZSCHEME_VERSION_MAJOR < 299
355# define scheme_format dll_scheme_format
356# else
357# define scheme_format_utf8 dll_scheme_format_utf8
358# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000359# define scheme_gc_ptr_ok dll_scheme_gc_ptr_ok
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000360# if MZSCHEME_VERSION_MAJOR < 299
361# define scheme_get_sized_string_output dll_scheme_get_sized_string_output
362# else
363# define scheme_get_sized_byte_string_output \
364 dll_scheme_get_sized_byte_string_output
Bram Moolenaar555b2802005-05-19 21:08:39 +0000365# define scheme_get_param dll_scheme_get_param
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000366# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000367# define scheme_intern_symbol dll_scheme_intern_symbol
368# define scheme_lookup_global dll_scheme_lookup_global
369# define scheme_make_closed_prim_w_arity dll_scheme_make_closed_prim_w_arity
370# define scheme_make_integer_value dll_scheme_make_integer_value
371# define scheme_make_namespace dll_scheme_make_namespace
372# define scheme_make_pair dll_scheme_make_pair
Bram Moolenaar555b2802005-05-19 21:08:39 +0000373# define scheme_make_prim_w_arity dll_scheme_make_prim_w_arity
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000374# if MZSCHEME_VERSION_MAJOR < 299
375# define scheme_make_string dll_scheme_make_string
376# define scheme_make_string_output_port dll_scheme_make_string_output_port
377# else
378# define scheme_make_byte_string dll_scheme_make_byte_string
379# define scheme_make_byte_string_output_port \
380 dll_scheme_make_byte_string_output_port
381# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000382# define scheme_make_struct_instance dll_scheme_make_struct_instance
383# define scheme_make_struct_names dll_scheme_make_struct_names
384# define scheme_make_struct_type dll_scheme_make_struct_type
385# define scheme_make_struct_values dll_scheme_make_struct_values
386# define scheme_make_type dll_scheme_make_type
387# define scheme_make_vector dll_scheme_make_vector
388# define scheme_malloc_fail_ok dll_scheme_malloc_fail_ok
389# define scheme_open_input_file dll_scheme_open_input_file
390# define scheme_primitive_module dll_scheme_primitive_module
391# define scheme_proper_list_length dll_scheme_proper_list_length
392# define scheme_raise dll_scheme_raise
393# define scheme_read dll_scheme_read
394# define scheme_register_static dll_scheme_register_static
395# define scheme_set_stack_base dll_scheme_set_stack_base
396# define scheme_signal_error dll_scheme_signal_error
397# define scheme_wrong_type dll_scheme_wrong_type
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000398# if MZSCHEME_VERSION_MAJOR >= 299
399# define scheme_set_param dll_scheme_set_param
400# define scheme_current_config dll_scheme_current_config
401# define scheme_char_string_to_byte_string \
402 dll_scheme_char_string_to_byte_string
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000403# define scheme_char_string_to_path \
404 dll_scheme_char_string_to_path
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000405# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000406
407typedef struct
408{
409 char *name;
410 void **ptr;
411} Thunk_Info;
412
413static Thunk_Info mzgc_imports[] = {
414 {"GC_malloc", (void **)&dll_GC_malloc},
415 {"GC_malloc_atomic", (void **)&dll_GC_malloc_atomic},
416 {NULL, NULL}};
417
418static Thunk_Info mzsch_imports[] = {
419 {"scheme_eof", (void **)&dll_scheme_eof},
420 {"scheme_false", (void **)&dll_scheme_false},
421 {"scheme_void", (void **)&dll_scheme_void},
422 {"scheme_null", (void **)&dll_scheme_null},
423 {"scheme_true", (void **)&dll_scheme_true},
424 {"scheme_current_thread", (void **)&dll_scheme_current_thread_ptr},
425 {"scheme_console_printf", (void **)&dll_scheme_console_printf_ptr},
426 {"scheme_console_output", (void **)&dll_scheme_console_output_ptr},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000427 {"scheme_notify_multithread",
Bram Moolenaar33570922005-01-25 22:26:29 +0000428 (void **)&dll_scheme_notify_multithread_ptr},
429 {"scheme_add_global", (void **)&dll_scheme_add_global},
430 {"scheme_add_global_symbol", (void **)&dll_scheme_add_global_symbol},
431 {"scheme_apply", (void **)&dll_scheme_apply},
432 {"scheme_basic_env", (void **)&dll_scheme_basic_env},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000433# if MZSCHEME_VERSION_MAJOR >= 299
434 {"scheme_byte_string_to_char_string", (void **)&dll_scheme_byte_string_to_char_string},
435# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000436 {"scheme_builtin_value", (void **)&dll_scheme_builtin_value},
437 {"scheme_check_threads", (void **)&dll_scheme_check_threads},
438 {"scheme_close_input_port", (void **)&dll_scheme_close_input_port},
439 {"scheme_count_lines", (void **)&dll_scheme_count_lines},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000440 {"scheme_current_continuation_marks",
Bram Moolenaar33570922005-01-25 22:26:29 +0000441 (void **)&dll_scheme_current_continuation_marks},
442 {"scheme_display", (void **)&dll_scheme_display},
443 {"scheme_display_to_string", (void **)&dll_scheme_display_to_string},
444 {"scheme_do_eval", (void **)&dll_scheme_do_eval},
445 {"scheme_dont_gc_ptr", (void **)&dll_scheme_dont_gc_ptr},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000446 {"scheme_eq", (void **)&dll_scheme_eq},
Bram Moolenaar33570922005-01-25 22:26:29 +0000447 {"scheme_eval", (void **)&dll_scheme_eval},
448 {"scheme_eval_string", (void **)&dll_scheme_eval_string},
449 {"scheme_eval_string_all", (void **)&dll_scheme_eval_string_all},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000450 {"scheme_finish_primitive_module",
Bram Moolenaar33570922005-01-25 22:26:29 +0000451 (void **)&dll_scheme_finish_primitive_module},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000452# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000453 {"scheme_format", (void **)&dll_scheme_format},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000454# else
455 {"scheme_format_utf8", (void **)&dll_scheme_format_utf8},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000456 {"scheme_get_param", (void **)&dll_scheme_get_param},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000457#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000458 {"scheme_gc_ptr_ok", (void **)&dll_scheme_gc_ptr_ok},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000459# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000460 {"scheme_get_sized_string_output",
Bram Moolenaar33570922005-01-25 22:26:29 +0000461 (void **)&dll_scheme_get_sized_string_output},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000462# else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000463 {"scheme_get_sized_byte_string_output",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000464 (void **)&dll_scheme_get_sized_byte_string_output},
465#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000466 {"scheme_intern_symbol", (void **)&dll_scheme_intern_symbol},
467 {"scheme_lookup_global", (void **)&dll_scheme_lookup_global},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000468 {"scheme_make_closed_prim_w_arity",
Bram Moolenaar33570922005-01-25 22:26:29 +0000469 (void **)&dll_scheme_make_closed_prim_w_arity},
470 {"scheme_make_integer_value", (void **)&dll_scheme_make_integer_value},
471 {"scheme_make_namespace", (void **)&dll_scheme_make_namespace},
472 {"scheme_make_pair", (void **)&dll_scheme_make_pair},
Bram Moolenaar555b2802005-05-19 21:08:39 +0000473 {"scheme_make_prim_w_arity", (void **)&dll_scheme_make_prim_w_arity},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000474# if MZSCHEME_VERSION_MAJOR < 299
Bram Moolenaar33570922005-01-25 22:26:29 +0000475 {"scheme_make_string", (void **)&dll_scheme_make_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000476 {"scheme_make_string_output_port",
Bram Moolenaar33570922005-01-25 22:26:29 +0000477 (void **)&dll_scheme_make_string_output_port},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000478# else
479 {"scheme_make_byte_string", (void **)&dll_scheme_make_byte_string},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000480 {"scheme_make_byte_string_output_port",
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000481 (void **)&dll_scheme_make_byte_string_output_port},
482# endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000483 {"scheme_make_struct_instance",
Bram Moolenaar33570922005-01-25 22:26:29 +0000484 (void **)&dll_scheme_make_struct_instance},
485 {"scheme_make_struct_names", (void **)&dll_scheme_make_struct_names},
486 {"scheme_make_struct_type", (void **)&dll_scheme_make_struct_type},
487 {"scheme_make_struct_values", (void **)&dll_scheme_make_struct_values},
488 {"scheme_make_type", (void **)&dll_scheme_make_type},
489 {"scheme_make_vector", (void **)&dll_scheme_make_vector},
490 {"scheme_malloc_fail_ok", (void **)&dll_scheme_malloc_fail_ok},
491 {"scheme_open_input_file", (void **)&dll_scheme_open_input_file},
492 {"scheme_primitive_module", (void **)&dll_scheme_primitive_module},
493 {"scheme_proper_list_length", (void **)&dll_scheme_proper_list_length},
494 {"scheme_raise", (void **)&dll_scheme_raise},
495 {"scheme_read", (void **)&dll_scheme_read},
496 {"scheme_register_static", (void **)&dll_scheme_register_static},
497 {"scheme_set_stack_base", (void **)&dll_scheme_set_stack_base},
498 {"scheme_signal_error", (void **)&dll_scheme_signal_error},
499 {"scheme_wrong_type", (void **)&dll_scheme_wrong_type},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000500# if MZSCHEME_VERSION_MAJOR >= 299
501 {"scheme_set_param", (void **)&dll_scheme_set_param},
502 {"scheme_current_config", (void **)&dll_scheme_current_config},
503 {"scheme_char_string_to_byte_string",
504 (void **)&dll_scheme_char_string_to_byte_string},
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000505 {"scheme_char_string_to_path",
506 (void **)&dll_scheme_char_string_to_path},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000507# endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000508 {NULL, NULL}};
509
510static HINSTANCE hMzGC = 0;
511static HINSTANCE hMzSch = 0;
512
513static void dynamic_mzscheme_end(void);
514static int mzscheme_runtime_link_init(char *sch_dll, char *gc_dll,
515 int verbose);
516
517 static int
518mzscheme_runtime_link_init(char *sch_dll, char *gc_dll, int verbose)
519{
520 Thunk_Info *thunk = NULL;
521
522 if (hMzGC && hMzSch)
523 return OK;
524 hMzSch = LoadLibrary(sch_dll);
525 hMzGC = LoadLibrary(gc_dll);
526
527 if (!hMzSch)
528 {
529 if (verbose)
530 EMSG2(_(e_loadlib), sch_dll);
531 return FAIL;
532 }
533
534 if (!hMzGC)
535 {
536 if (verbose)
537 EMSG2(_(e_loadlib), gc_dll);
538 return FAIL;
539 }
540
541 for (thunk = mzsch_imports; thunk->name; thunk++)
542 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000543 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000544 (void *)GetProcAddress(hMzSch, thunk->name)) == NULL)
545 {
546 FreeLibrary(hMzSch);
547 hMzSch = 0;
548 FreeLibrary(hMzGC);
549 hMzGC = 0;
550 if (verbose)
551 EMSG2(_(e_loadfunc), thunk->name);
552 return FAIL;
553 }
554 }
555 for (thunk = mzgc_imports; thunk->name; thunk++)
556 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000557 if ((*thunk->ptr =
Bram Moolenaar33570922005-01-25 22:26:29 +0000558 (void *)GetProcAddress(hMzGC, thunk->name)) == NULL)
559 {
560 FreeLibrary(hMzSch);
561 hMzSch = 0;
562 FreeLibrary(hMzGC);
563 hMzGC = 0;
564 if (verbose)
565 EMSG2(_(e_loadfunc), thunk->name);
566 return FAIL;
567 }
568 }
569 return OK;
570}
571
572 int
573mzscheme_enabled(int verbose)
574{
575 return mzscheme_runtime_link_init(
576 DYNAMIC_MZSCH_DLL, DYNAMIC_MZGC_DLL, verbose) == OK;
577}
578
579 static void
580dynamic_mzscheme_end(void)
581{
582 if (hMzSch)
583 {
584 FreeLibrary(hMzSch);
585 hMzSch = 0;
586 }
587 if (hMzGC)
588 {
589 FreeLibrary(hMzGC);
590 hMzGC = 0;
591 }
592}
593#endif /* DYNAMIC_MZSCHEME */
594
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000595/*
596 *========================================================================
597 * 1. MzScheme interpreter startup
598 *========================================================================
599 */
600
601static Scheme_Type mz_buffer_type;
602static Scheme_Type mz_window_type;
603
604static int initialized = 0;
605
606/* global environment */
607static Scheme_Env *environment = NULL;
608/* output/error handlers */
609static Scheme_Object *curout = NULL;
610static Scheme_Object *curerr = NULL;
611/* vim:exn exception */
612static Scheme_Object *exn_catching_apply = NULL;
613static Scheme_Object *exn_p = NULL;
614static Scheme_Object *exn_message = NULL;
615static Scheme_Object *vim_exn = NULL; /* Vim Error exception */
616 /* values for exn:vim - constructor, predicate, accessors etc */
617static Scheme_Object *vim_exn_names = NULL;
618static Scheme_Object *vim_exn_values = NULL;
619
620static long range_start;
621static long range_end;
622
623/* MzScheme threads scheduling stuff */
624static int mz_threads_allow = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000625
626#if defined(FEAT_GUI_W32)
627static void CALLBACK timer_proc(HWND, UINT, UINT, DWORD);
628static UINT timer_id = 0;
629#elif defined(FEAT_GUI_GTK)
630static gint timer_proc(gpointer);
631static guint timer_id = 0;
632#elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
633static void timer_proc(XtPointer, XtIntervalId *);
634static XtIntervalId timer_id = (XtIntervalId)0;
635#elif defined(FEAT_GUI_MAC)
636pascal void timer_proc(EventLoopTimerRef, void *);
637static EventLoopTimerRef timer_id = NULL;
638static EventLoopTimerUPP timerUPP;
639#endif
640
641#ifndef FEAT_GUI_W32 /* Win32 console and Unix */
642 void
643mzvim_check_threads(void)
644{
645 /* Last time MzScheme threads were scheduled */
646 static time_t mz_last_time = 0;
647
648 if (mz_threads_allow && p_mzq > 0)
649 {
650 time_t now = time(NULL);
651
652 if ((now - mz_last_time) * 1000 > p_mzq)
653 {
654 mz_last_time = now;
655 scheme_check_threads();
656 }
657 }
658}
659#endif
660
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000661#ifdef MZSCHEME_GUI_THREADS
662static void setup_timer(void);
663static void remove_timer(void);
664
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000665/* timers are presented in GUI only */
666# if defined(FEAT_GUI_W32)
667 static void CALLBACK
668timer_proc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
669# elif defined(FEAT_GUI_GTK)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000670 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000671timer_proc(gpointer data UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000672# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000673 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000674timer_proc(XtPointer timed_out UNUSED, XtIntervalId *interval_id UNUSED)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000675# elif defined(FEAT_GUI_MAC)
676 pascal void
677timer_proc(EventLoopTimerRef theTimer, void *userData)
678# endif
679{
680 scheme_check_threads();
681# if defined(FEAT_GUI_GTK)
682 return TRUE; /* continue receiving notifications */
683# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
684 /* renew timeout */
685 if (mz_threads_allow && p_mzq > 0)
686 timer_id = XtAppAddTimeOut(app_context, p_mzq,
687 timer_proc, NULL);
688# endif
689}
690
691 static void
692setup_timer(void)
693{
694# if defined(FEAT_GUI_W32)
695 timer_id = SetTimer(NULL, 0, p_mzq, timer_proc);
696# elif defined(FEAT_GUI_GTK)
697 timer_id = gtk_timeout_add((guint32)p_mzq, (GtkFunction)timer_proc, NULL);
698# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
699 timer_id = XtAppAddTimeOut(app_context, p_mzq, timer_proc, NULL);
700# elif defined(FEAT_GUI_MAC)
701 timerUPP = NewEventLoopTimerUPP(timer_proc);
702 InstallEventLoopTimer(GetMainEventLoop(), p_mzq * kEventDurationMillisecond,
703 p_mzq * kEventDurationMillisecond, timerUPP, NULL, &timer_id);
704# endif
705}
706
707 static void
708remove_timer(void)
709{
710# if defined(FEAT_GUI_W32)
711 KillTimer(NULL, timer_id);
712# elif defined(FEAT_GUI_GTK)
713 gtk_timeout_remove(timer_id);
714# elif defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
715 XtRemoveTimeOut(timer_id);
716# elif defined(FEAT_GUI_MAC)
717 RemoveEventLoopTimer(timer_id);
718 DisposeEventLoopTimerUPP(timerUPP);
719# endif
720 timer_id = 0;
721}
722
723 void
724mzvim_reset_timer(void)
725{
726 if (timer_id != 0)
727 remove_timer();
728 if (mz_threads_allow && p_mzq > 0 && gui.in_use)
729 setup_timer();
730}
731
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000732#endif /* MZSCHEME_GUI_THREADS */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000733
734 static void
735notify_multithread(int on)
736{
737 mz_threads_allow = on;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000738#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000739 if (on && timer_id == 0 && p_mzq > 0 && gui.in_use)
740 setup_timer();
741 if (!on && timer_id != 0)
742 remove_timer();
743#endif
744}
745
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000746 void
747mzscheme_end(void)
748{
Bram Moolenaar33570922005-01-25 22:26:29 +0000749#ifdef DYNAMIC_MZSCHEME
750 dynamic_mzscheme_end();
751#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000752}
753
754 static void
755startup_mzscheme(void)
756{
Bram Moolenaar555b2802005-05-19 21:08:39 +0000757 Scheme_Object *proc_make_security_guard;
758
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000759 scheme_set_stack_base(NULL, 1);
760
761 MZ_REGISTER_STATIC(environment);
762 MZ_REGISTER_STATIC(curout);
763 MZ_REGISTER_STATIC(curerr);
764 MZ_REGISTER_STATIC(exn_catching_apply);
765 MZ_REGISTER_STATIC(exn_p);
766 MZ_REGISTER_STATIC(exn_message);
767 MZ_REGISTER_STATIC(vim_exn);
768 MZ_REGISTER_STATIC(vim_exn_names);
769 MZ_REGISTER_STATIC(vim_exn_values);
770
771 environment = scheme_basic_env();
772
773 /* redirect output */
774 scheme_console_output = do_output;
775 scheme_console_printf = do_printf;
776
777#ifdef MZSCHEME_COLLECTS
778 /* setup 'current-library-collection-paths' parameter */
779 scheme_set_param(scheme_config, MZCONFIG_COLLECTION_PATHS,
Bram Moolenaare2a49d82007-07-06 17:43:08 +0000780 scheme_make_pair(
781# if MZSCHEME_VERSION_MAJOR >= 299
782 scheme_char_string_to_path(
783 scheme_byte_string_to_char_string(
784 scheme_make_byte_string(MZSCHEME_COLLECTS))),
785# else
786 scheme_make_string(MZSCHEME_COLLECTS),
787# endif
788 scheme_null));
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000789#endif
Bram Moolenaar555b2802005-05-19 21:08:39 +0000790#ifdef HAVE_SANDBOX
791 /* setup sandbox guards */
792 proc_make_security_guard = scheme_lookup_global(
793 scheme_intern_symbol("make-security-guard"),
794 environment);
795 if (proc_make_security_guard != NULL)
796 {
797 Scheme_Object *args[3];
798 Scheme_Object *guard;
799 args[0] = scheme_get_param(scheme_config, MZCONFIG_SECURITY_GUARD);
800 args[1] = scheme_make_prim_w_arity(sandbox_file_guard,
801 "sandbox-file-guard", 3, 3);
802 args[2] = scheme_make_prim_w_arity(sandbox_network_guard,
803 "sandbox-network-guard", 4, 4);
804 guard = scheme_apply(proc_make_security_guard, 3, args);
805 scheme_set_param(scheme_config, MZCONFIG_SECURITY_GUARD, guard);
806 }
807#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000808 /* Create buffer and window types for use in Scheme code */
809 mz_buffer_type = scheme_make_type("<vim-buffer>");
810 mz_window_type = scheme_make_type("<vim-window>");
811
812 register_vim_exn(environment);
813 make_modules(environment);
814
815 /*
816 * setup callback to receive notifications
817 * whether thread scheduling is (or not) required
818 */
819 scheme_notify_multithread = notify_multithread;
820 initialized = 1;
821}
822
823/*
824 * This routine is called for each new invocation of MzScheme
825 * to make sure things are properly initialized.
826 */
827 static int
828mzscheme_init(void)
829{
830 int do_require = FALSE;
831
832 if (!initialized)
833 {
834 do_require = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000835#ifdef DYNAMIC_MZSCHEME
836 if (!mzscheme_enabled(TRUE))
837 {
838 EMSG(_("???: Sorry, this command is disabled, the MzScheme library could not be loaded."));
839 return -1;
840 }
841#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000842 startup_mzscheme();
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000843
844 if (mzscheme_io_init())
845 return -1;
846
847 }
848 /* recreate ports each call effectivelly clearing these ones */
849 curout = scheme_make_string_output_port();
850 curerr = scheme_make_string_output_port();
851 scheme_set_param(scheme_config, MZCONFIG_OUTPUT_PORT, curout);
852 scheme_set_param(scheme_config, MZCONFIG_ERROR_PORT, curerr);
853
854 if (do_require)
855 {
856 /* auto-instantiate in basic env */
857 eval_in_namespace("(require (prefix vimext: vimext))", do_eval,
858 environment, NULL);
859 }
860
861 return 0;
862}
863
864/*
865 * This routine fills the namespace with various important routines that can
866 * be used within MzScheme.
867 */
868 static void
869mzscheme_interface_init(vim_mz_buffer *mzbuff)
870{
871 Scheme_Object *attach;
872
873 mzbuff->env = (Scheme_Env *)scheme_make_namespace(0, NULL);
874
875 /*
876 * attach instantiated modules from global namespace
877 * so they can be easily instantiated in the buffer namespace
878 */
879 attach = scheme_lookup_global(
880 scheme_intern_symbol("namespace-attach-module"),
881 environment);
882
883 if (attach != NULL)
884 {
885 Scheme_Object *ret;
886 Scheme_Object *args[2];
887
888 args[0] = (Scheme_Object *)environment;
889 args[1] = scheme_intern_symbol("vimext");
890
891 ret = (Scheme_Object *)mzvim_apply(attach, 2, args);
892 }
893
894 add_vim_exn(mzbuff->env);
895}
896
897/*
898 *========================================================================
899 * 2. External Interface
900 *========================================================================
901 */
902
903/*
904 * Evaluate command in namespace with exception handling
905 */
906 static int
907eval_in_namespace(void *data, Scheme_Closed_Prim *what, Scheme_Env *env,
908 Scheme_Object **ret)
909{
910 Scheme_Object *value;
911 Scheme_Object *exn;
912 Cmd_Info info; /* closure info */
913
914 info.data = data;
915 info.env = env;
916
917 scheme_set_param(scheme_config, MZCONFIG_ENV,
918 (Scheme_Object *) env);
919 /*
920 * ensure all evaluations will be in current buffer namespace,
921 * the second argument to scheme_eval_string isn't enough!
922 */
923 value = _apply_thunk_catch_exceptions(
924 scheme_make_closed_prim_w_arity(what, &info, "mzvim", 0, 0),
925 &exn);
926
927 if (!value)
928 {
929 value = extract_exn_message(exn);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000930 /* Got an exn? */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000931 if (value)
932 {
933 scheme_display(value, curerr); /* Send to stderr-vim */
934 do_flush();
935 }
936 /* `raise' was called on some arbitrary value */
937 return FAIL;
938 }
939
940 if (ret != NULL) /* if pointer to retval supported give it up */
941 *ret = value;
942 /* Print any result, as long as it's not a void */
943 else if (!SCHEME_VOIDP(value))
944 scheme_display(value, curout); /* Send to stdout-vim */
945
946 do_flush();
947 return OK;
948}
949
950/* :mzscheme */
951 static int
952do_mzscheme_command(exarg_T *eap, void *data, Scheme_Closed_Prim *what)
953{
954 if (mzscheme_init())
955 return FAIL;
956
957 range_start = eap->line1;
958 range_end = eap->line2;
959
960 return eval_in_namespace(data, what, get_vim_curr_buffer()->env, NULL);
961}
962
963/*
964 * Routine called by VIM when deleting a buffer
965 */
966 void
967mzscheme_buffer_free(buf_T *buf)
968{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000969 if (buf->b_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000970 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000971 vim_mz_buffer *bp;
972
Bram Moolenaare344bea2005-09-01 20:46:49 +0000973 bp = buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000974 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000975 buf->b_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000976 scheme_gc_ptr_ok(bp);
977 }
978}
979
980/*
981 * Routine called by VIM when deleting a Window
982 */
983 void
984mzscheme_window_free(win_T *win)
985{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000986 if (win->w_mzscheme_ref)
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000987 {
988 vim_mz_window *wp;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000989 wp = win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000990 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000991 win->w_mzscheme_ref = NULL;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000992 scheme_gc_ptr_ok(wp);
993 }
994}
995
996/*
997 * ":mzscheme" (or ":mz")
998 */
999 void
1000ex_mzscheme(exarg_T *eap)
1001{
1002 char_u *script;
1003
1004 script = script_get(eap, eap->arg);
1005 if (!eap->skip)
1006 {
1007 if (script == NULL)
1008 do_mzscheme_command(eap, eap->arg, do_eval);
1009 else
1010 {
1011 do_mzscheme_command(eap, script, do_eval);
1012 vim_free(script);
1013 }
1014 }
1015}
1016
1017/* eval MzScheme string */
1018 void *
1019mzvim_eval_string(char_u *str)
1020{
1021 Scheme_Object *ret = NULL;
1022 if (mzscheme_init())
1023 return FAIL;
1024
1025 eval_in_namespace(str, do_eval, get_vim_curr_buffer()->env, &ret);
1026 return ret;
1027}
1028
1029/*
1030 * apply MzScheme procedure with arguments,
1031 * handling errors
1032 */
1033 Scheme_Object *
1034mzvim_apply(Scheme_Object *proc, int argc, Scheme_Object **argv)
1035{
1036 Apply_Info data;
1037 Scheme_Object *ret = NULL;
1038
1039 if (mzscheme_init())
1040 return FAIL;
1041
1042 data.proc = proc;
1043 data.argc = argc;
1044 data.argv = argv;
1045
1046 eval_in_namespace(&data, do_apply, get_vim_curr_buffer()->env, &ret);
1047 return ret;
1048}
1049
1050 static Scheme_Object *
1051do_load(void *data, int noargc, Scheme_Object **noargv)
1052{
1053 Cmd_Info *info = (Cmd_Info *)data;
1054 Scheme_Object *result = scheme_void;
1055 Scheme_Object *expr;
1056 char_u *file = scheme_malloc_fail_ok(
1057 scheme_malloc_atomic, MAXPATHL + 1);
1058 Port_Info *pinfo = (Port_Info *)(info->data);
1059
1060 /* make Vim expansion */
1061 expand_env((char_u *)pinfo->name, file, MAXPATHL);
1062 /* scheme_load looks strange working with namespaces and error handling*/
1063 pinfo->port = scheme_open_input_file(file, "mzfile");
1064 scheme_count_lines(pinfo->port); /* to get accurate read error location*/
1065
1066 /* Like REPL but print only last result */
1067 while (!SCHEME_EOFP(expr = scheme_read(pinfo->port)))
1068 result = scheme_eval(expr, info->env);
1069
1070 /* errors will be caught in do_mzscheme_comamnd and ex_mzfile */
1071 scheme_close_input_port(pinfo->port);
1072 pinfo->port = NULL;
1073 return result;
1074}
1075
1076/* :mzfile */
1077 void
1078ex_mzfile(exarg_T *eap)
1079{
1080 Port_Info pinfo;
1081
1082 pinfo.name = (char *)eap->arg;
1083 pinfo.port = NULL;
1084 if (do_mzscheme_command(eap, &pinfo, do_load) != OK
1085 && pinfo.port != NULL) /* looks like port was not closed */
1086 scheme_close_input_port(pinfo.port);
1087}
1088
1089
1090/*
1091 *========================================================================
1092 * Exception handling code -- cribbed form the MzScheme sources and
1093 * Matthew Flatt's "Inside PLT MzScheme" document.
1094 *========================================================================
1095 */
1096 static void
1097init_exn_catching_apply(void)
1098{
1099 if (!exn_catching_apply)
1100 {
1101 char *e =
1102 "(lambda (thunk) "
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001103 "(with-handlers ([void (lambda (exn) (cons #f exn))]) "
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001104 "(cons #t (thunk))))";
1105
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001106 /* make sure we have a namespace with the standard syntax: */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001107 Scheme_Env *env = (Scheme_Env *)scheme_make_namespace(0, NULL);
1108 add_vim_exn(env);
1109
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001110 exn_catching_apply = scheme_eval_string(e, env);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001111 exn_p = scheme_lookup_global(scheme_intern_symbol("exn?"), env);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001112 exn_message = scheme_lookup_global(
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001113 scheme_intern_symbol("exn-message"), env);
1114 }
1115}
1116
1117/*
1118 * This function applies a thunk, returning the Scheme value if there's
1119 * no exception, otherwise returning NULL and setting *exn to the raised
1120 * value (usually an exn structure).
1121 */
1122 static Scheme_Object *
1123_apply_thunk_catch_exceptions(Scheme_Object *f, Scheme_Object **exn)
1124{
1125 Scheme_Object *v;
1126
1127 init_exn_catching_apply();
1128
1129 v = _scheme_apply(exn_catching_apply, 1, &f);
1130 /* v is a pair: (cons #t value) or (cons #f exn) */
1131
1132 if (SCHEME_TRUEP(SCHEME_CAR(v)))
1133 return SCHEME_CDR(v);
1134 else
1135 {
1136 *exn = SCHEME_CDR(v);
1137 return NULL;
1138 }
1139}
1140
1141 static Scheme_Object *
1142extract_exn_message(Scheme_Object *v)
1143{
1144 init_exn_catching_apply();
1145
1146 if (SCHEME_TRUEP(_scheme_apply(exn_p, 1, &v)))
1147 return _scheme_apply(exn_message, 1, &v);
1148 else
1149 return NULL; /* Not an exn structure */
1150}
1151
1152 static Scheme_Object *
1153do_eval(void *s, int noargc, Scheme_Object **noargv)
1154{
1155 Cmd_Info *info = (Cmd_Info *)s;
1156
1157 return scheme_eval_string_all((char *)(info->data), info->env, TRUE);
1158}
1159
1160 static Scheme_Object *
1161do_apply(void *a, int noargc, Scheme_Object **noargv)
1162{
1163 Apply_Info *info = (Apply_Info *)(((Cmd_Info *)a)->data);
1164
1165 return scheme_apply(info->proc, info->argc, info->argv);
1166}
1167
1168/*
1169 *========================================================================
1170 * 3. MzScheme I/O Handlers
1171 *========================================================================
1172 */
1173 static void
1174do_intrnl_output(char *mesg, long len, int error)
1175{
1176 char *p, *prev;
1177
1178 prev = mesg;
1179 p = strchr(prev, '\n');
1180 while (p)
1181 {
1182 *p = '\0';
1183 if (error)
1184 EMSG(prev);
1185 else
1186 MSG(prev);
1187 prev = p + 1;
1188 p = strchr(prev, '\n');
1189 }
1190
1191 if (error)
1192 EMSG(prev);
1193 else
1194 MSG(prev);
1195}
1196
1197 static void
1198do_output(char *mesg, long len)
1199{
1200 do_intrnl_output(mesg, len, 0);
1201}
1202
1203 static void
1204do_err_output(char *mesg, long len)
1205{
1206 do_intrnl_output(mesg, len, 1);
1207}
1208
1209 static void
1210do_printf(char *format, ...)
1211{
1212 do_intrnl_output(format, STRLEN(format), 1);
1213}
1214
1215 static void
1216do_flush(void)
1217{
1218 char *buff;
1219 long length;
1220
1221 buff = scheme_get_sized_string_output(curerr, &length);
1222 if (length)
1223 {
1224 do_err_output(buff, length);
1225 return;
1226 }
1227
1228 buff = scheme_get_sized_string_output(curout, &length);
1229 if (length)
1230 do_output(buff, length);
1231}
1232
1233 static int
1234mzscheme_io_init(void)
1235{
1236 /* Nothing needed so far... */
1237 return 0;
1238}
1239
1240/*
1241 *========================================================================
1242 * 4. Implementation of the Vim Features for MzScheme
1243 *========================================================================
1244 */
1245
1246/* (command {command-string}) */
1247 static Scheme_Object *
1248vim_command(void *data, int argc, Scheme_Object **argv)
1249{
1250 Vim_Prim *prim = (Vim_Prim *)data;
1251 char *cmd = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1252
1253 /* may be use do_cmdline_cmd? */
1254 do_cmdline((char_u *)cmd, NULL, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
1255 update_screen(VALID);
1256
1257 raise_if_error();
1258 return scheme_void;
1259}
1260
1261/* (eval {expr-string}) */
1262 static Scheme_Object *
1263vim_eval(void *data, int argc, Scheme_Object **argv)
1264{
1265#ifdef FEAT_EVAL
1266 Vim_Prim *prim = (Vim_Prim *)data;
1267 char *expr;
1268 char *str;
1269 Scheme_Object *result;
1270
1271 expr = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1272
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001273 str = (char *)eval_to_string((char_u *)expr, NULL, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001274
1275 if (str == NULL)
1276 raise_vim_exn(_("invalid expression"));
1277
1278 result = scheme_make_string(str);
1279
1280 vim_free(str);
1281
1282 return result;
1283#else
1284 raise_vim_exn(_("expressions disabled at compile time"));
1285 /* unreachable */
1286 return scheme_false;
1287#endif
1288}
1289
1290/* (range-start) */
1291 static Scheme_Object *
1292get_range_start(void *data, int argc, Scheme_Object **argv)
1293{
1294 return scheme_make_integer(range_start);
1295}
1296
1297/* (range-end) */
1298 static Scheme_Object *
1299get_range_end(void *data, int argc, Scheme_Object **argv)
1300{
1301 return scheme_make_integer(range_end);
1302}
1303
1304/* (beep) */
1305 static Scheme_Object *
1306mzscheme_beep(void *data, int argc, Scheme_Object **argv)
1307{
1308 vim_beep();
1309 return scheme_void;
1310}
1311
1312static Scheme_Object *M_global = NULL;
1313
1314/* (get-option {option-name}) [buffer/window] */
1315 static Scheme_Object *
1316get_option(void *data, int argc, Scheme_Object **argv)
1317{
1318 Vim_Prim *prim = (Vim_Prim *)data;
1319 char_u *name;
1320 long value;
1321 char_u *strval;
1322 int rc;
1323 Scheme_Object *rval;
1324 int opt_flags = 0;
1325 buf_T *save_curb = curbuf;
1326 win_T *save_curw = curwin;
1327
1328 name = (char_u *)SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1329
1330 if (argc > 1)
1331 {
1332 if (M_global == NULL)
1333 {
1334 MZ_REGISTER_STATIC(M_global);
1335 M_global = scheme_intern_symbol("global");
1336 }
1337
1338 if (argv[1] == M_global)
1339 opt_flags = OPT_GLOBAL;
1340 else if (SCHEME_VIMBUFFERP(argv[1]))
1341 {
1342 curbuf = get_valid_buffer(argv[1]);
1343 opt_flags = OPT_LOCAL;
1344 }
1345 else if (SCHEME_VIMWINDOWP(argv[1]))
1346 {
1347 win_T *win = get_valid_window(argv[1]);
1348
1349 curwin = win;
1350 curbuf = win->w_buffer;
1351 opt_flags = OPT_LOCAL;
1352 }
1353 else
1354 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1355 }
1356
1357 rc = get_option_value(name, &value, &strval, opt_flags);
1358 curbuf = save_curb;
1359 curwin = save_curw;
1360
1361 switch (rc)
1362 {
1363 case 1:
1364 return scheme_make_integer_value(value);
1365 case 0:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001366 rval = scheme_make_string(strval);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001367 vim_free(strval);
1368 return rval;
1369 case -1:
1370 case -2:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001371 raise_vim_exn(_("hidden option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001372 case -3:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001373 raise_vim_exn(_("unknown option"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001374 }
1375 /* unreachable */
1376 return scheme_void;
1377}
1378
1379/* (set-option {option-changing-string} [buffer/window]) */
1380 static Scheme_Object *
1381set_option(void *data, int argc, Scheme_Object **argv)
1382{
1383 char_u *cmd;
1384 int opt_flags = 0;
1385 buf_T *save_curb = curbuf;
1386 win_T *save_curw = curwin;
1387 Vim_Prim *prim = (Vim_Prim *)data;
1388
1389 GUARANTEE_STRING(prim->name, 0);
1390 if (argc > 1)
1391 {
1392 if (M_global == NULL)
1393 {
1394 MZ_REGISTER_STATIC(M_global);
1395 M_global = scheme_intern_symbol("global");
1396 }
1397
1398 if (argv[1] == M_global)
1399 opt_flags = OPT_GLOBAL;
1400 else if (SCHEME_VIMBUFFERP(argv[1]))
1401 {
1402 curbuf = get_valid_buffer(argv[1]);
1403 opt_flags = OPT_LOCAL;
1404 }
1405 else if (SCHEME_VIMWINDOWP(argv[1]))
1406 {
1407 win_T *win = get_valid_window(argv[1]);
1408 curwin = win;
1409 curbuf = win->w_buffer;
1410 opt_flags = OPT_LOCAL;
1411 }
1412 else
1413 scheme_wrong_type(prim->name, "vim-buffer/window", 1, argc, argv);
1414 }
1415
1416 /* do_set can modify cmd, make copy */
1417 cmd = vim_strsave((char_u *)SCHEME_STR_VAL(argv[0]));
1418 do_set(cmd, opt_flags);
1419 vim_free(cmd);
1420 update_screen(NOT_VALID);
1421 curbuf = save_curb;
1422 curwin = save_curw;
1423 raise_if_error();
1424 return scheme_void;
1425}
1426
1427/*
1428 *===========================================================================
1429 * 5. Vim Window-related Manipulation Functions
1430 *===========================================================================
1431 */
1432
1433/* (curr-win) */
1434 static Scheme_Object *
1435get_curr_win(void *data, int argc, Scheme_Object **argv)
1436{
1437 return (Scheme_Object *)get_vim_curr_window();
1438}
1439
1440/* (win-count) */
1441 static Scheme_Object *
1442get_window_count(void *data, int argc, Scheme_Object **argv)
1443{
1444 win_T *w;
1445 int n = 0;
1446
Bram Moolenaarf740b292006-02-16 22:11:02 +00001447 for (w = firstwin; w != NULL; w = w->w_next)
1448 ++n;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001449 return scheme_make_integer(n);
1450}
1451
1452/* (get-win-list [buffer]) */
1453 static Scheme_Object *
1454get_window_list(void *data, int argc, Scheme_Object **argv)
1455{
1456 Vim_Prim *prim = (Vim_Prim *)data;
1457 vim_mz_buffer *buf;
1458 Scheme_Object *list;
1459 win_T *w;
1460
1461 buf = get_buffer_arg(prim->name, 0, argc, argv);
1462 list = scheme_null;
1463
Bram Moolenaarf740b292006-02-16 22:11:02 +00001464 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001465 if (w->w_buffer == buf->buf)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001466 list = scheme_make_pair(window_new(w), list);
1467
1468 return list;
1469}
1470
1471 static Scheme_Object *
1472window_new(win_T *win)
1473{
1474 vim_mz_window *self;
1475
1476 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001477 * If we add a "w_mzscheme_ref" field to the win_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001478 * then we can get at it in win_free() in vim.
1479 *
1480 * On a win_free() we set the Scheme object's win_T *field
1481 * to an invalid value. We trap all uses of a window
1482 * object, and reject them if the win_T *field is invalid.
1483 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001484 if (win->w_mzscheme_ref != NULL)
1485 return win->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001486
1487 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_window));
1488
1489 vim_memset(self, 0, sizeof(vim_mz_window));
1490 scheme_dont_gc_ptr(self); /* because win isn't visible to GC */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001491 win->w_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001492 self->win = win;
1493 self->tag = mz_window_type;
1494
1495 return (Scheme_Object *)(self);
1496}
1497
1498/* (get-win-num [window]) */
1499 static Scheme_Object *
1500get_window_num(void *data, int argc, Scheme_Object **argv)
1501{
1502 Vim_Prim *prim = (Vim_Prim *)data;
1503 win_T *win = get_window_arg(prim->name, 0, argc, argv)->win;
1504 int nr = 1;
1505 win_T *wp;
1506
1507 for (wp = firstwin; wp != win; wp = wp->w_next)
1508 ++nr;
1509
1510 return scheme_make_integer(nr);
1511}
1512
1513/* (get-win-by-num {windownum}) */
1514 static Scheme_Object *
1515get_window_by_num(void *data, int argc, Scheme_Object **argv)
1516{
1517 Vim_Prim *prim = (Vim_Prim *)data;
1518 win_T *win;
1519 int fnum;
1520
1521 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1522 if (fnum < 1)
1523 scheme_signal_error(_("window index is out of range"));
1524
Bram Moolenaarf740b292006-02-16 22:11:02 +00001525 for (win = firstwin; win != NULL; win = win->w_next, --fnum)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001526 if (fnum == 1) /* to be 1-based */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001527 return window_new(win);
1528
1529 return scheme_false;
1530}
1531
1532/* (get-win-buffer [window]) */
1533 static Scheme_Object *
1534get_window_buffer(void *data, int argc, Scheme_Object **argv)
1535{
1536 Vim_Prim *prim = (Vim_Prim *)data;
1537 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1538
1539 return buffer_new(win->win->w_buffer);
1540}
1541
1542/* (get-win-height [window]) */
1543 static Scheme_Object *
1544get_window_height(void *data, int argc, Scheme_Object **argv)
1545{
1546 Vim_Prim *prim = (Vim_Prim *)data;
1547 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1548
1549 return scheme_make_integer(win->win->w_height);
1550}
1551
1552/* (set-win-height {height} [window]) */
1553 static Scheme_Object *
1554set_window_height(void *data, int argc, Scheme_Object **argv)
1555{
1556 Vim_Prim *prim = (Vim_Prim *)data;
1557 vim_mz_window *win;
1558 win_T *savewin;
1559 int height;
1560
1561 win = get_window_arg(prim->name, 1, argc, argv);
1562 height = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1563
1564#ifdef FEAT_GUI
1565 need_mouse_correct = TRUE;
1566#endif
1567
1568 savewin = curwin;
1569 curwin = win->win;
1570 win_setheight(height);
1571 curwin = savewin;
1572
1573 raise_if_error();
1574 return scheme_void;
1575}
1576
1577#ifdef FEAT_VERTSPLIT
1578/* (get-win-width [window]) */
1579 static Scheme_Object *
1580get_window_width(void *data, int argc, Scheme_Object **argv)
1581{
1582 Vim_Prim *prim = (Vim_Prim *)data;
1583 vim_mz_window *win = get_window_arg(prim->name, 0, argc, argv);
1584
1585 return scheme_make_integer(W_WIDTH(win->win));
1586}
1587
1588/* (set-win-width {width} [window]) */
1589 static Scheme_Object *
1590set_window_width(void *data, int argc, Scheme_Object **argv)
1591{
1592 Vim_Prim *prim = (Vim_Prim *)data;
1593 vim_mz_window *win;
1594 win_T *savewin;
1595 int width = 0;
1596
1597 win = get_window_arg(prim->name, 1, argc, argv);
1598 width = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1599
1600# ifdef FEAT_GUI
1601 need_mouse_correct = TRUE;
1602# endif
1603
1604 savewin = curwin;
1605 curwin = win->win;
1606 win_setwidth(width);
1607 curwin = savewin;
1608
1609 raise_if_error();
1610 return scheme_void;
1611}
1612#endif
1613
1614/* (get-cursor [window]) -> (line . col) */
1615 static Scheme_Object *
1616get_cursor(void *data, int argc, Scheme_Object **argv)
1617{
1618 Vim_Prim *prim = (Vim_Prim *)data;
1619 vim_mz_window *win;
1620 pos_T pos;
1621
1622 win = get_window_arg(prim->name, 0, argc, argv);
1623 pos = win->win->w_cursor;
1624 return scheme_make_pair(scheme_make_integer_value((long)pos.lnum),
1625 scheme_make_integer_value((long)pos.col + 1));
1626}
1627
1628/* (set-cursor (line . col) [window]) */
1629 static Scheme_Object *
1630set_cursor(void *data, int argc, Scheme_Object **argv)
1631{
1632 Vim_Prim *prim = (Vim_Prim *)data;
1633 vim_mz_window *win;
1634 long lnum = 0;
1635 long col = 0;
1636
Bram Moolenaar555b2802005-05-19 21:08:39 +00001637#ifdef HAVE_SANDBOX
1638 sandbox_check();
1639#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001640 win = get_window_arg(prim->name, 1, argc, argv);
1641 GUARANTEE_PAIR(prim->name, 0);
1642
1643 if (!SCHEME_INTP(SCHEME_CAR(argv[0]))
1644 || !SCHEME_INTP(SCHEME_CDR(argv[0])))
1645 scheme_wrong_type(prim->name, "integer pair", 0, argc, argv);
1646
1647 lnum = SCHEME_INT_VAL(SCHEME_CAR(argv[0]));
1648 col = SCHEME_INT_VAL(SCHEME_CDR(argv[0])) - 1;
1649
1650 check_line_range(lnum, win->win->w_buffer);
1651 /* don't know how to catch invalid column value */
1652
1653 win->win->w_cursor.lnum = lnum;
1654 win->win->w_cursor.col = col;
1655 update_screen(VALID);
1656
1657 raise_if_error();
1658 return scheme_void;
1659}
1660/*
1661 *===========================================================================
1662 * 6. Vim Buffer-related Manipulation Functions
1663 * Note that each buffer should have its own private namespace.
1664 *===========================================================================
1665 */
1666
1667/* (open-buff {filename}) */
1668 static Scheme_Object *
1669mzscheme_open_buffer(void *data, int argc, Scheme_Object **argv)
1670{
1671 Vim_Prim *prim = (Vim_Prim *)data;
1672 char *fname;
1673 int num = 0;
1674 Scheme_Object *onum;
1675
Bram Moolenaar555b2802005-05-19 21:08:39 +00001676#ifdef HAVE_SANDBOX
1677 sandbox_check();
1678#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001679 fname = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1680 /* TODO make open existing file */
1681 num = buflist_add(fname, BLN_LISTED | BLN_CURBUF);
1682
1683 if (num == 0)
1684 raise_vim_exn(_("couldn't open buffer"));
1685
1686 onum = scheme_make_integer(num);
1687 return get_buffer_by_num(data, 1, &onum);
1688}
1689
1690/* (get-buff-by-num {buffernum}) */
1691 static Scheme_Object *
1692get_buffer_by_num(void *data, int argc, Scheme_Object **argv)
1693{
1694 Vim_Prim *prim = (Vim_Prim *)data;
1695 buf_T *buf;
1696 int fnum;
1697
1698 fnum = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1699
1700 for (buf = firstbuf; buf; buf = buf->b_next)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001701 if (buf->b_fnum == fnum)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001702 return buffer_new(buf);
1703
1704 return scheme_false;
1705}
1706
1707/* (get-buff-by-name {buffername}) */
1708 static Scheme_Object *
1709get_buffer_by_name(void *data, int argc, Scheme_Object **argv)
1710{
1711 Vim_Prim *prim = (Vim_Prim *)data;
1712 buf_T *buf;
1713 char_u *fname;
1714
1715 fname = SCHEME_STR_VAL(GUARANTEE_STRING(prim->name, 0));
1716
1717 for (buf = firstbuf; buf; buf = buf->b_next)
1718 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1719 /* empty string */
1720 {
1721 if (fname[0] == NUL)
1722 return buffer_new(buf);
1723 }
1724 else if (!fnamecmp(buf->b_ffname, fname)
1725 || !fnamecmp(buf->b_sfname, fname))
1726 /* either short or long filename matches */
1727 return buffer_new(buf);
1728
1729 return scheme_false;
1730}
1731
1732/* (get-next-buff [buffer]) */
1733 static Scheme_Object *
1734get_next_buffer(void *data, int argc, Scheme_Object **argv)
1735{
1736 Vim_Prim *prim = (Vim_Prim *)data;
1737 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1738
1739 if (buf->b_next == NULL)
1740 return scheme_false;
1741 else
1742 return buffer_new(buf->b_next);
1743}
1744
1745/* (get-prev-buff [buffer]) */
1746 static Scheme_Object *
1747get_prev_buffer(void *data, int argc, Scheme_Object **argv)
1748{
1749 Vim_Prim *prim = (Vim_Prim *)data;
1750 buf_T *buf = get_buffer_arg(prim->name, 0, argc, argv)->buf;
1751
1752 if (buf->b_prev == NULL)
1753 return scheme_false;
1754 else
1755 return buffer_new(buf->b_prev);
1756}
1757
1758/* (get-buff-num [buffer]) */
1759 static Scheme_Object *
1760get_buffer_num(void *data, int argc, Scheme_Object **argv)
1761{
1762 Vim_Prim *prim = (Vim_Prim *)data;
1763 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1764
1765 return scheme_make_integer(buf->buf->b_fnum);
1766}
1767
1768/* (buff-count) */
1769 static Scheme_Object *
1770get_buffer_count(void *data, int argc, Scheme_Object **argv)
1771{
1772 buf_T *b;
1773 int n = 0;
1774
1775 for (b = firstbuf; b; b = b->b_next) ++n;
1776 return scheme_make_integer(n);
1777}
1778
1779/* (get-buff-name [buffer]) */
1780 static Scheme_Object *
1781get_buffer_name(void *data, int argc, Scheme_Object **argv)
1782{
1783 Vim_Prim *prim = (Vim_Prim *)data;
1784 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1785
1786 return scheme_make_string(buf->buf->b_ffname);
1787}
1788
1789/* (curr-buff) */
1790 static Scheme_Object *
1791get_curr_buffer(void *data, int argc, Scheme_Object **argv)
1792{
1793 return (Scheme_Object *)get_vim_curr_buffer();
1794}
1795
1796 static Scheme_Object *
1797buffer_new(buf_T *buf)
1798{
1799 vim_mz_buffer *self;
1800
1801 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001802 * If we add a "b_mzscheme_ref" field to the buf_T structure,
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001803 * then we can get at it in buf_freeall() in vim.
1804 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001805 if (buf->b_mzscheme_ref)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001806 return buf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001807
1808 self = scheme_malloc_fail_ok(scheme_malloc, sizeof(vim_mz_buffer));
1809
1810 vim_memset(self, 0, sizeof(vim_mz_buffer));
1811 scheme_dont_gc_ptr(self); /* because buf isn't visible to GC */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001812 buf->b_mzscheme_ref = self;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001813 self->buf = buf;
1814 self->tag = mz_buffer_type;
1815
1816 mzscheme_interface_init(self); /* Set up namespace */
1817
1818 return (Scheme_Object *)(self);
1819}
1820
1821/*
1822 * (get-buff-size [buffer])
1823 *
1824 * Get the size (number of lines) in the current buffer.
1825 */
1826 static Scheme_Object *
1827get_buffer_size(void *data, int argc, Scheme_Object **argv)
1828{
1829 Vim_Prim *prim = (Vim_Prim *)data;
1830 vim_mz_buffer *buf = get_buffer_arg(prim->name, 0, argc, argv);
1831
1832 return scheme_make_integer(buf->buf->b_ml.ml_line_count);
1833}
1834
1835/*
1836 * (get-buff-line {linenr} [buffer])
1837 *
1838 * Get a line from the specified buffer. The line number is
1839 * in Vim format (1-based). The line is returned as a MzScheme
1840 * string object.
1841 */
1842 static Scheme_Object *
1843get_buffer_line(void *data, int argc, Scheme_Object **argv)
1844{
1845 Vim_Prim *prim = (Vim_Prim *)data;
1846 vim_mz_buffer *buf;
1847 int linenr;
1848 char *line;
1849
1850 buf = get_buffer_arg(prim->name, 1, argc, argv);
1851 linenr = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1852 line = ml_get_buf(buf->buf, (linenr_T)linenr, FALSE);
1853
1854 raise_if_error();
1855 return scheme_make_string(line);
1856}
1857
1858
1859/*
1860 * (get-buff-line-list {start} {end} [buffer])
1861 *
1862 * Get a list of lines from the specified buffer. The line numbers
1863 * are in Vim format (1-based). The range is from lo up to, but not
1864 * including, hi. The list is returned as a list of string objects.
1865 */
1866 static Scheme_Object *
1867get_buffer_line_list(void *data, int argc, Scheme_Object **argv)
1868{
1869 Vim_Prim *prim = (Vim_Prim *)data;
1870 vim_mz_buffer *buf;
1871 int i, hi, lo, n;
1872 Scheme_Object *list;
1873
1874 buf = get_buffer_arg(prim->name, 2, argc, argv);
1875 list = scheme_null;
1876 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
1877 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1878
1879 /*
1880 * Handle some error conditions
1881 */
1882 if (lo < 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001883 lo = 0;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001884
1885 if (hi < 0)
1886 hi = 0;
1887 if (hi < lo)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001888 hi = lo;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001889
1890 n = hi - lo;
1891
1892 for (i = n; i >= 0; --i)
1893 {
1894 Scheme_Object *str = scheme_make_string(
1895 (char *)ml_get_buf(buf->buf, (linenr_T)(lo+i), FALSE));
1896 raise_if_error();
1897
1898 /* Set the list item */
1899 list = scheme_make_pair(str, list);
1900 }
1901
1902 return list;
1903}
1904
1905/*
1906 * (set-buff-line {linenr} {string/#f} [buffer])
1907 *
1908 * Replace a line in the specified buffer. The line number is
1909 * in Vim format (1-based). The replacement line is given as
1910 * an MzScheme string object. The object is checked for validity
1911 * and correct format. An exception is thrown if the values are not
1912 * the correct format.
1913 *
1914 * It returns a Scheme Object that indicates the length of the
1915 * string changed.
1916 */
1917 static Scheme_Object *
1918set_buffer_line(void *data, int argc, Scheme_Object **argv)
1919{
1920 /* First of all, we check the the of the supplied MzScheme object.
1921 * There are three cases:
1922 * 1. #f - this is a deletion.
1923 * 2. A string - this is a replacement.
1924 * 3. Anything else - this is an error.
1925 */
1926 Vim_Prim *prim = (Vim_Prim *)data;
1927 vim_mz_buffer *buf;
1928 Scheme_Object *line;
1929 char *save;
1930 buf_T *savebuf;
1931 int n;
1932
Bram Moolenaar555b2802005-05-19 21:08:39 +00001933#ifdef HAVE_SANDBOX
1934 sandbox_check();
1935#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001936 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
1937 if (!SCHEME_STRINGP(argv[1]) && !SCHEME_FALSEP(argv[1]))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001938 scheme_wrong_type(prim->name, "string or #f", 1, argc, argv);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001939 line = argv[1];
1940 buf = get_buffer_arg(prim->name, 2, argc, argv);
1941
1942 check_line_range(n, buf->buf);
1943
1944 if (SCHEME_FALSEP(line))
1945 {
1946 savebuf = curbuf;
1947 curbuf = buf->buf;
1948
1949 if (u_savedel((linenr_T)n, 1L) == FAIL)
1950 {
1951 curbuf = savebuf;
1952 raise_vim_exn(_("cannot save undo information"));
1953 }
1954 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
1955 {
1956 curbuf = savebuf;
1957 raise_vim_exn(_("cannot delete line"));
1958 }
1959 deleted_lines_mark((linenr_T)n, 1L);
1960 if (buf->buf == curwin->w_buffer)
1961 mz_fix_cursor(n, n + 1, -1);
1962
1963 curbuf = savebuf;
1964
1965 raise_if_error();
1966 return scheme_void;
1967 }
1968
1969 /* Otherwise it's a line */
1970 save = string_to_line(line);
1971 savebuf = curbuf;
1972
1973 curbuf = buf->buf;
1974
1975 if (u_savesub((linenr_T)n) == FAIL)
1976 {
1977 curbuf = savebuf;
1978 raise_vim_exn(_("cannot save undo information"));
1979 }
1980 else if (ml_replace((linenr_T)n, (char_u *)save, TRUE) == FAIL)
1981 {
1982 curbuf = savebuf;
1983 raise_vim_exn(_("cannot replace line"));
1984 }
1985 else
1986 changed_bytes((linenr_T)n, 0);
1987
1988 curbuf = savebuf;
1989
1990 raise_if_error();
1991 return scheme_void;
1992}
1993
1994/*
1995 * (set-buff-line-list {start} {end} {string-list/#f/null} [buffer])
1996 *
1997 * Replace a range of lines in the specified buffer. The line numbers are in
1998 * Vim format (1-based). The range is from lo up to, but not including, hi.
1999 * The replacement lines are given as a Scheme list of string objects. The
2000 * list is checked for validity and correct format.
2001 *
2002 * Errors are returned as a value of FAIL. The return value is OK on success.
2003 * If OK is returned and len_change is not NULL, *len_change is set to the
2004 * change in the buffer length.
2005 */
2006 static Scheme_Object *
2007set_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2008{
2009 /* First of all, we check the type of the supplied MzScheme object.
2010 * There are three cases:
2011 * 1. #f - this is a deletion.
2012 * 2. A list - this is a replacement.
2013 * 3. Anything else - this is an error.
2014 */
2015 Vim_Prim *prim = (Vim_Prim *)data;
2016 vim_mz_buffer *buf;
2017 Scheme_Object *line_list;
2018 Scheme_Object *line;
2019 Scheme_Object *rest;
2020 char **array;
2021 buf_T *savebuf;
2022 int i, old_len, new_len, hi, lo;
2023 long extra;
2024
Bram Moolenaar555b2802005-05-19 21:08:39 +00002025#ifdef HAVE_SANDBOX
2026 sandbox_check();
2027#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002028 lo = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2029 hi = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 1));
2030 if (!SCHEME_PAIRP(argv[2])
2031 && !SCHEME_FALSEP(argv[2]) && !SCHEME_NULLP(argv[2]))
2032 scheme_wrong_type(prim->name, "list or #f", 2, argc, argv);
2033 line_list = argv[2];
2034 buf = get_buffer_arg(prim->name, 3, argc, argv);
2035 old_len = hi - lo;
2036 if (old_len < 0) /* process inverse values wisely */
2037 {
2038 i = lo;
2039 lo = hi;
2040 hi = i;
2041 old_len = -old_len;
2042 }
2043 extra = 0;
2044
2045 check_line_range(lo, buf->buf); /* inclusive */
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002046 check_line_range(hi - 1, buf->buf); /* exclusive */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002047
2048 if (SCHEME_FALSEP(line_list) || SCHEME_NULLP(line_list))
2049 {
2050 savebuf = curbuf;
2051 curbuf = buf->buf;
2052
2053 if (u_savedel((linenr_T)lo, (long)old_len) == FAIL)
2054 {
2055 curbuf = savebuf;
2056 raise_vim_exn(_("cannot save undo information"));
2057 }
2058 else
2059 {
2060 for (i = 0; i < old_len; i++)
2061 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2062 {
2063 curbuf = savebuf;
2064 raise_vim_exn(_("cannot delete line"));
2065 }
2066 deleted_lines_mark((linenr_T)lo, (long)old_len);
2067 if (buf->buf == curwin->w_buffer)
2068 mz_fix_cursor(lo, hi, -old_len);
2069 }
2070
2071 curbuf = savebuf;
2072
2073 raise_if_error();
2074 return scheme_void;
2075 }
2076
2077 /* List */
2078 new_len = scheme_proper_list_length(line_list);
2079 if (new_len < 0) /* improper or cyclic list */
2080 scheme_wrong_type(prim->name, "proper list",
2081 2, argc, argv);
2082
2083 /* Using MzScheme allocator, so we don't need to free this and
2084 * can safely keep pointers to GC collected strings
2085 */
2086 array = (char **)scheme_malloc_fail_ok(scheme_malloc,
2087 (unsigned)(new_len * sizeof(char *)));
2088
2089 rest = line_list;
2090 for (i = 0; i < new_len; ++i)
2091 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002092 line = SCHEME_CAR(rest);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002093 rest = SCHEME_CDR(rest);
2094 if (!SCHEME_STRINGP(line))
2095 scheme_wrong_type(prim->name, "string-list", 2, argc, argv);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002096 array[i] = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002097 }
2098
2099 savebuf = curbuf;
2100 curbuf = buf->buf;
2101
2102 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2103 {
2104 curbuf = savebuf;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002105 raise_vim_exn(_("cannot save undo information"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002106 }
2107
2108 /*
2109 * If the size of the range is reducing (ie, new_len < old_len) we
2110 * need to delete some old_len. We do this at the start, by
2111 * repeatedly deleting line "lo".
2112 */
2113 for (i = 0; i < old_len - new_len; ++i)
2114 {
2115 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2116 {
2117 curbuf = savebuf;
2118 raise_vim_exn(_("cannot delete line"));
2119 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002120 extra--;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002121 }
2122
2123 /*
2124 * For as long as possible, replace the existing old_len with the
2125 * new old_len. This is a more efficient operation, as it requires
2126 * less memory allocation and freeing.
2127 */
2128 for (i = 0; i < old_len && i < new_len; i++)
2129 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], TRUE) == FAIL)
2130 {
2131 curbuf = savebuf;
2132 raise_vim_exn(_("cannot replace line"));
2133 }
2134
2135 /*
2136 * Now we may need to insert the remaining new_len. We don't need to
2137 * free the string passed back because MzScheme has control of that
2138 * memory.
2139 */
2140 while (i < new_len)
2141 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002142 if (ml_append((linenr_T)(lo + i - 1),
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002143 (char_u *)array[i], 0, FALSE) == FAIL)
2144 {
2145 curbuf = savebuf;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002146 raise_vim_exn(_("cannot insert line"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002147 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002148 ++i;
2149 ++extra;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002150 }
2151
2152 /*
2153 * Adjust marks. Invalidate any which lie in the
2154 * changed range, and move any in the remainder of the buffer.
2155 */
2156 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra);
2157 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2158
2159 if (buf->buf == curwin->w_buffer)
2160 mz_fix_cursor(lo, hi, extra);
2161 curbuf = savebuf;
2162
2163 raise_if_error();
2164 return scheme_void;
2165}
2166
2167/*
2168 * (insert-buff-line-list {linenr} {string/string-list} [buffer])
2169 *
2170 * Insert a number of lines into the specified buffer after the specifed line.
2171 * The line number is in Vim format (1-based). The lines to be inserted are
2172 * given as an MzScheme list of string objects or as a single string. The lines
2173 * to be added are checked for validity and correct format. Errors are
2174 * returned as a value of FAIL. The return value is OK on success.
2175 * If OK is returned and len_change is not NULL, *len_change
2176 * is set to the change in the buffer length.
2177 */
2178 static Scheme_Object *
2179insert_buffer_line_list(void *data, int argc, Scheme_Object **argv)
2180{
2181 Vim_Prim *prim = (Vim_Prim *)data;
2182 vim_mz_buffer *buf;
2183 Scheme_Object *list;
2184 Scheme_Object *line;
2185 Scheme_Object *rest;
2186 char **array;
2187 char *str;
2188 buf_T *savebuf;
2189 int i, n, size;
2190
Bram Moolenaar555b2802005-05-19 21:08:39 +00002191#ifdef HAVE_SANDBOX
2192 sandbox_check();
2193#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002194 /*
2195 * First of all, we check the type of the supplied MzScheme object.
2196 * It must be a string or a list, or the call is in error.
2197 */
2198 n = SCHEME_INT_VAL(GUARANTEE_INTEGER(prim->name, 0));
2199 list = argv[1];
2200
2201 if (!SCHEME_STRINGP(list) && !SCHEME_PAIRP(list))
2202 scheme_wrong_type(prim->name, "string or list", 1, argc, argv);
2203 buf = get_buffer_arg(prim->name, 2, argc, argv);
2204
2205 if (n != 0) /* 0 can be used in insert */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002206 check_line_range(n, buf->buf);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002207 if (SCHEME_STRINGP(list))
2208 {
2209 str = string_to_line(list);
2210
2211 savebuf = curbuf;
2212 curbuf = buf->buf;
2213
2214 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2215 {
2216 curbuf = savebuf;
2217 raise_vim_exn(_("cannot save undo information"));
2218 }
2219 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2220 {
2221 curbuf = savebuf;
2222 raise_vim_exn(_("cannot insert line"));
2223 }
2224 else
2225 appended_lines_mark((linenr_T)n, 1L);
2226
2227 curbuf = savebuf;
2228 update_screen(VALID);
2229
2230 raise_if_error();
2231 return scheme_void;
2232 }
2233
2234 /* List */
2235 size = scheme_proper_list_length(list);
2236 if (size < 0) /* improper or cyclic list */
2237 scheme_wrong_type(prim->name, "proper list",
2238 2, argc, argv);
2239
2240 /* Using MzScheme allocator, so we don't need to free this and
2241 * can safely keep pointers to GC collected strings
2242 */
2243 array = (char **)scheme_malloc_fail_ok(
2244 scheme_malloc, (unsigned)(size * sizeof(char *)));
2245
2246 rest = list;
2247 for (i = 0; i < size; ++i)
2248 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002249 line = SCHEME_CAR(rest);
2250 rest = SCHEME_CDR(rest);
2251 array[i] = string_to_line(line);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002252 }
2253
2254 savebuf = curbuf;
2255 curbuf = buf->buf;
2256
2257 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2258 {
2259 curbuf = savebuf;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002260 raise_vim_exn(_("cannot save undo information"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002261 }
2262 else
2263 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002264 for (i = 0; i < size; ++i)
2265 if (ml_append((linenr_T)(n + i), (char_u *)array[i],
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002266 0, FALSE) == FAIL)
2267 {
2268 curbuf = savebuf;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002269 raise_vim_exn(_("cannot insert line"));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002270 }
2271
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002272 if (i > 0)
2273 appended_lines_mark((linenr_T)n, (long)i);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002274 }
2275
2276 curbuf = savebuf;
2277 update_screen(VALID);
2278
2279 raise_if_error();
2280 return scheme_void;
2281}
2282
2283/* (get-buff-namespace [buffer]) */
2284 static Scheme_Object *
2285get_buffer_namespace(void *data, int argc, Scheme_Object **argv)
2286{
2287 Vim_Prim *prim = (Vim_Prim *)data;
2288
2289 return (Scheme_Object *)get_buffer_arg(prim->name, 0, argc, argv)->env;
2290}
2291
2292/*
2293 * Predicates
2294 */
2295/* (buff? obj) */
2296 static Scheme_Object *
2297vim_bufferp(void *data, int argc, Scheme_Object **argv)
2298{
2299 if (SCHEME_VIMBUFFERP(argv[0]))
2300 return scheme_true;
2301 else
2302 return scheme_false;
2303}
2304
2305/* (win? obj) */
2306 static Scheme_Object *
2307vim_windowp(void *data, int argc, Scheme_Object **argv)
2308{
2309 if (SCHEME_VIMWINDOWP(argv[0]))
2310 return scheme_true;
2311 else
2312 return scheme_false;
2313}
2314
2315/* (buff-valid? obj) */
2316 static Scheme_Object *
2317vim_buffer_validp(void *data, int argc, Scheme_Object **argv)
2318{
2319 if (SCHEME_VIMBUFFERP(argv[0])
2320 && ((vim_mz_buffer *)argv[0])->buf != INVALID_BUFFER_VALUE)
2321 return scheme_true;
2322 else
2323 return scheme_false;
2324}
2325
2326/* (win-valid? obj) */
2327 static Scheme_Object *
2328vim_window_validp(void *data, int argc, Scheme_Object **argv)
2329{
2330 if (SCHEME_VIMWINDOWP(argv[0])
2331 && ((vim_mz_window *)argv[0])->win != INVALID_WINDOW_VALUE)
2332 return scheme_true;
2333 else
2334 return scheme_false;
2335}
2336
2337/*
2338 *===========================================================================
2339 * Utilities
2340 *===========================================================================
2341 */
2342
2343/*
2344 * Convert an MzScheme string into a Vim line.
2345 *
2346 * The result is in allocated memory. All internal nulls are replaced by
2347 * newline characters. It is an error for the string to contain newline
2348 * characters.
2349 *
2350 */
2351 static char *
2352string_to_line(Scheme_Object *obj)
2353{
2354 char *str;
2355 long len;
2356 int i;
2357
2358 str = scheme_display_to_string(obj, &len);
2359
2360 /* Error checking: String must not contain newlines, as we
2361 * are replacing a single line, and we must replace it with
2362 * a single line.
2363 */
2364 if (memchr(str, '\n', len))
2365 scheme_signal_error(_("string cannot contain newlines"));
2366
2367 /* Create a copy of the string, with internal nulls replaced by
2368 * newline characters, as is the vim convention.
2369 */
2370 for (i = 0; i < len; ++i)
2371 {
2372 if (str[i] == '\0')
2373 str[i] = '\n';
2374 }
2375
2376 str[i] = '\0';
2377
2378 return str;
2379}
2380
2381/*
2382 * Check to see whether a Vim error has been reported, or a keyboard
2383 * interrupt (from vim --> got_int) has been detected.
2384 */
2385 static int
2386vim_error_check(void)
2387{
2388 return (got_int || did_emsg);
2389}
2390
2391/*
2392 * register Scheme exn:vim
2393 */
2394 static void
2395register_vim_exn(Scheme_Env *env)
2396{
2397 Scheme_Object *exn_name = scheme_intern_symbol("exn:vim");
2398
2399 if (vim_exn == NULL)
2400 vim_exn = scheme_make_struct_type(exn_name,
2401 scheme_builtin_value("struct:exn"), NULL, 0, 0, NULL, NULL
2402#if MZSCHEME_VERSION_MAJOR >= 299
2403 , NULL
2404#endif
2405 );
2406
2407 if (vim_exn_values == NULL)
2408 {
2409 int nc = 0;
2410
2411 Scheme_Object **exn_names = scheme_make_struct_names(
2412 exn_name, scheme_null, 0, &nc);
2413 Scheme_Object **exn_values = scheme_make_struct_values(
2414 vim_exn, exn_names, nc, 0);
2415
2416 vim_exn_names = scheme_make_vector(nc, scheme_false);
2417 vim_exn_values = scheme_make_vector(nc, scheme_false);
2418 /* remember names and values */
2419 mch_memmove(SCHEME_VEC_ELS(vim_exn_names), exn_names,
2420 nc * sizeof(Scheme_Object *));
2421 mch_memmove(SCHEME_VEC_ELS(vim_exn_values), exn_values,
2422 nc * sizeof(Scheme_Object *));
2423 }
2424
2425 add_vim_exn(env);
2426}
2427
2428/*
2429 * Add stuff of exn:vim to env
2430 */
2431 static void
2432add_vim_exn(Scheme_Env *env)
2433{
2434 int i;
2435
2436 for (i = 0; i < SCHEME_VEC_SIZE(vim_exn_values); i++)
2437 scheme_add_global_symbol(SCHEME_VEC_ELS(vim_exn_names)[i],
2438 SCHEME_VEC_ELS(vim_exn_values)[i], env);
2439}
2440
2441/*
2442 * raise exn:vim, may be with additional info string
2443 */
2444 void
2445raise_vim_exn(const char *add_info)
2446{
2447 Scheme_Object *argv[2];
2448 char_u *fmt = _("Vim error: ~a");
2449
2450 if (add_info != NULL)
2451 {
2452 Scheme_Object *info = scheme_make_string(add_info);
Bram Moolenaar555b2802005-05-19 21:08:39 +00002453 argv[0] = scheme_byte_string_to_char_string(scheme_make_string(
2454 scheme_format(fmt, strlen(fmt), 1, &info, NULL)));
2455 SCHEME_SET_IMMUTABLE(argv[0]);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002456 }
2457 else
2458 argv[0] = scheme_make_string(_("Vim error"));
2459
Bram Moolenaar049377e2007-05-12 15:32:12 +00002460#if MZSCHEME_VERSION_MAJOR < 360
2461 argv[1] = scheme_current_continuation_marks();
2462#else
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00002463 argv[1] = scheme_current_continuation_marks(NULL);
Bram Moolenaar049377e2007-05-12 15:32:12 +00002464#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002465
2466 scheme_raise(scheme_make_struct_instance(vim_exn, 2, argv));
2467}
2468
2469 void
2470raise_if_error(void)
2471{
2472 if (vim_error_check())
2473 raise_vim_exn(NULL);
2474}
2475
2476/* get buffer:
2477 * either current
2478 * or passed as argv[argnum] with checks
2479 */
2480 static vim_mz_buffer *
2481get_buffer_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
2482{
2483 vim_mz_buffer *b;
2484
2485 if (argc < argnum + 1)
2486 return get_vim_curr_buffer();
2487 if (!SCHEME_VIMBUFFERP(argv[argnum]))
2488 scheme_wrong_type(fname, "vim-buffer", argnum, argc, argv);
2489 b = (vim_mz_buffer *)argv[argnum];
2490 (void)get_valid_buffer(argv[argnum]);
2491 return b;
2492}
2493
2494/* get window:
2495 * either current
2496 * or passed as argv[argnum] with checks
2497 */
2498 static vim_mz_window *
2499get_window_arg(const char *fname, int argnum, int argc, Scheme_Object **argv)
2500{
2501 vim_mz_window *w;
2502
2503 if (argc < argnum + 1)
2504 return get_vim_curr_window();
2505 w = (vim_mz_window *)argv[argnum];
2506 if (!SCHEME_VIMWINDOWP(argv[argnum]))
2507 scheme_wrong_type(fname, "vim-window", argnum, argc, argv);
2508 (void)get_valid_window(argv[argnum]);
2509 return w;
2510}
2511
2512/* get valid Vim buffer from Scheme_Object* */
2513buf_T *get_valid_buffer(void *obj)
2514{
2515 buf_T *buf = ((vim_mz_buffer *)obj)->buf;
2516
2517 if (buf == INVALID_BUFFER_VALUE)
2518 scheme_signal_error(_("buffer is invalid"));
2519 return buf;
2520}
2521
2522/* get valid Vim window from Scheme_Object* */
2523win_T *get_valid_window(void *obj)
2524{
2525 win_T *win = ((vim_mz_window *)obj)->win;
2526 if (win == INVALID_WINDOW_VALUE)
2527 scheme_signal_error(_("window is invalid"));
2528 return win;
2529}
2530
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002531 int
2532mzthreads_allowed(void)
2533{
2534 return mz_threads_allow;
2535}
2536
2537 static int
2538line_in_range(linenr_T lnum, buf_T *buf)
2539{
2540 return (lnum > 0 && lnum <= buf->b_ml.ml_line_count);
2541}
2542
2543 static void
2544check_line_range(linenr_T lnum, buf_T *buf)
2545{
2546 if (!line_in_range(lnum, buf))
2547 scheme_signal_error(_("linenr out of range"));
2548}
2549
2550/*
2551 * Check if deleting lines made the cursor position invalid
2552 * (or you'll get msg from Vim about invalid linenr).
2553 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2554 * deleted). Got from if_python.c
2555 */
2556 static void
2557mz_fix_cursor(int lo, int hi, int extra)
2558{
2559 if (curwin->w_cursor.lnum >= lo)
2560 {
2561 /* Adjust the cursor position if it's in/after the changed
2562 * lines. */
2563 if (curwin->w_cursor.lnum >= hi)
2564 {
2565 curwin->w_cursor.lnum += extra;
2566 check_cursor_col();
2567 }
2568 else if (extra < 0)
2569 {
2570 curwin->w_cursor.lnum = lo;
2571 check_cursor();
2572 }
2573 changed_cline_bef_curs();
2574 }
2575 invalidate_botline();
2576}
2577
2578static Vim_Prim prims[]=
2579{
2580 /*
2581 * Buffer-related commands
2582 */
2583 {get_buffer_line, "get-buff-line", 1, 2},
2584 {set_buffer_line, "set-buff-line", 2, 3},
2585 {get_buffer_line_list, "get-buff-line-list", 2, 3},
2586 {get_buffer_name, "get-buff-name", 0, 1},
2587 {get_buffer_num, "get-buff-num", 0, 1},
2588 {get_buffer_size, "get-buff-size", 0, 1},
2589 {set_buffer_line_list, "set-buff-line-list", 3, 4},
2590 {insert_buffer_line_list, "insert-buff-line-list", 2, 3},
2591 {get_curr_buffer, "curr-buff", 0, 0},
2592 {get_buffer_count, "buff-count", 0, 0},
2593 {get_next_buffer, "get-next-buff", 0, 1},
2594 {get_prev_buffer, "get-prev-buff", 0, 1},
2595 {mzscheme_open_buffer, "open-buff", 1, 1},
2596 {get_buffer_by_name, "get-buff-by-name", 1, 1},
2597 {get_buffer_by_num, "get-buff-by-num", 1, 1},
2598 {get_buffer_namespace, "get-buff-namespace", 0, 1},
2599 /*
2600 * Window-related commands
2601 */
2602 {get_curr_win, "curr-win", 0, 0},
2603 {get_window_count, "win-count", 0, 0},
2604 {get_window_by_num, "get-win-by-num", 1, 1},
2605 {get_window_num, "get-win-num", 0, 1},
2606 {get_window_buffer, "get-win-buffer", 0, 1},
2607 {get_window_height, "get-win-height", 0, 1},
2608 {set_window_height, "set-win-height", 1, 2},
2609#ifdef FEAT_VERTSPLIT
2610 {get_window_width, "get-win-width", 0, 1},
2611 {set_window_width, "set-win-width", 1, 2},
2612#endif
2613 {get_cursor, "get-cursor", 0, 1},
2614 {set_cursor, "set-cursor", 1, 2},
2615 {get_window_list, "get-win-list", 0, 1},
2616 /*
2617 * Vim-related commands
2618 */
2619 {vim_command, "command", 1, 1},
2620 {vim_eval, "eval", 1, 1},
2621 {get_range_start, "range-start", 0, 0},
2622 {get_range_end, "range-end", 0, 0},
2623 {mzscheme_beep, "beep", 0, 0},
2624 {get_option, "get-option", 1, 2},
2625 {set_option, "set-option", 1, 2},
2626 /*
2627 * small utilities
2628 */
2629 {vim_bufferp, "buff?", 1, 1},
2630 {vim_windowp, "win?", 1, 1},
2631 {vim_buffer_validp, "buff-valid?", 1, 1},
2632 {vim_window_validp, "win-valid?", 1, 1}
2633};
2634
2635/* return MzScheme wrapper for curbuf */
2636 static vim_mz_buffer *
2637get_vim_curr_buffer(void)
2638{
Bram Moolenaare344bea2005-09-01 20:46:49 +00002639 if (curbuf->b_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002640 return (vim_mz_buffer *)buffer_new(curbuf);
2641 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00002642 return (vim_mz_buffer *)curbuf->b_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002643}
2644
2645/* return MzScheme wrapper for curwin */
2646 static vim_mz_window *
2647get_vim_curr_window(void)
2648{
Bram Moolenaare344bea2005-09-01 20:46:49 +00002649 if (curwin->w_mzscheme_ref == NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002650 return (vim_mz_window *)window_new(curwin);
2651 else
Bram Moolenaare344bea2005-09-01 20:46:49 +00002652 return (vim_mz_window *)curwin->w_mzscheme_ref;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002653}
2654
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002655 static void
2656make_modules(Scheme_Env *env)
2657{
2658 int i;
2659 Scheme_Env *mod;
2660
2661 mod = scheme_primitive_module(scheme_intern_symbol("vimext"), env);
2662 /* all prims made closed so they can access their own names */
2663 for (i = 0; i < sizeof(prims)/sizeof(prims[0]); i++)
2664 {
2665 Vim_Prim *prim = prims + i;
2666 scheme_add_global(prim->name,
2667 scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
2668 prim->mina, prim->maxa),
2669 mod);
2670 }
2671 scheme_add_global("global-namespace", (Scheme_Object *)environment, mod);
2672 scheme_finish_primitive_module(mod);
2673}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002674
Bram Moolenaar555b2802005-05-19 21:08:39 +00002675#ifdef HAVE_SANDBOX
2676static Scheme_Object *M_write = NULL;
2677static Scheme_Object *M_read = NULL;
2678static Scheme_Object *M_execute = NULL;
2679static Scheme_Object *M_delete = NULL;
2680
2681 static void
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00002682sandbox_check(void)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002683{
2684 if (sandbox)
2685 raise_vim_exn(_("not allowed in the Vim sandbox"));
2686}
2687
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002688/* security guards to force Vim's sandbox restrictions on MzScheme level */
Bram Moolenaar555b2802005-05-19 21:08:39 +00002689 static Scheme_Object *
2690sandbox_file_guard(int argc, Scheme_Object **argv)
2691{
2692 if (sandbox)
2693 {
2694 Scheme_Object *requested_access = argv[2];
2695
2696 if (M_write == NULL)
2697 {
2698 MZ_REGISTER_STATIC(M_write);
2699 M_write = scheme_intern_symbol("write");
2700 }
2701 if (M_read == NULL)
2702 {
2703 MZ_REGISTER_STATIC(M_read);
2704 M_read = scheme_intern_symbol("read");
2705 }
2706 if (M_execute == NULL)
2707 {
2708 MZ_REGISTER_STATIC(M_execute);
2709 M_execute = scheme_intern_symbol("execute");
2710 }
2711 if (M_delete == NULL)
2712 {
2713 MZ_REGISTER_STATIC(M_delete);
2714 M_delete = scheme_intern_symbol("delete");
2715 }
2716
2717 while (!SCHEME_NULLP(requested_access))
2718 {
2719 Scheme_Object *item = SCHEME_CAR(requested_access);
2720 if (scheme_eq(item, M_write) || scheme_eq(item, M_read)
2721 || scheme_eq(item, M_execute) || scheme_eq(item, M_delete))
2722 {
2723 raise_vim_exn(_("not allowed in the Vim sandbox"));
2724 }
2725 requested_access = SCHEME_CDR(requested_access);
2726 }
2727 }
2728 return scheme_void;
2729}
2730
2731 static Scheme_Object *
2732sandbox_network_guard(int argc, Scheme_Object **argv)
2733{
2734 return scheme_void;
2735}
2736#endif
Bram Moolenaar76b92b22006-03-24 22:46:53 +00002737
2738#endif