1
votes

I'm doing the ELISP - Tutorial right now and am unsure about the meaning of "restore" in the standard elisp function save-excursion (or equally save-current-buffer). It's probably just cause I'm no native English speaker and dont get how strong or weak "restore" is.

Let's say current buffer is A and i do in save-excursion enviroment a operation in which A is altered, for example a string inserted, I'd expect save excursion to restore A, so undo the changes in that operation.

The actual problem I don't understand is the example from copy-to-buffer:

(defun copy-to-buffer (buffer start end)
  (interactive "BCopy to buffer: \nr")
   (let ((oldbuf (current-buffer)))
    (with-current-buffer (get-buffer-create buffer)
      (barf-if-buffer-read-only)
      (erase-buffer)
      (save-excursion
        (insert-buffer-substring oldbuf start end)))))

The last save-excursion works in the frame of with-current-buffer, which made the buffer given by the user current and inserts a string there. If it restores the buffer it would undo the insertion. Obviously it does not, but what does "restore the buffer" then mean ?

1
The documentation for save-excursion is fairly explicit (Ctrl-h f save-excursion): "Save point, mark, and current buffer; execute BODY; restore those things." - tripleee

1 Answers

1
votes

Here's a simple function to illustrate what saving the buffer means:

(defun foo ()
  (interactive)
  (insert "foo")
  (save-excursion
    (set-buffer "*scratch*")
    (insert "bar")))

Call this from a buffer that's not *scratch. It will insert in current buffer, switch to *scratch*, insert there. And here's the thing: save-excursion restores buffer by switching away from *scratch*, so you're back in the buffer where you started.