unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* select-frame-set-input-focus operates on wrong frame
@ 2024-10-14 15:01 Óscar Fuentes
  2024-10-15  1:25 ` Óscar Fuentes
  2024-10-19 16:43 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 4+ messages in thread
From: Óscar Fuentes @ 2024-10-14 15:01 UTC (permalink / raw)
  To: help-gnu-emacs

I have a frame named "pdf" to view files opened via emacsclient. This
code shows those files on the "pdf" frame (if it exists) when
`emacsclient -n somefile' is invoked:


(defun ofv-display-buffer-pop-up-frame (buffer &rest alist)
  (unless (catch 'out
	    (dolist (frame (frame-list))
	      (when (and (equal (frame-parameter frame 'display)
				(frame-parameter nil 'display))
			 (string= "pdf" (frame-parameter frame 'name)))
		(with-selected-frame frame
		  (select-frame-set-input-focus frame)
		  (display-buffer-full-frame buffer '())
		  (throw 'out t)))))
    (display-buffer-full-frame buffer alist)))

(setq server-window 'ofv-display-buffer-pop-up-frame)


This works except select-frame-set-input-focus raises and sets focus to
the frame that had the focus before emacsclient was invoked.

I guess that the code that processes the emacsclient request in Emacs
sets the focus after the function contained in server-window is
invoked. How can I avoid this?




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: select-frame-set-input-focus operates on wrong frame
  2024-10-14 15:01 select-frame-set-input-focus operates on wrong frame Óscar Fuentes
@ 2024-10-15  1:25 ` Óscar Fuentes
  2024-10-19 16:43 ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 4+ messages in thread
From: Óscar Fuentes @ 2024-10-15  1:25 UTC (permalink / raw)
  To: help-gnu-emacs

Óscar Fuentes <ofv@wanadoo.es> writes:

> I have a frame named "pdf" to view files opened via emacsclient. This
> code shows those files on the "pdf" frame (if it exists) when
> `emacsclient -n somefile' is invoked:
>
>
> (defun ofv-display-buffer-pop-up-frame (buffer &rest alist)
>   (unless (catch 'out
> 	    (dolist (frame (frame-list))
> 	      (when (and (equal (frame-parameter frame 'display)
> 				(frame-parameter nil 'display))
> 			 (string= "pdf" (frame-parameter frame 'name)))
> 		(with-selected-frame frame
> 		  (select-frame-set-input-focus frame)
> 		  (display-buffer-full-frame buffer '())
> 		  (throw 'out t)))))
>     (display-buffer-full-frame buffer alist)))
>
> (setq server-window 'ofv-display-buffer-pop-up-frame)
>
>
> This works except select-frame-set-input-focus raises and sets focus to
> the frame that had the focus before emacsclient was invoked.
>
> I guess that the code that processes the emacsclient request in Emacs
> sets the focus after the function contained in server-window is
> invoked. How can I avoid this?

The problem consists on the semantics of with-selected-frame: it
restores the selected frame, so changing focus to the other frame inside
with-selected-frame is futile.

This version of the code works:

(defun ofv-display-buffer-pop-up-frame (buffer &rest alist)
  (let ((f (cl-find-if (lambda (frame)
			 (and (equal (frame-parameter frame 'display)
				     (frame-parameter nil 'display))
			      (string= "pdf" (frame-parameter frame 'name))))
		       (frame-list))))
    (when f
      (select-frame-set-input-focus f))
    (display-buffer-full-frame buffer '())))

(setq server-window 'ofv-display-buffer-pop-up-frame)




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: select-frame-set-input-focus operates on wrong frame
  2024-10-14 15:01 select-frame-set-input-focus operates on wrong frame Óscar Fuentes
  2024-10-15  1:25 ` Óscar Fuentes
@ 2024-10-19 16:43 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2024-10-20 12:51   ` Óscar Fuentes
  1 sibling, 1 reply; 4+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-10-19 16:43 UTC (permalink / raw)
  To: help-gnu-emacs

> (defun ofv-display-buffer-pop-up-frame (buffer &rest alist)
>   (unless (catch 'out
> 	    (dolist (frame (frame-list))
> 	      (when (and (equal (frame-parameter frame 'display)
> 				(frame-parameter nil 'display))
> 			 (string= "pdf" (frame-parameter frame 'name)))
> 		(with-selected-frame frame
> 		  (select-frame-set-input-focus frame)
> 		  (display-buffer-full-frame buffer '())
> 		  (throw 'out t)))))
>     (display-buffer-full-frame buffer alist)))

As you discovered, using something like `select-frame-set-input-focus`
from within a `with-selected-frame` is not a good idea.
I don't know why you did that, because it seems contradictory, I suspect
it was an oversight.

Regarding the focus itself (which is a subtly different concept than the
selected frame), the situation is very messy and what happens in which
case can also depend on your window-manager (or the part of your OS
which takes on that responsibility) as well as timing.  🙁

> (setq server-window 'ofv-display-buffer-pop-up-frame)

I recommend #' here to clarify that this is intended to set the var to
a function.


        Stefan




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: select-frame-set-input-focus operates on wrong frame
  2024-10-19 16:43 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2024-10-20 12:51   ` Óscar Fuentes
  0 siblings, 0 replies; 4+ messages in thread
From: Óscar Fuentes @ 2024-10-20 12:51 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> As you discovered, using something like `select-frame-set-input-focus`
> from within a `with-selected-frame` is not a good idea.
> I don't know why you did that, because it seems contradictory, I suspect
> it was an oversight.

Yeah, somehow I was under the impression that
select-frame-set-input-focus would be two orthogonal things: set the
selected frame *and* set focus, then `with-selected-frame' would reverse
the change of selected frame, but not the focus.

> Regarding the focus itself (which is a subtly different concept than the
> selected frame), the situation is very messy and what happens in which
> case can also depend on your window-manager (or the part of your OS
> which takes on that responsibility) as well as timing.  🙁

Indeed. On KDE using the default focus settings things work so far.

>> (setq server-window 'ofv-display-buffer-pop-up-frame)
>
> I recommend #' here to clarify that this is intended to set the var to
> a function.

Ok, thanks.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-10-20 12:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-14 15:01 select-frame-set-input-focus operates on wrong frame Óscar Fuentes
2024-10-15  1:25 ` Óscar Fuentes
2024-10-19 16:43 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-10-20 12:51   ` Óscar Fuentes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).