From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help,gmane.emacs.orgmode 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> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1453071456 5489 80.91.229.3 (17 Jan 2016 22:57:36 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 17 Jan 2016 22:57:36 +0000 (UTC) Cc: Help Gnu Emacs mailing list , Org-Mode mailing list To: Oleh Krehel Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Jan 17 23:57:29 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aKwGG-0001El-Fe for geh-help-gnu-emacs@m.gmane.org; Sun, 17 Jan 2016 23:57:28 +0100 Original-Received: from localhost ([::1]:56173 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aKwGF-0006kh-N1 for geh-help-gnu-emacs@m.gmane.org; Sun, 17 Jan 2016 17:57:27 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50722) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aKwFz-0006fp-Sq for help-gnu-emacs@gnu.org; Sun, 17 Jan 2016 17:57:13 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aKwFy-00048i-P9 for help-gnu-emacs@gnu.org; Sun, 17 Jan 2016 17:57:11 -0500 Original-Received: from mail.mojserwer.eu ([2a01:5e00:2:52::8]:60586) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aKwFp-00044T-P3; Sun, 17 Jan 2016 17:57:06 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 606F48F2023; Sun, 17 Jan 2016 23:57:00 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id y9ZK9Rz1NnTI; Sun, 17 Jan 2016 23:56:57 +0100 (CET) Original-Received: from localhost (unknown [109.232.24.28]) by mail.mojserwer.eu (Postfix) with ESMTPSA id C7A1B8F2020; Sun, 17 Jan 2016 23:56:56 +0100 (CET) User-agent: mu4e 0.9.13; emacs 25.1.50.1 In-reply-to: <878u3rcdpu.fsf@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2a01:5e00:2:52::8 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor 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 Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:108763 gmane.emacs.orgmode:104284 Archived-At: 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