From 13f0c4785e957cdfbed0370e03e101f788a88c8c Mon Sep 17 00:00:00 2001 From: Wetlize Date: Sat, 24 Jul 2021 16:24:11 +0300 Subject: [PATCH] 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 --- modules/lang/org/contrib/roam2.el | 61 ++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/modules/lang/org/contrib/roam2.el b/modules/lang/org/contrib/roam2.el index 724827b2e..ff1af099a 100644 --- a/modules/lang/org/contrib/roam2.el +++ b/modules/lang/org/contrib/roam2.el @@ -136,27 +136,46 @@ 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) - :nv "]" #'magit-section-forward-sibling - :nv "[" #'magit-section-backward-sibling - :nv "gj" #'magit-section-forward-sibling - :nv "gk" #'magit-section-backward-sibling - :nv "gr" #'revert-buffer - :nv "gR" #'revert-buffer - :nv "z1" #'magit-section-show-level-1 - :nv "z2" #'magit-section-show-level-2 - :nv "z3" #'magit-section-show-level-3 - :nv "z4" #'magit-section-show-level-4 - :nv "za" #'magit-section-toggle - :nv "zc" #'magit-section-hide - :nv "zC" #'magit-section-hide-children - :nv "zo" #'magit-section-show - :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))) + "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 + :nv "gk" #'magit-section-backward-sibling + :nv "gr" #'revert-buffer + :nv "gR" #'revert-buffer + :nv "z1" #'magit-section-show-level-1 + :nv "z2" #'magit-section-show-level-2 + :nv "z3" #'magit-section-show-level-3 + :nv "z4" #'magit-section-show-level-4 + :nv "za" #'magit-section-toggle + :nv "zc" #'magit-section-hide + :nv "zC" #'magit-section-hide-children + :nv "zo" #'magit-section-show + :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 + :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