From 883cd2128b7681d414b81a46d886947d22ebeab6 Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Wed, 18 May 2016 15:15:37 -0700 Subject: [PATCH] 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. --- autoload/dirvish.vim | 23 ++++++++++++++++++++++- doc/dirvish.txt | 8 ++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/autoload/dirvish.vim b/autoload/dirvish.vim index 3225e03..538c070 100644 --- a/autoload/dirvish.vim +++ b/autoload/dirvish.vim @@ -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 @@ -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 @@ -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 diff --git a/doc/dirvish.txt b/doc/dirvish.txt index 1fd23e0..df63572 100644 --- a/doc/dirvish.txt +++ b/doc/dirvish.txt @@ -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: