0

Is there a function to search for a heading in all org-mode files in all known Projectile projects?

I see that I can search for a string, but can I restrict it to an org-mode heading?;)

2
  • I don't know if Projectile provides a way to list all its projects, but Org mode certainly does not provide a way to list all Org mode files - AFAIK the only exception is the set of Org mode files that contribute to the agenda, which is stored in org-agenda-files. So you will probably have to collect the set of files in other ways. Once you have that set, you can loop over them and e.g. do C-c / m ITEM={foo} on each one: this command creates a sparse tree with the matching headlines unfolded. It's probably OK as an interactive method, but it might be awkward as a bulk method.
    – NickD
    Commented Dec 2, 2022 at 17:37
  • I guess there are various "obvious" approaches. But, I am wondering if you might mean 'across all project files'. Otherwise, it would be interesting to know how you search for a string across all of your projects, and it would be great if you could add that info to your question. Commented Dec 7, 2022 at 10:45

1 Answer 1

2

One way to achieve it is by taking inspiration from the project-find-regexp function. Instead of using files from the project, or from a directory, we give it all org files from all projectile-known-projects:

(defun my-search-heading-across-projects ()
  (interactive)
  (let ((files (seq-filter (lambda (b) (string-match "org$" b))
                           (mapcan (lambda (root)
                                     (mapcar (lambda (f)
                                               (concat root f))
                                             (projectile-project-files root)))
                                   projectile-known-projects))))
    (xref-show-xrefs
     (apply-partially #'project--find-regexp-in-files "^\\*+" files)
     nil)))

This function will list all headings. You can then search for the heading using isearch (or narrow down if you use some completion framework like ivy/helm i.c.w. helm-xref / ivy-xref), and subsequently jump to the 'selected' heading by pressing RET.

7
  • 1
    wow, now that's what I call finding things;). Superb. Thanks a bunch;) Commented Dec 7, 2022 at 21:30
  • This worked fine and I've been using it alot, but suddenly it doesn't work anymore;) It says: "(void-function xref-show-xrefs)". Not sure if I updated something. Commented Feb 16, 2023 at 10:02
  • Does it work after doing (require 'xref)? Commented Feb 16, 2023 at 11:41
  • Hmm, no, still the same Commented Feb 16, 2023 at 11:46
  • Ah, okay. It seems that the function is not available in the built-in xref.el for Emacs version < 29, so you should install xref from elpa to get the newer version (or update your Emacs to version >= 29). Commented Feb 16, 2023 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.