From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?ISO-8859-1?Q?Jos=E9_A=2E_Romero_L=2E?= Newsgroups: gmane.emacs.help Subject: Re: How to put this in a macro Date: Fri, 16 Apr 2010 16:28:05 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <87pr1zw164.fsf@linux-lqcw.site> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1273008240 15727 80.91.229.12 (4 May 2010 21:24:00 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 4 May 2010 21:24:00 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue May 04 23:23:59 2010 connect(): No such file or directory 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 1O9Paw-0004Rr-OM for geh-help-gnu-emacs@m.gmane.org; Tue, 04 May 2010 23:23:59 +0200 Original-Received: from localhost ([127.0.0.1]:51901 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O9Paw-0002dx-5Y for geh-help-gnu-emacs@m.gmane.org; Tue, 04 May 2010 17:23:58 -0400 Original-Path: usenet.stanford.edu!postnews.google.com!8g2000yqz.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 49 Original-NNTP-Posting-Host: 89.78.68.41 Original-X-Trace: posting.google.com 1271460485 16077 127.0.0.1 (16 Apr 2010 23:28:05 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 16 Apr 2010 23:28:05 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 8g2000yqz.googlegroups.com; posting-host=89.78.68.41; posting-account=mkEKGAoAAACAV2vhv5r9WHXWqsdL_niD User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3,gzip(gfe) Original-Xref: usenet.stanford.edu gnu.emacs.help:177734 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:73182 Archived-At: On 16 Kwi, 23:13, Cecil Westerhof wrote: > In my code I use the following code on several places: > =A0 =A0 (if (equal start end) > =A0 =A0 =A0 =A0 (setq start (point-min) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 end =A0 (point-max)) > =A0 =A0 =A0 (setq start (or start (point-min))) > =A0 =A0 =A0 (setq end =A0 (or end =A0 (point-max)))) > > I think it would be good to put this in a macro. How would I write a > macro for this code? > > Or could it be done with a function? You could certainly write some macro for that piece of code, but I'd strongly recommend against doing so. Just think about it, suppose you write this: (defmacro cw/fix-bounds (start end) `(if (equal ,start ,end) (setq start (point-min) end (point-max)) (setq start (or ,start (point-min)) end (or ,end (point-max))))) then, at run time you have no real control over what you are actually setting: the variables start and end may or may not be defined at the point (and in the scope) where the macro is expanded, or may have a totally different meaning or type than what you originally imagined - Welcome to dynamic scoping land :-) But this isn't only a matter of dynamic vs. lexical scoping - I think you may be trying to refactor your code at a too low level. Consider the way you're using those variables: are they by chance being used always together, to represent some kind of extent in a buffer? if so, then that's what IMHO you should be trying to model. In any case, try to look at the surroundings of those lines for more common behavior that could be factored out, even if the code doesn't look the same. That's it: see your program as living behavior, not as dead code. Duh, looks like I should better go to sleep now ;-) Cheers, -- Jos=E9 A. Romero L. escherdragon at gmail "We who cut mere stones must always be envisioning cathedrals." (Quarry worker's creed)