@@ -162,6 +162,72 @@ function! wiki#page#rename_section(...) abort "{{{1
162162 call s: update_links_external (l: source , l: target )
163163endfunction
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
166232function ! wiki#page#export (line1, line2, ... ) abort " {{{1
167233 let l: cfg = deepcopy (g: wiki_export )
0 commit comments