Skip to content

Commit 50acec1

Browse files
authored
Fix wrong function access & output refile error (#313)
1 parent 57cf939 commit 50acec1

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

lua/orgmode/capture/init.lua

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ end
147147

148148
---Triggered from org file when we want to refile headline
149149
function Capture:refile_headline_to_destination()
150-
local agenda_file = Files.get_current_file()
151-
local item = agenda_file:get_closest_headline()
152-
local lines = agenda_file:get_headline_lines(item)
150+
local destination_file = Files.get_current_file()
151+
local item = destination_file:get_closest_headline()
152+
local lines = destination_file:get_headline_lines(item)
153153
return self:_refile_content_with_fallback(lines, nil, item)
154154
end
155155

@@ -194,42 +194,52 @@ function Capture:_refile_content_with_fallback(lines, fallback_file, item)
194194
destination = vim.split(destination, '/', true)
195195

196196
if not valid_destinations[destination[1]] then
197+
if not default_file then -- we know that this comes from org_refile and not org_capture_refile
198+
utils.echo_error(
199+
"'" .. destination[1] .. "' is not a file specified in the 'org_agenda_files' setting. Refiling cancelled."
200+
)
201+
return
202+
end
197203
return self:_refile_to_end(default_file, lines, item)
198204
end
199205

200206
local destination_file = valid_destinations[destination[1]]
201-
if not destination[2] or destination[2] == '' then
207+
local destination_headline = destination[2]
208+
if not destination_headline or destination_headline == '' then
202209
return self:_refile_to_end(destination_file, lines, item)
203210
end
204-
return self:refile_to_headline(destination_file, lines, item, destination[2])
211+
return self:refile_to_headline(destination_file, lines, item, destination_headline)
205212
end
206213

207-
---@param destination_file string
214+
---@param destination_filename string
208215
---@param lines string[]
209216
---@param item? Section
210217
---@param headline_title? string
211-
function Capture:refile_to_headline(destination_file, lines, item, headline_title)
212-
local agenda_file = Files.get(destination_file)
218+
function Capture:refile_to_headline(destination_filename, lines, item, headline_title)
219+
local destination_file = Files.get(destination_filename)
213220
local headline
214221
if headline_title then
215-
headline = agenda_file:find_headline_by_title(headline_title, true)
216-
end
222+
headline = destination_file:find_headline_by_title(headline_title, true)
217223

218-
if not headline then
219-
return self._refile_to_end(destination_file, lines, item)
224+
if not headline then
225+
utils.echo_error(
226+
"headline '" .. headline_title .. "' does not exist in '" .. destination_filename .. "'. Aborted refiling."
227+
)
228+
return false
229+
end
220230
end
221231

222232
if item and item.level <= headline.level then
223233
-- Refiling in same file just moves the lines from one position
224234
-- to another,so we need to apply demote instantly
225-
local is_same_file = agenda_file.filename == item.root.filename
235+
local is_same_file = destination_file.filename == item.root.filename
226236
lines = item:demote(headline.level - item.level + 1, true, not is_same_file)
227237
end
228-
local refiled = self:_refile_to(destination_file, lines, item, headline.range.end_line)
238+
local refiled = self:_refile_to(destination_filename, lines, item, headline.range.end_line)
229239
if not refiled then
230240
return false
231241
end
232-
utils.echo_info(string.format('Wrote %s', destination_file))
242+
utils.echo_info(string.format('Wrote %s', destination_filename))
233243
return true
234244
end
235245

0 commit comments

Comments
 (0)