Skip to content

Instantly share code, notes, and snippets.

@jdtsmith
Last active July 15, 2025 21:21
Show Gist options
  • Select an option

  • Save jdtsmith/57c9c94bc916b58c0e4ebfc01d300bf1 to your computer and use it in GitHub Desktop.

Select an option

Save jdtsmith/57c9c94bc916b58c0e4ebfc01d300bf1 to your computer and use it in GitHub Desktop.
Redirect xref on elisp to the source-directory
(defun my/elisp-xref-find-def-in-source (sym)
"Find elisp files in the source directory."
(let* ((elisp-xref-find-def-functions nil) ; recurses otherwise
(records (elisp--xref-find-definitions sym)))
(dolist (rec records)
(when-let* ((file (xref-elisp-location-file (xref-item-location rec)))
( (and (stringp file)
(string-match (rx ".el" (? ".gz") eos) file)
(file-in-directory-p file lisp-directory)))
(relname (file-relative-name file lisp-directory))
(src-name (file-name-concat
source-directory "lisp"
(string-trim-right relname "\\.gz")))
( (file-exists-p src-name)))
(setf (xref-elisp-location-file (xref-item-location rec)) src-name)))
records))
(add-to-list 'elisp-xref-find-def-functions 'my/elisp-xref-find-def-in-source)
@jdtsmith
Copy link
Author

jdtsmith commented Jul 15, 2025

If you compile your own emacs, you might prefer to have xref visit elisp files in the original source directory, not in the install directory. This small function arranges for that to happen by translating file paths for (compressed) elisp files from lisp-directory to source-directory/lisp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment