diff --git a/autoload/wiki/buffer.vim b/autoload/wiki/buffer.vim index 2e88b04..7770f3b 100644 --- a/autoload/wiki/buffer.vim +++ b/autoload/wiki/buffer.vim @@ -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) @@ -139,6 +140,7 @@ function! s:init_buffer_mappings() abort " {{{1 nnoremap (wiki-link-return) :WikiLinkReturn nnoremap (wiki-link-transform) :WikiLinkTransform nnoremap (wiki-page-delete) :WikiPageDelete + nnoremap (wiki-page-refile) :WikiPageRefile nnoremap (wiki-page-rename) :WikiPageRename nnoremap (wiki-page-rename-section) :WikiPageRenameSection nnoremap (wiki-toc-generate) :WikiTocGenerate @@ -197,6 +199,7 @@ function! s:init_buffer_mappings() abort " {{{1 \ '(wiki-link-transform)': 'wf', \ '(wiki-link-transform-operator)': 'gl', \ '(wiki-page-delete)': 'wd', + \ '(wiki-page-refile)' : 'wq', \ '(wiki-page-rename)': 'wr', \ '(wiki-page-rename-section)': '', \ '(wiki-toc-generate)': 'wt', diff --git a/autoload/wiki/page.vim b/autoload/wiki/page.vim index ef3214c..f563f18 100644 --- a/autoload/wiki/page.vim +++ b/autoload/wiki/page.vim @@ -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)