Skip to content

Commit

Permalink
When switching current directory, resource local vimrc files
Browse files Browse the repository at this point in the history
Idea by github.com/tarruda. Changes:
- Make it optional so that people can opt-out
- Only ource .vimrc once on startup if you do :enew by
  making LVRRecurseUp set s:last_cwd which is called the first time not
  by the au command, but by line 72 of plugin/localvimrc.vim
  • Loading branch information
tarruda authored and MarcWeber committed Sep 18, 2012
1 parent 16e952f commit a01ffbf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README
Expand Up @@ -9,6 +9,8 @@ Features:
calculates a hash before sourcing. If its unkown you must confirm sourcing
the file. The hash is updated automatcially if you write a local vimrc file.

- if you change a directory and edit a file the local vimrc files are resourced

USAGE:
========
create a .vimrc in your project directory and add
Expand Down Expand Up @@ -43,3 +45,7 @@ directory local .vimrc without walking up directory tree using vim builtin 'exrc
:h 'secure'
but 'secure' is not very secure, eg echo system('cat .vimrc') is executed
unless the file belongs to a different owner..

contributors
============
Thiago de Arruda (github.com/tarruba)
18 changes: 17 additions & 1 deletion plugin/localvimrc.vim
Expand Up @@ -8,6 +8,8 @@ let s:c.names = get(s:c,'names',['.vimrc'])

let s:c.hash_fun = get(s:c,'hash_fun','LVRHashOfFile')
let s:c.cache_file = get(s:c,'cache_file', $HOME.'/.vim_local_rc_cache')
let s:c.resource_on_cwd_change = get(s:c, 'resource_on_cwd_change', 1)
let s:last_cwd = ''

" very simple hash function using md5 falling back to VimL implementation
fun! LVRHashOfFile(file, seed)
Expand Down Expand Up @@ -40,7 +42,7 @@ endf

fun! LVRWithCache(F, args)
" for each computer use different unique seed based on time so that its
" horder to find collisions
" harder to find collisions
let cache = filereadable(s:c.cache_file)
\ ? eval(readfile(s:c.cache_file)[0])
\ : {'seed':localtime()}
Expand All @@ -52,6 +54,7 @@ endf

" find all local .vimrc in parent directories
fun! LVRRecurseUp(cache, dir, names)
let s:last_cwd = a:dir
let files = []
for n in a:names
let nr = 1
Expand All @@ -75,5 +78,18 @@ fun! LVRUpdateCache(cache)
endf

augroup LOCAL_VIMRC
" If the current file is a local .vimrc file and you're writing it
" automatically update the cache
autocmd BufWritePost * if index(s:c.names, expand('%:t')) >= 0 | call LVRWithCache('LVRUpdateCache', [] ) | endif

" If autochdir is not set, then resource local vimrc files if current
" directory has changed. There is no event for signaling change of current
" directory - so this is only an approximation to what people might expect.
" Idle events and the like would be an alternative
if ! &autochdir
autocmd BufNewFile,BufRead *
\ if s:c.resource_on_cwd_change && s:last_cwd != getcwd()
\ | call LVRWithCache('LVRRecurseUp', [getcwd(), s:c.names] )
\ | endif
endif
augroup end

0 comments on commit a01ffbf

Please sign in to comment.