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

From: Arash Esbati <[email-masked]>
Date: Tue, 24 Jun 2025 10:14:53 +0200

Tim Ruffing <[email-masked]> writes:

> Hi again, thanks for taking the time and sorry that my instructions
> were still unclear.

Thanks for checking, and no problem 👍

> I can't reproduce the bug with your commands only,

Good.

> but I can reproduce it if I add this line (setq
> latexenc-dont-use-TeX-master-flag nil) as mentioned in my previous
> email.

Ok.  But this is not a real requirement, right?  This is just to
showcase the issue.  Your setup involves setting `TeX-master' to a
constant string, right?  If so, can you please delete this line:

%%% TeX-master: "biblatex-main"

in biblatex-sub.tex and save it, start a new Emacs with 'emacs -Q', eval
this in scratch:

--8<---------------cut here---------------start------------->8---
(progn
  (load "~/.config/emacs/.local/straight/build-30.1/auctex/auctex-autoloads.el" nil t t)
  (setq-default TeX-master "biblatex-main")
  (setq TeX-parse-self t)
  (add-hook 'LaTeX-mode-hook #'reftex-mode)
  (setq reftex-plug-into-AUCTeX t)
  (defun latexenc-find-file-coding-system (arg-list)
    "Determine the coding system of a LaTeX file if it uses \"inputenc.sty\".
The mapping from LaTeX's \"inputenc.sty\" encoding names to Emacs
coding system names is determined from `latex-inputenc-coding-alist'."
    (if (eq (car arg-list) 'insert-file-contents)
        (save-excursion
          ;; try to find the coding system in this file
          (goto-char (point-min))
          (if (catch 'cs
                (let ((case-fold-search nil))
                  (while (search-forward "inputenc" nil t)
                    (goto-char (match-beginning 0))
                    (beginning-of-line)
                    (if (or (looking-at "[^%\n]*\\\\usepackage\\[\\([^]]*\\)\\]{\\([^}]*,\\)?inputenc\\(,[^}]*\\)?}")
                            (looking-at "[^%\n]*\\\\inputencoding{\\([^}]*\\)}"))
                        (throw 'cs t)
                      (goto-char (match-end 0))))))
              (let* ((match (match-string 1))
                     (sym (or (latexenc-inputenc-to-coding-system match)
                              (intern match))))
                (cond
                 ((coding-system-p sym) sym)
                 ((and (require 'code-pages nil t) (coding-system-p sym)) sym)
                 (t 'undecided)))
            ;; else try to find it in the master/main file

            ;; Fixme: If the current file is in an archive (e.g. tar,
            ;; zip), we should find the master file in that archive.
            ;; But, that is not yet implemented.   -- K.Handa
            (let ((default-directory (if (stringp (nth 1 arg-list))
                                         (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?
              (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 (or (re-search-forward
                           "^%+ *\\(TeX-master\\|tex-main-file\\): *\"\\(.+\\)\""
                           nil t)
                          ;; Addition start
                          (and (boundp 'TeX-master)
                               (stringp (default-value 'TeX-master)))
                          ;; Addition end
                          )
                  (let ((file
                         ;; Addition start
                         (or (match-string 2)
                             (and (boundp 'TeX-master)
                                  (default-value 'TeX-master))
                             ;; Addition end
                             )))
                    (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))
                ;; Use a separate `when' so the byte-compiler sees the fboundp.
                (when (fboundp 'tex-guess-main-file)
                  (let ((tex-start-of-header "\\\\document\\(style\\|class\\)"))
                    (setq latexenc-main-file (tex-guess-main-file)))))
              ;; if we found a master/main file get the coding system from it
              (if (and latexenc-main-file
                       (file-regular-p latexenc-main-file)
                       (file-readable-p latexenc-main-file))
                  (let* ((latexenc-dont-use-tex-guess-main-file-flag t)
                         (latexenc-dont-use-TeX-master-flag t)
                         (latexenc-main-buffer
                          (find-file-noselect latexenc-main-file t)))
                    (coding-system-base   ;Disregard the EOL part of the CS.
                     (with-current-buffer latexenc-main-buffer
                       (or coding-system-for-write buffer-file-coding-system
                           'undecided))))
                'undecided))))
      'undecided)) )
--8<---------------cut here---------------end--------------->8---

and try it again?  IIUC, `latexenc-find-file-coding-system' isn't
prepared to deal with `TeX-master' set to a string top-level.  My change
above adds that.

Best, Arash