Skip to content

Commit dfcb945

Browse files
feat(archive): set outline path when archiving (#746)
1 parent a5aeb14 commit dfcb945

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

lua/orgmode/capture/init.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ function Capture:refile_file_headline_to_archive(headline)
275275
end
276276

277277
local destination_file = self.files:get(archive_location)
278+
local todo_state = headline:get_todo()
278279

279280
local target_line = self:_refile_from_org_file({
280281
source_headline = headline,
@@ -287,8 +288,12 @@ function Capture:refile_file_headline_to_archive(headline)
287288
local archived_headline = archive_file:get_closest_headline({ target_line, 0 })
288289
archived_headline:set_property('ARCHIVE_TIME', Date.now():to_string())
289290
archived_headline:set_property('ARCHIVE_FILE', file.filename)
291+
local outline_path = headline:get_outline_path()
292+
if outline_path ~= '' then
293+
archived_headline:set_property('ARCHIVE_OLPATH', outline_path)
294+
end
290295
archived_headline:set_property('ARCHIVE_CATEGORY', headline:get_category())
291-
archived_headline:set_property('ARCHIVE_TODO', headline:get_todo() or '')
296+
archived_headline:set_property('ARCHIVE_TODO', todo_state or '')
292297
end)
293298
end
294299

lua/orgmode/files/headline.lua

+26
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,32 @@ function Headline:get_category()
226226
return self.file:get_category()
227227
end
228228

229+
memoize('get_outline_path')
230+
--- @return string
231+
function Headline:get_outline_path()
232+
local inner_to_outer_parent_headlines = {}
233+
local parent_section = self:node():parent():parent()
234+
235+
while parent_section do
236+
local headline_node = parent_section:field('headline')[1]
237+
if headline_node then
238+
local headline = Headline:new(headline_node, self.file)
239+
local headline_title = headline:get_title()
240+
table.insert(inner_to_outer_parent_headlines, headline_title)
241+
end
242+
parent_section = parent_section:parent()
243+
end
244+
245+
-- reverse headline order
246+
local outer_to_inner_parent_headlines = {}
247+
for i = #inner_to_outer_parent_headlines, 1, -1 do
248+
table.insert(outer_to_inner_parent_headlines, inner_to_outer_parent_headlines[i])
249+
end
250+
251+
local outline_path = table.concat(outer_to_inner_parent_headlines, '/')
252+
return outline_path
253+
end
254+
229255
---@param tags string
230256
function Headline:set_tags(tags)
231257
---@type TSNode

tests/plenary/ui/mappings/archive_spec.lua

+32
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('Archive', function()
2828
' :PROPERTIES:',
2929
' :ARCHIVE_TIME: ' .. now:to_string(),
3030
' :ARCHIVE_FILE: ' .. file.filename,
31+
-- no ARCHIVE_OLPATH because top-level headline
3132
' :ARCHIVE_CATEGORY: ' .. file:get_category(),
3233
' :ARCHIVE_TODO: ',
3334
' :END:',
@@ -55,10 +56,41 @@ describe('Archive', function()
5556
' :PROPERTIES:',
5657
' :ARCHIVE_TIME: ' .. now:to_string(),
5758
' :ARCHIVE_FILE: ' .. file.filename,
59+
-- no ARCHIVE_OLPATH because top-level headline
5860
' :ARCHIVE_CATEGORY: ' .. file:get_category(),
5961
' :ARCHIVE_TODO: ',
6062
' :END:',
6163
'** baz',
6264
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
6365
end)
66+
67+
it('sets outline path when archiving lower-level headline', function()
68+
local file = helpers.create_agenda_file({
69+
'* foo',
70+
'** TODO bar',
71+
'*** TODO baz',
72+
' Body text baz',
73+
})
74+
75+
vim.cmd([[exe "norm G,o$"]])
76+
-- Pause to finish the archiving
77+
vim.wait(50)
78+
assert.are.same({
79+
'* foo',
80+
'** TODO bar',
81+
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
82+
83+
vim.cmd(('edit %s'):format(file.filename .. '_archive'))
84+
assert.are.same({
85+
'* TODO baz', -- keep todo state in the title
86+
' :PROPERTIES:',
87+
' :ARCHIVE_TIME: ' .. Date.now():to_string(),
88+
' :ARCHIVE_FILE: ' .. file.filename,
89+
' :ARCHIVE_OLPATH: foo/bar', -- remove todo state in any headline in outline path
90+
' :ARCHIVE_CATEGORY: ' .. file:get_category(),
91+
' :ARCHIVE_TODO: TODO',
92+
' :END:',
93+
' Body text baz',
94+
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
95+
end)
6496
end)

0 commit comments

Comments
 (0)