Fix keymap precedence for evil users in the org-roam-buffer

`org-roam-mode` based buffers use `magit-section` to render sections.
The problem is that `magit-section` will layer keymaps for each section
under a text property. In Emacs, text property based keymaps have a
higher precedence for a lookup[0] than `emulation-mode-map-alists`, in
which Evil and leader keymaps are stored in.

[0]: https://www.gnu.org/software/emacs/manual/html_node/elisp/Searching-Keymaps.html
This commit is contained in:
Wetlize 2021-07-24 16:24:11 +03:00
parent ff331781c5
commit 13f0c4785e

View file

@ -136,9 +136,23 @@ In case of failure, fail gracefully."
"t" #'org-roam-tag-add
"T" #'org-roam-tag-remove
"r" #'org-roam-ref-add
"R" #'org-roam-ref-remove))
(:map org-roam-mode-map
:when (featurep! :editor evil +everywhere)
"R" #'org-roam-ref-remove)))
(when (featurep! :editor evil +everywhere)
(add-hook! 'org-roam-mode-hook
(defun +org-roam-detach-magit-section-mode-map-h ()
"Detach `magit-section-mode-map' from `org-roam-mode-map'.
Inheriting its keymaps introduces a lot of conflicts in
`org-roam-mode' based buffers, where Evil and leader keybindings
will become completely overridden. This is because `magit-section'
uses 'keymap text-property to attach section-unique keymaps, which
has a higher level of precedence than `emulation-mode-map-alists'.
Note: We do this each time through the hook, because otherwise
sections seems to ignore the detachment."
(set-keymap-parent org-roam-mode-map nil)))
(map! :map org-roam-mode-map
:nv "]" #'magit-section-forward-sibling
:nv "[" #'magit-section-backward-sibling
:nv "gj" #'magit-section-forward-sibling
@ -156,7 +170,12 @@ In case of failure, fail gracefully."
:nv "zO" #'magit-section-show-children
:nv "zr" #'magit-section-show-level-4-all
:nv "C-j" #'magit-section-forward
:nv "C-k" #'magit-section-backward)))
:nv "C-k" #'magit-section-backward
:g "M-p" #'magit-section-backward-sibling
:g "M-n" #'magit-section-forward-sibling
:g [tab] #'magit-section-toggle
:g [C-tab] #'magit-section-cycle
:g [backtab] #'magit-section-cycle-global)))
;; Since the org module lazy loads org-protocol (waits until an org URL is