如何在 Elisp defmacro 中使用局部变量?
How to use local variable in Elisp defmacro?
我在emacs的配置文件里写了一个elisp宏,但是像下面这样的东西,(intern (format "%s-display-table" name))
用了好几次,我怎么用一个类似变量的东西来表示呢?
;; Change the glyphs of "wrap", "truncation" and "vertical-border" in the display table specified by
;; parameter "name", obviously "↩", "…" and "ǁ" is better choice than the default values "\", "$"
;; and "|".
(defmacro change-glyphs-of-display-table (name)
`(lambda ()
(interactive)
(unless ,(intern (format "%s-display-table" name))
(setq ,(intern (format "%s-display-table" name)) (make-display-table)))
(set-display-table-slot ,(intern (format "%s-display-table" name)) 'wrap ?\↩)
(set-display-table-slot ,(intern (format "%s-display-table" name)) 'truncation ?\…)
(set-display-table-slot ,(intern (format "%s-display-table" name)) 'vertical-border ?\ǁ)))
将 name
作为宏的参数,您知道它在扩展时始终可用,因此您可以在反引号形式之外处理它:
;; Change the glyphs of "wrap", "truncation" and "vertical-border" in the display table specified by
;; parameter "name", obviously "↩", "…" and "ǁ" is better choice than the default values "\", "$"
;; and "|".
(defmacro change-glyphs-of-display-table (name)
(let ((namedisplaytable (intern (format "%s-display-table" name))))
`(lambda ()
(interactive)
(unless ,namedisplaytable
(setq ,namedisplaytable (make-display-table)))
(set-display-table-slot ,namedisplaytable 'wrap ?\↩)
(set-display-table-slot ,namedisplaytable 'truncation ?\…)
(set-display-table-slot ,namedisplaytable 'vertical-border ?\ǁ))))
我在emacs的配置文件里写了一个elisp宏,但是像下面这样的东西,(intern (format "%s-display-table" name))
用了好几次,我怎么用一个类似变量的东西来表示呢?
;; Change the glyphs of "wrap", "truncation" and "vertical-border" in the display table specified by
;; parameter "name", obviously "↩", "…" and "ǁ" is better choice than the default values "\", "$"
;; and "|".
(defmacro change-glyphs-of-display-table (name)
`(lambda ()
(interactive)
(unless ,(intern (format "%s-display-table" name))
(setq ,(intern (format "%s-display-table" name)) (make-display-table)))
(set-display-table-slot ,(intern (format "%s-display-table" name)) 'wrap ?\↩)
(set-display-table-slot ,(intern (format "%s-display-table" name)) 'truncation ?\…)
(set-display-table-slot ,(intern (format "%s-display-table" name)) 'vertical-border ?\ǁ)))
将 name
作为宏的参数,您知道它在扩展时始终可用,因此您可以在反引号形式之外处理它:
;; Change the glyphs of "wrap", "truncation" and "vertical-border" in the display table specified by
;; parameter "name", obviously "↩", "…" and "ǁ" is better choice than the default values "\", "$"
;; and "|".
(defmacro change-glyphs-of-display-table (name)
(let ((namedisplaytable (intern (format "%s-display-table" name))))
`(lambda ()
(interactive)
(unless ,namedisplaytable
(setq ,namedisplaytable (make-display-table)))
(set-display-table-slot ,namedisplaytable 'wrap ?\↩)
(set-display-table-slot ,namedisplaytable 'truncation ?\…)
(set-display-table-slot ,namedisplaytable 'vertical-border ?\ǁ))))