Subject: bug#47300: delete-window to select window with same position

From: martin rudalics <[email-masked]>
Date: Sat, 22 May 2021 10:05:20 +0200

> No.  You probably have to save the old position of the deleted window's
 > point in frame coordinates and have the function that selects the new
 > window use the window that is now at that frame position.

Just to explain what I meant above look at the attached patch.  I think
we should then move the `get-mru-window' call to `delete-window' too and
combine them all with the 'no-other-window' check so we never select a
window with NORECORD nil twice here.

martin
diff --git a/lisp/window.el b/lisp/window.el
index a403daf10b..32742f07c5 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -4800,6 +4800,8 @@ window--in-subtree-p
 		(throw 'done t)
 	      (setq parent (window-parent parent))))))))

+(defvar delete-window-use-posn-at-point nil)
+
 (defun delete-window (&optional window)
   "Delete WINDOW.
 WINDOW must be a valid window and defaults to the selected one.
@@ -4822,7 +4824,7 @@ delete-window
   (let* ((frame (window-frame window))
 	 (function (window-parameter window 'delete-window))
 	 (parent (window-parent window))
-	 atom-root)
+	 atom-root posn-at-point window-at-posn-at-point)
     (window--check frame)
     (catch 'done
       ;; Handle window parameters.
@@ -4871,10 +4873,20 @@ delete-window
 	 (t
 	  ;; Can't do without resizing fixed-size windows.
 	  (window--resize-siblings window (- size) horizontal t)))
+        (when delete-window-use-posn-at-point
+          ;; Remember WINDOW's position at point.
+          (setq posn-at-point (posn-at-point nil window)))
 	;; Actually delete WINDOW.
-	(delete-window-internal window)
+        (delete-window-internal window)
 	(window--pixel-to-total frame horizontal)
-	(when (and frame-selected
+        (when (and posn-at-point
+                   (setq window-at-posn-at-point
+                              (window-at (car (nth 6 posn-at-point))
+                                         (cdr (nth 6 posn-at-point))
+                                         frame)))
+          ;; Select window at WINDOW's position at point.
+          (select-window window-at-posn-at-point))
+        (when (and frame-selected
 		   (window-parameter
 		    (frame-selected-window frame) 'no-other-window))
 	  ;; `delete-window-internal' has selected a window that should