Skip to content

Instantly share code, notes, and snippets.

@outworlder
Created November 12, 2008 17:26
Show Gist options
  • Save outworlder/24214 to your computer and use it in GitHub Desktop.
Save outworlder/24214 to your computer and use it in GitHub Desktop.
find-file-in-project
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; find-file-in-project
(defvar rinari-project-files-table ())
(defun populate-project-files-table (file)
(if (file-directory-p file)
(mapc 'populate-project-files-table (directory-files file t "^[^\.]"))
(let* ((file-name (file-name-nondirectory file))
(existing-record (assoc file-name project-files-table))
(unique-parts (get-unique-directory-names file (cdr existing-record))))
(if existing-record
(let ((new-key (concat file-name " - " (car unique-parts)))
(old-key (concat (car existing-record) " - " (cadr unique-parts))))
(setf (car existing-record) old-key)
(setq project-files-table (acons new-key file project-files-table)))
(setq project-files-table (acons file-name file project-files-table))))))
(defun get-unique-directory-names (path1 path2)
(let* ((parts1 (and path1 (split-string path1 "/" t)))
(parts2 (and path2 (split-string path2 "/" t)))
(part1 (pop parts1))
(part2 (pop parts2))
(looping t))
(while (and part1 part2 looping)
(if (equal part1 part2)
(setq part1 (pop parts1) part2 (pop parts2))
(setq looping nil)))
(list part1 part2)))
(defun find-file-in-project (file)
(interactive (list (if (functionp 'ido-completing-read)
(ido-completing-read "Find file in project: " (mapcar 'car (project-files)))
(completing-read "Find file in project: " (mapcar 'car (project-files))))))
(find-file (cdr (assoc file project-files-table))))
;; TODO: Add only the lib directory. Currently adding the whole rails root directory.
(defun project-files (&optional file)
; uncomment these lines if it's too slow to load the whole project-files-table
; (when (or (not project-files-table) ; initial load
; (not (string-match (rails-root) (cdar project-files-table)))) ; switched projects
(setq project-files-table nil)
(append
(populate-project-files-table (or file (concat (rails-root) "/app")))
(populate-project-files-table (or file (concat (rails-root) "/lib"))))
project-files-table)
(defvar rinari-config-files
'("config/environment.rb"
"config/database.yml"
"config/routes.rb"
"config/deploy.rb"
"db/schema.rb"))
(defun rinari-find-config-file (file)
(interactive (list (if (functionp 'ido-completing-read)
(ido-completing-read "Find config file: " rinari-config-files)
(completing-read "Find config file: " rinari-config-files))))
(find-file (concat (rails-root) file)))
(defun rails-root (&optional dir)
(or dir (setq dir default-directory))
(if (file-exists-p (concat dir "config/environment.rb"))
dir
(if (equal dir "/")
nil
(rails-root (expand-file-name (concat dir "../"))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment