Replace end-of-line, save-excursion etc with point-at-eol, point-at-bol.
[bpt/emacs.git] / lisp / cedet / ede / proj-elisp.el
CommitLineData
acc33231
CY
1;;; ede-proj-elisp.el --- EDE Generic Project Emacs Lisp support
2
e180ab9f
GM
3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4;; 2008, 2009, 2010 Free Software Foundation, Inc.
acc33231
CY
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;;
b90caf50 26;; Handle Emacs Lisp in an EDE Project file.
acc33231
CY
27
28(require 'ede/proj)
29(require 'ede/pmake)
30(require 'ede/pconf)
31
a2095e2e
CY
32(autoload 'semantic-ede-proj-target-grammar "semantic/ede-grammar")
33
acc33231
CY
34;;; Code:
35(defclass ede-proj-target-elisp (ede-proj-target-makefile)
36 ((menu :initform nil)
37 (keybindings :initform nil)
38 (phony :initform t)
cb85c0d8
EL
39 (sourcetype :initform '(ede-source-emacs))
40 (availablecompilers :initform '(ede-emacs-compiler ede-xemacs-compiler))
acc33231
CY
41 (aux-packages :initarg :aux-packages
42 :initform nil
43 :type list
44 :custom (repeat string)
45 :documentation "Additional packages needed.
46There should only be one toplevel package per auxiliary tool needed.
47These packages location is found, and added to the compile time
48load path."
49 ))
50 "This target consists of a group of lisp files.
51A 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.
89Lays 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.
125Bonus: 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))
39a766fb
GM
132 (elc (concat (file-name-sans-extension fsrc) ".elc")))
133 (if (eq (byte-recompile-file fsrc nil 0) t)
430e7297 134 (setq comp (1+ comp))
acc33231
CY
135 (setq utd (1+ utd)))))
136 (oref obj source))
137 (message "All Emacs Lisp sources are up to date in %s" (object-name obj))
39a766fb 138 (cons comp utd)))
acc33231
CY
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.
142There are standards in Elisp files specifying how the version string
143is 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
0816d744
SM
149 (with-current-buffer (find-file-noselect
150 (ede-expand-filename this (car vs)))
acc33231
CY
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))
0816d744 241 (with-current-buffer (find-file-noselect ec t)
acc33231
CY
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
e180ab9f 248 (delete-region (point-at-bol) (point-at-bol 2)))))))))
acc33231
CY
249
250;;;
251;; Autoload generators
252;;
253(defclass ede-proj-target-elisp-autoloads (ede-proj-target-elisp)
cb85c0d8 254 ((availablecompilers :initform '(ede-emacs-cedet-autogen-compiler))
acc33231
CY
255 (aux-packages :initform ("cedet-autogen"))
256 (phony :initform t)
257 (autoload-file :initarg :autoload-file
258 :initform "loaddefs.el"
259 :type string
260 :custom string
261 :documentation "The file that autoload definitions are placed in.
262There should be one load defs file for a given package. The load defs are created
263for all Emacs Lisp sources that exist in the directory of the created target.")
264 (autoload-dirs :initarg :autoload-dirs
265 :initform nil
266 :type list
267 :custom (repeat string)
268 :documentation "The directories to scan for autoload definitions.
269If nil defaults to the current directory.")
270 )
271 "Target that builds an autoload file.
272Files do not need to be added to this target.")
273
274
275;;; Claiming files
276(defmethod ede-buffer-mine ((this ede-proj-target-elisp-autoloads) buffer)
277 "Return t if object THIS lays claim to the file in BUFFER.
278Lays claim to all .elc files that match .el files in this target."
279 (if (string-match
280 (concat (regexp-quote (oref this autoload-file)) "$")
281 (buffer-file-name buffer))
282 t
283 (call-next-method) ; The usual thing.
284 ))
285
286;; Compilers
287(defvar ede-emacs-cedet-autogen-compiler
288 (ede-compiler
289 "ede-emacs-autogen-compiler"
290 :name "emacs"
291 :variables '(("EMACS" . "emacs"))
292 :commands
293 '("@echo \"(add-to-list 'load-path nil)\" > $@-compile-script"
294 "for loadpath in . ${LOADPATH}; do \\"
295 " echo \"(add-to-list 'load-path \\\"$$loadpath\\\")\" >> $@-compile-script; \\"
296 "done;"
297 "@echo \"(require 'cedet-autogen)\" >> $@-compile-script"
298 "\"$(EMACS)\" -batch --no-site-file -l $@-compile-script -f cedet-batch-update-autoloads $(LOADDEFS) $(LOADDIRS)"
299 )
300 :sourcetype '(ede-source-emacs)
301 )
302 "Build an autoloads file.")
303
304(defmethod ede-proj-compilers ((obj ede-proj-target-elisp-autoloads))
305 "List of compilers being used by OBJ.
306If the `compiler' slot is empty, get the car of the compilers list."
307 (let ((comp (oref obj compiler)))
308 (if comp
309 (if (listp comp)
310 (setq comp (mapcar 'symbol-value comp))
311 (setq comp (list (symbol-value comp))))
312 ;; Get the first element from our list of compilers.
313 (let ((avail (mapcar 'symbol-value (oref obj availablecompilers))))
314 (setq comp (list (car avail)))))
315 comp))
316
317(defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target-elisp-autoloads)
318 &optional
319 moresource)
320 "Insert the source variables needed by THIS.
321Optional argument MORESOURCE is a list of additional sources to add to the
322sources variable."
323 nil)
324
325(defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp-autoloads))
326 "Return the variable name for THIS's sources."
327 nil) ; "LOADDEFS")
328
329(defmethod ede-proj-makefile-dependencies ((this ede-proj-target-elisp-autoloads))
330 "Return a string representing the dependencies for THIS.
331Always return an empty string for an autoloads generator."
332 "")
333
334(defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp-autoloads))
335 "Insert variables needed by target THIS."
336 (ede-pmake-insert-variable-shared "LOADDEFS"
337 (insert (oref this autoload-file)))
338 (ede-pmake-insert-variable-shared "LOADDIRS"
339 (insert (mapconcat 'identity
340 (or (oref this autoload-dirs) '("."))
341 " ")))
342 )
343
344(defmethod project-compile-target ((obj ede-proj-target-elisp-autoloads))
345 "Create or update the autoload target."
346 (require 'cedet-autogen)
347 (let ((default-directory (ede-expand-filename obj ".")))
348 (apply 'cedet-update-autoloads
349 (oref obj autoload-file)
350 (oref obj autoload-dirs))
351 ))
352
353(defmethod ede-update-version-in-source ((this ede-proj-target-elisp-autoloads) version)
354 "In a Lisp file, updated a version string for THIS to VERSION.
355There are standards in Elisp files specifying how the version string
356is found, such as a `-version' variable, or the standard header."
357 nil)
358
359(defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-target-elisp-autoloads))
360 "Insert any symbols that the DIST rule should depend on.
361Emacs Lisp autoload files ship the generated .el files.
362Argument THIS is the target which needs to insert an info file."
363 ;; In some cases, this is ONLY the index file. That should generally
364 ;; be ok.
365 (insert " " (ede-proj-makefile-target-name this))
366 )
367
368(defmethod ede-proj-makefile-insert-dist-filepatterns ((this ede-proj-target-elisp-autoloads))
369 "Insert any symbols that the DIST rule should distribute.
370Emacs Lisp autoload files ship the generated .el files.
371Argument THIS is the target which needs to insert an info file."
372 (insert " " (oref this autoload-file))
373 )
374
375(defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp-autoloads))
376 "Tweak the configure file (current buffer) to accomodate THIS."
a785b776 377 (error "Autoloads not supported in autoconf yet"))
acc33231
CY
378
379(defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp-autoloads))
380 "Flush the configure file (current buffer) to accomodate THIS."
381 nil)
382
383(provide 'ede/proj-elisp)
384
385;;; ede/proj-elisp.el ends here