Skip to content

Commit 22cb7a3

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 c76e3d3 commit 22cb7a3

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

autoload/dirvish.vim

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ let s:sep = (&shell =~? 'cmd.exe') ? '\' : '/'
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
@@ -219,11 +222,21 @@ function! s:open_selected(split_cmd, bg, line1, line2) abort
219222
endfunction
220223

221224
function! s:set_altbuf(bnr) abort
225+
set noautochdir
226+
let s:dirvish_autochdir_blocked_level += 1
227+
222228
let curbuf = bufnr('%')
223229
call s:try_visit(a:bnr)
224230
let noau = bufloaded(curbuf) ? 'noau' : ''
225231
" Return to the current buffer.
226232
execute 'silent keepjumps' noau s:noswapfile 'buffer' curbuf
233+
234+
let s:dirvish_autochdir_blocked_level -= 1
235+
if s:should_autochdir()
236+
" TODO: Using autochdir still breaks other things (buffername is sometimes
237+
" empty).
238+
"set autochdir
239+
endif
227240
endfunction
228241

229242
function! s:try_visit(bnr) abort
@@ -375,9 +388,17 @@ function! s:buf_isvalid(bnr) abort
375388
return bufexists(a:bnr) && !isdirectory(s:sl(bufname(a:bnr)))
376389
endfunction
377390

391+
function! s:should_autochdir() abort
392+
return s:dirvish_autochdir_blocked_level == -1
393+
endfunction
394+
395+
function! dirvish#can_autochdir() abort
396+
return s:dirvish_autochdir_blocked_level <= 0
397+
endfunction
398+
378399
function! dirvish#open(...) range abort
379400
if &autochdir
380-
call s:msg_error("'autochdir' is not supported")
401+
call s:msg_error("'autochdir' is not supported. See help for workaround.")
381402
return
382403
endif
383404
if !&hidden && &modified

doc/dirvish.txt

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

171+
Is there a way to use 'autochdir'?~
172+
Since autochdir is not supported, you can use this workaround: >
173+
" 'autochdir' alternative to specify exceptions.
174+
" Switch to the directory of the current file, unless it's a help or conflicts with dirvish.
175+
autocmd BufEnter * if dirvish#can_autochdir() && match(['help', 'dirvish'], &ft) < 0 && filereadable(expand("%")) | silent! cd %:p:h | endif
176+
<
177+
Alternatively, you can use |BufReadPost| instead of |BufEnter| to change directories only on load.
178+
171179
==============================================================================
172180
vim:tw=78:ts=8:ft=help:norl:

0 commit comments

Comments
 (0)