Skip to content

Commit a0f16dd

Browse files
feat(capture): Trigger on_pre_refile earlier
1 parent 837d931 commit a0f16dd

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

lua/orgmode/capture/init.lua

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,25 @@ end
465465
---@private
466466
---@return OrgProcessCaptureOpts | false
467467
function Capture:_get_refile_vars()
468-
local file = self._window.template:get_target()
468+
local source_file = self.files:get_current_file()
469+
local source_headline = nil
470+
if not self._window.template.whole_file then
471+
source_headline = source_file:get_headlines()[1]
472+
end
469473

474+
local opts = {
475+
source_file = source_file,
476+
source_headline = source_headline,
477+
destination_file = nil,
478+
destination_headline = nil,
479+
template = self._window.template,
480+
}
481+
482+
if self.on_pre_refile then
483+
self.on_pre_refile(self, opts)
484+
end
485+
486+
local file = opts.template:get_target()
470487
if vim.fn.filereadable(file) == 0 then
471488
local choice = vim.fn.confirm(('Refile destination %s does not exist. Create now?'):format(file), '&Yes\n&No')
472489
if choice ~= 1 then
@@ -477,28 +494,16 @@ function Capture:_get_refile_vars()
477494
vim.fn.writefile({}, file)
478495
end
479496

480-
local source_file = self.files:get_current_file()
481-
local source_headline = nil
482-
if not self._window.template.whole_file then
483-
source_headline = source_file:get_headlines()[1]
484-
end
485-
local destination_file = self.files:get(file)
486-
local destination_headline = nil
487-
if self._window.template.headline then
488-
destination_headline = destination_file:find_headline_by_title(self._window.template.headline)
489-
if not destination_headline then
490-
utils.echo_error(('Refile headline "%s" does not exist in "%s"'):format(self._window.template.headline, file))
497+
opts.destination_file = self.files:get(file)
498+
if opts.template.headline then
499+
opts.destination_headline = opts.destination_file:find_headline_by_title(opts.template.headline)
500+
if not opts.destination_headline then
501+
utils.echo_error(('Refile headline "%s" does not exist in "%s"'):format(opts.template.headline, file))
491502
return false
492503
end
493504
end
494505

495-
return {
496-
source_file = source_file,
497-
source_headline = source_headline,
498-
destination_file = destination_file,
499-
destination_headline = destination_headline,
500-
template = self._window.template,
501-
}
506+
return opts
502507
end
503508

504509
---@private

0 commit comments

Comments
 (0)