Function: replace-buffer-in-windows

replace-buffer-in-windows is an interactive and byte-compiled function defined in window.el.gz.

Signature

(replace-buffer-in-windows &optional BUFFER-OR-NAME)

Documentation

Replace BUFFER-OR-NAME with some other buffer in all windows showing it.

BUFFER-OR-NAME may be a buffer or the name of an existing buffer and defaults to the current buffer.

With the exception of side windows, when a window showing BUFFER-OR-NAME is dedicated, that window is deleted. If that window is the only window on its frame, the frame is deleted too when there are other frames left. If there are no other frames left, some other buffer is displayed in that window.

This function removes the buffer denoted by BUFFER-OR-NAME from all window-local buffer lists.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/window.el.gz
(defun replace-buffer-in-windows (&optional buffer-or-name)
  "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
BUFFER-OR-NAME may be a buffer or the name of an existing buffer
and defaults to the current buffer.

With the exception of side windows, when a window showing BUFFER-OR-NAME
is dedicated, that window is deleted.  If that window is the only window
on its frame, the frame is deleted too when there are other frames left.
If there are no other frames left, some other buffer is displayed in that
window.

This function removes the buffer denoted by BUFFER-OR-NAME from
all window-local buffer lists."
  (interactive "bBuffer to replace: ")
  (let ((buffer (window-normalize-buffer buffer-or-name)))
    (dolist (window (window-list-1 nil nil t))
      (if (eq (window-buffer window) buffer)
          ;; Delete a dedicated window unless it is a side window.
          (let ((dedicated-side (eq (window-dedicated-p window) 'side)))
            (when (or dedicated-side (not (window--delete window t t)))
              ;; Switch to another buffer in that window.
              (set-window-dedicated-p window nil)
              (if (switch-to-prev-buffer window 'kill)
                  (and dedicated-side (set-window-dedicated-p window 'side))
                (window--delete window nil 'kill))))
	;; Unrecord BUFFER in WINDOW.
	(unrecord-window-buffer window buffer)))))