Bram Moolenaar | 0536570 | 2010-10-27 18:34:44 +0200 | [diff] [blame] | 1 | *if_ruby.txt* For Vim version 7.3. Last change: 2010 Oct 27 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Shugo Maeda |
| 5 | |
| 6 | The Ruby Interface to Vim *ruby* *Ruby* |
| 7 | |
| 8 | |
| 9 | 1. Commands |ruby-commands| |
| 10 | 2. The VIM module |ruby-vim| |
| 11 | 3. VIM::Buffer objects |ruby-buffer| |
| 12 | 4. VIM::Window objects |ruby-window| |
| 13 | 5. Global variables |ruby-globals| |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 14 | 6. Dynamic loading |ruby-dynamic| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | |
| 16 | {Vi does not have any of these commands} |
| 17 | *E266* *E267* *E268* *E269* *E270* *E271* *E272* *E273* |
| 18 | |
| 19 | The Ruby interface only works when Vim was compiled with the |+ruby| feature. |
| 20 | |
| 21 | The home page for ruby is http://www.ruby-lang.org/. You can find links for |
| 22 | downloading Ruby there. |
| 23 | |
| 24 | ============================================================================== |
| 25 | 1. Commands *ruby-commands* |
| 26 | |
| 27 | *:ruby* *:rub* |
| 28 | :rub[y] {cmd} Execute Ruby command {cmd}. |
| 29 | |
| 30 | :rub[y] << {endpattern} |
| 31 | {script} |
| 32 | {endpattern} |
| 33 | Execute Ruby script {script}. |
| 34 | {endpattern} must NOT be preceded by any white space. |
| 35 | If {endpattern} is omitted, it defaults to a dot '.' |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 36 | like for the |:append| and |:insert| commands. This |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 37 | form of the |:ruby| command is mainly useful for |
| 38 | including ruby code in vim scripts. |
| 39 | Note: This command doesn't work when the Ruby feature |
| 40 | wasn't compiled in. To avoid errors, see |
| 41 | |script-here|. |
| 42 | |
Bram Moolenaar | 0536570 | 2010-10-27 18:34:44 +0200 | [diff] [blame] | 43 | Command to try it out: > |
| 44 | :ruby print "Hello" # this is a comment |
| 45 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | Example Vim script: > |
| 47 | |
| 48 | function! RedGem() |
| 49 | ruby << EOF |
| 50 | class Garnet |
| 51 | def initialize(s) |
| 52 | @buffer = VIM::Buffer.current |
| 53 | vimputs(s) |
| 54 | end |
| 55 | def vimputs(s) |
| 56 | @buffer.append(@buffer.count,s) |
| 57 | end |
| 58 | end |
| 59 | gem = Garnet.new("pretty") |
| 60 | EOF |
| 61 | endfunction |
| 62 | < |
| 63 | |
| 64 | *:rubydo* *:rubyd* *E265* |
| 65 | :[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the |
| 66 | [range], with $_ being set to the text of each line in |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 67 | turn, without a trailing <EOL>. Setting $_ will change |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | the text, but note that it is not possible to add or |
| 69 | delete lines using this command. |
| 70 | The default for [range] is the whole file: "1,$". |
| 71 | |
| 72 | *:rubyfile* *:rubyf* |
| 73 | :rubyf[ile] {file} Execute the Ruby script in {file}. This is the same as |
| 74 | ":ruby load 'file'", but allows file name completion. |
| 75 | |
| 76 | Executing Ruby commands is not possible in the |sandbox|. |
| 77 | |
| 78 | ============================================================================== |
| 79 | 2. The VIM module *ruby-vim* |
| 80 | |
| 81 | Ruby code gets all of its access to vim via the "VIM" module. |
| 82 | |
| 83 | Overview > |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 84 | print "Hello" # displays a message |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 85 | VIM.command(cmd) # execute an Ex command |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 86 | num = VIM::Window.count # gets the number of windows |
| 87 | w = VIM::Window[n] # gets window "n" |
| 88 | cw = VIM::Window.current # gets the current window |
| 89 | num = VIM::Buffer.count # gets the number of buffers |
| 90 | b = VIM::Buffer[n] # gets buffer "n" |
| 91 | cb = VIM::Buffer.current # gets the current buffer |
| 92 | w.height = lines # sets the window height |
| 93 | w.cursor = [row, col] # sets the window cursor position |
| 94 | pos = w.cursor # gets an array [row, col] |
| 95 | name = b.name # gets the buffer file name |
| 96 | line = b[n] # gets a line from the buffer |
| 97 | num = b.count # gets the number of lines |
| 98 | b[n] = str # sets a line in the buffer |
| 99 | b.delete(n) # deletes a line |
| 100 | b.append(n, str) # appends a line after n |
| 101 | line = VIM::Buffer.current.line # gets the current line |
| 102 | num = VIM::Buffer.current.line_number # gets the current line number |
| 103 | VIM::Buffer.current.line = "test" # sets the current line number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | < |
| 105 | |
| 106 | Module Functions: |
| 107 | |
| 108 | *ruby-message* |
| 109 | VIM::message({msg}) |
| 110 | Displays the message {msg}. |
| 111 | |
| 112 | *ruby-set_option* |
| 113 | VIM::set_option({arg}) |
| 114 | Sets a vim option. {arg} can be any argument that the ":set" command |
| 115 | accepts. Note that this means that no spaces are allowed in the |
| 116 | argument! See |:set|. |
| 117 | |
| 118 | *ruby-command* |
| 119 | VIM::command({cmd}) |
| 120 | Executes Ex command {cmd}. |
| 121 | |
| 122 | *ruby-evaluate* |
| 123 | VIM::evaluate({expr}) |
| 124 | Evaluates {expr} using the vim internal expression evaluator (see |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 125 | |expression|). Returns the expression result as a string. |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 126 | A |List| is turned into a string by joining the items and inserting |
| 127 | line breaks. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | |
| 129 | ============================================================================== |
| 130 | 3. VIM::Buffer objects *ruby-buffer* |
| 131 | |
| 132 | VIM::Buffer objects represent vim buffers. |
| 133 | |
| 134 | Class Methods: |
| 135 | |
| 136 | current Returns the current buffer object. |
| 137 | count Returns the number of buffers. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 138 | self[{n}] Returns the buffer object for the number {n}. The first number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | is 0. |
| 140 | |
| 141 | Methods: |
| 142 | |
| 143 | name Returns the name of the buffer. |
| 144 | number Returns the number of the buffer. |
| 145 | count Returns the number of lines. |
| 146 | length Returns the number of lines. |
| 147 | self[{n}] Returns a line from the buffer. {n} is the line number. |
| 148 | self[{n}] = {str} |
| 149 | Sets a line in the buffer. {n} is the line number. |
| 150 | delete({n}) Deletes a line from the buffer. {n} is the line number. |
| 151 | append({n}, {str}) |
| 152 | Appends a line after the line {n}. |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 153 | line Returns the current line of the buffer if the buffer is |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 154 | active. |
| 155 | line = {str} Sets the current line of the buffer if the buffer is active. |
| 156 | line_number Returns the number of the current line if the buffer is |
| 157 | active. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 158 | |
| 159 | ============================================================================== |
| 160 | 4. VIM::Window objects *ruby-window* |
| 161 | |
| 162 | VIM::Window objects represent vim windows. |
| 163 | |
| 164 | Class Methods: |
| 165 | |
| 166 | current Returns the current window object. |
| 167 | count Returns the number of windows. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 168 | self[{n}] Returns the window object for the number {n}. The first number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 169 | is 0. |
| 170 | |
| 171 | Methods: |
| 172 | |
| 173 | buffer Returns the buffer displayed in the window. |
| 174 | height Returns the height of the window. |
| 175 | height = {n} Sets the window height to {n}. |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 176 | width Returns the width of the window. |
| 177 | width = {n} Sets the window width to {n}. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 178 | cursor Returns a [row, col] array for the cursor position. |
| 179 | cursor = [{row}, {col}] |
| 180 | Sets the cursor position to {row} and {col}. |
| 181 | |
| 182 | ============================================================================== |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 183 | 5. Global variables *ruby-globals* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 184 | |
| 185 | There are two global variables. |
| 186 | |
| 187 | $curwin The current window object. |
| 188 | $curbuf The current buffer object. |
| 189 | |
| 190 | ============================================================================== |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 191 | 6. Dynamic loading *ruby-dynamic* |
| 192 | |
Bram Moolenaar | 0536570 | 2010-10-27 18:34:44 +0200 | [diff] [blame] | 193 | On MS-Windows and Unix the Ruby library can be loaded dynamically. The |
| 194 | |:version| output then includes |+ruby/dyn|. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 195 | |
Bram Moolenaar | 0536570 | 2010-10-27 18:34:44 +0200 | [diff] [blame] | 196 | This means that Vim will search for the Ruby DLL file or shared library only |
| 197 | when needed. When you don't use the Ruby interface you don't need it, thus |
| 198 | you can use Vim even though this library file is not on your system. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 199 | |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 200 | You need to install the right version of Ruby for this to work. You can find |
| 201 | the package to download from: |
| 202 | http://www.garbagecollect.jp/ruby/mswin32/en/download/release.html |
| 203 | Currently that is ruby-1.9.1-p429-i386-mswin32.zip |
| 204 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 205 | To use the Ruby interface the Ruby DLL must be in your search path. In a |
| 206 | console window type "path" to see what directories are used. |
| 207 | |
| 208 | The name of the DLL must match the Ruby version Vim was compiled with. |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 209 | Currently the name is "msvcrt-ruby191.dll". That is for Ruby 1.9.1. To know |
| 210 | for sure edit "gvim.exe" and search for "ruby\d*.dll\c". |
| 211 | |
| 212 | If you want to build Vim with Ruby 1.9.1, you need to edit the config.h file |
| 213 | and comment-out the check for _MSC_VER. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 214 | |
| 215 | ============================================================================== |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | vim:tw=78:ts=8:ft=help:norl: |