vim_client-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .bundle/
2
+ vendor/bundle/
3
+ tmp/
4
+ *.so
data/.yardopts ADDED
@@ -0,0 +1,3 @@
1
+ --title "VimClient Documentation"
2
+ --readme README.md
3
+ --markup markdown
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ vim_client-ruby (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ rake (0.9.2.2)
11
+ rake-compiler (0.8.1)
12
+ rake
13
+ redcarpet (2.1.1)
14
+ rspec (2.10.0)
15
+ rspec-core (~> 2.10.0)
16
+ rspec-expectations (~> 2.10.0)
17
+ rspec-mocks (~> 2.10.0)
18
+ rspec-core (2.10.1)
19
+ rspec-expectations (2.10.0)
20
+ diff-lcs (~> 1.1.3)
21
+ rspec-mocks (2.10.1)
22
+ yard (0.8.2.1)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ rake
29
+ rake-compiler
30
+ redcarpet
31
+ rspec
32
+ vim_client-ruby!
33
+ yard
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Brian D. Burns
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ VimClient is a Ruby C extension providing a client for Vim's [clientserver][] feature.
2
+ It allows communication with a Vim server through a persistent connection to the X11 server,
3
+ much like using another instance of Vim.
4
+
5
+ Prerequisites
6
+ --------
7
+
8
+ - Ruby 1.9.2 or better
9
+ - X11 development headers
10
+ - For the Vim server, Vim must be compiled with the '+X11' and '+clientserver' features.
11
+
12
+ Installation
13
+ ------------
14
+
15
+ Install the gem using:
16
+
17
+ ```
18
+ gem install vim_client-ruby
19
+ ```
20
+
21
+ Or, add it to your project's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'vim_client-ruby'
25
+ ```
26
+
27
+ Usage
28
+ -----
29
+
30
+ Require the library:
31
+
32
+ ```ruby
33
+ require 'vim_client'
34
+ ```
35
+
36
+ Before calling any methods, be sure to set the name of your Vim server:
37
+
38
+ ```ruby
39
+ VimClient.server_name = 'vim_server1'
40
+ VimClient.timeout_sec = 30 # and optionally update the timeout
41
+ ```
42
+
43
+ For more usage information, see the documentation in `lib/vim_client.rb`.
44
+ Best viewed with [Yard][]
45
+
46
+ Also, see the [wiki][] for examples, tips and sample projects.
47
+
48
+ [clientserver]: http://vimdoc.sourceforge.net/htmldoc/remote.html#clientserver
49
+ [Yard]: http://rubydoc.info/gems/vim_client-ruby
50
+ [wiki]: http://github.com/burns/vim_client-ruby/wiki
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+ require 'rake/extensiontask'
3
+ require 'rspec/core/rake_task'
4
+
5
+ task :default => :spec
6
+ task :spec => [:compile]
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ Rake::ExtensionTask.new('vim_client') do |ext|
11
+ ext.lib_dir = 'lib/vim_client'
12
+ end
@@ -0,0 +1,20 @@
1
+ require 'mkmf'
2
+
3
+ unless have_header('sys/select.h')
4
+ unless have_header('sys/poll.h')
5
+ abort 'select or poll is required'
6
+ end
7
+ end
8
+
9
+ unless have_func('strcasecmp')
10
+ have_func('stricmp')
11
+ end
12
+ unless have_func('strncasecmp')
13
+ have_func('strnicmp')
14
+ end
15
+
16
+ unless have_library('X11')
17
+ abort 'X11 is required'
18
+ end
19
+
20
+ create_makefile('vim_client/vim_client')
@@ -0,0 +1,267 @@
1
+ /* vi:set ts=8 sts=4 sw=4:
2
+ *
3
+ * Misc functions
4
+ */
5
+
6
+ #include "vim_client.h"
7
+
8
+ /*
9
+ * Copy "string" into newly allocated memory.
10
+ */
11
+ char_u *
12
+ vim_strsave(char_u *string) {
13
+ char_u *p;
14
+ unsigned len;
15
+
16
+ len = (unsigned)STRLEN(string) + 1;
17
+ p = alloc(len);
18
+ if (p != NULL)
19
+ memmove(p, string, (size_t)len);
20
+ return p;
21
+ }
22
+
23
+ char_u *
24
+ alloc(long_u size) {
25
+ char_u *p = NULL; /* pointer to new storage space */
26
+
27
+ /* Safety check for allocating zero bytes */
28
+ if (size == 0)
29
+ return NULL;
30
+
31
+ if ((p = (char_u *)malloc((size_t)size)) != NULL)
32
+ return p;
33
+
34
+ rb_raise(ex_NoMemoryError, "out of memory");
35
+ }
36
+
37
+ /*
38
+ * Replacement for free() that ignores NULL pointers.
39
+ */
40
+ void
41
+ vim_free(void *x) {
42
+ if (x != NULL)
43
+ free(x);
44
+ }
45
+
46
+ #if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP))
47
+ /*
48
+ * Compare two strings, ignoring case, using current locale.
49
+ * Doesn't work for multi-byte characters.
50
+ * return 0 for match, < 0 for smaller, > 0 for bigger
51
+ */
52
+ int
53
+ vim_stricmp(char *s1, char *s2) {
54
+ int i;
55
+
56
+ for (;;)
57
+ {
58
+ i = (int)tolower(*s1) - (int)tolower(*s2);
59
+ if (i != 0)
60
+ return i; /* this character different */
61
+ if (*s1 == NUL)
62
+ break; /* strings match until NUL */
63
+ ++s1;
64
+ ++s2;
65
+ }
66
+ return 0; /* strings match */
67
+ }
68
+ #endif
69
+
70
+ #if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP))
71
+ /*
72
+ * Compare two strings, for length "len", ignoring case, using current locale.
73
+ * Doesn't work for multi-byte characters.
74
+ * return 0 for match, < 0 for smaller, > 0 for bigger
75
+ */
76
+ int
77
+ vim_strnicmp(char *s1, char *s2, size_t len) {
78
+ int i;
79
+
80
+ while (len > 0)
81
+ {
82
+ i = (int)tolower(*s1) - (int)tolower(*s2);
83
+ if (i != 0)
84
+ return i; /* this character different */
85
+ if (*s1 == NUL)
86
+ break; /* strings match until NUL */
87
+ ++s1;
88
+ ++s2;
89
+ --len;
90
+ }
91
+ return 0; /* strings match */
92
+ }
93
+ #endif
94
+
95
+ /*
96
+ * These are Vim's canonical encoding names (src/mbyte.c)
97
+ * with the Ruby encoding names they resolve to.
98
+ * Those names which Ruby was not able to resolve are marked with (*).
99
+ *
100
+ * The Aliases listed will be used to fill in the gaps.
101
+ * 'dec-mcs' and 'hp-roman8' will be unsupported, since these
102
+ * can't be mapped from Ruby to Vim.
103
+ *
104
+ * Note that Vim's converter handles all Unicode as UTF-8.
105
+ * The encoding reported from the server does not reflect this, so these will
106
+ * be aliases as UTF-8. The corresponding Ruby encodings will be converted to
107
+ * UTF-8 before being sent to the server. These are marked with (**).
108
+ *
109
+ * Also, it appears that support for macRoman and the DOS/Windows codepages
110
+ * require that Vim is compiled on those systems.
111
+ *
112
+ * Aliases
113
+ * Vim Ruby Vim-to-Ruby Ruby-to-Vim
114
+ ***********************************************************************
115
+ latin1 * ASCII-8BIT ISO-8859-1
116
+ iso-8859-2 ISO-8859-2
117
+ iso-8859-3 ISO-8859-3
118
+ iso-8859-4 ISO-8859-4
119
+ iso-8859-5 ISO-8859-5
120
+ iso-8859-6 ISO-8859-6
121
+ iso-8859-7 ISO-8859-7
122
+ iso-8859-8 ISO-8859-8
123
+ iso-8859-9 ISO-8859-9
124
+ iso-8859-10 ISO-8859-10
125
+ iso-8859-11 ISO-8859-11
126
+ iso-8859-13 ISO-8859-13
127
+ iso-8859-14 ISO-8859-14
128
+ iso-8859-15 ISO-8859-15
129
+ koi8-r KOI8-R
130
+ koi8-u KOI8-U
131
+ utf-8 UTF-8
132
+ ucs-2 * ASCII-8BIT UTF-16BE **
133
+ ucs-2le * ASCII-8BIT UTF-16LE **
134
+ utf-16 * ASCII-8BIT UTF-16BE **
135
+ utf-16le UTF-16LE **
136
+ ucs-4 * ASCII-8BIT UTF-32BE **
137
+ ucs-4le UTF-32LE **
138
+ euc-jp EUC-JP
139
+ sjis Shift_JIS
140
+ euc-kr EUC-KR
141
+ euc-cn GB2312
142
+ euc-tw EUC-TW
143
+ big5 Big5
144
+ cp437 IBM437 cp437
145
+ cp737 IBM737 cp737
146
+ cp775 IBM775 cp775
147
+ cp850 CP850
148
+ cp852 CP852
149
+ cp855 CP855
150
+ cp857 IBM857 cp857
151
+ cp860 IBM860 cp860
152
+ cp861 IBM861 cp861
153
+ cp862 IBM862 cp862
154
+ cp863 IBM863 cp863
155
+ cp865 IBM865 cp865
156
+ cp866 IBM866 cp866
157
+ cp869 IBM869 cp869
158
+ cp874 Windows-874 cp874
159
+ cp932 Windows-31J cp932
160
+ cp936 GBK
161
+ cp949 CP949
162
+ cp950 Big5
163
+ cp1250 Windows-1250 cp1250
164
+ cp1251 Windows-1251 cp1251
165
+ cp1252 Windows-1252 latin1
166
+ cp1253 Windows-1253 cp1253
167
+ cp1254 Windows-1254 cp1254
168
+ cp1255 Windows-1255 cp1255
169
+ cp1256 Windows-1256 cp1256
170
+ cp1257 Windows-1257 cp1257
171
+ cp1258 Windows-1258 cp1258
172
+ macroman macRoman
173
+ dec-mcs * ASCII-8BIT -- Unsupported --
174
+ hp-roman8 * ASCII-8BIT -- Unsupported --
175
+ */
176
+
177
+ /* Vim-to-Ruby aliases from above */
178
+ static struct {
179
+ const char_u *name;
180
+ const char_u *alias;
181
+ } vim2rb_enc_aliases[] = {
182
+ { "latin1", "ISO-8859-1" },
183
+ { "ucs-2", "UTF-8" },
184
+ { "ucs-2le", "UTF-8" },
185
+ { "utf-16", "UTF-8" },
186
+ { "utf-16le", "UTF-8" },
187
+ { "ucs-4", "UTF-8" },
188
+ { "ucs-4le", "UTF-8" },
189
+ { NULL, NULL }
190
+ };
191
+
192
+ /*
193
+ * Takes the response and encoding name returned from the Vim server
194
+ * and returns a Ruby string associated with the given encoding, or an
195
+ * alias from the Vim-to-Ruby alias table.
196
+ * This does not perform any transcoding.
197
+ */
198
+ VALUE
199
+ vim2rb_enc_str(char_u **res, char_u *server_enc) {
200
+ int i;
201
+ for (i = 0; vim2rb_enc_aliases[i].name != NULL; ++i) {
202
+ if (STRCMP(server_enc, vim2rb_enc_aliases[i].name) == 0)
203
+ server_enc = (char_u *)vim2rb_enc_aliases[i].alias;
204
+ }
205
+
206
+ return rb_enc_str_new(*res, STRLEN(*res), rb_enc_find(server_enc));
207
+ }
208
+
209
+ /* Ruby-to-Vim aliases from above */
210
+ static struct {
211
+ const char_u *name;
212
+ const char_u *alias;
213
+ int convert;
214
+ } rb2vim_enc_aliases[] = {
215
+ { "UTF-16BE", "UTF-8", 1 },
216
+ { "UTF-16LE", "UTF-8", 1 },
217
+ { "UTF-32BE", "UTF-8", 1 },
218
+ { "UTF-32LE", "UTF-8", 1 },
219
+ { "IBM437", "cp437", 0 },
220
+ { "IBM737", "cp737", 0 },
221
+ { "IBM775", "cp775", 0 },
222
+ { "IBM857", "cp857", 0 },
223
+ { "IBM860", "cp860", 0 },
224
+ { "IBM861", "cp861", 0 },
225
+ { "IBM862", "cp862", 0 },
226
+ { "IBM863", "cp863", 0 },
227
+ { "IBM865", "cp865", 0 },
228
+ { "IBM866", "cp866", 0 },
229
+ { "IBM869", "cp869", 0 },
230
+ { "Windows-874", "cp874", 0 },
231
+ { "Windows-31J", "cp932", 0 },
232
+ { "Windows-1250", "cp1250", 0 },
233
+ { "Windows-1251", "cp1251", 0 },
234
+ { "Windows-1252", "latin1", 0 },
235
+ { "Windows-1253", "cp1253", 0 },
236
+ { "Windows-1254", "cp1254", 0 },
237
+ { "Windows-1255", "cp1255", 0 },
238
+ { "Windows-1256", "cp1256", 0 },
239
+ { "Windows-1257", "cp1257", 0 },
240
+ { "Windows-1258", "cp1258", 0 },
241
+ { NULL, NULL }
242
+ };
243
+
244
+ /* This function uses the Ruby-to-Vim aliases table to set the encoding
245
+ * name to be sent to the server (returned in cmd_enc), and returns the
246
+ * command string in C string format. It will also transcode any 16/32 bit
247
+ * Unicode to UTF-8.
248
+ */
249
+ char_u *
250
+ rb2vim_enc_str(VALUE str, char_u **cmd_enc) {
251
+ *cmd_enc = (char_u *)rb_enc_name(rb_enc_get(str));
252
+
253
+ int i;
254
+ for (i = 0; rb2vim_enc_aliases[i].name != NULL; ++i) {
255
+ if (STRCMP(*cmd_enc, rb2vim_enc_aliases[i].name) == 0) {
256
+ *cmd_enc = (char_u *)rb2vim_enc_aliases[i].alias;
257
+
258
+ if (rb2vim_enc_aliases[i].convert)
259
+ /* returns a new string */
260
+ str = rb_str_export_to_enc(str, rb_enc_find(*cmd_enc));
261
+
262
+ break;
263
+ }
264
+ }
265
+
266
+ return StringValueCStr(str);
267
+ }