emacs.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ;;; ede/emacs.el --- Special project for Emacs
  2. ;; Copyright (C) 2008-2017 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Provide a special project type just for Emacs, cause Emacs is special.
  18. ;;
  19. ;; Identifies an Emacs project automatically.
  20. ;; Speedy ede-expand-filename based on extension.
  21. ;; Pre-populates the preprocessor map from lisp.h
  22. ;;
  23. ;; ToDo :
  24. ;; * Add "build" options.
  25. ;; * Add texinfo lookup options.
  26. ;; * Add website
  27. (require 'ede)
  28. (declare-function semanticdb-file-table-object "semantic/db")
  29. (declare-function semanticdb-needs-refresh-p "semantic/db")
  30. (declare-function semanticdb-refresh-table "semantic/db")
  31. ;;; Code:
  32. ;; @TODO - get rid of this. Stuck in loaddefs right now.
  33. (defun ede-emacs-project-root (&optional _dir)
  34. "Get the root directory for DIR."
  35. nil)
  36. (defun ede-emacs-version (dir)
  37. "Find the Emacs version for the Emacs src in DIR.
  38. Return a tuple of ( EMACSNAME . VERSION )."
  39. (let ((buff (get-buffer-create " *emacs-query*"))
  40. (configure_ac "configure.ac")
  41. (emacs "Emacs")
  42. (ver ""))
  43. (with-current-buffer buff
  44. (erase-buffer)
  45. (setq default-directory (file-name-as-directory dir))
  46. (cond
  47. ;; Maybe XEmacs?
  48. ((file-exists-p "version.sh")
  49. (setq emacs "XEmacs")
  50. (insert-file-contents "version.sh")
  51. (goto-char (point-min))
  52. (re-search-forward "emacs_major_version=\\([0-9]+\\)
  53. emacs_minor_version=\\([0-9]+\\)
  54. emacs_beta_version=\\([0-9]+\\)")
  55. (setq ver (concat (match-string 1) "."
  56. (match-string 2) "."
  57. (match-string 3)))
  58. )
  59. ((file-exists-p "sxemacs.pc.in")
  60. (setq emacs "SXEmacs")
  61. (insert-file-contents "sxemacs_version.m4")
  62. (goto-char (point-min))
  63. (re-search-forward "m4_define(\\[SXEM4CS_MAJOR_VERSION\\], \\[\\([0-9]+\\)\\])
  64. m4_define(\\[SXEM4CS_MINOR_VERSION\\], \\[\\([0-9]+\\)\\])
  65. m4_define(\\[SXEM4CS_BETA_VERSION\\], \\[\\([0-9]+\\)\\])")
  66. (setq ver (concat (match-string 1) "."
  67. (match-string 2) "."
  68. (match-string 3)))
  69. )
  70. ;; Insert other Emacs here...
  71. ;; Vaguely recent version of GNU Emacs?
  72. ((or (file-exists-p configure_ac)
  73. (file-exists-p (setq configure_ac "configure.in")))
  74. (insert-file-contents configure_ac)
  75. (goto-char (point-min))
  76. (re-search-forward "AC_INIT(\\(?:GNU \\)?[eE]macs,\\s-*\\([0-9.]+\\)\\s-*[,)]")
  77. (setq ver (match-string 1))
  78. )
  79. )
  80. ;; Return a tuple
  81. (cons emacs ver))))
  82. (defclass ede-emacs-project (ede-project)
  83. (
  84. )
  85. "Project Type for the Emacs source code."
  86. :method-invocation-order :depth-first)
  87. (defun ede-emacs-load (dir &optional _rootproj)
  88. "Return an Emacs Project object if there is a match.
  89. Return nil if there isn't one.
  90. Argument DIR is the directory it is created for.
  91. ROOTPROJ is nil, since there is only one project."
  92. ;; Doesn't already exist, so let's make one.
  93. (let* ((vertuple (ede-emacs-version dir)))
  94. (ede-emacs-project
  95. (car vertuple)
  96. :name (car vertuple)
  97. :version (cdr vertuple)
  98. :directory (file-name-as-directory dir)
  99. :file (expand-file-name "src/emacs.c"
  100. dir))))
  101. ;;;###autoload
  102. (ede-add-project-autoload
  103. (make-instance 'ede-project-autoload
  104. :name "EMACS ROOT"
  105. :file 'ede/emacs
  106. :proj-file "src/emacs.c"
  107. :load-type 'ede-emacs-load
  108. :class-sym 'ede-emacs-project
  109. :new-p nil
  110. :safe-p t)
  111. 'unique)
  112. (defclass ede-emacs-target-c (ede-target)
  113. ()
  114. "EDE Emacs Project target for C code.
  115. All directories need at least one target.")
  116. (defclass ede-emacs-target-el (ede-target)
  117. ()
  118. "EDE Emacs Project target for Emacs Lisp code.
  119. All directories need at least one target.")
  120. (defclass ede-emacs-target-misc (ede-target)
  121. ()
  122. "EDE Emacs Project target for Misc files.
  123. All directories need at least one target.")
  124. (cl-defmethod initialize-instance ((this ede-emacs-project)
  125. &rest _fields)
  126. "Make sure the targets slot is bound."
  127. (cl-call-next-method)
  128. (unless (slot-boundp this 'targets)
  129. (oset this :targets nil)))
  130. ;;; File Stuff
  131. ;;
  132. (cl-defmethod ede-project-root-directory ((this ede-emacs-project)
  133. &optional _file)
  134. "Return the root for THIS Emacs project with file."
  135. (ede-up-directory (file-name-directory (oref this file))))
  136. (cl-defmethod ede-project-root ((this ede-emacs-project))
  137. "Return my root."
  138. this)
  139. (cl-defmethod ede-find-subproject-for-directory ((proj ede-emacs-project)
  140. _dir)
  141. "Return PROJ, for handling all subdirs below DIR."
  142. proj)
  143. ;;; TARGET MANAGEMENT
  144. ;;
  145. (defun ede-emacs-find-matching-target (class dir targets)
  146. "Find a target that is a CLASS and is in DIR in the list of TARGETS."
  147. (let ((match nil))
  148. (dolist (T targets)
  149. (when (and (object-of-class-p T class)
  150. (string= (oref T path) dir))
  151. (setq match T)
  152. ))
  153. match))
  154. (cl-defmethod ede-find-target ((proj ede-emacs-project) buffer)
  155. "Find an EDE target in PROJ for BUFFER.
  156. If one doesn't exist, create a new one for this directory."
  157. (let* ((ext (file-name-extension (buffer-file-name buffer)))
  158. (cls (cond ((not ext)
  159. 'ede-emacs-target-misc)
  160. ((string-match "c\\|h" ext)
  161. 'ede-emacs-target-c)
  162. ((string-match "elc?" ext)
  163. 'ede-emacs-target-el)
  164. (t 'ede-emacs-target-misc)))
  165. (targets (oref proj targets))
  166. (dir default-directory)
  167. (ans (ede-emacs-find-matching-target cls dir targets))
  168. )
  169. (when (not ans)
  170. (setq ans (make-instance
  171. cls
  172. :name (file-name-nondirectory
  173. (directory-file-name dir))
  174. :path dir
  175. :source nil))
  176. (object-add-to-list proj :targets ans)
  177. )
  178. ans))
  179. ;;; UTILITIES SUPPORT.
  180. ;;
  181. (cl-defmethod ede-preprocessor-map ((this ede-emacs-target-c))
  182. "Get the pre-processor map for Emacs C code.
  183. All files need the macros from lisp.h!"
  184. (require 'semantic/db)
  185. (let* ((proj (ede-target-parent this))
  186. (root (ede-project-root proj))
  187. (table (semanticdb-file-table-object
  188. (ede-expand-filename root "lisp.h")))
  189. (config (semanticdb-file-table-object
  190. (ede-expand-filename root "config.h")))
  191. filemap
  192. )
  193. (when table
  194. (when (semanticdb-needs-refresh-p table)
  195. (semanticdb-refresh-table table))
  196. (setq filemap (append filemap (oref table lexical-table)))
  197. )
  198. (when config
  199. (when (semanticdb-needs-refresh-p config)
  200. (semanticdb-refresh-table config))
  201. (setq filemap (append filemap (oref config lexical-table)))
  202. )
  203. filemap
  204. ))
  205. (defun ede-emacs-find-in-directories (name base dirs)
  206. "Find NAME is BASE directory sublist of DIRS."
  207. (let ((ans nil))
  208. (while (and dirs (not ans))
  209. (let* ((D (car dirs))
  210. (ed (expand-file-name D base))
  211. (ef (expand-file-name name ed)))
  212. (if (file-exists-p ef)
  213. (setq ans ef)
  214. ;; Not in this dir? How about subdirs?
  215. (let ((dirfile (directory-files ed t))
  216. (moredirs nil)
  217. )
  218. ;; Get all the subdirs.
  219. (dolist (DF dirfile)
  220. (when (and (file-directory-p DF)
  221. (not (string-match "\\.$" DF)))
  222. (push DF moredirs)))
  223. ;; Try again.
  224. (setq ans (ede-emacs-find-in-directories name ed moredirs))
  225. ))
  226. (setq dirs (cdr dirs))))
  227. ans))
  228. (cl-defmethod ede-expand-filename-impl ((proj ede-emacs-project) name)
  229. "Within this project PROJ, find the file NAME.
  230. Knows about how the Emacs source tree is organized."
  231. (let* ((ext (file-name-extension name))
  232. (root (ede-project-root proj))
  233. (dir (ede-project-root-directory root))
  234. (dirs (cond
  235. ((not ext) nil)
  236. ((string-match "h\\|c" ext)
  237. '("src" "lib-src" "lwlib"))
  238. ((string-match "elc?" ext)
  239. '("lisp"))
  240. ((string-match "texi" ext)
  241. '("doc"))
  242. (t nil)))
  243. )
  244. (if (not dirs) (cl-call-next-method)
  245. (ede-emacs-find-in-directories name dir dirs))
  246. ))
  247. ;;; Command Support
  248. ;;
  249. (cl-defmethod project-rescan ((this ede-emacs-project))
  250. "Rescan this Emacs project from the sources."
  251. (let ((ver (ede-emacs-version (ede-project-root-directory this))))
  252. (oset this name (car ver))
  253. (oset this version (cdr ver))
  254. ))
  255. (provide 'ede/emacs)
  256. ;; Local variables:
  257. ;; generated-autoload-file: "loaddefs.el"
  258. ;; generated-autoload-load-name: "ede/emacs"
  259. ;; End:
  260. ;;; ede/emacs.el ends here