From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Barry Margolin Newsgroups: gmane.emacs.help Subject: Re: How to define a macro correctly? Date: Mon, 18 Oct 2010 21:39:44 -0400 Organization: A noiseless patient Spider Message-ID: References: NNTP-Posting-Host: lo.gmane.org X-Trace: dough.gmane.org 1291876076 2646 80.91.229.12 (9 Dec 2010 06:27:56 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 9 Dec 2010 06:27:56 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 09 07:27:52 2010 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.69) (envelope-from ) id 1PQZyp-0008UM-Ap for geh-help-gnu-emacs@m.gmane.org; Thu, 09 Dec 2010 07:27:51 +0100 Original-Received: from localhost ([127.0.0.1]:58877 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQZyo-0000EZ-Fp for geh-help-gnu-emacs@m.gmane.org; Thu, 09 Dec 2010 01:27:50 -0500 Original-Path: usenet.stanford.edu!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 45 Injection-Info: barmar.motzarella.org; posting-host="+bxBiZT/p0ZLLuDb5H+JCw"; logging-data="13250"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19V4hWrlMTq58l9vHQjW0tI" User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Cancel-Lock: sha1:2x+dGinoyOqql8q/453cZL+bajU= Original-Xref: usenet.stanford.edu gnu.emacs.help:181857 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:76790 Archived-At: In article , Johan Andersson wrote: > Hey, > > I want to create a macro that sets a variable value and then executes body. > I know how to solve it, but I want to know which way is the best (more > correct). I came up with these solutions: > > a) > (defmacro mac (&rest body) > `(progn > (setq var t) > ,@body)) > > b) > (defmacro mac (&rest body) > (setq var t) > `(progn ,@body)) > > c) > (defmacro mac (&rest body) > (cons 'progn (cons (list 'setq 'var t) body))) > > I noticed that (using macroexpand) macro a and c expands to the same list. b > however sets the variable in the macro and then only return the list body. > What does that mean exactly, that I set the variable in the macro and do not > return it as a list? > > What way is the best? Or is there some other way that is better? > > Thanks! a and c are equivalent. The backquote expression in a is just shorthand for the cons stuff you wrote in c (or something equivalent to it). b is wrong. The setq happens once at compile time, not execution time. You'll notice the difference when you byte-compile a file that uses the macro. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group ***