Skip to content

Commit

Permalink
Add imenu support
Browse files Browse the repository at this point in the history
This changeset sets the buffer-local variable
`imenu-create-index-function' if `rg-group-result` is enabled,
configuring Imenu to list all the matched files and hence allowing to
quickly jump to them.

Closes dajva#136
  • Loading branch information
nbarrientos committed Apr 23, 2022
1 parent 444a8cc commit fcc5e36
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions rg-result.el
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,17 @@ Commands:
(setq rg-cur-search search)
(rg-history-push (rg-search-copy rg-cur-search)
rg-search-history)
(rg-maybe-show-header))
(rg-maybe-show-header)
(rg-configure-imenu))

(defun rg-recompile ()
"Rerun the current search."
(interactive)
(let ((rg-recompile t))
(recompile))
(hack-dir-local-variables-non-file-buffer)
(rg-maybe-show-header))
(rg-maybe-show-header)
(rg-configure-imenu))

(defun rg-rerun (&optional no-history)
"Run `recompile' with `compilation-arguments' taken from `rg-cur-search'.
Expand Down Expand Up @@ -763,6 +765,24 @@ previous file with grouped matches."
(rg-rerun 'no-history))
(message "No more history elements for forward.")))

(defun rg-configure-imenu ()
"Add files with matches to imenu if rg-group-result is enabled."
(when rg-group-result
(setq imenu-create-index-function
(lambda ()
(goto-char (point-min))
(let ((elements nil)
(filepath nil)
(nextfile (point-min)))
(while (setq nextfile (rg-navigate-file-message nextfile nil 1))
(save-excursion
(goto-char nextfile)
(skip-chars-forward "File: ")
(setq filepath (buffer-substring (point) (line-end-position))))
(set-text-properties 0 (length filepath) nil filepath)
(setq elements (push (cons filepath nextfile) elements)))
(nreverse elements))))))

(provide 'rg-result)

;; Local Variables:
Expand Down

0 comments on commit fcc5e36

Please sign in to comment.