GNU bug report logs - #79776
[patch] Add dark-mode-toggle-hook

Previous Next

Package: emacs;

Reported by: Ahmed Khanzada <me <at> enzu.ru>

Date: Thu, 6 Nov 2025 06:22:02 UTC

Severity: normal

Tags: patch

To reply to this bug, email your comments to 79776 AT debbugs.gnu.org.

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

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


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#79776; Package emacs. (Thu, 06 Nov 2025 06:22:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ahmed Khanzada <me <at> enzu.ru>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Thu, 06 Nov 2025 06:22:02 GMT) Full text and rfc822 format available.

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

From: Ahmed Khanzada <me <at> enzu.ru>
To: bug-gnu-emacs <at> gnu.org
Subject: [patch] Add dark-mode-toggle-hook
Date: Wed, 5 Nov 2025 22:21:20 -0800
[Message part 1 (text/plain, inline)]
After I told the community about my PGTK dark mode work, I had requests
to have a hook that they could use.

This patch adds that hook. I also tried adding it to the Win32 code, but
I cannot test this as I do not have a Win32 dev environment.

Lastly, I was confused how I should name an abnormal hook, as the
document suggests that sometimes they won't have -hook in the name.
Guidance here would be appreciated.
[0001-Add-abnormal-dark-mode-toggle-hook.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79776; Package emacs. (Thu, 06 Nov 2025 08:17:02 GMT) Full text and rfc822 format available.

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

From: Eli Zaretskii <eliz <at> gnu.org>
To: Ahmed Khanzada <me <at> enzu.ru>
Cc: 79776 <at> debbugs.gnu.org
Subject: Re: bug#79776: [patch] Add dark-mode-toggle-hook
Date: Thu, 06 Nov 2025 10:15:52 +0200
> Date: Wed, 5 Nov 2025 22:21:20 -0800
> From: Ahmed Khanzada <me <at> enzu.ru>
> 
> After I told the community about my PGTK dark mode work, I had requests
> to have a hook that they could use.
> 
> This patch adds that hook. I also tried adding it to the Win32 code, but
> I cannot test this as I do not have a Win32 dev environment.

We cannot call Lisp from that place, see below.

> Lastly, I was confused how I should name an abnormal hook, as the
> document suggests that sometimes they won't have -hook in the name.
> Guidance here would be appreciated.

From the ELisp manual:

     If the hook variable's name does not end with ‘-hook’, that indicates
  it is probably an “abnormal hook”.  These differ from normal hooks in
  two ways: they can be called with one or more arguments, and their
  return values can be used in some way.  The hook's documentation says
  how the functions are called and how their return values are used.  Any
  functions added to an abnormal hook must follow the hook's calling
  convention.  By convention, abnormal hook names end in ‘-functions’.

Note the last sentence.

> +---
> +** emacs-dark-mode-toggle-hook called when dark mode is toggled.
> +When dark mode is toggled on PGTK or Win32 builds,
> +emacs-dark-mode-toggle-hook is called.

Please quote symbols in NEWS 'like this'.

Also, GNU coding standards frown on using "Win32"; we use "MS-Windows"
instead.

> --- a/src/w32fns.c
> +++ b/src/w32fns.c
> @@ -2440,6 +2440,15 @@ w32_applytheme (HWND hwnd)
>  				    &w32_darkmode, sizeof (w32_darkmode));
>  	}
>      }
> +
> +   Lisp_Object hook = intern ("dark-mode-toggle-hook");
> +   if (!NILP (Fboundp (hook)))
> +     {
> +       Lisp_Object args[2];
> +       args[0] = hook;
> +       args[1] = w32_darkmode ? Qt : Qnil;
> +       Frun_hook_with_args (2, args);
> +     }

This cannot work in the w32 build, because w32_applytheme is called
from a separate thread, which handles Windows GUI system messages.  We
cannot call Lisp from that thread.  What is needed is to send a
message to the main (a.k.a. "Lisp") thread, using my_post_msg, and
then handle that message in w32_read_socket (which runs in the main
thread).  You will probably need to invent a new WM_EMACS_* message
for this purpose.

Let me know if you need further help with this.

Note that (at least on MS-Windows) the hook will be always called at
startup, when Emacs sets the theme using the system theme -- is that
what we want?

Thanks.




This bug report was last modified 5 days ago.

Previous Next


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