PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/cedet/ede/emacs.el

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