Function: with-selected-window

with-selected-window is a macro defined in subr.el.gz.

Signature

(with-selected-window WINDOW &rest BODY)

Documentation

Execute the forms in BODY with WINDOW as the 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 of each frame. It does not change the order of recently selected windows. 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. If the selected window is no longer live, then whatever window is selected at the end of BODY remains selected.

This macro uses save-current-buffer to save and restore the current buffer, since otherwise its normal operation could potentially make a different buffer current. It does not alter the buffer list ordering.

View in manual

Probably introduced at or before Emacs version 22.1.

Aliases

erc-with-selected-window (obsolete since 28.1)

Source Code

;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defmacro with-selected-window (window &rest body)
  "Execute the forms in BODY with WINDOW as the 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 of each frame.  It does not change the order of
recently selected windows.  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.  If the selected window is no
longer live, then whatever window is selected at the end of BODY
remains selected.

This macro uses `save-current-buffer' to save and restore the
current buffer, since otherwise its normal operation could
potentially make a different buffer current.  It does not alter
the buffer list ordering."
  (declare (indent 1) (debug t))
  `(let ((save-selected-window--state
          (internal--before-with-selected-window ,window)))
     (save-current-buffer
       (unwind-protect
           (progn (select-window (car save-selected-window--state) 'norecord)
		  ,@body)
         (internal--after-with-selected-window save-selected-window--state)))))