help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: save-restriction, save-excursion


From: Ryan Yeske
Subject: Re: save-restriction, save-excursion
Date: Tue, 17 Sep 2002 02:02:05 GMT
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]