Function: ffap-latex-mode

ffap-latex-mode is a byte-compiled function defined in ffap.el.gz.

Signature

(ffap-latex-mode NAME)

Documentation

ffap function suitable for latex buffers.

This uses the program kpsewhich if available. In this case, the variable ffap-latex-guess-rules is used for building a filename out of NAME.

Source Code

;; Defined in /usr/src/emacs/lisp/ffap.el.gz
(defun ffap-latex-mode (name)
  "`ffap' function suitable for latex buffers.
This uses the program kpsewhich if available.  In this case, the
variable `ffap-latex-guess-rules' is used for building a filename
out of NAME."
  (cond ((file-exists-p name)
         name)
        ((not (executable-find "kpsewhich"))
         (ffap-tex-init)
         (ffap-locate-file name '(".cls" ".sty" ".tex" "") ffap-tex-path))
        (t
         (let ((curbuf (current-buffer))
               (guess-rules ffap-latex-guess-rules)
               (preferred-suffix-rules '(("input" . ".tex")
                                         ("include" . ".tex")
                                         ("usepackage" . ".sty")
                                         ("RequirePackageWithOptions" . ".sty")
                                         ("RequirePackage" . ".sty")
                                         ("documentclass" . ".cls")
                                         ("documentstyle" . ".cls")
                                         ("LoadClass" . ".cls")
                                         ("LoadClassWithOptions" . ".cls")
                                         ("bibliography" . ".bib")
                                         ("addbibresource" . ""))))
           ;; We now add preferred suffix in front of suffixes.
           (when
               ;; The condition is essentially:
               ;; (assoc (TeX-current-macro)
               ;;        (mapcar 'car preferred-suffix-rules))
               ;; but (TeX-current-macro) can take time, so we just
               ;; check if one of the `car' in preferred-suffix-rules
               ;; is found before point on the current line.  It
               ;; should cover most cases.
               (save-excursion
                 (re-search-backward (regexp-opt
                                      (mapcar 'car preferred-suffix-rules))
                                     (line-beginning-position)
                                     t))
             (push (cons "" (cdr (assoc (match-string 0) ; i.e. "(TeX-current-macro)"
                                        preferred-suffix-rules)))
                   guess-rules))
           (with-temp-buffer
             (let ((process-environment (buffer-local-value
                                         'process-environment curbuf))
                   (exec-path (buffer-local-value 'exec-path curbuf)))
               (apply #'call-process "kpsewhich" nil t nil
                      (mapcar (lambda (rule)
                                (concat (car rule) name (cdr rule)))
                              guess-rules)))
             ;; Starting with TeXlive 2025, kpsewhich returns blank
             ;; lines when multiple filenames are given and a given file
             ;; is not found, so we have to go to the first non-blank
             ;; line in order to find a file-path (bug#79397):
             (goto-char (point-min))
             (skip-chars-forward "\r\n")
             (when (< (point) (line-end-position))
               (buffer-substring (point) (line-end-position))))))))