Skip to content

Commit d0aa8ac

Browse files
committed
Support custom faces everywhere.
1 parent 1a13273 commit d0aa8ac

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

hydra.el

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,32 +209,54 @@ When nil, you can specify your own at each location like this: _ 5a_.")
209209
"Since the docstrings are aligned by hand anyway, this isn't very useful."
210210
"0.13.1")
211211

212+
(defface hydra-default-face
213+
'((t (:inherit default)))
214+
"Face from which all Hydra faces inherit."
215+
:group 'hydra)
216+
217+
(defface hydra-docstring-face
218+
'((t (:inherit hydra-default-face)))
219+
"Face used for the header of the hydra menu."
220+
:group 'hydra)
221+
222+
(defface hydra-item-face
223+
'((t (:inherit hydra-default-face)))
224+
"Face used or individual Hydra entries, i. e, selection prompts
225+
and docstrings."
226+
:group 'hydra)
227+
228+
(defface hydra-selection-face
229+
'((t (:inherit hydra-item-face)))
230+
"Face from which the red, blue, pink and amaranth selection
231+
prompts inherit."
232+
:group 'hydra)
233+
212234
(defface hydra-face-red
213-
'((t (:foreground "#FF0000" :bold t)))
235+
'((t (:inherit hydra-selection-face :foreground "#FF0000" :bold t)))
214236
"Red Hydra heads don't exit the Hydra.
215237
Every other command exits the Hydra."
216238
:group 'hydra)
217239

218240
(defface hydra-face-blue
219241
'((((class color) (background light))
220-
:foreground "#0000FF" :bold t)
242+
:inherit hydra-selection-face :foreground "#0000FF" :bold t)
221243
(((class color) (background dark))
222-
:foreground "#8ac6f2" :bold t))
244+
:inherit hydra-selection-face :foreground "#8ac6f2" :bold t))
223245
"Blue Hydra heads exit the Hydra.
224246
Every other command exits as well.")
225247

226248
(defface hydra-face-amaranth
227-
'((t (:foreground "#E52B50" :bold t)))
249+
'((t (:inherit hydra-selection-face :foreground "#E52B50" :bold t)))
228250
"Amaranth body has red heads and warns on intercepting non-heads.
229251
Exitable only through a blue head.")
230252

231253
(defface hydra-face-pink
232-
'((t (:foreground "#FF6EB4" :bold t)))
254+
'((t (:inherit hydra-selection-face :foreground "#FF6EB4" :bold t)))
233255
"Pink body has red heads and runs intercepted non-heads.
234256
Exitable only through a blue head.")
235257

236258
(defface hydra-face-teal
237-
'((t (:foreground "#367588" :bold t)))
259+
'((t (:inherit hydra-selection-face :foreground "#367588" :bold t)))
238260
"Teal body has blue heads and warns on intercepting non-heads.
239261
Exitable only through a blue head.")
240262

@@ -439,11 +461,11 @@ BODY, and HEADS are parameters to `defhydra'."
439461
(lambda (x)
440462
(format
441463
(if (> (length (cdr x)) 0)
442-
(concat "[%s]: " (cdr x))
464+
(propertize (concat "[%s]: " (cdr x)) 'face 'hydra-item-face)
443465
"%s")
444466
(car x)))
445467
keys
446-
", "))))
468+
(propertize ", " 'face 'hydra-default-face)))))
447469

448470
(defvar hydra-fontify-head-function nil
449471
"Possible replacement for `hydra-fontify-head-default'.")
@@ -506,7 +528,8 @@ HEAD's binding is returned as a string wrapped with [] or {}."
506528
\"%`...\" expressions are extracted into \"%S\".
507529
_NAME, BODY, DOCSTRING and HEADS are parameters of `defhydra'.
508530
The expressions can be auto-expanded according to NAME."
509-
(setq docstring (hydra--strip-align-markers docstring))
531+
(setq docstring (propertize (hydra--strip-align-markers docstring)
532+
'face 'hydra-docstring-face))
510533
(let ((rest (hydra--hint body heads))
511534
(start 0)
512535
varlist
@@ -544,14 +567,26 @@ The expressions can be auto-expanded according to NAME."
544567
(/= (aref spec (1- (length spec))) ?s))
545568
(setq spec (concat spec "S")))
546569
(setq docstring
547-
(concat
548-
(substring docstring 0 start)
549-
"%" spec
550-
(substring docstring (+ start offset 1 lspec varp))))))))
570+
(propertize
571+
(concat
572+
(substring docstring 0 start)
573+
"%" spec
574+
(substring docstring (+ start offset 1 lspec varp)))
575+
'face 'hydra-docstring-face))))))
551576
(if (eq ?\n (aref docstring 0))
552-
`(concat (format ,(substring docstring 1) ,@(nreverse varlist))
553-
,rest)
554-
`(format ,(concat docstring ": " rest ".")))))
577+
`(let ((format-statement
578+
(concat (format ,(substring docstring 1)
579+
,@(nreverse varlist)) ,rest)))
580+
(loop for i from 0 to (- (length format-statement) 1) do
581+
(when (eq (get-char-property i 'face format-statement) nil)
582+
(add-text-properties i (+ i 1)
583+
'(face hydra-default-face)
584+
format-statement)))
585+
format-statement)
586+
`(format ,(concat (propertize (concat docstring ": ")
587+
'face 'hydra-docstring-face)
588+
rest
589+
(propertize "." 'face 'hydra-default-face))))))
555590

556591
(defun hydra--complain (format-string &rest args)
557592
"Forward to (`message' FORMAT-STRING ARGS) unless `hydra-verbose' is nil."

0 commit comments

Comments
 (0)