Skip to content

Commit 30a9dfb

Browse files
committed
feat: add initial version of WikiPageRefile
refer: #58
1 parent 622d36b commit 30a9dfb

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

autoload/wiki/buffer.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function! s:init_buffer_commands() abort " {{{1
8888
command! -buffer WikiLinkReturn call wiki#nav#return()
8989
command! -buffer WikiLinkTransform call wiki#link#transform_current()
9090
command! -buffer WikiPageDelete call wiki#page#delete()
91+
command! -buffer WikiPageRefile call wiki#page#refile()
9192
command! -buffer WikiPageRename call wiki#page#rename()
9293
command! -buffer WikiPageRenameSection call wiki#page#rename_section()
9394
command! -buffer WikiTocGenerate call wiki#toc#create(0)
@@ -139,6 +140,7 @@ function! s:init_buffer_mappings() abort " {{{1
139140
nnoremap <silent><buffer> <plug>(wiki-link-return) :WikiLinkReturn<cr>
140141
nnoremap <silent><buffer> <plug>(wiki-link-transform) :WikiLinkTransform<cr>
141142
nnoremap <silent><buffer> <plug>(wiki-page-delete) :WikiPageDelete<cr>
143+
nnoremap <silent><buffer> <plug>(wiki-page-refile) :WikiPageRefile<cr>
142144
nnoremap <silent><buffer> <plug>(wiki-page-rename) :WikiPageRename<cr>
143145
nnoremap <silent><buffer> <plug>(wiki-page-rename-section) :WikiPageRenameSection<cr>
144146
nnoremap <silent><buffer> <plug>(wiki-toc-generate) :WikiTocGenerate<cr>
@@ -197,6 +199,7 @@ function! s:init_buffer_mappings() abort " {{{1
197199
\ '<plug>(wiki-link-transform)': '<leader>wf',
198200
\ '<plug>(wiki-link-transform-operator)': 'gl',
199201
\ '<plug>(wiki-page-delete)': '<leader>wd',
202+
\ '<plug>(wiki-page-refile)' : '<leader>wq',
200203
\ '<plug>(wiki-page-rename)': '<leader>wr',
201204
\ '<plug>(wiki-page-rename-section)': '<f2>',
202205
\ '<plug>(wiki-toc-generate)': '<leader>wt',

autoload/wiki/page.vim

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,79 @@ function! wiki#page#rename_section(...) abort "{{{1
173173
call wiki#log#info(printf('Updated %d links in %d files', l:n, l:m))
174174
endfunction
175175

176+
" }}}1
177+
function! wiki#page#refile(...) abort "{{{1
178+
let l:opts = extend(#{
179+
\ target_page: '',
180+
\ target_lnum: 0,
181+
\}, a:0 > 0 ? a:1 : {})
182+
183+
let l:source = {}
184+
let l:source.path = expand('%:p')
185+
let l:source.section = wiki#toc#get_section()
186+
if empty(l:source.section)
187+
return wiki#log#error('No source section recognized!')
188+
endif
189+
let l:source.anchor = l:source.section.anchor
190+
191+
let l:target = {}
192+
let l:target.path = wiki#u#eval_filename(l:opts.target_page)
193+
let l:target.node = wiki#paths#to_node(l:target.path)
194+
let l:target.section = wiki#toc#get_section(#{
195+
\ path: l:target.path,
196+
\ at_lnum: l:opts.target_lnum
197+
\})
198+
199+
" Calculate target anchor
200+
let l:depth = len(l:source.section.anchors)
201+
let l:target.anchors = (l:depth > 1
202+
\ ? get(l:target.section, 'anchors', [])[:l:depth - 2]
203+
\ : []) + [l:source.section.anchors[-1]]
204+
let l:target.anchor = '#' . join(l:target.anchors, '#')
205+
206+
207+
call wiki#log#info(
208+
\ printf('Refiling section "%s" to page "%s"',
209+
\ l:source.section.header, l:target.node))
210+
211+
" Move the lines
212+
if l:target.path ==# l:source.path
213+
let l:target.url = ''
214+
call execute(printf('%d,%dm %d',
215+
\ l:source.section.lnum,
216+
\ l:source.section.lnum_end,
217+
\ l:opts.target_lnum))
218+
silent update
219+
else
220+
let l:lines = getline(l:source.section.lnum, l:source.section.lnum_end)
221+
call deletebufline('', l:source.section.lnum, l:source.section.lnum_end)
222+
223+
" Append lines to target page
224+
let l:buf_exists = bufexists(l:target.path)
225+
call bufload(l:target.path)
226+
let l:bufnr = bufnr(l:target.path)
227+
call appendbufline(l:bufnr, l:opts.target_lnum, l:lines)
228+
" TODO
229+
230+
let l:target.url
231+
\ = wiki#paths#to_wiki_url(l:target.path, wiki#get_root())
232+
endif
233+
234+
" Update local links
235+
let l:pos = getcurpos()
236+
keepjumps execute printf(
237+
\ '%%s/\V%s/%s/e%s',
238+
\ l:source.anchor,
239+
\ l:target.url . l:target.anchor,
240+
\ &gdefault ? '' : 'g')
241+
call cursor(l:pos[1:])
242+
silent update
243+
244+
" Update remote links
245+
let [l:n, l:m] = s:update_links(l:source, l:target)
246+
call wiki#log#info(printf('Updated %d links in %d files', l:n, l:m))
247+
endfunction
248+
176249
" }}}1
177250
function! wiki#page#export(line1, line2, ...) abort " {{{1
178251
let l:cfg = deepcopy(g:wiki_export)

0 commit comments

Comments
 (0)