9f7101e69f5fd1ff7c7430e658cd548e7505bd49
[emacs.git] / .emacs.d / elisp / find-file-in-project / find-file-in-project.el
1 ;;; find-file-in-project.el --- Find files in a project quickly.
2
3 ;; Copyright (C) 2006-2009, 2011-2012
4 ;; Phil Hagelberg, Doug Alcorn, and Will Farrington
5
6 ;; Author: Phil Hagelberg, Doug Alcorn, and Will Farrington
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/FindFileInProject
8 ;; Git: git://github.com/technomancy/find-file-in-project.git
9 ;; Version: 20140221.557
10 ;; X-Original-Version: 3.2
11 ;; Created: 2008-03-18
12 ;; Keywords: project, convenience
13 ;; EmacsWiki: FindFileInProject
14
15 ;; This file is NOT part of GNU Emacs.
16
17 ;;; License:
18
19 ;; This program is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 3, or (at your option)
22 ;; any later version.
23 ;;
24 ;; This program is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
28 ;;
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs; see the file COPYING. If not, write to the
31 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
33
34 ;;; Commentary:
35
36 ;; This library provides a couple methods for quickly finding any file
37 ;; in a given project. It depends on GNU find.
38
39 ;; A project is found by searching up the directory tree until a file
40 ;; is found that matches `ffip-project-file'. (".git" by default.)
41 ;; You can set `ffip-project-root-function' to provide an alternate
42 ;; function to search for the project root. By default, it looks only
43 ;; for files whose names match `ffip-patterns',
44
45 ;; If you have so many files that it becomes unwieldy, you can set
46 ;; `ffip-find-options' to a string which will be passed to the `find'
47 ;; invocation in order to exclude irrelevant subdirectories. For
48 ;; instance, in a Ruby on Rails project, you may be interested in all
49 ;; .rb files that don't exist in the "vendor" directory. In that case
50 ;; you could set `ffip-find-options' to "-not -regex \".*vendor.*\"".
51
52 ;; All these variables may be overridden on a per-directory basis in
53 ;; your .dir-locals.el. See (info "(Emacs) Directory Variables") for
54 ;; details.
55
56 ;; Recommended binding: (global-set-key (kbd "C-x f") 'find-file-in-project)
57
58 ;;; TODO:
59
60 ;; Add compatibility with BSD find (PDI; I can't virtualize OS X)
61
62 ;;; Code:
63
64 (require 'cl)
65
66 (defvar ffip-project-file ".git"
67 "The file that should be used to define a project root.
68
69 May be set using .dir-locals.el. Checks each entry if set to a list.")
70
71 (defvar ffip-patterns
72 '("*.html" "*.org" "*.txt" "*.md" "*.el" "*.clj" "*.py" "*.rb" "*.js" "*.pl"
73 "*.sh" "*.erl" "*.hs" "*.ml")
74 "List of patterns to look for with `find-file-in-project'.")
75
76 (defvar ffip-prune-patterns
77 '(".git")
78 "List of directory patterns to not decend into when listing files in `find-file-in-project'.")
79
80 (defvar ffip-find-options ""
81 "Extra options to pass to `find' when using `find-file-in-project'.
82
83 Use this to exclude portions of your project: \"-not -regex \\\".*svn.*\\\"\".")
84
85 (defvar ffip-project-root nil
86 "If non-nil, overrides the project root directory location.")
87
88 (defvar ffip-project-root-function nil
89 "If non-nil, this function is called to determine the project root.
90
91 This overrides variable `ffip-project-root' when set.")
92
93 (defvar ffip-limit 512
94 "Limit results to this many files.")
95
96 (defvar ffip-full-paths nil
97 "If non-nil, show fully project-relative paths.")
98
99 (defun ffip-project-root ()
100 "Return the root of the project."
101 (let ((project-root (or ffip-project-root
102 (if (functionp ffip-project-root-function)
103 (funcall ffip-project-root-function)
104 (if (listp ffip-project-file)
105 (some (apply-partially 'locate-dominating-file
106 default-directory)
107 ffip-project-file)
108 (locate-dominating-file default-directory
109 ffip-project-file))))))
110 (or project-root
111 (progn (message "No project was defined for the current file.")
112 nil))))
113
114 (defun ffip-uniqueify (file-cons)
115 "Set the car of FILE-CONS to include the directory name plus the file name."
116 (setcar file-cons
117 (concat (cadr (reverse (split-string (cdr file-cons) "/"))) "/"
118 (car file-cons))))
119
120 (defun ffip-join-patterns ()
121 "Turn `ffip-patterns' into a string that `find' can use."
122 (mapconcat (lambda (pat) (format "-name \"%s\"" pat))
123 ffip-patterns " -or "))
124
125 (defun ffip-prune-patterns ()
126 "Turn `ffip-prune-patterns' into a string that `find' can use."
127 (mapconcat (lambda (pat) (format "-name \"%s\"" pat))
128 ffip-prune-patterns " -or "))
129
130
131 (defun ffip-project-files ()
132 "Return an alist of all filenames in the project and their path.
133
134 Files with duplicate filenames are suffixed with the name of the
135 directory they are found in so that they are unique."
136 (let ((file-alist nil)
137 (root (expand-file-name (or ffip-project-root (ffip-project-root)
138 (error "No project root found")))))
139 (mapcar (lambda (file)
140 (if ffip-full-paths
141 (cons (substring (expand-file-name file) (length root))
142 (expand-file-name file))
143 (let ((file-cons (cons (file-name-nondirectory file)
144 (expand-file-name file))))
145 (when (assoc (car file-cons) file-alist)
146 (ffip-uniqueify (assoc (car file-cons) file-alist))
147 (ffip-uniqueify file-cons))
148 (add-to-list 'file-alist file-cons)
149 file-cons)))
150 (split-string (shell-command-to-string
151 (format "find %s -type d -a \\( %s \\) -prune -o -type f \\( %s \\) -print %s | head -n %s"
152 root (ffip-prune-patterns) (ffip-join-patterns)
153 ffip-find-options ffip-limit))))))
154
155 ;;;###autoload
156 (defun find-file-in-project ()
157 "Prompt with a completing list of all files in the project to find one.
158
159 The project's scope is defined as the first directory containing
160 an `.emacs-project' file. You can override this by locally
161 setting the variable `ffip-project-root'."
162 (interactive)
163 (let* ((project-files (ffip-project-files))
164 (files (mapcar 'car project-files))
165 (file (if (and (boundp 'ido-mode) ido-mode)
166 (ido-completing-read "Find file in project: " files)
167 (completing-read "Find file in project: " files))))
168 (find-file (cdr (assoc file project-files)))))
169
170 ;;;###autoload
171 (defalias 'ffip 'find-file-in-project)
172
173 ;; safe locals
174 ;;;###autoload
175 (progn
176 (put 'ffip-patterns 'safe-local-variable 'listp)
177 (put 'ffip-find-options 'safe-local-variable 'stringp)
178 (put 'ffip-project-file 'safe-local-variable 'stringp)
179 (put 'ffip-project-root 'safe-local-variable 'stringp)
180 (put 'ffip-project-root-function 'safe-local-variable 'functionp)
181 (put 'ffip-limit 'safe-local-variable 'integerp))
182
183 (provide 'find-file-in-project)
184 ;;; find-file-in-project.el ends here