From 8e4202847f5c10d400b5a66574db194999395c9c Mon Sep 17 00:00:00 2001 From: Brennen Bearnes Date: Sat, 21 May 2022 17:56:14 -0600 Subject: [PATCH] follow_link: reuse existing tabs with tab drop (closes #238) Replaces :e in `vimwiki#base#follow_link()` with :drop, making this the default behavior for pressing on a link. Checks for the existence of :drop first, since this isn't available in some builds. Adds a new :VimwikiTabDropLink and makes this a default for the keybindings formerly occupied by :VimwikiTabnewLink; leaving :VimwikiTabnewLink available for backwards compatibility and anyone who still wants the old behavior. Doesn't touch the split window reuse functionality, or the :VimwikiGoBackLink behavior, although I can see an argument for adding :drop to the latter. I've wanted this for a while and happened to notice @davidlmontgomery's patch from 2016 in #238. Code has moved around a little since, but I think this is correct. --- autoload/vimwiki/base.vim | 10 +++++++++- doc/vimwiki.txt | 12 ++++++++++-- ftplugin/vimwiki.vim | 8 ++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim index 0cce6cf..52fbe82 100644 --- a/autoload/vimwiki/base.vim +++ b/autoload/vimwiki/base.vim @@ -1670,8 +1670,17 @@ function! vimwiki#base#follow_link(split, ...) abort let cmd = ':badd ' elseif a:split ==# 'tab' let cmd = ':tabnew ' + elseif a:split ==# 'tabdrop' + " Use tab drop if we've already got the file open in an existing tab + let cmd = ':tab drop ' else + " Same as above - doing this by default reduces incidence of multiple + " tabs with the same file. We default to :e just in case :drop doesn't + " exist in the current build. let cmd = ':e ' + if exists(':drop') + let cmd = ':drop ' + endif endif " if we want to and can reuse a split window, jump to that window and open @@ -1684,7 +1693,6 @@ function! vimwiki#base#follow_link(split, ...) abort endif endif - if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown' let processed_by_markdown_reflink = vimwiki#markdown_base#open_reflink(lnk) if processed_by_markdown_reflink diff --git a/doc/vimwiki.txt b/doc/vimwiki.txt index d8307d8..7369593 100644 --- a/doc/vimwiki.txt +++ b/doc/vimwiki.txt @@ -370,8 +370,10 @@ MAP MODE , n Follow wiki link (create target wiki page if needed), opening in a new tab. May not work in some terminals. Remapping could help. - Maps to |:VimwikiTabnewLink|. - Remap command: `VimwikiTabnewLink` + Maps to |:VimwikiTabDropLink|. + Remap command: `VimwikiTabDropLink` + See |:VimwikiTabnewLink| for old behavior which + always opens a new tab. *vimwiki_* n Go back to previously visited wiki page. @@ -810,6 +812,11 @@ Vimwiki file. *:VimwikiTabnewLink* Follow wiki link in a new tab (create target wiki page if needed). +*:VimwikiTabDropLink* + Follow wiki link with tab drop, reusing any existing tabs with the page. + Creates a new tab if the page isn't already open (create target wiki page + if needed). + *:VimwikiNextLink* Find next link on the current page. @@ -3985,6 +3992,7 @@ http://code.google.com/p/vimwiki/issues/list. They may be accessible from https://github.com/vimwiki-backup/vimwiki/issues. New:~ + * Feature: #238: Reuse existing tabs with tab drop * Issue #621: Feature request: Highlight code listings in HTML * Issue #290: Calendar plugin, do not sign if no wiki * Issue #281: Permit `\|` in tables diff --git a/ftplugin/vimwiki.vim b/ftplugin/vimwiki.vim index 04af334..31bc171 100644 --- a/ftplugin/vimwiki.vim +++ b/ftplugin/vimwiki.vim @@ -312,6 +312,8 @@ command! -buffer -nargs=? VimwikiNormalizeLink call vimwiki#base#normalize_link( command! -buffer VimwikiTabnewLink call vimwiki#base#follow_link('tab', 0, 1) +command! -buffer VimwikiTabDropLink call vimwiki#base#follow_link('tabdrop', 0, 1) + command! -buffer -nargs=? VimwikiGenerateLinks call vimwiki#base#generate_links(1, ) command! -buffer -nargs=0 VimwikiBacklinks call vimwiki#base#backlinks() @@ -418,6 +420,8 @@ vnoremap