unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Ryan Yeske <rcyeske+spam@vcn.bc.ca>
Subject: Re: save-restriction, save-excursion
Date: Tue, 17 Sep 2002 02:02:05 GMT	[thread overview]
Message-ID: <87ofaxcr3e.fsf@cut.hotdog.tmp> (raw)
In-Reply-To: Xns928C53AA2BB4pokerfaceusenet@130.133.1.4

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	other threads:[~2002-09-17  2:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-16 23:31 save-restriction, save-excursion pokerface
2002-09-17  2:02 ` Ryan Yeske [this message]
2002-09-17  2:33   ` David Kastrup
2002-09-17  6:24     ` Ryan Yeske
2002-09-18 22:17       ` pokerface

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ofaxcr3e.fsf@cut.hotdog.tmp \
    --to=rcyeske+spam@vcn.bc.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).