transient-state: Improve handling of additional bindings

Add two new functions: `spacemacs/transient-state-register-add-bindings`
and `spacemacs/transient-state-register-remove-bindings` to prevent layer
authors and end users from dealing with the underlying variables' subtleties.
This commit is contained in:
MadAnd 2017-02-21 12:07:23 +02:00 committed by Eivind Fonn
parent c7df45a0a9
commit 5eeaed75d9
4 changed files with 58 additions and 22 deletions

View File

@ -27,6 +27,16 @@
holds the key bindings."
(intern (format "spacemacs/%S-transient-state/heads" name)))
(defun spacemacs//transient-state-add-bindings-name (name)
"Return the name of the transient state add-bindings variable which
may hold the additional key bindings. The variable may be unbound."
(intern (format "spacemacs-%s-transient-state-add-bindings" name)))
(defun spacemacs//transient-state-remove-bindings-name (name)
"Return the name of the transient state remove-bindings variable which
may hold the keys to be removed. The variable may be unbound."
(intern (format "spacemacs-%s-transient-state-remove-bindings" name)))
(defun spacemacs//transient-state-adjust-bindings (bindings to-remove to-add)
(append
(cl-remove-if
@ -47,6 +57,34 @@ holds the key bindings."
(eval
(hydra--format nil body docstring (symbol-value heads)))))
(defun spacemacs/transient-state-register-add-bindings (name bindings)
"Register additional BINDINGS for the transient state NAME.
BINDINGS should be a list of Hydra head definitions. See `defhydra'.
Since a transient state initializes its Hydra right after
the `dotspacemacs/user-config', this function will have no
effect if called after that point."
(declare (indent defun))
(let ((var-name (spacemacs//transient-state-add-bindings-name name)))
(or (boundp var-name)
(set var-name '()))
(set var-name (append (symbol-value var-name) bindings))))
(defun spacemacs/transient-state-register-remove-bindings (name keys)
"Register KEYS to be removed from the transient state NAME.
KEYS should be a list of strings.
Since a transient state initializes its Hydra right after
the `dotspacemacs/user-config', this function will have no
effect if called after that point."
(declare (indent defun))
(let ((var-name (spacemacs//transient-state-remove-bindings-name name)))
(or (boundp var-name)
(set var-name '()))
(set var-name (append (symbol-value var-name) keys))))
(defmacro spacemacs|transient-state-format-hint (name var hint)
"Format HINT and store the result in VAR for transient state NAME."
(declare (indent 1))
@ -126,10 +164,8 @@ used."
(let* ((func (spacemacs//transient-state-func-name name))
(props-var (spacemacs//transient-state-props-var-name name))
(body-func (spacemacs//transient-state-body-func-name name))
(add-bindings
(intern (format "spacemacs-%s-transient-state-add-bindings" name)))
(remove-bindings
(intern (format "spacemacs-%s-transient-state-remove-bindings" name)))
(add-bindings (spacemacs//transient-state-add-bindings-name name))
(remove-bindings (spacemacs//transient-state-remove-bindings-name name))
(bindings (spacemacs/mplist-get props :bindings))
(doc (or (plist-get props :doc) "\n"))
(title (plist-get props :title))

View File

@ -46,12 +46,12 @@
;; add some functions to ahs transient states
(setq spacemacs--symbol-highlight-transient-state-doc
(concat spacemacs--symbol-highlight-transient-state-doc
" [_b_] search buffers [_/_] search proj [_f_] search files [_s_] swoop")
spacemacs-symbol-highlight-transient-state-add-bindings
'(("/" spacemacs/helm-project-smart-do-search-region-or-symbol :exit t)
("b" spacemacs/helm-buffers-smart-do-search-region-or-symbol :exit t)
("f" spacemacs/helm-files-smart-do-search-region-or-symbol :exit t)
("s" spacemacs/helm-swoop-region-or-symbol :exit t)))))
" [_b_] search buffers [_/_] search proj [_f_] search files [_s_] swoop"))
(spacemacs/transient-state-register-add-bindings "symbol-highlight"
'(("/" spacemacs/helm-project-smart-do-search-region-or-symbol :exit t)
("b" spacemacs/helm-buffers-smart-do-search-region-or-symbol :exit t)
("f" spacemacs/helm-files-smart-do-search-region-or-symbol :exit t)
("s" spacemacs/helm-swoop-region-or-symbol :exit t)))))
(defun helm/post-init-bookmark ()
(spacemacs/set-leader-keys "fb" 'helm-filtered-bookmarks))
@ -643,6 +643,6 @@ Search for a search tool in the order provided by `dotspacemacs-search-tools'."
(setq projectile-completion-system 'helm))
(defun helm/post-init-persp-mode ()
(setq spacemacs-layouts-transient-state-add-bindings
'(("b" spacemacs/persp-helm-mini :exit t)
("l" spacemacs/helm-perspectives :exit t))))
(spacemacs/transient-state-register-add-bindings "layouts"
'(("b" spacemacs/persp-helm-mini :exit t)
("l" spacemacs/helm-perspectives :exit t))))

View File

@ -214,13 +214,13 @@
'spacemacs/ivy-spacemacs-layouts
'(("c" persp-kill-without-buffers "Close layout(s)")
("k" persp-kill "Kill layout(s)")))
(setq spacemacs-layouts-transient-state-remove-bindings
'("C" "X"))
(setq spacemacs-layouts-transient-state-add-bindings
'(("b" spacemacs/ivy-spacemacs-layout-buffer :exit t)
("l" spacemacs/ivy-spacemacs-layouts :exit t)
("C" spacemacs/ivy-spacemacs-layout-close-other :exit t)
("X" spacemacs/ivy-spacemacs-layout-kill-other :exit t))))
(spacemacs/transient-state-register-remove-bindings "layouts"
'("C" "X"))
(spacemacs/transient-state-register-add-bindings "layouts"
'(("b" spacemacs/ivy-spacemacs-layout-buffer :exit t)
("l" spacemacs/ivy-spacemacs-layouts :exit t)
("C" spacemacs/ivy-spacemacs-layout-close-other :exit t)
("X" spacemacs/ivy-spacemacs-layout-kill-other :exit t))))
(defun ivy/post-init-projectile ()
(setq projectile-completion-system 'ivy)

View File

@ -229,8 +229,8 @@
:defer t
:init
(progn
(setq spacemacs-window-manipulation-transient-state-add-bindings
'(("g" spacemacs/toggle-golden-ratio)))
(spacemacs/transient-state-register-add-bindings "window-manipulation"
'(("g" spacemacs/toggle-golden-ratio)))
(spacemacs|add-toggle golden-ratio
:status golden-ratio-mode
:on (golden-ratio-mode) (golden-ratio)