blob: 7dfdfb5ad7f16ec6dd3d0d91f5ef40f46ff3da91 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00002" Language: Ruby
Bram Moolenaar91170f82006-05-05 21:15:17 +00003" Maintainer: Gavin Sinclair <gsinclair at gmail.com>
Bram Moolenaar5c736222010-01-06 20:54:52 +01004" Info: $Id: ruby.vim,v 1.40 2008/06/29 04:18:43 tpope Exp $
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00005" URL: http://vim-ruby.rubyforge.org
6" Anon CVS: See above site
Bram Moolenaar756ec0f2007-05-05 17:59:48 +00007" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
Bram Moolenaar60a795a2005-09-16 21:55:43 +00008" ----------------------------------------------------------------------------
9"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000010" Original matchit support thanks to Ned Konz. See his ftplugin/ruby.vim at
Bram Moolenaar60a795a2005-09-16 21:55:43 +000011" http://bike-nomad.com/vim/ruby.vim.
12" ----------------------------------------------------------------------------
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14" Only do this when not done yet for this buffer
15if (exists("b:did_ftplugin"))
Bram Moolenaar60a795a2005-09-16 21:55:43 +000016 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000017endif
18let b:did_ftplugin = 1
19
Bram Moolenaar60a795a2005-09-16 21:55:43 +000020let s:cpo_save = &cpo
21set cpo&vim
22
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000023if has("gui_running") && !has("gui_win32")
24 setlocal keywordprg=ri\ -T
25else
26 setlocal keywordprg=ri
27endif
28
Bram Moolenaar60a795a2005-09-16 21:55:43 +000029" Matchit support
30if exists("loaded_matchit") && !exists("b:match_words")
31 let b:match_ignorecase = 0
32
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000033 let b:match_words =
Bram Moolenaar79166c42007-05-10 18:29:51 +000034 \ '\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|begin\)\>=\@!' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000035 \ ':' .
Bram Moolenaar79166c42007-05-10 18:29:51 +000036 \ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000037 \ ':' .
Bram Moolenaar79166c42007-05-10 18:29:51 +000038 \ '\<end\>' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000039 \ ',{:},\[:\],(:)'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000040
41 let b:match_skip =
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000042 \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
Bram Moolenaar79166c42007-05-10 18:29:51 +000043 \ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Escape\\|" .
44 \ "Interpolation\\|NoInterpolation\\|Comment\\|Documentation\\|" .
45 \ "ConditionalModifier\\|RepeatModifier\\|OptionalDo\\|" .
46 \ "Function\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
47 \ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000048endif
49
50setlocal formatoptions-=t formatoptions+=croql
51
52setlocal include=^\\s*\\<\\(load\\\|\w*require\\)\\>
53setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','')
54setlocal suffixesadd=.rb
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000055
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000056if exists("&ofu") && has("ruby")
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000057 setlocal omnifunc=rubycomplete#Complete
58endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +000059
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000060" To activate, :set ballooneval
61if has('balloon_eval') && exists('+balloonexpr')
62 setlocal balloonexpr=RubyBalloonexpr()
63endif
64
65
Bram Moolenaar60a795a2005-09-16 21:55:43 +000066" TODO:
67"setlocal define=^\\s*def
68
69setlocal comments=:#
70setlocal commentstring=#\ %s
71
72if !exists("s:rubypath")
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000073 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")
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000077 let s:code = "print ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,})"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000078 if &shellxquote == "'"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000079 let s:rubypath = system('ruby -e "' . s:code . '"')
Bram Moolenaar60a795a2005-09-16 21:55:43 +000080 else
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000081 let s:rubypath = system("ruby -e '" . s:code . "'")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000082 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000083 let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
Bram Moolenaar60a795a2005-09-16 21:55:43 +000084 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
89endif
90
91let &l:path = s:rubypath
92
93if has("gui_win32") && !exists("b:browsefilter")
94 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000095 \ "All Files (*.*)\t*.*\n"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000096endif
97
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000098let b:undo_ftplugin = "setl fo< inc< inex< sua< def< com< cms< path< kp<"
Bram Moolenaar79166c42007-05-10 18:29:51 +000099 \."| 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"
102
103if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
104
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>
109
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>
114
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'"
118endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000119
120let &cpo = s:cpo_save
121unlet s:cpo_save
122
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000123if exists("g:did_ruby_ftplugin_functions")
124 finish
125endif
126let g:did_ruby_ftplugin_functions = 1
127
128function! 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$','','')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000168 if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000169 return ''
170 endif
171 return res
172 else
173 return ""
174 endif
175endfunction
176
Bram Moolenaar79166c42007-05-10 18:29:51 +0000177function! 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
194endfunction
195
196function! s:synname()
197 return synIDattr(synID(line('.'),col('.'),0),'name')
198endfunction
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000199
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000200"
201" Instructions for enabling "matchit" support:
202"
203" 1. Look for the latest "matchit" plugin at
204"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000205" http://www.vim.org/scripts/script.php?script_id=39
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000206"
207" It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
208"
209" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
210"
211" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
212"
213" 4. Ensure this file (ftplugin/ruby.vim) is installed.
214"
215" 5. Ensure you have this line in your $HOME/.vimrc:
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000216" filetype plugin on
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000217"
218" 6. Restart Vim and create the matchit documentation:
219"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000220" :helptags ~/.vim/doc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000221"
222" Now you can do ":help matchit", and you should be able to use "%" on Ruby
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000223" keywords. Try ":echo b:match_words" to be sure.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000224"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000225" Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000226" 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.
228"
229
Bram Moolenaar446cb832008-06-24 21:56:24 +0000230" vim: nowrap sw=2 sts=2 ts=8: