Subject: Re: bug#69431: 30.0.50; Strange fontificaion behavior

From: Stefan Monnier <[email-masked]>
Date: Sun, 07 Apr 2024 11:29:25 -0400

> I'd like to have Stefan opinion if this is okay

It's an ugly workaround, so it would need some comment.

> or we have a better way to fix this.

Yes, it's a problem in `easy-mmode.el`.  I think the patch below should
fix it (it'll need recompiling those files which define globalized
minor modes before it gets effective).

Actually, nowadays modes that don't use `run-mode-hooks` should be
vanishingly rare and I think we could "drop support" for them, which
would significantly simplify that macro.

        Stefan
diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el
index 4fa05008dd8..55cda4c5aea 100644
--- a/lisp/emacs-lisp/easy-mmode.el
+++ b/lisp/emacs-lisp/easy-mmode.el
@@ -493,6 +493,8 @@ define-globalized-minor-mode
 	 (extra-keywords nil)
          (MODE-variable mode)
 	 (MODE-buffers (intern (concat global-mode-name "-buffers")))
+	 (MODE-enable-in-buffer
+	  (intern (concat global-mode-name "-enable-in-buffer")))
 	 (MODE-enable-in-buffers
 	  (intern (concat global-mode-name "-enable-in-buffers")))
 	 (MODE-check-buffers
@@ -559,10 +561,10 @@ define-globalized-minor-mode
 	 (if ,global-mode
 	     (progn
 	       (add-hook 'after-change-major-mode-hook
-			 #',MODE-enable-in-buffers)
+			 #',MODE-enable-in-buffer)
 	       (add-hook 'find-file-hook #',MODE-check-buffers)
 	       (add-hook 'change-major-mode-hook #',MODE-cmhh))
-	   (remove-hook 'after-change-major-mode-hook #',MODE-enable-in-buffers)
+	   (remove-hook 'after-change-major-mode-hook #',MODE-enable-in-buffer)
 	   (remove-hook 'find-file-hook #',MODE-check-buffers)
 	   (remove-hook 'change-major-mode-hook #',MODE-cmhh))
 
@@ -609,6 +611,22 @@ define-globalized-minor-mode
        ;; List of buffers left to process.
        (defvar ,MODE-buffers nil)
 
+       ;; The function that calls TURN-ON in the current buffer.
+       (defun ,MODE-enable-in-buffer ()
+         (setq ,MODE-buffers (delq (current-buffer) ,MODE-buffers))
+         (unless ,MODE-set-explicitly
+           (unless (eq ,MODE-major-mode major-mode)
+             (if ,MODE-variable
+                 (progn
+                   (,mode -1)
+                   (funcall ,turn-on-function))
+               (funcall ,turn-on-function))))
+         (setq ,MODE-major-mode major-mode))
+       (put ',MODE-enable-in-buffer 'definition-name ',global-mode)
+
+       ;; All the functions below are trying to handle those
+       ;;  major modes which don't use `run-mode-hooks'.
+
        ;; The function that calls TURN-ON in each buffer.
        (defun ,MODE-enable-in-buffers ()
          (let ((buffers ,MODE-buffers))
@@ -618,15 +636,8 @@ define-globalized-minor-mode
            (setq ,MODE-buffers nil)
            (dolist (buf buffers)
              (when (buffer-live-p buf)
-               (with-current-buffer buf
-                 (unless ,MODE-set-explicitly
-                   (unless (eq ,MODE-major-mode major-mode)
-                     (if ,MODE-variable
-                         (progn
-                           (,mode -1)
-                           (funcall ,turn-on-function))
-                       (funcall ,turn-on-function))))
-                 (setq ,MODE-major-mode major-mode))))))
+              (with-current-buffer buf
+                (,MODE-enable-in-buffer))))))
        (put ',MODE-enable-in-buffers 'definition-name ',global-mode)
 
        (defun ,MODE-check-buffers ()