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: Mon, 28 Dec 2009 22:58:49 +0100 Message-ID: References: <87d41yzsua.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 1262037598 18009 80.91.229.12 (28 Dec 2009 21:59:58 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 28 Dec 2009 21:59:58 +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 Mon Dec 28 22:59:50 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 1NPNcw-0007F2-M9 for geh-help-gnu-emacs@m.gmane.org; Mon, 28 Dec 2009 22:59:50 +0100 Original-Received: from localhost ([127.0.0.1]:56854 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NPNcw-00022S-31 for geh-help-gnu-emacs@m.gmane.org; Mon, 28 Dec 2009 16:59:46 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NPNca-00021V-CW for help-gnu-emacs@gnu.org; Mon, 28 Dec 2009 16:59:24 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NPNcV-00020p-JS for help-gnu-emacs@gnu.org; Mon, 28 Dec 2009 16:59:24 -0500 Original-Received: from [199.232.76.173] (port=60506 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NPNcV-00020m-En for help-gnu-emacs@gnu.org; Mon, 28 Dec 2009 16:59:19 -0500 Original-Received: from mail-yw0-f177.google.com ([209.85.211.177]:54462) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NPNcS-0004YX-GZ for help-gnu-emacs@gnu.org; Mon, 28 Dec 2009 16:59:19 -0500 Original-Received: by ywh7 with SMTP id 7so910836ywh.24 for ; Mon, 28 Dec 2009 13:59:09 -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=NQ0vYDVOWRygthWBXU1ZtrSTo3KawuXpz4sNf6MaJHo=; b=GSMuzmY4N/qCLMVDJMSBPAyOE3/uS4+pDEE4RGDkGDifTHlyElx1cL4RtWsRIfGR3/ a+MGCb77OkAKwT7DIooVxmWsyqSUmcHd5Xgio8z7J9OPw3dQJfhLnHUQgoiSwvjlgGc3 GlEf8hpkceooJDMD1Yo8ICdQrl4JX6CqfZ0kk= 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=fLCUkM5MlB66HKOaSS5+pbZGHikzYLjMXDO1e7yQSUi4i9St+6oyq3Qq3lDkrxVp3B BOIqN++tnYrQngupHeYIfCj2CMT49i7/bHCJs2nNfryGK20fdYmv3ge88DVzKK4qjYuX 4AkJmXEnpPH61zkfByhUFCgl9MRdPWBQ7uIMs= Original-Received: by 10.101.181.15 with SMTP id i15mr24502133anp.90.1262037549672; Mon, 28 Dec 2009 13:59:09 -0800 (PST) In-Reply-To: <87d41yzsua.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:70914 Archived-At: 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 function= ." >> =C2=A0 (declare =C2=A0 (doc-string 3) =C2=A0 (debug t)) >> =C2=A0 (let* ((SYMBOL-toggle (intern (concat (symbol-name symbol) "-togg= le"))) >> =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 \(boolea= n) 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,fu= n-doc >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(in= teractive) >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(cu= stomize-set-variable (quote ,symbol) (not ,symbol))))) >> =C2=A0 =C2=A0 =C2=A0 `(list 'progn ,var ,fun)))) >> >> This seems to work, but if the call to define-toggle already has been >> done then loading a file with this define-toggle gives trouble: >> >> Debugger entered--Lisp error: (void-variable rngalt-display-validation-h= eader-toggle) >> =C2=A0 (progn rngalt-display-validation-header rngalt-display-validation= -header-toggle) >> =C2=A0 (define-toggle rngalt-display-validation-header t "Display XML va= lidation headers at the top of buffer when t.\nThe validation header is onl= y displayed in buffers where the main\nmajor mode is derivd from `nxml-mode= '." :set (lambda (sym val) (set-default sym val) (rngalt-update-validation-= header-overlay-everywhere)) :group (quote relax-ng) :group (quote nxhtml)) >> >> ... >> >> There is obviously something I got wrong in the evaluation order since >> now, in this situation, the macro now tries to evaluate >> >> =C2=A0 (progn rngalt-display-validation-header rngalt-display-validation= -header-toggle) >> >> I expected it to redefine the defcustom and the defun instead. >> Can someone please try to explain what is going on? > > There must be an eval somewhere that tries to evaluate one time too many. Hm, yes, but it is not mine... As you can see from the backtrace there is some eval-buffer in the stack. > 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-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. I have done some small changes and at the moment it seems to work, but I have no idea why... There is clearly something in the order of evaluation that beats me. If I just print out the values of fun and var before comuting the result to return they look as I expect them to, ie they are lists. But then in the progn list they are converted to symbols, in some step.