Skip to content

Commit

Permalink
feat: implement visual version of WikiLinkExtractHeader
Browse files Browse the repository at this point in the history
refer: #280
  • Loading branch information
lervag committed May 18, 2023
1 parent 6880ebe commit 4b7e7cb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
5 changes: 4 additions & 1 deletion autoload/wiki/buffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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(<range>, <line1>, <line2>)
command! -buffer WikiLinkFollow call wiki#link#follow()
command! -buffer WikiLinkFollowSplit call wiki#link#follow(expand(<q-mods>) . ' split')
command! -buffer WikiLinkFollowTab call wiki#link#follow('tabedit')
Expand Down Expand Up @@ -99,6 +100,7 @@ function! s:init_buffer_mappings() abort " {{{1
nnoremap <silent><buffer> <plug>(wiki-link-next) :WikiLinkNext<cr>
nnoremap <silent><buffer> <plug>(wiki-link-show) :WikiLinkShow<cr>
nnoremap <silent><buffer> <plug>(wiki-link-extract-header) :WikiLinkExtractHeader<cr>
xnoremap <silent><buffer> <plug>(wiki-link-extract-header) :WikiLinkExtractHeader<cr>
nnoremap <silent><buffer> <plug>(wiki-link-follow) :WikiLinkFollow<cr>
nnoremap <silent><buffer> <plug>(wiki-link-follow-split) :WikiLinkFollowSplit<cr>
nnoremap <buffer> <plug>(wiki-link-follow-vsplit) :vert WikiLinkFollowSplit<cr>
Expand Down Expand Up @@ -155,6 +157,7 @@ function! s:init_buffer_mappings() abort " {{{1
\ '<plug>(wiki-link-prev)': '<s-tab>',
\ '<plug>(wiki-link-show)': '<leader>wll',
\ '<plug>(wiki-link-extract-header)': '<leader>wlh',
\ 'x_<plug>(wiki-link-extract-header)': '<leader>wlh',
\ '<plug>(wiki-link-follow)': '<cr>',
\ '<plug>(wiki-link-follow-split)': '<c-w><cr>',
\ '<plug>(wiki-link-follow-vsplit)': '<c-w><tab>',
Expand Down
30 changes: 19 additions & 11 deletions autoload/wiki/link.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion doc/wiki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,9 @@ MAPPINGS AND COMMANDS REFERENCE *wiki-mappings-reference*

*<plug>(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.

*<plug>(wiki-page-delete)*
*WikiPageDelete*
Expand Down

0 comments on commit 4b7e7cb

Please sign in to comment.