Browse files

Version 1.0: Initial upload

  • Loading branch information...
0 parents commit 9e29b54ceba31b6c2c9f18a5605b0b2a6df37f09 Tim Pope committed with Oct 13, 2012
Showing with 218 additions and 0 deletions.
  1. +26 −0 README
  2. +35 −0 doc/rvm.txt
  3. +157 −0 plugin/rvm.vim
26 README
@@ -0,0 +1,26 @@
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=4269
+
+Want to use RVM with Vim? You don't need a plugin to do that: Just start Vim from your RVM enabled shell and it will work. But say you started MacVim from Launchpad, or you started Vim with one version of Ruby and now you want another. That's where rvm.vim comes in.
+
+ :Rvm 1.9.2
+
+If you want to see the version that was chosen, use "use":
+
+ :Rvm use default
+
+If you leave off the version, it goes .rvmrc hunting relative to the current buffer.
+
+ :Rvm
+ :Rvm use
+
+If you really want to get crazy, you can make this happen automatically as you switch from buffer to buffer.
+
+ :autocmd BufEnter * Rvm
+
+You can also invoke any old command.
+
+ :Rvm install 1.9.3
+
+Add %{rvm#statusline()} to 'statusline' (or 'titlestring') to see the current Ruby version at all times.
+
+Report bugs at https://github.com/tpope/vim-rvm/issues.
35 doc/rvm.txt
@@ -0,0 +1,35 @@
+*rvm.txt* Switch Ruby versions from inside Vim
+
+Author: Tim Pope <http://tpo.pe/>
+License: Same terms as Vim itself (see |license|)
+
+This plugin is only available if 'compatible' is not set.
+
+COMMANDS *:Rvm*
+
+:Rvm Set the current Ruby version to the version found in
+ the .rvmrc for the current buffer. If no .rvmrc is
+ found, set it to the default version.
+
+:Rvm {version} Set the current Ruby version to {version}.
+
+:Rvm use [version] As above, but echo the version chosen.
+
+:Rvm {command} Run rvm with arbitrary arguments.
+
+To automatically switch Ruby versions when switching buffers:
+>
+ autocmd BufEnter * Rvm
+
+STATUSLINE *rvm#statusline()*
+
+Add rvm#statusline() to 'statusline' (or 'titlestring') to see the current
+version in your statusline (or title bar).
+
+ABOUT *rvm-about*
+
+Grab the latest version or report a bug on GitHub:
+
+https://github.com/tpope/vim-rvm
+
+ vim:tw=78:et:ft=help:norl:
157 plugin/rvm.vim
@@ -0,0 +1,157 @@
+" rvm.vim - Switch Ruby versions from inside Vim
+" Maintainer: Tim Pope <http://tpo.pe/>
+" Version: 1.0
+
+if exists('g:loaded_rvm') || v:version < 700 || &cp
+ finish
+endif
+
+if !exists('$rvm_path') && isdirectory(expand('~/.rvm'))
+ let $rvm_path = expand('~/.rvm')
+ let $PATH .= ':' . $rvm_path . '/bin'
+endif
+
+if !exists('$rvm_path')
+ finish
+endif
+
+let g:loaded_rvm = 1
+
+" Utility {{{1
+
+function! s:shellesc(arg) abort
+ if a:arg =~ '^[A-Za-z0-9_/.-]\+$'
+ return a:arg
+ else
+ return shellescape(a:arg)
+ endif
+endfunction
+
+" }}}1
+" :Rvm {{{1
+
+function! rvm#buffer_path_identifier(...)
+ let name = bufname(a:0 ? a:1 : '%')
+ if name ==# ''
+ let path = '.'
+ elseif isdirectory(name)
+ let path = name
+ else
+ let path = fnamemodify(name, ':h')
+ endif
+ return system('rvm tools path-identifier '.s:shellesc(path))
+endfunction
+
+function! s:Rvm(bang,...) abort
+ let path = split($PATH,':')
+ call filter(path, 'v:val[0:strlen($rvm_path)] !=# $rvm_path."/"')
+
+ if a:0 && a:0 < 3 && a:1 ==# 'use'
+ let use = 1
+ let args = a:000[1:-1]
+ else
+ let use = 0
+ let args = copy(a:000)
+ endif
+
+ if len(args) > 1 || (len(args) == 1 && args[0] !~ '^\%(@\|\d\|default\|j\=ruby\|goruby\|rbx\|ree\|kiji\|maglev\|ironruby\|system\)' && !use)
+ return '!rvm '.join(map(copy(a:000), 's:shellesc(v:val)'), ' ')
+ elseif !empty(args) && args[-1] ==# 'system'
+ let desired = 'system'
+ elseif !empty(args)
+ let desired = system('rvm tools strings '.s:shellesc(args[0]))[0:-2]
+ elseif use || !exists('b:rvm_string')
+ let desired = rvm#buffer_path_identifier()
+ else
+ let desired = b:rvm_string
+ endif
+
+ if desired ==# 'system'
+ let $RUBY_VERSION = ''
+ let $MY_RUBY_HOME = ''
+ let $IRBRC = expand('~/.irbrc')
+ let $PATH = join(path + [$rvm_path.'/bin'],':')
+ let $GEM_HOME = system('env -i PATH="'.$PATH.'" ruby -rubygems -e "print Gem.dir"')
+ let $GEM_PATH = system('env -i PATH="'.$PATH.'" ruby -rubygems -e "print Gem.path.join(%{:})"')
+ if use
+ return 'echomsg "Using system ruby"'
+ else
+ return ''
+ endif
+ endif
+
+ let ver = matchstr(desired,'[^@]*')
+ let gemset = matchstr(desired,'@.*')
+ if ver ==# ''
+ return 'echoerr "Ruby version not found"'
+ endif
+ if !isdirectory($rvm_path . '/rubies/' . ver)
+ if $rvm_install_on_use_flag
+ execute 'Rvm install '.ver
+ else
+ return 'echoerr "Ruby version not installed: :Rvm install ".'.string(ver)
+ endif
+ endif
+ let b:rvm_string = desired
+
+ let $RUBY_VERSION = ver
+ let $GEM_HOME = $rvm_path . '/gems/' . $RUBY_VERSION . gemset
+ let $MY_RUBY_HOME = $rvm_path . '/rubies/' . $RUBY_VERSION
+ let $IRBRC = $MY_RUBY_HOME . '/.irbrc'
+
+ let gemsets = [$GEM_HOME, $rvm_path . '/gems/' . $RUBY_VERSION . '@global']
+
+ let $GEM_PATH = join(gemsets, ':')
+ let $PATH = join(
+ \ [$MY_RUBY_HOME.'/bin'] +
+ \ map(gemsets,'v:val."/bin"') +
+ \ [$rvm_path.'/bin'] +
+ \ path, ':')
+ if use
+ return 'echomsg "Using " . $GEM_HOME'
+ else
+ return ''
+ endif
+endfunction
+
+function! s:Complete(A,L,P)
+ if a:A =~# '@'
+ let requested = matchstr(a:A,'^[^@]*')
+ let desired = system('rvm tools strings '.s:shellesc(requested))[0:-2]
+ let all = split(glob($rvm_path.'/gems/'.desired.'@*'),"\n")
+ call map(all,"v:val[strlen($rvm_path)+6:-1]")
+ call map(all,'substitute(v:val,"^[^@]*",requested,"")')
+ else
+ let all = split(glob($rvm_path.'/rubies/*'),"\n")
+ call map(all,"v:val[strlen($rvm_path)+8:-1]")
+ if a:A !~# '^r'
+ call map(all,'substitute(v:val,"^ruby-\\ze\\d","","")')
+ endif
+ endif
+ return join(all,"\n")
+endfunction
+
+command! -bar -nargs=* -complete=custom,s:Complete Rvm :execute s:Rvm(<bang>0,<f-args>)
+
+" }}}1
+" Statusline {{{1
+
+function! rvm#string()
+ return matchstr($GEM_HOME,'[^/]*$')
+endfunction
+
+function! rvm#statusline()
+ return substitute('['.rvm#string().']','^\[\]$','','')
+endfunction
+
+function! rvm#statusline_ft_ruby()
+ if &filetype ==# 'ruby'
+ return rvm#statusline()
+ else
+ return ''
+ endif
+endfunction
+
+" }}}1
+
+" vim:set sw=2 sts=2:

0 comments on commit 9e29b54

Please sign in to comment.