4

Consider:

(defun test()
  (interactive)
  (save-selected-window
    (switch-to-buffer-other-window "Test buffer.")))

When test is run interactively while in a buffer visiting a file, the buffer is successfully restored. Why doesn't the following do the job?

(defun test()
  (interactive)
  (save-current-buffer
    (switch-to-buffer-other-window "Test buffer.")))

2 Answers 2

2

Actually, it does successfully restore the buffer that was current, as the newly current buffer. Try this:

(defun test()
  (interactive)
  (save-current-buffer
    (switch-to-buffer-other-window "Test buffer."))
  (message "current: %S" (current-buffer)))

current-buffer is not related to any particular window. The buffer that is displayed in the (newly) selected window is buffer Test buffer.. But the current buffer is the original buffer - the one that was saved and restored by save-current-buffer.

Don't confuse the current buffer with a buffer that is currently displayed. You'll see the same behavior if you use switch-to-buffer instead of switch-to-buffer-other-window, in your test function. The buffer that is displayed is Test buffer.. But the current-buffer is the saved-and-restored buffer that was originally current.

See the Elisp manual, node Current Buffer.

3
  • Does this boil down to the difference between selecting a buffer and switching to one? Sep 12, 2017 at 18:25
  • Yes, kind of. Switching to a buffer displays it in a given window, and it selects that window. If you use switch-to-buffer in your function, you will still see the message saying that the original buffer is current after the buffer-switching and the save-current-buffer. But after that, if you evaluate M-: (current-buffer) with that window selected, it will tell you that the value is Test buffer., because that is the buffer that is current in the selected window.
    – Drew
    Sep 12, 2017 at 21:47
  • IOW, the original buffer was current when your test function finished, but after that, if you check the value of current-buffer anew, in that window, the value is Test buffer.. So it is about the difference between setting the current buffer (we don't say "select" a buffer) and displaying a buffer. When you select a window and check the current buffer, the buffer displayed in that window is current.
    – Drew
    Sep 12, 2017 at 21:48
1

The save-current-buffer macro saves only the current buffer and restores it, unlike the save-selected-window macro, which saves selected windows in each frame.

The function switch-to-buffer-other-window doesn't affect the currently selected buffer, but the one in the next window, hence save-current-buffer doesn't restore the buffer selected in it.

2
  • The documentation of save-current-buffer says: "Record which buffer is current; execute BODY; make that buffer current." This implies to me that the original buffer should become current again. I don't understand from your answer why this does not happen. Sep 12, 2017 at 16:56
  • That applies to the current buffer (which is in the window that's active.)
    – user12563
    Sep 12, 2017 at 16:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.