all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to put this in a macro
@ 2010-04-16 21:13 Cecil Westerhof
  2010-04-16 23:28 ` José A. Romero L.
                   ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Cecil Westerhof @ 2010-04-16 21:13 UTC (permalink / raw)
  To: help-gnu-emacs

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?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-16 21:13 How to put this in a macro Cecil Westerhof
@ 2010-04-16 23:28 ` José A. Romero L.
  2010-04-17  3:08 ` Barry Margolin
  2010-04-18  7:50 ` Pascal J. Bourguignon
  2 siblings, 0 replies; 18+ messages in thread
From: José A. Romero L. @ 2010-04-16 23:28 UTC (permalink / raw)
  To: help-gnu-emacs

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)



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-16 21:13 How to put this in a macro Cecil Westerhof
  2010-04-16 23:28 ` José A. Romero L.
@ 2010-04-17  3:08 ` Barry Margolin
  2010-04-17  6:38   ` Cecil Westerhof
  2010-04-18  7:50 ` Pascal J. Bourguignon
  2 siblings, 1 reply; 18+ messages in thread
From: Barry Margolin @ 2010-04-17  3:08 UTC (permalink / raw)
  To: help-gnu-emacs

In article <87pr1zw164.fsf@linux-lqcw.site>,
 Cecil Westerhof <Cecil@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?

(defmacro canonicalize-start-end (start-var end-var)
  `(if (equal ,start-var ,end-var)
       (setq ,start-var (point-min)
             ,end-var (point-max))
     (setq ,start-var (or ,start-var (point-min))
           ,end-var (or ,end-var (point-max))))

You then use this as:

(canonicalize-start-end start end)

-- 
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 ***


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  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 15:12     ` Barry Margolin
  0 siblings, 2 replies; 18+ messages in thread
From: Cecil Westerhof @ 2010-04-17  6:38 UTC (permalink / raw)
  To: help-gnu-emacs

Barry Margolin <barmar@alum.mit.edu> writes:

> (defmacro canonicalize-start-end (start-var end-var)
>   `(if (equal ,start-var ,end-var)
>        (setq ,start-var (point-min)
>              ,end-var (point-max))
>      (setq ,start-var (or ,start-var (point-min))
>            ,end-var (or ,end-var (point-max))))

This gives an error:
    Debugger entered--Lisp error: (void-variable start-var)
      (list (quote equal) start-var end-var)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  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 15:14       ` Barry Margolin
  2010-04-17 15:12     ` Barry Margolin
  1 sibling, 2 replies; 18+ messages in thread
From: José A. Romero L. @ 2010-04-17 10:30 UTC (permalink / raw)
  To: help-gnu-emacs

On 17 Kwi, 08:38, Cecil Westerhof <Ce...@decebal.nl> wrote:
> Barry Margolin <bar...@alum.mit.edu> writes:
> > (defmacro canonicalize-start-end (start-var end-var)
> >   `(if (equal ,start-var ,end-var)
> >        (setq ,start-var (point-min)
> >              ,end-var (point-max))
> >      (setq ,start-var (or ,start-var (point-min))
> >            ,end-var (or ,end-var (point-max))))
>
> This gives an error:
>     Debugger entered--Lisp error: (void-variable start-var)
>       (list (quote equal) start-var end-var)
(...)

Makes  sense.  In general writing things like `(setq ,foo bar) is (if
possible at all) asking for trouble -- what would happen when foo  is
nil, or 5?

Maybe you could do something like this:

    (defmacro cw/set-bounds (start-sym end-sym)
      `(if (equal (symbol-value ,start-sym) (symbol-value ,end-sym))
           (progn
             (set ,start-sym (point-min))
             (set ,end-sym (point-max)))
         (progn
           (set ,start-sym (or start (point-min)))
           (set ,end-sym (or end (point-max))))))

and use it as:

    (cw/set-bounds 'start 'end)

i.e. passing the symbols of the variables you want to set.

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  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 12:13         ` José A. Romero L.
  2010-04-17 15:14       ` Barry Margolin
  1 sibling, 2 replies; 18+ messages in thread
From: Cecil Westerhof @ 2010-04-17 11:07 UTC (permalink / raw)
  To: help-gnu-emacs

José A. Romero L. <escherdragon@gmail.com> writes:

> Maybe you could do something like this:
>
>     (defmacro cw/set-bounds (start-sym end-sym)
>       `(if (equal (symbol-value ,start-sym) (symbol-value ,end-sym))
>            (progn
>              (set ,start-sym (point-min))
>              (set ,end-sym (point-max)))
>          (progn
>            (set ,start-sym (or start (point-min)))
>            (set ,end-sym (or end (point-max))))))

Almost. It looks like the following does what I want:
    (defmacro cw/set-bounds (start-sym end-sym)
      `(if (equal (symbol-value ,start-sym) (symbol-value ,end-sym))
           (progn
             (set ,start-sym (point-min))
             (set ,end-sym (point-max)))
         (progn
           (set ,start-sym (or ,start-sym (point-min)))
           (set ,end-sym (or ,end-sym (point-max))))))

In the or's the parameter is used instead of the hard variables start
and end.

Thanks.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  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 12:13         ` José A. Romero L.
  1 sibling, 1 reply; 18+ messages in thread
From: Cecil Westerhof @ 2010-04-17 11:40 UTC (permalink / raw)
  To: help-gnu-emacs

Cecil Westerhof <Cecil@decebal.nl> writes:

> Almost. It looks like the following does what I want:
>     (defmacro cw/set-bounds (start-sym end-sym)
>       `(if (equal (symbol-value ,start-sym) (symbol-value ,end-sym))
>            (progn
>              (set ,start-sym (point-min))
>              (set ,end-sym (point-max)))
>          (progn
>            (set ,start-sym (or ,start-sym (point-min)))
>            (set ,end-sym (or ,end-sym (point-max))))))
>

Almost. It should be:
    (defmacro dcbl-set-bounds (start-sym end-sym)
      `(if (equal (symbol-value ,start-sym) (symbol-value ,end-sym))
           (progn
             (set ,start-sym (point-min))
             (set ,end-sym   (point-max)))
         (progn
           (set ,start-sym (or (symbol-value ,start-sym) (point-min)))
           (set ,end-sym   (or (symbol-value ,end-sym)   (point-max))))
           (if (> (symbol-value ,start-sym) (symbol-value ,end-sym))
               (let ((temp (symbol-value ,start-sym)))
                 (set ,start-sym (symbol-value ,end-sym))
                 (set ,end-sym   temp)))))

I also make sure now that start is not after end.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-17 11:07       ` Cecil Westerhof
  2010-04-17 11:40         ` Cecil Westerhof
@ 2010-04-17 12:13         ` José A. Romero L.
  1 sibling, 0 replies; 18+ messages in thread
From: José A. Romero L. @ 2010-04-17 12:13 UTC (permalink / raw)
  To: help-gnu-emacs

On 17 Kwi, 13:07, Cecil Westerhof <Ce...@decebal.nl> wrote:
> José A. Romero L. <escherdra...@gmail.com> writes:
(...)
> >          (progn
> >            (set ,start-sym (or start (point-min)))
> >            (set ,end-sym (or end (point-max))))))
>
> Almost. It looks like the following does what I want:
(...)
>          (progn
>            (set ,start-sym (or ,start-sym (point-min)))
>            (set ,end-sym (or ,end-sym (point-max))))))
>
> In the or's the parameter is used instead of the hard variables start
> and end.

Oops, sorry, that was a mind shortcut happening too soon. In any case
it should be rather:

(...)
    (progn
      (set ,start-sym (or (symbol-value ,start-sym) (point-min)))
      (set ,start-sym (or (symbol-value ,end-sym) (point-max))))

as you won't be passing nil as one of the arguments to the macro.

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-17  6:38   ` Cecil Westerhof
  2010-04-17 10:30     ` José A. Romero L.
@ 2010-04-17 15:12     ` Barry Margolin
  1 sibling, 0 replies; 18+ messages in thread
From: Barry Margolin @ 2010-04-17 15:12 UTC (permalink / raw)
  To: help-gnu-emacs

In article <87d3xywpm6.fsf@linux-lqcw.site>,
 Cecil Westerhof <Cecil@decebal.nl> wrote:

> Barry Margolin <barmar@alum.mit.edu> writes:
> 
> > (defmacro canonicalize-start-end (start-var end-var)
> >   `(if (equal ,start-var ,end-var)
> >        (setq ,start-var (point-min)
> >              ,end-var (point-max))
> >      (setq ,start-var (or ,start-var (point-min))
> >            ,end-var (or ,end-var (point-max))))
> 
> This gives an error:
>     Debugger entered--Lisp error: (void-variable start-var)
>       (list (quote equal) start-var end-var)

Sorry, I posted without testing.  Add another close paren at the end.

-- 
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 ***


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-17 10:30     ` José A. Romero L.
  2010-04-17 11:07       ` Cecil Westerhof
@ 2010-04-17 15:14       ` Barry Margolin
  2010-04-17 16:49         ` José A. Romero L.
  1 sibling, 1 reply; 18+ messages in thread
From: Barry Margolin @ 2010-04-17 15:14 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1689 bytes --]

In article 
<bf0039f9-f7eb-497d-bb74-88d3bb9c5077@u34g2000yqu.googlegroups.com>,
 José A. Romero L. <escherdragon@gmail.com> wrote:

> On 17 Kwi, 08:38, Cecil Westerhof <Ce...@decebal.nl> wrote:
> > Barry Margolin <bar...@alum.mit.edu> writes:
> > > (defmacro canonicalize-start-end (start-var end-var)
> > >   `(if (equal ,start-var ,end-var)
> > >        (setq ,start-var (point-min)
> > >              ,end-var (point-max))
> > >      (setq ,start-var (or ,start-var (point-min))
> > >            ,end-var (or ,end-var (point-max))))
> >
> > This gives an error:
> >     Debugger entered--Lisp error: (void-variable start-var)
> >       (list (quote equal) start-var end-var)
> (...)
> 
> Makes  sense.  In general writing things like `(setq ,foo bar) is (if
> possible at all) asking for trouble -- what would happen when foo  is
> nil, or 5?

My macro requires that the parameters be variables, not expressions.

> 
> Maybe you could do something like this:
> 
>     (defmacro cw/set-bounds (start-sym end-sym)
>       `(if (equal (symbol-value ,start-sym) (symbol-value ,end-sym))
>            (progn
>              (set ,start-sym (point-min))
>              (set ,end-sym (point-max)))
>          (progn
>            (set ,start-sym (or start (point-min)))
>            (set ,end-sym (or end (point-max))))))
> 
> and use it as:
> 
>     (cw/set-bounds 'start 'end)
> 
> i.e. passing the symbols of the variables you want to set.

If you call it like that, it can be a function, not a 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 ***


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  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 19:38             ` Cecil Westerhof
  0 siblings, 2 replies; 18+ messages in thread
From: José A. Romero L. @ 2010-04-17 15:15 UTC (permalink / raw)
  To: help-gnu-emacs

On 17 Kwi, 13:40, Cecil Westerhof <Ce...@decebal.nl> wrote:
(...)
>            (if (> (symbol-value ,start-sym) (symbol-value ,end-sym))
>                (let ((temp (symbol-value ,start-sym)))
>                  (set ,start-sym (symbol-value ,end-sym))
>                  (set ,end-sym   temp)))))
>
> I also make sure now that start is not after end.
(...)

Hmm, be careful with lets inside macros - this will work OK until one
of the variables you try to set is named precisely "temp" (no lexical
scope, remember?).

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  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 19:38             ` Cecil Westerhof
  1 sibling, 1 reply; 18+ messages in thread
From: Barry Margolin @ 2010-04-17 16:49 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 921 bytes --]

In article 
<0b67cc27-1c96-459b-a8f8-435cef946281@q15g2000yqj.googlegroups.com>,
 José A. Romero L. <escherdragon@gmail.com> wrote:

> On 17 Kwi, 13:40, Cecil Westerhof <Ce...@decebal.nl> wrote:
> (...)
> >            (if (> (symbol-value ,start-sym) (symbol-value ,end-sym))
> >                (let ((temp (symbol-value ,start-sym)))
> >                  (set ,start-sym (symbol-value ,end-sym))
> >                  (set ,end-sym   temp)))))
> >
> > I also make sure now that start is not after end.
> (...)
> 
> Hmm, be careful with lets inside macros - this will work OK until one
> of the variables you try to set is named precisely "temp" (no lexical
> scope, remember?).

Another reason why his version should be a function.

-- 
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 ***


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-17 15:14       ` Barry Margolin
@ 2010-04-17 16:49         ` José A. Romero L.
  0 siblings, 0 replies; 18+ messages in thread
From: José A. Romero L. @ 2010-04-17 16:49 UTC (permalink / raw)
  To: help-gnu-emacs

On 17 Kwi, 17:14, Barry Margolin <bar...@alum.mit.edu> wrote:
(...)
> > possible at all) asking for trouble -- what would happen when foo  is
> > nil, or 5?
>
> My macro requires that the parameters be variables, not expressions.

You're right. Emacs is actually smarter than I thought and won't let
you pass anything but variables to the call.

(...)
> > i.e. passing the symbols of the variables you want to set.
>
> If you call it like that, it can be a function, not a macro.
(...)

It can be a function or a macro, whatever you prefer, though I do
agree your version is definitely better.

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-17 16:49             ` Barry Margolin
@ 2010-04-17 16:57               ` José A. Romero L.
  2010-04-17 21:41                 ` Barry Margolin
  0 siblings, 1 reply; 18+ messages in thread
From: José A. Romero L. @ 2010-04-17 16:57 UTC (permalink / raw)
  To: help-gnu-emacs

On 17 Kwi, 18:49, Barry Margolin <bar...@alum.mit.edu> wrote:
(...)
> > Hmm, be careful with lets inside macros - this will work OK until one
> > of the variables you try to set is named precisely "temp" (no lexical
> > scope, remember?).
>
> Another reason why his version should be a function.
(...)

I don't think that would make any difference - let binds the same in
functions as it does in macros.

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-17 15:15           ` José A. Romero L.
  2010-04-17 16:49             ` Barry Margolin
@ 2010-04-17 19:38             ` Cecil Westerhof
  2010-04-17 23:58               ` José A. Romero L.
  1 sibling, 1 reply; 18+ messages in thread
From: Cecil Westerhof @ 2010-04-17 19:38 UTC (permalink / raw)
  To: help-gnu-emacs

José A. Romero L. <escherdragon@gmail.com> writes:

>>            (if (> (symbol-value ,start-sym) (symbol-value ,end-sym))
>>                (let ((temp (symbol-value ,start-sym)))
>>                  (set ,start-sym (symbol-value ,end-sym))
>>                  (set ,end-sym   temp)))))
>>
>> I also make sure now that start is not after end.
> (...)
>
> Hmm, be careful with lets inside macros - this will work OK until one
> of the variables you try to set is named precisely "temp" (no lexical
> scope, remember?).

I deleted it again. It is at the moment not important, because all the
functions that use the macro do not care if the end is before the
beginning. So it is not really important at the moment. But I like to
make robust code, and when in the future the macro is used where it is a
problem when the start is after the end ... So is it possible to switch
the values when the end is before the start?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-17 16:57               ` José A. Romero L.
@ 2010-04-17 21:41                 ` Barry Margolin
  0 siblings, 0 replies; 18+ messages in thread
From: Barry Margolin @ 2010-04-17 21:41 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 853 bytes --]

In article 
<20cf62c9-76ef-47c7-b9e4-edcc4b99b560@u34g2000yqu.googlegroups.com>,
 José A. Romero L. <escherdragon@gmail.com> wrote:

> On 17 Kwi, 18:49, Barry Margolin <bar...@alum.mit.edu> wrote:
> (...)
> > > Hmm, be careful with lets inside macros - this will work OK until one
> > > of the variables you try to set is named precisely "temp" (no lexical
> > > scope, remember?).
> >
> > Another reason why his version should be a function.
> (...)
> 
> I don't think that would make any difference - let binds the same in
> functions as it does in macros.

Right.  You even mentioned non-lexical scoping, although that's actually 
irrelevant when it's a 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 ***


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-17 19:38             ` Cecil Westerhof
@ 2010-04-17 23:58               ` José A. Romero L.
  0 siblings, 0 replies; 18+ messages in thread
From: José A. Romero L. @ 2010-04-17 23:58 UTC (permalink / raw)
  To: help-gnu-emacs

On 17 Kwi, 21:38, Cecil Westerhof <Ce...@decebal.nl> wrote:
(...)
> beginning. So it is not really important at the moment. But I like to
> make robust code, and when in the future the macro is used where it is a
> problem when the start is after the end ... So is it possible to switch
> the values when the end is before the start?
(...)

If you're in for a bit of coding stunts :-) I guess we could have one
last try, using your name, Barry's macro and shorter variable names:

    (defmacro dcbl-set-bounds (start end)
      `(progn
         (if (equal ,start ,end)
             (setq ,start (point-min)
                   ,end (point-max))
           (setq ,start (or ,start (point-min))
                 ,end (or ,end (point-max))))

         (setq ,start (list ,start ,end)
               ,start (sort ,start '<)
               ,end   (second ,start)
               ,start (first ,start))))

it should be OK to use one of the variables being changed as the temp
var, shouldn't it?

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: How to put this in a macro
  2010-04-16 21:13 How to put this in a macro Cecil Westerhof
  2010-04-16 23:28 ` José A. Romero L.
  2010-04-17  3:08 ` Barry Margolin
@ 2010-04-18  7:50 ` Pascal J. Bourguignon
  2 siblings, 0 replies; 18+ messages in thread
From: Pascal J. Bourguignon @ 2010-04-18  7:50 UTC (permalink / raw)
  To: help-gnu-emacs


Cecil Westerhof <Cecil@decebal.nl> writes:

> 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?

Yes.  


;; In a functional style:

(require 'cl)

(defun normalize-start-end (start end)
   ;; In addition, we will sort the bounds:
   (cond 
     ((null start)  (values (point-min) (or end (point-max))))
     ((null end)    (values start (point-max)))
     ((= start end) (values (point-min) (point-max)))
     ((< start end) (values start end))
     (t             (values end   start))))    

(defun cmd (start end)
  (interactive "r")
  (multiple-value-bind (start end) (normalize-start-end start end)
     ...))


;; In emacs lisp, we can also set the symbols:

(defun set-normalized-start-end (start-sym end-sym)
   (multiple-value-bind (start end) (normalize-start-end    
                                       (symbol-value start-sym)
                                       (symbol-value end-sym))
       (setf (symbol-value start-sym) start
             (symbol-value end-sym)   end)))

(defun cmd (start end)
   (interactive "r")
   (set-normalized-start-end 'start 'end)
   ...)


I would write the macro as:

(defmacro with-normalized-start-end ((start-var end-var) &body body)
  `(multiple-value-bind (,start-var ,end-var) 
                (normalize-start-end ,start-var ,end-var)
       ,@body))

(defun cmd (start end)
   (interactive "r")
   (with-normalized-start-end (start end)
       ...))


-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2010-04-18  7:50 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-16 21:13 How to put this in a macro Cecil Westerhof
2010-04-16 23:28 ` José A. Romero L.
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

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.