@@ -50,12 +50,12 @@ function M.refile(closest_headline)
5050 local entry = action_state .get_selected_entry ()
5151 actions .close (prompt_bufnr )
5252
53- -- Refile to the file by default
54- local destination = entry .value .file
53+ local destination = entry .value .headline
54+ and org .get_api_headline (entry .filename , entry .value .headline .line_number )
55+ or org .get_api_file (entry .filename )
5556
56- -- Refile to a specific heading if is set
57- if entry .value .headline then
58- destination = entry .value .headline
57+ if not destination then
58+ error (' Could not find destination headline or file' )
5959 end
6060
6161 return org .refile ({
@@ -72,14 +72,15 @@ function M.insert(_)
7272 --- @type MatchEntry
7373 local entry = action_state .get_selected_entry ()
7474
75- local destination = (function ()
76- if entry .value .headline then
77- -- Link to a specific heading if is set
78- return entry .value .headline :get_link ()
79- else
80- return entry .value .file :get_link ()
81- end
82- end )()
75+ local api_object = entry .value .headline
76+ and org .get_api_headline (entry .filename , entry .value .headline .line_number )
77+ or org .get_api_file (entry .filename )
78+
79+ if not api_object then
80+ error (' Could not find ' .. (entry .value .headline and ' headline' or ' file' ) .. ' for link' )
81+ end
82+
83+ local destination = api_object :get_link ()
8384
8485 org .insert_link (destination )
8586 return true
0 commit comments