1 ;;; ede.el --- Emacs Development Environment gloss
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: project, make
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; EDE is the top level Lisp interface to a project management scheme
27 ;; for Emacs. Emacs does many things well, including editing,
28 ;; building, and debugging. Folks migrating from other IDEs don't
29 ;; seem to think this qualifies, however, because they still have to
30 ;; write the makefiles, and specify parameters to programs.
32 ;; This EDE mode will attempt to link these diverse programs together
33 ;; into a comprehensive single interface, instead of a bunch of
38 ;; This command enables project mode on all files.
40 ;; (global-ede-mode t)
44 (require 'eieio-speedbar
)
49 (load "ede/loaddefs" nil
'nomessage
)
51 (declare-function ede-commit-project
"ede/custom")
52 (declare-function ede-convert-path
"ede/files")
53 (declare-function ede-directory-get-open-project
"ede/files")
54 (declare-function ede-directory-get-toplevel-open-project
"ede/files")
55 (declare-function ede-directory-project-p
"ede/files")
56 (declare-function ede-find-subproject-for-directory
"ede/files")
57 (declare-function ede-project-directory-remove-hash
"ede/files")
58 (declare-function ede-toplevel
"ede/files")
59 (declare-function ede-toplevel-project
"ede/files")
60 (declare-function ede-up-directory
"ede/files")
61 (declare-function semantic-lex-make-spp-table
"semantic/lex-spp")
63 (defconst ede-version
"1.0"
64 "Current version of the Emacs EDE.")
68 "Display the current running version of EDE."
69 (interactive) (message "EDE %s" ede-version
))
72 "Emacs Development Environment."
76 (defcustom ede-auto-add-method
'ask
77 "Whether a new source file should be automatically added to a target.
78 Whenever a new file is encountered in a directory controlled by a
79 project file, all targets are queried to see if it should be added.
80 If the value is 'always, then the new file is added to the first
81 target encountered. If the value is 'multi-ask, then if more than one
82 target wants the file, the user is asked. If only one target wants
83 the file, then then it is automatically added to that target. If the
84 value is 'ask, then the user is always asked, unless there is no
85 target willing to take the file. 'never means never perform the check."
87 :type
'(choice (const always
)
92 (defcustom ede-debug-program-function
'gdb
93 "Default Emacs command used to debug a target."
95 :type
'sexp
) ; make this be a list of options some day
98 ;;; Management variables
100 (defvar ede-projects nil
101 "A list of all active projects currently loaded in Emacs.")
103 (defvar ede-object-root-project nil
104 "The current buffer's current root project.
105 If a file is under a project, this specifies the project that is at
106 the root of a project tree.")
107 (make-variable-buffer-local 'ede-object-root-project
)
109 (defvar ede-object-project nil
110 "The current buffer's current project at that level.
111 If a file is under a project, this specifies the project that contains the
113 (make-variable-buffer-local 'ede-object-project
)
115 (defvar ede-object nil
116 "The current buffer's target object.
117 This object's class determines how to compile and debug from a buffer.")
118 (make-variable-buffer-local 'ede-object
)
120 (defvar ede-selected-object nil
121 "The currently user-selected project or target.
122 If `ede-object' is nil, then commands will operate on this object.")
124 (defvar ede-constructing nil
125 "Non nil when constructing a project hierarchy.
126 If the project is being constructed from an autoload, then the
127 value is the autoload object being used.")
129 (defvar ede-deep-rescan nil
130 "Non nil means scan down a tree, otherwise rescans are top level only.
131 Do not set this to non-nil globally. It is used internally.")
136 (defun ede-singular-object (prompt)
137 "Using PROMPT, choose a single object from the current buffer."
138 (if (listp ede-object
)
139 (ede-choose-object prompt ede-object
)
142 (defun ede-choose-object (prompt list-o-o
)
143 "Using PROMPT, ask the user which OBJECT to use based on the name field.
144 Argument LIST-O-O is the list of objects to choose from."
145 (let* ((al (object-assoc-list 'name list-o-o
))
146 (ans (completing-read prompt al nil t
)))
147 (setq ans
(assoc ans al
))
152 (defvar ede-minor-mode-map
153 (let ((map (make-sparse-keymap))
154 (pmap (make-sparse-keymap)))
155 (define-key pmap
"e" 'ede-edit-file-target
)
156 (define-key pmap
"a" 'ede-add-file
)
157 (define-key pmap
"d" 'ede-remove-file
)
158 (define-key pmap
"t" 'ede-new-target
)
159 (define-key pmap
"g" 'ede-rescan-toplevel
)
160 (define-key pmap
"s" 'ede-speedbar
)
161 (define-key pmap
"l" 'ede-load-project-file
)
162 (define-key pmap
"f" 'ede-find-file
)
163 (define-key pmap
"C" 'ede-compile-project
)
164 (define-key pmap
"c" 'ede-compile-target
)
165 (define-key pmap
"\C-c" 'ede-compile-selected
)
166 (define-key pmap
"D" 'ede-debug-target
)
167 (define-key pmap
"R" 'ede-run-target
)
168 ;; bind our submap into map
169 (define-key map
"\C-c." pmap
)
171 "Keymap used in project minor mode.")
173 (defvar global-ede-mode-map
174 (let ((map (make-sparse-keymap)))
175 (define-key map
[menu-bar cedet-menu
]
176 (cons "Development" cedet-menu-map
))
178 "Keymap used in `global-ede-mode'.")
180 ;; Activate the EDE items in cedet-menu-map
182 (define-key cedet-menu-map
[ede-find-file
]
183 '(menu-item "Find File in Project..." ede-find-file
:enable ede-object
184 :visible global-ede-mode
))
185 (define-key cedet-menu-map
[ede-speedbar
]
186 '(menu-item "View Project Tree" ede-speedbar
:enable ede-object
187 :visible global-ede-mode
))
188 (define-key cedet-menu-map
[ede]
189 '(menu-item "Load Project" ede
190 :visible global-ede-mode))
191 (define-key cedet-menu-map [ede-new]
192 '(menu-item "Create Project" ede-new
193 :enable (not ede-object)
194 :visible global-ede-mode))
195 (define-key cedet-menu-map [ede-target-options]
196 '(menu-item "Target Options" ede-target-options
197 :filter ede-target-forms-menu
198 :visible global-ede-mode))
199 (define-key cedet-menu-map [ede-project-options]
200 '(menu-item "Project Options" ede-project-options
201 :filter ede-project-forms-menu
202 :visible global-ede-mode))
203 (define-key cedet-menu-map [ede-build-forms-menu]
204 '(menu-item "Build Project" ede-build-forms-menu
205 :filter ede-build-forms-menu
207 :visible global-ede-mode))
209 (defun ede-buffer-belongs-to-target-p ()
210 "Return non-nil if this buffer belongs to at least one target."
211 (let ((obj ede-object))
213 (setq obj (car obj)))
214 (and obj (obj-of-class-p obj ede-target))))
216 (defun ede-buffer-belongs-to-project-p ()
217 "Return non-nil if this buffer belongs to at least one target."
218 (if (or (null ede-object) (consp ede-object)) nil
219 (obj-of-class-p ede-object ede-project)))
221 (defun ede-menu-obj-of-class-p (class)
222 "Return non-nil if some member of `ede-object' is a child of CLASS."
223 (if (listp ede-object)
224 (eval (cons 'or (mapcar (lambda (o) (obj-of-class-p o class)) ede-object)))
225 (obj-of-class-p ede-object class)))
227 (defun ede-build-forms-menu (menu-def)
228 "Create a sub menu for building different parts of an EDE system.
229 Argument MENU-DEF is the menu definition to use."
230 (easy-menu-filter-return
231 (easy-menu-create-menu
233 (let ((obj (ede-current-project))
234 (newmenu nil) ;'([ "Build Selected..." ede-compile-selected t ]))
241 (setq targets (when (slot-boundp obj 'targets)
243 ede-obj (if (listp ede-object) ede-object (list ede-object)))
244 ;; First, collect the build items from the project
245 (setq newmenu (append newmenu (ede-menu-items-build obj t)))
246 ;; Second, Declare the current target menu items
247 (if (and ede-obj (ede-menu-obj-of-class-p ede-target))
249 (setq newmenu (append newmenu
250 (ede-menu-items-build (car ede-obj) t))
252 ede-obj (cdr ede-obj))))
253 ;; Third, by name, enable builds for other local targets
255 (unless (eq tskip (car targets))
256 (setq targitems (ede-menu-items-build (car targets) nil))
259 (if (= 1 (length targitems))
261 (cons (ede-name (car targets))
264 (setq targets (cdr targets)))
265 ;; Fourth, build sub projects.
267 ;; Fifth, Add make distribution
268 (append newmenu (list [ "Make distribution" ede-make-dist t ]))
271 (defun ede-target-forms-menu (menu-def)
272 "Create a target MENU-DEF based on the object belonging to this buffer."
273 (easy-menu-filter-return
274 (easy-menu-create-menu
276 (let ((obj (or ede-selected-object ede-object)))
278 '([ "Add File" ede-add-file
279 (and (ede-current-project)
280 (oref (ede-current-project) targets)) ]
281 [ "Remove File" ede-remove-file
282 (ede-buffer-belongs-to-project-p) ]
286 (if (and (not (listp obj)) (oref obj menu))
289 ;; This is bad, but I'm not sure what else to do.
290 (oref (car obj) menu)))))))))
292 (defun ede-project-forms-menu (menu-def)
293 "Create a target MENU-DEF based on the object belonging to this buffer."
294 (easy-menu-filter-return
295 (easy-menu-create-menu
297 (let* ((obj (ede-current-project))
298 (class (if obj (object-class obj)))
302 (while (and class (slot-exists-p class 'menu))
303 ;;(message "Looking at class %S" class)
304 (setq menu (append menu (oref class menu))
305 class (class-parent class))
306 (if (listp class) (setq class (car class))))
308 '( [ "Add Target" ede-new-target (ede-current-project) ]
309 [ "Remove Target" ede-delete-target ede-object ]
313 (error (message "Err found: %S" err)
317 (defun ede-customize-forms-menu (menu-def)
318 "Create a menu of the project, and targets that can be customized.
319 Argument MENU-DEF is the definition of the current menu."
320 (easy-menu-filter-return
321 (easy-menu-create-menu
323 (let* ((obj (ede-current-project))
326 (setq targ (when (and obj (slot-boundp obj 'targets))
328 ;; Make custom menus for everything here.
330 (cons (concat "Project " (ede-name obj))
331 (eieio-customize-object-group obj))
332 [ "Reorder Targets" ede-project-sort-targets t ]
335 (cons (concat "Target " (ede-name o))
336 (eieio-customize-object-group o)))
340 (defun ede-apply-object-keymap (&optional default)
341 "Add target specific keybindings into the local map.
342 Optional argument DEFAULT indicates if this should be set to the default
343 version of the keymap."
344 (let ((object (or ede-object ede-selected-object)))
346 (let ((keys (ede-object-keybindings object)))
348 (local-set-key (concat "\C-c." (car (car keys)))
350 (setq keys (cdr keys))))
353 ;;; Menu building methods for building
355 (defmethod ede-menu-items-build ((obj ede-project) &optional current)
356 "Return a list of menu items for building project OBJ.
357 If optional argument CURRENT is non-nil, return sub-menu code."
359 (list [ "Build Current Project" ede-compile-project t ])
362 (concat "Build Project " (ede-name obj))
363 `(project-compile-project ,obj))))))
365 (defmethod ede-menu-items-build ((obj ede-target) &optional current)
366 "Return a list of menu items for building target OBJ.
367 If optional argument CURRENT is non-nil, return sub-menu code."
369 (list [ "Build Current Target" ede-compile-target t ])
371 (concat "Build Target " (ede-name obj))
372 `(project-compile-target ,obj)
375 ;;; Mode Declarations
378 (autoload 'ede-dired-minor-mode "ede/dired" "EDE commands for dired" t))
380 (defun ede-apply-target-options ()
381 "Apply options to the current buffer for the active project/target."
382 (if (ede-current-project)
383 (ede-set-project-variables (ede-current-project)))
384 (ede-apply-object-keymap)
385 (ede-apply-preprocessor-map)
388 (defun ede-turn-on-hook ()
389 "Turn on EDE minor mode in the current buffer if needed.
390 To be used in hook functions."
391 (if (or (and (stringp (buffer-file-name))
392 (stringp default-directory))
393 ;; Emacs 21 has no buffer file name for directory edits.
394 ;; so we need to add these hacks in.
395 (eq major-mode 'dired-mode)
396 (eq major-mode 'vc-dired-mode))
399 (define-minor-mode ede-minor-mode
400 "Toggle EDE (Emacs Development Environment) minor mode.
401 With non-nil argument ARG, enable EDE minor mode if ARG is
402 positive; otherwise, disable it.
404 If this file is contained, or could be contained in an EDE
405 controlled project, then this mode is activated automatically
406 provided `global-ede-mode' is enabled."
408 (cond ((or (eq major-mode 'dired-mode)
409 (eq major-mode 'vc-dired-mode))
410 (ede-dired-minor-mode (if ede-minor-mode 1 -1)))
412 (if (not ede-constructing)
413 (ede-initialize-state-current-buffer)
414 ;; If we fail to have a project here, turn it back off.
415 (ede-minor-mode -1)))))
417 (defun ede-initialize-state-current-buffer ()
418 "Initialize the current buffer's state for EDE.
419 Sets buffer local variables for EDE."
421 (proj (ede-directory-get-open-project default-directory
424 (ede-directory-project-p default-directory t))
427 ;; @todo - this could be wasteful.
428 (setq proj (ede-load-project-file default-directory 'ROOT)))
430 (setq ede-object (ede-buffer-object (current-buffer)
431 'ede-object-project))
433 (setq ede-object-root-project
434 (or ROOT (ede-project-root ede-object-project)))
436 (if (and (not ede-object) ede-object-project)
437 (ede-auto-add-to-target))
439 (ede-apply-target-options))))
441 (defun ede-reset-all-buffers (onoff)
442 "Reset all the buffers due to change in EDE.
443 ONOFF indicates enabling or disabling the mode."
444 (let ((b (buffer-list)))
446 (when (buffer-file-name (car b))
447 (with-current-buffer (car b)
448 ;; Reset all state variables
450 ede-object-project nil
451 ede-object-root-project nil)
452 ;; Now re-initialize this buffer.
453 (ede-initialize-state-current-buffer)
459 (define-minor-mode global-ede-mode
460 "Toggle global EDE (Emacs Development Environment) mode.
461 With non-nil argument ARG, enable global EDE mode if ARG is
462 positive; otherwise, disable it.
464 This global minor mode enables `ede-minor-mode' in all buffers in
465 an EDE controlled project."
469 ;; Turn on global-ede-mode
472 (define-key cedet-menu-map [cedet-menu-separator] '("--")))
473 (add-hook 'semanticdb-project-predicate-functions 'ede-directory-project-p)
474 (add-hook 'semanticdb-project-root-functions 'ede-toplevel-project-or-nil)
475 (add-hook 'ecb-source-path-functions 'ede-ecb-project-paths)
476 (add-hook 'find-file-hook 'ede-turn-on-hook)
477 (add-hook 'dired-mode-hook 'ede-turn-on-hook)
478 (add-hook 'kill-emacs-hook 'ede-save-cache)
480 (ede-reset-all-buffers 1))
481 ;; Turn off global-ede-mode
482 (define-key cedet-menu-map [cedet-menu-separator] nil)
483 (remove-hook 'semanticdb-project-predicate-functions 'ede-directory-project-p)
484 (remove-hook 'semanticdb-project-root-functions 'ede-toplevel-project-or-nil)
485 (remove-hook 'ecb-source-path-functions 'ede-ecb-project-paths)
486 (remove-hook 'find-file-hook 'ede-turn-on-hook)
487 (remove-hook 'dired-mode-hook 'ede-turn-on-hook)
488 (remove-hook 'kill-emacs-hook 'ede-save-cache)
490 (ede-reset-all-buffers -1)))
492 (defvar ede-ignored-file-alist
497 "List of file name patters that EDE will never ask about.")
499 (defun ede-ignore-file (filename)
500 "Should we ignore FILENAME?"
502 (F ede-ignored-file-alist))
503 (while (and (not any) F)
504 (when (string-match (car F) filename)
509 (defun ede-auto-add-to-target ()
510 "Look for a target that wants to own the current file.
511 Follow the preference set with `ede-auto-add-method' and get the list
512 of objects with the `ede-want-file-p' method."
513 (if ede-object (error "Ede-object already defined for %s" (buffer-name)))
514 (if (or (eq ede-auto-add-method 'never)
515 (ede-ignore-file (buffer-file-name)))
518 ;; Find all the objects.
519 (setq wants (oref (ede-current-project) targets))
521 (if (ede-want-file-p (car wants) (buffer-file-name))
522 (setq desires (cons (car wants) desires)))
523 (setq wants (cdr wants)))
525 (cond ((or (eq ede-auto-add-method 'ask)
526 (and (eq ede-auto-add-method 'multi-ask)
527 (< 1 (length desires))))
531 ("new target" . new))
532 ;; If we are in an unparented subdir,
533 ;; offer new a subproject
534 (if (ede-directory-project-p default-directory)
536 '(("create subproject" . project)))
537 ;; Here are the existing objects we want.
538 (object-assoc-list 'name desires)))
540 (ans (completing-read
541 (format "Add %s to target: " (buffer-file-name))
543 (setq ans (assoc ans al))
544 (cond ((eieio-object-p (cdr ans))
545 (ede-add-file (cdr ans)))
549 ((or (eq ede-auto-add-method 'always)
550 (and (eq ede-auto-add-method 'multi-ask)
551 (= 1 (length desires))))
552 (ede-add-file (car desires)))
556 ;;; Interactive method invocations
559 "Start up EDE on something.
560 Argument FILE is the file or directory to load a project from."
561 (interactive "fProject File: ")
562 (if (not (file-exists-p file))
564 (ede-load-project-file (file-name-directory file))))
566 (defun ede-new (type &optional name)
567 "Create a new project starting of project type TYPE.
568 Optional argument NAME is the name to give this project."
570 (list (completing-read "Project Type: "
573 (let* ((l ede-project-class-files)
574 (cp (ede-current-project))
575 (cs (when cp (object-class cp)))
579 (if (eq (oref (car l) :class-sym)
581 (setq r (cons (car l) r)))
582 (if (oref (car l) new-p)
583 (setq r (cons (car l) r))))
587 (error "No valid interactive sub project types for %s"
589 (error "EDE error: Can't fin project types to create")))
593 (require 'ede/custom)
594 ;; Make sure we have a valid directory
595 (when (not (file-exists-p default-directory))
596 (error "Cannot create project in non-existent directory %s" default-directory))
597 (when (not (file-writable-p default-directory))
598 (error "No write permissions for %s" default-directory))
599 ;; Create the project
600 (let* ((obj (object-assoc type 'name ede-project-class-files))
601 (nobj (let ((f (oref obj file))
602 (pf (oref obj proj-file)))
603 ;; We are about to make something new, changing the
604 ;; state of existing directories.
605 (ede-project-directory-remove-hash default-directory)
606 ;; Make sure this class gets loaded!
608 (make-instance (oref obj class-sym)
609 :name (or name (read-string "Name: "))
610 :directory default-directory
611 :file (cond ((stringp pf)
612 (expand-file-name pf))
617 "Unknown file name specifier %S"
620 (inits (oref obj initializers)))
621 ;; Force the name to match for new objects.
622 (object-set-name-string nobj (oref nobj :name))
625 (eieio-oset nobj (car inits) (car (cdr inits)))
626 (setq inits (cdr (cdr inits))))
627 (let ((pp (ede-parent-project)))
629 (ede-add-subproject pp nobj)
630 (ede-commit-project pp)))
631 (ede-commit-project nobj))
632 ;; Have the menu appear
633 (setq ede-minor-mode t)
635 (message "Project created and saved. You may now create targets."))
637 (defmethod ede-add-subproject ((proj-a ede-project) proj-b)
638 "Add into PROJ-A, the subproject PROJ-B."
639 (oset proj-a subproj (cons proj-b (oref proj-a subproj))))
641 (defun ede-invoke-method (sym &rest args)
642 "Invoke method SYM on the current buffer's project object.
643 ARGS are additional arguments to pass to method sym."
645 (error "Cannot invoke %s for %s" (symbol-name sym)
647 ;; Always query a target. There should never be multiple
648 ;; projects in a single buffer.
649 (apply sym (ede-singular-object "Target: ") args))
651 (defun ede-rescan-toplevel ()
652 "Rescan all project files."
654 (let ((toppath (ede-toplevel-project default-directory))
656 (project-rescan (ede-load-project-file toppath))
657 (ede-reset-all-buffers 1)
660 (defun ede-new-target (&rest args)
661 "Create a new target specific to this type of project file.
662 Different projects accept different arguments ARGS.
663 Typically you can specify NAME, target TYPE, and AUTOADD, where AUTOADD is
664 a string \"y\" or \"n\", which answers the y/n question done interactively."
666 (apply 'project-new-target (ede-current-project) args)
667 (setq ede-object nil)
668 (setq ede-object (ede-buffer-object (current-buffer)))
669 (ede-apply-target-options))
671 (defun ede-new-target-custom ()
672 "Create a new target specific to this type of project file."
674 (project-new-target-custom (ede-current-project)))
676 (defun ede-delete-target (target)
677 "Delete TARGET from the current project."
679 (let ((ede-object (ede-current-project)))
680 (ede-invoke-method 'project-interactive-select-target
682 ;; Find all sources in buffers associated with the condemned buffer.
683 (let ((condemned (ede-target-buffers target)))
684 (project-delete-target target)
685 ;; Loop over all project controlled buffers
688 (set-buffer (car condemned))
689 (setq ede-object nil)
690 (setq ede-object (ede-buffer-object (current-buffer)))
691 (setq condemned (cdr condemned))))
692 (ede-apply-target-options)))
694 (defun ede-add-file (target)
695 "Add the current buffer to a TARGET in the current project."
697 (let ((ede-object (ede-current-project)))
698 (ede-invoke-method 'project-interactive-select-target
700 (when (stringp target)
701 (let* ((proj (ede-current-project))
702 (ob (object-assoc-list 'name (oref proj targets))))
703 (setq target (cdr (assoc target ob)))))
706 (error "Could not find specified target %S" target))
708 (project-add-file target (buffer-file-name))
709 (setq ede-object nil)
710 (setq ede-object (ede-buffer-object (current-buffer)))
711 (when (not ede-object)
712 (error "Can't add %s to target %s: Wrong file type"
713 (file-name-nondirectory (buffer-file-name))
714 (object-name target)))
715 (ede-apply-target-options))
717 (defun ede-remove-file (&optional force)
718 "Remove the current file from targets.
719 Optional argument FORCE forces the file to be removed without asking."
722 (error "Cannot invoke remove-file for %s" (buffer-name)))
723 (let ((eo (if (listp ede-object)
729 (if (or force (y-or-n-p (format "Remove from %s? " (ede-name (car eo)))))
730 (project-remove-file (car eo) (buffer-file-name)))
732 (setq ede-object nil)
733 (setq ede-object (ede-buffer-object (current-buffer)))
734 (ede-apply-target-options)))
736 (defun ede-edit-file-target ()
737 "Enter the project file to hand edit the current buffer's target."
739 (ede-invoke-method 'project-edit-file-target))
741 (defun ede-compile-project ()
742 "Compile the current project."
744 ;; @TODO - This just wants the root. There should be a better way.
745 (let ((cp (ede-current-project)))
746 (while (ede-parent-project cp)
747 (setq cp (ede-parent-project cp)))
748 (let ((ede-object cp))
749 (ede-invoke-method 'project-compile-project))))
751 (defun ede-compile-selected (target)
752 "Compile some TARGET from the current project."
753 (interactive (list (project-interactive-select-target (ede-current-project)
754 "Target to Build: ")))
755 (project-compile-target target))
757 (defun ede-compile-target ()
758 "Compile the current buffer's associated target."
760 (ede-invoke-method 'project-compile-target))
762 (defun ede-debug-target ()
763 "Debug the current buffer's associated target."
765 (ede-invoke-method 'project-debug-target))
767 (defun ede-run-target ()
768 "Run the current buffer's associated target."
770 (ede-invoke-method 'project-run-target))
772 (defun ede-make-dist ()
773 "Create a distribution from the current project."
775 (let ((ede-object (ede-toplevel)))
776 (ede-invoke-method 'project-make-dist)))
779 ;;; EDE project target baseline methods.
781 ;; If you are developing a new project type, you need to implement
782 ;; all of these methods, unless, of course, they do not make sense
783 ;; for your particular project.
785 ;; Your targets should inherit from `ede-target', and your project
786 ;; files should inherit from `ede-project'. Create the appropriate
787 ;; methods based on those below.
789 (defmethod project-interactive-select-target ((this ede-project-placeholder) prompt)
790 ; checkdoc-params: (prompt)
791 "Make sure placeholder THIS is replaced with the real thing, and pass through."
792 (project-interactive-select-target this prompt))
794 (defmethod project-interactive-select-target ((this ede-project) prompt)
795 "Interactively query for a target that exists in project THIS.
796 Argument PROMPT is the prompt to use when querying the user for a target."
797 (let ((ob (object-assoc-list 'name (oref this targets))))
798 (cdr (assoc (completing-read prompt ob nil t) ob))))
800 (defmethod project-add-file ((this ede-project-placeholder) file)
801 ; checkdoc-params: (file)
802 "Make sure placeholder THIS is replaced with the real thing, and pass through."
803 (project-add-file this file))
805 (defmethod project-add-file ((ot ede-target) file)
806 "Add the current buffer into project project target OT.
807 Argument FILE is the file to add."
808 (error "add-file not supported by %s" (object-name ot)))
810 (defmethod project-remove-file ((ot ede-target) fnnd)
811 "Remove the current buffer from project target OT.
812 Argument FNND is an argument."
813 (error "remove-file not supported by %s" (object-name ot)))
815 (defmethod project-edit-file-target ((ot ede-target))
816 "Edit the target OT associated w/ this file."
817 (find-file (oref (ede-current-project) file)))
819 (defmethod project-new-target ((proj ede-project) &rest args)
820 "Create a new target. It is up to the project PROJ to get the name."
821 (error "new-target not supported by %s" (object-name proj)))
823 (defmethod project-new-target-custom ((proj ede-project))
824 "Create a new target. It is up to the project PROJ to get the name."
825 (error "New-target-custom not supported by %s" (object-name proj)))
827 (defmethod project-delete-target ((ot ede-target))
828 "Delete the current target OT from its parent project."
829 (error "add-file not supported by %s" (object-name ot)))
831 (defmethod project-compile-project ((obj ede-project) &optional command)
832 "Compile the entire current project OBJ.
833 Argument COMMAND is the command to use when compiling."
834 (error "compile-project not supported by %s" (object-name obj)))
836 (defmethod project-compile-target ((obj ede-target) &optional command)
837 "Compile the current target OBJ.
838 Argument COMMAND is the command to use for compiling the target."
839 (error "compile-target not supported by %s" (object-name obj)))
841 (defmethod project-debug-target ((obj ede-target))
842 "Run the current project target OBJ in a debugger."
843 (error "debug-target not supported by %s" (object-name obj)))
845 (defmethod project-run-target ((obj ede-target))
846 "Run the current project target OBJ."
847 (error "run-target not supported by %s" (object-name obj)))
849 (defmethod project-make-dist ((this ede-project))
850 "Build a distribution for the project based on THIS project."
851 (error "Make-dist not supported by %s" (object-name this)))
853 (defmethod project-dist-files ((this ede-project))
854 "Return a list of files that constitute a distribution of THIS project."
855 (error "Dist-files is not supported by %s" (object-name this)))
857 (defmethod project-rescan ((this ede-project))
858 "Rescan the EDE proj project THIS."
859 (error "Rescanning a project is not supported by %s" (object-name this)))
861 (defun ede-ecb-project-paths ()
862 "Return a list of all paths for all active EDE projects.
863 This functions is meant for use with ECB."
864 (let ((p ede-projects)
867 (setq d (cons (file-name-directory (oref (car p) file))
872 ;;; PROJECT LOADING/TRACKING
874 (defun ede-add-project-to-global-list (proj)
875 "Add the project PROJ to the master list of projects.
876 On success, return the added project."
878 (error "No project created to add to master list"))
879 (when (not (eieio-object-p proj))
880 (error "Attempt to add Non-object to master project list"))
881 (when (not (obj-of-class-p proj ede-project-placeholder))
882 (error "Attempt to add a non-project to the ede projects list"))
883 (add-to-list 'ede-projects proj)
886 (defun ede-load-project-file (dir &optional rootreturn)
887 "Project file independent way to read a project in from DIR.
888 Optional ROOTRETURN will return the root project for DIR."
889 ;; Only load if something new is going on. Flush the dirhash.
890 (ede-project-directory-remove-hash dir)
892 ;;(message "EDE LOAD : %S" file)
894 (path (expand-file-name (file-name-directory file)))
895 (pfc (ede-directory-project-p path))
900 ;; @TODO - Do we really need to scan? Is this a waste of time?
901 ;; Scan upward for a the next project file style.
903 (while (and p (not (ede-directory-project-p p)))
904 (setq p (ede-up-directory p)))
905 (if p (ede-load-project-file p)
907 ;; recomment as we go
910 ;; Do nothing if we are buiding an EDE project already
913 ;; Load in the project in question.
915 (setq toppath (ede-toplevel-project path))
916 ;; We found the top-most directory. Check to see if we already
917 ;; have an object defining its project.
918 (setq pfc (ede-directory-project-p toppath t))
920 ;; See if it's been loaded before
921 (setq o (object-assoc (ede-dir-to-projectfile pfc toppath) 'file
924 ;; If not, get it now.
925 (let ((ede-constructing pfc))
926 (setq o (funcall (oref pfc load-type) toppath))
928 (error "Project type error: :load-type failed to create a project"))
929 (ede-add-project-to-global-list o)))
931 ;; Return the found root project.
932 (when rootreturn (set rootreturn o))
935 ;; Now find the project file belonging to FILE!
936 (setq tocheck (list o))
937 (setq file (ede-dir-to-projectfile pfc (expand-file-name path)))
938 (while (and tocheck (not found))
941 (if (string= file (oref (car tocheck) file))
942 (setq found (car tocheck)))
943 (setq newbits (oref (car tocheck) subproj)))
945 (append (cdr tocheck) newbits))))
947 (message "No project for %s, but passes project-p test" file)
948 ;; Now that the file has been reset inside the project object, do
949 ;; the cache maintenance.
950 (setq ede-project-cache-files
951 (delete (oref found file) ede-project-cache-files)))
954 ;;; PROJECT ASSOCIATIONS
956 ;; Moving between relative projects. Associating between buffers and
959 (defun ede-parent-project (&optional obj)
960 "Return the project belonging to the parent directory.
961 Return nil if there is no previous directory.
962 Optional argument OBJ is an object to find the parent of."
963 (let* ((proj (or obj ede-object-project)) ;; Current project.
964 (root (if obj (ede-project-root obj)
965 ede-object-root-project)))
966 ;; This case is a SHORTCUT if the project has defined
967 ;; a way to calculate the project root.
968 (if (and root proj (eq root proj))
969 nil ;; we are at the root.
970 ;; Else, we may have a nil proj or root.
971 (let* ((thisdir (if obj (oref obj directory)
973 (updir (ede-up-directory thisdir)))
975 ;; If there was no root, perhaps we can derive it from
977 (let ((root (or root (ede-directory-get-toplevel-open-project updir))))
979 ;; This lets us find a subproject under root based on updir.
981 (ede-find-subproject-for-directory root updir))
982 ;; Try the all structure based search.
983 (ede-directory-get-open-project updir)
984 ;; Load up the project file as a last resort.
985 ;; Last resort since it uses file-truename, and other
987 (and (ede-directory-project-p updir)
988 (ede-load-project-file
989 (file-name-as-directory updir))))))))))
991 (defun ede-current-project (&optional dir)
992 "Return the current project file.
993 If optional DIR is provided, get the project for DIR instead."
995 ;; If it matches the current directory, do we have a pre-existing project?
996 (when (and (or (not dir) (string= dir default-directory))
998 (setq ans ede-object-project)
1000 ;; No current project.
1002 (let* ((ldir (or dir default-directory)))
1003 (setq ans (ede-directory-get-open-project ldir))
1005 ;; No open project, if this dir pass project-p, then load.
1006 (when (ede-directory-project-p ldir)
1007 (setq ans (ede-load-project-file ldir))))))
1008 ;; Return what we found.
1011 (defun ede-buffer-object (&optional buffer projsym)
1012 "Return the target object for BUFFER.
1013 This function clears cached values and recalculates.
1014 Optional PROJSYM is a symbol, which will be set to the project
1015 that contains the target that becomes buffer's object."
1017 (if (not buffer) (setq buffer (current-buffer)))
1019 (setq ede-object nil)
1020 (let* ((localpo (ede-current-project))
1022 (top (ede-toplevel po)))
1023 (if po (setq ede-object (ede-find-target po buffer)))
1024 ;; If we get nothing, go with the backup plan of slowly
1026 (while (and (not ede-object) (not (eq po top)))
1027 (setq po (ede-parent-project po))
1028 (if po (setq ede-object (ede-find-target po buffer))))
1029 ;; Filter down to 1 project if there are dups.
1030 (if (= (length ede-object) 1)
1031 (setq ede-object (car ede-object)))
1032 ;; Track the project, if needed.
1033 (when (and projsym (symbolp projsym))
1035 ;; If we found a target, then PO is the
1038 ;; If there is no ede-object, then the projsym
1039 ;; is whichever part of the project is most local.
1040 (set projsym localpo))
1042 ;; Return our findings.
1045 (defmethod ede-target-in-project-p ((proj ede-project) target)
1046 "Is PROJ the parent of TARGET?
1047 If TARGET belongs to a subproject, return that project file."
1048 (if (and (slot-boundp proj 'targets)
1049 (memq target (oref proj targets)))
1051 (let ((s (oref proj subproj))
1053 (while (and s (not ans))
1054 (setq ans (ede-target-in-project-p (car s) target))
1058 (defun ede-target-parent (target)
1059 "Return the project which is the parent of TARGET.
1060 It is recommended you track the project a different way as this function
1061 could become slow in time."
1062 ;; @todo - use ede-object-project as a starting point.
1063 (let ((ans nil) (projs ede-projects))
1064 (while (and (not ans) projs)
1065 (setq ans (ede-target-in-project-p (car projs) target)
1069 (defmethod ede-find-target ((proj ede-project) buffer)
1070 "Fetch the target in PROJ belonging to BUFFER or nil."
1071 (with-current-buffer buffer
1073 (if (ede-buffer-mine proj buffer)
1075 (let ((targets (oref proj targets))
1078 (if (ede-buffer-mine (car targets) buffer)
1079 (setq f (cons (car targets) f)))
1080 (setq targets (cdr targets)))
1083 (defmethod ede-target-buffer-in-sourcelist ((this ede-target) buffer source)
1084 "Return non-nil if object THIS is in BUFFER to a SOURCE list.
1085 Handles complex path issues."
1086 (member (ede-convert-path this (buffer-file-name buffer)) source))
1088 (defmethod ede-buffer-mine ((this ede-project) buffer)
1089 "Return non-nil if object THIS lays claim to the file in BUFFER."
1092 (defmethod ede-buffer-mine ((this ede-target) buffer)
1093 "Return non-nil if object THIS lays claim to the file in BUFFER."
1095 (ede-target-buffer-in-sourcelist this buffer (oref this source))
1096 ;; An error implies a bad match.
1102 (defun ede-project-buffers (project)
1103 "Return a list of all active buffers controlled by PROJECT.
1104 This includes buffers controlled by a specific target of PROJECT."
1105 (let ((bl (buffer-list))
1108 (with-current-buffer (car bl)
1109 (if (ede-buffer-belongs-to-project-p)
1110 (setq pl (cons (car bl) pl))))
1114 (defun ede-target-buffers (target)
1115 "Return a list of buffers that are controlled by TARGET."
1116 (let ((bl (buffer-list))
1119 (with-current-buffer (car bl)
1120 (if (if (listp ede-object)
1121 (memq target ede-object)
1122 (eq ede-object target))
1123 (setq pl (cons (car bl) pl))))
1127 (defun ede-buffers ()
1128 "Return a list of all buffers controlled by an EDE object."
1129 (let ((bl (buffer-list))
1132 (with-current-buffer (car bl)
1134 (setq pl (cons (car bl) pl))))
1138 (defun ede-map-buffers (proc)
1139 "Execute PROC on all buffers controlled by EDE."
1140 (mapcar proc (ede-buffers)))
1142 (defmethod ede-map-project-buffers ((this ede-project) proc)
1143 "For THIS, execute PROC on all buffers belonging to THIS."
1144 (mapcar proc (ede-project-buffers this)))
1146 (defmethod ede-map-target-buffers ((this ede-target) proc)
1147 "For THIS, execute PROC on all buffers belonging to THIS."
1148 (mapcar proc (ede-target-buffers this)))
1150 ;; other types of mapping
1151 (defmethod ede-map-subprojects ((this ede-project) proc)
1152 "For object THIS, execute PROC on all direct subprojects.
1153 This function does not apply PROC to sub-sub projects.
1154 See also `ede-map-all-subprojects'."
1155 (mapcar proc (oref this subproj)))
1157 (defmethod ede-map-all-subprojects ((this ede-project) allproc)
1158 "For object THIS, execute PROC on THIS and all subprojects.
1159 This function also applies PROC to sub-sub projects.
1160 See also `ede-map-subprojects'."
1162 (list (funcall allproc this))
1163 (ede-map-subprojects
1166 (ede-map-all-subprojects sp allproc))
1169 ;; (ede-map-all-subprojects (ede-load-project-file "../semantic/") (lambda (sp) (oref sp file)))
1171 (defmethod ede-map-targets ((this ede-project) proc)
1172 "For object THIS, execute PROC on all targets."
1173 (mapcar proc (oref this targets)))
1175 (defmethod ede-map-any-target-p ((this ede-project) proc)
1176 "For project THIS, map PROC to all targets and return if any non-nil.
1177 Return the first non-nil value returned by PROC."
1178 (eval (cons 'or (ede-map-targets this proc))))
1182 (defun ede-maybe-checkout (&optional buffer)
1183 "Check BUFFER out of VC if necessary."
1185 (if buffer (set-buffer buffer))
1186 (if (and buffer-read-only vc-mode
1187 (y-or-n-p "Checkout Makefile.am from VC? "))
1188 (vc-toggle-read-only))))
1191 ;;; Some language specific methods.
1193 ;; These items are needed by ede-cpp-root to add better support for
1194 ;; configuring items for Semantic.
1195 (defun ede-apply-preprocessor-map ()
1196 "Apply preprocessor tables onto the current buffer."
1197 (when (and ede-object (boundp 'semantic-lex-spp-macro-symbol-obarray))
1198 (let* ((objs ede-object)
1199 (map (ede-preprocessor-map (if (consp objs)
1203 ;; We can't do a require for the below symbol.
1204 (setq semantic-lex-spp-macro-symbol-obarray
1205 (semantic-lex-make-spp-table map)))
1207 (message "Choosing preprocessor syms for project %s"
1208 (object-name (car objs)))))))
1210 (defmethod ede-system-include-path ((this ede-project))
1211 "Get the system include path used by project THIS."
1214 (defmethod ede-preprocessor-map ((this ede-project))
1215 "Get the pre-processor map for project THIS."
1218 (defmethod ede-system-include-path ((this ede-target))
1219 "Get the system include path used by project THIS."
1222 (defmethod ede-preprocessor-map ((this ede-target))
1223 "Get the pre-processor map for project THIS."
1227 ;;; Project-local variables
1229 (defun ede-make-project-local-variable (variable &optional project)
1230 "Make VARIABLE project-local to PROJECT."
1231 (if (not project) (setq project (ede-current-project)))
1232 (if (assoc variable (oref project local-variables))
1234 (oset project local-variables (cons (list variable)
1235 (oref project local-variables)))
1236 (dolist (b (ede-project-buffers project))
1237 (with-current-buffer b
1238 (make-local-variable variable)))))
1240 (defmethod ede-set-project-variables ((project ede-project) &optional buffer)
1241 "Set variables local to PROJECT in BUFFER."
1242 (if (not buffer) (setq buffer (current-buffer)))
1243 (with-current-buffer buffer
1244 (dolist (v (oref project local-variables))
1245 (make-local-variable (car v))
1246 ;; set its value here?
1247 (set (car v) (cdr v)))))
1249 (defun ede-set (variable value &optional proj)
1250 "Set the project local VARIABLE to VALUE.
1251 If VARIABLE is not project local, just use set. Optional argument PROJ
1252 is the project to use, instead of `ede-current-project'."
1253 (let ((p (or proj (ede-current-project)))
1255 (if (and p (setq a (assoc variable (oref p local-variables))))
1258 (dolist (b (ede-project-buffers p))
1259 (with-current-buffer b
1260 (set variable value))))
1261 (set variable value))
1262 (ede-commit-local-variables p))
1265 (defmethod ede-commit-local-variables ((proj ede-project))
1266 "Commit change to local variables in PROJ."
1271 ;; Include this last because it depends on ede.
1272 (require 'ede/files)
1274 ;; If this does not occur after the provide, we can get a recursive
1276 (if (featurep 'speedbar)
1277 (ede-speedbar-file-setup)
1278 (add-hook 'speedbar-load-hook 'ede-speedbar-file-setup))
1280 ;; arch-tag: 0e1e0eba-484f-4119-abdb-30951f725705
1281 ;;; ede.el ends here