Skip to content

Commit 895eb33

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 a197607 commit 895eb33

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

autoload/dirvish.vim

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ let s:noswapfile = (2 == exists(':noswapfile')) ? 'noswapfile' : ''
44
let s:noau = 'silent noautocmd keepjumps'
55
let s:cb_map = {} " callback map
66

7+
" Can autochdir when <= 0, should set autochdir when < 0.
8+
let s:dirvish_autochdir_blocked_level = 0 - &autochdir
9+
710
function! s:msg_error(msg) abort
811
redraw | echohl ErrorMsg | echomsg 'dirvish:' a:msg | echohl None
912
endfunction
@@ -288,11 +291,23 @@ function! s:set_altbuf(bnr) abort
288291
if has('patch-7.4.605') | let @# = a:bnr | return | endif
289292

290293
let curbuf = bufnr('%')
294+
295+
set noautochdir
296+
let s:dirvish_autochdir_blocked_level += 1
297+
291298
if s:try_visit(a:bnr, 1)
292299
let noau = bufloaded(curbuf) ? 'noau' : ''
293300
" Return to the current buffer.
294301
execute 'silent keepjumps' noau s:noswapfile 'buffer' curbuf
295302
endif
303+
304+
let s:dirvish_autochdir_blocked_level -= 1
305+
306+
if s:should_autochdir()
307+
" TODO: Using autochdir still breaks other things (buffername is sometimes
308+
" empty).
309+
"set autochdir
310+
endif
296311
endfunction
297312

298313
function! s:try_visit(bnr, noau) abort
@@ -472,9 +487,17 @@ function! s:buf_isvalid(bnr) abort
472487
return bufexists(a:bnr) && !isdirectory(s:sl(bufname(a:bnr)))
473488
endfunction
474489

490+
function! s:should_autochdir() abort
491+
return s:dirvish_autochdir_blocked_level == -1
492+
endfunction
493+
494+
function! dirvish#can_autochdir() abort
495+
return s:dirvish_autochdir_blocked_level <= 0
496+
endfunction
497+
475498
function! dirvish#open(...) range abort
476499
if &autochdir
477-
call s:msg_error("'autochdir' is not supported")
500+
call s:msg_error("'autochdir' is not supported. See help for workaround.")
478501
return
479502
endif
480503
if !&autowriteall && !&hidden && &modified

doc/dirvish.txt

+8
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ Check your 'isfname' setting. On Windows (where filepaths may contain
231231
backslashes "\") |fnamemodify()| may produce nonsense if 'isfname' does not
232232
contain the aforementioned unusual characters.
233233

234+
Is there a way to use 'autochdir'?~
235+
Since autochdir is not supported, you can use this workaround: >
236+
" 'autochdir' alternative to specify exceptions.
237+
" Switch to the directory of the current file, unless it's a help or conflicts with dirvish.
238+
autocmd BufEnter * if dirvish#can_autochdir() && match(['help', 'dirvish'], printf("\<%s\>", &ft)) < 0 && filereadable(expand("%")) | silent! cd %:p:h | endif
239+
<
240+
Alternatively, you can use |BufReadPost| instead of |BufEnter| to change directories only on load.
241+
234242
How to override the netrw :Explore, :Sexplore, :Vexplore commands? ~
235243
Put this in your vimrc: >
236244

0 commit comments

Comments
 (0)