Generalize the bind properties for toggles and micro-states

This commit is contained in:
syl20bnr 2015-02-23 23:28:23 -05:00
parent 239722fb7b
commit bf2635e372
5 changed files with 64 additions and 48 deletions

View File

@ -72,4 +72,41 @@ and its values are removed."
(setq spacemacs--init-redisplay-count (1+ spacemacs--init-redisplay-count))
(redisplay))
(defun spacemacs//create-key-binding-form (props func)
"Helper which returns a from to bind FUNC to a key according to PROPS.
Supported properties:
`:evil-leader STRING'
One or several key sequence strings to be set with `evil-leader/set-key'.
`:evil-leader-for-mode CONS CELL'
One or several cons cells (MODE . KEY) where MODE is a major-mode symbol
and KEY is a key sequence string to be set with
`evil-leader/set-key-for-mode'.
`:global-key STRING'
One or several key sequence strings to be set with `global-set-key'.
`:define-key CONS CELL'
One or several cons cells (MAP . KEY) where MAP is a mode map and KEY is a
key sequence string to be set with `define-key'. "
(let ((evil-leader (spacemacs/mplist-get props :evil-leader))
(evil-leader-for-mode (spacemacs/mplist-get props :evil-leader-for-mode))
(global-key (spacemacs/mplist-get props :global-key))
(def-key (spacemacs/mplist-get props :define-key)))
`((unless (null ',evil-leader)
(dolist (key ',evil-leader)
(evil-leader/set-key key ',func)))
(unless (null ',evil-leader-for-mode)
(dolist (val ',evil-leader-for-mode)
(evil-leader/set-key-for-mode
(car val) (cdr val) ',func)))
(unless (null ',global-key)
(dolist (key ',global-key)
(global-set-key (kbd key) ',func)))
(unless (null ',def-key)
(dolist (val ',def-key)
(define-key (eval (car val)) (kbd (cdr val)) ',func))))))
(provide 'core-funcs)

View File

@ -70,7 +70,10 @@ Available PROPS:
- :pre is an SEXP evaluated before the bound action
- :post is an SEXP evaluated after the bound action
- :exit SYMBOL is either `:exit t' or `:exit nil', if non nil then
pressing this key will leave the micro-state (default is nil)."
pressing this key will leave the micro-state (default is nil).
All properties supported by `spacemacs//create-key-binding-form' can be
used."
(declare (indent 1))
(let* ((func (spacemacs//micro-state-func-name name))
(doc (spacemacs/mplist-get props :doc))
@ -79,21 +82,23 @@ Available PROPS:
(on-exit (spacemacs/mplist-get props :on-exit))
(bindings (spacemacs/mplist-get props :bindings))
(wrappers (spacemacs//micro-state-create-wrappers name doc bindings))
(keymap-body (spacemacs//micro-state-fill-map-sexps wrappers)))
`(defun ,func ()
,(format "%S micro-state." name)
(interactive)
(let ((doc ,@doc))
(when doc
(lv-message (spacemacs//micro-state-propertize-doc
(format "%S: %s" ',name doc)))))
,@on-enter
(,(if (version< emacs-version "24.4")
'set-temporary-overlay-map
'set-transient-map)
(let ((map (make-sparse-keymap)))
,@keymap-body map) ',(spacemacs//micro-state-create-exit-func
name wrappers persistent on-exit)))))
(keymap-body (spacemacs//micro-state-fill-map-sexps wrappers))
(bindkeys (spacemacs//create-key-binding-form props func)))
`(progn (defun ,func ()
,(format "%S micro-state." name)
(interactive)
(let ((doc ,@doc))
(when doc
(lv-message (spacemacs//micro-state-propertize-doc
(format "%S: %s" ',name doc)))))
,@on-enter
(,(if (version< emacs-version "24.4")
'set-temporary-overlay-map
'set-transient-map)
(let ((map (make-sparse-keymap)))
,@keymap-body map) ',(spacemacs//micro-state-create-exit-func
name wrappers persistent on-exit)))
,@bindkeys)))
(defun spacemacs//micro-state-func-name (name)
"Return the name of the micro-state function."

View File

@ -36,19 +36,8 @@ Avaiblabe PROPS:
`:documentation STRING'
STRING describes what the toggle does.
`:evil-leader STRING'
A key sequence string to be set with `evil-leader/set-key'.
`:evil-leader-for-mode CONS CELL'
A cons cell (MODE . KEY) where MODE is a major-mode symbol and KEY is a
key sequence string to be set with `evil-leader/set-key-for-mode'.
`:global-key STRING'
A key sequence string to be set with `global-set-key'.
`:define-key CONS CELL'
A cons cell (MAP . KEY) where MAP is a mode map and KEY is a
key sequence string to be set with `define-key'. "
All properties supported by `spacemacs//create-key-binding-form' can be
used."
(let* ((wrapper-func (intern (format "spacemacs/toggle-%s"
(symbol-name name))))
(status (plist-get props :status))
@ -56,10 +45,7 @@ Avaiblabe PROPS:
(doc (plist-get props :documentation))
(on-body (spacemacs/mplist-get props :on))
(off-body (spacemacs/mplist-get props :off))
(evil-leader (plist-get props :evil-leader))
(evil-leader-for-mode (plist-get props :evil-leader-for-mode))
(global-key (plist-get props :global-key))
(def-key (plist-get props :define-key)))
(bindkeys (spacemacs//create-key-binding-form props wrapper-func)))
`(progn
(push (append '(,name) '(:function ,wrapper-func) ',props)
spacemacs-toggles)
@ -77,16 +63,6 @@ Avaiblabe PROPS:
(listp ',status))
,status) (progn ,@off-body) ,@on-body)
(message "This toggle is not supported.")))
;; key bindings
(when ,evil-leader
(evil-leader/set-key ,evil-leader ',wrapper-func))
(when ,evil-leader-for-mode
(evil-leader/set-key-for-mode
'(car ,evil-leader-for-mode)
(cdr ,evil-leader-for-mode) ',wrapper-func))
(when ,global-key
(global-set-key (kbd ,global-key) ',wrapper-func))
(when ,def-key
(define-key (car ,def-key) (kbd ,(cdr def-key)) ',wrapper-func)))))
,@bindkeys)))
(provide 'core-toggle)

View File

@ -224,7 +224,6 @@
(golden-ratio)))
(evil-leader/set-key
"w." 'spacemacs/window-manipulation-micro-state
"w2" 'layout-double-columns
"w3" 'layout-triple-columns
"wb" 'switch-to-minibuffer-window
@ -356,6 +355,7 @@
(spacemacs|define-micro-state window-manipulation
:doc "[?] for help"
:evil-leader "w."
:bindings
("?" nil :doc (spacemacs//window-manipulation-full-doc))
("0" select-window-0 :doc (spacemacs//window-manipulation-number-doc))

View File

@ -1278,6 +1278,7 @@ which require an initialization must be listed explicitly in the list.")
(spacemacs|define-micro-state helm-navigation
:persistent t
:define-key (helm-map . "C-SPC") (helm-map . "C-@")
:on-enter (spacemacs//helm-navigation-ms-on-enter)
:on-exit (spacemacs//helm-navigation-ms-on-exit)
:bindings
@ -1297,9 +1298,6 @@ which require an initialization must be listed explicitly in the list.")
("T" helm-toggle-all-marks)
("v" helm-execute-persistent-action))
(define-key helm-map (kbd "C-SPC") 'spacemacs/helm-navigation-micro-state)
(define-key helm-map (kbd "C-@") 'spacemacs/helm-navigation-micro-state)
(eval-after-load "helm-mode" ; required
'(spacemacs|hide-lighter helm-mode)))))