0
|
1 /* vi:set ts=8 sts=4 sw=4:
|
|
2 *
|
|
3 * VIM - Vi IMproved by Bram Moolenaar
|
|
4 *
|
|
5 * Ruby interface by Shugo Maeda
|
|
6 * with improvements by SegPhault (Ryan Paul)
|
39
|
7 * with improvements by Jon Maken
|
0
|
8 *
|
|
9 * Do ":help uganda" in Vim to read copying and usage conditions.
|
|
10 * Do ":help credits" in Vim to see a list of people who contributed.
|
|
11 * See README.txt for an overview of the Vim source code.
|
|
12 */
|
|
13
|
|
14 #include <stdio.h>
|
|
15 #include <string.h>
|
|
16
|
39
|
17 #ifdef HAVE_CONFIG_H
|
|
18 # include "auto/config.h"
|
|
19 #endif
|
|
20
|
0
|
21 #ifdef _WIN32
|
|
22 # if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
|
|
23 # define NT
|
|
24 # endif
|
|
25 # ifndef DYNAMIC_RUBY
|
|
26 # define IMPORT /* For static dll usage __declspec(dllimport) */
|
|
27 # define RUBYEXTERN __declspec(dllimport)
|
|
28 # endif
|
|
29 #endif
|
|
30 #ifndef RUBYEXTERN
|
|
31 # define RUBYEXTERN extern
|
|
32 #endif
|
|
33
|
39
|
34 #ifdef DYNAMIC_RUBY
|
0
|
35 /*
|
|
36 * This is tricky. In ruby.h there is (inline) function rb_class_of()
|
|
37 * definition. This function use these variables. But we want function to
|
|
38 * use dll_* variables.
|
|
39 */
|
|
40 # define rb_cFalseClass (*dll_rb_cFalseClass)
|
|
41 # define rb_cFixnum (*dll_rb_cFixnum)
|
|
42 # define rb_cNilClass (*dll_rb_cNilClass)
|
|
43 # define rb_cSymbol (*dll_rb_cSymbol)
|
|
44 # define rb_cTrueClass (*dll_rb_cTrueClass)
|
|
45 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
|
|
46 /*
|
39
|
47 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
|
|
48 * in ruby.h. But it causes trouble for these variables, because it is
|
0
|
49 * defined in this file. When defined this RUBY_EXPORT it modified to
|
|
50 * "extern" and be able to avoid this problem.
|
|
51 */
|
|
52 # define RUBY_EXPORT
|
|
53 # endif
|
39
|
54
|
|
55 #if !(defined(WIN32) || defined(_WIN64))
|
|
56 # include <dlfcn.h>
|
|
57 # define HINSTANCE void*
|
|
58 # define RUBY_PROC void*
|
|
59 # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
|
|
60 # define symbol_from_dll dlsym
|
|
61 # define close_dll dlclose
|
|
62 #else
|
|
63 # define RUBY_PROC FARPROC
|
|
64 # define load_dll vimLoadLib
|
|
65 # define symbol_from_dll GetProcAddress
|
|
66 # define close_dll FreeLibrary
|
|
67 #endif
|
|
68
|
|
69 #endif /* ifdef DYNAMIC_RUBY */
|
|
70
|
|
71 /* suggested by Ariya Mizutani */
|
|
72 #if (_MSC_VER == 1200)
|
|
73 # undef _WIN32_WINNT
|
|
74 #endif
|
|
75
|
|
76 #if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
|
|
77 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
|
|
78 # define RUBY19_OR_LATER 1
|
|
79 #endif
|
|
80
|
|
81 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
|
|
82 /* Ruby 1.9 defines a number of static functions which use rb_num2long and
|
|
83 * rb_int2big */
|
|
84 # define rb_num2long rb_num2long_stub
|
|
85 # define rb_int2big rb_int2big_stub
|
0
|
86 #endif
|
|
87
|
|
88 #include <ruby.h>
|
39
|
89 #ifdef RUBY19_OR_LATER
|
|
90 # include <ruby/encoding.h>
|
|
91 #endif
|
0
|
92
|
|
93 #undef EXTERN
|
|
94 #undef _
|
|
95
|
|
96 /* T_DATA defined both by Ruby and Mac header files, hack around it... */
|
|
97 #if defined(MACOS_X_UNIX) || defined(macintosh)
|
|
98 # define __OPENTRANSPORT__
|
|
99 # define __OPENTRANSPORTPROTOCOL__
|
|
100 # define __OPENTRANSPORTPROVIDERS__
|
|
101 #endif
|
|
102
|
39
|
103 /*
|
|
104 * Backward compatiblity for Ruby 1.8 and earlier.
|
|
105 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
|
|
106 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
|
|
107 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
|
|
108 */
|
|
109 #ifndef StringValuePtr
|
|
110 # define StringValuePtr(s) STR2CSTR(s)
|
|
111 #endif
|
|
112 #ifndef RARRAY_LEN
|
|
113 # define RARRAY_LEN(s) RARRAY(s)->len
|
|
114 #endif
|
|
115 #ifndef RARRAY_PTR
|
|
116 # define RARRAY_PTR(s) RARRAY(s)->ptr
|
|
117 #endif
|
|
118 #ifndef RSTRING_LEN
|
|
119 # define RSTRING_LEN(s) RSTRING(s)->len
|
|
120 #endif
|
|
121 #ifndef RSTRING_PTR
|
|
122 # define RSTRING_PTR(s) RSTRING(s)->ptr
|
|
123 #endif
|
|
124
|
0
|
125 #include "vim.h"
|
|
126 #include "version.h"
|
|
127
|
|
128 #if defined(PROTO) && !defined(FEAT_RUBY)
|
|
129 /* Define these to be able to generate the function prototypes. */
|
|
130 # define VALUE int
|
|
131 # define RUBY_DATA_FUNC int
|
|
132 #endif
|
|
133
|
|
134 static int ruby_initialized = 0;
|
|
135 static VALUE objtbl;
|
|
136
|
|
137 static VALUE mVIM;
|
|
138 static VALUE cBuffer;
|
|
139 static VALUE cVimWindow;
|
|
140 static VALUE eDeletedBufferError;
|
|
141 static VALUE eDeletedWindowError;
|
|
142
|
|
143 static int ensure_ruby_initialized(void);
|
|
144 static void error_print(int);
|
|
145 static void ruby_io_init(void);
|
|
146 static void ruby_vim_init(void);
|
|
147
|
|
148 #if defined(DYNAMIC_RUBY) || defined(PROTO)
|
|
149 #ifdef PROTO
|
|
150 # define HINSTANCE int /* for generating prototypes */
|
|
151 #endif
|
|
152
|
|
153 /*
|
|
154 * Wrapper defines
|
|
155 */
|
|
156 #define rb_assoc_new dll_rb_assoc_new
|
|
157 #define rb_cObject (*dll_rb_cObject)
|
|
158 #define rb_check_type dll_rb_check_type
|
|
159 #define rb_class_path dll_rb_class_path
|
|
160 #define rb_data_object_alloc dll_rb_data_object_alloc
|
|
161 #define rb_define_class_under dll_rb_define_class_under
|
|
162 #define rb_define_const dll_rb_define_const
|
|
163 #define rb_define_global_function dll_rb_define_global_function
|
|
164 #define rb_define_method dll_rb_define_method
|
|
165 #define rb_define_module dll_rb_define_module
|
|
166 #define rb_define_module_function dll_rb_define_module_function
|
|
167 #define rb_define_singleton_method dll_rb_define_singleton_method
|
|
168 #define rb_define_virtual_variable dll_rb_define_virtual_variable
|
|
169 #define rb_stdout (*dll_rb_stdout)
|
|
170 #define rb_eArgError (*dll_rb_eArgError)
|
|
171 #define rb_eIndexError (*dll_rb_eIndexError)
|
|
172 #define rb_eRuntimeError (*dll_rb_eRuntimeError)
|
|
173 #define rb_eStandardError (*dll_rb_eStandardError)
|
|
174 #define rb_eval_string_protect dll_rb_eval_string_protect
|
|
175 #define rb_global_variable dll_rb_global_variable
|
|
176 #define rb_hash_aset dll_rb_hash_aset
|
|
177 #define rb_hash_new dll_rb_hash_new
|
|
178 #define rb_inspect dll_rb_inspect
|
|
179 #define rb_int2inum dll_rb_int2inum
|
|
180 #define rb_lastline_get dll_rb_lastline_get
|
|
181 #define rb_lastline_set dll_rb_lastline_set
|
|
182 #define rb_load_protect dll_rb_load_protect
|
39
|
183 #ifndef RUBY19_OR_LATER
|
0
|
184 #define rb_num2long dll_rb_num2long
|
39
|
185 #endif
|
0
|
186 #define rb_num2ulong dll_rb_num2ulong
|
|
187 #define rb_obj_alloc dll_rb_obj_alloc
|
|
188 #define rb_obj_as_string dll_rb_obj_as_string
|
|
189 #define rb_obj_id dll_rb_obj_id
|
|
190 #define rb_raise dll_rb_raise
|
|
191 #define rb_str_cat dll_rb_str_cat
|
|
192 #define rb_str_concat dll_rb_str_concat
|
|
193 #define rb_str_new dll_rb_str_new
|
39
|
194 #ifdef rb_str_new2
|
|
195 /* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
|
|
196 # define need_rb_str_new_cstr 1
|
|
197 /* Ruby's headers #define rb_str_new_cstr to make use of GCC's
|
|
198 * __builtin_constant_p extension. */
|
|
199 # undef rb_str_new_cstr
|
|
200 # define rb_str_new_cstr dll_rb_str_new_cstr
|
|
201 #else
|
|
202 # define rb_str_new2 dll_rb_str_new2
|
|
203 #endif
|
|
204 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
|
|
205 # define rb_string_value dll_rb_string_value
|
|
206 # define rb_string_value_ptr dll_rb_string_value_ptr
|
|
207 # define rb_float_new dll_rb_float_new
|
|
208 # define rb_ary_new dll_rb_ary_new
|
|
209 # define rb_ary_push dll_rb_ary_push
|
|
210 #else
|
|
211 # define rb_str2cstr dll_rb_str2cstr
|
|
212 #endif
|
|
213 #ifdef RUBY19_OR_LATER
|
|
214 # define rb_errinfo dll_rb_errinfo
|
|
215 #else
|
|
216 # define ruby_errinfo (*dll_ruby_errinfo)
|
|
217 #endif
|
0
|
218 #define ruby_init dll_ruby_init
|
|
219 #define ruby_init_loadpath dll_ruby_init_loadpath
|
39
|
220 #ifdef WIN3264
|
|
221 # define NtInitialize dll_NtInitialize
|
|
222 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
|
|
223 # define rb_w32_snprintf dll_rb_w32_snprintf
|
|
224 # endif
|
|
225 #endif
|
|
226
|
|
227 #ifdef RUBY19_OR_LATER
|
|
228 # define ruby_script dll_ruby_script
|
|
229 # define rb_enc_find_index dll_rb_enc_find_index
|
|
230 # define rb_enc_find dll_rb_enc_find
|
|
231 # define rb_enc_str_new dll_rb_enc_str_new
|
|
232 # define rb_intern2 dll_rb_intern2
|
|
233 # define rb_const_remove dll_rb_const_remove
|
|
234 # define rb_sprintf dll_rb_sprintf
|
|
235 # define ruby_init_stack dll_ruby_init_stack
|
0
|
236 #endif
|
|
237
|
|
238 /*
|
|
239 * Pointers for dynamic link
|
|
240 */
|
|
241 static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
|
39
|
242 VALUE *dll_rb_cFalseClass;
|
|
243 VALUE *dll_rb_cFixnum;
|
|
244 VALUE *dll_rb_cNilClass;
|
0
|
245 static VALUE *dll_rb_cObject;
|
39
|
246 VALUE *dll_rb_cSymbol;
|
|
247 VALUE *dll_rb_cTrueClass;
|
0
|
248 static void (*dll_rb_check_type) (VALUE,int);
|
|
249 static VALUE (*dll_rb_class_path) (VALUE);
|
|
250 static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
|
|
251 static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
|
|
252 static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
|
|
253 static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
|
|
254 static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
|
|
255 static VALUE (*dll_rb_define_module) (const char*);
|
|
256 static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
|
|
257 static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
|
|
258 static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
|
|
259 static VALUE *dll_rb_stdout;
|
|
260 static VALUE *dll_rb_eArgError;
|
|
261 static VALUE *dll_rb_eIndexError;
|
|
262 static VALUE *dll_rb_eRuntimeError;
|
|
263 static VALUE *dll_rb_eStandardError;
|
|
264 static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
|
|
265 static void (*dll_rb_global_variable) (VALUE*);
|
|
266 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
|
|
267 static VALUE (*dll_rb_hash_new) (void);
|
|
268 static VALUE (*dll_rb_inspect) (VALUE);
|
|
269 static VALUE (*dll_rb_int2inum) (long);
|
|
270 static VALUE (*dll_rb_int2inum) (long);
|
|
271 static VALUE (*dll_rb_lastline_get) (void);
|
|
272 static void (*dll_rb_lastline_set) (VALUE);
|
|
273 static void (*dll_rb_load_protect) (VALUE, int, int*);
|
|
274 static long (*dll_rb_num2long) (VALUE);
|
|
275 static unsigned long (*dll_rb_num2ulong) (VALUE);
|
|
276 static VALUE (*dll_rb_obj_alloc) (VALUE);
|
|
277 static VALUE (*dll_rb_obj_as_string) (VALUE);
|
|
278 static VALUE (*dll_rb_obj_id) (VALUE);
|
|
279 static void (*dll_rb_raise) (VALUE, const char*, ...);
|
39
|
280 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
|
|
281 static VALUE (*dll_rb_string_value) (volatile VALUE*);
|
|
282 #else
|
0
|
283 static char *(*dll_rb_str2cstr) (VALUE,int*);
|
39
|
284 #endif
|
0
|
285 static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
|
|
286 static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
|
|
287 static VALUE (*dll_rb_str_new) (const char*, long);
|
39
|
288 #ifdef need_rb_str_new_cstr
|
|
289 /* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
|
|
290 static VALUE (*dll_rb_str_new_cstr) (const char*);
|
|
291 #else
|
0
|
292 static VALUE (*dll_rb_str_new2) (const char*);
|
39
|
293 #endif
|
|
294 #ifdef RUBY19_OR_LATER
|
|
295 static VALUE (*dll_rb_errinfo) (void);
|
|
296 #else
|
0
|
297 static VALUE *dll_ruby_errinfo;
|
39
|
298 #endif
|
0
|
299 static void (*dll_ruby_init) (void);
|
|
300 static void (*dll_ruby_init_loadpath) (void);
|
39
|
301 #ifdef WIN3264
|
|
302 static void (*dll_NtInitialize) (int*, char***);
|
|
303 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
|
|
304 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
|
|
305 # endif
|
|
306 #endif
|
0
|
307 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
|
39
|
308 static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
|
|
309 static VALUE (*dll_rb_float_new) (double);
|
|
310 static VALUE (*dll_rb_ary_new) (void);
|
|
311 static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
|
|
312 #endif
|
|
313 #ifdef RUBY19_OR_LATER
|
|
314 static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
|
0
|
315 #endif
|
|
316
|
39
|
317 #ifdef RUBY19_OR_LATER
|
|
318 static void (*dll_ruby_script) (const char*);
|
|
319 static int (*dll_rb_enc_find_index) (const char*);
|
|
320 static rb_encoding* (*dll_rb_enc_find) (const char*);
|
|
321 static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
|
|
322 static ID (*dll_rb_intern2) (const char*, long);
|
|
323 static void (*dll_Init_prelude) (void);
|
|
324 static VALUE (*dll_rb_const_remove) (VALUE, ID);
|
|
325 static VALUE (*dll_rb_sprintf) (const char*, ...);
|
|
326 static void (*ruby_init_stack)(VALUE*);
|
|
327 #endif
|
|
328
|
|
329 #ifdef RUBY19_OR_LATER
|
|
330 SIGNED_VALUE rb_num2long_stub(VALUE x)
|
|
331 {
|
|
332 return dll_rb_num2long(x);
|
|
333 }
|
|
334 VALUE rb_int2big_stub(SIGNED_VALUE x)
|
|
335 {
|
|
336 return dll_rb_int2big(x);
|
|
337 }
|
|
338 #endif
|
|
339
|
|
340 static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
|
0
|
341
|
|
342 /*
|
|
343 * Table of name to function pointer of ruby.
|
|
344 */
|
|
345 static struct
|
|
346 {
|
|
347 char *name;
|
|
348 RUBY_PROC *ptr;
|
|
349 } ruby_funcname_table[] =
|
|
350 {
|
|
351 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
|
|
352 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
|
|
353 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
|
|
354 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
|
|
355 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
|
|
356 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
|
|
357 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
|
|
358 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
|
|
359 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
|
|
360 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
|
|
361 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
|
|
362 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
|
|
363 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
|
|
364 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
|
|
365 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
|
|
366 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
|
|
367 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
|
|
368 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
|
|
369 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
|
|
370 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
|
|
371 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
|
|
372 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
|
|
373 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
|
|
374 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
|
|
375 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
|
|
376 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
|
|
377 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
|
|
378 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
|
|
379 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
|
|
380 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
|
|
381 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
|
|
382 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
|
|
383 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
|
|
384 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
|
|
385 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
|
|
386 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
|
|
387 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
|
|
388 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
|
39
|
389 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
|
|
390 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
|
|
391 #else
|
0
|
392 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
|
39
|
393 #endif
|
0
|
394 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
|
|
395 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
|
|
396 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
|
39
|
397 #ifdef need_rb_str_new_cstr
|
|
398 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
|
|
399 #else
|
0
|
400 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
|
39
|
401 #endif
|
|
402 #ifdef RUBY19_OR_LATER
|
|
403 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
|
|
404 #else
|
0
|
405 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
|
39
|
406 #endif
|
0
|
407 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
|
|
408 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
|
39
|
409 #ifdef WIN3264
|
|
410 {
|
|
411 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
|
|
412 "NtInitialize",
|
|
413 # else
|
|
414 "ruby_sysinit",
|
|
415 # endif
|
|
416 (RUBY_PROC*)&dll_NtInitialize},
|
|
417 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
|
|
418 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
|
|
419 # endif
|
|
420 #endif
|
0
|
421 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
|
39
|
422 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
|
|
423 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
|
|
424 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
|
|
425 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
|
|
426 #endif
|
|
427 #ifdef RUBY19_OR_LATER
|
|
428 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
|
|
429 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
|
|
430 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
|
|
431 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
|
|
432 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
|
|
433 {"rb_intern2", (RUBY_PROC*)&dll_rb_intern2},
|
|
434 {"rb_const_remove", (RUBY_PROC*)&dll_rb_const_remove},
|
|
435 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
|
|
436 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
|
0
|
437 #endif
|
|
438 {"", NULL},
|
|
439 };
|
|
440
|
|
441 /*
|
|
442 * Free ruby.dll
|
|
443 */
|
|
444 static void
|
|
445 end_dynamic_ruby()
|
|
446 {
|
|
447 if (hinstRuby)
|
|
448 {
|
39
|
449 close_dll(hinstRuby);
|
|
450 hinstRuby = NULL;
|
0
|
451 }
|
|
452 }
|
|
453
|
|
454 /*
|
|
455 * Load library and get all pointers.
|
|
456 * Parameter 'libname' provides name of DLL.
|
|
457 * Return OK or FAIL.
|
|
458 */
|
|
459 static int
|
|
460 ruby_runtime_link_init(char *libname, int verbose)
|
|
461 {
|
|
462 int i;
|
|
463
|
|
464 if (hinstRuby)
|
|
465 return OK;
|
39
|
466 hinstRuby = load_dll(libname);
|
0
|
467 if (!hinstRuby)
|
|
468 {
|
|
469 if (verbose)
|
|
470 EMSG2(_(e_loadlib), libname);
|
|
471 return FAIL;
|
|
472 }
|
|
473
|
|
474 for (i = 0; ruby_funcname_table[i].ptr; ++i)
|
|
475 {
|
39
|
476 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
|
0
|
477 ruby_funcname_table[i].name)))
|
|
478 {
|
39
|
479 close_dll(hinstRuby);
|
|
480 hinstRuby = NULL;
|
0
|
481 if (verbose)
|
|
482 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
|
|
483 return FAIL;
|
|
484 }
|
|
485 }
|
|
486 return OK;
|
|
487 }
|
|
488
|
|
489 /*
|
|
490 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
|
|
491 * else FALSE.
|
|
492 */
|
|
493 int
|
|
494 ruby_enabled(verbose)
|
|
495 int verbose;
|
|
496 {
|
|
497 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
|
|
498 }
|
|
499 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
|
|
500
|
|
501 void
|
|
502 ruby_end()
|
|
503 {
|
|
504 #ifdef DYNAMIC_RUBY
|
|
505 end_dynamic_ruby();
|
|
506 #endif
|
|
507 }
|
|
508
|
|
509 void ex_ruby(exarg_T *eap)
|
|
510 {
|
|
511 int state;
|
|
512 char *script = NULL;
|
|
513
|
|
514 script = (char *)script_get(eap, eap->arg);
|
|
515 if (!eap->skip && ensure_ruby_initialized())
|
|
516 {
|
|
517 if (script == NULL)
|
|
518 rb_eval_string_protect((char *)eap->arg, &state);
|
|
519 else
|
|
520 rb_eval_string_protect(script, &state);
|
|
521 if (state)
|
|
522 error_print(state);
|
|
523 }
|
|
524 vim_free(script);
|
|
525 }
|
|
526
|
39
|
527 /*
|
|
528 * In Ruby 1.9 or later, ruby String object has encoding.
|
|
529 * conversion buffer string of vim to ruby String object using
|
|
530 * VIM encoding option.
|
|
531 */
|
|
532 static VALUE
|
|
533 vim_str2rb_enc_str(const char *s)
|
|
534 {
|
|
535 #ifdef RUBY19_OR_LATER
|
|
536 int isnum;
|
|
537 long lval;
|
|
538 char_u *sval;
|
|
539 rb_encoding *enc;
|
|
540
|
|
541 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
|
|
542 if (isnum == 0)
|
|
543 {
|
|
544 enc = rb_enc_find((char *)sval);
|
|
545 vim_free(sval);
|
|
546 if (enc) {
|
|
547 return rb_enc_str_new(s, strlen(s), enc);
|
|
548 }
|
|
549 }
|
|
550 #endif
|
|
551 return rb_str_new2(s);
|
|
552 }
|
|
553
|
|
554 static VALUE
|
|
555 eval_enc_string_protect(const char *str, int *state)
|
|
556 {
|
|
557 #ifdef RUBY19_OR_LATER
|
|
558 int isnum;
|
|
559 long lval;
|
|
560 char_u *sval;
|
|
561 rb_encoding *enc;
|
|
562 VALUE v;
|
|
563
|
|
564 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
|
|
565 if (isnum == 0)
|
|
566 {
|
|
567 enc = rb_enc_find((char *)sval);
|
|
568 vim_free(sval);
|
|
569 if (enc)
|
|
570 {
|
|
571 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
|
|
572 return rb_eval_string_protect(StringValuePtr(v), state);
|
|
573 }
|
|
574 }
|
|
575 #endif
|
|
576 return rb_eval_string_protect(str, state);
|
|
577 }
|
|
578
|
0
|
579 void ex_rubydo(exarg_T *eap)
|
|
580 {
|
|
581 int state;
|
|
582 linenr_T i;
|
|
583
|
|
584 if (ensure_ruby_initialized())
|
|
585 {
|
|
586 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
|
|
587 return;
|
|
588 for (i = eap->line1; i <= eap->line2; i++) {
|
39
|
589 VALUE line;
|
0
|
590
|
39
|
591 line = vim_str2rb_enc_str((char *)ml_get(i));
|
0
|
592 rb_lastline_set(line);
|
39
|
593 eval_enc_string_protect((char *) eap->arg, &state);
|
0
|
594 if (state) {
|
|
595 error_print(state);
|
|
596 break;
|
|
597 }
|
|
598 line = rb_lastline_get();
|
|
599 if (!NIL_P(line)) {
|
|
600 if (TYPE(line) != T_STRING) {
|
|
601 EMSG(_("E265: $_ must be an instance of String"));
|
|
602 return;
|
|
603 }
|
39
|
604 ml_replace(i, (char_u *) StringValuePtr(line), 1);
|
0
|
605 changed();
|
|
606 #ifdef SYNTAX_HL
|
|
607 syn_changed(i); /* recompute syntax hl. for this line */
|
|
608 #endif
|
|
609 }
|
|
610 }
|
|
611 check_cursor();
|
|
612 update_curbuf(NOT_VALID);
|
|
613 }
|
|
614 }
|
|
615
|
|
616 void ex_rubyfile(exarg_T *eap)
|
|
617 {
|
|
618 int state;
|
|
619
|
|
620 if (ensure_ruby_initialized())
|
|
621 {
|
|
622 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
|
|
623 if (state) error_print(state);
|
|
624 }
|
|
625 }
|
|
626
|
|
627 void ruby_buffer_free(buf_T *buf)
|
|
628 {
|
|
629 if (buf->b_ruby_ref)
|
|
630 {
|
|
631 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
|
|
632 RDATA(buf->b_ruby_ref)->data = NULL;
|
|
633 }
|
|
634 }
|
|
635
|
|
636 void ruby_window_free(win_T *win)
|
|
637 {
|
|
638 if (win->w_ruby_ref)
|
|
639 {
|
|
640 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
|
|
641 RDATA(win->w_ruby_ref)->data = NULL;
|
|
642 }
|
|
643 }
|
|
644
|
|
645 static int ensure_ruby_initialized(void)
|
|
646 {
|
|
647 if (!ruby_initialized)
|
|
648 {
|
|
649 #ifdef DYNAMIC_RUBY
|
|
650 if (ruby_enabled(TRUE))
|
|
651 {
|
|
652 #endif
|
39
|
653 #ifdef _WIN32
|
|
654 /* suggested by Ariya Mizutani */
|
|
655 int argc = 1;
|
|
656 char *argv[] = {"gvim.exe"};
|
|
657 NtInitialize(&argc, &argv);
|
|
658 #endif
|
|
659 {
|
|
660 #ifdef RUBY19_OR_LATER
|
|
661 RUBY_INIT_STACK;
|
|
662 #endif
|
|
663 ruby_init();
|
|
664 }
|
|
665 #ifdef RUBY19_OR_LATER
|
|
666 ruby_script("vim-ruby");
|
|
667 #endif
|
0
|
668 ruby_init_loadpath();
|
|
669 ruby_io_init();
|
39
|
670 #ifdef RUBY19_OR_LATER
|
|
671 rb_enc_find_index("encdb");
|
|
672
|
|
673 /* This avoids the error "Encoding::ConverterNotFoundError: code
|
|
674 * converter not found (UTF-16LE to ASCII-8BIT)". */
|
|
675 rb_define_module("Gem");
|
|
676 rb_const_remove(rb_cObject, rb_intern2("TMP_RUBY_PREFIX", 15));
|
|
677 #endif
|
0
|
678 ruby_vim_init();
|
|
679 ruby_initialized = 1;
|
|
680 #ifdef DYNAMIC_RUBY
|
|
681 }
|
|
682 else
|
|
683 {
|
|
684 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
|
|
685 return 0;
|
|
686 }
|
|
687 #endif
|
|
688 }
|
|
689 return ruby_initialized;
|
|
690 }
|
|
691
|
|
692 static void error_print(int state)
|
|
693 {
|
|
694 #ifndef DYNAMIC_RUBY
|
39
|
695 #if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
|
|
696 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
|
0
|
697 RUBYEXTERN VALUE ruby_errinfo;
|
|
698 #endif
|
39
|
699 #endif
|
0
|
700 VALUE eclass;
|
|
701 VALUE einfo;
|
|
702 char buff[BUFSIZ];
|
|
703
|
|
704 #define TAG_RETURN 0x1
|
|
705 #define TAG_BREAK 0x2
|
|
706 #define TAG_NEXT 0x3
|
|
707 #define TAG_RETRY 0x4
|
|
708 #define TAG_REDO 0x5
|
|
709 #define TAG_RAISE 0x6
|
|
710 #define TAG_THROW 0x7
|
|
711 #define TAG_FATAL 0x8
|
|
712 #define TAG_MASK 0xf
|
|
713
|
|
714 switch (state) {
|
|
715 case TAG_RETURN:
|
|
716 EMSG(_("E267: unexpected return"));
|
|
717 break;
|
|
718 case TAG_NEXT:
|
|
719 EMSG(_("E268: unexpected next"));
|
|
720 break;
|
|
721 case TAG_BREAK:
|
|
722 EMSG(_("E269: unexpected break"));
|
|
723 break;
|
|
724 case TAG_REDO:
|
|
725 EMSG(_("E270: unexpected redo"));
|
|
726 break;
|
|
727 case TAG_RETRY:
|
|
728 EMSG(_("E271: retry outside of rescue clause"));
|
|
729 break;
|
|
730 case TAG_RAISE:
|
|
731 case TAG_FATAL:
|
39
|
732 #ifdef RUBY19_OR_LATER
|
|
733 eclass = CLASS_OF(rb_errinfo());
|
|
734 einfo = rb_obj_as_string(rb_errinfo());
|
|
735 #else
|
0
|
736 eclass = CLASS_OF(ruby_errinfo);
|
|
737 einfo = rb_obj_as_string(ruby_errinfo);
|
39
|
738 #endif
|
|
739 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
|
0
|
740 EMSG(_("E272: unhandled exception"));
|
|
741 }
|
|
742 else {
|
|
743 VALUE epath;
|
|
744 char *p;
|
|
745
|
|
746 epath = rb_class_path(eclass);
|
|
747 vim_snprintf(buff, BUFSIZ, "%s: %s",
|
39
|
748 RSTRING_PTR(epath), RSTRING_PTR(einfo));
|
0
|
749 p = strchr(buff, '\n');
|
|
750 if (p) *p = '\0';
|
|
751 EMSG(buff);
|
|
752 }
|
|
753 break;
|
|
754 default:
|
|
755 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
|
|
756 EMSG(buff);
|
|
757 break;
|
|
758 }
|
|
759 }
|
|
760
|
39
|
761 static VALUE vim_message(VALUE self UNUSED, VALUE str)
|
0
|
762 {
|
|
763 char *buff, *p;
|
|
764
|
|
765 str = rb_obj_as_string(str);
|
39
|
766 buff = ALLOCA_N(char, RSTRING_LEN(str));
|
|
767 strcpy(buff, RSTRING_PTR(str));
|
0
|
768 p = strchr(buff, '\n');
|
|
769 if (p) *p = '\0';
|
|
770 MSG(buff);
|
|
771 return Qnil;
|
|
772 }
|
|
773
|
39
|
774 static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
|
0
|
775 {
|
39
|
776 do_set((char_u *)StringValuePtr(str), 0);
|
0
|
777 update_screen(NOT_VALID);
|
|
778 return Qnil;
|
|
779 }
|
|
780
|
39
|
781 static VALUE vim_command(VALUE self UNUSED, VALUE str)
|
0
|
782 {
|
39
|
783 do_cmdline_cmd((char_u *)StringValuePtr(str));
|
0
|
784 return Qnil;
|
|
785 }
|
|
786
|
39
|
787 #ifdef FEAT_EVAL
|
|
788 static VALUE vim_to_ruby(typval_T *tv)
|
|
789 {
|
|
790 VALUE result = Qnil;
|
|
791
|
|
792 if (tv->v_type == VAR_STRING)
|
|
793 {
|
|
794 result = rb_str_new2(tv->vval.v_string == NULL
|
|
795 ? "" : (char *)(tv->vval.v_string));
|
|
796 }
|
|
797 else if (tv->v_type == VAR_NUMBER)
|
|
798 {
|
|
799 result = INT2NUM(tv->vval.v_number);
|
|
800 }
|
|
801 # ifdef FEAT_FLOAT
|
|
802 else if (tv->v_type == VAR_FLOAT)
|
|
803 {
|
|
804 result = rb_float_new(tv->vval.v_float);
|
|
805 }
|
|
806 # endif
|
|
807 else if (tv->v_type == VAR_LIST)
|
|
808 {
|
|
809 list_T *list = tv->vval.v_list;
|
|
810 listitem_T *curr;
|
|
811
|
|
812 result = rb_ary_new();
|
|
813
|
|
814 if (list != NULL)
|
|
815 {
|
|
816 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
|
|
817 {
|
|
818 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
|
|
819 }
|
|
820 }
|
|
821 }
|
|
822 else if (tv->v_type == VAR_DICT)
|
|
823 {
|
|
824 result = rb_hash_new();
|
|
825
|
|
826 if (tv->vval.v_dict != NULL)
|
|
827 {
|
|
828 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
|
|
829 long_u todo = ht->ht_used;
|
|
830 hashitem_T *hi;
|
|
831 dictitem_T *di;
|
|
832
|
|
833 for (hi = ht->ht_array; todo > 0; ++hi)
|
|
834 {
|
|
835 if (!HASHITEM_EMPTY(hi))
|
|
836 {
|
|
837 --todo;
|
|
838
|
|
839 di = dict_lookup(hi);
|
|
840 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
|
|
841 vim_to_ruby(&di->di_tv));
|
|
842 }
|
|
843 }
|
|
844 }
|
|
845 } /* else return Qnil; */
|
|
846
|
|
847 return result;
|
|
848 }
|
|
849 #endif
|
|
850
|
|
851 static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
|
0
|
852 {
|
|
853 #ifdef FEAT_EVAL
|
39
|
854 typval_T *tv;
|
|
855 VALUE result;
|
0
|
856
|
39
|
857 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
|
|
858 if (tv == NULL)
|
0
|
859 {
|
39
|
860 return Qnil;
|
0
|
861 }
|
39
|
862 result = vim_to_ruby(tv);
|
|
863
|
|
864 free_tv(tv);
|
|
865
|
|
866 return result;
|
|
867 #else
|
|
868 return Qnil;
|
0
|
869 #endif
|
|
870 }
|
|
871
|
|
872 static VALUE buffer_new(buf_T *buf)
|
|
873 {
|
|
874 if (buf->b_ruby_ref)
|
|
875 {
|
|
876 return (VALUE) buf->b_ruby_ref;
|
|
877 }
|
|
878 else
|
|
879 {
|
|
880 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
|
|
881 buf->b_ruby_ref = (void *) obj;
|
|
882 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
|
|
883 return obj;
|
|
884 }
|
|
885 }
|
|
886
|
|
887 static buf_T *get_buf(VALUE obj)
|
|
888 {
|
|
889 buf_T *buf;
|
|
890
|
|
891 Data_Get_Struct(obj, buf_T, buf);
|
|
892 if (buf == NULL)
|
|
893 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
|
|
894 return buf;
|
|
895 }
|
|
896
|
|
897 static VALUE buffer_s_current()
|
|
898 {
|
|
899 return buffer_new(curbuf);
|
|
900 }
|
|
901
|
|
902 static VALUE buffer_s_count()
|
|
903 {
|
|
904 buf_T *b;
|
|
905 int n = 0;
|
|
906
|
|
907 for (b = firstbuf; b != NULL; b = b->b_next)
|
|
908 {
|
|
909 /* Deleted buffers should not be counted
|
|
910 * SegPhault - 01/07/05 */
|
|
911 if (b->b_p_bl)
|
|
912 n++;
|
|
913 }
|
|
914
|
|
915 return INT2NUM(n);
|
|
916 }
|
|
917
|
39
|
918 static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
|
0
|
919 {
|
|
920 buf_T *b;
|
|
921 int n = NUM2INT(num);
|
|
922
|
|
923 for (b = firstbuf; b != NULL; b = b->b_next)
|
|
924 {
|
|
925 /* Deleted buffers should not be counted
|
|
926 * SegPhault - 01/07/05 */
|
|
927 if (!b->b_p_bl)
|
|
928 continue;
|
|
929
|
|
930 if (n == 0)
|
|
931 return buffer_new(b);
|
|
932
|
|
933 n--;
|
|
934 }
|
|
935 return Qnil;
|
|
936 }
|
|
937
|
|
938 static VALUE buffer_name(VALUE self)
|
|
939 {
|
|
940 buf_T *buf = get_buf(self);
|
|
941
|
|
942 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
|
|
943 }
|
|
944
|
|
945 static VALUE buffer_number(VALUE self)
|
|
946 {
|
|
947 buf_T *buf = get_buf(self);
|
|
948
|
|
949 return INT2NUM(buf->b_fnum);
|
|
950 }
|
|
951
|
|
952 static VALUE buffer_count(VALUE self)
|
|
953 {
|
|
954 buf_T *buf = get_buf(self);
|
|
955
|
|
956 return INT2NUM(buf->b_ml.ml_line_count);
|
|
957 }
|
|
958
|
|
959 static VALUE get_buffer_line(buf_T *buf, linenr_T n)
|
|
960 {
|
39
|
961 if (n <= 0 || n > buf->b_ml.ml_line_count)
|
|
962 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
|
|
963 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
|
0
|
964 }
|
|
965
|
|
966 static VALUE buffer_aref(VALUE self, VALUE num)
|
|
967 {
|
|
968 buf_T *buf = get_buf(self);
|
|
969
|
|
970 if (buf != NULL)
|
|
971 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
|
|
972 return Qnil; /* For stop warning */
|
|
973 }
|
|
974
|
|
975 static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
|
|
976 {
|
39
|
977 char *line = StringValuePtr(str);
|
0
|
978 aco_save_T aco;
|
|
979
|
|
980 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
|
|
981 {
|
|
982 /* set curwin/curbuf for "buf" and save some things */
|
|
983 aucmd_prepbuf(&aco, buf);
|
|
984
|
|
985 if (u_savesub(n) == OK) {
|
|
986 ml_replace(n, (char_u *)line, TRUE);
|
|
987 changed();
|
|
988 #ifdef SYNTAX_HL
|
|
989 syn_changed(n); /* recompute syntax hl. for this line */
|
|
990 #endif
|
|
991 }
|
|
992
|
|
993 /* restore curwin/curbuf and a few other things */
|
|
994 aucmd_restbuf(&aco);
|
|
995 /* Careful: autocommands may have made "buf" invalid! */
|
|
996
|
|
997 update_curbuf(NOT_VALID);
|
|
998 }
|
|
999 else
|
|
1000 {
|
39
|
1001 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
|
0
|
1002 }
|
|
1003 return str;
|
|
1004 }
|
|
1005
|
|
1006 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
|
|
1007 {
|
|
1008 buf_T *buf = get_buf(self);
|
|
1009
|
|
1010 if (buf != NULL)
|
|
1011 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
|
|
1012 return str;
|
|
1013 }
|
|
1014
|
|
1015 static VALUE buffer_delete(VALUE self, VALUE num)
|
|
1016 {
|
|
1017 buf_T *buf = get_buf(self);
|
|
1018 long n = NUM2LONG(num);
|
|
1019 aco_save_T aco;
|
|
1020
|
|
1021 if (n > 0 && n <= buf->b_ml.ml_line_count)
|
|
1022 {
|
|
1023 /* set curwin/curbuf for "buf" and save some things */
|
|
1024 aucmd_prepbuf(&aco, buf);
|
|
1025
|
|
1026 if (u_savedel(n, 1) == OK) {
|
|
1027 ml_delete(n, 0);
|
|
1028
|
|
1029 /* Changes to non-active buffers should properly refresh
|
|
1030 * SegPhault - 01/09/05 */
|
|
1031 deleted_lines_mark(n, 1L);
|
|
1032
|
|
1033 changed();
|
|
1034 }
|
|
1035
|
|
1036 /* restore curwin/curbuf and a few other things */
|
|
1037 aucmd_restbuf(&aco);
|
|
1038 /* Careful: autocommands may have made "buf" invalid! */
|
|
1039
|
|
1040 update_curbuf(NOT_VALID);
|
|
1041 }
|
|
1042 else
|
|
1043 {
|
39
|
1044 rb_raise(rb_eIndexError, "line number %ld out of range", n);
|
0
|
1045 }
|
|
1046 return Qnil;
|
|
1047 }
|
|
1048
|
|
1049 static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
|
|
1050 {
|
|
1051 buf_T *buf = get_buf(self);
|
39
|
1052 char *line = StringValuePtr(str);
|
0
|
1053 long n = NUM2LONG(num);
|
|
1054 aco_save_T aco;
|
|
1055
|
39
|
1056 if (line == NULL)
|
|
1057 {
|
|
1058 rb_raise(rb_eIndexError, "NULL line");
|
|
1059 }
|
|
1060 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
|
0
|
1061 {
|
|
1062 /* set curwin/curbuf for "buf" and save some things */
|
|
1063 aucmd_prepbuf(&aco, buf);
|
|
1064
|
|
1065 if (u_inssub(n + 1) == OK) {
|
|
1066 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
|
|
1067
|
|
1068 /* Changes to non-active buffers should properly refresh screen
|
|
1069 * SegPhault - 12/20/04 */
|
|
1070 appended_lines_mark(n, 1L);
|
|
1071
|
|
1072 changed();
|
|
1073 }
|
|
1074
|
|
1075 /* restore curwin/curbuf and a few other things */
|
|
1076 aucmd_restbuf(&aco);
|
|
1077 /* Careful: autocommands may have made "buf" invalid! */
|
|
1078
|
|
1079 update_curbuf(NOT_VALID);
|
|
1080 }
|
39
|
1081 else
|
|
1082 {
|
|
1083 rb_raise(rb_eIndexError, "line number %ld out of range", n);
|
0
|
1084 }
|
|
1085 return str;
|
|
1086 }
|
|
1087
|
|
1088 static VALUE window_new(win_T *win)
|
|
1089 {
|
|
1090 if (win->w_ruby_ref)
|
|
1091 {
|
|
1092 return (VALUE) win->w_ruby_ref;
|
|
1093 }
|
|
1094 else
|
|
1095 {
|
|
1096 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
|
|
1097 win->w_ruby_ref = (void *) obj;
|
|
1098 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
|
|
1099 return obj;
|
|
1100 }
|
|
1101 }
|
|
1102
|
|
1103 static win_T *get_win(VALUE obj)
|
|
1104 {
|
|
1105 win_T *win;
|
|
1106
|
|
1107 Data_Get_Struct(obj, win_T, win);
|
|
1108 if (win == NULL)
|
|
1109 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
|
|
1110 return win;
|
|
1111 }
|
|
1112
|
|
1113 static VALUE window_s_current()
|
|
1114 {
|
|
1115 return window_new(curwin);
|
|
1116 }
|
|
1117
|
|
1118 /*
|
|
1119 * Added line manipulation functions
|
|
1120 * SegPhault - 03/07/05
|
|
1121 */
|
|
1122 static VALUE line_s_current()
|
|
1123 {
|
|
1124 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
|
|
1125 }
|
|
1126
|
39
|
1127 static VALUE set_current_line(VALUE self UNUSED, VALUE str)
|
0
|
1128 {
|
|
1129 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
|
|
1130 }
|
|
1131
|
|
1132 static VALUE current_line_number()
|
|
1133 {
|
|
1134 return INT2FIX((int)curwin->w_cursor.lnum);
|
|
1135 }
|
|
1136
|
|
1137
|
|
1138
|
|
1139 static VALUE window_s_count()
|
|
1140 {
|
|
1141 #ifdef FEAT_WINDOWS
|
|
1142 win_T *w;
|
|
1143 int n = 0;
|
|
1144
|
|
1145 for (w = firstwin; w != NULL; w = w->w_next)
|
|
1146 n++;
|
|
1147 return INT2NUM(n);
|
|
1148 #else
|
|
1149 return INT2NUM(1);
|
|
1150 #endif
|
|
1151 }
|
|
1152
|
39
|
1153 static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
|
0
|
1154 {
|
|
1155 win_T *w;
|
|
1156 int n = NUM2INT(num);
|
|
1157
|
|
1158 #ifndef FEAT_WINDOWS
|
|
1159 w = curwin;
|
|
1160 #else
|
|
1161 for (w = firstwin; w != NULL; w = w->w_next, --n)
|
|
1162 #endif
|
|
1163 if (n == 0)
|
|
1164 return window_new(w);
|
|
1165 return Qnil;
|
|
1166 }
|
|
1167
|
|
1168 static VALUE window_buffer(VALUE self)
|
|
1169 {
|
|
1170 win_T *win = get_win(self);
|
|
1171
|
|
1172 return buffer_new(win->w_buffer);
|
|
1173 }
|
|
1174
|
|
1175 static VALUE window_height(VALUE self)
|
|
1176 {
|
|
1177 win_T *win = get_win(self);
|
|
1178
|
|
1179 return INT2NUM(win->w_height);
|
|
1180 }
|
|
1181
|
|
1182 static VALUE window_set_height(VALUE self, VALUE height)
|
|
1183 {
|
|
1184 win_T *win = get_win(self);
|
|
1185 win_T *savewin = curwin;
|
|
1186
|
|
1187 curwin = win;
|
|
1188 win_setheight(NUM2INT(height));
|
|
1189 curwin = savewin;
|
|
1190 return height;
|
|
1191 }
|
|
1192
|
|
1193 static VALUE window_width(VALUE self)
|
|
1194 {
|
|
1195 win_T *win = get_win(self);
|
|
1196
|
|
1197 return INT2NUM(win->w_width);
|
|
1198 }
|
|
1199
|
|
1200 static VALUE window_set_width(VALUE self, VALUE width)
|
|
1201 {
|
|
1202 win_T *win = get_win(self);
|
|
1203 win_T *savewin = curwin;
|
|
1204
|
|
1205 curwin = win;
|
|
1206 win_setwidth(NUM2INT(width));
|
|
1207 curwin = savewin;
|
|
1208 return width;
|
|
1209 }
|
|
1210
|
|
1211 static VALUE window_cursor(VALUE self)
|
|
1212 {
|
|
1213 win_T *win = get_win(self);
|
|
1214
|
|
1215 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
|
|
1216 }
|
|
1217
|
|
1218 static VALUE window_set_cursor(VALUE self, VALUE pos)
|
|
1219 {
|
|
1220 VALUE lnum, col;
|
|
1221 win_T *win = get_win(self);
|
|
1222
|
|
1223 Check_Type(pos, T_ARRAY);
|
39
|
1224 if (RARRAY_LEN(pos) != 2)
|
0
|
1225 rb_raise(rb_eArgError, "array length must be 2");
|
39
|
1226 lnum = RARRAY_PTR(pos)[0];
|
|
1227 col = RARRAY_PTR(pos)[1];
|
0
|
1228 win->w_cursor.lnum = NUM2LONG(lnum);
|
|
1229 win->w_cursor.col = NUM2UINT(col);
|
|
1230 check_cursor(); /* put cursor on an existing line */
|
|
1231 update_screen(NOT_VALID);
|
|
1232 return Qnil;
|
|
1233 }
|
|
1234
|
39
|
1235 static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
|
0
|
1236 {
|
|
1237 int i;
|
|
1238 VALUE str = rb_str_new("", 0);
|
|
1239
|
|
1240 for (i = 0; i < argc; i++) {
|
|
1241 if (i > 0) rb_str_cat(str, ", ", 2);
|
|
1242 rb_str_concat(str, rb_inspect(argv[i]));
|
|
1243 }
|
39
|
1244 MSG(RSTRING_PTR(str));
|
0
|
1245 return Qnil;
|
|
1246 }
|
|
1247
|
|
1248 static void ruby_io_init(void)
|
|
1249 {
|
|
1250 #ifndef DYNAMIC_RUBY
|
|
1251 RUBYEXTERN VALUE rb_stdout;
|
|
1252 #endif
|
|
1253
|
|
1254 rb_stdout = rb_obj_alloc(rb_cObject);
|
|
1255 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
|
|
1256 rb_define_global_function("p", f_p, -1);
|
|
1257 }
|
|
1258
|
|
1259 static void ruby_vim_init(void)
|
|
1260 {
|
|
1261 objtbl = rb_hash_new();
|
|
1262 rb_global_variable(&objtbl);
|
|
1263
|
|
1264 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
|
|
1265 * alias "VIM" for backwards compatiblity. */
|
|
1266 mVIM = rb_define_module("Vim");
|
|
1267 rb_define_const(rb_cObject, "VIM", mVIM);
|
|
1268 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
|
|
1269 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
|
|
1270 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
|
|
1271 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
|
|
1272 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
|
|
1273 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
|
|
1274 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
|
|
1275 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
|
|
1276 rb_define_module_function(mVIM, "message", vim_message, 1);
|
|
1277 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
|
|
1278 rb_define_module_function(mVIM, "command", vim_command, 1);
|
|
1279 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
|
|
1280
|
|
1281 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
|
|
1282 rb_eStandardError);
|
|
1283 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
|
|
1284 rb_eStandardError);
|
|
1285
|
|
1286 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
|
|
1287 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
|
|
1288 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
|
|
1289 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
|
|
1290 rb_define_method(cBuffer, "name", buffer_name, 0);
|
|
1291 rb_define_method(cBuffer, "number", buffer_number, 0);
|
|
1292 rb_define_method(cBuffer, "count", buffer_count, 0);
|
|
1293 rb_define_method(cBuffer, "length", buffer_count, 0);
|
|
1294 rb_define_method(cBuffer, "[]", buffer_aref, 1);
|
|
1295 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
|
|
1296 rb_define_method(cBuffer, "delete", buffer_delete, 1);
|
|
1297 rb_define_method(cBuffer, "append", buffer_append, 2);
|
|
1298
|
|
1299 /* Added line manipulation functions
|
|
1300 * SegPhault - 03/07/05 */
|
|
1301 rb_define_method(cBuffer, "line_number", current_line_number, 0);
|
|
1302 rb_define_method(cBuffer, "line", line_s_current, 0);
|
|
1303 rb_define_method(cBuffer, "line=", set_current_line, 1);
|
|
1304
|
|
1305
|
|
1306 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
|
|
1307 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
|
|
1308 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
|
|
1309 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
|
|
1310 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
|
|
1311 rb_define_method(cVimWindow, "height", window_height, 0);
|
|
1312 rb_define_method(cVimWindow, "height=", window_set_height, 1);
|
|
1313 rb_define_method(cVimWindow, "width", window_width, 0);
|
|
1314 rb_define_method(cVimWindow, "width=", window_set_width, 1);
|
|
1315 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
|
|
1316 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
|
|
1317
|
|
1318 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
|
|
1319 rb_define_virtual_variable("$curwin", window_s_current, 0);
|
|
1320 }
|