GNU bug report logs - #51862
after-focus-change-function: hard to understand documentation

Previous Next

Package: emacs;

Reported by: Stefan Kangas <stefan <at> marxist.se>

Date: Mon, 15 Nov 2021 11:01:01 UTC

Severity: normal

Fixed in version 29.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 51862 in the body.
You can then email your comments to 51862 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to dancol <at> dancol.org, bug-gnu-emacs <at> gnu.org:
bug#51862; Package emacs. (Mon, 15 Nov 2021 11:01:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Stefan Kangas <stefan <at> marxist.se>:
New bug report received and forwarded. Copy sent to dancol <at> dancol.org, bug-gnu-emacs <at> gnu.org. (Mon, 15 Nov 2021 11:01:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefan <at> marxist.se>
To: bug-gnu-emacs <at> gnu.org
Subject: after-focus-change-function: hard to understand documentation
Date: Mon, 15 Nov 2021 03:00:35 -0800
I have the following in my init file:

    (add-hook 'focus-out-hook #'foo-bar)

But `focus-out-hook' is obsolete in 27.1 and replaced with
`after-focus-change-function' (see commit 2f6c682061).  However, I find
it substantially harder to understand how to use the latter.

Please find below some comments based on my reading of the documentation
in (info "(elisp) Input Focus").

> -- Variable: after-focus-change-function This function is an extension
> point that code can use to receive a notification that focus has
> changed.

Why does this use the terminology "extension point"?  We don't use that
anywhere else in the ELisp manual.

Can't it be used for other things than "receiving a notification that
focus has changed"?

>     This function is called with no arguments when Emacs notices that
>     the set of focused frames may have changed.  Code wanting to do
>     something when frame focus changes should use ‘add-function’ to add
>     a function to this one, and in this added function, re-scan the set
>     of focused frames, calling ‘frame-focus-state’ to retrieve the last
>     known focus state of each frame.  Focus events are delivered
>     asynchronously, and frame input focus according to an external
>     system may not correspond to the notion of the Emacs selected
>     frame.  Multiple frames may appear to have input focus
>     simultaneously due to focus event delivery differences, the
>     presence of multiple Emacs terminals, and other factors, and code
>     should be robust in the face of this situation.

This for me comes off as pretty scary.  It sounds to me like I would
need to invest couple of hours at least into understanding all this,
when all I really want to do call a very simple function whenever focus
leaves a frame.

Could we have a simpler interface for this?  If not, could we perhaps
include some examples that would explain in a straightforward way what
one would need to do?

>     Depending on window system, focus events may also be delivered
>     repeatedly and with different focus states before settling to the
>     expected values.  Code relying on focus notifications should
>     “debounce” any user-visible updates arising from focus changes,
>     perhaps by deferring work until redisplay.

How do I "defer work until redisplay"?  Could we have an example?

>     This function may be called in arbitrary contexts, including from
>     inside ‘read-event’, so take the same care as you might when
>     writing a process filter.

Could we have some examples of what care I would need to take, or a
pointer to where that is explained?

Thanks.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#51862; Package emacs. (Tue, 16 Nov 2021 07:58:02 GMT) Full text and rfc822 format available.

Message #8 received at 51862 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Stefan Kangas <stefan <at> marxist.se>
Cc: Daniel Colascione <dancol <at> dancol.org>, 51862 <at> debbugs.gnu.org
Subject: Re: bug#51862: after-focus-change-function: hard to understand
 documentation
Date: Tue, 16 Nov 2021 08:57:20 +0100
Stefan Kangas <stefan <at> marxist.se> writes:

>> -- Variable: after-focus-change-function This function is an extension
>> point that code can use to receive a notification that focus has
>> changed.
>
> Why does this use the terminology "extension point"?  We don't use that
> anywhere else in the ELisp manual.

That's an odd way of putting it, yes.

> Could we have a simpler interface for this?  If not, could we perhaps
> include some examples that would explain in a straightforward way what
> one would need to do?

I think it would make sense to add a different mechanism here.  People
who need this want two things: The affected frame and the new status
(i.e., focus-in and focus-out).  So the function should be called with
these two arguments.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#51862; Package emacs. (Tue, 16 Nov 2021 17:47:01 GMT) Full text and rfc822 format available.

Message #11 received at 51862 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Lars Ingebrigtsen <larsi <at> gnus.org>, Daniel Colascione <dancol <at> dancol.org>
Cc: stefan <at> marxist.se, 51862 <at> debbugs.gnu.org
Subject: Re: bug#51862: after-focus-change-function: hard to understand
 documentation
Date: Tue, 16 Nov 2021 19:45:57 +0200
> From: Lars Ingebrigtsen <larsi <at> gnus.org>
> Date: Tue, 16 Nov 2021 08:57:20 +0100
> Cc: 51862 <at> debbugs.gnu.org
> 
> Stefan Kangas <stefan <at> marxist.se> writes:
> 
> >> -- Variable: after-focus-change-function This function is an extension
> >> point that code can use to receive a notification that focus has
> >> changed.
> >
> > Why does this use the terminology "extension point"?  We don't use that
> > anywhere else in the ELisp manual.
> 
> That's an odd way of putting it, yes.
> 
> > Could we have a simpler interface for this?  If not, could we perhaps
> > include some examples that would explain in a straightforward way what
> > one would need to do?
> 
> I think it would make sense to add a different mechanism here.  People
> who need this want two things: The affected frame and the new status
> (i.e., focus-in and focus-out).  So the function should be called with
> these two arguments.

Daniel, any comments?

Thanks.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#51862; Package emacs. (Tue, 20 Sep 2022 14:46:01 GMT) Full text and rfc822 format available.

Message #14 received at 51862 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Stefan Kangas <stefan <at> marxist.se>
Cc: Daniel Colascione <dancol <at> dancol.org>, 51862 <at> debbugs.gnu.org
Subject: Re: bug#51862: after-focus-change-function: hard to understand
 documentation
Date: Tue, 20 Sep 2022 16:45:18 +0200
Stefan Kangas <stefan <at> marxist.se> writes:

> This for me comes off as pretty scary.  It sounds to me like I would
> need to invest couple of hours at least into understanding all this,
> when all I really want to do call a very simple function whenever focus
> leaves a frame.

I think the reason is to be found in the doc string of the obsoleted
variables.  That is:

---
This hook is obsolete.  Despite its name, this hook may be run in
situations other than when a frame obtains input focus: for
example, we also run this hook when switching the selected frame
internally to handle certain input events (like mouse wheel
scrolling) even when the user's notion of input focus
hasn't changed.
---

So it's not reliable (any more?), and that's why the new variable is the
way it is.  Probably (but it'd be helpful if Daniel could chime in).

But looking at the code, I don't understand these caveats at all.  In
addition to the calls from xterm.el, the only place
after-focus-change-function is called is from
handle-focus-in/handle-focus-out -- which also call the old, obsolete
hooks.

So it doesn't seem to be more reliable, but just more incomprehensible.

In addition, handle-focus-in/out know which frame they got the event
for, but that's discarded.

I guess the idea here might be that since you can get more than one
focus-in event per real event, you should write your code to ensure that
you don't trigger it more than once per frame?  I.e., since this is all
apparently probabilistic, we leave the hard stuff to the user.

So I've tried to make the documentation here clearer and less scary, and
I've added a really un-scary example.




bug marked as fixed in version 29.1, send any further explanations to 51862 <at> debbugs.gnu.org and Stefan Kangas <stefan <at> marxist.se> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Tue, 20 Sep 2022 14:48:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 19 Oct 2022 11:24:09 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 190 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.