all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "José A. Romero L." <escherdragon@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to put this in a macro
Date: Fri, 16 Apr 2010 16:28:05 -0700 (PDT)	[thread overview]
Message-ID: <b56591a5-703b-45ce-ba4e-b7df3c8f77da@8g2000yqz.googlegroups.com> (raw)
In-Reply-To: 87pr1zw164.fsf@linux-lqcw.site

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	other threads:[~2010-04-16 23:28 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-16 21:13 How to put this in a macro Cecil Westerhof
2010-04-16 23:28 ` José A. Romero L. [this message]
2010-04-17  3:08 ` Barry Margolin
2010-04-17  6:38   ` Cecil Westerhof
2010-04-17 10:30     ` José A. Romero L.
2010-04-17 11:07       ` Cecil Westerhof
2010-04-17 11:40         ` Cecil Westerhof
2010-04-17 15:15           ` José A. Romero L.
2010-04-17 16:49             ` Barry Margolin
2010-04-17 16:57               ` José A. Romero L.
2010-04-17 21:41                 ` Barry Margolin
2010-04-17 19:38             ` Cecil Westerhof
2010-04-17 23:58               ` José A. Romero L.
2010-04-17 12:13         ` José A. Romero L.
2010-04-17 15:14       ` Barry Margolin
2010-04-17 16:49         ` José A. Romero L.
2010-04-17 15:12     ` Barry Margolin
2010-04-18  7:50 ` Pascal J. Bourguignon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b56591a5-703b-45ce-ba4e-b7df3c8f77da@8g2000yqz.googlegroups.com \
    --to=escherdragon@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.