]> code.delx.au - gnu-emacs/blob - lisp/progmodes/project.el
Rename project-library-roots to project-external-roots
[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 aborementioned 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 limit etags to certain files
62 ;; (e.g. have a whitelist, in addition to the blacklist provided by
63 ;; ignores), and/or allow specifying additional tag regexps.
64 ;;
65 ;; * UI for the user to be able to pick the current project for the
66 ;; whole Emacs session, independent of the current directory. Or,
67 ;; in the more advanced case, open a set of projects, and have some
68 ;; project-related commands to use them all. E.g., have a command
69 ;; to search for a regexp across all open projects. Provide a
70 ;; history of projects that were opened in the past (storing it as a
71 ;; list of directories should suffice).
72
73 ;;; Code:
74
75 (require 'cl-generic)
76
77 (defvar project-find-functions (list #'project-try-vc)
78 "Special hook to find the project containing a given directory.
79 Each functions on this hook is called in turn with one
80 argument (the directory) and should return either nil to mean
81 that it is not applicable, or a project instance.")
82
83 ;;;###autoload
84 (defun project-current (&optional maybe-prompt dir)
85 "Return the project instance in DIR or `default-directory'.
86 When no project found in DIR, and MAYBE-PROMPT is non-nil, ask
87 the user for a different directory to look in."
88 (unless dir (setq dir default-directory))
89 (let ((pr (project--find-in-directory dir)))
90 (cond
91 (pr)
92 (maybe-prompt
93 (setq dir (read-directory-name "Choose the project directory: " dir nil t)
94 pr (project--find-in-directory dir))
95 (unless pr
96 (user-error "No project found in `%s'" dir))))
97 pr))
98
99 (defun project--find-in-directory (dir)
100 (run-hook-with-args-until-success 'project-find-functions dir))
101
102 (cl-defgeneric project-roots (project)
103 "Return the list of directory roots of the current project.
104
105 Most often it's just one directory which contains the project
106 build file and everything else in the project. But in more
107 advanced configurations, a project can span multiple directories.
108
109 The directory names should be absolute.")
110
111 ;; FIXME: Add MODE argument, like in `ede-source-paths'?
112 (cl-defgeneric project-external-roots (_project)
113 "Return the list of external roots for PROJECT.
114
115 It's the list of directories outside of the project that are
116 still related to it. If the project deals with source code then,
117 depending on the languages used, this list should include the
118 headers search path, load path, class path, and so on.
119
120 The rule of thumb for whether to include a directory here, and
121 not in `project-roots', is whether its contents are meant to be
122 edited together with the rest of the project."
123 nil)
124
125 (cl-defgeneric project-ignores (_project _dir)
126 "Return the list of glob patterns to ignore inside DIR.
127 Patterns can match both regular files and directories.
128 To root an entry, start it with `./'. To match directories only,
129 end it with `/'. DIR must be one of `project-roots' or
130 `project-external-roots'."
131 (require 'grep)
132 (defvar grep-find-ignored-files)
133 (nconc
134 (mapcar
135 (lambda (dir)
136 (concat dir "/"))
137 vc-directory-exclusion-list)
138 grep-find-ignored-files))
139
140 (defgroup project-vc nil
141 "Project implementation using the VC package."
142 :group 'tools)
143
144 (defcustom project-vc-ignores nil
145 "List ot patterns to include in `project-ignores'."
146 :type '(repeat string)
147 :safe 'listp)
148
149 ;; FIXME: Using the current approach, major modes are supposed to set
150 ;; this variable to a buffer-local value. So we don't have access to
151 ;; the "external roots" of language A from buffers of language B, which
152 ;; seems desirable in multi-language projects, at least for some
153 ;; potential uses, like "jump to a file in project or external dirs".
154 ;;
155 ;; We could add a second argument to this function: a file extension,
156 ;; or a language name. Some projects will know the set of languages
157 ;; used in them; for others, like VC-based projects, we'll need
158 ;; auto-detection. I see two options:
159 ;;
160 ;; - That could be implemented as a separate second hook, with a
161 ;; list of functions that return file extensions.
162 ;;
163 ;; - This variable will be turned into a hook with "append" semantics,
164 ;; and each function in it will perform auto-detection when passed
165 ;; nil instead of an actual file extension. Then this hook will, in
166 ;; general, be modified globally, and not from major mode functions.
167 ;;
168 ;; The second option seems simpler, but the first one has the
169 ;; advantage that the user could override the list of languages used
170 ;; in a project via a directory-local variable, thus skipping
171 ;; languages they're not working on personally (in a big project), or
172 ;; working around problems in language detection (the detection logic
173 ;; might be imperfect for the project in question, or it might work
174 ;; too slowly for the user's taste).
175 (defvar project-vc-external-roots-function (lambda () tags-table-list)
176 "Function that returns a list of external roots.
177
178 It should return a list of directory roots that contain source
179 files related to the current buffer.
180
181 The directory names should be absolute. Used in the VC project
182 backend implementation of `project-external-roots'.")
183
184 (defun project-try-vc (dir)
185 (let* ((backend (ignore-errors (vc-responsible-backend dir)))
186 (root (and backend (ignore-errors
187 (vc-call-backend backend 'root dir)))))
188 (and root (cons 'vc root))))
189
190 (cl-defmethod project-roots ((project (head vc)))
191 (list (cdr project)))
192
193 (cl-defmethod project-external-roots ((project (head vc)))
194 (project-subtract-directories
195 (project-combine-directories
196 (mapcar
197 #'file-name-as-directory
198 (funcall project-vc-external-roots-function)))
199 (project-roots project)))
200
201 (cl-defmethod project-ignores ((project (head vc)) dir)
202 (let* ((root (cdr project))
203 backend)
204 (append
205 (when (file-equal-p dir root)
206 (setq backend (vc-responsible-backend root))
207 (mapcar
208 (lambda (entry)
209 (if (string-match "\\`/" entry)
210 (replace-match "./" t t entry)
211 entry))
212 (vc-call-backend backend 'ignore-completion-table root)))
213 (project--value-in-dir 'project-vc-ignores root)
214 (cl-call-next-method))))
215
216 (defun project-combine-directories (&rest lists-of-dirs)
217 "Return a sorted and culled list of directory names.
218 Appends the elements of LISTS-OF-DIRS together, removes
219 non-existing directories, as well as directories a parent of
220 whose is already in the list."
221 (let* ((dirs (sort
222 (mapcar
223 (lambda (dir)
224 (file-name-as-directory (expand-file-name dir)))
225 (apply #'append lists-of-dirs))
226 #'string<))
227 (ref dirs))
228 ;; Delete subdirectories from the list.
229 (while (cdr ref)
230 (if (string-prefix-p (car ref) (cadr ref))
231 (setcdr ref (cddr ref))
232 (setq ref (cdr ref))))
233 (cl-delete-if-not #'file-exists-p dirs)))
234
235 (defun project-subtract-directories (files dirs)
236 "Return a list of elements from FILES that are outside of DIRS.
237 DIRS must contain directory names."
238 ;; Sidestep the issue of expanded/abbreviated file names here.
239 (cl-set-difference files dirs :test #'file-in-directory-p))
240
241 (defun project--value-in-dir (var dir)
242 (with-temp-buffer
243 (setq default-directory dir)
244 (hack-dir-local-variables-non-file-buffer)
245 (symbol-value var)))
246
247 (declare-function grep-read-files "grep")
248 (declare-function xref-collect-matches "xref")
249 (declare-function xref--show-xrefs "xref")
250 (declare-function xref-backend-identifier-at-point "xref")
251
252 ;;;###autoload
253 (defun project-find-regexp (regexp)
254 "Find all matches for REGEXP in the current project's roots.
255 With \\[universal-argument] prefix, you can specify the directory
256 to search in, and the file name pattern to search for."
257 (interactive (list (project--read-regexp)))
258 (let* ((pr (project-current t))
259 (dirs (if current-prefix-arg
260 (list (read-directory-name "Base directory: "
261 nil default-directory t))
262 (project-roots pr))))
263 (project--find-regexp-in dirs regexp pr)))
264
265 ;;;###autoload
266 (defun project-or-external-find-regexp (regexp)
267 "Find all matches for REGEXP in the project roots or external roots.
268 With \\[universal-argument] prefix, you can specify the file name
269 pattern to search for."
270 (interactive (list (project--read-regexp)))
271 (let* ((pr (project-current t))
272 (dirs (append
273 (project-roots pr)
274 (project-external-roots pr))))
275 (project--find-regexp-in dirs regexp pr)))
276
277 (defun project--read-regexp ()
278 (read-regexp "Find regexp"
279 (xref-backend-identifier-at-point (xref-find-backend))))
280
281 (defun project--find-regexp-in (dirs regexp project)
282 (require 'grep)
283 (let* ((files (if current-prefix-arg
284 (grep-read-files regexp)
285 "*"))
286 (xrefs (cl-mapcan
287 (lambda (dir)
288 (xref-collect-matches regexp files dir
289 (project-ignores project dir)))
290 dirs)))
291 (unless xrefs
292 (user-error "No matches for: %s" regexp))
293 (xref--show-xrefs xrefs nil)))
294
295 (provide 'project)
296 ;;; project.el ends here