Skip to content

Commit 3b120ea

Browse files
feat(globals): Add Org.capture global helper
1 parent 11037e6 commit 3b120ea

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

docs/index.org

+2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ List of available actions:
6161
- =:Org help= - Open this documentation in new tab, set working directory to the docs folder for the tab to allow browsing
6262
- =:Org helpgrep= - Open search agenda view that allows searching through the documentation
6363
- =: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.
64+
- =:Org capture {type}= - Open capture template by the shortcut, for example =:Org capture t=.
6465

6566
All of the commands above can be executed through the global Lua =Org= variable. Examples:
6667
- =Org.help()=
6768
- =Org.helpgrep()=
6869
- =Org.open()= - Opens =agenda= view
6970
- =Org.open.m()= - Opens =tags= view
71+
- =Org.capture.t()= - Opens capture template for =t= shortcut

lua/orgmode/org/global.lua

+35-18
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,50 @@ local current_file_path = string.sub(debug.getinfo(1, 'S').source, 2)
22
local docs_dir = vim.fn.fnamemodify(current_file_path, ':p:h:h:h:h') .. '/docs'
33

44
---@param orgmode Org
5-
local build = function(orgmode)
5+
---@param config OrgConfig
6+
local function generate_open_object(orgmode, config)
67
local Open = setmetatable({}, {
78
__call = function(t, ...)
89
t.a(...)
910
end,
10-
__index = function(t, k)
11-
local existing = rawget(t, k)
12-
if existing then
13-
return existing
14-
end
11+
})
1512

16-
---@diagnostic disable-next-line: invisible
17-
local keys = orgmode.agenda:_build_menu():get_valid_keys()
13+
local agenda_keys = { 'a', 't', 'm', 'M', 's' }
14+
if config.org_agenda_custom_commands then
15+
for key, _ in pairs(config.org_agenda_custom_commands) do
16+
table.insert(agenda_keys, key)
17+
end
18+
end
1819

19-
for key, item in pairs(keys) do
20-
t[key] = item.action
21-
end
20+
table.sort(agenda_keys)
2221

23-
return rawget(t, k)
24-
end,
25-
})
22+
for _, key in ipairs(agenda_keys) do
23+
Open[key] = function()
24+
return orgmode.agenda:open_by_key(key)
25+
end
26+
end
2627

27-
for _, shortcut in ipairs({ 'a', 't', 'm', 'M', 's' }) do
28-
Open[shortcut] = function()
29-
return orgmode.agenda:open_by_key(shortcut)
28+
return Open
29+
end
30+
31+
---@param orgmode Org
32+
---@param config OrgConfig
33+
local function generate_capture_object(orgmode, config)
34+
local Capture = {}
35+
36+
for key, _ in pairs(config.org_capture_templates or {}) do
37+
Capture[key] = function()
38+
return orgmode.capture:open_template_by_shortcut(key)
3039
end
3140
end
3241

42+
return Capture
43+
end
44+
45+
---@param orgmode Org
46+
local build = function(orgmode)
47+
local config = require('orgmode.config')
48+
3349
local OrgGlobal = {
3450
help = function()
3551
vim.cmd(('tabnew %s'):format(('%s/%s'):format(docs_dir, 'index.org')))
@@ -42,7 +58,8 @@ local build = function(orgmode)
4258
})
4359
end,
4460

45-
open = Open,
61+
open = generate_open_object(orgmode, config),
62+
capture = generate_capture_object(orgmode, config),
4663
}
4764

4865
_G.Org = OrgGlobal

0 commit comments

Comments
 (0)