Skip to content

Commit 659aa16

Browse files
author
troiganto
committed
feat(global): add :Org attach subcommand
1 parent f949e7f commit 659aa16

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

Diff for: 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

Diff for: lua/orgmode/attach/init.lua

+19-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ end
2424

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

156+
return menu
157+
end
158+
159+
---@return nil
160+
function Attach:prompt()
161+
local menu = self:_build_menu()
156162
return menu:open()
157163
end
158164

165+
---@param key string
166+
---@return string?
167+
function Attach:open_by_key(key)
168+
local menu = self:_build_menu()
169+
local item = menu:get_entry_by_key(key)
170+
if not item then
171+
return utils.echo_error('No attachment action with key ' .. key)
172+
end
173+
return item.action()
174+
end
175+
159176
---Get the current attachment node.
160177
---
161178
---@return OrgAttachNode

Diff for: 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)