Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Elisp: Grep in Clojure Project

DZone's Guide to

Elisp: Grep in Clojure Project

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

 Grep'ing within my current project is something I do frequently. It's not much to type, but I do it often enough that I was looking for a keystroke. Most of my projects are Clojure and use Leiningen, thus I'm able to make some pretty safe assumptions. The following snippets allow you to easily grep within your project.


note: both use expand-region to grab the clojure that the cursor is on or immediately after, and grep for whatever was selected.

grep recursively starting at the directory where the project.clj file lives
(defun grep-in-project ()
  (interactive)
  (er/mark-clj-word)
  (let* ((project-root (locate-dominating-file
                         (file-name-directory (buffer-file-name)) "project.clj")))
    (if project-root
        (grep (concat "grep -nH -e "
                      (buffer-substring-no-properties (region-beginning) (region-end))
                      " -R " project-root))
      (message (concat "no project.clj found at or below " (buffer-file-name))))))

(global-set-key (kbd "C-c g") 'grep-in-project)

grep recursively, but allow the user to select the root directory (defaulting to the location of the project.clj file). I often use this one for selecting only the src or test directories of my project.
(defun grep-in (project-root)
  (interactive (list (read-directory-name "Project Root: "
                                          (locate-dominating-file
                                            default-directory
                                            "project.clj"))))
  (er/mark-clj-word)
  (grep (concat "grep -nH -e "
                (buffer-substring-no-properties (region-beginning) (region-end))
                " -R " project-root)))

(global-set-key (kbd "C-c C-g") 'grep-in)



Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Jay Fields, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}