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