Refactor format! macro

By removing the cl-flet call, we reduce the size of backtraces produced
during bin/doom commands by a whopping 80%. Noice.

Also renames doom-ansi-apply -> doom-color-apply
This commit is contained in:
Henrik Lissner 2019-03-05 23:52:59 -05:00
parent 4d649333a6
commit 884957bdc6
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -32,7 +32,7 @@
"TODO") "TODO")
;;;###autoload ;;;###autoload
(defun doom-ansi-apply (style text) (defun doom-color-apply (style text)
"Apply CODE to formatted MESSAGE with ARGS. CODE is derived from any of "Apply CODE to formatted MESSAGE with ARGS. CODE is derived from any of
`doom-message-fg', `doom-message-bg' or `doom-message-fx'. `doom-message-fg', `doom-message-bg' or `doom-message-fx'.
@ -53,19 +53,27 @@ Otherwise, it maps colors to a term-color-* face."
`(:foreground ,(face-foreground (caddr (assq style doom-ansi-alist))))) `(:foreground ,(face-foreground (caddr (assq style doom-ansi-alist)))))
((cddr (assq style doom-ansi-alist))))))))) ((cddr (assq style doom-ansi-alist)))))))))
(defun doom--short-color-replace (forms)
"Replace color-name functions with calls to `doom-color-apply'."
(cond ((null forms) nil)
((listp forms)
(append (cond ((not (symbolp (car forms)))
(list (doom--short-color-replace (car forms))))
((assq (car forms) doom-ansi-alist)
`(doom-color-apply ',(car forms)))
((eq (car forms) 'color)
(pop forms)
`(doom-color-apply ,(car forms)))
((list (car forms))))
(doom--short-color-replace (cdr forms))
nil))
(forms)))
;;;###autoload ;;;###autoload
(defmacro format! (message &rest args) (defmacro format! (message &rest args)
"An alternative to `format' that understands (color ...) and converts them "An alternative to `format' that understands (color ...) and converts them
into faces or ANSI codes depending on the type of sesssion we're in." into faces or ANSI codes depending on the type of sesssion we're in."
`(cl-flet `(format ,@(doom--short-color-replace `(,message ,@args))))
(,@(mapcar (lambda (rule) `(,(car rule)
(lambda (message)
(doom-ansi-apply ',(car rule) message))))
doom-ansi-alist)
(color
(lambda (code format)
(doom-ansi-apply code format))))
(format ,message ,@args)))
;;;###autoload ;;;###autoload
(defmacro print! (message &rest args) (defmacro print! (message &rest args)