Skip to content

Commit

Permalink
Features n bug fixes:
Browse files Browse the repository at this point in the history
- Use buffer local mappings in `core.keybinds` to prevent bugs where
  mappings would carry over into normal files
- Change `:Neorg set-mode` to `:Neorg mode` to be more consistent with
  other commands


Former-commit-id: 8c57d1f
  • Loading branch information
vhyrro committed Oct 24, 2021
1 parent 0a50253 commit bed1754
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 47 deletions.
4 changes: 2 additions & 2 deletions lua/neorg/modules/core/keybinds/default_keybinds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ module.public = {
-- Apply the below keys to all modes
keybinds.map_to_mode("all", {
n = {
{ neorg_leader .. "mn", ":Neorg set-mode norg<CR>" },
{ neorg_leader .. "mh", ":Neorg set-mode traverse-heading<CR>" },
{ neorg_leader .. "mn", ":Neorg mode norg<CR>" },
{ neorg_leader .. "mh", ":Neorg mode traverse-heading<CR>" },
},
}, {
silent = true,
Expand Down
47 changes: 12 additions & 35 deletions lua/neorg/modules/core/keybinds/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ module.load = function()
end
end

module.private = {
bound_keys = {},
}

module.config.public = {
default_keybinds = false,
neorg_leader = "<Leader>o",
Expand Down Expand Up @@ -108,13 +104,8 @@ module.public = {
end,

-- @Summary Rebinds all the keys defined via User Callbacks
bind_all = function()
-- If the table is already populated then don't populate it further
if not vim.tbl_isempty(module.private.bound_keys) then
return
end

local current_mode = module.required["core.mode"].get_mode()
bind_all = function(action, for_mode)
local current_mode = for_mode or module.required["core.mode"].get_mode()

-- Broadcast the enable_keybinds event to any user that might have registered a User Callback for it
local payload
Expand All @@ -128,10 +119,11 @@ module.public = {
-- @Param command (string) - same as the rhs parameter for :h nvim_buf_set_keymap
-- @Param opts (table) - same as the opts parameter for :h nvim_buf_set_keymap
map = function(mode, key, command, opts)
vim.api.nvim_set_keymap(mode, key, command, opts or {})
local ok, error = pcall(function() if action then action(0, mode, key, command, opts or {}) else vim.api.nvim_buf_set_keymap(0, mode, key, command, opts or {}) end end)

-- Insert it into the list of tracked keys
table.insert(module.private.bound_keys, { mode, key })
if not ok then
log.trace(string.format("An error occurred when trying to bind key '%s' in mode '%s' in neorg mode '%s' - %s", key, mode, current_mode, error))
end
end,

-- @Summary Maps a bunch of keys for a certain mode
Expand All @@ -146,7 +138,7 @@ module.public = {
end

-- If the current mode matches the desired mode then
if mode == "all" or module.required["core.mode"].get_mode() == mode then
if mode == "all" or (for_mode or module.required["core.mode"].get_mode()) == mode then
-- Loop through all the keybinds for a certain mode
for neovim_mode, keymaps in pairs(keys) do
-- Loop though all the keymaps in that mode
Expand All @@ -170,7 +162,7 @@ module.public = {
end

-- If the current mode matches the desired mode then
if mode == "all" or module.required["core.mode"].get_mode() == mode then
if mode == "all" or (for_mode or module.required["core.mode"].get_mode()) == mode then
-- Loop through all the keybinds for a certain mode
for neovim_mode, keymaps in pairs(keys) do
-- Loop though all the keymaps in that mode
Expand Down Expand Up @@ -234,27 +226,14 @@ module.public = {
-- If we're dealing with a table called "unbind" then go through all the strings defined there
-- and unbind each key
for _, to_unbind in ipairs(data) do
pcall(vim.api.nvim_del_keymap, mode, to_unbind)
pcall(vim.api.nvim_buf_del_keymap, 0, mode, to_unbind)
end
end
end
end
end
end,

-- @Summary Unbind all currently defined keys
-- @Description If the user has used the map() function, as they should have, Neorg will have tracked all the currently bound keymaps.
-- Thanks to this function all those keys will be cleared as a result of e.g. a mode change.
unbind_all = function()
-- Loop through every currently defined keybind and unbind it
for _, mode_key_pair in ipairs(module.private.bound_keys) do
pcall(vim.api.nvim_del_keymap, mode_key_pair[1], mode_key_pair[2])
end

-- Reset the bound keys table
module.private.bound_keys = {}
end,

-- @Summary Synchronizes all autocompletions
-- @Description Updates the list of known modes and keybinds for easy autocompletion. Invoked automatically during neorg_post_load().
sync = function()
Expand Down Expand Up @@ -304,14 +283,12 @@ module.on_event = function(event)
module.public.sync()
elseif event.type == "core.mode.events.mode_set" then
-- If a new mode has been set then reset all of our keybinds
module.public.unbind_all()
module.public.bind_all(function(buf, mode, key) vim.api.nvim_buf_del_keymap(buf, mode, key) end, event.content.current)
module.public.bind_all()
elseif event.type == "core.autocommands.events.bufenter" and event.content.norg then
-- If we have entered a buffer then rebind all keys
-- If a new mode has been set then reset all of our keybinds
module.public.bind_all(function(buf, mode, key) vim.api.nvim_buf_del_keymap(buf, mode, key) end, module.required["core.mode"].get_previous_mode())
module.public.bind_all()
elseif event.type == "core.autocommands.events.bufleave" and event.content.norg then
-- If we have left a buffer then unbind all keys
module.public.unbind_all()
end
end

Expand Down
25 changes: 15 additions & 10 deletions lua/neorg/modules/core/mode/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ USAGE:
To retrieve the *previous* mode name, use get_previous_mode().
To retrieve *all* modes, use get_modes()
If core.neorgcmd is loaded, core.mode.public.add_mode() also updates the autocompletion for the :Neorg set-mode command,
If core.neorgcmd is loaded, core.mode.public.add_mode() also updates the autocompletion for the :Neorg mode command,
which can be used by the user to switch modes.
EVENTS:
Expand Down Expand Up @@ -49,15 +49,15 @@ module.public = {
-- Define command for :Neorg
neorg_commands = {
definitions = {
["set-mode"] = {
["mode"] = {
norg = {},
},
},

data = {
["set-mode"] = {
args = 1,
name = "set-mode",
["mode"] = {
max_args = 1,
name = "mode",
},
},
},
Expand Down Expand Up @@ -85,7 +85,7 @@ module.public = {
)

-- Define the autocompletion tables and make them include the current mode
module.public.neorg_commands.definitions["set-mode"][mode_name] = {}
module.public.neorg_commands.definitions["mode"][mode_name] = {}

-- If core.neorgcmd is loaded then update all autocompletions
local neorgcmd = neorg.modules.get_module("core.neorgcmd")
Expand Down Expand Up @@ -143,9 +143,14 @@ module.public = {
}

module.on_event = function(event)
-- Retrieve the :Neorg set-mode command and set the mode accordingly
if event.type == "core.neorgcmd.events.set-mode" then
module.public.set_mode(event.content[1])
-- Retrieve the :Neorg mode command and set the mode accordingly
if event.type == "core.neorgcmd.events.mode" then
-- If no parameters were given then just print the current mode
if not event.content[1] then
vim.notify("Active Mode: " .. module.public.get_mode())
else -- Else actually set the mode to the one we specified
module.public.set_mode(event.content[1])
end
end
end

Expand All @@ -156,7 +161,7 @@ module.events.defined = {

module.events.subscribed = {
["core.neorgcmd"] = {
["set-mode"] = true,
["mode"] = true,
},
}

Expand Down

0 comments on commit bed1754

Please sign in to comment.