Skip to content

Commit 2c82236

Browse files
fix(archive): Properly archive headlines with level > 1
Fixes #729
1 parent d2fde79 commit 2c82236

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed

lua/orgmode/capture/_meta.lua

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
---@field source_headline OrgHeadline
55
---@field destination_file? OrgFile
66
---@field destination_headline? OrgHeadline
7-
---@field lines? string[]
87
---@field message? string
98

109
---@class OrgProcessCaptureOpts

lua/orgmode/capture/init.lua

+12-33
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,7 @@ function Capture:_refile_from_org_file(opts)
228228
target_line = destination_headline:get_range().end_line
229229
end
230230

231-
local lines = opts.lines
232-
233-
if not lines then
234-
lines = source_headline:get_lines()
235-
end
231+
local lines = source_headline:get_lines()
236232

237233
if destination_headline or source_headline:get_level() > 1 then
238234
lines = self:_adapt_headline_level(source_headline, target_level, is_same_file)
@@ -279,41 +275,24 @@ function Capture:refile_file_headline_to_archive(headline)
279275
if not vim.loop.fs_stat(archive_location) then
280276
vim.fn.writefile({}, archive_location)
281277
end
282-
local start_line = headline:get_range().start_line
283-
local lines = headline:get_lines()
284-
local properties_node = headline:get_properties()
285-
local append_line = headline:get_append_line() - start_line
286-
local indent = headline:get_indent()
287-
288-
local archive_props = {
289-
('%s:ARCHIVE_TIME: %s'):format(indent, Date.now():to_string()),
290-
('%s:ARCHIVE_FILE: %s'):format(indent, file.filename),
291-
('%s:ARCHIVE_CATEGORY: %s'):format(indent, headline:get_category()),
292-
('%s:ARCHIVE_TODO: %s'):format(indent, headline:get_todo() or ''),
293-
}
294-
295-
if properties_node then
296-
local front_lines = { unpack(lines, 1, append_line) }
297-
local back_lines = { unpack(lines, append_line + 1, #lines) }
298-
lines = vim.list_extend(front_lines, archive_props)
299-
lines = vim.list_extend(lines, back_lines)
300-
else
301-
local front_lines = { unpack(lines, 1, append_line + 1) }
302-
local back_lines = { unpack(lines, append_line + 2, #lines) }
303-
table.insert(front_lines, ('%s:PROPERTIES:'):format(indent))
304-
lines = vim.list_extend(front_lines, archive_props)
305-
table.insert(lines, ('%s:END:'):format(indent))
306-
lines = vim.list_extend(lines, back_lines)
307-
end
308278

309279
local destination_file = self.files:get(archive_location)
310280

311-
return self:_refile_from_org_file({
281+
self:_refile_from_org_file({
312282
source_headline = headline,
313283
destination_file = destination_file,
314-
lines = lines,
315284
message = ('Archived to %s'):format(destination_file.filename),
316285
})
286+
287+
destination_file = self.files:get(archive_location)
288+
self.files:update_file(destination_file.filename, function(archive_file)
289+
local headlines = archive_file:get_headlines_including_archived()
290+
local last_headline = headlines[#headlines]
291+
last_headline:set_property('ARCHIVE_TIME', Date.now():to_string())
292+
last_headline:set_property('ARCHIVE_FILE', file.filename)
293+
last_headline:set_property('ARCHIVE_CATEGORY', headline:get_category())
294+
last_headline:set_property('ARCHIVE_TODO', headline:get_todo() or '')
295+
end)
317296
end
318297

319298
---@param item OrgHeadline

tests/plenary/ui/mappings/archive_spec.lua

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ describe('Archive', function()
1111
assert.are.same(vim.fn.glob(vim.fn.fnamemodify(file.filename, ':p:h') .. '/**/*.org_archive', false, 1, 1), {})
1212

1313
vim.cmd([[exe "norm ,o$"]])
14+
-- Pause to finish the archiving
15+
vim.wait(50)
1416
assert.are.same({
1517
'* baz',
1618
'** foo',

0 commit comments

Comments
 (0)