Skip to content

Instantly share code, notes, and snippets.

@bradneuman
Created July 3, 2014 05:07
Show Gist options
  • Save bradneuman/6e014671ee142502336d to your computer and use it in GitHub Desktop.
Save bradneuman/6e014671ee142502336d to your computer and use it in GitHub Desktop.
Simple function to find files in a project from a manually specified list of projects
;; a list of root directories for projects. used for find-greping. Put
;; your root directories here. No need to start at root
(setq my-project-list '("/partial/path/to/directory"
"/another/directory/path"
"/one_more_path/"))
(defun match-list (string list)
"returns the entry in list which contains the string"
(when list
(let ((matchIndex (string-match (car list) string)))
(if matchIndex
(substring string 0 (+ matchIndex (length (car list))))
(match-list string (cdr list))))))
(defun get-root-dir ()
"attempts to find the root directory of the current project, based on a list of projects above"
(let ((filename (buffer-file-name)))
(if filename
(match-list filename my-project-list)
(message "no file in buffer!"))))
(defun find-in-project-helper (bounds)
(when bounds
(let ((rootDir (get-root-dir))
(needle (buffer-substring (car bounds) (cdr bounds))))
(when rootDir
;; don't find files that are binary or end in '~'
(find-grep (format "find '%s' -type f -and -not -name '*~' -exec grep -InH -e '%s' {} +" rootDir needle))))))
(defun find-in-project ()
"finds region or current word in point"
(interactive)
(if (use-region-p)
(find-in-project-helper (cons (region-beginning) (region-end)))
(find-in-project-helper (bounds-of-thing-at-point 'word))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment