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