Subject: bug#745: pop-to-buffer, frames, and input focus

From: martin rudalics <[email-masked]>
Date: Sat, 06 Sep 2008 13:56:14 +0200

I'm currently using

(defun pop-to-buffer (buffer-or-name &optional other-window norecord)
   "Select buffer BUFFER-OR-NAME in some window, preferably a different one.
...."
   (let ((buffer
          ;; FIXME: This behavior is carried over from the previous C version
          ;; of pop-to-buffer, but really we should use just
          ;; `get-buffer' here.
          (if (null buffer-or-name) (other-buffer (current-buffer))
            (or (get-buffer buffer-or-name)
                (let ((buf (get-buffer-create buffer-or-name)))
                  (set-buffer-major-mode buf)
                  buf))))
	(old-window (selected-window))
	(old-frame (selected-frame))
	new-window new-frame)
     (set-buffer buffer)
     (setq new-window (display-buffer buffer other-window) norecord)
     (unless (eq new-window old-window)
       ;; `display-buffer' has chosen another window.
       (setq new-frame (window-frame new-window))
       (unless (eq new-frame old-frame)
	;; `display-buffer' has chosen another frame, make sure it gets
	;; input focus and is risen.
	(select-frame-set-input-focus new-frame))
       ;; Make sure the window chosen by `display-buffer' gets selected.
       (select-window new-window))
     buffer))

so `select-frame-set-input-focus' gets called iff `display-buffer' did
choose another frame.  I noticed that I cannot use
`select-frame-set-input-focus' unconditionally for the following reason:
On my Emacs `focus-follows-mouse' is t so the final check in
`select-frame-set-input-focus'

     (when focus-follows-mouse
       (set-mouse-position (selected-frame) (1- (frame-width)) 0)))

_always_ succeeds and sends my mouse cursor to the upper right corner of
the corresponding frame.  This is annoying whenever I click, for
example, on a button in a help buffer to follow a link, causing the
window returned by `display-buffer' show up on the already _selected_
frame.  Since I additionally have `mouse-autoselect-window' non-nil, an
unrelated window on top of the selected frame may get selected too.

One annyoing aspect of the rigid use of `set-mouse-position' in
`select-frame-set-input-focus' obviously remains, namely that with
`mouse-autoselect-window' non-nil `other-frame' (and related functions)
may change the selected window on the other frame :-( This has to be
fixed independently.

Please check whether `pop-to-buffer' as defined above fits your needs.
If so, and no one else protests, I'll install this next week.

martin