Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b2285be

Browse files
feat: Add global :Org command and global Org lua variable
These allow interacting with orgmode in a more user friendly way
1 parent 8db7711 commit b2285be

File tree

5 files changed

+123
-2
lines changed

5 files changed

+123
-2
lines changed

docs/index.org

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
Nvim orgmode is a clone of Emacs Orgmode for Neovim 0.10.0+.
44
It aims to be a feature-complete implementation of Orgmode features in Neovim.
55

6+
💡 TIP: To view this documentation offline in Neovim, run =:Org help=. More info in [[#globals-and-commands][Globals and commands]] section.
7+
8+
69
** Quick start
710
:PROPERTIES:
811
:CUSTOM_ID: quick-start
@@ -45,3 +48,22 @@ that demonstrates how the similar Orgmode clone [[https://github.com/dhruvasagar
4548
:END:
4649
Nvim-orgmode exoses a Lua API that can be used to interact with the orgmode. To view it, check [[file:../docs/orgmode-api.txt][orgmode-api.txt]]
4750
or do =:h OrgApi= in Neovim.
51+
52+
** Globals and commands
53+
:PROPERTIES:
54+
:CUSTOM_ID: globals-and-commands
55+
:END:
56+
There are 2 additional ways to interact with Orgmode:
57+
1. Through the =:Org= command
58+
2. Through =Org= Lua global variable
59+
60+
List of available actions:
61+
- =:Org help= - Open this documentation in new tab, set working directory to the docs folder for the tab to allow browsing
62+
- =:Org helpgrep= - Open search agenda view that allows searching through the documentation
63+
- =: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+
65+
All of the commands above can be executed through the global Lua =Org= variable. Examples:
66+
- =Org.help()=
67+
- =Org.helpgrep()=
68+
- =Org.open()= - Opens =agenda= view
69+
- =Org.open.m()= - Opens =tags= view

lua/orgmode/agenda/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ local AgendaTypes = require('orgmode.agenda.types')
1717
local Agenda = {}
1818

1919
---@param opts? { highlighter: OrgHighlighter, files: OrgFiles }
20+
---@return OrgAgenda
2021
function Agenda:new(opts)
2122
opts = opts or {}
2223
local data = {

lua/orgmode/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
_G.orgmode = _G.orgmode or {}
2+
_G.Org = _G.Org or {}
23
---@type Org | nil
34
local instance = nil
45

@@ -35,6 +36,7 @@ setmetatable(Org, {
3536
})
3637

3738
function Org:new()
39+
require('orgmode.org.global')(self)
3840
self.initialized = false
3941
self:setup_autocmds()
4042
require('orgmode.config'):setup_ts_predicates()

lua/orgmode/org/global.lua

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
local current_file_path = string.sub(debug.getinfo(1, 'S').source, 2)
2+
local docs_dir = vim.fn.fnamemodify(current_file_path, ':p:h:h:h:h') .. '/docs'
3+
4+
---@param orgmode Org
5+
local build = function(orgmode)
6+
local Open = setmetatable({}, {
7+
__call = function(t, ...)
8+
t.a(...)
9+
end,
10+
__index = function(t, k)
11+
local existing = rawget(t, k)
12+
if existing then
13+
return existing
14+
end
15+
16+
---@diagnostic disable-next-line: invisible
17+
local keys = orgmode.agenda:_build_menu():get_valid_keys()
18+
19+
for key, item in pairs(keys) do
20+
t[key] = item.action
21+
end
22+
23+
return rawget(t, k)
24+
end,
25+
})
26+
27+
for _, shortcut in ipairs({ 'a', 't', 'm', 'M', 's' }) do
28+
Open[shortcut] = function()
29+
return orgmode.agenda:open_by_key(shortcut)
30+
end
31+
end
32+
33+
local OrgGlobal = {
34+
help = function()
35+
vim.cmd(('tabnew %s'):format(('%s/%s'):format(docs_dir, 'index.org')))
36+
vim.cmd(('tcd %s'):format(docs_dir))
37+
end,
38+
39+
helpgrep = function()
40+
orgmode.agenda:open_view('search', {
41+
agenda_files = ('%s/**/*'):format(docs_dir),
42+
})
43+
end,
44+
45+
open = Open,
46+
}
47+
48+
_G.Org = OrgGlobal
49+
end
50+
51+
---@param opts string[]
52+
---@return table
53+
local function resolve_item(opts)
54+
---@type table
55+
local obj = _G.Org
56+
for _, opt in ipairs(opts) do
57+
if type(obj) ~= 'table' then
58+
return obj
59+
end
60+
if obj[opt] then
61+
obj = obj[opt]
62+
end
63+
end
64+
65+
return obj
66+
end
67+
68+
vim.api.nvim_create_user_command('Org', function(opts)
69+
local item = resolve_item(opts.fargs)
70+
if item and type(item) == 'function' then
71+
return item()
72+
end
73+
require('orgmode.utils').echo_error(('Invalid command "Org %s"'):format(opts.args))
74+
end, {
75+
nargs = '*',
76+
complete = function(arg_lead, cmd_line)
77+
local opts = vim.split(cmd_line:sub(5), '%s+')
78+
local item = resolve_item(opts)
79+
if type(item) ~= 'table' then
80+
return {}
81+
end
82+
local list = vim.tbl_keys(item)
83+
84+
if arg_lead == '' then
85+
return list
86+
end
87+
return vim.fn.matchfuzzy(list, arg_lead)
88+
end,
89+
})
90+
91+
return build

lua/orgmode/ui/menu.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,19 @@ function Menu._default_menu(data)
135135
return entry.action()
136136
end
137137

138-
function Menu:get_entry_by_key(key)
138+
---@return table<string, OrgMenuOption | OrgMenuSeparator>
139+
function Menu:get_valid_keys()
139140
local valid_keys = {}
140141
for _, item in ipairs(self.items) do
141142
if item.key then
142143
valid_keys[item.key] = item
143144
end
144145
end
145-
return valid_keys[key]
146+
return valid_keys
147+
end
148+
149+
function Menu:get_entry_by_key(key)
150+
return self:get_valid_keys()[key]
146151
end
147152

148153
function Menu:open()

0 commit comments

Comments
 (0)