Function: save-selected-window

save-selected-window is a macro defined in window.el.gz.

Signature

(save-selected-window &rest BODY)

Documentation

Execute BODY, then select the previously selected window.

The value returned is the value of the last form in BODY.

This macro saves and restores the selected window, as well as the selected window in each frame. If the previously selected window is no longer live, then whatever window is selected at the end of BODY remains selected. If the previously selected window of some frame is no longer live at the end of BODY, that frame's selected window is left alone.

This macro saves and restores the current buffer, since otherwise its normal operation could make a different buffer current. The order of recently selected windows and the buffer list ordering are not altered by this macro (unless they are altered in BODY).

View in manual

Probably introduced at or before Emacs version 22.1.

Source Code

;; Defined in /usr/src/emacs/lisp/window.el.gz
(defmacro save-selected-window (&rest body)
  "Execute BODY, then select the previously selected window.
The value returned is the value of the last form in BODY.

This macro saves and restores the selected window, as well as the
selected window in each frame.  If the previously selected window
is no longer live, then whatever window is selected at the end of
BODY remains selected.  If the previously selected window of some
frame is no longer live at the end of BODY, that frame's selected
window is left alone.

This macro saves and restores the current buffer, since otherwise
its normal operation could make a different buffer current.  The
order of recently selected windows and the buffer list ordering
are not altered by this macro (unless they are altered in BODY)."
  (declare (indent 0) (debug t))
  `(let ((save-selected-window--state (internal--before-save-selected-window)))
     (save-current-buffer
       (unwind-protect
	   (progn ,@body)
         (internal--after-save-selected-window save-selected-window--state)))))