From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Lennart Borgman Newsgroups: gmane.emacs.help Subject: Re: Need some education on defmacro define-toggle Date: Tue, 29 Dec 2009 01:49:30 +0100 Message-ID: References: <87d41yzsua.fsf@hubble.informatimago.com> <871viezkcr.fsf@hubble.informatimago.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1262047827 10861 80.91.229.12 (29 Dec 2009 00:50:27 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 29 Dec 2009 00:50:27 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: "Pascal J. Bourguignon" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Dec 29 01:50:20 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NPQHz-0001BH-GB for geh-help-gnu-emacs@m.gmane.org; Tue, 29 Dec 2009 01:50:19 +0100 Original-Received: from localhost ([127.0.0.1]:53817 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NPQHz-0002tC-Sr for geh-help-gnu-emacs@m.gmane.org; Mon, 28 Dec 2009 19:50:19 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NPQHd-0002so-Gb for help-gnu-emacs@gnu.org; Mon, 28 Dec 2009 19:49:57 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NPQHY-0002sE-4t for help-gnu-emacs@gnu.org; Mon, 28 Dec 2009 19:49:56 -0500 Original-Received: from [199.232.76.173] (port=37096 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NPQHX-0002sB-W9 for help-gnu-emacs@gnu.org; Mon, 28 Dec 2009 19:49:52 -0500 Original-Received: from mail-gx0-f215.google.com ([209.85.217.215]:45099) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NPQHX-0002jD-GD for help-gnu-emacs@gnu.org; Mon, 28 Dec 2009 19:49:51 -0500 Original-Received: by gxk7 with SMTP id 7so139104gxk.12 for ; Mon, 28 Dec 2009 16:49:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=+rPIKPdPg8dXsZf6orwtIXG3ICIDOeD2E0c75dxtCGE=; b=A6Qr1un4A+haEmb+xpEXkc6Ol33MRkkReBsPYtvJLVvWrnr1awC6lawaUcvT7u4ps+ 4UFB37qYs7Ht5OWRr7PdVLFZkq5KZGzrH/aN0bBdFYWf9GuiLQiN8IMzgjFSfrxTul0y HiQ4+5WT01uRYvUMJbWUXHnDENW5bYkzfctNQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=DtQ5RnWBPtDHFuPSACj6O4LLDcDS06EjaojO5KM8pR452wdwKI2ggb8r5rzSH1aLFz w7M0Dk5ISjzr86CiuISqE0nIhm0OtvHKK5m1t7xeSM/fnGqbQYJL5SX5u6zhdk8L+7pZ yX5/orLVu1i0gZbmN3X6vvvpXqSEF5i6A1c0k= Original-Received: by 10.100.234.26 with SMTP id g26mr16208774anh.38.1262047790444; Mon, 28 Dec 2009 16:49:50 -0800 (PST) In-Reply-To: <871viezkcr.fsf@hubble.informatimago.com> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:70923 Archived-At: On Tue, Dec 29, 2009 at 1:37 AM, Pascal J. Bourguignon wrote: > Lennart Borgman writes: > >> On Mon, Dec 28, 2009 at 10:33 PM, Pascal J. Bourguignon >> wrote: >>> Lennart Borgman writes: >>> >>>> On Mon, Dec 28, 2009 at 6:16 AM, Lennart Borgman >>>> 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) >>>> =C2=A0 "Declare SYMBOL as a customizable variable with a toggle functi= on." >>>> =C2=A0 (declare =C2=A0 (doc-string 3) =C2=A0 (debug t)) >>>> =C2=A0 (let* ((SYMBOL-toggle (intern (concat (symbol-name symbol) "-to= ggle"))) >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(SYMBOL-name (symbol-name symbol)) >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(var-doc doc) >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(fun-doc (concat "Toggles the \(bool= ean) value of `" SYMBOL-name >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 "'.\nFor how to set it permanently see this >>>> variable.\n"))) >>>> =C2=A0 =C2=A0 (let ((var (append `(defcustom ,symbol ,value ,var-doc) >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 args >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 nil)) >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (fun `(defun ,SYMBOL-toggle () >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0,= fun-doc >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(= interactive) >>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(= customize-set-variable (quote ,symbol) (not ,symbol))))) >>>> =C2=A0 =C2=A0 =C2=A0 `(list 'progn ,var ,fun)))) >> >> >>> That said, why doesn't your macro just return >>> >>> =C2=A0 `(progn ,var ,fun) >>> >>> instead of returning a list form that evaluates to something such as >>> >>> =C2=A0 (progn rngalt-display-validation-header rngalt-display-validatio= n-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 arg= ument. 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: > > =C2=A0 =C2=A0 `(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?