Subject: bug#76781: Fontification of expl3 code

From: Ikumi Keita <[email-masked]>
Date: Thu, 27 Mar 2025 22:21:33 +0900

>>>>> Ikumi Keita <[email-masked]> writes:
> 2. I think that there are two approaches to this bug.
>    A: To move the call to `TeX-master-file' and `TeX-update-style' from
>       `find-file-hook' to somewhere run _before_
>       `after-change-major-mode-hook' and _after_ major mode hook and
>       local variables entries are applied.
>    B: To abandon the assumption in question and make a new AUCTeX policy
>       to write style files assuming that style hooks are run after font
>       lock is enabled.

>    In my view, A isn't satisfactory:

I changed my mind. :-) It seems that we can use
`hack-local-variables-hook'. The attached patch looks working well for
me, although this affects tex-jp.el.

>    o It wouldn't be robust. The detail of the interaction between major
>      mode function and turning on of font lock can be subject to further
>      changes in future.
>    o There is no suitable place for such "somewhere". Looking at the
>      definition of `run-mode-hooks', the only possibility is to register
>      the calls as local hook of `after-change-major-mode-hook' but I'm
>      afraid that such treatment can break in future as described in the
>      previous item.
>    (On the other hand, there is a good aspect that we can merge call to
>    `TeX-update-sytle' for non-file buffer in `TeX-mode-cleanup'.)

This doesn't happen for the attached patch because emacs doesn't run
`hack-local-variables-hook' for non-file buffer. However, we can
simplify `TeX-normal-mode', which doesn't run `find-file-hook' whereas
does `hack-local-variables-hook' through `normal-mode'.

>    If we take approach B, I think the proper solution is to use
>    `font-latex-add-to-syntax-alist' instead of re-calling
>    `font-lock-set-defaults' in expl3.el. See the attached patch.
>    Additionally, we would have to revise similarly existing style files
>    which install their own syntax tables. I expect this doesn't require
>    much work; Here is the output of "grep -E set-syntax-table *.el"
>    under style/ subdirectory:
> brazilian.el:   (set-syntax-table LaTeX-brazilian-mode-syntax-table)
> bulgarian.el:   (set-syntax-table LaTeX-bulgarian-mode-syntax-table)
> expl3.el:   (set-syntax-table LaTeX-expl3-syntax-table)
> german.el:   (set-syntax-table LaTeX-german-mode-syntax-table)
> icelandic.el:   (set-syntax-table LaTeX-icelandic-mode-syntax-table)
> ngerman.el:   (set-syntax-table LaTeX-german-mode-syntax-table)
> plfonts.el:   (set-syntax-table LaTeX-plfonts-mode-syntax-table)
> plhb.el:   (set-syntax-table LaTeX-plhb-mode-syntax-table)
> polish.el:   (set-syntax-table LaTeX-polish-mode-syntax-table)
> portuguese.el:   (set-syntax-table LaTeX-portuguese-mode-syntax-table)

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine
#Gaza #StopMassiveKilling #CeasefireNOW

>From c18befce8a1407bcd62d2ac85e95d0a2b59f8051 Mon Sep 17 00:00:00 2001
From: Ikumi Keita <[email-masked]>
Date: Wed, 26 Mar 2025 23:44:39 +0900
Subject: [PATCH] temporal commit

---
 style/expl3.el |  5 +----
 tex-jp.el      |  8 ++++++--
 tex.el         | 25 ++++++++++---------------
 3 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/style/expl3.el b/style/expl3.el
index 60103dc4..a8ec0c76 100644
--- a/style/expl3.el
+++ b/style/expl3.el
@@ -710,10 +710,7 @@ Pass OPTIONAL, PROMPT and NO-PARAM to `TeX-arg-expl3-macro', which see."
 
                                 ("cs_undefine:N" "\\")
                                 ("cs_undefine:c" "\\"))
-                              'function)
-     ;; Also tell font-lock to update its internals
-     (setq font-lock-major-mode nil)
-     (font-lock-set-defaults)))
+                              'function)))
  TeX-dialect)
 
 (defvar LaTeX-expl3-package-options-list
diff --git a/tex-jp.el b/tex-jp.el
index 9d9c75a9..439dcbc5 100644
--- a/tex-jp.el
+++ b/tex-jp.el
@@ -409,7 +409,9 @@ For inappropriate encoding, nil instead."
   :syntax-table nil
   ;; See the comments below in `japanese-LaTeX-mode' for the intent of
   ;; the following line.
-  :after-hook (setq major-mode 'plain-TeX-mode)
+  (add-hook 'hack-local-variables-hook
+            (lambda () (setq major-mode 'plain-TeX-mode))
+            nil t)
 
   (setq japanese-TeX-mode t)
 
@@ -446,7 +448,9 @@ Now `japanese-plain-tex-mode-initialization' is no-op.  Don't use it."))
   ;; variables prepared for `japanese-LaTeX-mode'.
   ;; Thus we change `major-mode' to `LaTeX-mode' after
   ;; `hack-local-variables' is done.
-  :after-hook (setq major-mode 'LaTeX-mode)
+  (add-hook 'hack-local-variables-hook
+            (lambda () (setq major-mode 'LaTeX-mode))
+            nil t)
 
   (setq japanese-TeX-mode t)
 
diff --git a/tex.el b/tex.el
index 3d28b2c6..c7db714e 100644
--- a/tex.el
+++ b/tex.el
@@ -3879,26 +3879,24 @@ Not intended for direct use for user."
             #'TeX--completion-at-point nil t)
 
   ;; Let `TeX-master-file' be called after a new file was opened and
-  ;; call `TeX-update-style' on any file opened.  (The addition to the
-  ;; hook has to be made here because its local value will be deleted
-  ;; by `kill-all-local-variables' if it is added e.g. in `tex-mode'.)
+  ;; call `TeX-update-style' on any file opened.
   ;;
-  ;; `TeX-update-style' has to be called before
-  ;; `global-font-lock-mode', which may also be specified in
-  ;; `find-file-hook', gets called.  Otherwise style-based
-  ;; fontification will break (in XEmacs).  That means, `add-hook'
-  ;; cannot be called with a non-nil value of the APPEND argument.
+  ;; `TeX-update-style' has to be called before font lock is turned on,
+  ;; which may get called in `change-major-mode-after-body-hook'.
+  ;; Otherwise font lock can break because tuning of syntax table by
+  ;; style files isn't reflected to `font-lock-syntax-table'.
   ;;
   ;; `(TeX-master-file nil nil t)' has to be called *before*
   ;; `TeX-update-style' as the latter will call `TeX-master-file'
   ;; without the `ask' bit set.
-  (add-hook 'find-file-hook
+  (add-hook 'hack-local-variables-hook
             (lambda ()
               ;; Check if we are looking at a new or shared file.
               (when (or (not (file-exists-p (TeX-buffer-file-name)))
                         (eq TeX-master 'shared))
                 (TeX-master-file nil nil t))
-              (TeX-update-style t)) nil t))
+              (TeX-update-style t))
+            nil t))
 
 (defun TeX-mode-cleanup ()
   "Cleanup function for `TeX-mode'.
@@ -6321,7 +6319,7 @@ Save buffer first including style information.
 With optional argument ARG, also reload the style hooks."
   (interactive "*P")
   (with-current-buffer
-  ;; In case this is an indirect buffer:
+      ;; In case this is an indirect buffer:
       (or (buffer-base-buffer) (current-buffer))
     (if arg
         (dolist (var TeX-normal-mode-reset-list)
@@ -6331,10 +6329,7 @@ With optional argument ARG, also reload the style hooks."
         (if (buffer-modified-p)
             (save-buffer)
           (TeX-auto-write)))
-      (normal-mode)
-      ;; See also addition to `find-file-hook' in `TeX-mode'.
-      (when (eq TeX-master 'shared) (TeX-master-file nil nil t))
-      (TeX-update-style t))))
+      (normal-mode))))
 
 (defgroup TeX-quote nil
   "Quoting in AUCTeX."
-- 
2.48.1