如何在Elisp defmacro中使用局部变量?

时间:2018-07-06 08:57:55

标签: emacs macros elisp

我在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 ?\ǁ)))

1 个答案:

答案 0 :(得分:2)

使用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 ?\ǁ))))