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?

Drew
  • 29,895
  • 7
  • 74
  • 104
Solaire
  • 21
  • 7
  • 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. – Solaire Jun 28 '21 at 00: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. – phils Jun 28 '21 at 01:28
  • 2
    Note also that character positions start from 1, not 0. – phils Jun 28 '21 at 01: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. – Drew Jun 28 '21 at 02:39

0 Answers0