Skip to content

Commit

Permalink
feat: allow specifying the mode at the command
Browse files Browse the repository at this point in the history
  • Loading branch information
jokajak committed Aug 6, 2023
1 parent 4d3bbd9 commit 28c5b82
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
5 changes: 5 additions & 0 deletions doc/keyseer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,10 @@ Parameters~
{mode} `(optional)` `(string)` the neovim mode to show keymaps for
{bufnr} `(optional)` buffer the buffer for buffer specific keymaps

------------------------------------------------------------------------------
*KeySeer.close()*
`KeySeer.close`()
the keyseer ui


vim:tw=78:ts=8:noet:ft=help:norl:
1 change: 1 addition & 0 deletions doc/tags
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
KeySeer keyseer.txt /*KeySeer*
KeySeer.close() keyseer.txt /*KeySeer.close()*
KeySeer.config keyseer.txt /*KeySeer.config*
KeySeer.setup() keyseer.txt /*KeySeer.setup()*
KeySeer.show() keyseer.txt /*KeySeer.show()*
25 changes: 10 additions & 15 deletions lua/keyseer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,17 @@ KeySeer.setup = function(config)

KeySeer.ns = vim.api.nvim_create_namespace("keyseer")

---@return string, string[]
local function parse(args)
local parts = vim.split(vim.trim(args), "%s+")
if parts[1]:find("KeySeer") then
table.remove(parts, 1)
end
if args:sub(-1) == " " then
parts[#parts + 1] = ""
end
return table.remove(parts, 1) or "", parts
end

vim.api.nvim_create_user_command("KeySeer", function(cmd)
local prefix, args = parse(cmd.args)
local args = vim.split(vim.trim(cmd.args), "%s+")
local mode
if #args == 1 and args[1] == "n" then
mode = "n"
local valid_modes = { "n", "i", "v", "o", "x", "s", "l", "c", "t", "ic" }
if #args == 1 then
if vim.tbl_contains(valid_modes, args[1]) then
mode = args[1]
else
error("Invalid mode specified. See map")
return
end
end
if vim.fn.win_gettype() == "command" then
error("Can't open keyseer from command-line window. See E11")
Expand Down Expand Up @@ -139,6 +133,7 @@ KeySeer.show = function(mode, bufnr)
UI.show("home", mode, bufnr)
end

---Close the keyseer ui
KeySeer.close = function()
local UI = require("keyseer.ui")
UI:close()
Expand Down
1 change: 1 addition & 0 deletions lua/keyseer/keymaps/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ end
---@returns table
function Keymaps:process_keymaps(bufnr, mode)
mode = mode or Config.initial_mode
D.log("Keymaps", "Getting keymaps for %s", mode)
if Config.include_builtin_keymaps then
local preset_keymaps = BuiltInKeyMaps[mode]
self:add_keymaps(preset_keymaps)
Expand Down
13 changes: 4 additions & 9 deletions lua/keyseer/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,13 @@ function KeySeerUI.show(pane, mode, bufnr)
bufnr = vim.F.if_nil(bufnr, vim.api.nvim_get_current_buf())
KeySeerUI.ui = KeySeerUI.visible() and KeySeerUI.ui or KeySeerUI.create(bufnr)

if pane then
KeySeerUI.ui.state.pane = pane
end

if mode then
KeySeerUI.ui.state.mode = mode
end
KeySeerUI.ui.state.mode = mode or KeySeerUI.ui.state.mode
KeySeerUI.ui.state.pane = pane or KeySeerUI.ui.state.pane

D.log("UI", "Setting bufnr to " .. bufnr)
KeySeerUI.ui.state.bufnr = bufnr

KeySeerUI.ui.state.keymaps:process_keymaps(bufnr, KeySeerUI.ui.state.mode)

KeySeerUI.ui:update()
end

Expand Down Expand Up @@ -111,7 +107,6 @@ function KeySeerUI.create(bufnr)
end
if not self.state.keymaps then
self.state.keymaps = Keymaps:new()
self.state.keymaps:process_keymaps(self.state.bufnr)
end

local width = math.max(Config.ui.size.width, keyboard.width)
Expand Down
3 changes: 3 additions & 0 deletions lua/keyseer/ui/panes/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ function M.render(ui)
ui.render:append("Current buffer: ", "KeySeerH2")
ui.render:append(tostring(ui.state.bufnr)):nl()

ui.render:append("mode for keymaps: ", "KeySeerH2")
ui.render:append(tostring(ui.state.mode)):nl()

ui.render:append("Show builtin keymaps: ", "KeySeerH2")
ui.render:append(tostring(Config.include_builtin_keymaps)):nl()
ui.render:append("Show global keymaps: ", "KeySeerH2")
Expand Down

0 comments on commit 28c5b82

Please sign in to comment.