Subject: Re: bug#21194: 2015-06-01; add support for ffap via kpsewhich

From: Mosè Giordano <[email-masked]>
Date: Wed, 5 Aug 2015 20:23:48 +0200

Hi Nicolas!

2015-08-05 19:34 GMT+02:00 Nicolas Richard <[email-masked]>:
> Hi all,
>
> I'm using the following customizations, which I thought I'd share with
> you. As the subject says it, it uses kpsewhich for guessing a filename
> at point. It allows to use M-x ffap with point on "geometry" to find
> geometry.sty, or with point on "memoir" to find memoir.cls.
>
> It is very quick because it calls kpsewhich only once.
>
> I'm not making this into an actual patch because while it works
> fine on my system, it's not ready for inclusion yet. I'm open to
> your ideas to make it better and eventually include it.
>
> ;; a first step is :
> ;; don't allow braces as possible characters in a filename (otherwise
> ;; \input{foo} is seen as a possible filename, instead of just foo)
> ;; (this is because of a recent change in emacs, see commit
> ;; ba6c32b6decaa2a72a3d5f854efd513e8e82c118 for detail)
>
> (push '(latex-mode "--:\\\\$+<>@-Z_[:alpha:]~*?" "<@" "@>;.,!:")
>       ffap-string-at-point-mode-alist)
>
> ;; second step is to extend rules for finding file at point, and use kpsewhich
>
> (defun yf/ffap-latex-mode (name)
>   "ffap function in latex buffers"
>   (if (file-exists-p name)
>       name
>     (let ((curbuf (current-buffer)))
>       (with-temp-buffer
>         (let ((suffixes '(".sty" ".cls" ".ltx" ".tex" "" ".ldf"))
>               (prefixes '("" "beamertheme"
>                           "beamercolortheme"
>                           "beamerfonttheme"
>                           "beamerinnertheme"
>                           "beameroutertheme"))
>               args)
>           (setq args (apply #'append (mapcar
>                                       (lambda (suffix)
>                                         (mapcar
>                                          (lambda (prefix)
>                                            (concat prefix name suffix))
>                                          prefixes))
>                                       suffixes)))
>           (let ((process-environment (buffer-local-value 'process-environment curbuf))
>                 (exec-path (buffer-local-value 'exec-path curbuf)))
>             (apply #'call-process "kpsewhich"  nil  t  nil args))
>           (when (< (point-min) (point-max))
>             (buffer-substring (goto-char (point-min)) (point-at-eol))))))))
>
> ;; use the above
> (advice-add 'ffap-latex-mode :override 'yf/ffap-latex-mode)
>
> Comments welcome!

I like this idea, indeed in my init file I've something similar:

--8<---------------cut here---------------start------------->8---
(defun mg-TeX-kpsewhich-find-file (&optional name)
  "Visit file associated to NAME searching for it with kpsewhich.
If NAME is nil prompt for a file name.  If there is an active
region, use it as initial input.  When it is called with
\\[universal-argument] prefix, visit file in another window, in
the current one otherwise."
  (interactive)
  (if (executable-find "kpsewhich")
      (let* ((fun (if current-prefix-arg 'find-file-other-window 'find-file))
         (default-directory (TeX-master-directory))
         (name (or name (TeX-read-string
                 "File name: "
                 (if (TeX-active-mark)
                 (buffer-substring-no-properties
                  (region-beginning) (region-end))))))
         (file (replace-regexp-in-string
            "[\n\r]*\\'" ""
            (shell-command-to-string (concat "kpsewhich " name)))))
    (if (and (not (zerop (length file))) (file-exists-p file))
        (funcall fun file)
      (message (concat "File " name " not found."))))
    (message "Kpsewhich not available.")))

;; From an original idea of John Wickerson and a first simple implementation of
;; David Carlisle: http://tex.stackexchange.com/q/113376
(defun mg-LaTeX-find-file-at-point ()
  "Visit LaTeX file searching for it with kpsewhich.
File basename is guessed from text around point and its extension
is guessed from current macro.  When it is called with
\\[universal-argument] prefix, visit file in another window, in
the current one otherwise.

See also `mg-TeX-kpsewhich-find-file'."
  (interactive)
  (let* ((file-name-regexp "-~/A-Za-z0-9_.$#%:+")
     ;; Get filename at point.
     (name
      ;; Check whether character at point is a valid file name
      ;; character.
      (if (string-match (concat "[" file-name-regexp "]")
                (string (char-after)))
          (save-excursion
        (skip-chars-backward file-name-regexp)
        (looking-at (concat "\\([" file-name-regexp "]+\\)"))
        (TeX-match-buffer 1))))
     ;; Get current macro once.
     (current-macro (TeX-current-macro))
     ;; Guess file extension based on current macro.
     (extension (cond
             ((or (equal "usepackage" current-macro)
              (equal "RequirePackage" current-macro)
              (equal "RequirePackageWithOptions" current-macro))
              ".sty")
             ((or (equal "documentclass" current-macro)
              (equal "documentstyle" current-macro)
              (equal "LoadClass" current-macro)
              (equal "LoadClassWithOptions" current-macro))
              ".cls")
             ((equal "include" current-macro) ".tex")
             ((equal "input" current-macro)
              ;; `input' macro accepts a file name with extension, in
              ;; that case use an empty but non-nil extension.
              (if (and name (file-name-extension name)) "" ".tex"))
             ((equal "bibliography" current-macro) ".bib")
             ((equal "addbibresource" current-macro) "")
             (t nil))))
    (if (and name extension)
    (mg-TeX-kpsewhich-find-file (concat name extension))
      (message "Cannot guess file name at point."))))
--8<---------------cut here---------------end--------------->8---

This is smarter than your function in the sense that it looks at
current macro to determine the extension (didn't test it, but I think
your implementation would open "article.sty" instead of the more
sensible "article.cls" with point on "article").  You can try to
improve your implementation stealing my code ;-)

Bye,
Mosè