Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2bc0072

Browse files
fix(archive): Set archive properties on correct headline
1 parent c4f11c7 commit 2bc0072

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

lua/orgmode/capture/init.lua

+9-8
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ end
205205
---Refile a headline from a regular org file (non-capture)
206206
---@private
207207
---@param opts OrgProcessRefileOpts
208+
---@return number
208209
function Capture:_refile_from_org_file(opts)
209210
local source_headline = opts.source_headline
210211
local source_file = source_headline.file
@@ -242,6 +243,7 @@ function Capture:_refile_from_org_file(opts)
242243
end
243244

244245
local range = self:_get_destination_range_without_empty_lines(Range.from_line(target_line))
246+
target_line = range.start_line
245247
vim.api.nvim_buf_set_lines(0, range.start_line, range.end_line, false, lines)
246248
end)
247249
:wait()
@@ -252,7 +254,7 @@ function Capture:_refile_from_org_file(opts)
252254
end
253255

254256
utils.echo_info(opts.message or ('Wrote %s'):format(destination_file.filename))
255-
return true
257+
return target_line + 1
256258
end
257259

258260
---@param headline OrgHeadline
@@ -278,20 +280,19 @@ function Capture:refile_file_headline_to_archive(headline)
278280

279281
local destination_file = self.files:get(archive_location)
280282

281-
self:_refile_from_org_file({
283+
local target_line = self:_refile_from_org_file({
282284
source_headline = headline,
283285
destination_file = destination_file,
284286
message = ('Archived to %s'):format(destination_file.filename),
285287
})
286288

287289
destination_file = self.files:get(archive_location)
288290
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 '')
291+
local archived_headline = archive_file:get_closest_headline({ target_line, 0 })
292+
archived_headline:set_property('ARCHIVE_TIME', Date.now():to_string())
293+
archived_headline:set_property('ARCHIVE_FILE', file.filename)
294+
archived_headline:set_property('ARCHIVE_CATEGORY', headline:get_category())
295+
archived_headline:set_property('ARCHIVE_TODO', headline:get_todo() or '')
295296
end)
296297
end
297298

0 commit comments

Comments
 (0)