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) :type '(repeat string)
:group 'evil-collection) :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 (defvar evil-collection-setup-hook nil
"Hook run by `evil-collection-init' for each mode that is evilified. "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 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)) (setq filtered-bindings (nreverse filtered-bindings))
(evil-collection--define-key 'operator map-sym 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) (defun evil-collection-define-key (state map-sym &rest bindings)
"Wrapper for `evil-define-key*' with additional features. "Wrapper for `evil-define-key*' with additional features.
Unlike `evil-define-key*' MAP-SYM should be a quoted keymap other than the 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)) (declare (indent defun))
(let* ((whitelist (mapcar 'kbd evil-collection-key-whitelist)) (let* ((whitelist (mapcar 'kbd evil-collection-key-whitelist))
(blacklist (mapcar 'kbd evil-collection-key-blacklist)) (blacklist (mapcar 'kbd evil-collection-key-blacklist))
(states-to-bind (evil-collection--filter-states state))
filtered-bindings) filtered-bindings)
(while bindings (when (or states-to-bind (null state))
(let ((key (pop bindings)) (while bindings
(def (pop bindings))) (let ((key (pop bindings))
(when (or (and whitelist (member key whitelist)) (def (pop bindings)))
(not (member key blacklist))) (when (or (and whitelist (member key whitelist))
(annalist-record 'evil-collection 'keybindings (not (member key blacklist)))
(list map-sym state key def) (annalist-record 'evil-collection 'keybindings
:local (or (eq map-sym 'local) (list map-sym state key def)
(local-variable-p map-sym))) :local (or (eq map-sym 'local)
(push key filtered-bindings) (local-variable-p map-sym)))
(push def filtered-bindings)))) (push key filtered-bindings)
(setq filtered-bindings (nreverse filtered-bindings)) (push def filtered-bindings))))
(evil-collection--define-key state map-sym 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) (defun evil-collection--define-key (state map-sym bindings)
"Workhorse function for `evil-collection-define-key'. "Workhorse function for `evil-collection-define-key'.