Make filtering states possible

This commit is contained in:
Daanturo 2021-06-23 20:20:56 +07:00 committed by Youmu
parent 5699d7b40b
commit 8b8c142dda
1 changed files with 44 additions and 13 deletions

View File

@ -324,6 +324,23 @@ This is a list of strings that are suitable for input to `kbd'."
:type '(repeat string)
:group 'evil-collection)
(defcustom evil-collection-state-passlist '()
"List of evil states that may be used by Evil Collection.
This is a list of symbols that are suitable for input to
`evil-define-key'. Ignore when there are no states in the list."
:type '(repeat symbol)
:group 'evil-collection)
(defcustom evil-collection-state-denylist
(if (bound-and-true-p evil-disable-insert-state-bindings)
'(insert)
'())
"List of evil states that may not be used by Evil Collection.
This is a list of symbols that are suitable for input to
`evil-define-key'."
:type '(repeat symbol)
:group 'evil-collection)
(defvar evil-collection-setup-hook nil
"Hook run by `evil-collection-init' for each mode that is evilified.
This hook runs after all setup (including keybindings) for a mode has already
@ -391,6 +408,18 @@ binding in `annalist' as so."
(setq filtered-bindings (nreverse filtered-bindings))
(evil-collection--define-key 'operator map-sym filtered-bindings)))
(defun evil-collection--filter-states (state)
"Return a list states after filtering STATE (a single symbol or list of symbols).
The return value adheres to `evil-collection-state-passlist' and
`evil-collection-state-denylist'. When the STATE is `nil', which
mean all states for `evil-define-key', return `nil'."
(let ((states (if (listp state) state (list state))))
(seq-difference
(if evil-collection-state-passlist
(seq-intersection states evil-collection-state-passlist)
states)
evil-collection-state-denylist)))
(defun evil-collection-define-key (state map-sym &rest bindings)
"Wrapper for `evil-define-key*' with additional features.
Unlike `evil-define-key*' MAP-SYM should be a quoted keymap other than the
@ -400,20 +429,22 @@ to filter keys on the basis of `evil-collection-key-whitelist' and
(declare (indent defun))
(let* ((whitelist (mapcar 'kbd evil-collection-key-whitelist))
(blacklist (mapcar 'kbd evil-collection-key-blacklist))
(states-to-bind (evil-collection--filter-states state))
filtered-bindings)
(while bindings
(let ((key (pop bindings))
(def (pop bindings)))
(when (or (and whitelist (member key whitelist))
(not (member key blacklist)))
(annalist-record 'evil-collection 'keybindings
(list map-sym state key def)
:local (or (eq map-sym 'local)
(local-variable-p map-sym)))
(push key filtered-bindings)
(push def filtered-bindings))))
(setq filtered-bindings (nreverse filtered-bindings))
(evil-collection--define-key state map-sym filtered-bindings)))
(when (or states-to-bind (null state))
(while bindings
(let ((key (pop bindings))
(def (pop bindings)))
(when (or (and whitelist (member key whitelist))
(not (member key blacklist)))
(annalist-record 'evil-collection 'keybindings
(list map-sym state key def)
:local (or (eq map-sym 'local)
(local-variable-p map-sym)))
(push key filtered-bindings)
(push def filtered-bindings))))
(setq filtered-bindings (nreverse filtered-bindings))
(evil-collection--define-key states-to-bind map-sym filtered-bindings))))
(defun evil-collection--define-key (state map-sym bindings)
"Workhorse function for `evil-collection-define-key'.