Fix VimwikiRenameLink ruins window layout (Issue #592)

This commit is contained in:
Tinmarino 2020-08-05 00:39:00 -04:00
parent b1de908f0e
commit 8cc99c00c2
3 changed files with 46 additions and 20 deletions

View File

@ -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 v7.4.1546 -name vim_7.4.1546 -build \
-tag v8.0.0027 -name vim_8.0.0027 -build \ -tag v8.0.0027 -name vim_8.0.0027 -build \
-tag v8.1.0519 -name vim_8.1.0519 -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 \

View File

@ -1734,6 +1734,7 @@ endfunction
" Rename current file, update all links to it " Rename current file, update all links to it
" Param: [new_filepath <string>] " Param: [new_filepath <string>]
" Exported: VimwikiRenameFile
function! vimwiki#base#rename_link(...) abort function! vimwiki#base#rename_link(...) abort
" Get filename and dir relative to wiki root " Get filename and dir relative to wiki root
let subdir = vimwiki#vars#get_bufferlocal('subdir') let subdir = vimwiki#vars#get_bufferlocal('subdir')
@ -1788,25 +1789,22 @@ function! vimwiki#base#rename_link(...) abort
let &buftype='nofile' let &buftype='nofile'
" Save current buffer: [file_name, buffer_name] " Save current buffer: [file_name, previous_name, buffer_number]
let cur_buffer = [expand('%:p'), vimwiki#vars#get_bufferlocal('prev_links')] 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 " Get all wiki buffer
let blist = s:get_wiki_buffers() let blist = s:get_wiki_buffers()
" Save wiki buffers " Dump wiki buffers: they may change
for bitem in blist for bitem in blist
execute ':b '.escape(bitem[0], ' ') execute ':b '.escape(bitem[0], ' ')
execute ':update' execute ':update'
endfor endfor
execute ':b '.escape(cur_buffer[0], ' ') " Prevent prompt from scrolling alone
" Remove wiki buffers
for bitem in blist
execute 'bwipeout '.escape(bitem[0], ' ')
endfor
let more_save = &more let more_save = &more
setlocal nomore setlocal nomore
@ -1820,20 +1818,39 @@ function! vimwiki#base#rename_link(...) abort
\ fnamemodify(new_fname_rel_dir, ':r') \ fnamemodify(new_fname_rel_dir, ':r')
\ ) \ )
" Restore wiki buffers "" Restore wiki buffers
let autoread_save = &autoread
set autoread
for bitem in blist for bitem in blist
if !vimwiki#path#is_equal(bitem[0], cur_buffer[0]) execute ':b '.escape(bitem[0], ' ')
call s:open_wiki_buffer(bitem) execute ':e!'
endif
endfor endfor
let &autoread = autoread_save
" Open the new buffer " Open the new buffer
call s:open_wiki_buffer([new_fname, cur_buffer[1]]) call s:open_wiki_buffer([new_fname, buf_old_info[1]])
" execute 'bwipeout '.escape(cur_buffer[0], ' ') 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 " Log success
echomsg 'Vimwiki: '.old_fname.' is renamed to '.new_fname echomsg 'Vimwiki: '.old_fname.' is renamed to '.new_fname
" Restore prompt
let &more = more_save let &more = more_save
endfunction endfunction

View File

@ -1,5 +1,6 @@
# VimwikiRenameFile # VimwikiRenameFile
# Related to link, file navigation # Related to link, file navigation
# Many commands are made with Do: They block with Execute
# Create directories I remove at end {{{1 # Create directories I remove at end {{{1
@ -32,6 +33,15 @@ Execute (Create 3 files):
edit $HOME/testmarkdown/Test-Rename-Completion.md edit $HOME/testmarkdown/Test-Rename-Completion.md
call WriteMe() call WriteMe()
Do (Testing Old buffer has been wiped out {{{2):
:edit $HOME/testmarkdown/Test-Rename-Completion.md\<Cr>
:let buf_old = bufnr('%')\<Cr>
:VimwikiRenameFile Test-Rename-new1\<Cr>
:AssertEqual 'Test-Rename-new1', expand('%:t:r')\<Cr>
:VimwikiRenameFile Test-Rename-Completion\<Cr>
:AssertEqual expand('%'), $HOME . '/testmarkdown/Test-Rename-Completion.md'\<Cr>
Do (Testing Completion {{{2): Do (Testing Completion {{{2):
# Rename and test (zzz) # Rename and test (zzz)
:VimwikiRenameFile Test-Rename-z\<C-l>1\<Cr> :VimwikiRenameFile Test-Rename-z\<C-l>1\<Cr>
@ -45,6 +55,7 @@ Do (Testing transforward {{{2):
:Log 'Forward: root -> dir1/dir11 {{{3'\<Cr> :Log 'Forward: root -> dir1/dir11 {{{3'\<Cr>
# Create dir1/dir11/Test-Rename and link to it # Create dir1/dir11/Test-Rename and link to it
:edit $HOME/testmarkdown/Test-Rename-Completion.md\<Cr> :edit $HOME/testmarkdown/Test-Rename-Completion.md\<Cr>
ggdG
idir1/dir11/Test-Rename.md\<Esc> idir1/dir11/Test-Rename.md\<Esc>
\<Cr>\<Cr> \<Cr>\<Cr>
:VimwikiRenameFile ../Test-Rename-2\<Cr> :VimwikiRenameFile ../Test-Rename-2\<Cr>
@ -70,9 +81,8 @@ Do (Testing transforward {{{2):
:VimwikiRenameFile dir1/Test-Rename-Completion-2\<Cr> :VimwikiRenameFile dir1/Test-Rename-Completion-2\<Cr>
:AssertEqual expand('%'), $HOME . '/testmarkdown/dir1/Test-Rename-Completion-2.md'\<Cr> :AssertEqual expand('%'), $HOME . '/testmarkdown/dir1/Test-Rename-Completion-2.md'\<Cr>
# 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/Test-Rename-zzz.md')
call DeleteFile('$HOME/testmarkdown/dir1/Test-Rename-Completion_2.md') call DeleteFile('$HOME/testmarkdown/dir1/Test-Rename-Completion_2.md')
call DeleteFile('$HOME/testmarkdown/dir1/Test-Rename-2.md') call DeleteFile('$HOME/testmarkdown/dir1/Test-Rename-2.md')