]> code.delx.au - gnu-emacs/blob - lisp/progmodes/project.el
c8e48e2275a9d2b983c8b3e95b0ca9afd8033f81
[gnu-emacs] / lisp / progmodes / project.el
1 ;;; project.el --- Operations on the current project -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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.
11
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.
16
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/>.
19
20 ;;; Commentary:
21
22 ;; This file contains generic infrastructure for dealing with
23 ;; projects, some utility functions, and commands using that
24 ;; infrastructure.
25 ;;
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.
29 ;;
30 ;; Infrastructure:
31 ;;
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.
35 ;;
36 ;; Utils:
37 ;;
38 ;; `project-combine-directories' and `project-subtract-directories',
39 ;; mainly for use in the abovementioned generics' implementations.
40 ;;
41 ;; Commands:
42 ;;
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.
45
46 ;;; TODO:
47
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.
52 ;;
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
58 ;; project is using.
59 ;;
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.
65 ;;
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).
73 ;;
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
81 ;; discussion.
82
83 ;;; Code:
84
85 (require 'cl-generic)
86
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.")
92
93 ;;;###autoload
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)))
100 (cond
101 (pr)
102 (maybe-prompt
103 (setq dir (read-directory-name "Choose the project directory: " dir nil t)
104 pr (project--find-in-directory dir))
105 (unless pr
106 (user-error "No project found in `%s'" dir))))
107 pr))
108
109 (defun project--find-in-directory (dir)
110 (run-hook-with-args-until-success 'project-find-functions dir))
111
112 (cl-defgeneric project-roots (project)
113 "Return the list of directory roots of the current project.
114
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.
118
119 The directory names should be absolute.")
120
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.
124
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.
129
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."
133 nil)
134
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'."
141 (require 'grep)
142 (defvar grep-find-ignored-files)
143 (nconc
144 (mapcar
145 (lambda (dir)
146 (concat dir "/"))
147 vc-directory-exclusion-list)
148 grep-find-ignored-files))
149
150 (defgroup project-vc nil
151 "Project implementation using the VC package."
152 :group 'tools)
153
154 (defcustom project-vc-ignores nil
155 "List ot patterns to include in `project-ignores'."
156 :type '(repeat string)
157 :safe 'listp)
158
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".
164 ;;
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:
169 ;;
170 ;; - That could be implemented as a separate second hook, with a
171 ;; list of functions that return file extensions.
172 ;;
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.
177 ;;
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.
187
188 It should return a list of directory roots that contain source
189 files related to the current buffer.
190
191 The directory names should be absolute. Used in the VC project
192 backend implementation of `project-external-roots'.")
193
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))))
199
200 (cl-defmethod project-roots ((project (head vc)))
201 (list (cdr project)))
202
203 (cl-defmethod project-external-roots ((project (head vc)))
204 (project-subtract-directories
205 (project-combine-directories
206 (mapcar
207 #'file-name-as-directory
208 (funcall project-vc-external-roots-function)))
209 (project-roots project)))
210
211 (cl-defmethod project-ignores ((project (head vc)) dir)
212 (let* ((root (cdr project))
213 backend)
214 (append
215 (when (file-equal-p dir root)
216 (setq backend (vc-responsible-backend root))
217 (mapcar
218 (lambda (entry)
219 (if (string-match "\\`/" entry)
220 (replace-match "./" t t entry)
221 entry))
222 (vc-call-backend backend 'ignore-completion-table root)))
223 (project--value-in-dir 'project-vc-ignores root)
224 (cl-call-next-method))))
225
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."
231 (let* ((dirs (sort
232 (mapcar
233 (lambda (dir)
234 (file-name-as-directory (expand-file-name dir)))
235 (apply #'append lists-of-dirs))
236 #'string<))
237 (ref dirs))
238 ;; Delete subdirectories from the list.
239 (while (cdr ref)
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)))
244
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))
250
251 (defun project--value-in-dir (var dir)
252 (with-temp-buffer
253 (setq default-directory dir)
254 (hack-dir-local-variables-non-file-buffer)
255 (symbol-value var)))
256
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")
261
262 ;;;###autoload
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)))
274
275 ;;;###autoload
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))
282 (dirs (append
283 (project-roots pr)
284 (project-external-roots pr))))
285 (project--find-regexp-in dirs regexp pr)))
286
287 (defun project--read-regexp ()
288 (read-regexp "Find regexp"
289 (xref-backend-identifier-at-point (xref-find-backend))))
290
291 (defun project--find-regexp-in (dirs regexp project)
292 (require 'grep)
293 (let* ((files (if current-prefix-arg
294 (grep-read-files regexp)
295 "*"))
296 (xrefs (cl-mapcan
297 (lambda (dir)
298 (xref-collect-matches regexp files dir
299 (project-ignores project dir)))
300 dirs)))
301 (unless xrefs
302 (user-error "No matches for: %s" regexp))
303 (xref--show-xrefs xrefs nil)))
304
305 (provide 'project)
306 ;;; project.el ends here