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