Subject: bug#76615: 14.0.9; error with amsmath style hooks

From: "Paul D. Nelson" <[email-masked]>
Date: Fri, 28 Feb 2025 17:06:58 +0100

Hi Ikumi,

>
> The fundamental origin of the problem is that RefTeX presumes that it
> always works in the buffer associated with files. In other words, RefTeX
> pays no attention to non-file buffer. (I have such impression, skimming
> over the RefTeX code. Please correct me if I'm wrong.)
>

This has also been my understanding (although I stopped using RefTeX
years ago, basically for this reason).

>>>>>> "Paul D. Nelson" <[email-masked]> writes:
>> The attached patch addresses the issue for me.  Thoughts?
>
> I have basically the same idea, but I think such partial treatment
> inctroduces inconsistent internal state of RefTeX and can be potential
> problems. Rather I propose to wrap the whole RefTeX management
> part in amsmath.el by (when buffer-file-name ... ). In addition, we
> should turn off RefTeX mode in `TeX-mode-cleanup' like
>   (unless buffer-file-truename
>     (if (bound-and-true-p reftex-mode) <-- addition
>       (reftex-mode -1))                <-- addition
>     (TeX-update-style))
> to avoid inconsistency.
>

Something like the attached, I guess?  There are other uses of "reftex-"
in style/, so I guess something similar should be done for all of them?

Thanks, best,

Paul

diff --git a/style/amsmath.el b/style/amsmath.el
index 9d6944dc..c73f7475 100644
--- a/style/amsmath.el
+++ b/style/amsmath.el
@@ -163,24 +163,25 @@
    (TeX-run-style-hooks "amstext" "amsbsy" "amsopn")
 
    ;; RefTeX support: Tell RefTeX about amsmath environments.
-   (when (fboundp 'reftex-add-to-label-alist)
-     (reftex-add-to-label-alist '(AMSTeX)))
-
-   ;; Add \eqref to `reftex-ref-style-alist' and associate it to `e'
-   (when (and (boundp 'reftex-ref-style-alist)
-              ;; check if Emacs is already equipped with this
-              (not (assoc "AMSmath" reftex-ref-style-alist)))
-     ;; Append it to a local version in order to avoid a clash with
-     ;; user settings
-     (add-to-list (make-local-variable 'reftex-ref-style-alist)
-                  '("AMSmath" "amsmath" (("\\eqref" ?e)))
-                  t))
-
-   ;; Activate `AMSmath' when `LaTeX-reftex-ref-style-auto-activate'
-   ;; is non-nil
-   (and LaTeX-reftex-ref-style-auto-activate
-        (fboundp 'reftex-ref-style-activate)
-        (reftex-ref-style-activate "AMSmath"))
+   (when buffer-file-truename
+     (when (fboundp 'reftex-add-to-label-alist)
+       (reftex-add-to-label-alist '(AMSTeX)))
+
+     ;; Add \eqref to `reftex-ref-style-alist' and associate it to `e'
+     (when (and (boundp 'reftex-ref-style-alist)
+                ;; check if Emacs is already equipped with this
+                (not (assoc "AMSmath" reftex-ref-style-alist)))
+       ;; Append it to a local version in order to avoid a clash with
+       ;; user settings
+       (add-to-list (make-local-variable 'reftex-ref-style-alist)
+                    '("AMSmath" "amsmath" (("\\eqref" ?e)))
+                    t))
+
+     ;; Activate `AMSmath' when `LaTeX-reftex-ref-style-auto-activate'
+     ;; is non-nil
+     (and LaTeX-reftex-ref-style-auto-activate
+          (fboundp 'reftex-ref-style-activate)
+          (reftex-ref-style-activate "AMSmath")))
 
    (when (and (featurep 'font-latex)
               (eq TeX-install-font-lock 'font-latex-setup))