From mboxrd@z Thu Jan 1 00:00:00 1970 From: Marcin Borkowski Subject: Re: Why does evaluating a piece of Elisp code seemingly not expand a macro? Date: Sun, 17 Jan 2016 23:56:49 +0100 Message-ID: <87io2r95n2.fsf@mbork.pl> References: <87a8o7duj6.fsf@mbork.pl> <878u3rcdpu.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: In-reply-to: <878u3rcdpu.fsf@gmail.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org To: Oleh Krehel Cc: Help Gnu Emacs mailing list , Org-Mode mailing list List-Id: emacs-orgmode.gnu.org On 2016-01-15, at 11:57, Oleh Krehel wrote: > Marcin Borkowski writes: > >> Why? > > Macro-expand the defun to get: > > (defalias 'print-answer > #'(lambda nil > (message > "The answer is %s." > (forty-two)))) > > `lambda' is a macro that /quotes/ its body. Therefore, the body of > `defun' is not evaluated or expanded when it's defined. Interesting. 1. Why is lambda sharp-quoted? I remember reading (in Artur's blog) that it shouldn't be. 2. I always thought that macros get expanded on compilation (or defining the function). If I evaluate all forms I've written about outside Org (using C-M-x, for instance), the `forty-two' macro seems to get expanded. In the manual (info "(elisp)Expansion"), I could find this: --8<---------------cut here---------------start------------->8--- Note that Emacs tries to expand macros when loading an uncompiled Lisp file. This is not always possible, but if it is, it speeds up subsequent execution. *Note How Programs Do Loading::. --8<---------------cut here---------------end--------------->8--- Does it mean that C-M-x is different than loading? Or C-x C-e, for that matter? Is this covered by the manual? (If not, it might need correcting.) > You probably wanted something like this instead: > > (macroexpand-all > '(lambda nil > (message > "The answer is %s." > (forty-two)))) > ;; => > ;; (function > ;; (lambda nil > ;; (message > ;; "The answer is %s." > ;; 42))) > > Which could be wrapped in a new macro: > > (defmacro defun-1 (name arglist &optional docstring &rest body) > (unless (stringp docstring) > (setq body > (if body > (cons docstring body) > docstring)) > (setq docstring nil)) > (list 'defun name arglist docstring (macroexpand-all body))) > > The above seems to work, at least superficially: > > (symbol-function > (defun-1 print-answer () > (message "The answer is %s." (forty-two)))) > ;; => > ;; (lambda nil > ;; (message > ;; "The answer is %s." > ;; 42)) Interesting, I will study this (but not today - it's 23:51 here, I'll need sleep soon!) > By the way, it might be more appropriate to ask similar questions on > help-gnu-emacs@gnu.org. I posted this reply there, too, though in view of what I wrote above I still think this is Org-related. > Oleh Best, -- Marcin Borkowski http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski Faculty of Mathematics and Computer Science Adam Mickiewicz University