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