3 " Maintainer: Gavin Sinclair <gsinclair at gmail.com>
4 " Info: $Id: ruby.vim,v 1.16 2008/08/09 17:50:07 vimboss Exp $
5 " URL: http://vim-ruby.rubyforge.org
6 " Anon CVS: See above site
7 " Release Coordinator: Doug Kearns <dougkearns@gmail.com>
8 " ----------------------------------------------------------------------------
10 " Original matchit support thanks to Ned Konz. See his ftplugin/ruby.vim at
11 " http://bike-nomad.com/vim/ruby.vim.
12 " ----------------------------------------------------------------------------
14 " Only do this when not done yet for this buffer
15 if (exists("b:did_ftplugin"))
18 let b:did_ftplugin = 1
23 if has("gui_running") && !has("gui_win32")
24 setlocal keywordprg=ri\ -T
26 setlocal keywordprg=ri
30 if exists("loaded_matchit") && !exists("b:match_words")
31 let b:match_ignorecase = 0
34 \ '\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|begin\)\>=\@!' .
36 \ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
42 \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
43 \ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Escape\\|" .
44 \ "Interpolation\\|NoInterpolation\\|Comment\\|Documentation\\|" .
45 \ "ConditionalModifier\\|RepeatModifier\\|OptionalDo\\|" .
46 \ "Function\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
47 \ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
50 setlocal formatoptions-=t formatoptions+=croql
52 setlocal include=^\\s*\\<\\(load\\\|\w*require\\)\\>
53 setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','')
54 setlocal suffixesadd=.rb
56 if exists("&ofu") && has("ruby")
57 setlocal omnifunc=rubycomplete#Complete
60 " To activate, :set ballooneval
61 if has('balloon_eval') && exists('+balloonexpr')
62 setlocal balloonexpr=RubyBalloonexpr()
67 "setlocal define=^\\s*def
70 setlocal commentstring=#\ %s
72 if !exists("s:rubypath")
73 if has("ruby") && has("win32")
74 ruby VIM::command( 'let s:rubypath = "%s"' % ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,}) )
75 let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
76 elseif executable("ruby")
77 let s:code = "print ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,})"
78 if &shellxquote == "'"
79 let s:rubypath = system('ruby -e "' . s:code . '"')
81 let s:rubypath = system("ruby -e '" . s:code . "'")
83 let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
85 " If we can't call ruby to get its path, just default to using the
86 " current directory and the directory of the current file.
87 let s:rubypath = ".,,"
91 let &l:path = s:rubypath
93 if has("gui_win32") && !exists("b:browsefilter")
94 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
95 \ "All Files (*.*)\t*.*\n"
98 let b:undo_ftplugin = "setl fo< inc< inex< sua< def< com< cms< path< kp<"
99 \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
100 \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
101 \."| if has('balloon_eval') && exists('+bexpr') | setl bexpr< | endif"
103 if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
105 noremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','b')<CR>
106 noremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','')<CR>
107 noremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','b')<CR>
108 noremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','')<CR>
110 noremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','b')<CR>
111 noremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','')<CR>
112 noremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','b')<CR>
113 noremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','')<CR>
115 let b:undo_ftplugin = b:undo_ftplugin
116 \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
117 \."| sil! exe 'unmap <buffer> [m' | sil! exe 'unmap <buffer> ]m' | sil! exe 'unmap <buffer> [M' | sil! exe 'unmap <buffer> ]M'"
120 let &cpo = s:cpo_save
123 if exists("g:did_ruby_ftplugin_functions")
126 let g:did_ruby_ftplugin_functions = 1
128 function! RubyBalloonexpr()
129 if !exists('s:ri_found')
130 let s:ri_found = executable('ri')
133 let line = getline(v:beval_lnum)
134 let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
135 let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
137 let before = strpart(line,0,v:beval_col-strlen(b))
138 let after = strpart(line,v:beval_col+strlen(a))
140 let str = substitute(str,'^\.','#','g')
141 if before =~ '\]\s*$'
142 let str = 'Array'.str
143 elseif before =~ '}\s*$'
144 " False positives from blocks here
146 elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
147 let str = 'String'.str
148 elseif before =~ '\$\d\+\.\d\+\s*$'
149 let str = 'Float'.str
150 elseif before =~ '\$\d\+\s*$'
151 let str = 'Integer'.str
152 elseif before =~ '/\s*$'
153 let str = 'Regexp'.str
155 let str = substitute(str,'^#','.','')
158 let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
159 let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
160 let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
161 let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
162 let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
163 let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
167 silent! let res = substitute(system("ri -f simple -T \"".str.'"'),'\n$','','')
168 if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
177 function! s:searchsyn(pattern,syn,flags)
180 let cnt = v:count ? v:count : 1
185 let pos = search(a:pattern,'W'.a:flags)
186 while pos != 0 && s:synname() !~# a:syn
187 let pos = search(a:pattern,'W'.a:flags)
190 call cursor(line,col)
196 function! s:synname()
197 return synIDattr(synID(line('.'),col('.'),0),'name')
201 " Instructions for enabling "matchit" support:
203 " 1. Look for the latest "matchit" plugin at
205 " http://www.vim.org/scripts/script.php?script_id=39
207 " It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
209 " 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
211 " 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
213 " 4. Ensure this file (ftplugin/ruby.vim) is installed.
215 " 5. Ensure you have this line in your $HOME/.vimrc:
218 " 6. Restart Vim and create the matchit documentation:
220 " :helptags ~/.vim/doc
222 " Now you can do ":help matchit", and you should be able to use "%" on Ruby
223 " keywords. Try ":echo b:match_words" to be sure.
225 " Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
226 " locations of plugin directories, etc., as there are several options, and it
227 " differs on Windows. Email gsinclair@soyabean.com.au if you need help.
230 " vim: nowrap sw=2 sts=2 ts=8: