vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

ruby.vim (18098B)


      1 " Vim filetype plugin
      2 " Language:		Ruby
      3 " Maintainer:		Tim Pope <vimNOSPAM@tpope.org>
      4 " URL:			https://github.com/vim-ruby/vim-ruby
      5 " Last Change:		2023 Dec 31
      6 "			2024 Jan 14 by Vim Project (browsefilter)
      7 "			2026 Jun 27 by Vim Project (normalize recommended style guard)
      8 
      9 if (exists("b:did_ftplugin"))
     10   finish
     11 endif
     12 let b:did_ftplugin = 1
     13 
     14 let s:cpo_save = &cpo
     15 set cpo&vim
     16 
     17 if has("gui_running") && !has("gui_win32")
     18   setlocal keywordprg=ri\ -T\ -f\ bs
     19 else
     20   setlocal keywordprg=ri
     21 endif
     22 
     23 " Matchit support
     24 if exists("loaded_matchit") && !exists("b:match_words")
     25   let b:match_ignorecase = 0
     26 
     27   let b:match_words =
     28 	\ '{\|\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|=\@<!begin\)\>=\@!' .
     29 	\ ':' .
     30 	\ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
     31 	\ ':' .
     32         \ '}\|\%(^\|[^.\:@$=]\)\@<=\<end\:\@!\>' .
     33         \ ',^=begin\>:^=end\>,' .
     34 	\ ',\[:\],(:)'
     35 
     36   let b:match_skip =
     37 	\ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
     38 	\ "\\<ruby\\%(String\\|.\+Delimiter\\|Character\\|.\+Escape\\|" .
     39         \ "Regexp\\|Interpolation\\|Comment\\|Documentation\\|" .
     40 	\ "ConditionalModifier\\|RepeatModifier\\|RescueModifier\\|OptionalDo\\|" .
     41 	\ "MethodName\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
     42 	\ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
     43 endif
     44 
     45 setlocal formatoptions-=t formatoptions+=croql
     46 
     47 setlocal include=^\\s*\\<\\(load\\>\\\|require\\>\\\|autoload\\s*:\\=[\"']\\=\\h\\w*[\"']\\=,\\)
     48 setlocal suffixesadd=.rb
     49 
     50 if exists("&ofu") && has("ruby")
     51   setlocal omnifunc=rubycomplete#Complete
     52 endif
     53 
     54 " TODO:
     55 "setlocal define=^\\s*def
     56 
     57 setlocal comments=b:#
     58 setlocal commentstring=#\ %s
     59 
     60 if !exists('g:ruby_version_paths')
     61   let g:ruby_version_paths = {}
     62 endif
     63 
     64 let s:path_split = has('win32') ? ';' : ':'
     65 
     66 function! s:query_path(root) abort
     67   " Disabled by default for security reasons.
     68   if !get(g:, 'ruby_exec', get(g:, 'plugin_exec', 0)) || empty(a:root)
     69     return map(split($RUBYLIB, s:path_split), 'v:val ==# "." ? "" : v:val')
     70   endif
     71   let code = "print $:.join %q{,}"
     72   if &shellxquote == "'"
     73     let args = ' --disable-gems -e "' . code . '"'
     74   else
     75     let args = " --disable-gems -e '" . code . "'"
     76   endif
     77 
     78   let cd = haslocaldir() ? 'lcd' : exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd'
     79   let cwd = fnameescape(getcwd())
     80   try
     81     exe cd fnameescape(a:root)
     82     for dir in split($PATH, s:path_split)
     83       if dir !=# '.' && executable(dir . '/ruby') == 1
     84 	let exepath = dir . '/ruby'
     85 	break
     86       endif
     87     endfor
     88     if exists('l:exepath')
     89       let path = split(system(exepath . args),',')
     90       if v:shell_error
     91 	let path = []
     92       endif
     93     else
     94       let path = []
     95     endif
     96     exe cd cwd
     97     return path
     98   finally
     99     exe cd cwd
    100   endtry
    101 endfunction
    102 
    103 function! s:build_path(path) abort
    104   let path = join(map(copy(a:path), 'v:val ==# "." ? "" : v:val'), ',')
    105   if &g:path =~# '\v^%(\.,)=%(/%(usr|emx)/include,)=,$'
    106     let path = path . ',.,,'
    107   elseif &g:path =~# ',\.,,$'
    108     let path = &g:path[0:-4] . path . ',.,,'
    109   elseif &g:path =~# ',,$'
    110     let path = &g:path[0:-2] . path . ',,'
    111   else
    112     let path = substitute(&g:path, '[^,]\zs$', ',', '') . path
    113   endif
    114   return path
    115 endfunction
    116 
    117 if !exists('b:ruby_version') && !exists('g:ruby_path') && isdirectory(expand('%:p:h'))
    118   let s:version_file = findfile('.ruby-version', '.;')
    119   if !empty(s:version_file) && filereadable(s:version_file)
    120     let b:ruby_version = get(readfile(s:version_file, '', 1), '')
    121     if !has_key(g:ruby_version_paths, b:ruby_version)
    122       let g:ruby_version_paths[b:ruby_version] = s:query_path(fnamemodify(s:version_file, ':p:h'))
    123     endif
    124   endif
    125 endif
    126 
    127 if exists("g:ruby_path")
    128   let s:ruby_path = type(g:ruby_path) == type([]) ? join(g:ruby_path, ',') : g:ruby_path
    129 elseif has_key(g:ruby_version_paths, get(b:, 'ruby_version', ''))
    130   let s:ruby_paths = g:ruby_version_paths[b:ruby_version]
    131   let s:ruby_path = s:build_path(s:ruby_paths)
    132 else
    133   if !exists('g:ruby_default_path')
    134     if has("ruby") && has("win32")
    135       ruby ::VIM::command( 'let g:ruby_default_path = split("%s",",")' % $:.join(%q{,}) )
    136     else
    137       let g:ruby_default_path = s:query_path($HOME)
    138     endif
    139   endif
    140   let s:ruby_paths = g:ruby_default_path
    141   let s:ruby_path = s:build_path(s:ruby_paths)
    142 endif
    143 
    144 if stridx(&l:path, s:ruby_path) == -1
    145   let &l:path = s:ruby_path
    146 endif
    147 if exists('s:ruby_paths') && stridx(&l:tags, join(map(copy(s:ruby_paths),'v:val."/tags"'),',')) == -1
    148   let &l:tags = &tags . ',' . join(map(copy(s:ruby_paths),'v:val."/tags"'),',')
    149 endif
    150 
    151 if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
    152   let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n"
    153   if has("win32")
    154     let b:browsefilter .= "All Files (*.*)\t*\n"
    155   else
    156     let b:browsefilter .= "All Files (*)\t*\n"
    157   endif
    158 endif
    159 
    160 let b:undo_ftplugin = "setl inc= sua= path= tags= fo< com< cms< kp="
    161       \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
    162       \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
    163 
    164 if get(g:, 'ruby_recommended_style',
    165       \ get(g:, 'filetype_recommended_style', 1))
    166   setlocal shiftwidth=2 softtabstop=2 expandtab
    167   let b:undo_ftplugin .= ' | setl sw< sts< et<'
    168 endif
    169 
    170 " To activate, :set ballooneval
    171 if exists('+balloonexpr') && get(g:, 'ruby_balloonexpr')
    172   setlocal balloonexpr=RubyBalloonexpr()
    173   let b:undo_ftplugin .= "| setl bexpr="
    174 endif
    175 
    176 function! s:map(mode, flags, map) abort
    177   let from = matchstr(a:map, '\S\+')
    178   if empty(mapcheck(from, a:mode))
    179     exe a:mode.'map' '<buffer>' a:flags a:map
    180     let b:undo_ftplugin .= '|sil! '.a:mode.'unmap <buffer> '.from
    181   endif
    182 endfunction
    183 
    184 cmap <buffer><script><expr> <Plug><ctag> substitute(RubyCursorTag(),'^$',"\022\027",'')
    185 cmap <buffer><script><expr> <Plug><cfile> substitute(RubyCursorFile(),'^$',"\022\006",'')
    186 let b:undo_ftplugin .= "| sil! cunmap <buffer> <Plug><ctag>| sil! cunmap <buffer> <Plug><cfile>"
    187 
    188 if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
    189   nmap <buffer><script> <SID>:  :<C-U>
    190   nmap <buffer><script> <SID>c: :<C-U><C-R>=v:count ? v:count : ''<CR>
    191   cmap <buffer> <SID><cfile> <Plug><cfile>
    192   cmap <buffer> <SID><ctag>  <Plug><ctag>
    193 
    194   nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','n')<CR>
    195   nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','n')<CR>
    196   nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','n')<CR>
    197   nnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','n')<CR>
    198   xnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','v')<CR>
    199   xnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','v')<CR>
    200   xnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','v')<CR>
    201   xnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','v')<CR>
    202 
    203   nnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','n')<CR>
    204   nnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','n')<CR>
    205   nnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','n')<CR>
    206   nnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','n')<CR>
    207   xnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','v')<CR>
    208   xnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','v')<CR>
    209   xnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','v')<CR>
    210   xnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','v')<CR>
    211 
    212   let b:undo_ftplugin = b:undo_ftplugin
    213         \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
    214         \."| sil! exe 'unmap <buffer> [m' | sil! exe 'unmap <buffer> ]m' | sil! exe 'unmap <buffer> [M' | sil! exe 'unmap <buffer> ]M'"
    215 
    216   if maparg('im','x') == '' && maparg('im','o') == '' && maparg('am','x') == '' && maparg('am','o') == ''
    217     onoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
    218     onoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
    219     xnoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
    220     xnoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
    221     let b:undo_ftplugin = b:undo_ftplugin
    222           \."| sil! exe 'ounmap <buffer> im' | sil! exe 'ounmap <buffer> am'"
    223           \."| sil! exe 'xunmap <buffer> im' | sil! exe 'xunmap <buffer> am'"
    224   endif
    225 
    226   if maparg('iM','x') == '' && maparg('iM','o') == '' && maparg('aM','x') == '' && maparg('aM','o') == ''
    227     onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
    228     onoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
    229     xnoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
    230     xnoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
    231     let b:undo_ftplugin = b:undo_ftplugin
    232           \."| sil! exe 'ounmap <buffer> iM' | sil! exe 'ounmap <buffer> aM'"
    233           \."| sil! exe 'xunmap <buffer> iM' | sil! exe 'xunmap <buffer> aM'"
    234   endif
    235 
    236   call s:map('c', '', '<C-R><C-F> <Plug><cfile>')
    237 
    238   cmap <buffer><script><expr> <SID>tagzv &foldopen =~# 'tag' ? '<Bar>norm! zv' : ''
    239   call s:map('n', '<script><silent>', '<C-]>       <SID>:exe  v:count1."tag <SID><ctag>"<SID>tagzv<CR>')
    240   call s:map('n', '<script><silent>', 'g<C-]>      <SID>:exe         "tjump <SID><ctag>"<SID>tagzv<CR>')
    241   call s:map('n', '<script><silent>', 'g]          <SID>:exe       "tselect <SID><ctag>"<SID>tagzv<CR>')
    242   call s:map('n', '<script><silent>', '<C-W>]      <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
    243   call s:map('n', '<script><silent>', '<C-W><C-]>  <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
    244   call s:map('n', '<script><silent>', '<C-W>g<C-]> <SID>:exe        "stjump <SID><ctag>"<SID>tagzv<CR>')
    245   call s:map('n', '<script><silent>', '<C-W>g]     <SID>:exe      "stselect <SID><ctag>"<SID>tagzv<CR>')
    246   call s:map('n', '<script><silent>', '<C-W>}      <SID>:exe v:count1."ptag <SID><ctag>"<CR>')
    247   call s:map('n', '<script><silent>', '<C-W>g}     <SID>:exe        "ptjump <SID><ctag>"<CR>')
    248 
    249   call s:map('n', '<script><silent>', 'gf           <SID>c:find <SID><cfile><CR>')
    250   call s:map('n', '<script><silent>', '<C-W>f      <SID>c:sfind <SID><cfile><CR>')
    251   call s:map('n', '<script><silent>', '<C-W><C-F>  <SID>c:sfind <SID><cfile><CR>')
    252   call s:map('n', '<script><silent>', '<C-W>gf   <SID>c:tabfind <SID><cfile><CR>')
    253 endif
    254 
    255 let &cpo = s:cpo_save
    256 unlet s:cpo_save
    257 
    258 if exists("g:did_ruby_ftplugin_functions")
    259   finish
    260 endif
    261 let g:did_ruby_ftplugin_functions = 1
    262 
    263 function! RubyBalloonexpr() abort
    264   if !exists('s:ri_found')
    265     let s:ri_found = executable('ri')
    266   endif
    267   if s:ri_found
    268     let line = getline(v:beval_lnum)
    269     let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
    270     let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
    271     let str = b.a
    272     let before = strpart(line,0,v:beval_col-strlen(b))
    273     let after  = strpart(line,v:beval_col+strlen(a))
    274     if str =~ '^\.'
    275       let str = substitute(str,'^\.','#','g')
    276       if before =~ '\]\s*$'
    277         let str = 'Array'.str
    278       elseif before =~ '}\s*$'
    279         " False positives from blocks here
    280         let str = 'Hash'.str
    281       elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
    282         let str = 'String'.str
    283       elseif before =~ '\$\d\+\.\d\+\s*$'
    284         let str = 'Float'.str
    285       elseif before =~ '\$\d\+\s*$'
    286         let str = 'Integer'.str
    287       elseif before =~ '/\s*$'
    288         let str = 'Regexp'.str
    289       else
    290         let str = substitute(str,'^#','.','')
    291       endif
    292     endif
    293     let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
    294     let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
    295     let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
    296     let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
    297     let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
    298     let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
    299     if str !~ '^\w'
    300       return ''
    301     endif
    302     silent! let res = substitute(system("ri -f rdoc -T \"".str.'"'),'\n$','','')
    303     if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
    304       return ''
    305     endif
    306     return res
    307   else
    308     return ""
    309   endif
    310 endfunction
    311 
    312 function! s:searchsyn(pattern, syn, flags, mode) abort
    313   let cnt = v:count1
    314   norm! m'
    315   if a:mode ==# 'v'
    316     norm! gv
    317   endif
    318   let i = 0
    319   call map(a:syn, 'hlID(v:val)')
    320   while i < cnt
    321     let i = i + 1
    322     let line = line('.')
    323     let col  = col('.')
    324     let pos = search(a:pattern,'W'.a:flags)
    325     while pos != 0 && index(a:syn, s:synid()) < 0
    326       let pos = search(a:pattern,'W'.a:flags)
    327     endwhile
    328     if pos == 0
    329       call cursor(line,col)
    330       return
    331     endif
    332   endwhile
    333 endfunction
    334 
    335 function! s:synid() abort
    336   return synID(line('.'),col('.'),0)
    337 endfunction
    338 
    339 function! s:wrap_i(back,forward) abort
    340   execute 'norm! k'
    341   execute 'norm '.a:forward
    342   let line = line('.')
    343   execute 'norm '.a:back
    344   if line('.') == line - 1
    345     return s:wrap_a(a:back,a:forward)
    346   endif
    347   execute 'norm! jV'
    348   execute 'norm '.a:forward
    349   execute 'norm! k'
    350 endfunction
    351 
    352 function! s:wrap_a(back,forward) abort
    353   execute 'norm '.a:forward
    354   if line('.') < line('$') && getline(line('.')+1) ==# ''
    355     let after = 1
    356   endif
    357   execute 'norm '.a:back
    358   while getline(line('.')-1) =~# '^\s*#' && line('.')
    359     -
    360   endwhile
    361   if exists('after')
    362     execute 'norm! V'
    363     execute 'norm '.a:forward
    364     execute 'norm! j'
    365   elseif line('.') > 1 && getline(line('.')-1) =~# '^\s*$'
    366     execute 'norm! kV'
    367     execute 'norm '.a:forward
    368   else
    369     execute 'norm! V'
    370     execute 'norm '.a:forward
    371   endif
    372 endfunction
    373 
    374 function! RubyCursorIdentifier() abort
    375   let asciicode    = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
    376   let number       = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
    377   let operator     = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
    378   let method       = '\%(\.[_a-zA-Z]\w*\s*=>\@!\|\<[_a-zA-Z]\w*\>[?!]\=\)'
    379   let global       = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
    380   let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
    381   let pattern      = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
    382   let [lnum, col]  = searchpos(pattern,'bcn',line('.'))
    383   let raw          = matchstr(getline('.')[col-1 : ],pattern)
    384   let stripped     = substitute(substitute(raw,'\s\+=$','=',''),'^\s*[:.]\=','','')
    385   return stripped == '' ? expand("<cword>") : stripped
    386 endfunction
    387 
    388 function! RubyCursorTag() abort
    389   return substitute(RubyCursorIdentifier(), '^[$@]*', '', '')
    390 endfunction
    391 
    392 function! RubyCursorFile() abort
    393   let isfname = &isfname
    394   try
    395     set isfname+=:
    396     let cfile = expand('<cfile>')
    397   finally
    398     let isfname = &isfname
    399   endtry
    400   let pre = matchstr(strpart(getline('.'), 0, col('.')-1), '.*\f\@<!')
    401   let post = matchstr(strpart(getline('.'), col('.')), '\f\@!.*')
    402   if s:synid() ==# hlID('rubyConstant')
    403     let cfile = substitute(cfile,'\.\w\+[?!=]\=$','','')
    404     let cfile = substitute(cfile,'^::','','')
    405     let cfile = substitute(cfile,'::','/','g')
    406     let cfile = substitute(cfile,'\(\u\+\)\(\u\l\)','\1_\2', 'g')
    407     let cfile = substitute(cfile,'\(\l\|\d\)\(\u\)','\1_\2', 'g')
    408     return tolower(cfile) . '.rb'
    409   elseif getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
    410     let cfile = expand('%:p:h') . '/' . matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
    411     let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
    412   elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
    413     let target = matchstr(getline('.'),'\(["'']\)\.\.\zs/.\{-\}\ze\1')
    414     let cfile = expand('%:p:h') . target
    415     let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
    416   elseif getline('.') =~# '^\s*\%(require \|load \|autoload :\w\+,\)\s*\(["'']\).*\1\s*$'
    417     let cfile = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
    418     let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
    419   elseif pre.post =~# '\<File.expand_path[( ].*[''"]\{2\}, *__FILE__\>' && cfile =~# '^\.\.'
    420     let cfile = expand('%:p:h') . strpart(cfile, 2)
    421   else
    422     return substitute(cfile, '\C\v^(.*):(\d+)%(:in)=$', '+\2 \1', '')
    423   endif
    424   let cwdpat = '^\M' . substitute(getcwd(), '[\/]', '\\[\\/]', 'g').'\ze\[\/]'
    425   let cfile = substitute(cfile, cwdpat, '.', '')
    426   if fnameescape(cfile) !=# cfile
    427     return '+ '.fnameescape(cfile)
    428   else
    429     return cfile
    430   endif
    431 endfunction
    432 
    433 "
    434 " Instructions for enabling "matchit" support:
    435 "
    436 " 1. Look for the latest "matchit" plugin at
    437 "
    438 "         http://www.vim.org/scripts/script.php?script_id=39
    439 "
    440 "    It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
    441 "
    442 " 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
    443 "
    444 " 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
    445 "
    446 " 4. Ensure this file (ftplugin/ruby.vim) is installed.
    447 "
    448 " 5. Ensure you have this line in your $HOME/.vimrc:
    449 "         filetype plugin on
    450 "
    451 " 6. Restart Vim and create the matchit documentation:
    452 "
    453 "         :helptags ~/.vim/doc
    454 "
    455 "    Now you can do ":help matchit", and you should be able to use "%" on Ruby
    456 "    keywords.  Try ":echo b:match_words" to be sure.
    457 "
    458 " Thanks to Mark J. Reed for the instructions.  See ":help vimrc" for the
    459 " locations of plugin directories, etc., as there are several options, and it
    460 " differs on Windows.  Email gsinclair@soyabean.com.au if you need help.
    461 "
    462 
    463 " vim: nowrap sw=2 sts=2 ts=8: