]> code.delx.au - gnu-emacs/blob - lisp/progmodes/project.el
* lisp/progmodes/project.el: Update Commentary.
[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, and a number of public functions: finding the current
24 ;; root, related project directories, and library directories. This
25 ;; list is to be extended in future versions.
26 ;;
27 ;; The goal is to make it easier for Lisp programs to operate on the
28 ;; current project, without having to know which package handles
29 ;; detection of that project type, parsing its config files, etc.
30
31 ;;; Code:
32
33 (require 'cl-generic)
34
35 (defvar project-find-functions (list #'project-try-vc)
36 "Special hook to find the project containing a given directory.
37 Each functions on this hook is called in turn with one
38 argument (the directory) and should return either nil to mean
39 that it is not applicable, or a project instance.")
40
41 ;; FIXME: Using the current approach, we don't have access to the
42 ;; "library roots" of language A from buffers of language B, which
43 ;; seems desirable in multi-language projects, at least for some
44 ;; potential uses, like "jump to a file in project or library".
45 ;;
46 ;; We can add a second argument to this function: a file extension, or
47 ;; a language name. Some projects will know the set of languages used
48 ;; in them; for others, like VC-based projects, we'll need
49 ;; auto-detection. I see two options:
50 ;;
51 ;; - That could be implemented as a separate second hook, with a
52 ;; list of functions that return file extensions.
53 ;;
54 ;; - This variable will be turned into a hook with "append" semantics,
55 ;; and each function in it will perform auto-detection when passed
56 ;; nil instead of an actual file extension. Then this hook will, in
57 ;; general, be modified globally, and not from major mode functions.
58 (defvar project-library-roots-function 'etags-library-roots
59 "Function that returns a list of library roots.
60
61 It should return a list of directories that contain source files
62 related to the current buffer. Depending on the language, it
63 should include the headers search path, load path, class path,
64 and so on.
65
66 The directory names should be absolute. Used in the default
67 implementation of `project-library-roots'.")
68
69 ;;;###autoload
70 (defun project-current (&optional maybe-prompt dir)
71 "Return the project instance in DIR or `default-directory'.
72 When no project found in DIR, and MAYBE-PROMPT is non-nil, ask
73 the user for a different directory to look in."
74 (unless dir (setq dir default-directory))
75 (let ((pr (project--find-in-directory dir)))
76 (cond
77 (pr)
78 (maybe-prompt
79 (setq dir (read-directory-name "Choose the project directory: " dir nil t)
80 pr (project--find-in-directory dir))
81 (unless pr
82 (user-error "No project found in `%s'" dir))))
83 pr))
84
85 (defun project--find-in-directory (dir)
86 (run-hook-with-args-until-success 'project-find-functions dir))
87
88 ;; FIXME: Add MODE argument, like in `ede-source-paths'?
89 (cl-defgeneric project-library-roots (project)
90 "Return the list of library roots for PROJECT.
91
92 It's the list of directories outside of the project that contain
93 related source files.
94
95 Project-specific version of `project-library-roots-function',
96 which see. Unless it knows better, a specialized implementation
97 should use the value returned by that function."
98 (project-subtract-directories
99 (project-combine-directories
100 (funcall project-library-roots-function))
101 (project-roots project)))
102
103 (cl-defgeneric project-roots (project)
104 "Return the list of directory roots belonging to the current project.
105
106 Most often it's just one directory, which contains the project
107 file and everything else in the project. But in more advanced
108 configurations, a project can span multiple directories.
109
110 The rule of tumb for whether to include a directory here, and not
111 in `project-library-roots', is whether its contents are meant to
112 be edited together with the rest of the project.
113
114 The directory names should be absolute.")
115
116 (cl-defgeneric project-ignores (_project _dir)
117 "Return the list of glob patterns to ignore inside DIR.
118 Patterns can match both regular files and directories.
119 To root an entry, start it with `./'. To match directories only,
120 end it with `/'. DIR must be one of `project-roots' or
121 `project-library-roots'."
122 (require 'grep)
123 (defvar grep-find-ignored-files)
124 (nconc
125 (mapcar
126 (lambda (dir)
127 (concat dir "/"))
128 vc-directory-exclusion-list)
129 grep-find-ignored-files))
130
131 (defgroup project-vc nil
132 "Project implementation using the VC package."
133 :group 'tools)
134
135 (defcustom project-vc-library-roots nil
136 "List ot directories to include in `project-library-roots'.
137 The file names can be absolute, or relative to the project root."
138 :type '(repeat file)
139 :safe 'listp)
140
141 (defcustom project-vc-ignores nil
142 "List ot patterns to include in `project-ignores'."
143 :type '(repeat string)
144 :safe 'listp)
145
146 (defun project-try-vc (dir)
147 (let* ((backend (ignore-errors (vc-responsible-backend dir)))
148 (root (and backend (ignore-errors
149 (vc-call-backend backend 'root dir)))))
150 (and root (cons 'vc root))))
151
152 (cl-defmethod project-roots ((project (head vc)))
153 (list (cdr project)))
154
155 (cl-defmethod project-library-roots ((project (head vc)))
156 (project-subtract-directories
157 (project-combine-directories
158 (append
159 (let ((root (cdr project)))
160 (mapcar
161 (lambda (dir) (file-name-as-directory (expand-file-name dir root)))
162 (project--value-in-dir 'project-vc-library-roots root)))
163 (funcall project-library-roots-function)))
164 (project-roots project)))
165
166 (cl-defmethod project-ignores ((project (head vc)) dir)
167 (let* ((root (cdr project))
168 backend)
169 (append
170 (when (file-equal-p dir root)
171 (setq backend (vc-responsible-backend root))
172 (mapcar
173 (lambda (entry)
174 (if (string-match "\\`/" entry)
175 (replace-match "./" t t entry)
176 entry))
177 (vc-call-backend backend 'ignore-completion-table root)))
178 (project--value-in-dir 'project-vc-ignores root)
179 (cl-call-next-method))))
180
181 (defun project-combine-directories (&rest lists-of-dirs)
182 "Return a sorted and culled list of directory names.
183 Appends the elements of LISTS-OF-DIRS together, removes
184 non-existing directories, as well as directories a parent of
185 whose is already in the list."
186 (let* ((dirs (sort
187 (mapcar
188 (lambda (dir)
189 (file-name-as-directory (expand-file-name dir)))
190 (apply #'append lists-of-dirs))
191 #'string<))
192 (ref dirs))
193 ;; Delete subdirectories from the list.
194 (while (cdr ref)
195 (if (string-prefix-p (car ref) (cadr ref))
196 (setcdr ref (cddr ref))
197 (setq ref (cdr ref))))
198 (cl-delete-if-not #'file-exists-p dirs)))
199
200 (defun project-subtract-directories (files dirs)
201 "Return a list of elements from FILES that are outside of DIRS.
202 DIRS must contain directory names."
203 ;; Sidestep the issue of expanded/abbreviated file names here.
204 (cl-set-difference files dirs :test #'file-in-directory-p))
205
206 (defun project--value-in-dir (var dir)
207 (with-temp-buffer
208 (setq default-directory dir)
209 (hack-dir-local-variables-non-file-buffer)
210 (symbol-value var)))
211
212 (declare-function grep-read-files "grep")
213 (declare-function xref-collect-matches "xref")
214 (declare-function xref--show-xrefs "xref")
215
216 ;;;###autoload
217 (defun project-find-regexp (regexp)
218 "Find all matches for REGEXP in the current project.
219 With \\[universal-argument] prefix, you can specify the directory
220 to search in, and the file name pattern to search for."
221 (interactive (list (project--read-regexp)))
222 (let* ((pr (project-current t))
223 (dirs (if current-prefix-arg
224 (list (read-directory-name "Base directory: "
225 nil default-directory t))
226 (project-roots pr))))
227 (project--find-regexp-in dirs regexp pr)))
228
229 ;;;###autoload
230 (defun project-or-libraries-find-regexp (regexp)
231 "Find all matches for REGEXP in the current project or libraries.
232 With \\[universal-argument] prefix, you can specify the file name
233 pattern to search for."
234 (interactive (list (project--read-regexp)))
235 (let* ((pr (project-current t))
236 (dirs (append
237 (project-roots pr)
238 (project-library-roots pr))))
239 (project--find-regexp-in dirs regexp pr)))
240
241 (defun project--read-regexp ()
242 (defvar xref-identifier-at-point-function)
243 (require 'xref)
244 (read-regexp "Find regexp"
245 (funcall xref-identifier-at-point-function)))
246
247 (defun project--find-regexp-in (dirs regexp project)
248 (require 'grep)
249 (let* ((files (if current-prefix-arg
250 (grep-read-files regexp)
251 "*"))
252 (xrefs (cl-mapcan
253 (lambda (dir)
254 (xref-collect-matches regexp files dir
255 (project-ignores project dir)))
256 dirs)))
257 (unless xrefs
258 (user-error "No matches for: %s" regexp))
259 (xref--show-xrefs xrefs nil)))
260
261 (provide 'project)
262 ;;; project.el ends here