Add possibility to reuse a split when opening a link

Also, refactor and simplify the corresponding functions (and finally
remove the chat between long inactive developers in the comments!)

Fix #316
This commit is contained in:
EinfachToll 2017-03-19 10:17:21 +01:00
parent 56cb06e73e
commit e4fe5ce09d
4 changed files with 93 additions and 93 deletions

View File

@ -1220,57 +1220,81 @@ function! vimwiki#base#find_prev_link() "{{{
endfunction " }}}
" vimwiki#base#follow_link
function! vimwiki#base#follow_link(split, ...) "{{{ Parse link at cursor and pass
" to VimwikiLinkHandler, or failing that, the default open_link handler
if exists('*vimwiki#'.VimwikiGet('syntax').'_base#follow_link')
" Syntax-specific links
" XXX: @Stuart: do we still need it?
" XXX: @Maxim: most likely! I am still working on a seemless way to
" integrate regexp's without complicating syntax/vimwiki.vim
if a:0
call vimwiki#{VimwikiGet('syntax')}_base#follow_link(a:split, a:1)
else
call vimwiki#{VimwikiGet('syntax')}_base#follow_link(a:split)
function! vimwiki#base#follow_link(split, reuse, move_cursor, ...) "{{{
" Parse link at cursor and pass to VimwikiLinkHandler, or failing that, the
" default open_link handler
" try WikiLink
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink),
\ g:vimwiki_rxWikiLinkMatchUrl)
" try WikiIncl
if lnk == ""
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiIncl),
\ g:vimwiki_rxWikiInclMatchUrl)
endif
" try Weblink
if lnk == ""
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWeblink),
\ g:vimwiki_rxWeblinkMatchUrl)
endif
if lnk != "" " cursor is indeed on a link
let processed_by_user_defined_handler = VimwikiLinkHandler(lnk)
if processed_by_user_defined_handler
return
endif
else
if a:split ==# "split"
if a:split ==# "hsplit"
let cmd = ":split "
elseif a:split ==# "vsplit"
let cmd = ":vsplit "
elseif a:split ==# "tabnew"
elseif a:split ==# "tab"
let cmd = ":tabnew "
else
let cmd = ":e "
endif
" try WikiLink
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink),
\ g:vimwiki_rxWikiLinkMatchUrl)
" try WikiIncl
if lnk == ""
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiIncl),
\ g:vimwiki_rxWikiInclMatchUrl)
endif
" try Weblink
if lnk == ""
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWeblink),
\ g:vimwiki_rxWeblinkMatchUrl)
endif
if lnk != ""
if !VimwikiLinkHandler(lnk)
call vimwiki#base#open_link(cmd, lnk)
" if we want to and can reuse a split window, jump to that window and open
" the new file there
if (a:split ==# 'hsplit' || a:split ==# 'vsplit') && a:reuse
let previous_window_nr = winnr('#')
if previous_window_nr > 0 && previous_window_nr != winnr()
execute previous_window_nr . 'wincmd w'
let cmd = ':e'
endif
return
endif
if VimwikiGet('syntax') == 'markdown'
let processed_by_markdown_reflink = vimwiki#markdown_base#open_reflink(lnk)
if processed_by_markdown_reflink
return
endif
" remove the extension from the filename if exists, because non-vimwiki
" markdown files usually include the extension in links
let lnk = substitute(lnk, VimwikiGet('ext').'$', '', '')
endif
let current_tab_page = tabpagenr()
call vimwiki#base#open_link(cmd, lnk)
if !a:move_cursor
if (a:split ==# 'hsplit' || a:split ==# 'vsplit')
execute 'wincmd p'
elseif a:split ==# 'tab'
execute 'tabnext ' . current_tab_page
endif
endif
else
if a:0 > 0
execute "normal! ".a:1
else
call vimwiki#base#normalize_link(0)
endif
endif
endfunction " }}}
" vimwiki#base#go_back_link

View File

@ -70,61 +70,6 @@ endfunction " }}}
" WIKI link following functions {{{
" vimwiki#markdown_base#follow_link
function! vimwiki#markdown_base#follow_link(split, ...) "{{{ Parse link at cursor and pass
" to VimwikiLinkHandler, or failing that, the default open_link handler
" echom "markdown_base#follow_link"
if 0
" Syntax-specific links
" XXX: @Stuart: do we still need it?
" XXX: @Maxim: most likely! I am still working on a seemless way to
" integrate regexp's without complicating syntax/vimwiki.vim
else
if a:split ==# "split"
let cmd = ":split "
elseif a:split ==# "vsplit"
let cmd = ":vsplit "
elseif a:split ==# "tabnew"
let cmd = ":tabnew "
else
let cmd = ":e "
endif
" try WikiLink
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink),
\ g:vimwiki_rxWikiLinkMatchUrl)
" try WikiIncl
if lnk == ""
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiIncl),
\ g:vimwiki_rxWikiInclMatchUrl)
endif
" try Weblink
if lnk == ""
let lnk = matchstr(vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWeblink),
\ g:vimwiki_rxWeblinkMatchUrl)
endif
if lnk != ""
if !VimwikiLinkHandler(lnk)
if !vimwiki#markdown_base#open_reflink(lnk)
" remove the extension from the filename if exists
let lnk = substitute(lnk, VimwikiGet('ext').'$', '', '')
call vimwiki#base#open_link(cmd, lnk)
endif
endif
return
endif
if a:0 > 0
execute "normal! ".a:1
else
call vimwiki#base#normalize_link(0)
endif
endif
endfunction " }}}
" LINK functions {{{
" s:normalize_link_syntax_n

View File

@ -594,6 +594,9 @@ il A single list item.
------------------------------------------------------------------------------
4.2. Local commands *vimwiki-local-commands*
These commands are only available (and meaningful) when you are currently in a
Vimwiki file.
*:VimwikiFollowLink*
Follow wiki link (or create target wiki page if needed).
@ -742,6 +745,34 @@ il A single list item.
their instances. Supports |cmdline-completion|. If
no arguments (tags) are specified, outputs all tags.
------------------------------------------------------------------------------
4.3. Functions *vimwiki-functions*
Functions to interact with Vimwiki. (It's intended that most commands will be
replaced with corresponding function calls in the future.)
Warning: this is currently unstable and likely to change.
To map them to a key, use >
nnoremap <C-K> :call vimwiki#base#function_name(arg1, arg2)<CR>
vimwiki#base#follow_link({split}, {reuse}, {move_cursor}) *follow_link*
Open the link under the cursor. {split} can have the following values:
'nosplit' open the link in the current window
'vsplit' open in a vertically split window
'hsplit' open in a horizontally split window
'tab' open in a new tab
If {reuse} is 1 and {split} one of 'vsplit' or 'hsplit', open the link in
a possibly existing split window instead of making a new split.
If {move_cursor} is 1 the cursor moves to the window or tab with the
opened link, otherwise, it stays in the window or tab with the link.
For example, <CR> is per default mapped to
vimwiki#base#follow_link('nosplit', 0, 1)
==============================================================================
5. Wiki syntax *vimwiki-syntax*

View File

@ -264,14 +264,14 @@ command! -buffer VimwikiNextLink call vimwiki#base#find_next_link()
command! -buffer VimwikiPrevLink call vimwiki#base#find_prev_link()
command! -buffer VimwikiDeleteLink call vimwiki#base#delete_link()
command! -buffer VimwikiRenameLink call vimwiki#base#rename_link()
command! -buffer VimwikiFollowLink call vimwiki#base#follow_link('nosplit')
command! -buffer VimwikiFollowLink call vimwiki#base#follow_link('nosplit', 0, 1)
command! -buffer VimwikiGoBackLink call vimwiki#base#go_back_link()
command! -buffer VimwikiSplitLink call vimwiki#base#follow_link('split')
command! -buffer VimwikiVSplitLink call vimwiki#base#follow_link('vsplit')
command! -buffer VimwikiSplitLink call vimwiki#base#follow_link('hsplit', 0, 1)
command! -buffer VimwikiVSplitLink call vimwiki#base#follow_link('vsplit', 0, 1)
command! -buffer -nargs=? VimwikiNormalizeLink call vimwiki#base#normalize_link(<f-args>)
command! -buffer VimwikiTabnewLink call vimwiki#base#follow_link('tabnew')
command! -buffer VimwikiTabnewLink call vimwiki#base#follow_link('tab', 0, 1)
command! -buffer VimwikiGenerateLinks call vimwiki#base#generate_links()
@ -327,7 +327,7 @@ command! -buffer -nargs=* -complete=custom,vimwiki#tags#complete_tags
if g:vimwiki_use_mouse
nmap <buffer> <S-LeftMouse> <NOP>
nmap <buffer> <C-LeftMouse> <NOP>
nnoremap <silent><buffer> <2-LeftMouse> :call vimwiki#base#follow_link("nosplit", "\<lt>2-LeftMouse>")<CR>
nnoremap <silent><buffer> <2-LeftMouse> :call vimwiki#base#follow_link('nosplit', 0, 1, "\<lt>2-LeftMouse>")<CR>
nnoremap <silent><buffer> <S-2-LeftMouse> <LeftMouse>:VimwikiSplitLink<CR>
nnoremap <silent><buffer> <C-2-LeftMouse> <LeftMouse>:VimwikiVSplitLink<CR>
nnoremap <silent><buffer> <RightMouse><LeftMouse> :VimwikiGoBackLink<CR>