Skip to content

Commit a37415f

Browse files
author
Sebastian Flügge
committed
feat: allow refiling within agenda
1 parent ae4197a commit a37415f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lua/telescope-orgmode/org.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local OrgApi = require('orgmode.api')
2+
local OrgAgendaApi = require('orgmode.api.agenda')
23

34
local M = {}
45

@@ -89,10 +90,19 @@ end
8990
--- In case of nested sections, it is the closest headline within the headline
9091
--- tree.
9192
---
92-
--- The precondition to run this function successfully is, that the cursor is
93-
--- placed in an orgfile when the function is called.
93+
--- Works in both org files and agenda views:
94+
--- - For org files: Uses treesitter parser to search the tree
95+
--- - For agenda views: Uses the agenda API to get the headline at cursor
9496
function M.get_closest_headline()
95-
return OrgApi.current():get_closest_headline()
97+
-- Handle different buffer types explicitly
98+
if vim.bo.filetype == 'org' then
99+
return OrgApi.current():get_closest_headline()
100+
elseif vim.bo.filetype == 'orgagenda' then
101+
return OrgAgendaApi.get_headline_at_cursor()
102+
end
103+
104+
-- Not in org or agenda buffer
105+
return nil
96106
end
97107

98108
--- Get the API headline object for a given filename and line number
@@ -153,7 +163,7 @@ function M.get_link_destination(entry, opts)
153163
if entry.value.headline and opts.original_file and entry.filename == opts.original_file then
154164
return M.get_intra_file_link(entry)
155165
end
156-
166+
157167
-- Use inter-file format for everything else
158168
return M.get_inter_file_link(entry)
159169
end

lua/telescope-orgmode/picker/refile_heading.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ return function(opts)
1515
}, 'headlines')
1616

1717
local closest_headline = org.get_closest_headline()
18+
19+
if not closest_headline then
20+
local filetype = vim.bo.filetype
21+
if filetype == 'org' then
22+
vim.notify('No headline found at cursor position in org file', vim.log.levels.WARN)
23+
else
24+
vim.notify('No headline found at cursor position. Make sure cursor is on a valid agenda item or org headline.', vim.log.levels.WARN)
25+
end
26+
return
27+
end
1828

1929
pickers
2030
.new(opts, {

0 commit comments

Comments
 (0)