Runtime files update
[MacVim.git] / runtime / ftplugin / ruby.vim
blob71801320c73d38ddc7c07970c31be275247646aa
1 " Vim filetype plugin
2 " Language:             Ruby
3 " Maintainer:           Gavin Sinclair <gsinclair at gmail.com>
4 " Info:                 $Id: ruby.vim,v 1.39 2007/05/06 16:38:40 tpope 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"))
16   finish
17 endif
18 let b:did_ftplugin = 1
20 let s:cpo_save = &cpo
21 set cpo&vim
23 if has("gui_running") && !has("gui_win32")
24   setlocal keywordprg=ri\ -T
25 else
26   setlocal keywordprg=ri
27 endif
29 " Matchit support
30 if exists("loaded_matchit") && !exists("b:match_words")
31   let b:match_ignorecase = 0
33   let b:match_words =
34         \ '\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|begin\)\>=\@!' .
35         \ ':' .
36         \ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
37         \ ':' .
38         \ '\<end\>' .
39         \ ',{:},\[:\],(:)'
41   let b:match_skip =
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\\)\\>'"
48 endif
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
58 endif
60 " To activate, :set ballooneval
61 if has('balloon_eval') && exists('+balloonexpr')
62   setlocal balloonexpr=RubyBalloonexpr()
63 endif
66 " TODO:
67 "setlocal define=^\\s*def
69 setlocal comments=:#
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 . '"')
80     else
81       let s:rubypath = system("ruby -e '" . s:code . "'")
82     endif
83     let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
84   else
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 = ".,,"
88   endif
89 endif
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"
96 endif
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'"
118 endif
120 let &cpo = s:cpo_save
121 unlet s:cpo_save
123 if exists("g:did_ruby_ftplugin_functions")
124   finish
125 endif
126 let g:did_ruby_ftplugin_functions = 1
128 function! RubyBalloonexpr()
129   if !exists('s:ri_found')
130     let s:ri_found = executable('ri')
131   endif
132   if s:ri_found
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')
136     let str = b.a
137     let before = strpart(line,0,v:beval_col-strlen(b))
138     let after  = strpart(line,v:beval_col+strlen(a))
139     if str =~ '^\.'
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
145         let str = 'Hash'.str
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
154       else
155         let str = substitute(str,'^#','.','')
156       endif
157     endif
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#','')
164     if str !~ '^\w'
165       return ''
166     endif
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'
169       return ''
170     endif
171     return res
172   else
173     return ""
174   endif
175 endfunction
177 function! s:searchsyn(pattern,syn,flags)
178     norm! m'
179     let i = 0
180     let cnt = v:count ? v:count : 1
181     while i < cnt
182         let i = i + 1
183         let line = line('.')
184         let col  = col('.')
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)
188         endwhile
189         if pos == 0
190             call cursor(line,col)
191             return
192         endif
193     endwhile
194 endfunction
196 function! s:synname()
197     return synIDattr(synID(line('.'),col('.'),0),'name')
198 endfunction
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:
216 "         filetype plugin on
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: