emacs.el 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. ;;; ede/emacs.el --- Special project for Emacs
  2. ;; Copyright (C) 2008-2012 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. (defvar ede-emacs-project-list nil
  33. "List of projects created by option `ede-emacs-project'.")
  34. (defun ede-emacs-file-existing (dir)
  35. "Find a Emacs project in the list of Emacs projects.
  36. DIR is the directory to search from."
  37. (let ((projs ede-emacs-project-list)
  38. (ans nil))
  39. (while (and projs (not ans))
  40. (let ((root (ede-project-root-directory (car projs))))
  41. (when (string-match (concat "^" (regexp-quote root))
  42. (file-name-as-directory dir))
  43. (setq ans (car projs))))
  44. (setq projs (cdr projs)))
  45. ans))
  46. ;;;###autoload
  47. (defun ede-emacs-project-root (&optional dir)
  48. "Get the root directory for DIR."
  49. (when (not dir) (setq dir default-directory))
  50. (let ((case-fold-search t)
  51. (proj (ede-emacs-file-existing dir)))
  52. (if proj
  53. (ede-up-directory (file-name-directory
  54. (oref proj :file)))
  55. ;; No pre-existing project. Let's take a wild-guess if we have
  56. ;; an Emacs project here.
  57. (when (string-match "emacs[^/]*" dir)
  58. (let ((base (substring dir 0 (match-end 0))))
  59. (when (file-exists-p (expand-file-name "src/emacs.c" base))
  60. base))))))
  61. (defun ede-emacs-version (dir)
  62. "Find the Emacs version for the Emacs src in DIR.
  63. Return a tuple of ( EMACSNAME . VERSION )."
  64. (let ((buff (get-buffer-create " *emacs-query*"))
  65. (emacs "Emacs")
  66. (ver ""))
  67. (with-current-buffer buff
  68. (erase-buffer)
  69. (setq default-directory (file-name-as-directory dir))
  70. ;(call-process "egrep" nil buff nil "-n" "-e" "^version=" "Makefile")
  71. (call-process "egrep" nil buff nil "-n" "-e" "AC_INIT" "configure.in")
  72. (goto-char (point-min))
  73. ;(re-search-forward "version=\\([0-9.]+\\)")
  74. (cond
  75. ;; Maybe XEmacs?
  76. ((file-exists-p "version.sh")
  77. (setq emacs "XEmacs")
  78. (insert-file-contents "version.sh")
  79. (goto-char (point-min))
  80. (re-search-forward "emacs_major_version=\\([0-9]+\\)
  81. emacs_minor_version=\\([0-9]+\\)
  82. emacs_beta_version=\\([0-9]+\\)")
  83. (setq ver (concat (match-string 1) "."
  84. (match-string 2) "."
  85. (match-string 3)))
  86. )
  87. ;; Insert other Emacs here...
  88. ;; Vaguely recent version of GNU Emacs?
  89. (t
  90. (insert-file-contents "configure.in")
  91. (goto-char (point-min))
  92. (re-search-forward "AC_INIT(emacs,\\s-*\\([0-9.]+\\)\\s-*)")
  93. (setq ver (match-string 1))
  94. )
  95. )
  96. ;; Return a tuple
  97. (cons emacs ver))))
  98. (defclass ede-emacs-project (ede-project eieio-instance-tracker)
  99. ((tracking-symbol :initform 'ede-emacs-project-list)
  100. )
  101. "Project Type for the Emacs source code."
  102. :method-invocation-order :depth-first)
  103. (defun ede-emacs-load (dir &optional rootproj)
  104. "Return an Emacs Project object if there is a match.
  105. Return nil if there isn't one.
  106. Argument DIR is the directory it is created for.
  107. ROOTPROJ is nil, since there is only one project."
  108. (or (ede-emacs-file-existing dir)
  109. ;; Doesn't already exist, so let's make one.
  110. (let* ((vertuple (ede-emacs-version dir)))
  111. (ede-emacs-project (car vertuple)
  112. :name (car vertuple)
  113. :version (cdr vertuple)
  114. :directory (file-name-as-directory dir)
  115. :file (expand-file-name "src/emacs.c"
  116. dir)))
  117. (ede-add-project-to-global-list this)
  118. )
  119. )
  120. ;;;###autoload
  121. (add-to-list 'ede-project-class-files
  122. (ede-project-autoload "emacs"
  123. :name "EMACS ROOT"
  124. :file 'ede/emacs
  125. :proj-file "src/emacs.c"
  126. :proj-root 'ede-emacs-project-root
  127. :load-type 'ede-emacs-load
  128. :class-sym 'ede-emacs-project
  129. :new-p nil)
  130. t)
  131. (defclass ede-emacs-target-c (ede-target)
  132. ()
  133. "EDE Emacs Project target for C code.
  134. All directories need at least one target.")
  135. (defclass ede-emacs-target-el (ede-target)
  136. ()
  137. "EDE Emacs Project target for Emacs Lisp code.
  138. All directories need at least one target.")
  139. (defclass ede-emacs-target-misc (ede-target)
  140. ()
  141. "EDE Emacs Project target for Misc files.
  142. All directories need at least one target.")
  143. (defmethod initialize-instance ((this ede-emacs-project)
  144. &rest fields)
  145. "Make sure the targets slot is bound."
  146. (call-next-method)
  147. (unless (slot-boundp this 'targets)
  148. (oset this :targets nil)))
  149. ;;; File Stuff
  150. ;;
  151. (defmethod ede-project-root-directory ((this ede-emacs-project)
  152. &optional file)
  153. "Return the root for THIS Emacs project with file."
  154. (ede-up-directory (file-name-directory (oref this file))))
  155. (defmethod ede-project-root ((this ede-emacs-project))
  156. "Return my root."
  157. this)
  158. (defmethod ede-find-subproject-for-directory ((proj ede-emacs-project)
  159. dir)
  160. "Return PROJ, for handling all subdirs below DIR."
  161. proj)
  162. ;;; TARGET MANAGEMENT
  163. ;;
  164. (defun ede-emacs-find-matching-target (class dir targets)
  165. "Find a target that is a CLASS and is in DIR in the list of TARGETS."
  166. (let ((match nil))
  167. (dolist (T targets)
  168. (when (and (object-of-class-p T class)
  169. (string= (oref T :path) dir))
  170. (setq match T)
  171. ))
  172. match))
  173. (defmethod ede-find-target ((proj ede-emacs-project) buffer)
  174. "Find an EDE target in PROJ for BUFFER.
  175. If one doesn't exist, create a new one for this directory."
  176. (let* ((ext (file-name-extension (buffer-file-name buffer)))
  177. (cls (cond ((not ext)
  178. 'ede-emacs-target-misc)
  179. ((string-match "c\\|h" ext)
  180. 'ede-emacs-target-c)
  181. ((string-match "elc?" ext)
  182. 'ede-emacs-target-el)
  183. (t 'ede-emacs-target-misc)))
  184. (targets (oref proj targets))
  185. (dir default-directory)
  186. (ans (ede-emacs-find-matching-target cls dir targets))
  187. )
  188. (when (not ans)
  189. (setq ans (make-instance
  190. cls
  191. :name (file-name-nondirectory
  192. (directory-file-name dir))
  193. :path dir
  194. :source nil))
  195. (object-add-to-list proj :targets ans)
  196. )
  197. ans))
  198. ;;; UTILITIES SUPPORT.
  199. ;;
  200. (defmethod ede-preprocessor-map ((this ede-emacs-target-c))
  201. "Get the pre-processor map for Emacs C code.
  202. All files need the macros from lisp.h!"
  203. (require 'semantic/db)
  204. (let* ((proj (ede-target-parent this))
  205. (root (ede-project-root proj))
  206. (table (semanticdb-file-table-object
  207. (ede-expand-filename root "lisp.h")))
  208. (config (semanticdb-file-table-object
  209. (ede-expand-filename root "config.h")))
  210. filemap
  211. )
  212. (when table
  213. (when (semanticdb-needs-refresh-p table)
  214. (semanticdb-refresh-table table))
  215. (setq filemap (append filemap (oref table lexical-table)))
  216. )
  217. (when config
  218. (when (semanticdb-needs-refresh-p config)
  219. (semanticdb-refresh-table config))
  220. (setq filemap (append filemap (oref config lexical-table)))
  221. )
  222. filemap
  223. ))
  224. (defun ede-emacs-find-in-directories (name base dirs)
  225. "Find NAME is BASE directory sublist of DIRS."
  226. (let ((ans nil))
  227. (while (and dirs (not ans))
  228. (let* ((D (car dirs))
  229. (ed (expand-file-name D base))
  230. (ef (expand-file-name name ed)))
  231. (if (file-exists-p ef)
  232. (setq ans ef)
  233. ;; Not in this dir? How about subdirs?
  234. (let ((dirfile (directory-files ed t))
  235. (moredirs nil)
  236. )
  237. ;; Get all the subdirs.
  238. (dolist (DF dirfile)
  239. (when (and (file-directory-p DF)
  240. (not (string-match "\\.$" DF)))
  241. (push DF moredirs)))
  242. ;; Try again.
  243. (setq ans (ede-emacs-find-in-directories name ed moredirs))
  244. ))
  245. (setq dirs (cdr dirs))))
  246. ans))
  247. (defmethod ede-expand-filename-impl ((proj ede-emacs-project) name)
  248. "Within this project PROJ, find the file NAME.
  249. Knows about how the Emacs source tree is organized."
  250. (let* ((ext (file-name-extension name))
  251. (root (ede-project-root proj))
  252. (dir (ede-project-root-directory root))
  253. (dirs (cond
  254. ((not ext) nil)
  255. ((string-match "h\\|c" ext)
  256. '("src" "lib-src" "lwlib"))
  257. ((string-match "elc?" ext)
  258. '("lisp"))
  259. ((string-match "texi" ext)
  260. '("doc"))
  261. (t nil)))
  262. )
  263. (if (not dirs) (call-next-method)
  264. (ede-emacs-find-in-directories name dir dirs))
  265. ))
  266. (provide 'ede/emacs)
  267. ;; Local variables:
  268. ;; generated-autoload-file: "loaddefs.el"
  269. ;; generated-autoload-load-name: "ede/emacs"
  270. ;; End:
  271. ;;; ede/emacs.el ends here