From: Marcin Borkowski <mbork@mbork.pl>
To: Oleh Krehel <ohwoeowho@gmail.com>
Cc: Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>,
Org-Mode mailing list <emacs-orgmode@gnu.org>
Subject: Re: Why does evaluating a piece of Elisp code seemingly not expand a macro?
Date: Sun, 17 Jan 2016 23:56:49 +0100 [thread overview]
Message-ID: <87io2r95n2.fsf@mbork.pl> (raw)
In-Reply-To: <878u3rcdpu.fsf@gmail.com>
On 2016-01-15, at 11:57, Oleh Krehel <ohwoeowho@gmail.com> wrote:
> Marcin Borkowski <mbork@mbork.pl> 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
next prev parent reply other threads:[~2016-01-17 22:56 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-01-15 10:08 Why does evaluating a piece of Elisp code seemingly not expand a macro? Marcin Borkowski
2016-01-15 10:57 ` Oleh Krehel
2016-01-17 22:56 ` Marcin Borkowski [this message]
2016-01-18 13:54 ` Stefan Monnier
2016-01-18 20:03 ` Marcin Borkowski
2016-01-18 20:19 ` Stefan Monnier
2016-01-15 21:10 ` Samuel W. Flint
2016-01-15 22:06 ` Nick Dokos
2016-01-15 22:24 ` Marcin Borkowski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87io2r95n2.fsf@mbork.pl \
--to=mbork@mbork.pl \
--cc=emacs-orgmode@gnu.org \
--cc=help-gnu-emacs@gnu.org \
--cc=ohwoeowho@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.