|
| 1 | +" A wiki plugin for Vim |
| 2 | +" |
| 3 | +" Maintainer: Karl Yngve Lervåg |
| 4 | + |
| 5 | +" |
| 6 | + |
| 7 | +function! wiki#page#refile#collect_source() abort " {{{1 |
| 8 | + " Returns: |
| 9 | + " source: dict(path, lnum, lnum_end, header, anchor) |
| 10 | + let l:source = wiki#toc#get_section() |
| 11 | + if !empty(l:source) |
| 12 | + let l:source.path = expand('%:p') |
| 13 | + endif |
| 14 | + |
| 15 | + return l:source |
| 16 | +endfunction |
| 17 | + |
| 18 | +" }}}1 |
| 19 | +function! wiki#page#refile#collect_target(opts) abort " {{{1 |
| 20 | + " Returns: |
| 21 | + " source: dict(path, lnum, anchor) |
| 22 | + |
| 23 | + let l:path = wiki#u#eval_filename(l:opts.target_page) |
| 24 | + if !filereadable(l:path) |
| 25 | + throw 'wiki.vim: target page not found' |
| 26 | + endif |
| 27 | + |
| 28 | + |
| 29 | + if has_key(l:opts, 'target_anchor_before') |
| 30 | + return s:collect_target_by_anchor_before(l:path, l:opts.target_anchor_before) |
| 31 | + endif |
| 32 | + |
| 33 | + return s:collect_target_by_lnum(l:path, l:opts.target_lnum) |
| 34 | +endfunction |
| 35 | + |
| 36 | +" }}}1 |
| 37 | +function! wiki#page#refile#move(source, target) abort " {{{1 |
| 38 | + " Arguments: |
| 39 | + " source: dict(path, lnum, lnum_end) |
| 40 | + " target: dict(path, lnum) |
| 41 | + |
| 42 | + if l:target.path ==# l:source.path |
| 43 | + call execute(printf('%d,%dm %d', |
| 44 | + \ l:source.lnum, l:source.lnum_end, l:target.lnum)) |
| 45 | + silent write |
| 46 | + else |
| 47 | + let l:lines = getline(l:source.lnum, l:source.lnum_end) |
| 48 | + call deletebufline('', l:source.lnum, l:source.lnum_end) |
| 49 | + silent write |
| 50 | + |
| 51 | + let l:current_bufnr = bufnr('') |
| 52 | + let l:was_loaded = bufloaded(l:target.path) |
| 53 | + keepalt execute 'silent edit' fnameescape(l:target.path) |
| 54 | + call append(l:target.lnum, l:lines) |
| 55 | + silent write |
| 56 | + if !l:was_loaded |
| 57 | + keepalt execute 'bwipeout' |
| 58 | + endif |
| 59 | + keepalt execute 'buffer' l:current_bufnr |
| 60 | + endif |
| 61 | +endfunction |
| 62 | + |
| 63 | +" }}}1 |
| 64 | + |
| 65 | +function! s:collect_target_anchor_before(path, anchor) abort " {{{1 |
| 66 | + let l:target_toc = wiki#toc#get_entries(#{ path: l:target.path }) |
| 67 | + |
| 68 | + for l:target_sec in l:target_toc |
| 69 | + if l:target_sec.anchor ==# l:opts.target_anchor_before |
| 70 | + break |
| 71 | + endif |
| 72 | + endfor |
| 73 | + |
| 74 | + if empty(l:target_sec) |
| 75 | + throw 'wiki.vim: anchor not recognized' |
| 76 | + endif |
| 77 | + |
| 78 | + let l:target.lnum = l:target_sec.lnum |
| 79 | + |
| 80 | + let l:target_anchors = get(l:target_sec, 'anchors', []) |
| 81 | + if len(l:target_anchors) > 0 |
| 82 | + call remove(l:target_anchors, -1) |
| 83 | + endif |
| 84 | + call add(l:target_anchors, l:source.anchors[-1]) |
| 85 | + let l:target.anchor = '#' . join(l:target_anchors, '#') |
| 86 | +endfunction |
| 87 | + |
| 88 | +" }}}1 |
| 89 | +function! s:collect_target_by_lnum(path, lnum) abort " {{{1 |
| 90 | + let l:current_anchors = get(wiki#toc#get_section(#{ |
| 91 | + \ path: l:target.path, |
| 92 | + \ at_lnum: l:.target_lnum |
| 93 | + \}), 'anchors', []) |
| 94 | + let l:target_anchors = l:source.level > 1 |
| 95 | + \ ? l:current_anchors[:l:source.level - 2] |
| 96 | + \ : [] |
| 97 | + call add(l:target_anchors, l:source.anchors[-1]) |
| 98 | + let l:target.anchor = '#' . join(l:target_anchors, '#') |
| 99 | + |
| 100 | + let l:target = { |
| 101 | + \ 'path': a:path, |
| 102 | + \ 'lnum': a:lnum, |
| 103 | + \} |
| 104 | + |
| 105 | +endfunction |
| 106 | + |
| 107 | +" }}}1 |
0 commit comments