]> code.delx.au - gnu-emacs/blob - lisp/progmodes/project.el
Remove dirs in vc project roots from the the vc project library 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, and a number of public functions: finding the current
24 ;; root, related project directories, search path, etc.
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 ;;; Code:
31
32 (require 'cl-generic)
33
34 (defvar project-find-functions (list #'project-try-vc
35 #'project-ask-user)
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 dir)
71 "Return the project instance in DIR or `default-directory'."
72 (unless dir (setq dir default-directory))
73 (run-hook-with-args-until-success 'project-find-functions dir))
74
75 ;; FIXME: Add MODE argument, like in `ede-source-paths'?
76 (cl-defgeneric project-library-roots (project)
77 "Return the list of library roots for PROJECT.
78
79 It's the list of directories outside of the project that contain
80 related source files.
81
82 Project-specific version of `project-library-roots-function',
83 which see. Unless it knows better, a specialized implementation
84 should use the value returned by that function."
85 (project-subtract-directories
86 (project-combine-directories
87 (funcall project-library-roots-function))
88 (project-roots project)))
89
90 (cl-defgeneric project-roots (project)
91 "Return the list of directory roots belonging to the current project.
92
93 Most often it's just one directory, which contains the project
94 file and everything else in the project. But in more advanced
95 configurations, a project can span multiple directories.
96
97 The rule of tumb for whether to include a directory here, and not
98 in `project-library-roots', is whether its contents are meant to
99 be edited together with the rest of the project.
100
101 The directory names should be absolute.")
102
103 (cl-defgeneric project-ignores (_project _dir)
104 "Return the list of glob patterns to ignore inside DIR.
105 Patterns can match both regular files and directories.
106 To root an entry, start it with `./'. To match directories only,
107 end it with `/'. DIR must be one of `project-roots' or
108 `project-library-roots'."
109 (require 'grep)
110 (defvar grep-find-ignored-files)
111 (nconc
112 (mapcar
113 (lambda (dir)
114 (concat dir "/"))
115 vc-directory-exclusion-list)
116 grep-find-ignored-files))
117
118 (defgroup project-vc nil
119 "Project implementation using the VC package."
120 :group 'tools)
121
122 (defcustom project-vc-library-roots nil
123 "List ot directories to include in `project-library-roots'.
124 The file names can be absolute, or relative to the project root."
125 :type '(repeat file)
126 :safe 'listp)
127
128 (defcustom project-vc-ignores nil
129 "List ot patterns to include in `project-ignores'."
130 :type '(repeat string)
131 :safe 'listp)
132
133 (defun project-try-vc (dir)
134 (let* ((backend (ignore-errors (vc-responsible-backend dir)))
135 (root (and backend (ignore-errors
136 (vc-call-backend backend 'root dir)))))
137 (and root (cons 'vc root))))
138
139 (cl-defmethod project-roots ((project (head vc)))
140 (list (cdr project)))
141
142 (cl-defmethod project-library-roots ((project (head vc)))
143 (project-subtract-directories
144 (project-combine-directories
145 (append
146 (let ((root (cdr project)))
147 (mapcar
148 (lambda (dir) (file-name-as-directory (expand-file-name dir root)))
149 (project--value-in-dir 'project-vc-library-roots root)))
150 (funcall project-library-roots-function)))
151 (project-roots project)))
152
153 (cl-defmethod project-ignores ((project (head vc)) dir)
154 (let* ((root (cdr project))
155 backend)
156 (append
157 (when (file-equal-p dir root)
158 (setq backend (vc-responsible-backend root))
159 (mapcar
160 (lambda (entry)
161 (if (string-match "\\`/" entry)
162 (replace-match "./" t t entry)
163 entry))
164 (vc-call-backend backend 'ignore-completion-table root)))
165 (project--value-in-dir 'project-vc-ignores root)
166 (cl-call-next-method))))
167
168 (defun project-ask-user (dir)
169 (cons 'user (read-directory-name "Project root: " dir nil t)))
170
171 (cl-defmethod project-roots ((project (head user)))
172 (list (cdr project)))
173
174 (defun project-combine-directories (&rest lists-of-dirs)
175 "Return a sorted and culled list of directory names.
176 Appends the elements of LISTS-OF-DIRS together, removes
177 non-existing directories, as well as directories a parent of
178 whose is already in the list."
179 (let* ((dirs (sort
180 (mapcar
181 (lambda (dir)
182 (file-name-as-directory (expand-file-name dir)))
183 (apply #'append lists-of-dirs))
184 #'string<))
185 (ref dirs))
186 ;; Delete subdirectories from the list.
187 (while (cdr ref)
188 (if (string-prefix-p (car ref) (cadr ref))
189 (setcdr ref (cddr ref))
190 (setq ref (cdr ref))))
191 (cl-delete-if-not #'file-exists-p dirs)))
192
193 (defun project-subtract-directories (files dirs)
194 "Return a list of elements from FILES that are outside of DIRS.
195 DIRS must contain directory names."
196 (cl-set-difference files dirs
197 :test (lambda (file dir) (string-prefix-p dir file))))
198
199 (defun project--value-in-dir (var dir)
200 (with-temp-buffer
201 (setq default-directory dir)
202 (hack-dir-local-variables-non-file-buffer)
203 (symbol-value var)))
204
205 (declare-function grep-read-files "grep")
206 (declare-function xref-collect-matches "xref")
207 (declare-function xref--show-xrefs "xref")
208
209 ;;;###autoload
210 (defun project-find-regexp (regexp)
211 "Find all matches for REGEXP in the current project.
212 With \\[universal-argument] prefix, you can specify the directory
213 to search in, and the file name pattern to search for."
214 (interactive (list (project--read-regexp)))
215 (let* ((pr (project-current))
216 (dirs (if current-prefix-arg
217 (list (read-directory-name "Base directory: "
218 nil default-directory t))
219 (project-roots pr))))
220 (project--find-regexp-in dirs regexp pr)))
221
222 ;;;###autoload
223 (defun project-or-libraries-find-regexp (regexp)
224 "Find all matches for REGEXP in the current project or libraries.
225 With \\[universal-argument] prefix, you can specify the file name
226 pattern to search for."
227 (interactive (list (project--read-regexp)))
228 (let* ((pr (project-current))
229 (dirs (append
230 (project-roots pr)
231 (project-library-roots pr))))
232 (project--find-regexp-in dirs regexp pr)))
233
234 (defun project--read-regexp ()
235 (defvar xref-identifier-at-point-function)
236 (require 'xref)
237 (read-regexp "Find regexp"
238 (funcall xref-identifier-at-point-function)))
239
240 (defun project--find-regexp-in (dirs regexp project)
241 (require 'grep)
242 (let* ((files (if current-prefix-arg
243 (grep-read-files regexp)
244 "*"))
245 (xrefs (cl-mapcan
246 (lambda (dir)
247 (xref-collect-matches regexp files dir
248 (project-ignores project dir)))
249 dirs)))
250 (unless xrefs
251 (user-error "No matches for: %s" regexp))
252 (xref--show-xrefs xrefs nil)))
253
254 (provide 'project)
255 ;;; project.el ends here