Skip to content

Commit 1b76bc4

Browse files
committed
Add a workaround for using autochdir
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.
1 parent 0188d6f commit 1b76bc4

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

autoload/dirvish.vim

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ let s:sep = exists('+shellslash') && !&shellslash ? '\' : '/'
22
let s:noswapfile = (2 == exists(':noswapfile')) ? 'noswapfile' : ''
33
let s:noau = 'silent noautocmd keepjumps'
44

5+
" Can autochdir when <= 0, should set autochdir when < 0.
6+
let s:dirvish_autochdir_blocked_level = 0 - &autochdir
7+
58
function! s:msg_error(msg) abort
69
redraw | echohl ErrorMsg | echomsg 'dirvish:' a:msg | echohl None
710
endfunction
@@ -238,11 +241,23 @@ function! s:set_altbuf(bnr) abort
238241
if has('patch-7.4.605') | let @# = a:bnr | return | endif
239242

240243
let curbuf = bufnr('%')
244+
245+
set noautochdir
246+
let s:dirvish_autochdir_blocked_level += 1
247+
241248
if s:try_visit(a:bnr)
242249
let noau = bufloaded(curbuf) ? 'noau' : ''
243250
" Return to the current buffer.
244251
execute 'silent keepjumps' noau s:noswapfile 'buffer' curbuf
245252
endif
253+
254+
let s:dirvish_autochdir_blocked_level -= 1
255+
256+
if s:should_autochdir()
257+
" TODO: Using autochdir still breaks other things (buffername is sometimes
258+
" empty).
259+
"set autochdir
260+
endif
246261
endfunction
247262

248263
function! s:try_visit(bnr) abort
@@ -404,9 +419,17 @@ function! s:buf_isvalid(bnr) abort
404419
return bufexists(a:bnr) && !isdirectory(s:sl(bufname(a:bnr)))
405420
endfunction
406421

422+
function! s:should_autochdir() abort
423+
return s:dirvish_autochdir_blocked_level == -1
424+
endfunction
425+
426+
function! dirvish#can_autochdir() abort
427+
return s:dirvish_autochdir_blocked_level <= 0
428+
endfunction
429+
407430
function! dirvish#open(...) range abort
408431
if &autochdir
409-
call s:msg_error("'autochdir' is not supported")
432+
call s:msg_error("'autochdir' is not supported. See help for workaround.")
410433
return
411434
endif
412435
if !&hidden && &modified

doc/dirvish.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,13 @@ Check your 'isfname' setting. On Windows (where filepaths may contain
184184
backslashes "\") |fnamemodify()| may produce nonsense if 'isfname' does not
185185
contain the aforementioned unusual characters.
186186

187+
Is there a way to use 'autochdir'?~
188+
Since autochdir is not supported, you can use this workaround: >
189+
" 'autochdir' alternative to specify exceptions.
190+
" Switch to the directory of the current file, unless it's a help or conflicts with dirvish.
191+
autocmd BufEnter * if dirvish#can_autochdir() && match(['help', 'dirvish'], printf("\<%s\>", &ft)) < 0 && filereadable(expand("%")) | silent! cd %:p:h | endif
192+
<
193+
Alternatively, you can use |BufReadPost| instead of |BufEnter| to change directories only on load.
194+
187195
==============================================================================
188196
vim:tw=78:ts=4:et:ft=help:norl:

0 commit comments

Comments
 (0)