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