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