Search code examples
emacsemacs-faces

How to define multiple emacs faces?


I'm trying to define some emacs font faces to do some custom highlighting. This seems to work when I define them individually:

(defface my-r-face `((t (:foreground "red")))  "Red highlight")
(defvar m-r-face 'my-r-face "Red.")
(defface my-g-face `((t (:foreground "green")))  "Green highlight")
(defvar m-g-face 'my-g-face "Green.")
(defface my-b-face `((t (:foreground "#0088ff")))  "Blue highlight")
(defvar m-b-face 'my-b-face "Blue.")
....etc

However I have a few dozen of these and I want to define them all in one go from some kind of colour table:

(setq ctable '(("red" "r")
           ("orange" "o")
           ("yellow" "y")
           ("#88ff00" "gy")
           ("green" "g")
           ("#00ff88" "gc")
           ("cyan" "c")
           ("#0088ff" "bc")
           ("blue" "b")
           ("purple" "bm")
           ("magenta" "m")
           ("#ff0088" "rm")
           ("grey" "lg")
           ("white" "w") ))

My difficulty is with assembling the symbol names for each face, ie concatenating "my-" and "-face" onto either side of an entry from the table. I have discovered (intern) that can make a new symbol from a string, however this symbol is then not acceptable to (defface), as it seems what I am doing is equivalent to (defface 'my-r-face ..., and defface doesn't like the quoted symbol, and expects (defface my-r-face .. instead. My attempt is as follows:

(dolist (tpl ctable)
  (defvar (intern (concat "my-" (nth 1 tpl) "-face"))
    (quote (intern (concat "my-" (nth 1 tpl) "-face"))) "colour")
  (defface (intern (concat "my-" (nth 1 tpl) "-face"))
    `((t (:foreground ,(car tpl)))) "Highlight" :group 'fortran)
)

Running this results in

Lisp error: (wrong-type-argument symbolp (intern (concat "fegs-" (nth 1 tpl) "-face")))
  (defvar (intern (concat "fegs-" ... "-face")) (quote (intern ...)) "colour")

Can anyone shed some light on what I'm doing wrong, or if I'm barking up the wrong tree entirely and there is a better way of doing this?

Thanks.


Solution

  • defvar is an special form, defface is a macro (so arguments are passed unevaluated). Have you tried to use something in the line of

    (eval `(defface ,(intern "foo") '((t (:foreground "red"))) "Highlight" :group 'fortran))
    
    (eval `(defvar ,(intern "bar")))