Add :exit and :documentation keywords for micro-state bindings

This commit is contained in:
syl20bnr 2015-02-08 15:12:55 -05:00
parent d6f4650105
commit 1ff47e09ac
2 changed files with 46 additions and 30 deletions

View File

@ -25,10 +25,15 @@ Available PROPS:
Evaluate BODDY when leaving the micro-state.
`:bindings EXPRESSIONS'
One or several EXPRESSIONS with the form (STRING SYMBOL) where STRING
is a key to bound to the function SYMBOL."
One or several EXPRESSIONS with the form
(STRING1 SYMBOL1 :documentation STRING :exit SYMBOL)
where:
- STRING1 is a key to bound to the function SYMBOL1.
- :documentation STRING is a doc string (not used for now)
- :exit SYMBOL is either `:exit t' or `:exit nil', if non nil then
pressing this key will leave the micro-state (default is nil)."
(declare (indent 1))
(let* ((func (intern (format "spacemacs/%s-micro-state" (symbol-name name))))
(let* ((func (spacemacs//micro-state-func-name name))
(on-enter (spacemacs/mplist-get props :on-enter))
(on-exit (spacemacs/mplist-get props :on-exit))
(bindings (spacemacs/mplist-get props :bindings))
@ -45,36 +50,55 @@ Available PROPS:
,@keymap-body map) ',(spacemacs//micro-state-create-exit-func
name wrappers on-exit)))))
(defun spacemacs//micro-state-func-name (name)
"Return the name of the micro-state function."
(intern (format "spacemacs/%s-micro-state" (symbol-name name))))
(defun spacemacs//micro-state-create-wrappers (name bindings)
"Return an alist (key wrapper) for each binding in BINDINGS."
(mapcar (lambda (x)
(apply 'spacemacs//micro-state-create-wrapper
name x)) bindings))
(mapcar (lambda (x) (spacemacs//micro-state-create-wrapper name x))
bindings))
(defun spacemacs//micro-state-create-wrapper (name key func)
"Create a wrapper of FUNC and return a tuple (KEY wrapper)."
(let* ((wrapper-name (intern (format "spacemacs//%s-%s" (symbol-name name)
(symbol-name func))))
(defun spacemacs//micro-state-create-wrapper (name binding)
"Create a wrapper of FUNC and return a tuple (key wrapper BINDING)."
(let* ((wrapped (cadr binding))
(wrapper-name (intern (format "spacemacs//%s-%s" (symbol-name name)
(symbol-name wrapped))))
(wrapper-func (eval `(defun ,wrapper-name ()
"Auto-generated function"
(interactive)
(call-interactively ',func)))))
(cons key wrapper-func)))
(when ',wrapped
(call-interactively ',wrapped))))))
(append (list (car binding) wrapper-func) binding)))
(defun spacemacs//micro-state-fill-map-sexps (wrappers)
"Return a list of `define-key' sexp to fill the micro-state temporary map."
(mapcar (lambda (x) `(define-key map ,(car x) ',(cdr x)))
(mapcar (lambda (x) `(define-key map ,(kbd (car x)) ',(cadr x)))
wrappers))
(defun spacemacs//micro-state-create-exit-func (name wrappers on-exit)
"Return a function to execute when leaving the micro-state."
"Return a function to execute when leaving the micro-state.
The returned function returns nil if the executed command exits the
micro-state."
(let ((func (intern (format "spacemacs//%s-on-exit" name))))
(eval `(defun ,func ()
"Function executed after each micro-state command."
(if (reduce (lambda (x y) (or x y))
(mapcar (lambda (x)
(eq this-command (cdr x))) ',wrappers)
(spacemacs//micro-state-stay? ',name x))
',wrappers)
:initial-value nil)
't ,@on-exit nil)))))
(defun spacemacs//micro-state-stay? (name wrapper)
"Return non nil if WRAPPER does not leave the micro-state."
(let ((micro-state-fun (spacemacs//micro-state-func-name name))
(key (car wrapper))
(func (cadr wrapper)))
(when (and (or (eq this-command micro-state-fun)
(eq this-command func))
(equal (this-command-keys) (kbd key)))
(not (plist-get wrapper :exit)))))
(provide 'core-micro-state)

View File

@ -1215,13 +1215,6 @@ which require an initialization must be listed explicitly in the list.")
(set-face-attribute
'helm-header nil
:background spacemacs-helm-navigation-micro-state-color)
;; deactivate TAB during the micro-state (any key can be used to exit
;; the micro-state but this one seems to be a better choice so it
;; deserves a special treatment)
(define-key helm-map (kbd "C-i") '(lambda () (interactive)))
(define-key helm-map (kbd "<tab>") '(lambda () (interactive)))
;; "r" switches to the action buffer and exit the micro-state
(define-key helm-map "r" 'helm-select-action)
;; bind actions on numbers starting from 1 which executes action 0
(dotimes (n 10)
(define-key helm-map (number-to-string n)
@ -1231,23 +1224,18 @@ which require an initialization must be listed explicitly in the list.")
(defun spacemacs//on-exit-helm-navigation-micro-state ()
"Action to perform when exiting helm micor-state."
;; restore helm key map
(define-key helm-map (kbd "C-i")
'spacemacs/helm-navigation-micro-state)
(define-key helm-map (kbd "<tab>")
'spacemacs/helm-navigation-micro-state)
(define-key helm-map "r" nil)
(dotimes (n 10)
(define-key helm-map (number-to-string n) nil))
(dotimes (n 10) (define-key helm-map (number-to-string n) nil))
;; restore faces
(set-face-attribute
'helm-header nil
:background (face-attribute 'header-line :background)))
(spacemacs//on-exit-helm-navigation-micro-state)
(spacemacs|define-micro-state helm-navigation
:on-enter (spacemacs//on-enter-helm-navigation-micro-state)
:on-exit (spacemacs//on-exit-helm-navigation-micro-state)
:bindings
("<tab>" nil :exit t)
("C-i" nil :exit t)
("?" helm-help)
("a" helm-select-action)
("g" helm-beginning-of-buffer)
@ -1256,10 +1244,14 @@ which require an initialization must be listed explicitly in the list.")
("j" helm-next-line)
("k" helm-previous-line)
("l" helm-next-source)
("r" helm-select-action :exit t)
("t" helm-toggle-visible-mark)
("T" helm-toggle-all-marks)
("v" helm-execute-persistent-action)))
(define-key helm-map (kbd "C-i") 'spacemacs/helm-navigation-micro-state)
(define-key helm-map (kbd "<tab>") 'spacemacs/helm-navigation-micro-state)
(eval-after-load "helm-mode" ; required
'(spacemacs|hide-lighter helm-mode)))))