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

From: Juri Linkov <[email-masked]>
Date: Mon, 31 May 2021 23:46:49 +0300

> +	 (when (and (>= x (nth 0 edges)) (< x (nth 2 edges))
> +		    (>= y (nth 1 edges)) (< y (nth 3 edges)))
>
> We have to mention the "<" somewhere.  For two side-by-side windows with
> the right window's point at 1, we obviously must avoid the left window's
> right edge because the X, Y coordinates set by pos_visible_p are the top
> left edge of the character box at that position, i.e., 0, 0.
>
> But OTOH the "<" leaves the right edge of a frame's rightmost window
> unhandled and someone might use that function for other purposes.

I changed <= to < to fix only Y coordinates for top/bottom windows,
but maybe X coordinates should still use <= ?

> It would be a tad more elegant to just save `posn-at-point' here ...
>
> +              (setq posn-at-point-x (+ (nth 0 edges) (car posn-at-point))
> +                    posn-at-point-y (+ (nth 1 edges) (cdr posn-at-point))))))
>  	;; Actually delete WINDOW.
>  	(delete-window-internal window)
>  	(window--pixel-to-total frame horizontal)
> -	(when (and frame-selected
>
> ... and delay extracting its car and cdr until here.
>
> +        (when (and posn-at-point-x posn-at-point-y
> +		   (setq window-at-posn-at-point
> +			 (window-at-pos posn-at-point-x posn-at-point-y frame)))

Please note that the crucial difference is that now it uses
frame relative positions rather than window relative ones
with '(window-edges window nil nil t)' of the window to be deleted,
and window coordinates added to X and Y of posn-at-point.

> I'd still urge you to transfer the `get-mru-window' call here too, that
> is, the entire
>
> 	  /* Now look whether `get-mru-window' gets us something.  */
> 	  mru_window = call1 (Qget_mru_window, frame);
> 	  if (WINDOW_LIVE_P (mru_window)
> 	      && EQ (XWINDOW (mru_window)->frame, frame))
> 	    new_selected_window = mru_window;
>
> 	  /* If all ended up well, we now promote the mru window.  */
> 	  if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
> 	    Fselect_window (new_selected_window, Qnil);
> 	  else
> 	    fset_selected_window (f, new_selected_window);
>
> block, and to include the
>
>         (when (and frame-selected
> 		   (window-parameter
> 		    (frame-selected-window frame) 'no-other-window))
> 	  ;; `delete-window-internal' has selected a window that should
> 	  ;; not be selected, fix this here.
> 	  (other-window -1 frame))
>
> so we can avoid selecting a new window up to four times when deleting a
> window (the one remaining call in `delete-window-internal' does not
> record the window and has to stay there).  It's simply not nice to do
> that.

1. `get-mru-window' could be one possible choice of the new option.
2. `use-posn-at-point' could be another choice.
3. Selecting the first window could remain in `delete-window-internal'.
4. But what to do with this code block with '(other-window -1 frame)'?
   Should this be simply deleted?