Search code examples
emacselispauctex

If I open doc.foo file, I want emacs to look for and open doc.bar file in the same folder


Specifically this problem arises when working in LaTeX (auctex) for me, but I think it must have a general emacs solution.

To every doc.tex file, I have an associated and oft-edited doc.sty file in the same folder.

Is there a way that whenever I open the doc.tex file I can have emacs open the doc.sty file in that folder? I'm not proficient at all in elisp, so something very simple---it doesn't need to be robust code: it can work on the assumption that both files are named doc.* and that both exist.


Solution

  • Take a look at the commentary for:
    M-x find-library RET find-file RET

    It's not precisely what you asked for, but it is a built-in solution for opening related files. Just bind a key to ff-find-other-file (or ff-find-related-file if you prefer that alias), and you can switch back and forth between the two files easily.

    In particular, see:

    • C-hv ff-other-file-alist RET
    • C-hv ff-search-directories RET

    So something like this:

    (add-hook 'latex-mode-hook 'my-latex-mode-hook)
    
    (defun my-latex-mode-hook ()
      "My LaTeX customisations."
      (setq ff-search-directories '(".")
            ff-other-file-alist  '(("\\.tex$" (".sty"))
                                   ("\\.sty$" (".tex"))))
      (local-set-key (kbd "C-c f") 'ff-find-other-file))