Skip to content

Commit 98776eb

Browse files
feat(global): Add store_link command to Org global
Closes #924
1 parent 22094c4 commit 98776eb

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

docs/index.org

+2
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ List of available actions:
6666
- =:Org capture {type?}= - Open capture template by the shortcut, for example =:Org capture t=. When =type= is omitted, it opens up Capture prompt.
6767
- =:Org install_treesitter_grammar= - Install the treesitter grammar for Orgmode. If installed, prompt to reinstall. Grammar is installed automatically
6868
on first run, but this is useful in case when there are issues with the grammar.
69+
- =:Org store_link= - [[file:/home/kristijan/github/orgmode/docs/configuration.org::#org_store_link][Store link]] to the headline under cursor. Works in both agenda and org files.
6970

7071
All of the commands above can be executed through the global Lua =Org= variable. Examples:
7172
- =Org.help()=
7273
- =Org.helpgrep()=
7374
- =Org.install_treesitter_grammar()=
75+
- =Org.store_link()=
7476
- =Org.agenda()= - Opens =agenda= prompt
7577
- =Org.agenda.m()= - Opens =tags= view
7678
- =Org.capture()= - Opens capture prompt

lua/orgmode/agenda/init.lua

+15
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,21 @@ function Agenda:_remote_edit(opts)
585585
end)
586586
end
587587

588+
---@return OrgHeadline | nil
589+
function Agenda:get_headline_at_cursor()
590+
local line_nr = vim.fn.line('.')
591+
592+
for _, view in ipairs(self.views) do
593+
local agenda_line = view:get_line(line_nr)
594+
if agenda_line then
595+
local headline = agenda_line.headline
596+
if headline then
597+
return headline
598+
end
599+
end
600+
end
601+
end
602+
588603
function Agenda:quit()
589604
vim.api.nvim_win_close(0, true)
590605
end

lua/orgmode/api/agenda.lua

+5-12
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,11 @@ end
105105
---Get the headline at the cursor position in the agenda view
106106
---@return OrgApiHeadline | nil
107107
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
108+
local headline = orgmode.agenda:get_headline_at_cursor()
109+
110+
if headline then
111+
local file = require('orgmode.api').load(headline.file.filename)
112+
return file:get_headline_on_line(headline:get_range().start_line)
120113
end
121114
end
122115

lua/orgmode/org/global.lua

+16
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ local build = function(orgmode)
7575

7676
agenda = generate_agenda_object(orgmode, config),
7777
capture = generate_capture_object(orgmode, config),
78+
79+
store_link = function()
80+
---@type OrgHeadline | nil
81+
local headline = nil
82+
if vim.bo.filetype == 'orgagenda' then
83+
headline = orgmode.agenda:get_headline_at_cursor()
84+
elseif vim.bo.filetype == 'org' then
85+
headline = orgmode.files:get_current_file():get_closest_headline_or_nil()
86+
end
87+
if not headline then
88+
require('orgmode.utils').echo_error('No headline found')
89+
return
90+
end
91+
orgmode.links:store_link_to_headline(headline)
92+
return require('orgmode.utils').echo_info('Stored: ' .. headline:get_title())
93+
end,
7894
}
7995

8096
_G.Org = OrgGlobal

0 commit comments

Comments
 (0)