Subject: bug#78841: reftex-get-bibfile-list fails to detect biblatex if there's no buffer for the main file

From: Tim Ruffing <[email-masked]>
Date: Wed, 02 Jul 2025 13:48:28 +0200

Independently of latexenc (which should be fixed, yes), here's a patch
that gets rid of the heuristic in reftex. I still believe that this
simplifies things, doesn't matter at all performance-wise, and gets rid
of numerous corner cases in which the heuristic won't work, some of
which are in my previous email:

>  * latexenc-dont-use-TeX-master-flag is t
>  * latexenc will be removed or changed in the future
>  * the \addbibresource commands is not in the same file as
>    \usepackage{biblatex}

If you think that's a good idea, then let me know and I'll write a
proper commit message.

Best,
Tim

On Sun, 2025-06-29 at 11:34 +0200, Arash Esbati wrote:
> Stefan Monnier <[email-masked]> writes:
> 
> > LGTM.
> 
> Thanks.
> 
> > This said, I don't think we need `default-value`.  And we can
> > presumably simplify the above to
> > 
> >              (let ((file (if (re-search-forward
> >                               "^%+ *\\(TeX-master\\|tex-main-
> > file\\): *\"\\(.+\\)\""
> >                               nil t)
> >                              (match-string 2)
> >                            (or (bound-and-true-p TeX-master)
> >                                (bound-and-true-p tex-main-file)))))
> >                (when (stringp file)
> >                  (dolist ...)))
> 
> Yes, you're right.  And while we're at it, I also think we should
> change:
> 
>   (search-forward "Local Variables:" nil t)
> 
> to
> 
>   (re-search-forward "^%+ *Local Variables:" nil t)
> 
> WDYT?  I'd install this change:
> 
> --8<---------------cut here---------------start------------->8---
> diff --git a/lisp/international/latexenc.el
> b/lisp/international/latexenc.el
> index 1b735810ee4..fea5be6dbd7 100644
> --- a/lisp/international/latexenc.el
> +++ b/lisp/international/latexenc.el
> @@ -144,24 +144,27 @@ latexenc-find-file-coding-system
>                                        (file-name-directory (nth 1
> arg-list))
>                                      default-directory))
>                 latexenc-main-file)
> -            ;; Is there a TeX-master or tex-main-file in the local
> variables
> -            ;; section?
> +            ;; Is there a TeX-master or tex-main-file in the local
> +            ;; variables section or is it globally set to a constant
> +            ;; string?
>              (unless latexenc-dont-use-TeX-master-flag
>                (goto-char (point-max))
>               (search-backward "\n\^L" (max (- (point-max) 3000)
> (point-min))
>                                 'move)
> -             (search-forward "Local Variables:" nil t)
> -              (when (re-search-forward
> -                     "^%+ *\\(TeX-master\\|tex-main-file\\):
> *\"\\(.+\\)\""
> -                     nil t)
> -                (let ((file (match-string 2)))
> -                  (dolist (ext `("" ,(if (boundp 'TeX-default-
> extension)
> -                                         (concat "." TeX-default-
> extension)
> -                                       "")
> -                                 ".tex" ".ltx" ".dtx" ".drv"))
> -                    (if (and (null latexenc-main-file) ;Stop at
> first.
> -                             (file-exists-p (concat file ext)))
> -                        (setq latexenc-main-file (concat file
> ext)))))))
> +             (re-search-forward "^%+ *Local Variables:" nil t)
> +              (let ((file (if (re-search-forward
> +                               "^%+ *\\(TeX-master\\|tex-main-
> file\\): *\"\\(.+\\)\""
> +                               nil t)
> +                              (match-string 2)
> +                            (or (bound-and-true-p TeX-master)
> +                                (bound-and-true-p tex-main-file)))))
> +                (dolist (ext `("" ,(if (boundp 'TeX-default-
> extension)
> +                                       (concat "." TeX-default-
> extension)
> +                                     "")
> +                               ".tex" ".ltx" ".dtx" ".drv"))
> +                  (if (and (null latexenc-main-file) ;Stop at first.
> +                           (file-exists-p (concat file ext)))
> +                      (setq latexenc-main-file (concat file
> ext))))))
>              ;; try tex-modes tex-guess-main-file
>              (when (and (not latexenc-dont-use-tex-guess-main-file-
> flag)
>                         (not latexenc-main-file))
> --8<---------------cut here---------------end--------------->8---
> 
> Best, Arash
From 265fb46a1488d54ab03ff3054631e9dbf2f87c71 Mon Sep 17 00:00:00 2001
From: Tim Ruffing <[email-masked]>
Date: Wed, 2 Jul 2025 13:42:52 +0200
Subject: [PATCH] reftex: Remove heuristics to detect biblatex

---
 lisp/textmodes/reftex-parse.el | 46 ++++++++++------------------------
 1 file changed, 13 insertions(+), 33 deletions(-)

diff --git a/lisp/textmodes/reftex-parse.el b/lisp/textmodes/reftex-parse.el
index a7ff07eff8e..a793807a7da 100644
--- a/lisp/textmodes/reftex-parse.el
+++ b/lisp/textmodes/reftex-parse.el
@@ -373,45 +373,25 @@ reftex-parse-from-file
     ;; Return the list
     docstruct))
 
-(defun reftex-using-biblatex-p ()
-  "Return non-nil if we are using biblatex or other specific cite package.
-biblatex and other similar packages like multibib allow multiple macro
-calls to load a bibliography file.  This function should be able to
-detect those packages."
-  (if (boundp 'TeX-active-styles)
-      ;; the sophisticated AUCTeX way
-      (or (member "biblatex" TeX-active-styles)
-          (member "multibib" TeX-active-styles))
-    ;; poor-man's check...
-    (save-excursion
-      (re-search-forward
-       "^[^%\n]*?\\\\usepackage\\(\\[[^]]*\\]\\)?{biblatex\\|multibib}" nil t))))
-
 ;;;###autoload
 (defun reftex-locate-bibliography-files (master-dir &optional files)
   "Scan buffer for bibliography macros and return file list."
   (unless files
     (save-excursion
       (goto-char (point-min))
-      ;; when biblatex or multibib are used, multiple \bibliography or
-      ;; \addbibresource macros are allowed.  With plain bibtex, only
-      ;; the first is used.
-      (let ((using-biblatex (reftex-using-biblatex-p))
-	    (again t))
-	(while (and again
-		    (re-search-forward
-		     (concat
-		      ;; "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\("
-		      "\\(^\\)[^%\n\r]*\\\\\\("
-		      (mapconcat #'identity reftex-bibliography-commands "\\|")
-		      "\\)\\(\\[.+?\\]\\)?{[ \t]*\\([^}]+\\)")
-		     nil t))
-	  (setq files
-		(append files
-			(split-string (reftex-match-string 4)
-				      "[ \t\n\r]*,[ \t\n\r]*")))
-	  (unless using-biblatex
-	    (setq again nil))))))
+      ;; When biblatex or multibib are used, multiple \bibliography or
+      ;; \addbibresource macros are allowed, so find all of them.
+      (while (re-search-forward
+              (concat
+               ;; "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\("
+               "\\(^\\)[^%\n\r]*\\\\\\("
+               (mapconcat #'identity reftex-bibliography-commands "\\|")
+               "\\)\\(\\[.+?\\]\\)?{[ \t]*\\([^}]+\\)")
+              nil t)
+        (setq files
+              (append files
+                      (split-string (reftex-match-string 4)
+                                    "[ \t\n\r]*,[ \t\n\r]*"))))))
   (when files
     (setq files
           (mapcar
-- 
2.50.0