2015-02-08 01:09:38 +00:00
|
|
|
;;; -*- lexical-binding: t -*-
|
2015-02-08 17:19:34 +00:00
|
|
|
;;; core-micro-state.el --- Spacemacs Core File
|
2015-02-08 01:09:38 +00:00
|
|
|
;;
|
|
|
|
;; Copyright (c) 2012-2014 Sylvain Benner
|
|
|
|
;; Copyright (c) 2014-2015 Sylvain Benner & Contributors
|
|
|
|
;;
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
;;
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;;; License: GPLv3
|
2015-05-19 06:28:02 +00:00
|
|
|
(require 'corelv)
|
2015-02-08 01:09:38 +00:00
|
|
|
|
2015-05-14 01:59:59 +00:00
|
|
|
(defun spacemacs/defface-micro-state-faces ()
|
2015-02-12 05:14:38 +00:00
|
|
|
"Define faces for micro-states."
|
|
|
|
(let* ((hname 'spacemacs-micro-state-header-face)
|
|
|
|
(bname 'spacemacs-micro-state-binding-face)
|
2015-02-21 04:26:22 +00:00
|
|
|
(box '(:line-width -1 :color (plist-get (face-attribute
|
|
|
|
'mode-line :box) :color)))
|
2015-02-12 05:14:38 +00:00
|
|
|
(err (face-attribute 'error :foreground)))
|
|
|
|
(eval `(defface ,hname '((t ()))
|
|
|
|
"Face for micro-state header in echo area.
|
|
|
|
The header is the name of the micro-state."
|
|
|
|
:group 'spacemacs))
|
|
|
|
(set-face-attribute hname nil
|
|
|
|
:background "DarkGoldenrod2"
|
|
|
|
:foreground "black"
|
|
|
|
:bold t
|
|
|
|
:box box)
|
|
|
|
(eval `(defface ,bname '((t ()))
|
|
|
|
"Face for micro-state key binding in echo area.
|
2015-02-12 04:57:39 +00:00
|
|
|
Characters enclosed in `[]' will have this face applied to them."
|
2015-02-12 05:14:38 +00:00
|
|
|
:group 'spacemacs))
|
|
|
|
(set-face-attribute bname nil
|
|
|
|
:foreground err
|
|
|
|
:bold t)))
|
2015-05-14 01:59:59 +00:00
|
|
|
(spacemacs/defface-micro-state-faces)
|
2015-02-12 04:57:39 +00:00
|
|
|
|
2015-11-09 04:50:41 +00:00
|
|
|
(defun spacemacs//micro-state-set-minibuffer-height (str)
|
|
|
|
"Set the max mini windows size given a string STR."
|
|
|
|
(let ((line-count (1+ (how-many-str "\n" str))))
|
|
|
|
(when (and (> line-count max-mini-window-height)
|
|
|
|
(> line-count 10))
|
|
|
|
(setq max-mini-window-height line-count))))
|
|
|
|
|
2015-02-08 01:22:43 +00:00
|
|
|
(defmacro spacemacs|define-micro-state (name &rest props)
|
2015-02-08 01:09:38 +00:00
|
|
|
"Define a micro-state called NAME.
|
|
|
|
|
|
|
|
NAME is a symbol.
|
|
|
|
|
|
|
|
Available PROPS:
|
|
|
|
|
2015-02-11 23:40:43 +00:00
|
|
|
`:on-enter SEXP'
|
|
|
|
Evaluate SEXP when the micro-state is switched on.
|
2015-02-08 01:09:38 +00:00
|
|
|
|
2015-02-11 23:40:43 +00:00
|
|
|
`:on-exit SEXP'
|
|
|
|
Evaluate SEXP when leaving the micro-state.
|
2015-02-08 01:09:38 +00:00
|
|
|
|
2015-02-11 23:40:43 +00:00
|
|
|
`:doc STRING or SEXP'
|
2015-03-08 04:55:47 +00:00
|
|
|
A STRING or a SEXP that evaluates to a string.
|
|
|
|
|
|
|
|
`:use-minibuffer BOOLEAN'
|
|
|
|
If non nil then the minibuffer is used to display the documenation
|
|
|
|
strings. Default is nil.
|
2015-02-11 03:08:50 +00:00
|
|
|
|
2015-03-11 03:18:57 +00:00
|
|
|
`:disable-evil-leader BOOLEAN'
|
|
|
|
If non nil then the evil leader has no effect when the micro state
|
|
|
|
is active. Default to nil.
|
|
|
|
|
2015-02-21 07:17:39 +00:00
|
|
|
`:persistent BOOLEAN'
|
2015-03-07 04:48:05 +00:00
|
|
|
If BOOLEAN is non nil then the micro-state never exits. A binding
|
2015-03-08 04:55:47 +00:00
|
|
|
with an explicitly set `exit t' property is required. Default is nil.
|
2015-02-21 07:17:39 +00:00
|
|
|
|
2015-05-10 02:36:16 +00:00
|
|
|
`:execute-binding-on-enter BOOLEAN'
|
|
|
|
If BOOLEAN is non nil then execute the micro-state command bound to
|
|
|
|
to the pressed key that started the micro-state.
|
|
|
|
|
2015-02-08 01:09:38 +00:00
|
|
|
`:bindings EXPRESSIONS'
|
2015-02-08 20:12:55 +00:00
|
|
|
One or several EXPRESSIONS with the form
|
2015-02-22 05:55:33 +00:00
|
|
|
(STRING1 SYMBOL1 :doc STRING
|
|
|
|
:pre SEXP
|
|
|
|
:post SEXP
|
|
|
|
:exit SYMBOL)
|
2015-02-08 20:12:55 +00:00
|
|
|
where:
|
2015-03-11 03:11:52 +00:00
|
|
|
- STRING1 is a key to be bound to the function or key map SYMBOL1.
|
2015-02-11 23:40:43 +00:00
|
|
|
- :doc STRING or SEXP is a STRING or an SEXP that evalutes
|
|
|
|
to a string
|
2015-02-22 05:55:33 +00:00
|
|
|
- :pre is an SEXP evaluated before the bound action
|
|
|
|
- :post is an SEXP evaluated after the bound action
|
2015-10-19 03:32:26 +00:00
|
|
|
- :exit SYMBOL or SEXP, if non nil then pressing this key will
|
|
|
|
leave the micro-state (default is nil).
|
|
|
|
Important note: due to inner working of transient-maps in Emacs
|
|
|
|
the `:exit' keyword is evaluate *before* the actual execution
|
|
|
|
of the bound command.
|
2015-02-24 04:28:23 +00:00
|
|
|
|
|
|
|
All properties supported by `spacemacs//create-key-binding-form' can be
|
|
|
|
used."
|
2015-02-08 01:09:38 +00:00
|
|
|
(declare (indent 1))
|
2015-02-08 20:12:55 +00:00
|
|
|
(let* ((func (spacemacs//micro-state-func-name name))
|
2015-02-11 23:40:43 +00:00
|
|
|
(doc (spacemacs/mplist-get props :doc))
|
2015-02-21 07:17:39 +00:00
|
|
|
(persistent (plist-get props :persistent))
|
2015-03-11 03:18:57 +00:00
|
|
|
(disable-leader (plist-get props :disable-evil-leader))
|
2015-11-09 01:58:12 +00:00
|
|
|
(msg-func (if (plist-get props :use-minibuffer)
|
|
|
|
'message
|
|
|
|
'corelv-message))
|
2015-02-28 05:05:09 +00:00
|
|
|
(exec-binding (plist-get props :execute-binding-on-enter))
|
2015-02-08 01:09:38 +00:00
|
|
|
(on-enter (spacemacs/mplist-get props :on-enter))
|
|
|
|
(on-exit (spacemacs/mplist-get props :on-exit))
|
|
|
|
(bindings (spacemacs/mplist-get props :bindings))
|
2015-03-08 04:55:47 +00:00
|
|
|
(wrappers (spacemacs//micro-state-create-wrappers
|
2015-03-11 03:18:57 +00:00
|
|
|
name doc msg-func disable-leader bindings))
|
2015-02-24 04:28:23 +00:00
|
|
|
(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)
|
2015-11-08 21:44:43 +00:00
|
|
|
,@on-enter
|
2015-02-24 04:28:23 +00:00
|
|
|
(let ((doc ,@doc))
|
|
|
|
(when doc
|
2015-11-09 04:50:41 +00:00
|
|
|
(spacemacs//micro-state-set-minibuffer-height doc)
|
2015-03-08 04:55:47 +00:00
|
|
|
(apply ',msg-func (list (spacemacs//micro-state-propertize-doc
|
2015-11-09 01:58:12 +00:00
|
|
|
(format "%S: %s" ',name doc))))))
|
2015-02-28 05:05:09 +00:00
|
|
|
,(when exec-binding
|
2015-02-28 04:57:10 +00:00
|
|
|
(spacemacs//micro-state-auto-execute bindings))
|
2015-02-24 04:28:23 +00:00
|
|
|
(,(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)))
|
2015-02-08 01:09:38 +00:00
|
|
|
|
2015-02-08 20:12:55 +00:00
|
|
|
(defun spacemacs//micro-state-func-name (name)
|
|
|
|
"Return the name of the micro-state function."
|
2015-02-22 06:02:17 +00:00
|
|
|
(intern (format "spacemacs/%S-micro-state" name)))
|
2015-02-08 20:12:55 +00:00
|
|
|
|
2015-02-28 04:57:10 +00:00
|
|
|
(defun spacemacs//micro-state-auto-execute (bindings)
|
|
|
|
"Auto execute the binding corresponding to `this-command-keys'."
|
|
|
|
`(let* ((key (substring (this-command-keys)
|
|
|
|
(1- (length (this-command-keys)))))
|
|
|
|
(binding (assoc key ',bindings)))
|
|
|
|
(when binding
|
|
|
|
(call-interactively (cadr binding)))))
|
|
|
|
|
2015-03-11 03:18:57 +00:00
|
|
|
(defun spacemacs//micro-state-create-wrappers
|
|
|
|
(name doc msg-func disable-leader bindings)
|
2015-02-08 01:09:38 +00:00
|
|
|
"Return an alist (key wrapper) for each binding in BINDINGS."
|
2015-03-08 04:55:47 +00:00
|
|
|
(mapcar (lambda (x) (spacemacs//micro-state-create-wrapper
|
|
|
|
name doc msg-func x))
|
2015-02-21 04:27:43 +00:00
|
|
|
(append bindings
|
|
|
|
;; force SPC to quit the micro-state to avoid a edge case
|
|
|
|
;; with evil-leader
|
2015-03-11 03:18:57 +00:00
|
|
|
(list `(,dotspacemacs-leader-key
|
|
|
|
,(unless disable-leader 'evil-leader--default-map)
|
|
|
|
:exit t)))))
|
2015-02-08 01:09:38 +00:00
|
|
|
|
2015-03-08 04:55:47 +00:00
|
|
|
(defun spacemacs//micro-state-create-wrapper (name default-doc msg-func binding)
|
2015-02-08 20:12:55 +00:00
|
|
|
"Create a wrapper of FUNC and return a tuple (key wrapper BINDING)."
|
2015-02-21 07:17:39 +00:00
|
|
|
(let* ((key (car binding))
|
|
|
|
(wrapped (cadr binding))
|
2015-02-11 23:40:43 +00:00
|
|
|
(binding-doc (spacemacs/mplist-get binding :doc))
|
2015-02-22 05:55:33 +00:00
|
|
|
(binding-pre (spacemacs/mplist-get binding :pre))
|
|
|
|
(binding-post (spacemacs/mplist-get binding :post))
|
2015-02-22 06:02:17 +00:00
|
|
|
(wrapper-name (intern (format "spacemacs//%S-%S-%s" name wrapped key)))
|
2015-11-09 01:58:12 +00:00
|
|
|
(doc-body
|
|
|
|
`((let ((bdoc ,@binding-doc)
|
|
|
|
(defdoc ,@default-doc))
|
|
|
|
(if bdoc
|
|
|
|
(apply ',msg-func
|
|
|
|
(list (spacemacs//micro-state-propertize-doc
|
|
|
|
(format "%S: %s" ',name bdoc))))
|
|
|
|
(when (and defdoc
|
|
|
|
',wrapped (not (plist-get ',binding :exit)))
|
2015-11-09 04:50:41 +00:00
|
|
|
(spacemacs//micro-state-set-minibuffer-height defdoc)
|
2015-11-09 01:58:12 +00:00
|
|
|
(apply ',msg-func
|
|
|
|
(list (spacemacs//micro-state-propertize-doc
|
2015-11-13 06:35:42 +00:00
|
|
|
(format "%S: %s" ',name defdoc))))
|
|
|
|
defdoc)))))
|
2015-02-12 04:57:39 +00:00
|
|
|
(wrapper-func
|
2015-03-11 03:11:52 +00:00
|
|
|
(if (and (boundp wrapped)
|
|
|
|
(eval `(keymapp ,wrapped)))
|
|
|
|
wrapped
|
|
|
|
`(defun ,wrapper-name ()
|
|
|
|
"Auto-generated function"
|
|
|
|
(interactive)
|
|
|
|
,@binding-pre
|
|
|
|
(let ((throwp t))
|
|
|
|
(catch 'exit
|
2015-08-11 03:56:40 +00:00
|
|
|
(when (fboundp ',wrapped)
|
|
|
|
(setq this-command ',wrapped)
|
2015-03-11 03:11:52 +00:00
|
|
|
(call-interactively ',wrapped)
|
|
|
|
(setq last-command ',wrapped))
|
|
|
|
(setq throwp nil))
|
|
|
|
,@binding-post
|
|
|
|
(when throwp (throw 'exit nil)))
|
2015-11-09 04:50:41 +00:00
|
|
|
(when ,@doc-body
|
|
|
|
(spacemacs//micro-state-set-minibuffer-height ,@doc-body)
|
|
|
|
,@doc-body)))))
|
2015-03-11 03:11:52 +00:00
|
|
|
(append (list (car binding) (eval wrapper-func)) binding)))
|
2015-02-08 01:09:38 +00:00
|
|
|
|
|
|
|
(defun spacemacs//micro-state-fill-map-sexps (wrappers)
|
|
|
|
"Return a list of `define-key' sexp to fill the micro-state temporary map."
|
2015-02-08 20:12:55 +00:00
|
|
|
(mapcar (lambda (x) `(define-key map ,(kbd (car x)) ',(cadr x)))
|
2015-02-08 01:09:38 +00:00
|
|
|
wrappers))
|
|
|
|
|
2015-02-21 07:17:39 +00:00
|
|
|
(defun spacemacs//micro-state-create-exit-func
|
|
|
|
(name wrappers persistent on-exit)
|
2015-02-08 20:12:55 +00:00
|
|
|
"Return a function to execute when leaving the micro-state.
|
|
|
|
|
|
|
|
The returned function returns nil if the executed command exits the
|
|
|
|
micro-state."
|
2015-02-08 01:09:38 +00:00
|
|
|
(let ((func (intern (format "spacemacs//%s-on-exit" name))))
|
|
|
|
(eval `(defun ,func ()
|
|
|
|
"Function executed after each micro-state command."
|
2015-02-21 07:17:39 +00:00
|
|
|
(let* ((cur-wrapper (spacemacs//get-current-wrapper
|
|
|
|
',name ',wrappers))
|
|
|
|
(exitp (if cur-wrapper (plist-get cur-wrapper :exit)
|
|
|
|
,(not persistent))))
|
2015-10-19 03:32:26 +00:00
|
|
|
(when (listp exitp) (setq exitp (eval exitp)))
|
2015-02-21 07:17:39 +00:00
|
|
|
(when exitp ,@on-exit (spacemacs//micro-state-close-window))
|
|
|
|
(not exitp))))))
|
|
|
|
|
|
|
|
(defun spacemacs//get-current-wrapper (name wrappers)
|
|
|
|
"Return the wrapper being executed.
|
2015-03-07 04:48:05 +00:00
|
|
|
Return nil if no wrapper is being executed (i.e. an unbound key has been
|
2015-02-21 07:17:39 +00:00
|
|
|
pressed)."
|
|
|
|
(let ((micro-state-fun (spacemacs//micro-state-func-name name)))
|
|
|
|
(catch 'found
|
|
|
|
(dolist (wrapper wrappers)
|
|
|
|
(let ((key (car wrapper))
|
|
|
|
(func (cadr wrapper)))
|
|
|
|
(if (and (or (eq this-command micro-state-fun)
|
|
|
|
(eq this-command func))
|
|
|
|
(equal (this-command-keys) (kbd key)))
|
|
|
|
(throw 'found wrapper))))
|
|
|
|
nil)))
|
2015-02-08 20:12:55 +00:00
|
|
|
|
2015-02-12 04:57:39 +00:00
|
|
|
(defun spacemacs//micro-state-propertize-doc (doc)
|
|
|
|
"Return a propertized doc string from DOC."
|
|
|
|
(when (string-match "^\\(.+?\\):\\([[:ascii:]]*\\)$" doc)
|
|
|
|
(let* ((header (match-string 1 doc))
|
|
|
|
(pheader (when header
|
|
|
|
(propertize (concat " " header " ")
|
|
|
|
'face 'spacemacs-micro-state-header-face)))
|
2015-03-07 04:48:05 +00:00
|
|
|
(tail (spacemacs//micro-state-propertize-doc-rec
|
2015-02-12 04:57:39 +00:00
|
|
|
(match-string 2 doc))))
|
|
|
|
(concat pheader tail))))
|
|
|
|
|
2015-03-07 04:48:05 +00:00
|
|
|
(defun spacemacs//micro-state-propertize-doc-rec (doc)
|
2015-02-12 04:57:39 +00:00
|
|
|
"Recursively propertize keys"
|
|
|
|
(if (string-match "^\\([[:ascii:]]*?\\)\\(\\[.+?\\]\\)\\([[:ascii:]]*\\)$" doc)
|
|
|
|
(let* ((head (match-string 1 doc))
|
|
|
|
(key (match-string 2 doc))
|
|
|
|
(pkey (when key
|
|
|
|
(propertize key 'face 'spacemacs-micro-state-binding-face)))
|
2015-03-07 04:48:05 +00:00
|
|
|
(tail (spacemacs//micro-state-propertize-doc-rec
|
2015-02-12 04:57:39 +00:00
|
|
|
(match-string 3 doc))))
|
|
|
|
(concat head pkey tail))
|
|
|
|
doc))
|
|
|
|
|
2015-02-20 05:27:08 +00:00
|
|
|
(defun spacemacs//micro-state-close-window ()
|
|
|
|
"Close micro-state help window."
|
2015-05-19 06:28:02 +00:00
|
|
|
(when (window-live-p corelv-wnd)
|
|
|
|
(let ((buf (window-buffer corelv-wnd)))
|
|
|
|
(delete-window corelv-wnd)
|
2015-02-20 05:27:08 +00:00
|
|
|
(kill-buffer buf))))
|
|
|
|
|
2015-02-08 17:19:34 +00:00
|
|
|
(provide 'core-micro-state)
|