diff --git a/plugin/dirdiff.vim b/plugin/dirdiff.vim index 2389f7a..ea139ab 100644 --- a/plugin/dirdiff.vim +++ b/plugin/dirdiff.vim @@ -414,21 +414,31 @@ function ToggleHex() endfunction function! DirDiffHexmode() - wincmd k - call ToggleHex() - wincmd l - call ToggleHex() + " Support hex mode only for existing files (not directories). + if filereadable(s:FilenameA) + call GotoFileAWindow() + call ToggleHex() + endif + if filereadable(s:FilenameB) + call GotoFileBWindow() + call ToggleHex() + endif " Go back to the diff window - wincmd j + call GotoDiffWindow() endfunction function! DirDiffWrapmode() - wincmd k - setlocal wrap! - wincmd l - setlocal wrap! + " Support wrap mode only for existing files (not directories). + if filereadable(s:FilenameA) + call GotoFileAWindow() + setlocal wrap! + endif + if filereadable(s:FilenameB) + call GotoFileBWindow() + setlocal wrap! + endif " Go back to the diff window - wincmd j + call GotoDiffWindow() endfunction function! EscapeFileName(path) @@ -455,10 +465,30 @@ function! Drop(fname) if winid > 0 call win_gotoid(winid) else - exe 'edit ' a:fname + " If the Netrw plugin is configured in tree style listing mode + " (i.e. g:netrw_liststyle = 3), then a buffer named 'NetrwTreeListing' + " is shown (i.e. active window). This happens only after the original + " buffer, which has the directory as its name, is focused for the first + " time. We work around this by comparing the 'netrw_curdir' variable of + " the 'NetrwTreeListing' buffer against the directory name. + let netrw_buffer = bufnr("NetrwTreeListing") + let netrw_winid = bufwinid(netrw_buffer) + if netrw_winid > 0 && a:fname == getbufvar(netrw_buffer, "netrw_curdir") + call win_gotoid(netrw_winid) + else + exe 'edit ' a:fname + endif endif endfunction +function! GotoFileAWindow() + call Drop(s:FilenameA) +endfunction + +function! GotoFileBWindow() + call Drop(s:FilenameB) +endfunction + function! GotoDiffWindow() call Drop(s:FilenameDiffWindow) endfunction @@ -511,7 +541,11 @@ function! DirDiffOpen() let previousFile = (s:LastMode == "A") ? previousFileA : previousFileB call Drop(previousFile) silent exec "edit ".fileToOpen - silent exec "bd ".bufnr(previousFile) + " The Netrw plugin creates an 'unlisted' buffer for directories. + " Running 'bd {buffer}' on an unlisted buffer produces the following error: + " E516: No buffers were deleted. + " Suppress the error with 'silent!'. + silent! exec "bd ".bufnr(previousFile) endif else silent exec "split ".fileToOpen @@ -541,7 +575,11 @@ function! DirDiffOpen() let previousFile = (s:LastMode == "A") ? previousFileA : previousFileB call Drop(previousFile) silent exec "edit ".s:FilenameB - silent exec "bd ".bufnr(previousFile) + " The Netrw plugin creates an 'unlisted' buffer for directories. + " Running 'bd {buffer}' on an unlisted buffer produces the following error: + " E516: No buffers were deleted. + " Suppress the error with 'silent!'. + silent! exec "bd ".bufnr(previousFile) diffthis " To ensure that A is on the left and B on the right, splitright must be off