elpa-find-file function

· :Elisp:Emacs:

While researching internals of agent-shell I needed to quickly visit Elisp files by name. Here's the helper, which takes the file name and finds it within installed packages.

Examples:

  • (elpa-find-file "agent-shell.el")
  • (elpa-find-file "avy.el")
(defun elpa-find-file (pattern)
  "Open first file matching PATTERN in the elpa directory.
Error if multiple versions of the same package match."
  (interactive "sFile: ")
  (let* ((elpa-dir (expand-file-name "elpa" user-emacs-directory))
         (files (thread-first
                  "rg --files %s | grep -F \"%s\" | grep -v '\\.elc$'"
                  (format (shell-quote-argument elpa-dir) pattern)
                  shell-command-to-string
                  split-string))
         (pkgs (mapcar (lambda (f)
                         (let ((dir (cadr (split-string f "/elpa/"))))
                           (replace-regexp-in-string
                            "-[0-9].*?/.*\\'" "" dir)))
                       files))
         (dups (seq-filter (lambda (p)
                             (> (cl-count p pkgs :test #'string=) 1))
                           (seq-uniq pkgs))))
    (when dups
      (user-error "Multiple versions found for: %s — clean up %s"
                  (string-join dups ", ") elpa-dir))
    (unless files
      (user-error "file %s not found" pattern))
    (find-file (cl-first files))))
Support to jumping to a :line:column

Okay, almost immediately I wanted something like this - (elpa-find-file "agent-shell.el:4616"), which is essentially what visit-source.el does. But, it turned out better not integrate visit-source, but implement parsing of line directly in the function.

So, version 2:

(defun elpa-find-file (pattern)
  "Open file matching PATTERN in the elpa directory.
PATTERN may include a trailing :LINE or :LINE:COL.
Error if multiple versions of the same package match."
  (interactive "sFile: ")
  (let* ((parts (split-string pattern ":"))
         (name (car parts))
         (line (and (nth 1 parts) (string-to-number (nth 1 parts))))
         (col (and (nth 2 parts) (string-to-number (nth 2 parts))))
         (elpa-dir (expand-file-name "elpa" user-emacs-directory))
         (files (thread-first
                  "rg --files %s | grep -F \"%s\" | grep -v '\\.elc$'"
                  (format (shell-quote-argument elpa-dir) name)
                  shell-command-to-string
                  split-string))
         (pkgs (mapcar (lambda (f)
                         (let ((dir (cadr (split-string f "/elpa/"))))
                           (replace-regexp-in-string
                            "-[0-9].*?/.*\\'" "" dir)))
                       files))
         (dups (seq-filter (lambda (p)
                             (> (cl-count p pkgs :test #'string=) 1))
                           (seq-uniq pkgs))))
    (when dups
      (user-error "Multiple versions found for: %s — clean up %s"
                  (string-join dups ", ") elpa-dir))
    (unless files
      (user-error "file %s not found" name))
    (find-file (cl-first files))
    (when line
      (goto-char (point-min))
      (forward-line (1- line))
      (when col (forward-char (1- col))))))

visit-source.el

· :Emacs:Elisp:

visit-source.el is an utility Emacs package to quickly go to a file at point (under cursor). I bind (keymap-global-set "H-RET" #'visit-sourse) and can quickly go to files with line precision from anywhere, including log outputs.

Installation

grab a file visit-source.el, put it somewhere on load-path, bind to a key – I have it within ffap package:

(use-package ffap :ensure nil
  :config
  (require 'visit-source)
  ;;(setq ffap-file-finder #'visit-source)
  (setq ffap-file-finder #'find-file)
  :bind (("H-<return>" . visit-source)))
Usage

In any buffer put a point on a file-looking string and call {M-x visit-source RET} (or {H-<return>} for me).

File-looking string can be:

  • a filename
  • a filename with lines (as in exception or log output)
History

When I first find it on r/emacs comment, it was much simpler version and was intended as drop-in replacement for find-file. Then I added project awareness, so you can visit-source on a file Pavels-Emacs-Configuration-v3.org by filename, from anywhere in the project.

Initially I adopted visit-source instead of find-file {C-x C-f} but then switched to find-file-at-point that has a bit different semantics and kept visit-source at {H-RET} separately.

visit-source v0.3.0

visit-source worked in most cases, but still had some rough edges, like didn't parse when path at point ends with dots, so text like ../Pavels-Emacs-Configuration-v3.org. didn't parse, so I had to manually end sentences with "../Pavels-Emacs-Configuration-v3.org file." or "../nv directory." Yesterday I bumped into the need to visit arbitrary Org Mode link. {C-c C-o} runs OS open on it, so log files were opening with TextEdit.app for me. Surely I can reconfigure it in macOS level to associate Emacs with log files (via UI?); but visit-source looks the way for it.

Given personal tooling reneissance, with Claude Code on <2026-05-06 Wed> I asked it to refactor the code - extract helper pure functions, implement fixes, support Org Mode links, get rid of trailing punctuation, add tests.

So meet visit-source v0.3.0 – visit-source.el!