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

From: Juri Linkov <[email-masked]>
Date: Mon, 05 Nov 2018 00:21:14 +0200

> I had that in mind as a side-effect.  But we should really get rid of
> this split-first-decide-what-to-put-there-afterwards approach first.

I agree split-first-decide-what-to-put-there-afterwards is too ugly.

So following your suggestion I reimplemented this feature in windmove.el
because it fits its description "directional window-selection routines":

diff --git a/lisp/windmove.el b/lisp/windmove.el
index 42e10b591f..69f2a9665a 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.
 ;;
@@ -551,6 +571,86 @@ 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)
+  "Display the next buffer in the window at direction DIR.
+Create a new window if there is no window in that direction."
+  (interactive)
+  (let* ((command this-command)
+         (depth (minibuffer-depth))
+         (action display-buffer-overriding-action)
+         (clearfun (make-symbol "clear-display-buffer-overriding-action"))
+         (exitfun
+          (lambda ()
+            (setq display-buffer-overriding-action action)
+            (remove-hook 'post-command-hook clearfun))))
+    (fset clearfun
+          (lambda ()
+            (unless (or
+		     ;; Remove the hook immediately
+		     ;; after exiting the minibuffer.
+		     (> (minibuffer-depth) depth)
+		     ;; But don't remove immediately after
+		     ;; adding the hook by the same command.
+		     (eq this-command command))
+              (funcall exitfun))))
+    (add-hook 'post-command-hook clearfun)
+    (push (lambda (buffer alist)
+	    (unless (> (minibuffer-depth) depth)
+	      (let ((win (if (eq dir 'same-window)
+			     (selected-window)
+			   (or (window-in-direction dir)
+			       (split-window nil nil dir)))))
+		(window--display-buffer
+		 buffer win (if (eq dir 'same-window) 'reuse 'window) 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."
+  (interactive "P")
+  (windmove-display-in-direction 'left))
+
+;;;###autoload
+(defun windmove-display-up (&optional _arg)
+  "Display the next buffer in window above the current one."
+  (interactive "P")
+  (windmove-display-in-direction 'up))
+
+;;;###autoload
+(defun windmove-display-right (&optional _arg)
+  "Display the next buffer in window to the right of the current one."
+  (interactive "P")
+  (windmove-display-in-direction 'right))
+
+;;;###autoload
+(defun windmove-display-down (&optional _arg)
+  "Display the next buffer in window below the current one."
+  (interactive "P")
+  (windmove-display-in-direction 'down))
+
+;;;###autoload
+(defun windmove-display-same-window (&optional _arg)
+  "Display the next buffer in window below the current one."
+  (interactive "P")
+  (windmove-display-in-direction 'same-window))
+
+;;;###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)