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