Skip to content

Instantly share code, notes, and snippets.

@lygaret
Created July 8, 2010 05:08
Show Gist options
  • Save lygaret/467659 to your computer and use it in GitHub Desktop.
Save lygaret/467659 to your computer and use it in GitHub Desktop.
vimplugin which looks for a .project.vim file up the dir tree for peoject specific vim settings
" Name: vimproject.vim
" Version: 0.1
" Author: Jon Raphaelson
" Summary: Loads a project specific vimrc when starting vim inside that
" project's directory tree.
" Licence: This program is free software; you can redistribute it and/or
" modify it under the terms of the GNU General Public License.
" See http://www.gnu.org/copyleft/gpl.txt
" Section: Documentation {{{1
" Description:
"
" This plugins searches for a .project.vim file in the directory
" structure above and including the current directory, when vim
" starts. If it finds one, it will ask whether to load the project,
" and if so, sources that config file. This allows you to have project
" specific settings in vim, without directory hacks in your vimrc.
"
" Installation:
"
" Copy the vimproject.vim file to the $HOME/.vim/plugin directory.
" Refer to ':help add-plugin', ':help add-global-plugin' and ':help
" runtimepath' for more details about Vim plugins.
"
" You can change the following variables to change certain behavior
"
" Variables:
"
" g:vimproject_disable
" Disables loading completely.
" Deaults to 0
"
" g:vimproject_filename
" Filename of local vimrc files.
" Defaults to ".project.vim".
"
" g:vimproject_depth
" Limits the number of parent directories that will be searched.
" Defaults to -1 (no limit)
"
" g:vimproject_asksource
" Ask before sourcing any project settings file.
" Defaults to 1.
"
" Credits:
" Heavily based on both:
" - dirsettings.vim (by Tye Zdrojewski)
" - localvimrc.vim (by Markus Braun)
"
" Section: Plugin Config {{{1
" only load once, and only in vim 7+
if (exists("g:vimproject_loaded")) || version < 700
finish
endif
let g:vimproject_loaded = "Version 1.0"
" default the variables
if (!exists("g:vimproject_disable"))
let g:vimproject_disable = 0
endif
if (!exists("g:vimproject_filename"))
let g:vimproject_filename = ".project.vim"
endif
if (!exists("g:vimproject_depth"))
let g:vimproject_depth = -1
endif
if (!exists("g:vimproject_asksource"))
let g:vimproject_asksource = 1
endif
" Section: Functions {{{1
" Function: s:findconfig {{{2
" Search up to g:vimproject_depth parent directories for a project settings file,
" and return the full path to the first one found.
"
function! s:findconfig()
if g:vimproject_depth >= 0
let l:stopdirectory = ";" . fnamemodify(expand("%"), ":p" . repeat(":h", g:vimproject_depth + 1)) . "/"
else
let l:stopdirectory = ";"
endif
let l:directory = fnamemodify(expand("%"), ":p:h")
echom l:directory . l:stopdirectory
let l:projfile = findfile(g:vimproject_filename, l:directory . l:stopdirectory)
if l:projfile != ""
return l:projfile
else
return ""
end
endfunction
" Function: s:sourceconfig {{{2
" Source the config file found, or do nothing is nothing is found.
function! s:sourceconfig()
let l:projfile = s:findconfig()
if l:projfile == ""
return
endif
let l:directory = fnamemodify(l:projfile, ":p:h")
let l:dosource = 1
if g:vimproject_asksource
let l:answer = input("Load project at " . l:directory . " (y/n)? ", "y")
if (l:answer == "n")
let l:dosource = 0
endif
endif
if l:dosource
exec "source " . l:projfile
echom "Project at " . l:directory . " loaded."
endif
endfunction
au BufEnter * call s:sourceconfig()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment