Skip to content

Commit 2c806ca

Browse files
feat(api): Add get_headline_at_cursor to Agenda api
Closes #816
1 parent e65a661 commit 2c806ca

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lua/orgmode/api/agenda.lua

+18
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,22 @@ function OrgAgenda.open_by_key(key)
102102
return orgmode.agenda:open_by_key(key)
103103
end
104104

105+
---Get the headline at the cursor position in the agenda view
106+
---@return OrgApiHeadline | nil
107+
function OrgAgenda.get_headline_at_cursor()
108+
local line_nr = vim.fn.line('.')
109+
local agenda_views = orgmode.agenda.views
110+
111+
for _, view in ipairs(agenda_views) do
112+
local agenda_line = view:get_line(line_nr)
113+
if agenda_line then
114+
local headline = agenda_line.headline
115+
if headline then
116+
local file = require('orgmode.api').load(headline.file.filename)
117+
return file:get_headline_on_line(headline:get_range().start_line)
118+
end
119+
end
120+
end
121+
end
122+
105123
return OrgAgenda

lua/orgmode/api/file.lua

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ function OrgFile:get_closest_headline(cursor)
9393
return nil
9494
end
9595

96+
---@param line_number number
97+
---@return OrgApiHeadline | nil
98+
function OrgFile:get_headline_on_line(line_number)
99+
return vim.tbl_filter(function(headline)
100+
return headline.position.start_line == line_number
101+
end, self.headlines)[1]
102+
end
103+
96104
--- Get a link destination as string
97105
---
98106
--- Depending if org_id_link_to_org_use_id is set the format is

0 commit comments

Comments
 (0)