Function: reftex-TeX-master-file

reftex-TeX-master-file is a byte-compiled function defined in reftex.el.gz.

Signature

(reftex-TeX-master-file)

Documentation

Return the name of the master file associated with the current buffer.

When AUCTeX is loaded, we will use it's more sophisticated method. We also support the default TeX and LaTeX modes by checking for a variable tex-main-file(var)/tex-main-file(fun).

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/reftex.el.gz
(defun reftex-TeX-master-file ()
  "Return the name of the master file associated with the current buffer.
When AUCTeX is loaded, we will use it's more sophisticated method.
We also support the default TeX and LaTeX modes by checking for a
variable `tex-main-file'."
  (with-current-buffer (or (buffer-base-buffer) (current-buffer))
    (let
        ;; Set master to a file name (possibly non-existent), or nil:
        ((master
          (cond
	   ;; Test if we're in a subfile using the subfiles document
	   ;; class, e.g., \documentclass[main.tex]{subfiles}.  It's
	   ;; argument is the main file, however it's not really the
	   ;; master file in `TeX-master-file' or `tex-main-file's
	   ;; sense.  It should be used for references but not for
	   ;; compilation, thus subfiles use a setting of
	   ;; `TeX-master'/`tex-main-file' being themselves.
	   ((save-excursion
              (goto-char (point-min))
              (re-search-forward
               "^[[:space:]]*\\\\documentclass\\[\\([^]]+\\)\\]{subfiles}"
               nil t))
            (match-string-no-properties 1))
           ;; AUCTeX is loaded.  Use its mechanism.
           ((fboundp 'TeX-master-file)
            (condition-case nil
                (TeX-master-file t)
              (error (buffer-file-name))))
           ;; Emacs LaTeX mode
           ((fboundp 'tex-main-file)
            (condition-case nil
                (tex-main-file)
              (error (buffer-file-name))))
           ;; Check the `TeX-master' variable.
           ((boundp 'TeX-master)
            (cond
             ((eq TeX-master t)
              (buffer-file-name))
             ((or (stringp TeX-master) (bufferp TeX-master)) TeX-master)
             (t
              (setq TeX-master (read-file-name "Master file: "
                                               nil nil t nil)))))
           ;; Check the `tex-main-file' variable.
           ((boundp 'tex-main-file)
            ;; This is the variable from the default TeX modes.
            (cond
             ((stringp tex-main-file)
              ;; ok, this must be it
              tex-main-file)
             (t
              ;; In this case, the buffer is its own master.
              (buffer-file-name))))
           ;; We know nothing about master file.  Assume this is a
           ;; master file.
           (t
            (buffer-file-name)))))
      (cond
       ((not (stringp master)))
       ((or (file-exists-p (concat master ".tex"))
            (find-buffer-visiting (concat master ".tex")))
        ;; Ahh, an extra .tex was missing...
        (setq master (concat master ".tex")))
       ((or (file-exists-p master)
            (find-buffer-visiting master))
        ;; We either see the file, or have a buffer on it.  OK.
        )
       (t
        ;; Use buffer file name.
        (setq master (buffer-file-name))))
      (if (stringp master)
          (expand-file-name master)
        (or master (current-buffer))))))