From 4b7e7cbe4f6a40afefe78eea8556fe074217695b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Thu, 18 May 2023 12:35:19 +0200 Subject: [PATCH] feat: implement visual version of WikiLinkExtractHeader refer: #280 --- autoload/wiki/buffer.vim | 5 ++++- autoload/wiki/link.vim | 30 +++++++++++++++++++----------- doc/wiki.txt | 4 +++- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/autoload/wiki/buffer.vim b/autoload/wiki/buffer.vim index 7ef0fae..0bb9bbe 100644 --- a/autoload/wiki/buffer.vim +++ b/autoload/wiki/buffer.vim @@ -50,7 +50,8 @@ function! s:init_buffer_commands() abort " {{{1 command! -buffer WikiJournalIndex call wiki#journal#make_index() command! -buffer WikiLinkNext call wiki#nav#next_link() command! -buffer WikiLinkShow call wiki#link#show() - command! -buffer WikiLinkExtractHeader call wiki#link#set_text_from_header() + command! -buffer -range WikiLinkExtractHeader + \ call wiki#link#set_text_from_header(, , ) command! -buffer WikiLinkFollow call wiki#link#follow() command! -buffer WikiLinkFollowSplit call wiki#link#follow(expand() . ' split') command! -buffer WikiLinkFollowTab call wiki#link#follow('tabedit') @@ -99,6 +100,7 @@ function! s:init_buffer_mappings() abort " {{{1 nnoremap (wiki-link-next) :WikiLinkNext nnoremap (wiki-link-show) :WikiLinkShow nnoremap (wiki-link-extract-header) :WikiLinkExtractHeader + xnoremap (wiki-link-extract-header) :WikiLinkExtractHeader nnoremap (wiki-link-follow) :WikiLinkFollow nnoremap (wiki-link-follow-split) :WikiLinkFollowSplit nnoremap (wiki-link-follow-vsplit) :vert WikiLinkFollowSplit @@ -155,6 +157,7 @@ function! s:init_buffer_mappings() abort " {{{1 \ '(wiki-link-prev)': '', \ '(wiki-link-show)': 'wll', \ '(wiki-link-extract-header)': 'wlh', + \ 'x_(wiki-link-extract-header)': 'wlh', \ '(wiki-link-follow)': '', \ '(wiki-link-follow-split)': '', \ '(wiki-link-follow-vsplit)': '', diff --git a/autoload/wiki/link.vim b/autoload/wiki/link.vim index cb059a9..5155549 100644 --- a/autoload/wiki/link.vim +++ b/autoload/wiki/link.vim @@ -183,20 +183,28 @@ function! wiki#link#follow(...) abort "{{{1 endfunction " }}}1 -function! wiki#link#set_text_from_header() abort "{{{1 - let l:link = wiki#link#get() - if index(['wiki', 'journal'], l:link.scheme) < 0 | return | endif +function! wiki#link#set_text_from_header(range, line1, line2) abort "{{{1 + if a:range == 0 + let l:links = [wiki#link#get()] + else + let l:links = wiki#link#get_all_from_range(a:line1, a:line2) + endif - let l:title = wiki#toc#get_page_title(l:link) - if empty(l:title) | return | endif + for l:link in filter( + \ l:links, + \ { _, x -> index(['wiki', 'journal'], x.scheme) >= 0 } + \) + let l:title = wiki#toc#get_page_title(l:link) + if empty(l:title) | return | endif - try - let l:new = wiki#link#templates#{l:link.type}(l:link.url, l:title, l:link) - catch /E117:/ - let l:new = wiki#link#templates#wiki(l:link.url, l:title) - endtry + try + let l:new = wiki#link#templates#{l:link.type}(l:link.url, l:title, l:link) + catch /E117:/ + let l:new = wiki#link#templates#wiki(l:link.url, l:title) + endtry - call l:link.replace(l:new) + call l:link.replace(l:new) + endfor endfunction " }}}1 diff --git a/doc/wiki.txt b/doc/wiki.txt index 94da7f0..0110e17 100644 --- a/doc/wiki.txt +++ b/doc/wiki.txt @@ -1714,7 +1714,9 @@ MAPPINGS AND COMMANDS REFERENCE *wiki-mappings-reference* *(wiki-link-extract-header)* *WikiLinkExtractHeader* - Set link title from the first header of the target file. + Set link titles from the first header of the target file. In normal mode, + this works on the link under the cursor. In visual mode, it works on all + links in the current visual selection. *(wiki-page-delete)* *WikiPageDelete*