From 8cc99c00c2fa03b263d137cdb841bb335a909c60 Mon Sep 17 00:00:00 2001 From: Tinmarino Date: Wed, 5 Aug 2020 00:39:00 -0400 Subject: [PATCH] Fix VimwikiRenameLink ruins window layout (Issue #592) --- Dockerfile | 3 +-- autoload/vimwiki/base.vim | 49 +++++++++++++++++++++++----------- test/command_rename_link.vader | 14 ++++++++-- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 08cbe60..cc82b0e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,5 +21,4 @@ RUN install_vim -tag v7.3.429 -name vim_7.3.429 -build \ -tag v7.4.1546 -name vim_7.4.1546 -build \ -tag v8.0.0027 -name vim_8.0.0027 -build \ -tag v8.1.0519 -name vim_8.1.0519 -build \ -+ -tag neovim:v0.2.2 -name nvim_0.2.2 -build \ -+ -tag neovim:v0.3.8 -name nvim_0.3.8 -build \ + -tag neovim:v0.3.8 -name nvim_0.3.8 -build \ diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim index 490e13d..06805fd 100644 --- a/autoload/vimwiki/base.vim +++ b/autoload/vimwiki/base.vim @@ -1734,6 +1734,7 @@ endfunction " Rename current file, update all links to it " Param: [new_filepath ] +" Exported: VimwikiRenameFile function! vimwiki#base#rename_link(...) abort " Get filename and dir relative to wiki root let subdir = vimwiki#vars#get_bufferlocal('subdir') @@ -1788,25 +1789,22 @@ function! vimwiki#base#rename_link(...) abort let &buftype='nofile' - " Save current buffer: [file_name, buffer_name] - let cur_buffer = [expand('%:p'), vimwiki#vars#get_bufferlocal('prev_links')] + " Save current buffer: [file_name, previous_name, buffer_number] + let buf_old_info = [expand('%:p'), vimwiki#vars#get_bufferlocal('prev_links'), bufnr('%')] + if v:version > 800 || has('patch-8.0.0083') + let win_old_id = win_getid() + endif " Get all wiki buffer let blist = s:get_wiki_buffers() - " Save wiki buffers + " Dump wiki buffers: they may change for bitem in blist execute ':b '.escape(bitem[0], ' ') execute ':update' endfor - execute ':b '.escape(cur_buffer[0], ' ') - - " Remove wiki buffers - for bitem in blist - execute 'bwipeout '.escape(bitem[0], ' ') - endfor - + " Prevent prompt from scrolling alone let more_save = &more setlocal nomore @@ -1820,20 +1818,39 @@ function! vimwiki#base#rename_link(...) abort \ fnamemodify(new_fname_rel_dir, ':r') \ ) - " Restore wiki buffers + "" Restore wiki buffers + let autoread_save = &autoread + set autoread for bitem in blist - if !vimwiki#path#is_equal(bitem[0], cur_buffer[0]) - call s:open_wiki_buffer(bitem) - endif + execute ':b '.escape(bitem[0], ' ') + execute ':e!' endfor + let &autoread = autoread_save " Open the new buffer - call s:open_wiki_buffer([new_fname, cur_buffer[1]]) - " execute 'bwipeout '.escape(cur_buffer[0], ' ') + call s:open_wiki_buffer([new_fname, buf_old_info[1]]) + let buf_new_nb = bufnr('%') + + " Change old_buffer by new buffer in all window + windo if bufnr('%') == buf_old_info[2] | exe 'b ' . buf_new_nb | endif + " Goto the window I belong + if v:version > 800 || has('patch-8.0.0083') + call win_gotoid(win_old_id) + endif + + " Wipeout the old buffer: avoid surprises <= If it is not the same + if buf_old_info[2] != buf_new_nb + exe 'bwipeout! ' . buf_old_info[2] + else + " Should not happen + echomsg 'Vimwiki Error: New buffer is the same as old, so will not delete: ' + \ . buf_new_nb . '.Please open an issue if see this messsage' + endif " Log success echomsg 'Vimwiki: '.old_fname.' is renamed to '.new_fname + " Restore prompt let &more = more_save endfunction diff --git a/test/command_rename_link.vader b/test/command_rename_link.vader index 3495a01..df6a806 100644 --- a/test/command_rename_link.vader +++ b/test/command_rename_link.vader @@ -1,5 +1,6 @@ # VimwikiRenameFile # Related to link, file navigation +# Many commands are made with Do: They block with Execute # Create directories I remove at end {{{1 @@ -32,6 +33,15 @@ Execute (Create 3 files): edit $HOME/testmarkdown/Test-Rename-Completion.md call WriteMe() + +Do (Testing Old buffer has been wiped out {{{2): + :edit $HOME/testmarkdown/Test-Rename-Completion.md\ + :let buf_old = bufnr('%')\ + :VimwikiRenameFile Test-Rename-new1\ + :AssertEqual 'Test-Rename-new1', expand('%:t:r')\ + :VimwikiRenameFile Test-Rename-Completion\ + :AssertEqual expand('%'), $HOME . '/testmarkdown/Test-Rename-Completion.md'\ + Do (Testing Completion {{{2): # Rename and test (zzz) :VimwikiRenameFile Test-Rename-z\1\ @@ -45,6 +55,7 @@ Do (Testing transforward {{{2): :Log 'Forward: root -> dir1/dir11 {{{3'\ # Create dir1/dir11/Test-Rename and link to it :edit $HOME/testmarkdown/Test-Rename-Completion.md\ + ggdG idir1/dir11/Test-Rename.md\ \\ :VimwikiRenameFile ../Test-Rename-2\ @@ -70,9 +81,8 @@ Do (Testing transforward {{{2): :VimwikiRenameFile dir1/Test-Rename-Completion-2\ :AssertEqual expand('%'), $HOME . '/testmarkdown/dir1/Test-Rename-Completion-2.md'\ -# Delete smaller unit changed {{{2 -Execute (Clean): +Execute (Delete smaller unit changed {{{2): call DeleteFile('$HOME/testmarkdown/Test-Rename-zzz.md') call DeleteFile('$HOME/testmarkdown/dir1/Test-Rename-Completion_2.md') call DeleteFile('$HOME/testmarkdown/dir1/Test-Rename-2.md')