Search code examples
emacswindowbuffer

Deleting windows displaying buffer


I would like to modify this function to detach a buffer to a new frame. And deleting any window displaying the buffer in the old frame.

(defun detach-window ()
  "Sends the selected window to a new frame."

  (interactive)

  (let* ( (window (selected-window))
          (buf (window-buffer window))
          (frame (make-frame)) )

    (select-frame frame)
    (switch-to-buffer buf)
    (delete-window window)))

Solution

  • (defun foo ()
      "Tear off window and delete others showing buffer.
    Delete all windows from selected frame that show buffer of
    selected window, and show the buffer in a new frame."
      (interactive)
      (let ((buf  (window-buffer (selected-window)))
            win)
        ;; If all windows in selected frame show BUF, then handle error
        ;; "Attempt to delete minibuffer or sole ordinary window", by
        ;; replacing BUF with`other-buffer` in the last window showing BUF.
        (condition-case nil
            (while (setq win  (get-buffer-window buf)) (delete-window win))
          (error (switch-to-buffer (other-buffer buf))))
        (switch-to-buffer-other-frame buf)))