Initial Commit
[packages] / xemacs-packages / ede / project-am.el
1 ;;; project-am.el --- A project management scheme based on automake files.
2
3 ;;;  Copyright (C) 1998, 1999, 2000, 2003, 2005, 2007  Eric M. Ludlam
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Version: 0.0.3
7 ;; Keywords: project, make
8 ;; RCS: $Id: project-am.el,v 1.1 2007-11-26 15:22:13 michaels Exp $
9
10 ;; This file is NOT part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28 ;; 
29 ;; The GNU Automake tool is the first step towards having a really
30 ;; good project management system.  It provides a simple and concise
31 ;; look at what is actually in a project, and records it in a simple
32 ;; fashion.
33 ;;
34 ;; project-am uses the structure defined in all good GNU projects with
35 ;; the Automake file as it's base template, and then maintains that
36 ;; information during edits, automatically updating the automake file
37 ;; where appropriate.
38
39 ;;; History:
40 ;; 
41
42 (eval-and-compile
43   ;; Compatibility for makefile mode.
44   (condition-case nil
45       (require 'makefile "make-mode")
46     (error (require 'make-mode "make-mode")))
47
48   ;; Requiring the .el files prevents incomplete builds.
49   (require 'eieio "eieio.el")
50   (require 'ede "ede.el"))
51
52 (eval-when-compile (require 'ede-speedbar "ede-speedbar.el"))
53 (eval-when-compile (require 'compile))
54
55 ;; customization stuff
56 (defgroup project-am nil
57   "File and tag browser frame."
58   :group 'tools
59   :group 'ede
60   )
61
62 (defcustom project-am-compile-project-command nil
63   "*Default command used to compile a project."
64   :group 'project-am
65   :type 'string)
66
67 (defcustom project-am-compile-target-command "make -k %s"
68   "*Default command used to compile a project."
69   :group 'project-am
70   :type 'string)
71
72 (defcustom project-am-debug-target-function 'gdb
73   "*Default Emacs command used to debug a target."
74   :group 'project-am
75   :type 'sexp) ; make this be a list some day
76
77 (defconst project-am-type-alist
78   '(("bin" project-am-program "bin_PROGRAMS")
79     ("sbin" project-am-program "sbin_PROGRAMS")
80     ("lib" project-am-lib "noinst_LIBS")
81     ("texinfo" project-am-texinfo "texinfo_TEXINFOS")
82     ("man" project-am-man "man_MANS")
83     ("lisp" project-am-lisp "lisp_LISP"))
84   "Alist of type names and the type of object to create for them.")
85
86 (defclass project-am-target (ede-target)
87   nil
88   "Base target class for everything in project-am.")
89
90 (defclass project-am-objectcode (project-am-target)
91   ((source :initarg :source :documentation "List of source files."))
92   "A target which creates object code, like a C program or library.")
93
94 (defclass project-am-program (project-am-objectcode)
95   ((ldadd :initarg :ldadd :documentation "Additional LD args."
96           :initform nil))
97   "A top level program to build")
98
99 (defclass project-am-lib (project-am-objectcode)
100   nil
101   "A top level library to build")
102
103 (defclass project-am-lisp (project-am-target)
104   ((lisp :initarg :lisp :documentation "List of lisp files to build."))
105   "A group of Emacs Lisp programs to byte compile.")
106
107 (defclass project-am-texinfo (project-am-target)
108   ((include :initarg :include
109             :initform nil
110             :documentation "Additional texinfo included in this one."))
111   "A top level texinfo file to build.")
112
113 (defclass project-am-man (project-am-target)
114   nil
115   "A top level man file to build.")
116
117 (defclass project-am-makefile (ede-project)
118   ((targets :initarg :targets
119             :initform nil
120             :documentation "Top level targets in this makefile.")
121    )
122   "Encode one makefile.")
123
124 ;;; Code:
125 (defmethod project-add-file ((ot project-am-target))
126   "Add the current buffer into a project.
127 OT is the object target.  DIR is the directory to start in."
128   (let* ((target (if ede-object (error "Already assocated w/ a target")
129                    (let ((amf (project-am-load default-directory)))
130                      (if (not amf) (error "No project file"))
131                      (completing-read "Target: "
132                                       (object-assoc-list 'name
133                                                          (oref amf targets))
134                                       nil t))))
135          ;; The input target might be new.  See if we can find it.
136          (amf (ede-load-project-file (oref ot path)))
137          (ot (object-assoc target 'name (oref amf targets)))
138          (ofn (file-name-nondirectory (buffer-file-name))))
139     (if (not ot)
140         (setq ot
141               (project-new-target
142                target (project-am-preferred-target-type (buffer-file-name)))))
143     (ede-with-projectfile ot
144       (makefile-move-to-macro (project-am-macro ot))
145       (ede-maybe-checkout)
146       (makefile-end-of-command)
147       (insert " " ofn)
148       (makefile-fill-paragraph nil)
149       (project-rescan ot)
150       (save-buffer))
151     (setq ede-object ot)))
152
153 (defmethod project-remove-file ((ot project-am-target) fnnd)
154   "Remove the current buffer from any project targets."
155   (ede-with-projectfile ot
156     (makefile-move-to-macro (project-am-macro ot))
157     (if (and buffer-read-only vc-mode
158              (y-or-n-p "Checkout Makefile.am from VC? "))
159         (vc-toggle-read-only t))
160     (ede-maybe-checkout)
161     (makefile-navigate-macro (concat " *" (regexp-quote (ede-name fnnd))))
162     (replace-match "" t t nil 0)
163     (makefile-fill-paragraph nil)
164     (project-rescan ot)
165     (save-buffer))
166   (setq ede-object nil))
167
168 (defmethod project-edit-file-target ((obj project-am-target))
169   "Edit the target associated w/ this file."
170   (find-file (concat (oref obj path) "Makefile.am"))
171   (goto-char (point-min))
172   (makefile-move-to-macro (project-am-macro obj))
173   (if (= (point-min) (point))
174       (re-search-forward (ede-target-name obj))))
175
176 (defmethod project-new-target ((proj project-am-makefile))
177   "Create a new target named NAME.
178 Argument TYPE is the type of target to insert.  This is a string
179 matching something in `project-am-type-alist' or type class symbol.
180 Despite the fact that this is a method, it depends on the current
181 buffer being in order to provide a smart default target type."
182   (let* ((name (read-string "Name: " ""))
183          (type (completing-read "Type: "
184                                 project-am-type-alist
185                                 nil t
186                                 (cond ((eq major-mode 'texinfo-mode)
187                                        "texinfo")
188                                       ((eq major-mode 'nroff-mode)
189                                        "man")
190                                       ((eq major-mode 'emacs-lisp-mode)
191                                        "lisp")
192                                       (t "bin"))))
193          (ntype (assoc type project-am-type-alist))
194          (ot nil))
195     (setq ot (apply (car (cdr ntype)) name :name name
196                     :path (expand-file-name default-directory) nil))
197     (if (not ot) (error "Error creating target object %S" ntype))
198     (ede-with-projectfile ot
199       (goto-char (point-min))
200       (ede-maybe-checkout)
201       (makefile-next-dependency)
202       (if (= (point) (point-min))
203           (goto-char (point-max))
204         (beginning-of-line)
205         (insert "\n")
206         (forward-char -1))
207       ;; Add the new target sources macro (if needed)
208       (if (project-am-macro ot)
209           (makefile-insert-macro (project-am-macro ot)))
210       ;; Add to the list of objects.
211       (goto-char (point-min))
212       (makefile-move-to-macro (car (cdr (cdr ntype))))
213       (if (= (point) (point-min))
214           (progn
215             (if (re-search-forward makefile-macroassign-regex nil t)
216                 (progn (forward-line -1)
217                        (end-of-line)
218                        (insert "\n"))
219               ;; If the above search fails, thats ok.  We'd just want to be at
220               ;; point-min anyway.
221               )
222             (makefile-insert-macro (car (cdr (cdr ntype))))))
223       (makefile-end-of-command)
224       (insert " " (ede-target-name ot))
225       (save-buffer)
226       ;; Rescan the object in this makefile.
227       (project-rescan ede-object))))
228
229 ;(defun project-am-rescan-toplevel ()
230 ;  "Rescan all projects in which the current buffer resides."
231 ;  (interactive)
232 ;  (let* ((tlof (project-am-find-topmost-level default-directory))
233 ;        (tlo (project-am-load tlof))
234 ;        (ede-deep-rescan t))  ; scan deep in this case.
235 ;    ;; tlo is the top level object for whatever file we are in
236 ;    ;; or nil.  If we have an object, call the rescan method.
237 ;    (if tlo (project-am-rescan tlo))))
238
239 ;;
240 ;; NOTE TO SELF
241 ;;
242 ;;  This should be handled at the EDE level, calling a method of the
243 ;; top most project.
244 ;;
245 (defmethod project-compile-project ((obj project-am-target) &optional command)
246   "Compile the entire current project.
247 Argument COMMAND is the command to use when compiling."
248   (require 'compile)
249   (if (not command)
250       (setq
251        command
252        ;; This interactive statement was taken from compile, and I'll
253        ;; use the same command history too.
254        (progn
255          (if (not project-am-compile-project-command)
256              (setq project-am-compile-project-command compile-command))
257          (if (or compilation-read-command current-prefix-arg)
258              (read-from-minibuffer "Project compile command: "
259                                    ;; hardcode make -k
260                                    ;; This is compile project after all.
261                                    project-am-compile-project-command
262                                    nil nil '(compile-history . 1))
263            project-am-compile-project-command))))
264   ;; When compile a project, we might be in a subdirectory,
265   ;; so we have to make sure we move all the way to the top.
266   (let* ((default-directory (project-am-find-topmost-level default-directory)))
267     (compile command)))
268
269 (defmethod project-compile-project ((obj project-am-makefile) 
270                                     &optional command)
271   "Compile the entire current project.
272 Argument COMMAND is the command to use when compiling."
273   (require 'compile)
274   (if (not command)
275       (setq
276        command
277        ;; This interactive statement was taken from compile, and I'll
278        ;; use the same command history too.
279        (progn
280          (if (not project-am-compile-project-command)
281              (setq project-am-compile-project-command compile-command))
282          (if (or compilation-read-command current-prefix-arg)
283              (read-from-minibuffer "Project compile command: "
284                                    ;; hardcode make -k
285                                    ;; This is compile project after all.
286                                    project-am-compile-project-command
287                                    nil nil '(compile-history . 1))
288            project-am-compile-project-command))))
289   ;; When compile a project, we might be in a subdirectory,
290   ;; so we have to make sure we move all the way to the top.
291   (let* ((default-directory (project-am-find-topmost-level default-directory)))
292     (compile command)))
293
294 (defmethod project-compile-target ((obj project-am-target) &optional command)
295   "Compile the current target.
296 Argument COMMAND is the command to use for compiling the target."
297   (require 'compile)
298   (if (not project-am-compile-project-command)
299       (setq project-am-compile-project-command compile-command))
300   (if (not command)
301       (setq
302        command
303        (if compilation-read-command
304            (read-from-minibuffer "Project compile command: "
305                                  ;; hardcode make -k
306                                  ;; This is compile project after all.
307                                  (if ede-object
308                                      (format
309                                       project-am-compile-target-command
310                                       (project-compile-target-command
311                                        ede-object))
312                                    project-am-compile-target-command)
313                                  nil nil
314                                  '(compile-history . 1))
315          (if ede-object
316              project-am-compile-project-command
317            (format
318             project-am-compile-target-command
319             (project-compile-target-command ede-object))))))
320   ;; We better be in the right place when compiling a specific target.
321   (compile command))
322
323 (defmethod project-debug-target ((obj project-am-objectcode))
324   "Run the current project target in a debugger."
325   (let ((tb (get-buffer-create " *padt*"))
326         (dd (oref obj path))
327         (cmd nil))
328     (unwind-protect
329         (progn
330           (set-buffer tb)
331           (setq default-directory dd)
332           (setq cmd (read-from-minibuffer
333                      "Run (like this): "
334                      (concat (symbol-name project-am-debug-target-function)
335                              " " (ede-target-name obj))))
336           (funcall project-am-debug-target-function cmd))
337       (kill-buffer tb))))
338
339 (defmethod project-make-dist ((this project-am-target))
340   "Run the current project in the debugger."
341   (require 'compile)
342   (if (not project-am-compile-project-command)
343       (setq project-am-compile-project-command compile-command))
344   (project-compile-project this (concat project-am-compile-project-command
345                                         " dist")))
346
347 ;;; Project loading and saving
348 ;;
349 (defun project-am-load (project)
350   "Read an automakefile PROJECT into our data structure.
351 Make sure that the tree down to our makefile is complete so that there
352 is cohesion in the project.  Return the project file (or sub-project).
353 If a given set of projects has already been loaded, then do nothing
354 but return the project for the directory given."
355   (setq project (expand-file-name project))
356   (let* ((ede-constructing t)
357          (fn (project-am-find-topmost-level
358               (if (string-match "/$" project)
359                   project
360                 (concat project "/"))))
361          (amo nil)
362          (trimmed (if (string-match (regexp-quote fn)
363                                     project)
364                       (replace-match "" t t project)
365                     ""))
366          (subdir nil))
367     (setq amo (object-assoc (concat fn "Makefile.am")
368                             'file ede-projects))
369     (if amo
370         (error "synchronous error in ede/project-am objects.")
371       (let ((project-am-constructiong t))
372         (setq amo (project-am-load-makefile fn))))
373     (if (not amo)
374         nil
375       ;; Now scan down from amo, and find the current directory
376       ;; from the PROJECT file.
377       (while (< 0 (length trimmed))
378         (if (string-match "\\([a-zA-Z0-9.-]+\\)/" trimmed)
379             (setq subdir (match-string 0 trimmed)
380                   trimmed (replace-match "" t t trimmed))
381           (error "Error scanning down path for project"))
382         (setq amo (project-am-subtree amo (concat fn subdir "Makefile.am"))
383               fn (concat fn subdir)))
384       amo)
385     ))
386
387 (defun project-am-find-topmost-level (path)
388   "Find the topmost automakefile starting with PATH."
389   (let ((newpath path))
390     (while (file-exists-p (concat newpath "Makefile.am"))
391       (setq path newpath newpath
392             (file-name-directory (substring path 0 (1- (length path))))))
393     (expand-file-name path)))
394
395 (defun project-am-load-makefile (path)
396   "Converts PATH into a project Makefile, and return it's object object.
397 It does not check for existing project objects.  Use `project-am-load'."
398   (let* ((fn (expand-file-name (concat path "Makefile.am")))
399          (kb (get-file-buffer fn)))
400     (if (not (file-exists-p fn))
401         nil
402       (save-excursion
403         (set-buffer (find-file-noselect fn))
404         (prog1
405             (if (and ede-object (project-am-makefile-p ede-object))
406                 ede-object
407               (let ((ampf (project-am-makefile (project-am-last-dir fn)
408                                                :name (project-am-last-dir fn)
409                                                :file fn)))
410                 (make-local-variable 'ede-object)
411                 (setq ede-object ampf)
412                 ;; Move the rescan after we set ede-object to prevent recursion
413                 (project-rescan ampf)
414                 ampf))
415           ;; If the buffer was not already loaded, kill it.
416           (if (not kb) (kill-buffer (current-buffer))))))))
417
418 ;;; Methods:
419 (defmethod ede-find-target ((amf project-am-makefile) buffer)
420   "Fetch the target belonging to BUFFER."
421   (or (call-next-method)
422       (let ((targ (oref amf targets))
423             (sobj (oref amf subproj))
424             (obj nil))
425         (while (and targ (not obj))
426           (if (ede-buffer-mine (car targ) buffer)
427               (setq obj (car targ)))
428           (setq targ (cdr targ)))
429         (while (and sobj (not obj))
430           (setq obj (project-am-buffer-object (car sobj) buffer)
431                 sobj (cdr sobj)))
432         obj)))
433
434 (defmethod project-targets-for-file ((proj project-am-makefile))
435   "Return a list of targets the project PROJ."
436   (oref proj targets))
437
438 (defmethod project-rescan ((this project-am-makefile))
439   "Rescan the makefile for all targets and sub targets."
440   (let ((osubproj (oref this subproj))
441         (otargets (oref this targets))
442         (csubproj (or
443                    ;; If DIST_SUBDIRS doesn't exist, then go for the
444                    ;; static list of SUBDIRS.  The DIST version should
445                    ;; contain SUBDIRS plus extra stuff.
446                    (makefile-macro-file-list "DIST_SUBDIRS")
447                    (makefile-macro-file-list "SUBDIRS")))
448         (nsubproj nil)
449         ;; Targets are excluded here because they require
450         ;; special attention.
451         (ntargets nil)
452         (tmp nil)
453         ;; Here are target prefixes as strings
454         (tp '(("bin_PROGRAMS" . project-am-program)
455               ("sbin_PROGRAMS" . project-am-program)
456               ("noinst_LIBRARIES" . project-am-lib)
457               ("info_TEXINFOS" . project-am-texinfo)
458               ("man_MANS" . project-am-man)))
459         (path (expand-file-name default-directory))
460         )
461     (mapcar
462      ;; Map all tye different types
463      (lambda (typecar)
464        ;; Map all the found objects
465        (mapcar (lambda (lstcar)
466                  (setq tmp (object-assoc lstcar 'name otargets))
467                  (if (not tmp)
468                      (setq tmp (apply (cdr typecar) lstcar
469                                       :name lstcar
470                                       :path path nil)))
471                  (project-rescan tmp)
472                  (setq ntargets (cons tmp ntargets)))
473                (makefile-macro-file-list (car typecar))))
474      tp)
475     ;; LISP is different.  Here there is only one kind of lisp (that I know of
476     ;; anyway) so it doesn't get mapped when it is found.
477     (if (makefile-move-to-macro "lisp_LISP")
478         (let ((tmp (project-am-lisp "lisp"
479                                     :name "lisp"
480                                     :path path)))
481           (project-rescan tmp)
482           (setq ntargets (cons tmp ntargets))))
483     ;; Now that we have this new list, chuck the old targets
484     ;; and replace it with the new list of targets I just created.
485     (oset this targets (nreverse ntargets))
486     ;; We still have a list of targets.  For all buffers, make sure
487     ;; their object still exists!
488
489     ;; FIGURE THIS OUT
490
491     ;; Ok, now lets look at all our sub-projects.
492     (mapcar (lambda (sp)
493               ;; For each project id found, see if we need to recycle,
494               ;; and if we do not, then make a new one.  Check the deep
495               ;; rescan value for behavior patterns.
496               (setq tmp (object-assoc
497                          (concat default-directory sp "/Makefile.am")
498                          'file osubproj))
499               (if (not tmp)
500                   ;; No tmp?  Create a new one.  Don't bother with
501                   ;; non-deep business since we need this object.
502                   (setq tmp (project-am-load-makefile
503                              (concat (file-name-directory (oref this :file))
504                                      sp "/")))
505                 ;; If we have tmp, then rescan it only if deep mode.
506                 (if ede-deep-rescan
507                     (project-rescan tmp)))
508               ;; Tac tmp onto our list of things to keep
509               (setq nsubproj (cons tmp nsubproj)))
510             csubproj)
511     (oset this subproj nsubproj)
512     ;; All elements should be updated now.
513     ))
514
515 (defmethod project-rescan ((this project-am-program))
516   "Rescan object THIS."
517   (oset this :source (makefile-macro-file-list (project-am-macro this)))
518   (oset this :ldadd (makefile-macro-file-list
519                      (concat (oref this :name) "_LDADD"))))
520
521 (defmethod project-rescan ((this project-am-lib))
522   "Rescan object THIS."
523   (oset this :source (makefile-macro-file-list (project-am-macro this))))
524
525 (defmethod project-rescan ((this project-am-texinfo))
526   "Rescan object THIS."
527   (oset this :include (makefile-macro-file-list (project-am-macro this))))
528
529 (defmethod project-rescan ((this project-am-man))
530   "Rescan object THIS."
531   )
532
533 (defmethod project-rescan ((this project-am-lisp))
534   "Rescan the lisp sources."
535   (oset this :lisp (makefile-macro-file-list (project-am-macro this))))
536
537 (defmethod project-am-macro ((this project-am-objectcode))
538   "Return the default macro to 'edit' for this object type."
539   (concat (oref this :name) "_SOURCES"))
540
541 (defmethod project-am-macro ((this project-am-texinfo))
542   "Return the default macro to 'edit' for this object type."
543   (concat (oref this :name) "_TEXINFOS"))
544
545 (defmethod project-am-macro ((this project-am-man))
546   "Return the default macro to 'edit' for this object type."
547   (concat (oref this :name) "_MANS"))
548
549 (defmethod project-am-macro ((this project-am-lisp))
550   "Return the default macro to 'edit' for this object."
551   "lisp_LISP")
552
553 (defun project-am-buffer-object (amf buffer)
554   "Return an object starting with AMF associated with BUFFER.
555 nil means that this buffer belongs to no-one."
556   (if (not amf)
557       nil
558     (if (ede-buffer-mine amf buffer)
559         amf
560       (let ((targ (oref amf targets))
561             (sobj (oref amf subproj))
562             (obj nil))
563         (while (and targ (not obj))
564           (if (ede-buffer-mine (car targ) buffer)
565               (setq obj (car targ)))
566           (setq targ (cdr targ)))
567         (while (and sobj (not obj))
568           (setq obj (project-am-buffer-object (car sobj) buffer)
569                 sobj (cdr sobj)))
570         obj))))
571   
572 (defmethod ede-buffer-mine ((this project-am-makefile) buffer)
573   "Return t if object THIS lays claim to the file in BUFFER."
574   (string= (oref this :file) (expand-file-name (buffer-file-name buffer))))
575
576 (defmethod ede-buffer-mine ((this project-am-objectcode) buffer)
577   "Return t if object THIS lays claim to the file in BUFFER."
578   (member (file-name-nondirectory (buffer-file-name buffer))
579           (oref this :source)))
580
581 (defmethod ede-buffer-mine ((this project-am-texinfo) buffer)
582   "Return t if object THIS lays claim to the file in BUFFER."
583   (let ((bfn (buffer-file-name buffer)))
584     (or (string= (oref this :name)  (file-name-nondirectory bfn))
585         (member (file-name-nondirectory bfn) (oref this :include)))))
586         
587 (defmethod ede-buffer-mine ((this project-am-man) buffer)
588   "Return t if object THIS lays claim to the file in BUFFER."
589   (string= (oref this :name) (buffer-file-name buffer)))
590
591 (defmethod ede-buffer-mine ((this project-am-lisp) buffer)
592   "Return t if object THIS lays claim to the file in BUFFER."
593   (member (file-name-nondirectory (buffer-file-name buffer))
594           (oref this :lisp)))
595
596 (defmethod project-am-subtree ((ampf project-am-makefile) subpath)
597   "Return the sub project in AMPF specified by SUBPATH."
598   (object-assoc (expand-file-name subpath) 'file (oref ampf subproj)))
599
600 (defmethod project-compile-target-command ((this project-am-target))
601   "Default target to use when compiling a given target."
602   ;; This is a pretty good default for most.
603   "")
604
605 (defmethod project-compile-target-command ((this project-am-objectcode))
606   "Default target to use when compiling an object code target."
607   (oref this :name))
608
609 (defmethod project-compile-target-command ((this project-am-texinfo))
610   "Default target t- use when compling a texinfo file."
611   (let ((n (oref this :name)))
612     (if (string-match "\\.texi?\\(nfo\\)?" n)
613         (setq n (replace-match ".info" t t n)))
614     n))
615
616 \f
617 ;;; Generic useful functions
618
619 (defun project-am-last-dir (file)
620   "Return the last part of a directory name.
621 Argument FILE is the file to extract the end directory name from."
622   (let ((s (file-name-directory file)))
623     (string-match "/\\([a-zA-Z0-9_]+\\)/$" s)
624     (match-string 1 s)))
625
626 (defun project-am-preferred-target-type (file)
627   "For FILE, return the preferred type for that file."
628   (cond ((string-match "\\.texi?\\(nfo\\)$" file)
629          project-am-texinfo)
630         ((string-match "\\.[0-9]$" file)
631          project-am-man)
632         ((string-match "\\.el$" file)
633          project-am-lisp)
634         (t
635          project-am-program)))
636
637 (defmethod ede-buffer-header-file((this project-am-objectcode) buffer)
638   "There are no default header files."
639   (or (call-next-method)
640       (let ((s (oref this source))
641             (found nil))
642         (while (and s (not found))
643           ;; Add more logic here if applicable.
644           (if (string-match "\\.\\(h\\|H\\|hh\\|hpp\\)" (car s))
645               (setq found (car s)))
646           (setq s (cdr s)))
647         found)))
648
649 (defmethod ede-documentation ((this project-am-texinfo))
650   "Return a list of files that provides documentation.
651 Documentation is not for object THIS, but is provided by THIS for other
652 files in the project."
653   (let ((src (append (oref this source)
654                      (oref this include)))
655         (out nil))
656     ;; Loop over all entries and expand
657     (while src
658       (setq out (cons
659                  (ede-expand-filename this (car src))
660                  out))
661       (setq src (cdr src)))
662     ;; return it
663     out))
664
665 \f
666 ;;; Makefile editing and scanning commands
667 ;;
668 ;; Formatting of a makefile
669 ;;
670 ;; 1) Creating an automakefile, stick in a top level comment about
671 ;;    being created by emacs
672 ;; 2) Leave order of variable contents alone, except for SOURCE
673 ;;    SOURCE always keep in the order of .c, .h, the other stuff.
674
675 ;; personal reference until I'm done
676 ; makefile-fill-paragraph -- refill a macro w/ backslashes
677 ; makefile-insert-macro -- insert "foo = "
678
679 (defun makefile-beginning-of-command ()
680   "Move the the beginning of the current command."
681   (interactive)
682   (if (save-excursion
683         (forward-line -1)
684         (makefile-line-continued-p))
685       (forward-line -1))
686   (beginning-of-line)
687   (if (not (makefile-line-continued-p))
688       nil
689     (while (and (makefile-line-continued-p)
690                 (not (bobp)))
691       (forward-line -1))
692     (forward-line 1)))
693
694 (defun makefile-end-of-command ()
695   "Move the the beginning of the current command."
696   (interactive)
697   (end-of-line)
698   (while (and (makefile-line-continued-p)
699               (not (eobp)))
700     (forward-line 1)
701     (end-of-line)))
702
703 (defun makefile-line-continued-p ()
704   "Return non-nil if the current line ends in continuation."
705   (save-excursion
706     (end-of-line)
707     (= (preceding-char) ?\\)))
708
709 ;;; Programatic editing of a Makefile
710 ;;
711 (defun makefile-move-to-macro (macro)
712   "Move to the definition of MACRO.  Return t if found."
713   (let ((oldpt (point)))
714     (goto-char (point-min))
715     (if (re-search-forward (concat "^" macro "\\s-*=") nil t)
716         t
717       (goto-char oldpt)
718       nil)))
719
720 (defun makefile-navigate-macro (stop-before)
721   "In a list of files, move forward until STOP-BEFORE is reached.
722 STOP-BEFORE is a regular expression matching a file name."
723   (save-excursion
724     (makefile-beginning-of-command)
725     (let ((e (save-excursion
726                (makefile-end-of-command)
727                (point))))
728       (if (re-search-forward stop-before nil t)
729           (goto-char (match-beginning 0))
730         (goto-char e)))))
731
732 (defun makefile-macro-file-list (macro)
733   "Return a list of all files in MACRO."
734   (save-excursion
735     (if (makefile-move-to-macro macro)
736         (let ((e (save-excursion
737                    (makefile-end-of-command)
738                    (point)))
739               (lst nil))
740           (while (re-search-forward "\\s-**\\([-a-zA-Z0-9./_@$%()]+\\)\\s-*" e t)
741             (setq lst (cons
742                        (buffer-substring-no-properties
743                         (match-beginning 1)
744                         (match-end 1))
745                        lst)))
746           (nreverse lst)))))
747
748 ;;; Methods used in speedbar
749 ;;
750 (defmethod ede-sb-button ((this project-am-program) depth)
751   "Create a speedbar button for object THIS at DEPTH."
752   (speedbar-make-tag-line 'angle ?+
753                           'ede-object-expand
754                           this (ede-name this)
755                           nil nil  ; nothing to jump to
756                           'speedbar-file-face depth))
757
758 (defmethod ede-sb-button ((this project-am-lib) depth)
759   "Create a speedbar button for object THIS at DEPTH."
760   (speedbar-make-tag-line 'angle ?+
761                           'ede-object-expand
762                           this (ede-name this)
763                           nil nil  ; nothing to jump to
764                           'speedbar-file-face depth))
765
766 (defmethod ede-sb-button ((this project-am-texinfo) depth)
767   "Create a speedbar button for object THIS at DEPTH."
768   (speedbar-make-tag-line 'bracket ?+
769                           'ede-object-expand
770                           this
771                           (ede-name this)
772                           'ede-file-find
773                           (concat (oref this :path)
774                                   (oref this :name))
775                           'speedbar-file-face depth))
776
777 (defmethod ede-sb-button ((this project-am-man) depth)
778   "Create a speedbar button for object THIS at DEPTH."
779   (speedbar-make-tag-line 'bracket ?? nil nil
780                           (ede-name this)
781                           'ede-file-find
782                           (concat (oref this :path)
783                                   (oref this :name))
784                           'speedbar-file-face depth))
785
786 (defmethod ede-sb-button ((this project-am-lisp) depth)
787   "Create a speedbar button for object THIS at DEPTH."
788   (speedbar-make-tag-line 'angle ?+
789                           'ede-object-expand
790                           this (ede-name this)
791                           nil nil  ; nothing to jump to
792                           'speedbar-file-face depth))
793
794 (defmethod ede-sb-expand ((this project-am-objectcode) depth)
795   "Expand node describing something built into objectcode.
796 TEXT is the text clicked on.  TOKEN is the object we are expanding from.
797 INDENT is the current indentatin level."
798   (let ((sources (oref this :source)))
799     (while sources
800       (speedbar-make-tag-line 'bracket ?+
801                               'ede-tag-file
802                               (concat (oref this :path)
803                                       (car sources))
804                               (car sources)
805                               'ede-file-find
806                               (concat
807                                (oref this :path)
808                                (car sources))
809                               'speedbar-file-face depth)
810       (setq sources (cdr sources)))))
811
812 (defmethod ede-sb-expand ((this project-am-texinfo) depth)
813   "Expand node describing a texinfo manual.
814 TEXT is the text clicked on.  TOKEN is the object we are expanding from.
815 INDENT is the current indentatin level."
816   (let ((includes (oref this :include)))
817     (while includes
818       (speedbar-make-tag-line 'bracket ?+
819                               'ede-tag-file
820                               (concat (oref this :path)
821                                       (car includes))
822                               (car includes)
823                               'ede-file-find
824                               (concat
825                                (oref this :path)
826                                (car includes))
827                               'speedbar-file-face depth)
828       (setq includes (cdr includes)))
829     ;; Not only do we show the included files (for future expansion)
830     ;; but we also want to display tags for this file too.
831     (ede-create-tag-buttons (concat (oref this :path)
832                                            (oref this :name))
833                                    depth)))
834
835 (defmethod ede-sb-expand ((this project-am-lisp) depth)
836   "Expand node describing lisp code.
837 TEXT is the text clicked on.  TOKEN is the object we are expanding from.
838 INDENT is the current indentatin level."
839   (let ((sources (oref this :lisp)))
840     (while sources
841       (speedbar-make-tag-line 'bracket ?+
842                               'ede-tag-file
843                               (concat (oref this :path)
844                                       (car sources))
845                               (car sources)
846                               'ede-file-find
847                               (concat
848                                (oref this :path)
849                                (car sources))
850                               'speedbar-file-face depth)
851       (setq sources (cdr sources)))))
852
853 (provide 'project-am)
854
855 ;;; project-am.el ends here