减少冗余的defface

时间:2013-07-21 02:42:05

标签: emacs elisp

我想为不同的角色定义一堆面孔,如下所示:

(defface char-face-a
  '((((type tty) (class color)) (:background "yellow" :foreground "black"))
    (((type tty) (class mono)) (:inverse-video t))
    (((class color) (background dark)) (:background "yellow" :foreground "black"))
    (((class color) (background light)) (:background "yellow" :foreground "black"))
    (t (:background "gray")))
  "Face for marking up A's"
  :group 'char-faces)

(defface char-face-b
  '((((type tty) (class color)) (:background "red" :foreground "black"))
    (((type tty) (class mono)) (:inverse-video t))
    (((class color) (background dark)) (:background "red" :foreground "black"))
    (((class color) (background light)) (:background "red" :foreground "black"))
    (t (:background "gray")))
  "Face for marking up B's"
  :group 'char-faces)

...
...

无论如何都要避免显式写入所有defface定义并减少代码冗余? (我知道make-face,但似乎已弃用,无法根据defface的不同终端类型设置属性。)

2 个答案:

答案 0 :(得分:5)

  1. make-face完全没有被弃用,AFAICT。

  2. defface可以使用继承 - 请参阅face属性:inherit。 Dunno是否有助于您的特定背景。

答案 1 :(得分:3)

对后缀< - >的映射进行操作的宏和循环怎么样?颜色:

(defmacro brian-def-char-face (letter backgrnd foregrnd)
  `(defface ,(intern (concat "brian-char-face-"
                 letter))
     '((((type tty) (class color)) 
        (:background 
     ,backgrnd
     :foreground
     ,foregrnd))
       (((type tty) (class color)) (:inverse-video t))
       (((class color) (background dark))
    (:foreground
     ,foregrnd
     :background
     ,backgrnd))
       (((class color) (background light))
    (:foreground
     ,foregrnd
     :background
     ,backgrnd))
       (t (:background "gray")))
     ,(concat "Face for marking up " (upcase letter) "'s")))

(let ((letcol-alist '((s . (white black))
              (t . (black yellow))
              (u . (green pink)))))
  (loop for elem in letcol-alist
    for l = (format "%s" (car elem))
    for back = (format "%s" (cadr elem))
    for fore = (format "%s" (caddr elem))
    do 
    (eval (macroexpand `(brian-def-char-face ,l ,back ,fore)))))

给你新面孔:

brian-char-face-sbrian-char-face-tbrian-char-face-u

现在您只需要维护字母< - >颜色映射列表,并可能扩展宏以支持其他面部属性(如果需要)。