2013-07-21 89 views
4

我想定義一堆面,用於不同的字符象下面這樣:defface用較少的冗餘

(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定義,使代碼冗餘少? (我根據作爲defface做不同的終端類型知道make-face,但似乎過時,不能設置屬性。)

回答

2

怎麼樣宏和上後綴的映射運行的循環< - >顏色:

(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

現在你只需要保持快報名單er < - >顏色映射,並可能擴展宏以支持其他面部屬性(如果需要)。

4
  1. make-face並沒有被棄用,AFAICT。

  2. defface可以使用繼承 - 請參閱面部屬性:inherit。 不知道這是否有助於你的特定環境。

+1

+1。我將爲包含所有常用屬性的所有面定義一個基礎面,然後從其繼承所有其他面。 –

+1

+1。我在幫助信息中看到:'(make-face FACE&可選的NO-INIT-FROM-RESOURCES) 定義一個名爲FACE(一個符號)的新面孔。 不要直接從Lisp代碼中調用它;請使用defface。「這就是爲什麼我認爲它已被棄用的原因 – RNA

+0

有趣的是,您使用的是什麼Emacs版本?我沒有看到最後一句(關於不在Lisp代碼中使用它)作爲我從Emacs 20到昨天的Emacs 24版本的任何Emacs版本幫助的一部分。 – Drew