Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim filetype plugin |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 2 | " Language: Ruby |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 3 | " Maintainer: Gavin Sinclair <gsinclair at gmail.com> |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4 | " Info: $Id$ |
| 5 | " URL: http://vim-ruby.rubyforge.org |
| 6 | " Anon CVS: See above site |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 7 | " Release Coordinator: Doug Kearns <dougkearns@gmail.com> |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 8 | " ---------------------------------------------------------------------------- |
| 9 | " |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 10 | " Original matchit support thanks to Ned Konz. See his ftplugin/ruby.vim at |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 11 | " http://bike-nomad.com/vim/ruby.vim. |
| 12 | " ---------------------------------------------------------------------------- |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 13 | |
| 14 | " Only do this when not done yet for this buffer |
| 15 | if (exists("b:did_ftplugin")) |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 16 | finish |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | endif |
| 18 | let b:did_ftplugin = 1 |
| 19 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 20 | let s:cpo_save = &cpo |
| 21 | set cpo&vim |
| 22 | |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 23 | if has("gui_running") && !has("gui_win32") |
| 24 | setlocal keywordprg=ri\ -T |
| 25 | else |
| 26 | setlocal keywordprg=ri |
| 27 | endif |
| 28 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 29 | " Matchit support |
| 30 | if exists("loaded_matchit") && !exists("b:match_words") |
| 31 | let b:match_ignorecase = 0 |
| 32 | |
| 33 | " TODO: improve optional do loops |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 34 | 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 Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 49 | |
| 50 | let b:match_skip = |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 51 | \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" . |
| 52 | \ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Interpolation\\|" . |
| 53 | \ "NoInterpolation\\|Escape\\|Comment\\|Documentation\\)\\>'" |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 54 | |
| 55 | endif |
| 56 | |
| 57 | setlocal formatoptions-=t formatoptions+=croql |
| 58 | |
| 59 | setlocal include=^\\s*\\<\\(load\\\|\w*require\\)\\> |
| 60 | setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','') |
| 61 | setlocal suffixesadd=.rb |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 62 | |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 63 | if exists("&ofu") && has("ruby") |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 64 | setlocal omnifunc=rubycomplete#Complete |
| 65 | endif |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 66 | |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 67 | " To activate, :set ballooneval |
| 68 | if has('balloon_eval') && exists('+balloonexpr') |
| 69 | setlocal balloonexpr=RubyBalloonexpr() |
| 70 | endif |
| 71 | |
| 72 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 73 | " TODO: |
| 74 | "setlocal define=^\\s*def |
| 75 | |
| 76 | setlocal comments=:# |
| 77 | setlocal commentstring=#\ %s |
| 78 | |
| 79 | if !exists("s:rubypath") |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 80 | 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 Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 84 | let s:code = "print ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,})" |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 85 | if &shellxquote == "'" |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 86 | let s:rubypath = system('ruby -e "' . s:code . '"') |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 87 | else |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 88 | let s:rubypath = system("ruby -e '" . s:code . "'") |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 89 | endif |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 90 | let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '') |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 91 | 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 |
| 96 | endif |
| 97 | |
| 98 | let &l:path = s:rubypath |
| 99 | |
| 100 | if has("gui_win32") && !exists("b:browsefilter") |
| 101 | let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" . |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 102 | \ "All Files (*.*)\t*.*\n" |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 103 | endif |
| 104 | |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 105 | let b:undo_ftplugin = "setl fo< inc< inex< sua< def< com< cms< path< kp<" |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 106 | \ "| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip" |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 107 | \ "| if exists('&ofu') && has('ruby') | setl ofu< | endif" |
| 108 | \ "| if has('balloon_eval') && exists('+bexpr') | setl bexpr< | endif" |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 109 | |
| 110 | let &cpo = s:cpo_save |
| 111 | unlet s:cpo_save |
| 112 | |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 113 | if exists("g:did_ruby_ftplugin_functions") |
| 114 | finish |
| 115 | endif |
| 116 | let g:did_ruby_ftplugin_functions = 1 |
| 117 | |
| 118 | function! 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 |
| 165 | endfunction |
| 166 | |
| 167 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 168 | " |
| 169 | " Instructions for enabling "matchit" support: |
| 170 | " |
| 171 | " 1. Look for the latest "matchit" plugin at |
| 172 | " |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 173 | " http://www.vim.org/scripts/script.php?script_id=39 |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 174 | " |
| 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 Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 184 | " filetype plugin on |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 185 | " |
| 186 | " 6. Restart Vim and create the matchit documentation: |
| 187 | " |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 188 | " :helptags ~/.vim/doc |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 189 | " |
| 190 | " Now you can do ":help matchit", and you should be able to use "%" on Ruby |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 191 | " keywords. Try ":echo b:match_words" to be sure. |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 192 | " |
Bram Moolenaar | 756ec0f | 2007-05-05 17:59:48 +0000 | [diff] [blame] | 193 | " Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 194 | " 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: |