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

From: Juri Linkov <[email-masked]>
Date: Tue, 30 Oct 2018 23:42:07 +0200

>>> Moreover, using 'post-command-hook' would automatically fix any
>>> problems when quitting an S-M-up action.  I see no way to catch
>>> quitting via a state change of windows.
>>
>> What do you mean by quitting an S-M-up action?  minibuffer-exit?
>
> Let's say anything C-g does.  I'm not sure whether we can catch that
> always via 'minibuffer-exit-hook' though.  But it should not harm to
> remove the overriding action in that case as well.

Ok, after a small change it handles also C-g:

(defun display-buffer-directionally ()
  "Specify in which direction the buffer should be displayed.
Arrows show the direction.  Mod-0 forces to display in the same window."
  (interactive)
  (let* ((dir (event-basic-type (aref (this-command-keys) 0)))
         (win (if (eq dir ?0)
		  (selected-window)
		(or (window-in-direction dir)
		    (split-window nil nil dir)))))
    (let ((hook (list 'lambda)))
      (setcdr hook `(()
                     (unless (or
			      ;; Remove the hook immediately
			      ;; after exiting the minibuffer.
			      (> (minibuffer-depth) 0)
			      ;; But don't remove immediately after
			      ;; adding the hook by the same command.
			      (eq this-command ',this-command))
                       (setq display-buffer-overriding-action
                             ',display-buffer-overriding-action)
                       (remove-hook 'post-command-hook ',hook))))
      (add-hook 'post-command-hook hook))
    (setq display-buffer-overriding-action
          `((lambda (buffer alist)
              (unless (> (minibuffer-depth) 0)
		(window--display-buffer buffer ,win 'reuse alist)))))))