5

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

(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这样根据不同的终端类型设置属性。)

4

2 回答 2

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

  2. defface可以利用继承——见人脸属性:inherit。不知道这在您的特定情况下是否有帮助。

于 2013-07-21T17:23:56.310 回答
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-s, brian-char-face-t, 和brian-char-face-u

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

于 2013-07-21T05:40:56.757 回答