]> code.delx.au - gnu-emacs/blob - lisp/cedet/ede/proj-elisp.el
Unify mouse-highlight code for all GUI and TTY sessions.
[gnu-emacs] / lisp / cedet / ede / proj-elisp.el
1 ;;; ede-proj-elisp.el --- EDE Generic Project Emacs Lisp support
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: project, make
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; Handle Emacs Lisp in an EDE Project file.
27
28 (require 'ede/proj)
29 (require 'ede/pmake)
30 (require 'ede/pconf)
31
32 (autoload 'semantic-ede-proj-target-grammar "semantic/ede-grammar")
33
34 ;;; Code:
35 (defclass ede-proj-target-elisp (ede-proj-target-makefile)
36 ((menu :initform nil)
37 (keybindings :initform nil)
38 (phony :initform t)
39 (sourcetype :initform '(ede-source-emacs))
40 (availablecompilers :initform '(ede-emacs-compiler ede-xemacs-compiler))
41 (aux-packages :initarg :aux-packages
42 :initform nil
43 :type list
44 :custom (repeat string)
45 :documentation "Additional packages needed.
46 There should only be one toplevel package per auxiliary tool needed.
47 These packages location is found, and added to the compile time
48 load path."
49 ))
50 "This target consists of a group of lisp files.
51 A lisp target may be one general program with many separate lisp files in it.")
52
53 (defvar ede-source-emacs
54 (ede-sourcecode "ede-emacs-source"
55 :name "Emacs Lisp"
56 :sourcepattern "\\.el$"
57 :garbagepattern '("*.elc"))
58 "Emacs Lisp source code definition.")
59
60 (defvar ede-emacs-compiler
61 (ede-compiler
62 "ede-emacs-compiler"
63 :name "emacs"
64 :variables '(("EMACS" . "emacs")
65 ("EMACSFLAGS" . "-batch --no-site-file"))
66 :commands
67 '("@echo \"(add-to-list 'load-path nil)\" > $@-compile-script"
68 "for loadpath in . ${LOADPATH}; do \\"
69 " echo \"(add-to-list 'load-path \\\"$$loadpath\\\")\" >> $@-compile-script; \\"
70 "done;"
71 "@echo \"(setq debug-on-error t)\" >> $@-compile-script"
72 "\"$(EMACS)\" $(EMACSFLAGS) -l $@-compile-script -f batch-byte-compile $^"
73 )
74 :autoconf '("AM_PATH_LISPDIR")
75 :sourcetype '(ede-source-emacs)
76 ; :objectextention ".elc"
77 )
78 "Compile Emacs Lisp programs.")
79
80 (defvar ede-xemacs-compiler
81 (clone ede-emacs-compiler "ede-xemacs-compiler"
82 :name "xemacs"
83 :variables '(("EMACS" . "xemacs")))
84 "Compile Emacs Lisp programs with XEmacs.")
85
86 ;;; Claiming files
87 (defmethod ede-buffer-mine ((this ede-proj-target-elisp) buffer)
88 "Return t if object THIS lays claim to the file in BUFFER.
89 Lays claim to all .elc files that match .el files in this target."
90 (if (string-match "\\.elc$" (buffer-file-name buffer))
91 (let ((fname
92 (concat
93 (file-name-sans-extension (buffer-file-name buffer))
94 ".el")
95 ))
96 ;; Is this in our list.
97 (member fname (oref this auxsource))
98 )
99 (call-next-method) ; The usual thing.
100 ))
101
102 ;;; Emacs Lisp Compiler
103 ;;; Emacs Lisp Target
104 (defun ede-proj-elisp-packages-to-loadpath (packages)
105 "Convert a list of PACKAGES, to a list of load path."
106 (let ((paths nil)
107 (ldir nil))
108 (while packages
109 (or (setq ldir (locate-library (car packages)))
110 (error "Cannot find package %s" (car packages)))
111 (let* ((fnd (file-name-directory ldir))
112 (rel (file-relative-name fnd))
113 (full nil)
114 )
115 ;; Make sure the relative name isn't to far off
116 (when (string-match "^\\.\\./\\.\\./\\.\\./\\.\\." rel)
117 (setq full fnd))
118 ;; Do the setup.
119 (setq paths (cons (or full rel) paths)
120 packages (cdr packages))))
121 paths))
122
123 (defmethod project-compile-target ((obj ede-proj-target-elisp))
124 "Compile all sources in a Lisp target OBJ.
125 Bonus: Return a cons cell: (COMPILED . UPTODATE)."
126 (let* ((proj (ede-target-parent obj))
127 (dir (oref proj directory))
128 (comp 0)
129 (utd 0))
130 (mapc (lambda (src)
131 (let* ((fsrc (expand-file-name src dir))
132 (elc (concat (file-name-sans-extension fsrc) ".elc")))
133 (if (eq (byte-recompile-file fsrc nil 0) t)
134 (setq comp (1+ comp))
135 (setq utd (1+ utd)))))
136 (oref obj source))
137 (message "All Emacs Lisp sources are up to date in %s" (object-name obj))
138 (cons comp utd)))
139
140 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp) version)
141 "In a Lisp file, updated a version string for THIS to VERSION.
142 There are standards in Elisp files specifying how the version string
143 is found, such as a `-version' variable, or the standard header."
144 (if (and (slot-boundp this 'versionsource)
145 (oref this versionsource))
146 (let ((vs (oref this versionsource))
147 (match nil))
148 (while vs
149 (with-current-buffer (find-file-noselect
150 (ede-expand-filename this (car vs)))
151 (goto-char (point-min))
152 (let ((case-fold-search t))
153 (if (re-search-forward "-version\\s-+\"\\([^\"]+\\)\"" nil t)
154 (progn
155 (setq match t)
156 (delete-region (match-beginning 1)
157 (match-end 1))
158 (goto-char (match-beginning 1))
159 (insert version)))))
160 (setq vs (cdr vs)))
161 (if (not match) (call-next-method)))))
162
163
164 ;;; Makefile generation functions
165 ;;
166 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp))
167 "Return the variable name for THIS's sources."
168 (cond ((ede-proj-automake-p) '("lisp_LISP" . share))
169 (t (concat (ede-pmake-varname this) "_LISP"))))
170
171 (defun ede-proj-makefile-insert-loadpath-items (items)
172 "Insert a sequence of ITEMS into the Makefile LOADPATH variable."
173 (when items
174 (ede-pmake-insert-variable-shared "LOADPATH"
175 (let ((begin (save-excursion (re-search-backward "\\s-*="))))
176 (while items
177 (when (not (save-excursion
178 (re-search-backward
179 (concat "\\s-" (regexp-quote (car items)) "[ \n\t\\]")
180 begin t)))
181 (insert " " (car items)))
182 (setq items (cdr items)))))
183 ))
184
185 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp))
186 "Insert variables needed by target THIS."
187 (let ((newitems (if (oref this aux-packages)
188 (ede-proj-elisp-packages-to-loadpath
189 (oref this aux-packages))))
190 )
191 (ede-proj-makefile-insert-loadpath-items newitems)))
192
193 (defun ede-proj-elisp-add-path (path)
194 "Add path PATH into the file if it isn't already there."
195 (goto-char (point-min))
196 (if (re-search-forward (concat "(cons \\\""
197 (regexp-quote path))
198 nil t)
199 nil;; We have it already
200 (if (re-search-forward "(cons nil" nil t)
201 (progn
202 ;; insert stuff here
203 (end-of-line)
204 (insert "\n"
205 " echo \"(setq load-path (cons \\\""
206 path
207 "\\\" load-path))\" >> script")
208 )
209 (error "Don't know how to update load path"))))
210
211 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp))
212 "Tweak the configure file (current buffer) to accomodate THIS."
213 (call-next-method)
214 ;; Ok, now we have to tweak the autoconf provided `elisp-comp' program.
215 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile)))
216 (if (or (not ec) (not (file-exists-p ec)))
217 (message "No elisp-comp file. There may be compile errors? Rerun a second time.")
218 (save-excursion
219 (if (file-symlink-p ec)
220 (progn
221 ;; Desymlinkafy
222 (rename-file ec (concat ec ".tmp"))
223 (copy-file (concat ec ".tmp") ec)
224 (delete-file (concat ec ".tmp"))))
225 (set-buffer (find-file-noselect ec t))
226 (ede-proj-elisp-add-path "..")
227 (let ((paths (ede-proj-elisp-packages-to-loadpath
228 (oref this aux-packages))))
229 ;; Add in the current list of paths
230 (while paths
231 (ede-proj-elisp-add-path (car paths))
232 (setq paths (cdr paths))))
233 (save-buffer)) )))
234
235 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp))
236 "Flush the configure file (current buffer) to accomodate THIS."
237 ;; Remove crufty old paths from elisp-compile
238 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile))
239 )
240 (if (and ec (file-exists-p ec))
241 (with-current-buffer (find-file-noselect ec t)
242 (goto-char (point-min))
243 (while (re-search-forward "(cons \\([^ ]+\\) load-path)"
244 nil t)
245 (let ((path (match-string 1)))
246 (if (string= path "nil")
247 nil
248 (delete-region (save-excursion (beginning-of-line) (point))
249 (save-excursion (end-of-line)
250 (forward-char 1)
251 (point))))))))))
252
253 ;;;
254 ;; Autoload generators
255 ;;
256 (defclass ede-proj-target-elisp-autoloads (ede-proj-target-elisp)
257 ((availablecompilers :initform '(ede-emacs-cedet-autogen-compiler))
258 (aux-packages :initform ("cedet-autogen"))
259 (phony :initform t)
260 (autoload-file :initarg :autoload-file
261 :initform "loaddefs.el"
262 :type string
263 :custom string
264 :documentation "The file that autoload definitions are placed in.
265 There should be one load defs file for a given package. The load defs are created
266 for all Emacs Lisp sources that exist in the directory of the created target.")
267 (autoload-dirs :initarg :autoload-dirs
268 :initform nil
269 :type list
270 :custom (repeat string)
271 :documentation "The directories to scan for autoload definitions.
272 If nil defaults to the current directory.")
273 )
274 "Target that builds an autoload file.
275 Files do not need to be added to this target.")
276
277
278 ;;; Claiming files
279 (defmethod ede-buffer-mine ((this ede-proj-target-elisp-autoloads) buffer)
280 "Return t if object THIS lays claim to the file in BUFFER.
281 Lays claim to all .elc files that match .el files in this target."
282 (if (string-match
283 (concat (regexp-quote (oref this autoload-file)) "$")
284 (buffer-file-name buffer))
285 t
286 (call-next-method) ; The usual thing.
287 ))
288
289 ;; Compilers
290 (defvar ede-emacs-cedet-autogen-compiler
291 (ede-compiler
292 "ede-emacs-autogen-compiler"
293 :name "emacs"
294 :variables '(("EMACS" . "emacs"))
295 :commands
296 '("@echo \"(add-to-list 'load-path nil)\" > $@-compile-script"
297 "for loadpath in . ${LOADPATH}; do \\"
298 " echo \"(add-to-list 'load-path \\\"$$loadpath\\\")\" >> $@-compile-script; \\"
299 "done;"
300 "@echo \"(require 'cedet-autogen)\" >> $@-compile-script"
301 "\"$(EMACS)\" -batch --no-site-file -l $@-compile-script -f cedet-batch-update-autoloads $(LOADDEFS) $(LOADDIRS)"
302 )
303 :sourcetype '(ede-source-emacs)
304 )
305 "Build an autoloads file.")
306
307 (defmethod ede-proj-compilers ((obj ede-proj-target-elisp-autoloads))
308 "List of compilers being used by OBJ.
309 If the `compiler' slot is empty, get the car of the compilers list."
310 (let ((comp (oref obj compiler)))
311 (if comp
312 (if (listp comp)
313 (setq comp (mapcar 'symbol-value comp))
314 (setq comp (list (symbol-value comp))))
315 ;; Get the first element from our list of compilers.
316 (let ((avail (mapcar 'symbol-value (oref obj availablecompilers))))
317 (setq comp (list (car avail)))))
318 comp))
319
320 (defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target-elisp-autoloads)
321 &optional
322 moresource)
323 "Insert the source variables needed by THIS.
324 Optional argument MORESOURCE is a list of additional sources to add to the
325 sources variable."
326 nil)
327
328 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp-autoloads))
329 "Return the variable name for THIS's sources."
330 nil) ; "LOADDEFS")
331
332 (defmethod ede-proj-makefile-dependencies ((this ede-proj-target-elisp-autoloads))
333 "Return a string representing the dependencies for THIS.
334 Always return an empty string for an autoloads generator."
335 "")
336
337 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp-autoloads))
338 "Insert variables needed by target THIS."
339 (ede-pmake-insert-variable-shared "LOADDEFS"
340 (insert (oref this autoload-file)))
341 (ede-pmake-insert-variable-shared "LOADDIRS"
342 (insert (mapconcat 'identity
343 (or (oref this autoload-dirs) '("."))
344 " ")))
345 )
346
347 (defmethod project-compile-target ((obj ede-proj-target-elisp-autoloads))
348 "Create or update the autoload target."
349 (require 'cedet-autogen)
350 (let ((default-directory (ede-expand-filename obj ".")))
351 (apply 'cedet-update-autoloads
352 (oref obj autoload-file)
353 (oref obj autoload-dirs))
354 ))
355
356 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp-autoloads) version)
357 "In a Lisp file, updated a version string for THIS to VERSION.
358 There are standards in Elisp files specifying how the version string
359 is found, such as a `-version' variable, or the standard header."
360 nil)
361
362 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-target-elisp-autoloads))
363 "Insert any symbols that the DIST rule should depend on.
364 Emacs Lisp autoload files ship the generated .el files.
365 Argument THIS is the target which needs to insert an info file."
366 ;; In some cases, this is ONLY the index file. That should generally
367 ;; be ok.
368 (insert " " (ede-proj-makefile-target-name this))
369 )
370
371 (defmethod ede-proj-makefile-insert-dist-filepatterns ((this ede-proj-target-elisp-autoloads))
372 "Insert any symbols that the DIST rule should distribute.
373 Emacs Lisp autoload files ship the generated .el files.
374 Argument THIS is the target which needs to insert an info file."
375 (insert " " (oref this autoload-file))
376 )
377
378 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp-autoloads))
379 "Tweak the configure file (current buffer) to accomodate THIS."
380 (error "Autoloads not supported in autoconf yet"))
381
382 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp-autoloads))
383 "Flush the configure file (current buffer) to accomodate THIS."
384 nil)
385
386 (provide 'ede/proj-elisp)
387
388 ;;; ede/proj-elisp.el ends here