1

So I'm reading through the Emacs Lisp intro, and I come across this function used as an example for save-excursion:

(message "We are %d characters into this buffer."
                   (- (point)
                      (save-excursion
                        (goto-char (point-min)) (point))))

This doesn't seem to make any sense in terms of why the save-excursion is there, what's the difference between that and this variation?

(message "We are %d characters into this buffer."
         (point))

To me, it seems like all that

(save-excursion
  (goto-char (point-min)) (point))

does is go to the beginning of the buffer and then call the point function...which will return zero. so then what we're effectively doing is subtracting the point called outside the save-excursion by 0. Is there some hidden functionality that I'm not catching or was that just put there for the sake of showing how save-excursion works?

4
  • 1
    Hmmm It seems as if the way it's implemented by the emacs intro actually makes it susceptible to narrowing based on the how point-min function works. Commented Jun 28, 2021 at 0:52
  • 2
    Yes, (save-excursion (goto-char (point-min)) (point)) could just be (point-min) -- which makes this a pretty weird example, given that the more verbose code uses (point-min). I'd say it's a contrived example for demonstration purposes, but that a better example would probably be sensible. Commented Jun 28, 2021 at 1:28
  • 2
    Note also that character positions start from 1, not 0. Commented Jun 28, 2021 at 1:31
  • 2
    I agree with what others have said in comments here. Someone should consider reporting it using M-x report-emacs-bug. Surely a better example could be used to illustrate what save-excursion is for. Commented Jun 28, 2021 at 2:39

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.