From: Lennart Borgman <lennart.borgman@gmail.com>
To: "Pascal J. Bourguignon" <pjb@informatimago.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Need some education on defmacro define-toggle
Date: Tue, 29 Dec 2009 01:49:30 +0100 [thread overview]
Message-ID: <e01d8a50912281649o2348e936se4e5d27b0a9a076e@mail.gmail.com> (raw)
In-Reply-To: <871viezkcr.fsf@hubble.informatimago.com>
On Tue, Dec 29, 2009 at 1:37 AM, Pascal J. Bourguignon
<pjb@informatimago.com> wrote:
> Lennart Borgman <lennart.borgman@gmail.com> writes:
>
>> On Mon, Dec 28, 2009 at 10:33 PM, Pascal J. Bourguignon
>> <pjb@informatimago.com> wrote:
>>> Lennart Borgman <lennart.borgman@gmail.com> writes:
>>>
>>>> On Mon, Dec 28, 2009 at 6:16 AM, Lennart Borgman
>>>> <lennart.borgman@gmail.com> wrote:
>>>>> I just got into some trouble because of a badly written macro I
>>>>> believe. After the long discussions about defmacro here I wonder if
>>>>> anyone wants to help me out. How should I write the defmacro below?
>>>>
>>>>
>>>> I resorted to self education (which is not too bad). However I still
>>>> got problems - just on a more complicated level. The macro now looks
>>>> like this:
>>>>
>>>> (defmacro define-toggle (symbol value doc &rest args)
>>>> "Declare SYMBOL as a customizable variable with a toggle function."
>>>> (declare (doc-string 3) (debug t))
>>>> (let* ((SYMBOL-toggle (intern (concat (symbol-name symbol) "-toggle")))
>>>> (SYMBOL-name (symbol-name symbol))
>>>> (var-doc doc)
>>>> (fun-doc (concat "Toggles the \(boolean) value of `" SYMBOL-name
>>>> "'.\nFor how to set it permanently see this
>>>> variable.\n")))
>>>> (let ((var (append `(defcustom ,symbol ,value ,var-doc)
>>>> args
>>>> nil))
>>>> (fun `(defun ,SYMBOL-toggle ()
>>>> ,fun-doc
>>>> (interactive)
>>>> (customize-set-variable (quote ,symbol) (not ,symbol)))))
>>>> `(list 'progn ,var ,fun))))
>>
>>
>>> That said, why doesn't your macro just return
>>>
>>> `(progn ,var ,fun)
>>>
>>> instead of returning a list form that evaluates to something such as
>>>
>>> (progn rngalt-display-validation-header rngalt-display-validation-header-toggle)
>>>
>>> that needs to be evaluated (and of course fails since you didn't
>>> define these symbols as variables)?
>>
>>
>> It fails sometimes, but not always. It looks like it has something to
>> do with those eval above, but I do not understand how. If I just do
>> eval-buffer it works as I expect it to.
>
> Of course, when you evaluate `(list 'progn ,defcustom-form ,defun-form)
> the defcustom and the defun are evaluated, and they return their name argument.
Hm, in that case the defcustom and defun must have been evaluated
earlier because they did exist. Now I have changed this so many times
so I am not sure when.
> That's why I advise you to write instead:
>
> `(progn ,defcustom-form ,defun-form)
Thanks, yes, I understand that. I tried a lot of alternatives though
to get evaluation at the right time. Surprisingly enough it sometimes
worked with `(list 'prog ,var ,fun) and sometimes not.
I did not dig deep in this, because I know there are other people here
who understands this much better than me ... ;-)
In the current implementation (which seems to work) I have this
(let ((var (append `(defcustom ,symbol ,value ,var-doc)
args
nil))
(fun `(defun ,SYMBOL-toggle ()
,fun-doc
(interactive)
(customize-set-variable (quote ,symbol) (not ,symbol)))))
`(progn ,fun ,var)
)))
Does that look correct?
prev parent reply other threads:[~2009-12-29 0:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-12-28 5:16 Need some education on defmacro define-toggle Lennart Borgman
2009-12-28 19:28 ` Lennart Borgman
[not found] ` <mailman.352.1262028532.18930.help-gnu-emacs@gnu.org>
2009-12-28 21:33 ` Pascal J. Bourguignon
2009-12-28 21:58 ` Lennart Borgman
[not found] ` <mailman.358.1262037566.18930.help-gnu-emacs@gnu.org>
2009-12-29 0:37 ` Pascal J. Bourguignon
2009-12-29 0:49 ` Lennart Borgman [this message]
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=e01d8a50912281649o2348e936se4e5d27b0a9a076e@mail.gmail.com \
--to=lennart.borgman@gmail.com \
--cc=help-gnu-emacs@gnu.org \
--cc=pjb@informatimago.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.