branch: elpa/bind-map commit 9a676378fe2c4e4be602f0b5e77330e28cba6cbd Author: justbur <jus...@burkett.cc> Commit: justbur <jus...@burkett.cc>
Simplify major-mode activation Instead of creating a new activation function for each map, use one centralized activation function that processes a list of vars corresponding to a map and the major-modes in which that map should be active. Reduces the number of functions and variables generated by bind-map --- bind-map.el | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/bind-map.el b/bind-map.el index a6b5db9b11..b549048561 100644 --- a/bind-map.el +++ b/bind-map.el @@ -136,6 +136,21 @@ be activated.") (define-key (symbol-value map) (nth 2 entry) (nth 3 entry)))))) (add-hook 'evil-local-mode-hook 'bind-map-evil-local-mode-hook) +(defvar bind-map-major-modes-alist '() + "Each element takes the form (MAP-ACTIVE (MAJOR-MODE1 +MAJOR-MODE2 ...)). The car is the variable used to activate a map +when the major mode is an element of the cdr. See +`bind-map-change-major-mode-after-body-hook'.") + +(defun bind-map-change-major-mode-after-body-hook () + "Called to activate major mode maps in a buffer." + ;; format is (ACTIVATE-VAR MAJOR-MODES-LIST) + (dolist (entry bind-map-major-modes-alist) + (setf (symbol-value (car entry)) + (not (null (member major-mode (cdr entry))))))) +(add-hook 'change-major-mode-after-body-hook + 'bind-map-change-major-mode-after-body-hook) + ;;;###autoload (defmacro bind-map (map &rest args) "Bind keymap MAP in multiple locations. @@ -205,9 +220,7 @@ unspecified the bindings are global. Declare a prefix command for MAP named COMMAND-NAME." (let* ((root-map (intern (format "%s-root-map" map))) - (major-mode-list (intern (format "%s-major-modes" map))) - (activate (intern (format "%s-activate" map))) - (activate-func (intern (format "%s-activate-function" map))) + (active-var (intern (format "%s-active" map))) (prefix-cmd (or (plist-get args :prefix-cmd) (intern (format "%s-prefix" map)))) (keys (plist-get args :keys)) @@ -243,18 +256,12 @@ mode maps. Set up by bind-map.el." map)) (push (cons mode ,root-map) minor-mode-map-alist))) (when ',major-modes - (defvar ,major-mode-list '()) ;; compiler warns about making a local var below the top-level - (with-no-warnings - (defvar-local ,activate nil)) - (push (cons ',activate ,root-map) minor-mode-map-alist) - (dolist (mode ',major-modes) - (add-to-list ',major-mode-list mode)) - (defun ,activate-func () - (setq ,activate (not (null (member major-mode ,major-mode-list))))) + (with-no-warnings (defvar-local ,active-var nil)) + (push (cons ',active-var ,root-map) minor-mode-map-alist) + (push (cons ',active-var ',major-modes) bind-map-major-modes-alist) ;; call once in case we are already in the relevant major mode - (,activate-func) - (add-hook 'change-major-mode-after-body-hook ',activate-func)) + (bind-map-change-major-mode-after-body-hook)) (when (and ,override-minor-modes (null ',major-modes)