我正在尝试定义一些emacs字体,以进行一些自定义突出显示。当我单独定义它时,这似乎有效:
(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
但是我有几打这些,我想从一些颜色表中一次性定义它们:
(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-”和“-face”连接到表格中条目的任一侧。我发现(实习生)可以从一个字符串中创建一个新符号,但是这个符号对于(defface)来说是不可接受的,因为看起来我所做的相当于(defface'my-r-face ...,并且defface不喜欢引用的符号,并期望(defface my-r-face ..而不是。我的尝试如下:
(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)
)
运行此结果
Lisp error: (wrong-type-argument symbolp (intern (concat "fegs-" (nth 1 tpl) "-face")))
(defvar (intern (concat "fegs-" ... "-face")) (quote (intern ...)) "colour")
任何人都可以了解我做错了什么,或者我是否完全吠叫了错误的树,还有更好的方法吗?
感谢。
答案 0 :(得分:3)
您可以避免eval
:
(defconst my-ctable '(...))
(defmacro my-init-cfaces ()
`(progn
,@(mapcar (lambda (tpl)
`(defface ,(intern (format "my-%s-face" (nth 1 tpl)))
'((t :foreground ,(car tpl)))
,(format "Face for color %s." (car tpl))
:group 'fortran))
my-ctable)))
(my-init-cfaces)
答案 1 :(得分:1)
defvar是一种特殊形式,defface是一个宏(因此参数传递未经评估)。您是否尝试使用
行中的内容(eval `(defface ,(intern "foo") '((t (:foreground "red"))) "Highlight" :group 'fortran))
(eval `(defvar ,(intern "bar")))
答案 2 :(得分:0)
最终有效的完整代码如下:
(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") ))
(dolist (tpl ctable)
(let ((fname (concat "fegs-" (nth 1 tpl) "-face")))
(eval `(defface ,(intern fname) '((t (:foreground ,(car tpl)))) "Highlight" :group 'fortran))
(eval `(defvar ,(intern fname) ',(intern fname)))
)
)
我的defvar线略有不同,因为这样可以通过其他地方的突出显示代码将面部拾取为全局变量。