Save point, mark, and current buffer; execute BODY; restore those things.
My understanding was that the restored buffer would again be visible following the evaluation of the body, but that appears not to be the case. A basic function demonstrating the behavior that I find surprising:
(defun test-excursion () (interactive) (save-excursion (find-file "just-a-test")) (insert "blah"))
On my system (using
emacs -q; running Debian version 24.5.1) interactively running this function using
M-x test-excursion I get most of the behavior I expect. A new empty file buffer is created (there is no such file on disk) named "just-a-test" and "blah" is inserted in the scratch buffer that I happened to have open at the time. However; the buffer that remains visible is "just-a-test" and not "*scratch*" which was
(current-buffer) at the time the command was invoked. No additional windows were created. It is only apparent by switching back to "*scratch*" that the string was inserted as expected.
My memory may be inaccurate on this, but I recall using
save-excursion when writing a function and observing the alternate behavior (the restored buffer restored as visible). Is there any chance that this behavior has changed or that there has been a regression in 24.5.1? I know that the definition has changed in 25. I checked on another system (also 24.5.1) and observed the same behavior as my system. Evaluation non-interactively (via
eval-last-sexp) yields the same.
save-current-buffer also yield the same.
My understanding is that each window (in the emacs sense) has a current buffer (edit: perhaps appropriately referred to simply as the "displayed buffer" and is returned by
current-buffer when the window has focus) which necessarily is visible and there are no other conceptions of a "visible buffer". If
save-excursion restores the current buffer then shouldn't it be visible?
I've been scratching my head at this one for a while. I hope someone can help me out.