blob: 630d260c9ed77d0be9cbe81000a5f16e87411a39 [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 Moolenaarc6249bb2006-04-15 20:25:09 +00004" Info: $Id$
5" 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
33 " TODO: improve optional do loops
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000034 let b:match_words =
35 \ '\%(' .
36 \ '\%(\%(\.\|\:\:\)\s*\|\:\)\@<!\<\%(class\|module\|begin\|def\|case\|for\|do\)\>' .
37 \ '\|' .
38 \ '\%(\%(^\|\.\.\.\=\|[{\:\,;([<>~\*/%&^|+-]\|\%(\<[_[\:lower\:]][_[\:alnum\:]]*\)\@<![!=?]\)\s*\)\@<=\%(if\|unless\|while\|until\)\>' .
39 \ '\)' .
40 \ ':' .
41 \ '\%(' .
42 \ '\%(\%(\.\|\:\:\)\s*\|\:\)\@<!\<\%(else\|elsif\|ensure\|when\)\>' .
43 \ '\|' .
44 \ '\%(\%(^\|;\)\s*\)\@<=\<rescue\>' .
45 \ '\)' .
46 \ ':' .
47 \ '\%(\%(\.\|\:\:\)\s*\|\:\)\@<!\<end\>' .
48 \ ',{:},\[:\],(:)'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000049
50 let b:match_skip =
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000051 \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
52 \ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Interpolation\\|" .
53 \ "NoInterpolation\\|Escape\\|Comment\\|Documentation\\)\\>'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000054
55endif
56
57setlocal formatoptions-=t formatoptions+=croql
58
59setlocal include=^\\s*\\<\\(load\\\|\w*require\\)\\>
60setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','')
61setlocal suffixesadd=.rb
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000062
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000063if exists("&ofu") && has("ruby")
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000064 setlocal omnifunc=rubycomplete#Complete
65endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +000066
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000067" To activate, :set ballooneval
68if has('balloon_eval') && exists('+balloonexpr')
69 setlocal balloonexpr=RubyBalloonexpr()
70endif
71
72
Bram Moolenaar60a795a2005-09-16 21:55:43 +000073" TODO:
74"setlocal define=^\\s*def
75
76setlocal comments=:#
77setlocal commentstring=#\ %s
78
79if !exists("s:rubypath")
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000080 if has("ruby") && has("win32")
81 ruby VIM::command( 'let s:rubypath = "%s"' % ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,}) )
82 let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
83 elseif executable("ruby")
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000084 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 +000085 if &shellxquote == "'"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000086 let s:rubypath = system('ruby -e "' . s:code . '"')
Bram Moolenaar60a795a2005-09-16 21:55:43 +000087 else
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000088 let s:rubypath = system("ruby -e '" . s:code . "'")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000089 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000090 let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
Bram Moolenaar60a795a2005-09-16 21:55:43 +000091 else
92 " If we can't call ruby to get its path, just default to using the
93 " current directory and the directory of the current file.
94 let s:rubypath = ".,,"
95 endif
96endif
97
98let &l:path = s:rubypath
99
100if has("gui_win32") && !exists("b:browsefilter")
101 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000102 \ "All Files (*.*)\t*.*\n"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000103endif
104
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000105let b:undo_ftplugin = "setl fo< inc< inex< sua< def< com< cms< path< kp<"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000106 \ "| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000107 \ "| if exists('&ofu') && has('ruby') | setl ofu< | endif"
108 \ "| if has('balloon_eval') && exists('+bexpr') | setl bexpr< | endif"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000109
110let &cpo = s:cpo_save
111unlet s:cpo_save
112
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000113if exists("g:did_ruby_ftplugin_functions")
114 finish
115endif
116let g:did_ruby_ftplugin_functions = 1
117
118function! RubyBalloonexpr()
119 if !exists('s:ri_found')
120 let s:ri_found = executable('ri')
121 endif
122 if s:ri_found
123 let line = getline(v:beval_lnum)
124 let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
125 let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
126 let str = b.a
127 let before = strpart(line,0,v:beval_col-strlen(b))
128 let after = strpart(line,v:beval_col+strlen(a))
129 if str =~ '^\.'
130 let str = substitute(str,'^\.','#','g')
131 if before =~ '\]\s*$'
132 let str = 'Array'.str
133 elseif before =~ '}\s*$'
134 " False positives from blocks here
135 let str = 'Hash'.str
136 elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
137 let str = 'String'.str
138 elseif before =~ '\$\d\+\.\d\+\s*$'
139 let str = 'Float'.str
140 elseif before =~ '\$\d\+\s*$'
141 let str = 'Integer'.str
142 elseif before =~ '/\s*$'
143 let str = 'Regexp'.str
144 else
145 let str = substitute(str,'^#','.','')
146 endif
147 endif
148 let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
149 let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
150 let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
151 let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
152 let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
153 let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
154 if str !~ '^\w'
155 return ''
156 endif
157 silent! let res = substitute(system("ri -f simple -T \"".str.'"'),'\n$','','')
158 if res =~ '^Nothing known about' || res =~ '^Bad argument:'
159 return ''
160 endif
161 return res
162 else
163 return ""
164 endif
165endfunction
166
167
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000168"
169" Instructions for enabling "matchit" support:
170"
171" 1. Look for the latest "matchit" plugin at
172"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000173" http://www.vim.org/scripts/script.php?script_id=39
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000174"
175" It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
176"
177" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
178"
179" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
180"
181" 4. Ensure this file (ftplugin/ruby.vim) is installed.
182"
183" 5. Ensure you have this line in your $HOME/.vimrc:
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000184" filetype plugin on
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000185"
186" 6. Restart Vim and create the matchit documentation:
187"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000188" :helptags ~/.vim/doc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000189"
190" Now you can do ":help matchit", and you should be able to use "%" on Ruby
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000191" keywords. Try ":echo b:match_words" to be sure.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000192"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000193" Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000194" locations of plugin directories, etc., as there are several options, and it
195" differs on Windows. Email gsinclair@soyabean.com.au if you need help.
196"
197
198" vim: nowrap sw=2 sts=2 ts=8 ff=unix: