Skip to content

Commit

Permalink
feat(globals): Add Org.capture global helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Jan 20, 2025
1 parent 11037e6 commit 3b120ea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
2 changes: 2 additions & 0 deletions docs/index.org
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ List of available actions:
- =:Org help= - Open this documentation in new tab, set working directory to the docs folder for the tab to allow browsing
- =:Org helpgrep= - Open search agenda view that allows searching through the documentation
- =: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.
- =:Org capture {type}= - Open capture template by the shortcut, for example =:Org capture t=.

All of the commands above can be executed through the global Lua =Org= variable. Examples:
- =Org.help()=
- =Org.helpgrep()=
- =Org.open()= - Opens =agenda= view
- =Org.open.m()= - Opens =tags= view
- =Org.capture.t()= - Opens capture template for =t= shortcut
53 changes: 35 additions & 18 deletions lua/orgmode/org/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,50 @@ local current_file_path = string.sub(debug.getinfo(1, 'S').source, 2)
local docs_dir = vim.fn.fnamemodify(current_file_path, ':p:h:h:h:h') .. '/docs'

---@param orgmode Org
local build = function(orgmode)
---@param config OrgConfig
local function generate_open_object(orgmode, config)
local Open = setmetatable({}, {
__call = function(t, ...)
t.a(...)
end,
__index = function(t, k)
local existing = rawget(t, k)
if existing then
return existing
end
})

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

for key, item in pairs(keys) do
t[key] = item.action
end
table.sort(agenda_keys)

return rawget(t, k)
end,
})
for _, key in ipairs(agenda_keys) do
Open[key] = function()
return orgmode.agenda:open_by_key(key)
end
end

for _, shortcut in ipairs({ 'a', 't', 'm', 'M', 's' }) do
Open[shortcut] = function()
return orgmode.agenda:open_by_key(shortcut)
return Open
end

---@param orgmode Org
---@param config OrgConfig
local function generate_capture_object(orgmode, config)
local Capture = {}

for key, _ in pairs(config.org_capture_templates or {}) do
Capture[key] = function()
return orgmode.capture:open_template_by_shortcut(key)
end
end

return Capture
end

---@param orgmode Org
local build = function(orgmode)
local config = require('orgmode.config')

local OrgGlobal = {
help = function()
vim.cmd(('tabnew %s'):format(('%s/%s'):format(docs_dir, 'index.org')))
Expand All @@ -42,7 +58,8 @@ local build = function(orgmode)
})
end,

open = Open,
open = generate_open_object(orgmode, config),
capture = generate_capture_object(orgmode, config),
}

_G.Org = OrgGlobal
Expand Down

0 comments on commit 3b120ea

Please sign in to comment.