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