unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#47300: delete-window to select window with same position
@ 2021-03-21 20:31 Juri Linkov
  2021-05-18 14:49 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 24+ messages in thread
From: Juri Linkov @ 2021-03-21 20:31 UTC (permalink / raw)
  To: 47300

[-- Attachment #1: Type: text/plain, Size: 1311 bytes --]

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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: delete-window-keep-pos.patch --]
[-- Type: text/x-diff, Size: 1635 bytes --]

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))))

^ permalink raw reply related	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2021-06-11 17:06 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-21 20:31 bug#47300: delete-window to select window with same position Juri Linkov
2021-05-18 14:49 ` Lars Ingebrigtsen
2021-05-18 16:00   ` martin rudalics
2021-05-18 20:17     ` Juri Linkov
2021-05-19  7:42       ` martin rudalics
2021-05-19 16:07         ` Juri Linkov
2021-05-19 17:41           ` martin rudalics
2021-05-22  8:05             ` martin rudalics
2021-05-22 21:25               ` Juri Linkov
2021-05-23  8:43                 ` martin rudalics
2021-05-25  6:50                 ` martin rudalics
2021-05-26 21:29                   ` Juri Linkov
2021-05-27 15:20                     ` martin rudalics
2021-05-31 20:46                       ` Juri Linkov
2021-06-02  9:08                         ` martin rudalics
2021-06-03 21:20                           ` Juri Linkov
2021-06-04  9:19                             ` martin rudalics
2021-06-04 16:29                               ` Juri Linkov
2021-06-06  7:43                                 ` martin rudalics
2021-06-06 20:47                                   ` Juri Linkov
2021-06-07  7:35                                     ` martin rudalics
2021-06-07 21:00                                       ` Juri Linkov
2021-06-10  7:44                                         ` martin rudalics
2021-06-11 17:06                                           ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).