TransWikia.com

How can I run imenu in all project files?

Emacs Asked by Robert Zaremba on December 5, 2020

Is there any package which will allow to run the imenu in all project files of a current mode?
More specifically I have a Go project defined by projectile and I would like to search for a symbol in the whole project.
I know about imenu-anywhere – but it only searches through symbols in opened buffers.

2 Answers

My solution is way too hacky:

A file that you want to have Imenu to access is likely to be tracked by Git.

Use find-file-noselect on all those files (I don't know how to detect binary files with Emacs so limiting on VCS-tracked files is better), and accept the risk of hooks and advices, auto-loading functions, etc.

;;;###autoload
(defun open-project-files-in-background ()
  "Open/find all source files in the background which are tracked by Git."
  (let ((default-directory (vc-root-dir)))
    (save-window-excursion
      (dolist (file (split-string (shell-command-to-string "git ls-files")))
        (unless (find-buffer-visiting file)
          (with-current-buffer (find-file-noselect file)
            (if (equal (get major-mode 'derived-mode-parent) 'prog-mode)
                (bury-buffer)
              (kill-buffer))))))))

;;;###autoload
(defun open-project-files-in-background-maybe (&rest _args)
  "Prepare a project-wide `Imenu'."
  (interactive)
  ;; 'vc-root-dir returns nil when not tracked, so checking if (not
  ;; that it is in a list that contains nil) is enough for both
  ;; (non-nil and in the list of fully opened projects)
  (defvar fully-opened-projects '(nil) "List of project paths, each has all tracked files opened.")
  (let ((git-dir (vc-root-dir)))
    (unless (member git-dir fully-opened-projects)
      (open-project-files-in-background)
      (push git-dir fully-opened-projects))))


(advice-add 'imenu-anywhere :before 'open-project-files-in-background-maybe)

Answered by Datool on December 5, 2020

Imenu is for open buffers.

Consider using a command such as find-grep-dired, find-dired, find-name-dired, find-time-dired, or xref-find-definitions.

Answered by Drew on December 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP