project.el 9.7 KB

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