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