diff --git a/lua/orgmode/agenda/init.lua b/lua/orgmode/agenda/init.lua index d7d8fa144..5e3c05efd 100644 --- a/lua/orgmode/agenda/init.lua +++ b/lua/orgmode/agenda/init.lua @@ -162,32 +162,8 @@ function Agenda:_build_custom_commands() end ---@private ----@return number buffer number -function Agenda:_open_window() - -- if an agenda window is already open, return it - for _, win in ipairs(vim.api.nvim_list_wins()) do - local buf = vim.api.nvim_win_get_buf(win) - local ft = vim.api.nvim_get_option_value('filetype', { - buf = buf, - }) - if ft == 'orgagenda' then - vim.bo[buf].modifiable = true - colors.highlight({}, true, buf) - vim.api.nvim_buf_set_lines(buf, 0, -1, true, {}) - return buf - end - end - - utils.open_window('orgagenda', math.max(34, config.org_agenda_min_height), config.win_split_mode, config.win_border) - - vim.cmd([[setf orgagenda]]) - vim.cmd([[setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap nospell]]) - vim.w.org_window_pos = vim.fn.win_screenpos(0) - config:setup_mappings('agenda', vim.api.nvim_get_current_buf()) - return vim.fn.bufnr() -end - -function Agenda:prompt() +---@return OrgMenu +function Agenda:_build_menu() local menu = Menu:new({ title = 'Press key for an agenda command', prompt = 'Press key for an agenda command', @@ -243,6 +219,47 @@ function Agenda:prompt() menu:add_option({ label = 'Quit', key = 'q' }) menu:add_separator({ icon = ' ', length = 1 }) + return menu +end + +---@private +---@return number buffer number +function Agenda:_open_window() + -- if an agenda window is already open, return it + for _, win in ipairs(vim.api.nvim_list_wins()) do + local buf = vim.api.nvim_win_get_buf(win) + local ft = vim.api.nvim_get_option_value('filetype', { + buf = buf, + }) + if ft == 'orgagenda' then + vim.bo[buf].modifiable = true + colors.highlight({}, true, buf) + vim.api.nvim_buf_set_lines(buf, 0, -1, true, {}) + return buf + end + end + + utils.open_window('orgagenda', math.max(34, config.org_agenda_min_height), config.win_split_mode, config.win_border) + + vim.cmd([[setf orgagenda]]) + vim.cmd([[setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap nospell]]) + vim.w.org_window_pos = vim.fn.win_screenpos(0) + config:setup_mappings('agenda', vim.api.nvim_get_current_buf()) + return vim.fn.bufnr() +end + +---@param key string +function Agenda:open_by_key(key) + local menu = self:_build_menu() + local item = menu:get_entry_by_key(key) + if not item then + return utils.echo_error('No agenda view with key ' .. key) + end + return item.action() +end + +function Agenda:prompt() + local menu = self:_build_menu() return menu:open() end diff --git a/lua/orgmode/api/agenda.lua b/lua/orgmode/api/agenda.lua index 4986e9b96..20140a044 100644 --- a/lua/orgmode/api/agenda.lua +++ b/lua/orgmode/api/agenda.lua @@ -97,4 +97,9 @@ function OrgAgenda.tags(options) orgmode.agenda:tags(opts) end +---@param key string Key in the agenda prompt (for example: "a", "t", "m", "M") +function OrgAgenda.open_by_key(key) + return orgmode.agenda:open_by_key(key) +end + return OrgAgenda diff --git a/lua/orgmode/ui/menu.lua b/lua/orgmode/ui/menu.lua index d313ffca1..a34d24961 100644 --- a/lua/orgmode/ui/menu.lua +++ b/lua/orgmode/ui/menu.lua @@ -135,6 +135,16 @@ function Menu._default_menu(data) return entry.action() end +function Menu:get_entry_by_key(key) + local valid_keys = {} + for _, item in ipairs(self.items) do + if item.key then + valid_keys[item.key] = item + end + end + return valid_keys[key] +end + function Menu:open() local menu_data = { title = self.title,