Subject: bug#32790: 27.0.50; point jumps unexpectedly after delete-window

From: Juri Linkov <[email-masked]>
Date: Thu, 08 Nov 2018 23:53:23 +0200

In bug#33258 I tried to use inhibit-select-window in
display-buffer-overriding-action, but it's better just to use
a prefix arg to determine whether the window should remain selected,
e.g. ‘C-u S-M-up’ means no-select, or ‘M-- S-M-left’ means no-select
and use the bottom egde, etc.

The only problem is that I can't decide what window to use for
window-in-direction and split-window in display-buffer-overriding-action:
an original window that was selected at the moment of ‘S-M-up’ invocation,
or a selected window that is current at the moment when
display-buffer-overriding-action is applied?  Please see
the questions in the comments of this patch:

diff --git a/lisp/windmove.el b/lisp/windmove.el
index 598e495c7a..ea4506049b 100644
--- a/lisp/windmove.el
+++ b/lisp/windmove.el
@@ -1,4 +1,4 @@
-;;; windmove.el --- directional window-selection routines
+;;; windmove.el --- directional window-selection routines  -*- lexical-binding:t -*-
 ;;
 ;; Copyright (C) 1998-2018 Free Software Foundation, Inc.
 ;;
@@ -571,6 +571,104 @@ windmove-default-keybindings
   (global-set-key (vector (append modifiers '(up)))    'windmove-up)
   (global-set-key (vector (append modifiers '(down)))  'windmove-down))
 
+;;; Directional window display
+
+(defun windmove-display-in-direction (dir &optional arg)
+  "Display the next buffer in the window at direction DIR.
+Create a new window if there is no window in that direction.
+If prefix ARG is `C-u' or `M--', restore the previously selected window.
+With no prefix ARG, or with prefix ARG equal to zero, a displayed window is
+relative to the position of point in the selected window; otherwise it is
+relative to the first edge (for positive ARG or `C-u') or the last edge
+(for negative ARG or `M--') of the selected window."
+  (interactive)
+  (let* ((command this-command)
+         (action display-buffer-overriding-action)
+         (minibuffer-depth (minibuffer-depth))
+         (selected-window (selected-window))
+         (restore-window (or (consp arg) (eq arg '-)))
+         (clearfun (make-symbol "clear-display-buffer-overriding-action"))
+         (exitfun
+          (lambda ()
+            (setq display-buffer-overriding-action action)
+            (when (and restore-window (window-live-p selected-window))
+              (select-window selected-window))
+            (remove-hook 'post-command-hook clearfun))))
+    (fset clearfun
+          (lambda ()
+            (unless (or
+		     ;; Remove the hook immediately
+		     ;; after exiting the minibuffer.
+		     (> (minibuffer-depth) minibuffer-depth)
+		     ;; But don't remove immediately after
+		     ;; adding the hook by the same command below.
+		     (eq this-command command))
+              (funcall exitfun))))
+    (add-hook 'post-command-hook clearfun)
+    (push (lambda (buffer alist)
+	    (unless (> (minibuffer-depth) minibuffer-depth)
+	      (let ((window (if (eq dir 'same-window)
+			        (selected-window) ;; or maybe `selected-window'?
+                              (window-in-direction
+                               dir selected-window nil ;; or `nil' instead of `selected-window'?
+                               (and arg (prefix-numeric-value arg))
+                               windmove-wrap-around)))
+                    (type 'reuse))
+                (unless window
+		  ;; maybe use `selected-window' in WINDOW arg of split-window?
+                  (setq window (split-window nil nil dir) type 'window))
+		(window--display-buffer buffer window type alist))))
+          display-buffer-overriding-action)
+    (message "[display-%s]" dir)))
+
+;;;###autoload
+(defun windmove-display-left (&optional arg)
+  "Display the next buffer in window to the left of the current one.
+With a prefix argument, restore the previously selected window."
+  (interactive "P")
+  (windmove-display-in-direction 'left arg))
+
+;;;###autoload
+(defun windmove-display-up (&optional arg)
+  "Display the next buffer in window above the current one.
+With a prefix argument, restore the previously selected window."
+  (interactive "P")
+  (windmove-display-in-direction 'up arg))
+
+;;;###autoload
+(defun windmove-display-right (&optional arg)
+  "Display the next buffer in window to the right of the current one.
+With a prefix argument, restore the previously selected window."
+  (interactive "P")
+  (windmove-display-in-direction 'right arg))
+
+;;;###autoload
+(defun windmove-display-down (&optional arg)
+  "Display the next buffer in window below the current one.
+With a prefix argument, restore the previously selected window."
+  (interactive "P")
+  (windmove-display-in-direction 'down arg))
+
+;;;###autoload
+(defun windmove-display-same-window (&optional arg)
+  "Display the next buffer in the same window."
+  (interactive "P")
+  (windmove-display-in-direction 'same-window arg))
+
+;;;###autoload
+(defun windmove-display-default-keybindings (&optional modifiers)
+  "Set up keybindings for directional display.
+Keybindings are of the form MODIFIERS-{left,right,up,down},
+where MODIFIERS is either a list of modifiers or a single modifier.
+Default value of MODIFIERS is `shift-meta'."
+  (interactive)
+  (unless modifiers (setq modifiers '(shift meta)))
+  (unless (listp modifiers) (setq modifiers (list modifiers)))
+  (global-set-key (vector (append modifiers '(left)))  'windmove-display-left)
+  (global-set-key (vector (append modifiers '(right))) 'windmove-display-right)
+  (global-set-key (vector (append modifiers '(up)))    'windmove-display-up)
+  (global-set-key (vector (append modifiers '(down)))  'windmove-display-down)
+  (global-set-key (vector (append modifiers '(?0)))    'windmove-display-same-window))
 
 (provide 'windmove)