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