help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to put this in a macro


From: José A . Romero L .
Subject: Re: How to put this in a macro
Date: Tue, 04 May 2010 15:44:53 -0000
User-agent: G2/1.0

On 16 Kwi, 23:13, Cecil Westerhof <Ce...@decebal.nl> wrote:
> In my code I use the following code on several places:
>     (if (equal start end)
>         (setq start (point-min)
>               end   (point-max))
>       (setq start (or start (point-min)))
>       (setq end   (or end   (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é A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]