Permalink
Browse files

Version c5b3e00: Initial upload

  • Loading branch information...
0 parents commit 7c1426884549daab3223c44a7a1da32b8d6ad557 @airblade airblade committed with Jul 23, 2012
Showing with 188 additions and 0 deletions.
  1. +13 −0 README
  2. +55 −0 README.mkd
  3. +120 −0 plugin/rooter.vim
13 README
@@ -0,0 +1,13 @@
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=4145
+
+See https://github.com/airblade/vim-rooter
+
+Rooter is a Vim plugin which changes the working directory to the project root
+when you open a file.
+
+More accurately, it'll do this when you open a typical webapp file. These files
+are identified by their extension.
+
+The project root is identified by the presence of a DVCS directory. Currently
+git, darcs, mercurial and bazaar are supported.
+
@@ -0,0 +1,55 @@
+# Rooter
+
+Rooter is a Vim plugin which changes the working directory to the project root
+when you open a file.
+
+More accurately, it'll do this when you open a typical webapp file. These files
+are identified by their extension.
+
+The project root is identified by the presence of a DVCS directory. Currently
+git, darcs, mercurial and bazaar are supported.
+
+
+## Use
+
+By default you don't need to do anything: vim-rooter will change the working
+directory automatically.
+
+You can always invoke vim-rooter manually with `<Leader>cd`.
+
+
+## Configuration
+
+You can change the manual-invocation mapping by adding this to your `.vimrc`:
+
+ map <silent> <unique> <Leader>foo <Plug>RooterChangeToRootDirectory
+
+where `<Leader>foo` is the mapping you want.
+
+You can change the file extensions which trigger vim-rooter with autocommands
+in `~/.vim/after/plugin/vim-rooter.vim`. You'll need to create this file, and
+possibly the directories, yourself.
+
+To add a file extension, e.g. foo:
+
+ autocmd rooter BufEnter *.foo :Rooter
+
+To remove a file extension, e.g. js:
+
+ autocmd! rooter BufEnter *.js
+
+To remove all the file extensions so vim-rooter doesn't change directory
+automatically at all:
+
+ autocmd! rooter
+
+Finally you can have vim-rooter locally change directory (`:lcd`) instead of
+changing directory (`:cd`) by adding this to your `.vimrc`:
+
+ let g:rooter_use_lcd = 1
+
+
+## Installation
+
+Install into `~/.vim/plugin/rooter.vim` or, if you're using Pathogen, into
+`~/.vim/bundle/rooter.vim`.
@@ -0,0 +1,120 @@
+" Vim plugin to change the working directory to the project root
+" (identified by the presence of a known SCM tool directory).
+"
+" Copyright 2010 Andrew Stewart, <boss@airbladesoftware.com>
+" Released under the MIT licence.
+"
+" This will happen automatically for typical Ruby webapp files.
+" If you don't want it to happen automatically, create the file
+" `.vim/after/plugin/vim-rooter.vim` with the single command:
+"
+" autocmd! rooter
+"
+" You can invoke it manually with <Leader>cd (usually \cd).
+" To change the mapping, put this in your .vimrc:
+"
+" map <silent> <unique> <Leader>foo <Plug>RooterChangeToRootDirectory
+"
+" ... where <Leader>foo is your preferred mapping.
+"
+" Options:
+" let g:rooter_use_lcd = 1
+" Use :lcd instead of :cd
+"
+
+
+"
+" Boilerplate
+"
+
+if exists("loaded_rooter")
+ finish
+endif
+let loaded_rooter = 1
+
+let s:save_cpo = &cpo
+set cpo&vim
+
+if !exists("g:rooter_use_lcd")
+ let g:rooter_use_lcd = 0
+endif
+
+"
+" Functions
+"
+
+" Find the root directory of the current file, i.e the closest parent directory
+" containing a <scm_type> directory, or an empty string if no such directory
+" is found.
+function! s:FindSCMDirectory(scm_type)
+ " Don't try to change directories when on a virtual filesystem (netrw, fugitive,...).
+ if match(expand('%:p'), '^\<.\+\>://.*') != -1
+ return ""
+ endif
+
+ let dir_current_file = expand("%:p:h")
+ let scm_dir = finddir(a:scm_type, dir_current_file . ";")
+ " If we're at the project root or we can't find one above us
+ if scm_dir == a:scm_type || empty(scm_dir)
+ return ""
+ else
+ return substitute(scm_dir, a:scm_type . "$", "", "")
+ endif
+endfunction
+
+" Returns the root directory for the current file based on the list of
+" known SCM directory names.
+function! s:FindRootDirectory()
+ " add any future tools here
+ let scm_list = ['.git', '_darcs', '.hg', '.bzr', '.svn']
+ for scmdir in scm_list
+ let result = s:FindSCMDirectory(scmdir)
+ if !empty(result)
+ return result
+ endif
+ endfor
+endfunction
+
+" Changes the current working directory to the current file's
+" root directory.
+function! s:ChangeToRootDirectory()
+ let root_dir = s:FindRootDirectory()
+ if !empty(root_dir)
+ if exists('+autochdir')
+ set noautochdir
+ endif
+ if g:rooter_use_lcd ==# 1
+ exe ":lcd " . root_dir
+ else
+ exe ":cd " . root_dir
+ endif
+ endif
+endfunction
+
+"
+" Mappings
+"
+
+if !hasmapto("<Plug>RooterChangeToRootDirectory")
+ map <silent> <unique> <Leader>cd <Plug>RooterChangeToRootDirectory
+endif
+noremap <unique> <script> <Plug>RooterChangeToRootDirectory <SID>ChangeToRootDirectory
+noremap <SID>ChangeToRootDirectory :call <SID>ChangeToRootDirectory()<CR>
+
+"
+" Commands
+"
+
+command! Rooter :call <SID>ChangeToRootDirectory()
+augroup rooter
+ autocmd!
+ autocmd BufEnter *.rb,*.html,*.haml,*.erb,*.rjs,*.css,*.js :Rooter
+augroup END
+
+"
+" Boilerplate
+"
+
+let &cpo = s:save_cpo
+
+" vim:set ft=vim sw=2 sts=2 et:

0 comments on commit 7c14268

Please sign in to comment.