all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* save-restriction, save-excursion
@ 2002-09-16 23:31 pokerface
  2002-09-17  2:02 ` Ryan Yeske
  0 siblings, 1 reply; 5+ messages in thread
From: pokerface @ 2002-09-16 23:31 UTC (permalink / raw)


I am beginning to study Emacs Lisp and I have the following question:

Why is it wrong to write (save-restriction
                           (save-excursion
                               ....))

and it is recommended instead to write
                         (save-excursion
                           (save-restriction
                               ....))

Can someone please give an example of the difference?

Thank you.

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

* Re: save-restriction, save-excursion
  2002-09-16 23:31 save-restriction, save-excursion pokerface
@ 2002-09-17  2:02 ` Ryan Yeske
  2002-09-17  2:33   ` David Kastrup
  0 siblings, 1 reply; 5+ messages in thread
From: Ryan Yeske @ 2002-09-17  2:02 UTC (permalink / raw)


pokerface <pokerface@use.net> writes:

> I am beginning to study Emacs Lisp and I have the following question:
> 
> Why is it wrong to write (save-restriction
>                            (save-excursion
>                                ....))
> 
> and it is recommended instead to write
>                          (save-excursion
>                            (save-restriction
>                                ....))

From the node "Narrowing" in the elisp manual:

     `save-restriction' does _not_ restore point and the mark; use
     `save-excursion' for that.  If you use both `save-restriction' and
     `save-excursion' together, `save-excursion' should come first (on
     the outside).  Otherwise, the old point value would be restored
     with temporary narrowing still in effect.  If the old point value
     were outside the limits of the temporary narrowing, this would
     fail to restore it accurately.

> Can someone please give an example of the difference?

(defun do-excursion-restriction ()
  (save-excursion
    (save-restriction
      (do-it))))

(defun do-restriction-excursion ()
  (save-restriction
    (save-excursion
      (do-it))))

(defun do-it ()
  (goto-char (point-min))
  (narrow-to-region (point) (+ 1 (point))))
    
;; this one restores point properly:
(do-excursion-restriction)
;; this one doesn't:
(do-restriction-excursion)

;; Hope that helps.
;; Ryan

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

* Re: save-restriction, save-excursion
  2002-09-17  2:02 ` Ryan Yeske
@ 2002-09-17  2:33   ` David Kastrup
  2002-09-17  6:24     ` Ryan Yeske
  0 siblings, 1 reply; 5+ messages in thread
From: David Kastrup @ 2002-09-17  2:33 UTC (permalink / raw)


Ryan Yeske <rcyeske+spam@vcn.bc.ca> writes:

> pokerface <pokerface@use.net> writes:
> 
> > I am beginning to study Emacs Lisp and I have the following question:
> > 
> > Why is it wrong to write (save-restriction
> >                            (save-excursion
> >                                ....))
> > 
> > and it is recommended instead to write
> >                          (save-excursion
> >                            (save-restriction
> >                                ....))
> 
> From the node "Narrowing" in the elisp manual:
> 
>      `save-restriction' does _not_ restore point and the mark; use
>      `save-excursion' for that.  If you use both `save-restriction' and
>      `save-excursion' together, `save-excursion' should come first (on
>      the outside).  Otherwise, the old point value would be restored
>      with temporary narrowing still in effect.  If the old point value
>      were outside the limits of the temporary narrowing, this would
>      fail to restore it accurately.

Hmmm.  What if I have a restriction active and write
(save-excursion
  (save-restriction
    (widen)
    (goto (point-min))))

When the restriction gets restored, point will lie outside of the
restored restriction.  Wouldn't that cause trouble?

Should one rather have
(save-excursion
  (save-restriction

When the change of restriction is to something narrower, but

(save-restriction
  (save-excursion

when the change is to something wider?

Or is this nonsense for some reason?

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum
Email: David.Kastrup@t-online.de

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

* Re: save-restriction, save-excursion
  2002-09-17  2:33   ` David Kastrup
@ 2002-09-17  6:24     ` Ryan Yeske
  2002-09-18 22:17       ` pokerface
  0 siblings, 1 reply; 5+ messages in thread
From: Ryan Yeske @ 2002-09-17  6:24 UTC (permalink / raw)


David Kastrup <David.Kastrup@t-online.de> writes:

> Ryan Yeske <rcyeske+spam@vcn.bc.ca> writes:
> 
> > From the node "Narrowing" in the elisp manual:
> > 
> >      `save-restriction' does _not_ restore point and the mark; use
> >      `save-excursion' for that.  If you use both `save-restriction' and
> >      `save-excursion' together, `save-excursion' should come first (on
> >      the outside).  Otherwise, the old point value would be restored
> >      with temporary narrowing still in effect.  If the old point value
> >      were outside the limits of the temporary narrowing, this would
> >      fail to restore it accurately.
> 
> Hmmm.  What if I have a restriction active and write
> (save-excursion
>   (save-restriction
>     (widen)
>     (goto (point-min))))
> 
> When the restriction gets restored, point will lie outside of the
> restored restriction.  Wouldn't that cause trouble?

Try it :)

It helps me to think about it like this:

1) save the point
2) save the restriction
3) frob the restriction (narrow *or* widen)
4) frob the point
5) restore the old restriction
6) restore the old point
 
It doesn't matter whether you are widening or narrowing.  When the
restriction is restored (step 5), the point is adjusted to be within
the restriction (the point can never be outside the current
restriction).  It doesn't really matter though, since the point is
then immediatly restored to the initial value within the initial
restriction at step 6.

Ryan

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

* Re: save-restriction, save-excursion
  2002-09-17  6:24     ` Ryan Yeske
@ 2002-09-18 22:17       ` pokerface
  0 siblings, 0 replies; 5+ messages in thread
From: pokerface @ 2002-09-18 22:17 UTC (permalink / raw)


Thank you gentlemen for the helpful replies!

Ryan Yeske <rcyeske@vcn.bc.ca> wrote in
news:87fzw9ceye.fsf@cut.hotdog.tmp: 

> David Kastrup <David.Kastrup@t-online.de> writes:
> 
>> Ryan Yeske <rcyeske+spam@vcn.bc.ca> writes:
>> 
>> > From the node "Narrowing" in the elisp manual:
>> > 
>> >      `save-restriction' does _not_ restore point and the mark; use
>> >      `save-excursion' for that.  If you use both `save-restriction'
>> >      and `save-excursion' together, `save-excursion' should come
>> >      first (on the outside).  Otherwise, the old point value would
>> >      be restored with temporary narrowing still in effect.  If the
>> >      old point value were outside the limits of the temporary
>> >      narrowing, this would fail to restore it accurately.
>> 
>> Hmmm.  What if I have a restriction active and write
>> (save-excursion
>>   (save-restriction
>>     (widen)
>>     (goto (point-min))))
>> 
>> When the restriction gets restored, point will lie outside of the
>> restored restriction.  Wouldn't that cause trouble?
> 
> Try it :)
> 
> It helps me to think about it like this:
> 
> 1) save the point
> 2) save the restriction
> 3) frob the restriction (narrow *or* widen)
> 4) frob the point
> 5) restore the old restriction
> 6) restore the old point
>  
> It doesn't matter whether you are widening or narrowing.  When the
> restriction is restored (step 5), the point is adjusted to be within
> the restriction (the point can never be outside the current
> restriction).  It doesn't really matter though, since the point is
> then immediatly restored to the initial value within the initial
> restriction at step 6.
> 
> Ryan
> 

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

end of thread, other threads:[~2002-09-18 22:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-09-16 23:31 save-restriction, save-excursion pokerface
2002-09-17  2:02 ` Ryan Yeske
2002-09-17  2:33   ` David Kastrup
2002-09-17  6:24     ` Ryan Yeske
2002-09-18 22:17       ` pokerface

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.