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

From: Ikumi Keita <[email-masked]>
Date: Thu, 20 Mar 2025 21:34:03 +0900

Hi Paul,

>>>>> "Paul D. Nelson" <[email-masked]> writes:
> Patching reftex in the suggested way does seem to alleviate the noted
> issue with style hooks.  I can only imagine it breaks other things, but
> given that non-file buffers are already not supported in reftex, maybe
> that's OK?

Thanks for your opinion. Yes, I think that's permissible.

> diff --git a/lisp/textmodes/reftex.el b/lisp/textmodes/reftex.el

I think that we should minimize the change in the RefTeX behavior. How
about the patches below, for both RefTeX and AUCTeX? It introduces a new
flag variable and change the behavior only when the new flag has non-nil
value.

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

diff -r eb0ce6c359be lisp/textmodes/reftex.el
--- a/lisp/textmodes/reftex.el	Wed Feb 26 22:15:46 2025 +0900
+++ b/lisp/textmodes/reftex.el	Thu Mar 20 21:19:43 2025 +0900
@@ -62,6 +62,12 @@
     (setq reftex-tables-dirty t)
     (set symbol value)))
 
+(defvar reftex--suppress-nonfile-error nil
+  "When non-nil, don't signal error in non-file buffer.
+
+Note that this is just a quick and dirty hack and is _not_ reliable at
+all.  It only circumvents disastrous error in `reftex-TeX-master-file',
+in case that the user turns on RefTeX in latex mode hook.")
 
 ;; Configuration variables
 (require 'reftex-vars)
@@ -378,7 +384,8 @@
           (buffer-file-name)))))
     (cond
      ((null master)
-      (error "Need a filename for this buffer, please save it first"))
+      (or reftex--suppress-nonfile-error
+          (error "Need a filename for this buffer, please save it first")))
      ((or (file-exists-p (concat master ".tex"))
           (find-buffer-visiting (concat master ".tex")))
       ;; Ahh, an extra .tex was missing...
@@ -390,7 +397,8 @@
      (t
       ;; Use buffer file name.
       (setq master (buffer-file-name))))
-    (expand-file-name master)))
+    (unless reftex--suppress-nonfile-error
+      (expand-file-name master))))
 
 (defun reftex-is-multi ()
   ;; Tell if this is a multifile document.  When not sure, say yes.

diff --git a/tex.el b/tex.el
index 3d28b2c6..2f650b39 100644
--- a/tex.el
+++ b/tex.el
@@ -102,6 +102,7 @@
 (defvar ispell-parser)                  ; ispell.el
 (defvar compilation-error-regexp-alist) ; compile.el
 (defvar compilation-in-progress)        ; compile.el
+(defvar reftex--suppress-nonfile-error) ; reftex.el
 
 (defconst TeX-mode-comparison-alist
   '((plain-tex-mode . plain-TeX-mode)
@@ -3929,7 +3930,12 @@ Run after mode hooks and file local variables application."
   ;; `find-file-hook'.  This is necessary for `xref-find-references',
   ;; for example. (bug#65912)
   (unless buffer-file-truename
-    (TeX-update-style))
+    (TeX-update-style)
+
+    ;; Suppress RefTeX error in non-file buffer. (Bug#76615)
+    (when (featurep 'reftex)
+      (setq-local reftex--suppress-nonfile-error t)
+      (reftex-mode -1)))
 
   (TeX-set-mode-name))