Skip to content

Commit

Permalink
Fix multiple bugs and problems
Browse files Browse the repository at this point in the history
I know, this should be more commits but each time I tried to fix one
bug, another came up and I wasn't sure if a fix for the one problem does
not raise up new problems. So I had to fix them all in one and now it
would be a lot of work to split this commit into multiple ones.

Sorry for that.

Now, here is the list of changes/fixes:

- trailing slashes of directory names are removed
  In some environments (for me: ssh in chroot) the home directory
  stored in $HOME had a trailing slash, in other environments (for me:
  my normal desktop) there was no trailing slash. This showed very
  different behaviour between the two environments.
- the config applyment just skips the home directory but continues to
  search in the home's parent directories for configs to apply.
- another parameter `rootpath` is added in order to tell the script a
  root path where it should stop looking for configs. This was needed to
  successfully run the tests. Otherwise the plugin also sourced my
  config in my home directory because I was running the tests in a
  subdirectory of my home dir.
  • Loading branch information
mantiz committed Mar 3, 2012
1 parent 9709168 commit 1e1609d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
30 changes: 18 additions & 12 deletions autoload/dirsettings.vim
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,39 @@ function dirsettings#Install(...)
let l:fname = a:0 > 0 ? a:1 : '.vimrc'
let l:dname = a:0 > 1 ? a:2 : '.vim'
let l:augroup = a:0 > 2 ? a:3 : 'dirsettings'
let l:rootpath = a:0 > 3 ? a:4 : ''

execute 'augroup ' . l:augroup
au!
execute 'au BufNewFile * : call s:PrepareBuffer(''BufNewFile'', ''' . l:fname . ''', ''' . l:dname . ''')'
execute 'au BufEnter * : call s:PrepareBuffer(''BufEnter'', ''' . l:fname . ''', ''' . l:dname . ''')'
execute 'au BufNewFile * : call s:PrepareBuffer(''BufNewFile'', ''' . l:fname . ''', ''' . l:dname . ''', ''' . l:rootpath . ''')'
execute 'au BufEnter * : call s:PrepareBuffer(''BufEnter'', ''' . l:fname . ''', ''' . l:dname . ''', ''' . l:rootpath . ''')'
augroup END
endfunction

function s:PrepareBuffer(event, fname, dname)
function s:PrepareBuffer(event, fname, dname, rootpath)
if (exists('b:dirsettings_prepared'))
return
endif

let b:dirsettings_prepared = 1
call s:LoadDirectorySettings(a:fname, a:dname)

call s:LoadDirectorySettings(a:fname, a:dname, a:rootpath, substitute($HOME, '/\+$', '', ''))
execute 'doautocmd ' . a:event . ' <buffer>'
endfunction

function s:LoadDirectorySettings(fname, dname, ...)
let l:here = a:0 > 0 ? a:1 : expand('%:p:h')
function s:LoadDirectorySettings(fname, dname, rootpath, home, ...)
let l:here = substitute(a:0 > 0 ? a:1 : expand('%:p:h'), '/\+$', '', '')

let l:lastSegmentStart = match(l:here, '/[^/]\+$')
let l:isHomeDirectory = (stridx(l:here, a:home) == 0 && strlen(l:here) == strlen(a:home)) ? 1 : 0

let l:lastSegmentStart = match(l:here, '/[^/]\+$')
let l:isHomeDirectory = (stridx(l:here, $HOME) == 0 && strlen(l:here) == strlen($HOME)) ? 1 : 0
if (l:lastSegmentStart > -1 && l:here != a:rootpath)
call s:LoadDirectorySettings(a:fname, a:dname, a:rootpath, a:home, strpart(l:here, 0, l:lastSegmentStart))
endif

if (l:lastSegmentStart > -1 && !l:isHomeDirectory)
call s:LoadDirectorySettings(a:fname, a:dname, strpart(l:here, 0, l:lastSegmentStart))
call s:ApplyLocalConfiguration(a:fname, a:dname, l:here)
endif
if (!l:isHomeDirectory)
call s:ApplyLocalConfiguration(a:fname, a:dname, l:here)
endif
endfunction

function s:ApplyLocalConfiguration(fname, dname, path)
Expand All @@ -74,3 +79,4 @@ function s:ApplyLocalConfiguration(fname, dname, path)
exec 'source ' . l:fullfname
endif
endfunction

2 changes: 1 addition & 1 deletion tests/environment/.vimrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source ../../autoload/dirsettings.vim
call dirsettings#Install()
call dirsettings#Install('.vimrc', '.vim', 'dirsettings', substitute(expand("<sfile>:p:h"), '/[^/]\+$', '', '') . '/data')

let g:dirsettingsVimRcVariable="set-by-vimrc"

0 comments on commit 1e1609d

Please sign in to comment.