Subject: bug#66741: 30.0.50; [BUG] pdumper, global-font-lock-mode, and :init-value in define-minor-mode

From: Ihor Radchenko <[email-masked]>
Date: Wed, 25 Oct 2023 09:02:25 +0000

X-Debbugs-CC: Eli Zaretskii <[email-masked]>

Hi,

This is a followup after emacs-devel thread on using pdumper:
https://yhetil.org/emacs-devel/[email-masked]/

I was trying to create a simple Emacs dump via

------ dump.el -------
(dump-emacs-portable "/tmp/emacs-dumped.dmp")
----------------------
./src/emacs -q --batch -l ~/Downloads/dump.el

Similar to the problem described in
https://archive.casouri.cc/note/2020/painless-transition-to-portable-dumper/index.html
I got global-font-lock-mode disabled (and the value is nil) when
starting Emacs with the above dump file.

The first immediate surprise here is that lisp/loadup.el already has
(load "font-lock"). So, it should be pre-loaded already. I do not fully
understand why re-dumping changes Emacs behavior.

Trying to investigate a bit about the possible cause, I found the
following suspicious form in font-core.el:

(define-globalized-minor-mode global-font-lock-mode
  font-lock-mode turn-on-font-lock-if-desired
  ;; What was this :extra-args thingy for?  --Stef
  ;; :extra-args (dummy)
  :initialize 'custom-initialize-delay
  :init-value (not (or noninteractive emacs-basic-display))
  :group 'font-lock
  :version "22.1")

Note how :init-value is set according to `noninteractive', which is t
when creating the dump file.

It looks like either a bug in font-core.el that makes wrong assumptions
about :init-value evaluation time or a bug in `define-minor-mode' that
may need to arrange :init-value calculation to be evaluated at load
time.

-----------

Another (possible) problem appeared when I tried to work around the
above issue.  I attempted

(defun font-lock--enable-after-pdump ()
  "Re-initialize `global-font-lock-mode' after loading Emacs with dump file."
  (setq global-font-lock-mode (not (or noninteractive emacs-basic-display))))
(add-hook 'after-pdump-load-hook #'font-lock--enable-after-pdump)

But, to my surprise, it did not lead to font-lock-mode being active in
*scratch* buffer. Either I miss something important here or
`after-pdump-load-hook' may be evaluated too late in Emacs loading for
the purposes for setting up font-lock.

In GNU Emacs 30.0.50 (build 19, x86_64-pc-linux-gnu, GTK+ Version
 3.24.38, cairo version 1.18.0) of 2023-10-25 built on localhost
Repository revision: 8f00c9c3c6c4e76651c000e41582e002755e598b
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101008
System Description: Gentoo Linux

Configured using:
 'configure --with-native-compilation CFLAGS=-g3 --with-pdumper=yes'

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>