Function: tagref--find-refs

tagref--find-refs is a natively compiled function defined in tagref.el.

Signature

(tagref--find-refs TAG-NAME)

Documentation

Find all references to TAG-NAME in the project.

Returns a list of (FILE . LINE-NUMBER) pairs. Uses ripgrep if available, then git grep in git repos, then grep.

Source Code

;; Defined in /nix/store/mnch55hf9yszlb4dvfqhbqf0840v41qh-emacs-packages-deps/share/emacs/site-lisp/tagref.el
(defun tagref--find-refs (tag-name)
  "Find all references to TAG-NAME in the project.
Returns a list of (FILE . LINE-NUMBER) pairs.
Uses ripgrep if available, then git grep in git repos, then grep."
  (when-let ((root (tagref--project-root)))
    (let ((default-directory root)
          (literal-pattern (format "[ref:%s]" tag-name))
          (use-rg (tagref--ripgrep-available-p))
          (use-git (tagref--in-git-repo-p))
          results)
      (with-temp-buffer
        (let ((exit-code
               (cond
                ;; Prefer ripgrep (fastest, respects .gitignore by default)
                (use-rg
                 (call-process "rg" nil t nil
                               "--line-number" "--fixed-strings"
                               "--no-heading" "--with-filename"
                               literal-pattern))
                ;; Git grep in git repos (fast, respects .gitignore)
                (use-git
                 (call-process "git" nil t nil
                               "grep" "-n" "--fixed-strings"
                               literal-pattern))
                ;; Fall back to grep
                (t
                 (call-process "grep" nil t nil
                               "-rn" "--include=*"
                               (format "\\[ref:%s\\]" tag-name) ".")))))
          (when (zerop exit-code)
            (goto-char (point-min))
            ;; rg and git grep output: FILE:LINE:...
            ;; grep output: ./FILE:LINE:...
            (let ((output-regexp (if (or use-rg use-git)
                                     "^\\([^:]+\\):\\([0-9]+\\):"
                                   "^\\./\\([^:]+\\):\\([0-9]+\\):")))
              (while (re-search-forward output-regexp nil t)
                (push (cons (match-string 1)
                            (string-to-number (match-string 2)))
                      results))))))
      (nreverse results))))