1 ;;; project.el --- Operations on the current project -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;; This file contains generic infrastructure for dealing with
23 ;; projects, some utility functions, and commands using that
26 ;; The goal is to make it easier for Lisp programs to operate on the
27 ;; current project, without having to know which package handles
28 ;; detection of that project type, parsing its config files, etc.
32 ;; Function `project-current', to determine the current project
33 ;; instance, and 3 (at the moment) generic functions that act on it.
34 ;; This list is to be extended in future versions.
38 ;; `project-combine-directories' and `project-subtract-directories',
39 ;; mainly for use in the abovementioned generics' implementations.
43 ;; `project-find-regexp' and `project-or-external-find-regexp' use the
44 ;; current API, and thus will work in any project that has an adapter.
48 ;; * Commands `project-find-file' and `project-or-external-find-file'.
49 ;; Currently blocked on adding a new completion style that would let
50 ;; the user enter just the base file name (or a part of it), and get
51 ;; it expanded to the absolute file name.
53 ;; * Build tool related functionality. Start with a `project-build'
54 ;; command, which should provide completions on tasks to run, and
55 ;; maybe allow entering some additional arguments. This might
56 ;; be handled better with a separate API, though. Then we won't
57 ;; force every project backend to be aware of the build tool(s) the
60 ;; * Command to (re)build the tag files in all project roots. To that
61 ;; end, we might need to add a way to provide file whitelist
62 ;; wildcards for each root to limit etags to certain files (in
63 ;; addition to the blacklist provided by ignores), and/or allow
64 ;; specifying additional tag regexps.
66 ;; * UI for the user to be able to pick the current project for the
67 ;; whole Emacs session, independent of the current directory. Or,
68 ;; in the more advanced case, open a set of projects, and have some
69 ;; project-related commands to use them all. E.g., have a command
70 ;; to search for a regexp across all open projects. Provide a
71 ;; history of projects that were opened in the past (storing it as a
72 ;; list of directories should suffice).
74 ;; * Support for project-local variables: a UI to edit them, and a
75 ;; utility function to retrieve a value. Probably useless without
76 ;; support in various built-in commands. In the API, we might get
77 ;; away with only adding a `project-configuration-directory' method,
78 ;; defaulting to the project root the current file/buffer is in.
79 ;; And prompting otherwise. How to best mix that with backends that
80 ;; want to set/provide certain variables themselves, is up for
87 (defvar project-find-functions (list #'project-try-vc)
88 "Special hook to find the project containing a given directory.
89 Each functions on this hook is called in turn with one
90 argument (the directory) and should return either nil to mean
91 that it is not applicable, or a project instance.")
94 (defun project-current (&optional maybe-prompt dir)
95 "Return the project instance in DIR or `default-directory'.
96 When no project found in DIR, and MAYBE-PROMPT is non-nil, ask
97 the user for a different directory to look in."
98 (unless dir (setq dir default-directory))
99 (let ((pr (project--find-in-directory dir)))
103 (setq dir (read-directory-name "Choose the project directory: " dir nil t)
104 pr (project--find-in-directory dir))
106 (user-error "No project found in `%s'" dir))))
109 (defun project--find-in-directory (dir)
110 (run-hook-with-args-until-success 'project-find-functions dir))
112 (cl-defgeneric project-roots (project)
113 "Return the list of directory roots of the current project.
115 Most often it's just one directory which contains the project
116 build file and everything else in the project. But in more
117 advanced configurations, a project can span multiple directories.
119 The directory names should be absolute.")
121 ;; FIXME: Add MODE argument, like in `ede-source-paths'?
122 (cl-defgeneric project-external-roots (_project)
123 "Return the list of external roots for PROJECT.
125 It's the list of directories outside of the project that are
126 still related to it. If the project deals with source code then,
127 depending on the languages used, this list should include the
128 headers search path, load path, class path, and so on.
130 The rule of thumb for whether to include a directory here, and
131 not in `project-roots', is whether its contents are meant to be
132 edited together with the rest of the project."
135 (cl-defgeneric project-ignores (_project _dir)
136 "Return the list of glob patterns to ignore inside DIR.
137 Patterns can match both regular files and directories.
138 To root an entry, start it with `./'. To match directories only,
139 end it with `/'. DIR must be one of `project-roots' or
140 `project-external-roots'."
142 (defvar grep-find-ignored-files)
147 vc-directory-exclusion-list)
148 grep-find-ignored-files))
150 (defgroup project-vc nil
151 "Project implementation using the VC package."
154 (defcustom project-vc-ignores nil
155 "List ot patterns to include in `project-ignores'."
156 :type '(repeat string)
159 ;; FIXME: Using the current approach, major modes are supposed to set
160 ;; this variable to a buffer-local value. So we don't have access to
161 ;; the "external roots" of language A from buffers of language B, which
162 ;; seems desirable in multi-language projects, at least for some
163 ;; potential uses, like "jump to a file in project or external dirs".
165 ;; We could add a second argument to this function: a file extension,
166 ;; or a language name. Some projects will know the set of languages
167 ;; used in them; for others, like VC-based projects, we'll need
168 ;; auto-detection. I see two options:
170 ;; - That could be implemented as a separate second hook, with a
171 ;; list of functions that return file extensions.
173 ;; - This variable will be turned into a hook with "append" semantics,
174 ;; and each function in it will perform auto-detection when passed
175 ;; nil instead of an actual file extension. Then this hook will, in
176 ;; general, be modified globally, and not from major mode functions.
178 ;; The second option seems simpler, but the first one has the
179 ;; advantage that the user could override the list of languages used
180 ;; in a project via a directory-local variable, thus skipping
181 ;; languages they're not working on personally (in a big project), or
182 ;; working around problems in language detection (the detection logic
183 ;; might be imperfect for the project in question, or it might work
184 ;; too slowly for the user's taste).
185 (defvar project-vc-external-roots-function (lambda () tags-table-list)
186 "Function that returns a list of external roots.
188 It should return a list of directory roots that contain source
189 files related to the current buffer.
191 The directory names should be absolute. Used in the VC project
192 backend implementation of `project-external-roots'.")
194 (defun project-try-vc (dir)
195 (let* ((backend (ignore-errors (vc-responsible-backend dir)))
196 (root (and backend (ignore-errors
197 (vc-call-backend backend 'root dir)))))
198 (and root (cons 'vc root))))
200 (cl-defmethod project-roots ((project (head vc)))
201 (list (cdr project)))
203 (cl-defmethod project-external-roots ((project (head vc)))
204 (project-subtract-directories
205 (project-combine-directories
207 #'file-name-as-directory
208 (funcall project-vc-external-roots-function)))
209 (project-roots project)))
211 (cl-defmethod project-ignores ((project (head vc)) dir)
212 (let* ((root (cdr project))
215 (when (file-equal-p dir root)
216 (setq backend (vc-responsible-backend root))
219 (if (string-match "\\`/" entry)
220 (replace-match "./" t t entry)
222 (vc-call-backend backend 'ignore-completion-table root)))
223 (project--value-in-dir 'project-vc-ignores root)
224 (cl-call-next-method))))
226 (defun project-combine-directories (&rest lists-of-dirs)
227 "Return a sorted and culled list of directory names.
228 Appends the elements of LISTS-OF-DIRS together, removes
229 non-existing directories, as well as directories a parent of
230 whose is already in the list."
234 (file-name-as-directory (expand-file-name dir)))
235 (apply #'append lists-of-dirs))
238 ;; Delete subdirectories from the list.
240 (if (string-prefix-p (car ref) (cadr ref))
241 (setcdr ref (cddr ref))
242 (setq ref (cdr ref))))
243 (cl-delete-if-not #'file-exists-p dirs)))
245 (defun project-subtract-directories (files dirs)
246 "Return a list of elements from FILES that are outside of DIRS.
247 DIRS must contain directory names."
248 ;; Sidestep the issue of expanded/abbreviated file names here.
249 (cl-set-difference files dirs :test #'file-in-directory-p))
251 (defun project--value-in-dir (var dir)
253 (setq default-directory dir)
254 (hack-dir-local-variables-non-file-buffer)
257 (declare-function grep-read-files "grep")
258 (declare-function xref-collect-matches "xref")
259 (declare-function xref--show-xrefs "xref")
260 (declare-function xref-backend-identifier-at-point "xref")
263 (defun project-find-regexp (regexp)
264 "Find all matches for REGEXP in the current project's roots.
265 With \\[universal-argument] prefix, you can specify the directory
266 to search in, and the file name pattern to search for."
267 (interactive (list (project--read-regexp)))
268 (let* ((pr (project-current t))
269 (dirs (if current-prefix-arg
270 (list (read-directory-name "Base directory: "
271 nil default-directory t))
272 (project-roots pr))))
273 (project--find-regexp-in dirs regexp pr)))
276 (defun project-or-external-find-regexp (regexp)
277 "Find all matches for REGEXP in the project roots or external roots.
278 With \\[universal-argument] prefix, you can specify the file name
279 pattern to search for."
280 (interactive (list (project--read-regexp)))
281 (let* ((pr (project-current t))
284 (project-external-roots pr))))
285 (project--find-regexp-in dirs regexp pr)))
287 (defun project--read-regexp ()
288 (read-regexp "Find regexp"
289 (xref-backend-identifier-at-point (xref-find-backend))))
291 (defun project--find-regexp-in (dirs regexp project)
293 (let* ((files (if current-prefix-arg
294 (grep-read-files regexp)
298 (xref-collect-matches regexp files dir
299 (project-ignores project dir)))
302 (user-error "No matches for: %s" regexp))
303 (xref--show-xrefs xrefs nil)))
306 ;;; project.el ends here