From 8b8c142ddabf388085b629181024fcb4303d932d Mon Sep 17 00:00:00 2001 From: Daanturo Date: Wed, 23 Jun 2021 20:20:56 +0700 Subject: [PATCH] Make filtering states possible --- evil-collection.el | 57 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/evil-collection.el b/evil-collection.el index 0723180..2010b39 100644 --- a/evil-collection.el +++ b/evil-collection.el @@ -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'.