1 ;;; ede/emacs.el --- Special project for Emacs
3 ;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;; Provide a special project type just for Emacs, cause Emacs is special.
26 ;; Identifies an Emacs project automatically.
27 ;; Speedy ede-expand-filename based on extension.
28 ;; Pre-populates the preprocessor map from lisp.h
31 ;; * Add "build" options.
32 ;; * Add texinfo lookup options.
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")
41 (defvar ede-emacs-project-list nil
42 "List of projects created by option `ede-emacs-project'.")
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)
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)))
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)))
64 (ede-up-directory (file-name-directory
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))
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*"))
79 (with-current-buffer buff
81 (setq default-directory (file-name-as-directory dir))
82 ;(call-process "egrep" nil buff nil "-n" "-e" "^version=" "Makefile")
83 (call-process "egrep" nil buff nil "-n" "-e" "AC_INIT" "configure.in")
84 (goto-char (point-min))
85 ;(re-search-forward "version=\\([0-9.]+\\)")
88 ((file-exists-p "version.sh")
90 (insert-file-contents "version.sh")
91 (goto-char (point-min))
92 (re-search-forward "emacs_major_version=\\([0-9]+\\)
93 emacs_minor_version=\\([0-9]+\\)
94 emacs_beta_version=\\([0-9]+\\)")
95 (setq ver (concat (match-string 1) "."
99 ;; Insert other Emacs here...
101 ;; Vaguely recent version of GNU Emacs?
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))
112 (defclass ede-emacs-project (ede-project eieio-instance-tracker)
113 ((tracking-symbol :initform 'ede-emacs-project-list)
115 "Project Type for the Emacs source code."
116 :method-invocation-order :depth-first)
118 (defun ede-emacs-load (dir &optional rootproj)
119 "Return an Emacs Project object if there is a match.
120 Return nil if there isn't one.
121 Argument DIR is the directory it is created for.
122 ROOTPROJ is nil, since there is only one project."
123 (or (ede-emacs-file-existing dir)
124 ;; Doesn't already exist, so lets make one.
125 (let* ((vertuple (ede-emacs-version dir)))
126 (ede-emacs-project (car vertuple)
128 :version (cdr vertuple)
129 :directory (file-name-as-directory dir)
130 :file (expand-file-name "src/emacs.c"
132 (ede-add-project-to-global-list this)
136 (defclass ede-emacs-target-c (ede-target)
138 "EDE Emacs Project target for C code.
139 All directories need at least one target.")
141 (defclass ede-emacs-target-el (ede-target)
143 "EDE Emacs Project target for Emacs Lisp code.
144 All directories need at least one target.")
146 (defclass ede-emacs-target-misc (ede-target)
148 "EDE Emacs Project target for Misc files.
149 All directories need at least one target.")
151 (defmethod initialize-instance ((this ede-emacs-project)
153 "Make sure the :file is fully expanded."
155 (unless (slot-boundp this 'targets)
156 (oset this :targets nil)))
160 (defmethod ede-project-root-directory ((this ede-emacs-project)
162 "Return the root for THIS Emacs project with file."
163 (ede-up-directory (file-name-directory (oref this file))))
165 (defmethod ede-project-root ((this ede-emacs-project))
169 (defmethod ede-find-subproject-for-directory ((proj ede-emacs-project)
171 "Return PROJ, for handling all subdirs below DIR."
174 ;;; TARGET MANAGEMENT
176 (defun ede-emacs-find-matching-target (class dir targets)
177 "Find a target that is a CLASS and is in DIR in the list of TARGETS."
180 (when (and (object-of-class-p T class)
181 (string= (oref T :path) dir))
186 (defmethod ede-find-target ((proj ede-emacs-project) buffer)
187 "Find an EDE target in PROJ for BUFFER.
188 If one doesn't exist, create a new one for this directory."
189 (let* ((ext (file-name-extension (buffer-file-name buffer)))
190 (cls (cond ((not ext)
191 'ede-emacs-target-misc)
192 ((string-match "c\\|h" ext)
194 ((string-match "elc?" ext)
195 'ede-emacs-target-el)
196 (t 'ede-emacs-target-misc)))
197 (targets (oref proj targets))
198 (dir default-directory)
199 (ans (ede-emacs-find-matching-target cls dir targets))
202 (setq ans (make-instance
204 :name (file-name-nondirectory
205 (directory-file-name dir))
208 (object-add-to-list proj :targets ans)
212 ;;; UTILITIES SUPPORT.
214 (defmethod ede-preprocessor-map ((this ede-emacs-target-c))
215 "Get the pre-processor map for Emacs C code.
216 All files need the macros from lisp.h!"
217 (require 'semantic/db)
218 (let* ((proj (ede-target-parent this))
219 (root (ede-project-root proj))
220 (table (semanticdb-file-table-object
221 (ede-expand-filename root "lisp.h")))
222 (config (semanticdb-file-table-object
223 (ede-expand-filename root "config.h")))
227 (when (semanticdb-needs-refresh-p table)
228 (semanticdb-refresh-table table))
229 (setq filemap (append filemap (oref table lexical-table)))
232 (when (semanticdb-needs-refresh-p config)
233 (semanticdb-refresh-table config))
234 (setq filemap (append filemap (oref config lexical-table)))
239 (defun ede-emacs-find-in-directories (name base dirs)
240 "Find NAME is BASE directory sublist of DIRS."
242 (while (and dirs (not ans))
243 (let* ((D (car dirs))
244 (ed (expand-file-name D base))
245 (ef (expand-file-name name ed)))
246 (if (file-exists-p ef)
248 ;; Not in this dir? How about subdirs?
249 (let ((dirfile (directory-files ed t))
252 ;; Get all the subdirs.
254 (when (and (file-directory-p DF)
255 (not (string-match "\\.$" DF)))
258 (setq ans (ede-emacs-find-in-directories name ed moredirs))
260 (setq dirs (cdr dirs))))
263 (defmethod ede-expand-filename-impl ((proj ede-emacs-project) name)
264 "Within this project PROJ, find the file NAME.
265 Knows about how the Emacs source tree is organized."
266 (let* ((ext (file-name-extension name))
267 (root (ede-project-root proj))
268 (dir (ede-project-root-directory root))
271 ((string-match "h\\|c" ext)
272 '("src" "lib-src" "lwlib"))
273 ((string-match "elc?" ext)
275 ((string-match "texi" ext)
279 (if (not dirs) (call-next-method)
280 (ede-emacs-find-in-directories name dir dirs))
286 ;; generated-autoload-file: "loaddefs.el"
287 ;; generated-autoload-load-name: "ede/emacs"
290 ;; arch-tag: 7cd0be95-663d-4101-8799-2f8216fd8233
291 ;;; ede/emacs.el ends here