by shigemk2

当面は技術的なことしか書かない

マクロを定義する

以下のコードをマクロを使って書くことにする。

(if t
    (progn
      (setq a 1)
      (message "%s" a)))		; => "1"
(if nil
    (progn
      (setq a 1)
      (message "%s" a)))		; => nil

マクロを定義するにはdefmacroを使う

(defmacro my-when (condition &rest body)
  `(if ,condition
       (progn ,@body)))
;; テスト
(my-when t
	 1 2)				; => 2
(my-when t
	 1)				; => 1
(my-when nil
	 1 2)				; => nil

;; インデントが気にくわないのでdeclareを使って
;; インデントを調整する
(defmacro my-when (condition &rest body)
  (declare (indent 1))
  `(if ,condition
       (progn ,@body)))
;; テスト
(my-when t
  1 2)					; => 2
(my-when nil
  1 2 3 4)				; => nil

P182

Emacs Lispテクニックバイブル

Emacs Lispテクニックバイブル