1 ;;; ede.el --- Emacs Development Environment gloss
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 ;; 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: project, make
10 ;; This file is part of GNU Emacs.
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 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; EDE is the top level Lisp interface to a project management scheme
28 ;; for Emacs. Emacs does many things well, including editing,
29 ;; building, and debugging. Folks migrating from other IDEs don't
30 ;; seem to think this qualifies, however, because they still have to
31 ;; write the makefiles, and specify parameters to programs.
33 ;; This EDE mode will attempt to link these diverse programs together
34 ;; into a comprehensive single interface, instead of a bunch of
39 ;; This command enables project mode on all files.
41 ;; (global-ede-mode t)
45 (require 'eieio-speedbar)
50 (load "ede/loaddefs" nil 'nomessage)
52 (declare-function ede-commit-project "ede/custom")
53 (declare-function ede-convert-path "ede/files")
54 (declare-function ede-directory-get-open-project "ede/files")
55 (declare-function ede-directory-get-toplevel-open-project "ede/files")
56 (declare-function ede-directory-project-p "ede/files")
57 (declare-function ede-find-subproject-for-directory "ede/files")
58 (declare-function ede-project-directory-remove-hash "ede/files")
59 (declare-function ede-toplevel "ede/base")
60 (declare-function ede-toplevel-project "ede/files")
61 (declare-function ede-up-directory "ede/files")
62 (declare-function semantic-lex-make-spp-table "semantic/lex-spp")
64 (defconst ede-version "1.0"
65 "Current version of the Emacs EDE.")
69 "Display the current running version of EDE."
70 (interactive) (message "EDE %s" ede-version))
73 "Emacs Development Environment."
77 (defcustom ede-auto-add-method 'ask
78 "Whether a new source file should be automatically added to a target.
79 Whenever a new file is encountered in a directory controlled by a
80 project file, all targets are queried to see if it should be added.
81 If the value is 'always, then the new file is added to the first
82 target encountered. If the value is 'multi-ask, then if more than one
83 target wants the file, the user is asked. If only one target wants
84 the file, then then it is automatically added to that target. If the
85 value is 'ask, then the user is always asked, unless there is no
86 target willing to take the file. 'never means never perform the check."
88 :type '(choice (const always)
93 (defcustom ede-debug-program-function 'gdb
94 "Default Emacs command used to debug a target."
96 :type 'sexp) ; make this be a list of options some day
99 ;;; Management variables
101 (defvar ede-projects nil
102 "A list of all active projects currently loaded in Emacs.")
104 (defvar ede-object-root-project nil
105 "The current buffer's current root project.
106 If a file is under a project, this specifies the project that is at
107 the root of a project tree.")
108 (make-variable-buffer-local 'ede-object-root-project)
110 (defvar ede-object-project nil
111 "The current buffer's current project at that level.
112 If a file is under a project, this specifies the project that contains the
114 (make-variable-buffer-local 'ede-object-project)
116 (defvar ede-object nil
117 "The current buffer's target object.
118 This object's class determines how to compile and debug from a buffer.")
119 (make-variable-buffer-local 'ede-object)
121 (defvar ede-selected-object nil
122 "The currently user-selected project or target.
123 If `ede-object' is nil, then commands will operate on this object.")
125 (defvar ede-constructing nil
126 "Non nil when constructing a project hierarchy.
127 If the project is being constructed from an autoload, then the
128 value is the autoload object being used.")
130 (defvar ede-deep-rescan nil
131 "Non nil means scan down a tree, otherwise rescans are top level only.
132 Do not set this to non-nil globally. It is used internally.")
137 (defun ede-singular-object (prompt)
138 "Using PROMPT, choose a single object from the current buffer."
139 (if (listp ede-object)
140 (ede-choose-object prompt ede-object)
143 (defun ede-choose-object (prompt list-o-o)
144 "Using PROMPT, ask the user which OBJECT to use based on the name field.
145 Argument LIST-O-O is the list of objects to choose from."
146 (let* ((al (object-assoc-list 'name list-o-o))
147 (ans (completing-read prompt al nil t)))
148 (setq ans (assoc ans al))
153 (defvar ede-minor-mode-map
154 (let ((map (make-sparse-keymap))
155 (pmap (make-sparse-keymap)))
156 (define-key pmap "e" 'ede-edit-file-target)
157 (define-key pmap "a" 'ede-add-file)
158 (define-key pmap "d" 'ede-remove-file)
159 (define-key pmap "t" 'ede-new-target)
160 (define-key pmap "g" 'ede-rescan-toplevel)
161 (define-key pmap "s" 'ede-speedbar)
162 (define-key pmap "l" 'ede-load-project-file)
163 (define-key pmap "f" 'ede-find-file)
164 (define-key pmap "C" 'ede-compile-project)
165 (define-key pmap "c" 'ede-compile-target)
166 (define-key pmap "\C-c" 'ede-compile-selected)
167 (define-key pmap "D" 'ede-debug-target)
168 (define-key pmap "R" 'ede-run-target)
169 ;; bind our submap into map
170 (define-key map "\C-c." pmap)
172 "Keymap used in project minor mode.")
174 (defvar global-ede-mode-map
175 (let ((map (make-sparse-keymap)))
176 (define-key map [menu-bar cedet-menu]
177 (cons "Development" cedet-menu-map))
179 "Keymap used in `global-ede-mode'.")
181 ;; Activate the EDE items in cedet-menu-map
183 (define-key cedet-menu-map [ede-find-file]
184 '(menu-item "Find File in Project..." ede-find-file :enable ede-object
185 :visible global-ede-mode))
186 (define-key cedet-menu-map [ede-speedbar]
187 '(menu-item "View Project Tree" ede-speedbar :enable ede-object
188 :visible global-ede-mode))
189 (define-key cedet-menu-map [ede]
190 '(menu-item "Load Project" ede
191 :visible global-ede-mode))
192 (define-key cedet-menu-map [ede-new]
193 '(menu-item "Create Project" ede-new
194 :enable (not ede-object)
195 :visible global-ede-mode))
196 (define-key cedet-menu-map [ede-target-options]
197 '(menu-item "Target Options" ede-target-options
198 :filter ede-target-forms-menu
199 :visible global-ede-mode))
200 (define-key cedet-menu-map [ede-project-options]
201 '(menu-item "Project Options" ede-project-options
202 :filter ede-project-forms-menu
203 :visible global-ede-mode))
204 (define-key cedet-menu-map [ede-build-forms-menu]
205 '(menu-item "Build Project" ede-build-forms-menu
206 :filter ede-build-forms-menu
208 :visible global-ede-mode))
210 (defun ede-buffer-belongs-to-target-p ()
211 "Return non-nil if this buffer belongs to at least one target."
212 (let ((obj ede-object))
214 (setq obj (car obj)))
215 (and obj (obj-of-class-p obj ede-target))))
217 (defun ede-buffer-belongs-to-project-p ()
218 "Return non-nil if this buffer belongs to at least one target."
219 (if (or (null ede-object) (consp ede-object)) nil
220 (obj-of-class-p ede-object ede-project)))
222 (defun ede-menu-obj-of-class-p (class)
223 "Return non-nil if some member of `ede-object' is a child of CLASS."
224 (if (listp ede-object)
225 (eval (cons 'or (mapcar (lambda (o) (obj-of-class-p o class)) ede-object)))
226 (obj-of-class-p ede-object class)))
228 (defun ede-build-forms-menu (menu-def)
229 "Create a sub menu for building different parts of an EDE system.
230 Argument MENU-DEF is the menu definition to use."
231 (easy-menu-filter-return
232 (easy-menu-create-menu
234 (let ((obj (ede-current-project))
235 (newmenu nil) ;'([ "Build Selected..." ede-compile-selected t ]))
242 (setq targets (when (slot-boundp obj 'targets)
244 ede-obj (if (listp ede-object) ede-object (list ede-object)))
245 ;; First, collect the build items from the project
246 (setq newmenu (append newmenu (ede-menu-items-build obj t)))
247 ;; Second, Declare the current target menu items
248 (if (and ede-obj (ede-menu-obj-of-class-p ede-target))
250 (setq newmenu (append newmenu
251 (ede-menu-items-build (car ede-obj) t))
253 ede-obj (cdr ede-obj))))
254 ;; Third, by name, enable builds for other local targets
256 (unless (eq tskip (car targets))
257 (setq targitems (ede-menu-items-build (car targets) nil))
260 (if (= 1 (length targitems))
262 (cons (ede-name (car targets))
265 (setq targets (cdr targets)))
266 ;; Fourth, build sub projects.
268 ;; Fifth, Add make distribution
269 (append newmenu (list [ "Make distribution" ede-make-dist t ]))
272 (defun ede-target-forms-menu (menu-def)
273 "Create a target MENU-DEF based on the object belonging to this buffer."
274 (easy-menu-filter-return
275 (easy-menu-create-menu
277 (let ((obj (or ede-selected-object ede-object)))
279 '([ "Add File" ede-add-file
280 (and (ede-current-project)
281 (oref (ede-current-project) targets)) ]
282 [ "Remove File" ede-remove-file
283 (ede-buffer-belongs-to-project-p) ]
287 (if (and (not (listp obj)) (oref obj menu))
290 ;; This is bad, but I'm not sure what else to do.
291 (oref (car obj) menu)))))))))
293 (defun ede-project-forms-menu (menu-def)
294 "Create a target MENU-DEF based on the object belonging to this buffer."
295 (easy-menu-filter-return
296 (easy-menu-create-menu
298 (let* ((obj (ede-current-project))
299 (class (if obj (object-class obj)))
303 (while (and class (slot-exists-p class 'menu))
304 ;;(message "Looking at class %S" class)
305 (setq menu (append menu (oref class menu))
306 class (class-parent class))
307 (if (listp class) (setq class (car class))))
309 '( [ "Add Target" ede-new-target (ede-current-project) ]
310 [ "Remove Target" ede-delete-target ede-object ]
314 (error (message "Err found: %S" err)
318 (defun ede-customize-forms-menu (menu-def)
319 "Create a menu of the project, and targets that can be customized.
320 Argument MENU-DEF is the definition of the current menu."
321 (easy-menu-filter-return
322 (easy-menu-create-menu
324 (let* ((obj (ede-current-project))
327 (setq targ (when (and obj (slot-boundp obj 'targets))
329 ;; Make custom menus for everything here.
331 (cons (concat "Project " (ede-name obj))
332 (eieio-customize-object-group obj))
333 [ "Reorder Targets" ede-project-sort-targets t ]
336 (cons (concat "Target " (ede-name o))
337 (eieio-customize-object-group o)))
341 (defun ede-apply-object-keymap (&optional default)
342 "Add target specific keybindings into the local map.
343 Optional argument DEFAULT indicates if this should be set to the default
344 version of the keymap."
345 (let ((object (or ede-object ede-selected-object)))
347 (let ((keys (ede-object-keybindings object)))
349 (local-set-key (concat "\C-c." (car (car keys)))
351 (setq keys (cdr keys))))
354 ;;; Menu building methods for building
356 (defmethod ede-menu-items-build ((obj ede-project) &optional current)
357 "Return a list of menu items for building project OBJ.
358 If optional argument CURRENT is non-nil, return sub-menu code."
360 (list [ "Build Current Project" ede-compile-project t ])
363 (concat "Build Project " (ede-name obj))
364 `(project-compile-project ,obj))))))
366 (defmethod ede-menu-items-build ((obj ede-target) &optional current)
367 "Return a list of menu items for building target OBJ.
368 If optional argument CURRENT is non-nil, return sub-menu code."
370 (list [ "Build Current Target" ede-compile-target t ])
372 (concat "Build Target " (ede-name obj))
373 `(project-compile-target ,obj)
376 ;;; Mode Declarations
379 (autoload 'ede-dired-minor-mode "ede/dired" "EDE commands for dired" t))
381 (defun ede-apply-target-options ()
382 "Apply options to the current buffer for the active project/target."
383 (if (ede-current-project)
384 (ede-set-project-variables (ede-current-project)))
385 (ede-apply-object-keymap)
386 (ede-apply-preprocessor-map)
389 (defun ede-turn-on-hook ()
390 "Turn on EDE minor mode in the current buffer if needed.
391 To be used in hook functions."
392 (if (or (and (stringp (buffer-file-name))
393 (stringp default-directory))
394 ;; Emacs 21 has no buffer file name for directory edits.
395 ;; so we need to add these hacks in.
396 (eq major-mode 'dired-mode)
397 (eq major-mode 'vc-dired-mode))
400 (define-minor-mode ede-minor-mode
401 "Toggle EDE (Emacs Development Environment) minor mode.
402 With non-nil argument ARG, enable EDE minor mode if ARG is
403 positive; otherwise, disable it.
405 If this file is contained, or could be contained in an EDE
406 controlled project, then this mode is activated automatically
407 provided `global-ede-mode' is enabled."
409 (cond ((or (eq major-mode 'dired-mode)
410 (eq major-mode 'vc-dired-mode))
411 (ede-dired-minor-mode (if ede-minor-mode 1 -1)))
413 (if (not ede-constructing)
414 (ede-initialize-state-current-buffer)
415 ;; If we fail to have a project here, turn it back off.
416 (ede-minor-mode -1)))))
418 (defun ede-initialize-state-current-buffer ()
419 "Initialize the current buffer's state for EDE.
420 Sets buffer local variables for EDE."
422 (proj (ede-directory-get-open-project default-directory
425 (ede-directory-project-p default-directory t))
428 ;; @todo - this could be wasteful.
429 (setq proj (ede-load-project-file default-directory 'ROOT)))
431 (setq ede-object (ede-buffer-object (current-buffer)
432 'ede-object-project))
434 (setq ede-object-root-project
435 (or ROOT (ede-project-root ede-object-project)))
437 (if (and (not ede-object) ede-object-project)
438 (ede-auto-add-to-target))
440 (ede-apply-target-options))))
442 (defun ede-reset-all-buffers (onoff)
443 "Reset all the buffers due to change in EDE.
444 ONOFF indicates enabling or disabling the mode."
445 (let ((b (buffer-list)))
447 (when (buffer-file-name (car b))
448 (with-current-buffer (car b)
449 ;; Reset all state variables
451 ede-object-project nil
452 ede-object-root-project nil)
453 ;; Now re-initialize this buffer.
454 (ede-initialize-state-current-buffer)
460 (define-minor-mode global-ede-mode
461 "Toggle global EDE (Emacs Development Environment) mode.
462 With non-nil argument ARG, enable global EDE mode if ARG is
463 positive; otherwise, disable it.
465 This global minor mode enables `ede-minor-mode' in all buffers in
466 an EDE controlled project."
470 ;; Turn on global-ede-mode
473 (define-key cedet-menu-map [cedet-menu-separator] '("--")))
474 (add-hook 'semanticdb-project-predicate-functions 'ede-directory-project-p)
475 (add-hook 'semanticdb-project-root-functions 'ede-toplevel-project-or-nil)
476 (add-hook 'ecb-source-path-functions 'ede-ecb-project-paths)
477 (add-hook 'find-file-hook 'ede-turn-on-hook)
478 (add-hook 'dired-mode-hook 'ede-turn-on-hook)
479 (add-hook 'kill-emacs-hook 'ede-save-cache)
481 (ede-reset-all-buffers 1))
482 ;; Turn off global-ede-mode
483 (define-key cedet-menu-map [cedet-menu-separator] nil)
484 (remove-hook 'semanticdb-project-predicate-functions 'ede-directory-project-p)
485 (remove-hook 'semanticdb-project-root-functions 'ede-toplevel-project-or-nil)
486 (remove-hook 'ecb-source-path-functions 'ede-ecb-project-paths)
487 (remove-hook 'find-file-hook 'ede-turn-on-hook)
488 (remove-hook 'dired-mode-hook 'ede-turn-on-hook)
489 (remove-hook 'kill-emacs-hook 'ede-save-cache)
491 (ede-reset-all-buffers -1)))
493 (defvar ede-ignored-file-alist
498 "List of file name patters that EDE will never ask about.")
500 (defun ede-ignore-file (filename)
501 "Should we ignore FILENAME?"
503 (F ede-ignored-file-alist))
504 (while (and (not any) F)
505 (when (string-match (car F) filename)
510 (defun ede-auto-add-to-target ()
511 "Look for a target that wants to own the current file.
512 Follow the preference set with `ede-auto-add-method' and get the list
513 of objects with the `ede-want-file-p' method."
514 (if ede-object (error "Ede-object already defined for %s" (buffer-name)))
515 (if (or (eq ede-auto-add-method 'never)
516 (ede-ignore-file (buffer-file-name)))
519 ;; Find all the objects.
520 (setq wants (oref (ede-current-project) targets))
522 (if (ede-want-file-p (car wants) (buffer-file-name))
523 (setq desires (cons (car wants) desires)))
524 (setq wants (cdr wants)))
526 (cond ((or (eq ede-auto-add-method 'ask)
527 (and (eq ede-auto-add-method 'multi-ask)
528 (< 1 (length desires))))
532 ("new target" . new))
533 ;; If we are in an unparented subdir,
534 ;; offer new a subproject
535 (if (ede-directory-project-p default-directory)
537 '(("create subproject" . project)))
538 ;; Here are the existing objects we want.
539 (object-assoc-list 'name desires)))
541 (ans (completing-read
542 (format "Add %s to target: " (buffer-file-name))
544 (setq ans (assoc ans al))
545 (cond ((eieio-object-p (cdr ans))
546 (ede-add-file (cdr ans)))
550 ((or (eq ede-auto-add-method 'always)
551 (and (eq ede-auto-add-method 'multi-ask)
552 (= 1 (length desires))))
553 (ede-add-file (car desires)))
557 ;;; Interactive method invocations
560 "Start up EDE on something.
561 Argument FILE is the file or directory to load a project from."
562 (interactive "fProject File: ")
563 (if (not (file-exists-p file))
565 (ede-load-project-file (file-name-directory file))))
567 (defun ede-new (type &optional name)
568 "Create a new project starting of project type TYPE.
569 Optional argument NAME is the name to give this project."
571 (list (completing-read "Project Type: "
574 (let* ((l ede-project-class-files)
575 (cp (ede-current-project))
576 (cs (when cp (object-class cp)))
580 (if (eq (oref (car l) :class-sym)
582 (setq r (cons (car l) r)))
583 (if (oref (car l) new-p)
584 (setq r (cons (car l) r))))
588 (error "No valid interactive sub project types for %s"
590 (error "EDE error: Can't fin project types to create")))
594 (require 'ede/custom)
595 ;; Make sure we have a valid directory
596 (when (not (file-exists-p default-directory))
597 (error "Cannot create project in non-existent directory %s" default-directory))
598 (when (not (file-writable-p default-directory))
599 (error "No write permissions for %s" default-directory))
600 ;; Create the project
601 (let* ((obj (object-assoc type 'name ede-project-class-files))
602 (nobj (let ((f (oref obj file))
603 (pf (oref obj proj-file)))
604 ;; We are about to make something new, changing the
605 ;; state of existing directories.
606 (ede-project-directory-remove-hash default-directory)
607 ;; Make sure this class gets loaded!
609 (make-instance (oref obj class-sym)
610 :name (or name (read-string "Name: "))
611 :directory default-directory
612 :file (cond ((stringp pf)
613 (expand-file-name pf))
618 "Unknown file name specifier %S"
621 (inits (oref obj initializers)))
622 ;; Force the name to match for new objects.
623 (object-set-name-string nobj (oref nobj :name))
626 (eieio-oset nobj (car inits) (car (cdr inits)))
627 (setq inits (cdr (cdr inits))))
628 (let ((pp (ede-parent-project)))
630 (ede-add-subproject pp nobj)
631 (ede-commit-project pp)))
632 (ede-commit-project nobj))
633 ;; Have the menu appear
634 (setq ede-minor-mode t)
636 (message "Project created and saved. You may now create targets."))
638 (defmethod ede-add-subproject ((proj-a ede-project) proj-b)
639 "Add into PROJ-A, the subproject PROJ-B."
640 (oset proj-a subproj (cons proj-b (oref proj-a subproj))))
642 (defun ede-invoke-method (sym &rest args)
643 "Invoke method SYM on the current buffer's project object.
644 ARGS are additional arguments to pass to method sym."
646 (error "Cannot invoke %s for %s" (symbol-name sym)
648 ;; Always query a target. There should never be multiple
649 ;; projects in a single buffer.
650 (apply sym (ede-singular-object "Target: ") args))
652 (defun ede-rescan-toplevel ()
653 "Rescan all project files."
655 (let ((toppath (ede-toplevel-project default-directory))
657 (project-rescan (ede-load-project-file toppath))
658 (ede-reset-all-buffers 1)
661 (defun ede-new-target (&rest args)
662 "Create a new target specific to this type of project file.
663 Different projects accept different arguments ARGS.
664 Typically you can specify NAME, target TYPE, and AUTOADD, where AUTOADD is
665 a string \"y\" or \"n\", which answers the y/n question done interactively."
667 (apply 'project-new-target (ede-current-project) args)
668 (setq ede-object nil)
669 (setq ede-object (ede-buffer-object (current-buffer)))
670 (ede-apply-target-options))
672 (defun ede-new-target-custom ()
673 "Create a new target specific to this type of project file."
675 (project-new-target-custom (ede-current-project)))
677 (defun ede-delete-target (target)
678 "Delete TARGET from the current project."
680 (let ((ede-object (ede-current-project)))
681 (ede-invoke-method 'project-interactive-select-target
683 ;; Find all sources in buffers associated with the condemned buffer.
684 (let ((condemned (ede-target-buffers target)))
685 (project-delete-target target)
686 ;; Loop over all project controlled buffers
689 (set-buffer (car condemned))
690 (setq ede-object nil)
691 (setq ede-object (ede-buffer-object (current-buffer)))
692 (setq condemned (cdr condemned))))
693 (ede-apply-target-options)))
695 (defun ede-add-file (target)
696 "Add the current buffer to a TARGET in the current project."
698 (let ((ede-object (ede-current-project)))
699 (ede-invoke-method 'project-interactive-select-target
701 (when (stringp target)
702 (let* ((proj (ede-current-project))
703 (ob (object-assoc-list 'name (oref proj targets))))
704 (setq target (cdr (assoc target ob)))))
707 (error "Could not find specified target %S" target))
709 (project-add-file target (buffer-file-name))
710 (setq ede-object nil)
711 (setq ede-object (ede-buffer-object (current-buffer)))
712 (when (not ede-object)
713 (error "Can't add %s to target %s: Wrong file type"
714 (file-name-nondirectory (buffer-file-name))
715 (object-name target)))
716 (ede-apply-target-options))
718 (defun ede-remove-file (&optional force)
719 "Remove the current file from targets.
720 Optional argument FORCE forces the file to be removed without asking."
723 (error "Cannot invoke remove-file for %s" (buffer-name)))
724 (let ((eo (if (listp ede-object)
730 (if (or force (y-or-n-p (format "Remove from %s? " (ede-name (car eo)))))
731 (project-remove-file (car eo) (buffer-file-name)))
733 (setq ede-object nil)
734 (setq ede-object (ede-buffer-object (current-buffer)))
735 (ede-apply-target-options)))
737 (defun ede-edit-file-target ()
738 "Enter the project file to hand edit the current buffer's target."
740 (ede-invoke-method 'project-edit-file-target))
742 (defun ede-compile-project ()
743 "Compile the current project."
745 ;; @TODO - This just wants the root. There should be a better way.
746 (let ((cp (ede-current-project)))
747 (while (ede-parent-project cp)
748 (setq cp (ede-parent-project cp)))
749 (let ((ede-object cp))
750 (ede-invoke-method 'project-compile-project))))
752 (defun ede-compile-selected (target)
753 "Compile some TARGET from the current project."
754 (interactive (list (project-interactive-select-target (ede-current-project)
755 "Target to Build: ")))
756 (project-compile-target target))
758 (defun ede-compile-target ()
759 "Compile the current buffer's associated target."
761 (ede-invoke-method 'project-compile-target))
763 (defun ede-debug-target ()
764 "Debug the current buffer's associated target."
766 (ede-invoke-method 'project-debug-target))
768 (defun ede-run-target ()
769 "Run the current buffer's associated target."
771 (ede-invoke-method 'project-run-target))
773 (defun ede-make-dist ()
774 "Create a distribution from the current project."
776 (let ((ede-object (ede-toplevel)))
777 (ede-invoke-method 'project-make-dist)))
780 ;;; EDE project target baseline methods.
782 ;; If you are developing a new project type, you need to implement
783 ;; all of these methods, unless, of course, they do not make sense
784 ;; for your particular project.
786 ;; Your targets should inherit from `ede-target', and your project
787 ;; files should inherit from `ede-project'. Create the appropriate
788 ;; methods based on those below.
790 (defmethod project-interactive-select-target ((this ede-project-placeholder) prompt)
791 ; checkdoc-params: (prompt)
792 "Make sure placeholder THIS is replaced with the real thing, and pass through."
793 (project-interactive-select-target this prompt))
795 (defmethod project-interactive-select-target ((this ede-project) prompt)
796 "Interactively query for a target that exists in project THIS.
797 Argument PROMPT is the prompt to use when querying the user for a target."
798 (let ((ob (object-assoc-list 'name (oref this targets))))
799 (cdr (assoc (completing-read prompt ob nil t) ob))))
801 (defmethod project-add-file ((this ede-project-placeholder) file)
802 ; checkdoc-params: (file)
803 "Make sure placeholder THIS is replaced with the real thing, and pass through."
804 (project-add-file this file))
806 (defmethod project-add-file ((ot ede-target) file)
807 "Add the current buffer into project project target OT.
808 Argument FILE is the file to add."
809 (error "add-file not supported by %s" (object-name ot)))
811 (defmethod project-remove-file ((ot ede-target) fnnd)
812 "Remove the current buffer from project target OT.
813 Argument FNND is an argument."
814 (error "remove-file not supported by %s" (object-name ot)))
816 (defmethod project-edit-file-target ((ot ede-target))
817 "Edit the target OT associated w/ this file."
818 (find-file (oref (ede-current-project) file)))
820 (defmethod project-new-target ((proj ede-project) &rest args)
821 "Create a new target. It is up to the project PROJ to get the name."
822 (error "new-target not supported by %s" (object-name proj)))
824 (defmethod project-new-target-custom ((proj ede-project))
825 "Create a new target. It is up to the project PROJ to get the name."
826 (error "New-target-custom not supported by %s" (object-name proj)))
828 (defmethod project-delete-target ((ot ede-target))
829 "Delete the current target OT from its parent project."
830 (error "add-file not supported by %s" (object-name ot)))
832 (defmethod project-compile-project ((obj ede-project) &optional command)
833 "Compile the entire current project OBJ.
834 Argument COMMAND is the command to use when compiling."
835 (error "compile-project not supported by %s" (object-name obj)))
837 (defmethod project-compile-target ((obj ede-target) &optional command)
838 "Compile the current target OBJ.
839 Argument COMMAND is the command to use for compiling the target."
840 (error "compile-target not supported by %s" (object-name obj)))
842 (defmethod project-debug-target ((obj ede-target))
843 "Run the current project target OBJ in a debugger."
844 (error "debug-target not supported by %s" (object-name obj)))
846 (defmethod project-run-target ((obj ede-target))
847 "Run the current project target OBJ."
848 (error "run-target not supported by %s" (object-name obj)))
850 (defmethod project-make-dist ((this ede-project))
851 "Build a distribution for the project based on THIS project."
852 (error "Make-dist not supported by %s" (object-name this)))
854 (defmethod project-dist-files ((this ede-project))
855 "Return a list of files that constitute a distribution of THIS project."
856 (error "Dist-files is not supported by %s" (object-name this)))
858 (defmethod project-rescan ((this ede-project))
859 "Rescan the EDE proj project THIS."
860 (error "Rescanning a project is not supported by %s" (object-name this)))
862 (defun ede-ecb-project-paths ()
863 "Return a list of all paths for all active EDE projects.
864 This functions is meant for use with ECB."
865 (let ((p ede-projects)
868 (setq d (cons (file-name-directory (oref (car p) file))
873 ;;; PROJECT LOADING/TRACKING
875 (defun ede-add-project-to-global-list (proj)
876 "Add the project PROJ to the master list of projects.
877 On success, return the added project."
879 (error "No project created to add to master list"))
880 (when (not (eieio-object-p proj))
881 (error "Attempt to add Non-object to master project list"))
882 (when (not (obj-of-class-p proj ede-project-placeholder))
883 (error "Attempt to add a non-project to the ede projects list"))
884 (add-to-list 'ede-projects proj)
887 (defun ede-load-project-file (dir &optional rootreturn)
888 "Project file independent way to read a project in from DIR.
889 Optional ROOTRETURN will return the root project for DIR."
890 ;; Only load if something new is going on. Flush the dirhash.
891 (ede-project-directory-remove-hash dir)
893 ;;(message "EDE LOAD : %S" file)
895 (path (expand-file-name (file-name-directory file)))
896 (pfc (ede-directory-project-p path))
901 ;; @TODO - Do we really need to scan? Is this a waste of time?
902 ;; Scan upward for a the next project file style.
904 (while (and p (not (ede-directory-project-p p)))
905 (setq p (ede-up-directory p)))
906 (if p (ede-load-project-file p)
908 ;; recomment as we go
911 ;; Do nothing if we are buiding an EDE project already
914 ;; Load in the project in question.
916 (setq toppath (ede-toplevel-project path))
917 ;; We found the top-most directory. Check to see if we already
918 ;; have an object defining its project.
919 (setq pfc (ede-directory-project-p toppath t))
921 ;; See if it's been loaded before
922 (setq o (object-assoc (ede-dir-to-projectfile pfc toppath) 'file
925 ;; If not, get it now.
926 (let ((ede-constructing pfc))
927 (setq o (funcall (oref pfc load-type) toppath))
929 (error "Project type error: :load-type failed to create a project"))
930 (ede-add-project-to-global-list o)))
932 ;; Return the found root project.
933 (when rootreturn (set rootreturn o))
936 ;; Now find the project file belonging to FILE!
937 (setq tocheck (list o))
938 (setq file (ede-dir-to-projectfile pfc (expand-file-name path)))
939 (while (and tocheck (not found))
942 (if (string= file (oref (car tocheck) file))
943 (setq found (car tocheck)))
944 (setq newbits (oref (car tocheck) subproj)))
946 (append (cdr tocheck) newbits))))
948 (message "No project for %s, but passes project-p test" file)
949 ;; Now that the file has been reset inside the project object, do
950 ;; the cache maintenance.
951 (setq ede-project-cache-files
952 (delete (oref found file) ede-project-cache-files)))
955 ;;; PROJECT ASSOCIATIONS
957 ;; Moving between relative projects. Associating between buffers and
960 (defun ede-parent-project (&optional obj)
961 "Return the project belonging to the parent directory.
962 Return nil if there is no previous directory.
963 Optional argument OBJ is an object to find the parent of."
964 (let* ((proj (or obj ede-object-project)) ;; Current project.
965 (root (if obj (ede-project-root obj)
966 ede-object-root-project)))
967 ;; This case is a SHORTCUT if the project has defined
968 ;; a way to calculate the project root.
969 (if (and root proj (eq root proj))
970 nil ;; we are at the root.
971 ;; Else, we may have a nil proj or root.
972 (let* ((thisdir (if obj (oref obj directory)
974 (updir (ede-up-directory thisdir)))
976 ;; If there was no root, perhaps we can derive it from
978 (let ((root (or root (ede-directory-get-toplevel-open-project updir))))
980 ;; This lets us find a subproject under root based on updir.
982 (ede-find-subproject-for-directory root updir))
983 ;; Try the all structure based search.
984 (ede-directory-get-open-project updir)
985 ;; Load up the project file as a last resort.
986 ;; Last resort since it uses file-truename, and other
988 (and (ede-directory-project-p updir)
989 (ede-load-project-file
990 (file-name-as-directory updir))))))))))
992 (defun ede-current-project (&optional dir)
993 "Return the current project file.
994 If optional DIR is provided, get the project for DIR instead."
996 ;; If it matches the current directory, do we have a pre-existing project?
997 (when (and (or (not dir) (string= dir default-directory))
999 (setq ans ede-object-project)
1001 ;; No current project.
1003 (let* ((ldir (or dir default-directory)))
1004 (setq ans (ede-directory-get-open-project ldir))
1006 ;; No open project, if this dir pass project-p, then load.
1007 (when (ede-directory-project-p ldir)
1008 (setq ans (ede-load-project-file ldir))))))
1009 ;; Return what we found.
1012 (defun ede-buffer-object (&optional buffer projsym)
1013 "Return the target object for BUFFER.
1014 This function clears cached values and recalculates.
1015 Optional PROJSYM is a symbol, which will be set to the project
1016 that contains the target that becomes buffer's object."
1018 (if (not buffer) (setq buffer (current-buffer)))
1020 (setq ede-object nil)
1021 (let* ((localpo (ede-current-project))
1023 (top (ede-toplevel po)))
1024 (if po (setq ede-object (ede-find-target po buffer)))
1025 ;; If we get nothing, go with the backup plan of slowly
1027 (while (and (not ede-object) (not (eq po top)))
1028 (setq po (ede-parent-project po))
1029 (if po (setq ede-object (ede-find-target po buffer))))
1030 ;; Filter down to 1 project if there are dups.
1031 (if (= (length ede-object) 1)
1032 (setq ede-object (car ede-object)))
1033 ;; Track the project, if needed.
1034 (when (and projsym (symbolp projsym))
1036 ;; If we found a target, then PO is the
1039 ;; If there is no ede-object, then the projsym
1040 ;; is whichever part of the project is most local.
1041 (set projsym localpo))
1043 ;; Return our findings.
1046 (defmethod ede-target-in-project-p ((proj ede-project) target)
1047 "Is PROJ the parent of TARGET?
1048 If TARGET belongs to a subproject, return that project file."
1049 (if (and (slot-boundp proj 'targets)
1050 (memq target (oref proj targets)))
1052 (let ((s (oref proj subproj))
1054 (while (and s (not ans))
1055 (setq ans (ede-target-in-project-p (car s) target))
1059 (defun ede-target-parent (target)
1060 "Return the project which is the parent of TARGET.
1061 It is recommended you track the project a different way as this function
1062 could become slow in time."
1063 ;; @todo - use ede-object-project as a starting point.
1064 (let ((ans nil) (projs ede-projects))
1065 (while (and (not ans) projs)
1066 (setq ans (ede-target-in-project-p (car projs) target)
1070 (defmethod ede-find-target ((proj ede-project) buffer)
1071 "Fetch the target in PROJ belonging to BUFFER or nil."
1072 (with-current-buffer buffer
1074 (if (ede-buffer-mine proj buffer)
1076 (let ((targets (oref proj targets))
1079 (if (ede-buffer-mine (car targets) buffer)
1080 (setq f (cons (car targets) f)))
1081 (setq targets (cdr targets)))
1084 (defmethod ede-target-buffer-in-sourcelist ((this ede-target) buffer source)
1085 "Return non-nil if object THIS is in BUFFER to a SOURCE list.
1086 Handles complex path issues."
1087 (member (ede-convert-path this (buffer-file-name buffer)) source))
1089 (defmethod ede-buffer-mine ((this ede-project) buffer)
1090 "Return non-nil if object THIS lays claim to the file in BUFFER."
1093 (defmethod ede-buffer-mine ((this ede-target) buffer)
1094 "Return non-nil if object THIS lays claim to the file in BUFFER."
1096 (ede-target-buffer-in-sourcelist this buffer (oref this source))
1097 ;; An error implies a bad match.
1103 (defun ede-project-buffers (project)
1104 "Return a list of all active buffers controlled by PROJECT.
1105 This includes buffers controlled by a specific target of PROJECT."
1106 (let ((bl (buffer-list))
1109 (with-current-buffer (car bl)
1110 (if (ede-buffer-belongs-to-project-p)
1111 (setq pl (cons (car bl) pl))))
1115 (defun ede-target-buffers (target)
1116 "Return a list of buffers that are controlled by TARGET."
1117 (let ((bl (buffer-list))
1120 (with-current-buffer (car bl)
1121 (if (if (listp ede-object)
1122 (memq target ede-object)
1123 (eq ede-object target))
1124 (setq pl (cons (car bl) pl))))
1128 (defun ede-buffers ()
1129 "Return a list of all buffers controlled by an EDE object."
1130 (let ((bl (buffer-list))
1133 (with-current-buffer (car bl)
1135 (setq pl (cons (car bl) pl))))
1139 (defun ede-map-buffers (proc)
1140 "Execute PROC on all buffers controlled by EDE."
1141 (mapcar proc (ede-buffers)))
1143 (defmethod ede-map-project-buffers ((this ede-project) proc)
1144 "For THIS, execute PROC on all buffers belonging to THIS."
1145 (mapcar proc (ede-project-buffers this)))
1147 (defmethod ede-map-target-buffers ((this ede-target) proc)
1148 "For THIS, execute PROC on all buffers belonging to THIS."
1149 (mapcar proc (ede-target-buffers this)))
1151 ;; other types of mapping
1152 (defmethod ede-map-subprojects ((this ede-project) proc)
1153 "For object THIS, execute PROC on all direct subprojects.
1154 This function does not apply PROC to sub-sub projects.
1155 See also `ede-map-all-subprojects'."
1156 (mapcar proc (oref this subproj)))
1158 (defmethod ede-map-all-subprojects ((this ede-project) allproc)
1159 "For object THIS, execute PROC on THIS and all subprojects.
1160 This function also applies PROC to sub-sub projects.
1161 See also `ede-map-subprojects'."
1163 (list (funcall allproc this))
1164 (ede-map-subprojects
1167 (ede-map-all-subprojects sp allproc))
1170 ;; (ede-map-all-subprojects (ede-load-project-file "../semantic/") (lambda (sp) (oref sp file)))
1172 (defmethod ede-map-targets ((this ede-project) proc)
1173 "For object THIS, execute PROC on all targets."
1174 (mapcar proc (oref this targets)))
1176 (defmethod ede-map-any-target-p ((this ede-project) proc)
1177 "For project THIS, map PROC to all targets and return if any non-nil.
1178 Return the first non-nil value returned by PROC."
1179 (eval (cons 'or (ede-map-targets this proc))))
1183 (defun ede-maybe-checkout (&optional buffer)
1184 "Check BUFFER out of VC if necessary."
1186 (if buffer (set-buffer buffer))
1187 (if (and buffer-read-only vc-mode
1188 (y-or-n-p "Checkout Makefile.am from VC? "))
1189 (vc-toggle-read-only))))
1192 ;;; Some language specific methods.
1194 ;; These items are needed by ede-cpp-root to add better support for
1195 ;; configuring items for Semantic.
1196 (defun ede-apply-preprocessor-map ()
1197 "Apply preprocessor tables onto the current buffer."
1198 (when (and ede-object (boundp 'semantic-lex-spp-macro-symbol-obarray))
1199 (let* ((objs ede-object)
1200 (map (ede-preprocessor-map (if (consp objs)
1204 ;; We can't do a require for the below symbol.
1205 (setq semantic-lex-spp-macro-symbol-obarray
1206 (semantic-lex-make-spp-table map)))
1208 (message "Choosing preprocessor syms for project %s"
1209 (object-name (car objs)))))))
1211 (defmethod ede-system-include-path ((this ede-project))
1212 "Get the system include path used by project THIS."
1215 (defmethod ede-preprocessor-map ((this ede-project))
1216 "Get the pre-processor map for project THIS."
1219 (defmethod ede-system-include-path ((this ede-target))
1220 "Get the system include path used by project THIS."
1223 (defmethod ede-preprocessor-map ((this ede-target))
1224 "Get the pre-processor map for project THIS."
1228 ;;; Project-local variables
1230 (defun ede-make-project-local-variable (variable &optional project)
1231 "Make VARIABLE project-local to PROJECT."
1232 (if (not project) (setq project (ede-current-project)))
1233 (if (assoc variable (oref project local-variables))
1235 (oset project local-variables (cons (list variable)
1236 (oref project local-variables)))
1237 (dolist (b (ede-project-buffers project))
1238 (with-current-buffer b
1239 (make-local-variable variable)))))
1241 (defmethod ede-set-project-variables ((project ede-project) &optional buffer)
1242 "Set variables local to PROJECT in BUFFER."
1243 (if (not buffer) (setq buffer (current-buffer)))
1244 (with-current-buffer buffer
1245 (dolist (v (oref project local-variables))
1246 (make-local-variable (car v))
1247 ;; set its value here?
1248 (set (car v) (cdr v)))))
1250 (defun ede-set (variable value &optional proj)
1251 "Set the project local VARIABLE to VALUE.
1252 If VARIABLE is not project local, just use set. Optional argument PROJ
1253 is the project to use, instead of `ede-current-project'."
1254 (let ((p (or proj (ede-current-project)))
1256 (if (and p (setq a (assoc variable (oref p local-variables))))
1259 (dolist (b (ede-project-buffers p))
1260 (with-current-buffer b
1261 (set variable value))))
1262 (set variable value))
1263 (ede-commit-local-variables p))
1266 (defmethod ede-commit-local-variables ((proj ede-project))
1267 "Commit change to local variables in PROJ."
1272 ;; Include this last because it depends on ede.
1273 (require 'ede/files)
1275 ;; If this does not occur after the provide, we can get a recursive
1277 (if (featurep 'speedbar)
1278 (ede-speedbar-file-setup)
1279 (add-hook 'speedbar-load-hook 'ede-speedbar-file-setup))
1281 ;;; ede.el ends here