Subject: delete-window to select window with same position

From: Juri Linkov <[email-masked]>
Date: Sun, 21 Mar 2021 22:31:27 +0200

People still have trouble with unpredictable behavior of 'C-x 0'
that selects an unexpected window after the current window is deleted.

This is a recent example:
https://old.reddit.com/r/emacs/comments/m8omt0/how_can_deletewindow_cx_0_be_configured_to_stay/

The previous failed attempt to fix this was in bug#32790.

I still don't understand the current logic what window is selected
after deleting the selected window with 'C-x 0'.

But maybe this could be customizable?  Tried to write an advice:

(advice-add 'delete-window :around
            (lambda (orig-fun &optional window)
              (if window
                  ;; Non-interactive call
                  (funcall orig-fun window)
                ;; Interactive call
                (let* ((pos (window-absolute-pixel-position))
                       (x (when pos (/ (car pos) (frame-char-width))))
                       (y (when pos (/ (cdr pos) (frame-char-height)))))
                  (funcall orig-fun window)
                  ;; Select window that takes space from the deleted window
                  (when (and x y)
                    (select-window (window-at x y))))))
            '((name . delete-window-keep-pos)))

Maybe something like this could be adapted to delete-window
by adding an optional interactive argument keep-pos:

diff --git a/lisp/window.el b/lisp/window.el
index f27631bb86..e2029406c7 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -4177,7 +4177,7 @@ window--before-delete-windows
                (push (list buf start-m pos-m) prev-buf)
                (set-window-prev-buffers win prev-buf)))))))
 
-(defun delete-window (&optional window)
+(defun delete-window (&optional window keep-pos)
   "Delete WINDOW.
 WINDOW must be a valid window and defaults to the selected one.
 Return nil.
@@ -4194,13 +4194,16 @@ delete-window
 argument.  Signal an error if WINDOW is either the only window on
 its frame, the last non-side window, or part of an atomic window
 that is its frame's root window."
-  (interactive)
+  (interactive (list nil t))
   (when switch-to-buffer-preserve-window-point
     (window--before-delete-windows))
+  (setq keep-pos (and keep-pos (not window) (window-absolute-pixel-position)))
   (setq window (window-normalize-window window))
   (let* ((frame (window-frame window))
 	 (function (window-parameter window 'delete-window))
 	 (parent (window-parent window))
+	 (keep-pos-x (when keep-pos (/ (car keep-pos) (frame-char-width))))
+	 (keep-pos-y (when keep-pos (/ (cdr keep-pos) (frame-char-height))))
 	 atom-root)
     (window--check frame)
     (catch 'done
@@ -4259,6 +4262,8 @@ delete-window
 	  ;; `delete-window-internal' has selected a window that should
 	  ;; not be selected, fix this here.
 	  (other-window -1 frame))
+	(when (and keep-pos-x keep-pos-y (window-at keep-pos-x keep-pos-y))
+	  (select-window (window-at keep-pos-x keep-pos-y)))
 	(window--check frame)
 	;; Always return nil.
 	nil))))