From 64e211c871c6d6a6cdcdf2e0b02100e5a8544172 Mon Sep 17 00:00:00 2001 From: Nacho Barrientos Date: Fri, 22 Apr 2022 10:33:49 +0200 Subject: [PATCH] Add imenu support 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 #136 --- docs/usage.org | 3 +++ rg-result.el | 23 +++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/usage.org b/docs/usage.org index d6acda0..26c1d3e 100644 --- a/docs/usage.org +++ b/docs/usage.org @@ -440,6 +440,9 @@ Visit match in file. :END: + If [[opt:rg-group-result][rg-group-result]] is enabled, the [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Imenu.html][Imenu]] facility is configured to + jump across files. + *** Refine search From the results buffer it's easy to change the search parameters. Some bindings toggle a flag while others allow you to diff --git a/rg-result.el b/rg-result.el index 2cfdf69..69d1516 100644 --- a/rg-result.el +++ b/rg-result.el @@ -571,7 +571,8 @@ 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." @@ -579,7 +580,8 @@ Commands: (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'. @@ -763,6 +765,23 @@ 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-no-properties (point) (line-end-position)))) + (push (cons filepath nextfile) elements)) + (nreverse elements)))))) + (provide 'rg-result) ;; Local Variables: