@@ -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 ))
174174endfunction
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
177250function ! wiki#page#export (line1, line2, ... ) abort " {{{1
178251 let l: cfg = deepcopy (g: wiki_export )
0 commit comments