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...))

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.