Skip to content

Commit 1c6d962

Browse files
committed
feat: add initial version of WikiPageRefile
refer: #58
1 parent a3eb8e3 commit 1c6d962

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

autoload/wiki/buffer.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function! s:init_buffer_commands() abort " {{{1
6161
command! -buffer WikiLinkIncomingToggle call wiki#link#incoming_display_toggle()
6262
command! -buffer WikiLinkIncomingHover call wiki#link#incoming_hover()
6363
command! -buffer WikiPageDelete call wiki#page#delete()
64+
command! -buffer WikiPageRefile call wiki#page#refile()
6465
command! -buffer WikiPageRename call wiki#page#rename()
6566
command! -buffer WikiPageRenameSection call wiki#page#rename_section()
6667
command! -buffer WikiToc call g:wiki_select_method.toc()
@@ -110,6 +111,7 @@ function! s:init_buffer_mappings() abort " {{{1
110111
nnoremap <silent><buffer> <plug>(wiki-link-incoming-toggle) :WikiLinkIncomingToggle<cr>
111112
nnoremap <silent><buffer> <plug>(wiki-link-incoming-hover) :WikiLinkIncomingHover<cr>
112113
nnoremap <silent><buffer> <plug>(wiki-page-delete) :WikiPageDelete<cr>
114+
nnoremap <silent><buffer> <plug>(wiki-page-refile) :WikiPageRefile<cr>
113115
nnoremap <silent><buffer> <plug>(wiki-page-rename) :WikiPageRename<cr>
114116
nnoremap <silent><buffer> <plug>(wiki-page-rename-section) :WikiPageRenameSection<cr>
115117
nnoremap <silent><buffer> <plug>(wiki-toc-generate) :WikiTocGenerate<cr>
@@ -170,6 +172,7 @@ function! s:init_buffer_mappings() abort " {{{1
170172
\ '<plug>(wiki-link-incoming-toggle)': '<leader>wli',
171173
\ '<plug>(wiki-link-incoming-hover)': '<leader>wlI',
172174
\ '<plug>(wiki-page-delete)': '<leader>wd',
175+
\ '<plug>(wiki-page-refile)' : '<leader>wq',
173176
\ '<plug>(wiki-page-rename)': '<leader>wr',
174177
\ '<plug>(wiki-page-rename-section)': '<f2>',
175178
\ '<plug>(wiki-toc-generate)': '<leader>wt',

autoload/wiki/page.vim

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,72 @@ function! wiki#page#rename_section(...) abort "{{{1
162162
call s:update_links_external(l:source, l:target)
163163
endfunction
164164

165+
" }}}1
166+
function! wiki#page#refile(...) abort "{{{1
167+
let l:opts = extend(#{
168+
\ target_page: '',
169+
\ target_lnum: 0,
170+
\}, a:0 > 0 ? a:1 : {})
171+
172+
" Collect source data
173+
let l:source = wiki#toc#get_section()
174+
if empty(l:source)
175+
return wiki#log#error('No source section recognized!')
176+
endif
177+
let l:source.path = expand('%:p')
178+
179+
" Collect target data
180+
let l:target = {}
181+
let l:target.path = wiki#u#eval_filename(l:opts.target_page)
182+
if !filereadable(l:target.path)
183+
return wiki#log#error('Target page was not found!')
184+
endif
185+
let l:target.lnum = l:opts.target_lnum
186+
let l:target.node = wiki#paths#to_node(l:target.path)
187+
let l:target.url = l:target.path !=# l:source.path
188+
\ ? wiki#paths#to_wiki_url(l:target.path, wiki#get_root())
189+
\ : ''
190+
191+
call wiki#log#info(
192+
\ printf('Refiling section "%s" to page "%s"',
193+
\ l:source.header, l:target.node))
194+
195+
" Determine target anchor
196+
let l:current_anchors = get(wiki#toc#get_section(#{
197+
\ path: l:target.path,
198+
\ at_lnum: l:opts.target_lnum
199+
\}), 'anchors', [])
200+
let l:target_anchors = l:source.level > 1
201+
\ ? l:current_anchors[:l:source.level - 2]
202+
\ : []
203+
call add(l:target_anchors, l:source.anchors[-1])
204+
let l:target.anchor = '#' . join(l:target_anchors, '#')
205+
206+
" Move the section lines
207+
if empty(l:target.url)
208+
call execute(printf('%d,%dm %d',
209+
\ l:source.lnum, l:source.lnum_end, l:target.lnum))
210+
silent write
211+
else
212+
let l:lines = getline(l:source.lnum, l:source.lnum_end)
213+
call deletebufline('', l:source.lnum, l:source.lnum_end)
214+
silent write
215+
216+
let l:current_bufnr = bufnr('')
217+
let l:was_loaded = bufloaded(l:target.path)
218+
keepalt execute 'silent edit' fnameescape(l:target.path)
219+
call append(l:target.lnum, l:lines)
220+
silent write
221+
if !l:was_loaded
222+
keepalt execute 'bwipeout'
223+
endif
224+
keepalt execute 'buffer' l:current_bufnr
225+
endif
226+
227+
call s:update_links_local(l:source, l:target)
228+
call s:update_links_external(l:source, l:target)
229+
endfunction
230+
165231
" }}}1
166232
function! wiki#page#export(line1, line2, ...) abort " {{{1
167233
let l:cfg = deepcopy(g:wiki_export)

0 commit comments

Comments
 (0)