减少冗余

RNA*_*RNA 5 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)

...
...
Run Code Online (Sandbox Code Playgroud)

无论如何,要避免显式地编写所有defface定义并使代码减少冗余吗?(我知道make-face,但似乎已弃用,无法像其他终端类型一样设置属性defface。)

Dre*_*rew 5

  1. make-face 完全不推荐使用,AFAICT。

  2. defface可以利用继承-参见face属性:inherit。Dunno在您的特定情况下是否有帮助。

  • +1。我在帮助信息中看到了这一点:`(make-face FACE&optional NO-INIT-FROM-RESOURCES)定义一个新面孔,名称为FACE,一个符号。不要直接从Lisp代码中调用它;请改用defface。这就是为什么我认为它已被弃用 (2认同)

ass*_*sem 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)))))
Run Code Online (Sandbox Code Playgroud)

为您带来新面孔:

brian-char-face-sbrian-char-face-t, 和brian-char-face-u

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