Here is the official definition of copy-to-buffer
in Emacs 26.1 (the doc string part is omitted for brevity)
(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)))))
I don't understand why it's necessary to use save-excursion
in conjoint with with-current-buffer
? As far as I'm concerned, the former is equivalent to a combination of save-excursion
and set-buffer
, so the current point would be saved and restored anyway.
In other words, I'm wondering why don't the Emacs implementers use the following piece of code, which after some experiments appears to be functionally identical to the current one.
(defun my-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)
(insert-buffer-substring oldbuf start end))))