Skip to content

Commit

Permalink
feat: add initial version of WikiPageRefile
Browse files Browse the repository at this point in the history
refer: #58
  • Loading branch information
lervag committed Jul 20, 2023
1 parent 088c54c commit 53a92e3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions autoload/wiki/buffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function! s:init_buffer_commands() abort " {{{1
command! -buffer WikiLinkReturn call wiki#nav#return()
command! -buffer WikiLinkTransform call wiki#link#transform_current()
command! -buffer WikiPageDelete call wiki#page#delete()
command! -buffer WikiPageRefile call wiki#page#refile()
command! -buffer WikiPageRename call wiki#page#rename()
command! -buffer WikiPageRenameSection call wiki#page#rename_section()
command! -buffer WikiTocGenerate call wiki#toc#create(0)
Expand Down Expand Up @@ -139,6 +140,7 @@ function! s:init_buffer_mappings() abort " {{{1
nnoremap <silent><buffer> <plug>(wiki-link-return) :WikiLinkReturn<cr>
nnoremap <silent><buffer> <plug>(wiki-link-transform) :WikiLinkTransform<cr>
nnoremap <silent><buffer> <plug>(wiki-page-delete) :WikiPageDelete<cr>
nnoremap <silent><buffer> <plug>(wiki-page-refile) :WikiPageRefile<cr>
nnoremap <silent><buffer> <plug>(wiki-page-rename) :WikiPageRename<cr>
nnoremap <silent><buffer> <plug>(wiki-page-rename-section) :WikiPageRenameSection<cr>
nnoremap <silent><buffer> <plug>(wiki-toc-generate) :WikiTocGenerate<cr>
Expand Down Expand Up @@ -197,6 +199,7 @@ function! s:init_buffer_mappings() abort " {{{1
\ '<plug>(wiki-link-transform)': '<leader>wf',
\ '<plug>(wiki-link-transform-operator)': 'gl',
\ '<plug>(wiki-page-delete)': '<leader>wd',
\ '<plug>(wiki-page-refile)' : '<leader>wq',
\ '<plug>(wiki-page-rename)': '<leader>wr',
\ '<plug>(wiki-page-rename-section)': '<f2>',
\ '<plug>(wiki-toc-generate)': '<leader>wt',
Expand Down
66 changes: 66 additions & 0 deletions autoload/wiki/page.vim
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,72 @@ function! wiki#page#rename_section(...) abort "{{{1
call s:update_links_external(l:source, l:target)
endfunction

" }}}1
function! wiki#page#refile(...) abort "{{{1
let l:opts = extend(#{
\ target_page: '',
\ target_lnum: 0,
\}, a:0 > 0 ? a:1 : {})

" Collect source data
let l:source = wiki#toc#get_section()
if empty(l:source)
return wiki#log#error('No source section recognized!')
endif
let l:source.path = expand('%:p')

" Collect target data
let l:target = {}
let l:target.path = wiki#u#eval_filename(l:opts.target_page)
if !filereadable(l:target.path)
return wiki#log#error('Target page was not found!')
endif
let l:target.lnum = l:opts.target_lnum
let l:target.node = wiki#paths#to_node(l:target.path)
let l:target.url = l:target.path !=# l:source.path
\ ? wiki#paths#to_wiki_url(l:target.path, wiki#get_root())
\ : ''

call wiki#log#info(
\ printf('Refiling section "%s" to page "%s"',
\ l:source.header, l:target.node))

" Determine target anchor
let l:current_anchors = get(wiki#toc#get_section(#{
\ path: l:target.path,
\ at_lnum: l:opts.target_lnum
\}), 'anchors', [])
let l:target_anchors = l:source.level > 1
\ ? l:current_anchors[:l:source.level - 2]
\ : []
call add(l:target_anchors, l:source.anchors[-1])
let l:target.anchor = '#' . join(l:target_anchors, '#')

" Move the section lines
if empty(l:target.url)
call execute(printf('%d,%dm %d',
\ l:source.lnum, l:source.lnum_end, l:target.lnum))
silent write
else
let l:lines = getline(l:source.lnum, l:source.lnum_end)
call deletebufline('', l:source.lnum, l:source.lnum_end)
silent write

let l:current_bufnr = bufnr('')
let l:was_loaded = bufloaded(l:target.path)
keepalt execute 'silent edit' fnameescape(l:target.path)
call append(l:target.lnum, l:lines)
silent write
if !l:was_loaded
keepalt execute 'bwipeout'
endif
keepalt execute 'buffer' l:current_bufnr
endif

call s:update_links_local(l:source, l:target)
call s:update_links_external(l:source, l:target)
endfunction

" }}}1
function! wiki#page#export(line1, line2, ...) abort " {{{1
let l:cfg = deepcopy(g:wiki_export)
Expand Down

0 comments on commit 53a92e3

Please sign in to comment.