3

I'm reading Emacs Lisp Intro by Robert J. Chassell.

In 6.1 The ‘save-restriction’ Special Form

when you use both ‘save-excursion’ and ‘save-restriction’, one right after the other, you should use ‘save-excursion’ outermost. If you write them in reverse order, you may fail to record narrowing in the buffer to which Emacs switches after calling ‘save-excursion’.

And in 6.2 what-line

(Note that the ‘(widen)’ expression comes between the ‘save-restriction’ and ‘save-excursion’ special forms. When you write the two ‘save- ...’ expressions in sequence, write ‘save-excursion’ outermost.)

I'm really confused about why it should be written like this:

(save-excursion
  (save-restriction
    BODY...))
shynur
  • 4,065
  • 1
  • 3
  • 23

1 Answers1

1

The Emacs Lisp Reference Manual says:



     ‘save-restriction’ does _not_ restore point; 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

NickD
  • 27,023
  • 3
  • 23
  • 42