Skip to content

Commit 2c0dbe4

Browse files
author
troiganto
committed
feat(global): add :Org attach subcommand
This brings the feature in line with capture and agenda, which similarly have a top-level menu prompt.
1 parent 1379e6c commit 2c0dbe4

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

docs/index.org

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ List of available actions:
6464
- =:Org helpgrep= - Open search agenda view that allows searching through the documentation
6565
- =:Org agenda {type?}= - Open agenda view by the shortcut, for example =:Org agenda M= will open =tags_todo= view. When =type= is omitted, it opens up Agenda view.
6666
- =:Org capture {type?}= - Open capture template by the shortcut, for example =:Org capture t=. When =type= is omitted, it opens up Capture prompt.
67+
- =:Org attach {type?}= - Take attachment action by the shortcut, for example =:Org attach o= will prompt for an attached file to open. When =type= is omitted, it opens up Attach prompt.
6768
- =:Org install_treesitter_grammar= - Install the treesitter grammar for Orgmode. If installed, prompt to reinstall. Grammar is installed automatically
6869
on first run, but this is useful in case when there are issues with the grammar.
6970

@@ -75,3 +76,5 @@ All of the commands above can be executed through the global Lua =Org= variable.
7576
- =Org.agenda.m()= - Opens =tags= view
7677
- =Org.capture()= - Opens capture prompt
7778
- =Org.capture.t()= - Opens capture template for =t= shortcut
79+
- =Org.attach()= - Opens attach prompt
80+
- =Org.attach.c()= - Opens attach template for =c= shortcut

lua/orgmode/attach/init.lua

+19-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ end
2323

2424
---The dispatcher for attachment commands.
2525
---Shows a list of commands and prompts for another key to execute a command.
26-
---@return nil
27-
function Attach:prompt()
26+
---@return OrgMenu
27+
function Attach:_build_menu()
2828
local menu = Menu:new({
2929
title = 'Press key for an attach command',
3030
prompt = 'Press key for an attach command',
@@ -147,9 +147,26 @@ function Attach:prompt()
147147
menu:add_option({ label = 'Quit', key = 'q' })
148148
menu:add_separator({ icon = ' ', length = 1 })
149149

150+
return menu
151+
end
152+
153+
---@return nil
154+
function Attach:prompt()
155+
local menu = self:_build_menu()
150156
return menu:open()
151157
end
152158

159+
---@param key string
160+
---@return string?
161+
function Attach:open_by_key(key)
162+
local menu = self:_build_menu()
163+
local item = menu:get_entry_by_key(key)
164+
if not item then
165+
return utils.echo_error('No attachment action with key ' .. key)
166+
end
167+
return item.action()
168+
end
169+
153170
---Get the current attachment node.
154171
---
155172
---@return OrgAttachNode

lua/orgmode/org/global.lua

+20
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ local function generate_capture_object(orgmode, config)
4646
return Capture
4747
end
4848

49+
---@param orgmode Org
50+
---@param config OrgConfig
51+
local function generate_attach_object(orgmode, config)
52+
local Attach = setmetatable({}, {
53+
__call = function()
54+
return orgmode.attach:prompt()
55+
end,
56+
})
57+
58+
local attach_keys = { 'a', 'c', 'm', 'l', 'y', 'u', 'b', 'n', 'z', 'o', 'O', 'f', 'F', 'd', 'D', 's', 'S' }
59+
for _, key in ipairs(attach_keys) do
60+
Attach[key] = function()
61+
return orgmode.agenda:open_by_key(key)
62+
end
63+
end
64+
65+
return Attach
66+
end
67+
4968
---@param orgmode Org
5069
local build = function(orgmode)
5170
local config = require('orgmode.config')
@@ -75,6 +94,7 @@ local build = function(orgmode)
7594

7695
agenda = generate_agenda_object(orgmode, config),
7796
capture = generate_capture_object(orgmode, config),
97+
attach = generate_attach_object(orgmode, config),
7898
}
7999

80100
_G.Org = OrgGlobal

0 commit comments

Comments
 (0)