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

Commit 1afac86

Browse files
feat(api): Add open_by_key function for agenda
1 parent 86ed523 commit 1afac86

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

lua/orgmode/agenda/init.lua

+43-26
Original file line numberDiff line numberDiff line change
@@ -162,32 +162,8 @@ function Agenda:_build_custom_commands()
162162
end
163163

164164
---@private
165-
---@return number buffer number
166-
function Agenda:_open_window()
167-
-- if an agenda window is already open, return it
168-
for _, win in ipairs(vim.api.nvim_list_wins()) do
169-
local buf = vim.api.nvim_win_get_buf(win)
170-
local ft = vim.api.nvim_get_option_value('filetype', {
171-
buf = buf,
172-
})
173-
if ft == 'orgagenda' then
174-
vim.bo[buf].modifiable = true
175-
colors.highlight({}, true, buf)
176-
vim.api.nvim_buf_set_lines(buf, 0, -1, true, {})
177-
return buf
178-
end
179-
end
180-
181-
utils.open_window('orgagenda', math.max(34, config.org_agenda_min_height), config.win_split_mode, config.win_border)
182-
183-
vim.cmd([[setf orgagenda]])
184-
vim.cmd([[setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap nospell]])
185-
vim.w.org_window_pos = vim.fn.win_screenpos(0)
186-
config:setup_mappings('agenda', vim.api.nvim_get_current_buf())
187-
return vim.fn.bufnr()
188-
end
189-
190-
function Agenda:prompt()
165+
---@return OrgMenu
166+
function Agenda:_build_menu()
191167
local menu = Menu:new({
192168
title = 'Press key for an agenda command',
193169
prompt = 'Press key for an agenda command',
@@ -243,6 +219,47 @@ function Agenda:prompt()
243219
menu:add_option({ label = 'Quit', key = 'q' })
244220
menu:add_separator({ icon = ' ', length = 1 })
245221

222+
return menu
223+
end
224+
225+
---@private
226+
---@return number buffer number
227+
function Agenda:_open_window()
228+
-- if an agenda window is already open, return it
229+
for _, win in ipairs(vim.api.nvim_list_wins()) do
230+
local buf = vim.api.nvim_win_get_buf(win)
231+
local ft = vim.api.nvim_get_option_value('filetype', {
232+
buf = buf,
233+
})
234+
if ft == 'orgagenda' then
235+
vim.bo[buf].modifiable = true
236+
colors.highlight({}, true, buf)
237+
vim.api.nvim_buf_set_lines(buf, 0, -1, true, {})
238+
return buf
239+
end
240+
end
241+
242+
utils.open_window('orgagenda', math.max(34, config.org_agenda_min_height), config.win_split_mode, config.win_border)
243+
244+
vim.cmd([[setf orgagenda]])
245+
vim.cmd([[setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap nospell]])
246+
vim.w.org_window_pos = vim.fn.win_screenpos(0)
247+
config:setup_mappings('agenda', vim.api.nvim_get_current_buf())
248+
return vim.fn.bufnr()
249+
end
250+
251+
---@param key string
252+
function Agenda:open_by_key(key)
253+
local menu = self:_build_menu()
254+
local item = menu:get_entry_by_key(key)
255+
if not item then
256+
return utils.echo_error('No agenda view with key ' .. key)
257+
end
258+
return item.action()
259+
end
260+
261+
function Agenda:prompt()
262+
local menu = self:_build_menu()
246263
return menu:open()
247264
end
248265

lua/orgmode/api/agenda.lua

+5
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,9 @@ function OrgAgenda.tags(options)
9797
orgmode.agenda:tags(opts)
9898
end
9999

100+
---@param key string Key in the agenda prompt (for example: "a", "t", "m", "M")
101+
function OrgAgenda.open_by_key(key)
102+
return orgmode.agenda:open_by_key(key)
103+
end
104+
100105
return OrgAgenda

lua/orgmode/ui/menu.lua

+10
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ function Menu._default_menu(data)
135135
return entry.action()
136136
end
137137

138+
function Menu:get_entry_by_key(key)
139+
local valid_keys = {}
140+
for _, item in ipairs(self.items) do
141+
if item.key then
142+
valid_keys[item.key] = item
143+
end
144+
end
145+
return valid_keys[key]
146+
end
147+
138148
function Menu:open()
139149
local menu_data = {
140150
title = self.title,

0 commit comments

Comments
 (0)