Skip to content

Commit

Permalink
Add a workaround for using autochdir
Browse files Browse the repository at this point in the history
Related to justinmk#19

Allow the :cd on autocmd emulation of autochdir to work with dirvish.
Describe this workaround for autochdir-like behavior in the help.

This change might be a first step towards autochdir, but it's not enough
yet.

The autocmd breaks dirvish when we try to force an alternate file
because we're :cd-ing while dirvish is trying to do stuff and we end up
with an empty buffername. This change tracks when we're in set_altbuf so
the autocmd can be disabled during that region (using noau didn't work
and replacing set_altbuf with keepalt everywhere didn't work either).

Ideally, we detect if autochdir is enabled and re-enable it when we're
clear of set_altbuf, but it doesn't work yet.
  • Loading branch information
idbrii committed May 26, 2016
1 parent 0cfdd16 commit 883cd21
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 22 additions & 1 deletion autoload/dirvish.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ let s:sep = (&shell =~? 'cmd.exe') ? '\' : '/'
let s:noswapfile = (2 == exists(':noswapfile')) ? 'noswapfile' : ''
let s:noau = 'silent noautocmd keepjumps'

" Can autochdir when <= 0, should set autochdir when < 0.
let s:dirvish_autochdir_blocked_level = 0 - &autochdir

function! s:msg_error(msg) abort
redraw | echohl ErrorMsg | echomsg 'dirvish:' a:msg | echohl None
endfunction
Expand Down Expand Up @@ -219,11 +222,21 @@ function! s:open_selected(split_cmd, bg, line1, line2) abort
endfunction

function! s:set_altbuf(bnr) abort
set noautochdir
let s:dirvish_autochdir_blocked_level += 1

let curbuf = bufnr('%')
call s:try_visit(a:bnr)
let noau = bufloaded(curbuf) ? 'noau' : ''
" Return to the current buffer.
execute 'silent keepjumps' noau s:noswapfile 'buffer' curbuf

let s:dirvish_autochdir_blocked_level -= 1
if s:should_autochdir()
" TODO: Using autochdir still breaks other things (buffername is sometimes
" empty).
"set autochdir
endif
endfunction

function! s:try_visit(bnr) abort
Expand Down Expand Up @@ -375,9 +388,17 @@ function! s:buf_isvalid(bnr) abort
return bufexists(a:bnr) && !isdirectory(s:sl(bufname(a:bnr)))
endfunction

function! s:should_autochdir() abort
return s:dirvish_autochdir_blocked_level == -1
endfunction

function! dirvish#can_autochdir() abort
return s:dirvish_autochdir_blocked_level <= 0
endfunction

function! dirvish#open(...) range abort
if &autochdir
call s:msg_error("'autochdir' is not supported")
call s:msg_error("'autochdir' is not supported. See help for workaround.")
return
endif
if !&hidden && &modified
Expand Down
8 changes: 8 additions & 0 deletions doc/dirvish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,13 @@ Check your 'isfname' setting. On Windows (where filepaths may contain
backslashes "\") |fnamemodify()| may produce nonsense if 'isfname' does not
contain the aforementioned unusual characters.

Is there a way to use 'autochdir'?~
Since autochdir is not supported, you can use this workaround: >
" 'autochdir' alternative to specify exceptions.
" Switch to the directory of the current file, unless it's a help or conflicts with dirvish.
autocmd BufEnter * if dirvish#can_autochdir() && match(['help', 'dirvish'], printf("\<%s\>", &ft)) < 0 && filereadable(expand("%")) | silent! cd %:p:h | endif
<
Alternatively, you can use |BufReadPost| instead of |BufEnter| to change directories only on load.

==============================================================================
vim:tw=78:ts=8:ft=help:norl:

0 comments on commit 883cd21

Please sign in to comment.