On 2023-01-28, Matt Martini wrote:
> Gary,
>
> I (and others) are having the same issue with vim-nerdtree-tabs.
> It is fully described here: issue #102
>
> There is a function that when a tab is closed, checks if the last buffer is
> NERDTree.
> and if it is it closes/quits.
>
> It was using similar code to yours:
>
> if exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1 && winnr
> ("$") == 1
>
> or the variants
>
> if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType ==
> "primary"):
>
> if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree())
>
> The code was working until 9.0.900+ (I don't know the exact patch).
>
> Unfortunately, the vim-nerdtree-tabs plugin is no longer maintained, so we must
> find a solution on our own.
>
> Were you able to find a workaround or another method of solving your issue?
>
> Matt
Matt,
The solution I finally adopted was to use feedkeys(), like this.
" Note the ending ":\<BS>". This clears the "quit" from the command line.
"
autocmd BufWinEnter __Calendar
\ augroup MyCalendar
\ | autocmd!
\ | autocmd BufEnter <buffer> if winnr("$") == 1 | call feedkeys(":quit\<CR>:\<BS>") | endif
\ | augroup END
This isn't all that different from the original code that failed
after patch 9.0.907.
autocmd BufWinEnter __Calendar
\ autocmd BufEnter <buffer> if winnr("$")==1 | quit | endif
Sometime later, I encountered the same error while using the linediff
plugin (
https://badge.fury.io/gh/andrewradev/linediff.vim). I fixed
that one temporarily with this change, then reported it to the
author.
diff --git a/autoload/linediff/differ.vim b/autoload/linediff/differ.vim
index 23a69ce..023c716 100644
--- a/autoload/linediff/differ.vim
+++ b/autoload/linediff/differ.vim
@@ -211,7 +211,11 @@ endfunction
function! linediff#differ#CloseDiffBuffer(force) dict
if bufexists(self.diff_buffer)
let bang = a:force ? '!' : ''
- exe "bdelete".bang." ".self.diff_buffer
+ " [GAJ 2022-12-19] Change direct :bdelete command to a feedkeys()
+ " operation to work around the new behavior of patch 9.0.907 that
+ " prohibits certain operations such as buffer deletions in autocommand
+ " contexts. The ":\<BS>" erases the command from the command line.
+ call feedkeys(":bdelete".bang." ".self.diff_buffer."\<CR>:\<BS>")
endif
endfunction
He thought there might be a race condition (he may have actually
observed one--I forget) when using feedkeys() and preferred the
following solution.
diff --git a/autoload/linediff/differ.vim b/autoload/linediff/differ.vim
index 23a69ce..b9f3d12 100644
--- a/autoload/linediff/differ.vim
+++ b/autoload/linediff/differ.vim
@@ -211,7 +211,16 @@ endfunction
function! linediff#differ#CloseDiffBuffer(force) dict
if bufexists(self.diff_buffer)
let bang = a:force ? '!' : ''
- exe "bdelete".bang." ".self.diff_buffer
+ let diff_buffer = self.diff_buffer
+
+ if has('patch-9.0.907')
+ " Vim forbids closing the window inside an autocommand, so let's do it
+ " afterwards.
+ " Ref:
https://github.com/AndrewRadev/linediff.vim/issues/36
+ call timer_start(1, {-> execute('bdelete'.bang.' '.diff_buffer) })
+ else
+ exe 'bdelete'.bang.' '.diff_buffer
+ endif
endif
endfunction
Regards,
Gary