diff --git a/lua/orgmode/capture/init.lua b/lua/orgmode/capture/init.lua index 637235adf..7ce6f21bd 100644 --- a/lua/orgmode/capture/init.lua +++ b/lua/orgmode/capture/init.lua @@ -278,6 +278,8 @@ function Capture:refile_file_headline_to_archive(headline) local destination_file = self.files:get(archive_location) + local headline_num_lines = #headline:get_lines() + self:_refile_from_org_file({ source_headline = headline, destination_file = destination_file, @@ -287,11 +289,11 @@ function Capture:refile_file_headline_to_archive(headline) destination_file = self.files:get(archive_location) self.files:update_file(destination_file.filename, function(archive_file) local headlines = archive_file:get_headlines_including_archived() - local last_headline = headlines[#headlines] - last_headline:set_property('ARCHIVE_TIME', Date.now():to_string()) - last_headline:set_property('ARCHIVE_FILE', file.filename) - last_headline:set_property('ARCHIVE_CATEGORY', headline:get_category()) - last_headline:set_property('ARCHIVE_TODO', headline:get_todo() or '') + local subtree_headline = headlines[#headlines - headline_num_lines + 1] + subtree_headline:set_property('ARCHIVE_TIME', Date.now():to_string()) + subtree_headline:set_property('ARCHIVE_FILE', file.filename) + subtree_headline:set_property('ARCHIVE_CATEGORY', headline:get_category()) + subtree_headline:set_property('ARCHIVE_TODO', headline:get_todo() or '') end) end diff --git a/tests/plenary/capture/capture_spec.lua b/tests/plenary/capture/capture_spec.lua index 65046cfdb..a6c423cf3 100644 --- a/tests/plenary/capture/capture_spec.lua +++ b/tests/plenary/capture/capture_spec.lua @@ -1,4 +1,5 @@ local Capture = require('orgmode.capture') +local Date = require('orgmode.objects.date') local Templates = require('orgmode.capture.templates') local Template = require('orgmode.capture.template') local helpers = require('tests.plenary.helpers') @@ -212,6 +213,60 @@ describe('Refile', function() '', }, vim.api.nvim_buf_get_lines(0, 0, -1, false)) end) + + describe('when archiving', function() + describe('a subtree with children', function() + it('sets the context on the archived headline', function() + local capture_lines = { + '* foo', + '** bar', + '*** TODO baz', + } + local capture_file = helpers.create_file(capture_lines) + local source_headline = capture_file:get_headlines()[1] + + ---@diagnostic disable-next-line: invisible + org.capture:refile_file_headline_to_archive(source_headline) + + assert.are.same({ + '* foo', + ' :PROPERTIES:', + ' :ARCHIVE_TIME: ' .. Date.now():to_string(), + ' :ARCHIVE_FILE: ' .. capture_file.filename, + ' :ARCHIVE_CATEGORY: ' .. capture_file:get_category(), + ' :ARCHIVE_TODO: ' .. (source_headline:get_todo() or ''), + ' :END:', + '** bar', + '*** TODO baz', + }, vim.api.nvim_buf_get_lines(0, 0, -1, false)) + end) + end) + + describe('a leaf headline', function() + it('sets the context on the archived headline', function() + local capture_lines = { + '* foo', + '** bar', + '*** TODO baz', + } + local capture_file = helpers.create_file(capture_lines) + local source_headline = capture_file:get_headlines()[3] + + ---@diagnostic disable-next-line: invisible + org.capture:refile_file_headline_to_archive(source_headline) + + assert.are.same({ + '* TODO baz', + ' :PROPERTIES:', + ' :ARCHIVE_TIME: ' .. Date.now():to_string(), + ' :ARCHIVE_FILE: ' .. capture_file.filename, + ' :ARCHIVE_CATEGORY: ' .. capture_file:get_category(), + ' :ARCHIVE_TODO: ' .. (source_headline:get_todo() or ''), + ' :END:', + }, vim.api.nvim_buf_get_lines(0, 0, -1, false)) + end) + end) + end) end) describe('Capture', function()