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

From: "Paul D. Nelson" <[email-masked]>
Date: Sat, 01 Mar 2025 08:33:57 +0100

>> There are other uses of "reftex-" in style/, so I guess something
>> similar should be done for all of them?
>
> Ideally, yes. (But it seems tedious... Does anyone see possibility to
> tame RefTeX to live along with non-file buffer?)
>

Making RefTeX work well with non-file buffers always seemed to me like a
major undertaking.  The commands

;; reftex-add-index-macros
;; reftex-add-section-levels
;; reftex-add-label-environments
;; reftex-scanning-info-available-p
;; reftex-access-parse-file
;; reftex-access-scan-info
;; reftex-ref-style-list
;; reftex-ref-style-toggle
;; reftex-set-cite-format

each call reftex-tie-multifile-symbols, which in turn calls
reftex-TeX-master-file, which fails (with a hard landing) in a non-file
buffer for which TeX-master has not been set to a tex file.

The attached patch wraps the calls from style files to such functions
with a check for buffer-true-filename (although I would also prefer a
less tedious way to address the underlying issue).

diff --git a/style/alphanum.el b/style/alphanum.el
index 318281b4..37ff8f8b 100644
--- a/style/alphanum.el
+++ b/style/alphanum.el
@@ -92,10 +92,11 @@
    ;; this won't work in multifile documents, but at least there is
    ;; something.
 
-   (if (fboundp 'reftex-add-section-levels)
-       (reftex-add-section-levels
-        '(("toc" .  reftex-get-section-level-alphanum)
-          ("sub" .  reftex-get-section-level-alphanum)))))
+   (when (and buffer-file-truename
+              (fboundp 'reftex-add-section-levels))
+     (reftex-add-section-levels
+      '(("toc" .  reftex-get-section-level-alphanum)
+        ("sub" .  reftex-get-section-level-alphanum)))))
  TeX-dialect)
 
 ;; Local Variables:
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))
diff --git a/style/beamer.el b/style/beamer.el
index fe1e4611..4afa2973 100644
--- a/style/beamer.el
+++ b/style/beamer.el
@@ -605,7 +605,8 @@ also be a string.  Then the length of the string is used."
     (insert LaTeX-title TeX-grcl)
     (LaTeX-newline)
     ;; If RefTeX is available, tell it that we've just made a new section
-    (and (fboundp 'reftex-notice-new-section)
+    (and buffer-file-truename
+         (fboundp 'reftex-notice-new-section)
          (reftex-notice-new-section))))
 
 (defvar LaTeX-beamer-class-options-list
diff --git a/style/biblatex.el b/style/biblatex.el
index 8b2c6b7a..c8d58740 100644
--- a/style/biblatex.el
+++ b/style/biblatex.el
@@ -672,7 +672,8 @@ for citation keys."
 
    ;; Tell RefTeX: If package option `natbib' is given, activate that
    ;; format, otherwise stick with `biblatex':
-   (when (and LaTeX-reftex-cite-format-auto-activate
+   (when (and buffer-file-truename
+              LaTeX-reftex-cite-format-auto-activate
               (fboundp 'reftex-set-cite-format))
      (if (or (LaTeX-provided-package-options-member "biblatex" "natbib")
              (LaTeX-provided-package-options-member "biblatex" "natbib=true"))
@@ -708,22 +709,22 @@ for citation keys."
    ;;; Preamble Options
    LaTeX-biblatex-executebibliographyoptions-options
    '(;;; Load-time Options
-    ("backend" ("biber" "bibtex" "bibtexu" "bibtex8"))
-    ("style" BibLaTeX-global-style-files)
-    ("bibstyle" BibLaTeX-global-style-files)
-    ("citestyle" BibLaTeX-global-style-files)
-    ("natbib" ("true" "false"))
-    ("mcite" ("true" "false"))
+     ("backend" ("biber" "bibtex" "bibtexu" "bibtex8"))
+     ("style" BibLaTeX-global-style-files)
+     ("bibstyle" BibLaTeX-global-style-files)
+     ("citestyle" BibLaTeX-global-style-files)
+     ("natbib" ("true" "false"))
+     ("mcite" ("true" "false"))
     ;;; Entry Options
-    ;; Preamble/Type/Entry Options
-    ("useauthor" ("true" "false"))
-    ("useeditor" ("true" "false"))
-    ("usetranslator" ("true" "false"))
-    ("useprefix" ("true" "false"))
-    ("indexing" ("true" "false" "cite" "bib"))
-    ;; Type/Entry Options are not available globally.
-    ;; Legacy Options (deprecated)
-    ("openbib")))
+     ;; Preamble/Type/Entry Options
+     ("useauthor" ("true" "false"))
+     ("useeditor" ("true" "false"))
+     ("usetranslator" ("true" "false"))
+     ("useprefix" ("true" "false"))
+     ("indexing" ("true" "false" "cite" "bib"))
+     ;; Type/Entry Options are not available globally.
+     ;; Legacy Options (deprecated)
+     ("openbib")))
   "Package options for the biblatex package.")
 
 (defun LaTeX-biblatex-package-options nil
diff --git a/style/breqn.el b/style/breqn.el
index f2784bf9..5b4db486 100644
--- a/style/breqn.el
+++ b/style/breqn.el
@@ -102,7 +102,8 @@ The keys \"label\" and \"labelprefix\" are omitted.")
    ;; dseries   == equation   -- dseries* == unnumbered
    ;; dgroup    == align      -- dgroup*  == unnumbered
    ;; darray    == eqnarray   -- darray*  == unnumbered
-   (when (fboundp 'reftex-add-label-environments)
+   (when (and buffer-file-truename
+              (fboundp 'reftex-add-label-environments))
      (dolist (env '("dmath" "dseries" "dgroup"))
        (reftex-add-label-environments `((,env ?e nil nil t))))
      (reftex-add-label-environments '(("darray" ?e nil nil eqnarray-like))))
diff --git a/style/cleveref.el b/style/cleveref.el
index 129d4ce1..ceed42d6 100644
--- a/style/cleveref.el
+++ b/style/cleveref.el
@@ -209,7 +209,8 @@ string."
                               'function))
 
    ;; Activate RefTeX reference style.
-   (and LaTeX-reftex-ref-style-auto-activate
+   (and buffer-file-truename
+        LaTeX-reftex-ref-style-auto-activate
         (fboundp 'reftex-ref-style-activate)
         (reftex-ref-style-activate "Cleveref")))
  TeX-dialect)
diff --git a/style/empheq.el b/style/empheq.el
index 31c22eaf..ffbb8e92 100644
--- a/style/empheq.el
+++ b/style/empheq.el
@@ -252,7 +252,8 @@ number of ampersands if possible."
    (add-to-list 'LaTeX-item-list '("empheq" . LaTeX-empheq-item-equation) t)
 
    ;; Reftex support: Use `reftex-add-label-environments'
-   (when (fboundp 'reftex-add-label-environments)
+   (when (and buffer-file-truename
+              (fboundp 'reftex-add-label-environments))
      (reftex-add-label-environments '(("empheq" ?e nil nil t))))
 
    (TeX-add-symbols
@@ -398,7 +399,8 @@ number of ampersands if possible."
          (add-to-list 'LaTeX-label-alist `(,env . LaTeX-amsmath-label) t)))
 
      ;; RefTeX support: Add original definitions with `reftex-add-label-environments'
-     (when (fboundp 'reftex-add-label-environments)
+     (when (and buffer-file-truename
+                (fboundp 'reftex-add-label-environments))
        (reftex-add-label-environments
         '(("AmSalign"     ?e nil nil eqnarray-like)
           ("AmSequation"  ?e nil nil t)
diff --git a/style/exam.el b/style/exam.el
index 64179192..ee250dae 100644
--- a/style/exam.el
+++ b/style/exam.el
@@ -87,7 +87,8 @@ Arguments NAME and TYPE are the same as for the function
        (add-to-list 'LaTeX-label-alist
                     (cons env 'LaTeX-exam-label))
        ;; to RefTeX with `reftex-add-label-environments'
-       (when (fboundp 'reftex-add-label-environments)
+       (when (and buffer-file-truename
+                  (fboundp 'reftex-add-label-environments))
          (reftex-add-label-environments
           `((,env ,LaTeX-exam-reftex-quick-id-key ,LaTeX-exam-label
                   "~\\ref{%s}" nil
diff --git a/style/fancyref.el b/style/fancyref.el
index d2d87bf5..6b71a7cd 100644
--- a/style/fancyref.el
+++ b/style/fancyref.el
@@ -101,7 +101,8 @@
      (font-latex-add-keywords '(("fref" "[{") ("Fref" "[{")) 'reference))
 
    ;; Activate RefTeX reference style.
-   (and LaTeX-reftex-ref-style-auto-activate
+   (and buffer-file-truename
+        LaTeX-reftex-ref-style-auto-activate
         (fboundp 'reftex-ref-style-activate)
         (reftex-ref-style-activate "Fancyref")))
  TeX-dialect)
diff --git a/style/harvard.el b/style/harvard.el
index 36e2e471..431e72b5 100644
--- a/style/harvard.el
+++ b/style/harvard.el
@@ -101,7 +101,8 @@
                LaTeX-item-list))
 
    ;; Tell RefTeX
-   (when (and LaTeX-reftex-cite-format-auto-activate
+   (when (and buffer-file-truename
+              LaTeX-reftex-cite-format-auto-activate
               (fboundp 'reftex-set-cite-format))
      (reftex-set-cite-format 'harvard)))
  TeX-dialect)
diff --git a/style/hyperref.el b/style/hyperref.el
index 868acd33..03f7575f 100644
--- a/style/hyperref.el
+++ b/style/hyperref.el
@@ -341,7 +341,8 @@
          (TeX-run-style-hooks "backref"))))
 
    ;; Activate RefTeX reference style.
-   (and LaTeX-reftex-ref-style-auto-activate
+   (and buffer-file-truename
+        LaTeX-reftex-ref-style-auto-activate
         (fboundp 'reftex-ref-style-activate)
         (reftex-ref-style-activate "Hyperref")))
  TeX-dialect)
diff --git a/style/imakeidx.el b/style/imakeidx.el
index 0d6a626c..ae0efce5 100644
--- a/style/imakeidx.el
+++ b/style/imakeidx.el
@@ -93,7 +93,8 @@
          (append
           '(("|see{\\([^{}\n\r]*\\)" 1 LaTeX-index-entry-list))
           TeX-complete-list))
-   (and (fboundp 'reftex-add-index-macros)
+   (and buffer-file-truename
+        (fboundp 'reftex-add-index-macros)
         (reftex-add-index-macros '(default)))
 
    ;; Fontification
diff --git a/style/index.el b/style/index.el
index 1e5657e6..9e370fad 100644
--- a/style/index.el
+++ b/style/index.el
@@ -75,7 +75,8 @@
           TeX-complete-list))
 
    ;; RefTeX support
-   (and (fboundp 'reftex-add-index-macros)
+   (and buffer-file-truename
+        (fboundp 'reftex-add-index-macros)
         (reftex-add-index-macros '(index))))
  TeX-dialect)
 
diff --git a/style/jurabib.el b/style/jurabib.el
index 0aa6966d..532a3cfa 100644
--- a/style/jurabib.el
+++ b/style/jurabib.el
@@ -153,7 +153,8 @@
                                 'variable))
 
      ;; Tell RefTeX (Thanks, Carsten)
-     (when (and LaTeX-reftex-cite-format-auto-activate
+     (when (and buffer-file-truename
+                LaTeX-reftex-cite-format-auto-activate
                 (fboundp 'reftex-set-cite-format))
        ;; Check if RefTeX supports jurabib.
        (if (assoc 'jurabib reftex-cite-format-builtin)
diff --git a/style/listings.el b/style/listings.el
index e03b8216..4738d687 100644
--- a/style/listings.el
+++ b/style/listings.el
@@ -349,7 +349,8 @@
                                       (LaTeX-extract-key-value-label))
                              1 LaTeX-auto-label))
       ;; Tell RefTeX
-      (when (fboundp 'reftex-add-label-environments)
+      (when (and buffer-file-truename
+                 (fboundp 'reftex-add-label-environments))
         (reftex-add-label-environments
          `((,env ?l "lst:" "~\\ref{%s}"
                  LaTeX-keyval-caption-reftex-context-function
@@ -431,7 +432,8 @@
    ;; RefTeX support lstlistings environment via
    ;; `reftex-label-alist-builtin'.  We add the same thing here only
    ;; with our function as 5th element:
-   (when (fboundp 'reftex-add-label-environments)
+   (when (and buffer-file-truename
+              (fboundp 'reftex-add-label-environments))
      (reftex-add-label-environments
       '(("lstlisting" ?l "lst:" "~\\ref{%s}"
          LaTeX-keyval-caption-reftex-context-function
diff --git a/style/makeidx.el b/style/makeidx.el
index 680a16b7..0f7bbb3e 100644
--- a/style/makeidx.el
+++ b/style/makeidx.el
@@ -45,7 +45,8 @@
           TeX-complete-list))
 
    ;; RefTeX support
-   (and (fboundp 'reftex-add-index-macros)
+   (and buffer-file-truename
+        (fboundp 'reftex-add-index-macros)
         (reftex-add-index-macros '(default)))
 
    ;; Fontification
diff --git a/style/mathtools.el b/style/mathtools.el
index 853093d3..55039731 100644
--- a/style/mathtools.el
+++ b/style/mathtools.el
@@ -145,7 +145,8 @@
                    `(,env . LaTeX-item-equation) t)
       (add-to-list 'LaTeX-label-alist
                    `(,env . LaTeX-amsmath-label) t)
-      (when (fboundp 'reftex-add-label-environments)
+      (when (and buffer-file-truename
+                 (fboundp 'reftex-add-label-environments))
         (reftex-add-label-environments `((,env ?e nil nil t)))))))
 
 (add-hook 'TeX-auto-prepare-hook #'LaTeX-mathtools-auto-prepare t)
@@ -473,7 +474,8 @@ Put line break macro on the last line.  Next, insert an ampersand."
                  LaTeX-label-alist))
 
    ;; RefTeX support: Add env's with `reftex-add-label-environments'
-   (when (fboundp 'reftex-add-label-environments)
+   (when (and buffer-file-truename
+              (fboundp 'reftex-add-label-environments))
      (let ((envs '(("lgathered"  ?e nil nil t)
                    ("rgathered"  ?e nil nil t)
                    ("multlined"  ?e nil nil t))))
diff --git a/style/minted.el b/style/minted.el
index 655e9a4f..94a2aa1a 100644
--- a/style/minted.el
+++ b/style/minted.el
@@ -440,7 +440,8 @@ a list of strings."
      (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
                   '("listing" current-indentation) t)
      (add-to-list 'LaTeX-label-alist '("listing" . LaTeX-listing-label) t)
-     (when (fboundp 'reftex-add-label-environments)
+     (when (and buffer-file-truename
+                (fboundp 'reftex-add-label-environments))
        (reftex-add-label-environments
         '(("listing" ?l "lst:" "~\\ref{%s}" caption nil nil)))))
 
diff --git a/style/minted2.el b/style/minted2.el
index 687b195c..08ddcc09 100644
--- a/style/minted2.el
+++ b/style/minted2.el
@@ -421,7 +421,8 @@ a list of strings."
      (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
                   '("listing" current-indentation) t)
      (add-to-list 'LaTeX-label-alist '("listing" . LaTeX-listing-label) t)
-     (when (fboundp 'reftex-add-label-environments)
+     (when (and buffer-file-truename
+                (fboundp 'reftex-add-label-environments))
        (reftex-add-label-environments
         '(("listing" ?l "lst:" "~\\ref{%s}" caption nil nil)))))
 
diff --git a/style/multind.el b/style/multind.el
index a4e921bd..0bad853b 100644
--- a/style/multind.el
+++ b/style/multind.el
@@ -59,7 +59,8 @@
           TeX-complete-list))
 
    ;; RefTeX support
-   (and (fboundp 'reftex-add-index-macros)
+   (and buffer-file-truename
+        (fboundp 'reftex-add-index-macros)
         (reftex-add-index-macros '(multind)))
 
    ;; Fontification
diff --git a/style/natbib.el b/style/natbib.el
index e04de458..f08565d0 100644
--- a/style/natbib.el
+++ b/style/natbib.el
@@ -168,7 +168,8 @@
                               'function))
 
    ;; Tell RefTeX
-   (when (and LaTeX-reftex-cite-format-auto-activate
+   (when (and buffer-file-truename
+              LaTeX-reftex-cite-format-auto-activate
               (fboundp 'reftex-set-cite-format))
      (reftex-set-cite-format 'natbib)))
  TeX-dialect)
diff --git a/style/newfloat.el b/style/newfloat.el
index e3824a82..2fff45b4 100644
--- a/style/newfloat.el
+++ b/style/newfloat.el
@@ -117,13 +117,15 @@ If `caption.el' is loaded, add the new floating environment to
       (cond ((string-equal type "figure")
              (LaTeX-add-environments `(,flt LaTeX-env-figure))
              (add-to-list 'LaTeX-label-alist `(,flt . LaTeX-figure-label) t)
-             (when (fboundp 'reftex-add-label-environments)
+             (when (and buffer-file-truename
+                      (fboundp 'reftex-add-label-environments))
                (reftex-add-label-environments
                 `((,flt ?f ,LaTeX-figure-label "~\\ref{%s}" caption nil nil)))))
             ((string-equal type "table")
              (LaTeX-add-environments `(,flt LaTeX-env-figure))
              (add-to-list 'LaTeX-label-alist `(,flt . LaTeX-table-label) t)
-             (when (fboundp 'reftex-add-label-environments)
+             (when (and buffer-file-truename
+                      (fboundp 'reftex-add-label-environments))
                (reftex-add-label-environments
                 `((,flt ?t ,LaTeX-table-label "~\\ref{%s}" caption nil nil)))))
             ((string-equal type "verbatim")
@@ -131,7 +133,8 @@ If `caption.el' is loaded, add the new floating environment to
              (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
                           `(,flt current-indentation) t)
              (add-to-list 'LaTeX-label-alist `(,flt . LaTeX-listing-label) t)
-             (when (fboundp 'reftex-add-label-environments)
+             (when (and buffer-file-truename
+                      (fboundp 'reftex-add-label-environments))
                (reftex-add-label-environments
                 `((,flt ?l "lst:" "~\\ref{%s}" caption nil nil)))))
             (t
diff --git a/style/subcaption.el b/style/subcaption.el
index 4ae2975d..2c6e98c5 100644
--- a/style/subcaption.el
+++ b/style/subcaption.el
@@ -215,7 +215,8 @@ outer-pos adjusted."
    (add-to-list 'LaTeX-label-alist '("subtable" . LaTeX-table-label) t)
 
    ;; Introduce env's to RefTeX if loaded
-   (when (fboundp 'reftex-add-label-environments)
+   (when (and buffer-file-truename
+              (fboundp 'reftex-add-label-environments))
      (reftex-add-label-environments
       `(("subfigure" ?f ,LaTeX-figure-label "~\\ref{%s}" caption)
         ("subtable"  ?t ,LaTeX-table-label  "~\\ref{%s}" caption))))
diff --git a/style/varioref.el b/style/varioref.el
index e7f95f7f..4c9ffb26 100644
--- a/style/varioref.el
+++ b/style/varioref.el
@@ -88,7 +88,8 @@
                               'reference))
 
    ;; Activate RefTeX reference style.
-   (and LaTeX-reftex-ref-style-auto-activate
+   (and buffer-file-truename
+        LaTeX-reftex-ref-style-auto-activate
         (fboundp 'reftex-ref-style-activate)
         (reftex-ref-style-activate "Varioref")))
  TeX-dialect)