Update copyright year to 2015
[emacs.git] / lisp / cedet / ede.el
blobedf87f640cfb034076dfa91cfc2a8e3e4fe431a5
1 ;;; ede.el --- Emacs Development Environment gloss
3 ;; Copyright (C) 1998-2005, 2007-2015 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project, make
7 ;; Version: 1.2
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/>.
24 ;;; Commentary:
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
34 ;; different ones.
36 ;;; Install
38 ;; This command enables project mode on all files.
40 ;; (global-ede-mode t)
42 (require 'cedet)
43 (require 'eieio)
44 (require 'eieio-speedbar)
45 (require 'ede/source)
46 (require 'ede/base)
47 (require 'ede/auto)
48 (require 'ede/detect)
50 (eval-and-compile
51 (load "ede/loaddefs" nil 'nomessage))
53 (declare-function ede-commit-project "ede/custom")
54 (declare-function ede-convert-path "ede/files")
55 (declare-function ede-directory-get-open-project "ede/files")
56 (declare-function ede-directory-get-toplevel-open-project "ede/files")
57 (declare-function ede-directory-project-p "ede/files")
58 (declare-function ede-find-subproject-for-directory "ede/files")
59 (declare-function ede-project-directory-remove-hash "ede/files")
60 (declare-function ede-toplevel "ede/base")
61 (declare-function ede-toplevel-project "ede/files")
62 (declare-function ede-up-directory "ede/files")
63 (declare-function semantic-lex-make-spp-table "semantic/lex-spp")
65 (defconst ede-version "2.0"
66 "Current version of the Emacs EDE.")
68 ;;; Code:
69 (defun ede-version ()
70 "Display the current running version of EDE."
71 (interactive) (message "EDE %s" ede-version))
73 (defgroup ede nil
74 "Emacs Development Environment."
75 :group 'tools
76 :group 'extensions)
78 (defcustom ede-auto-add-method 'ask
79 "Whether a new source file should be automatically added to a target.
80 Whenever a new file is encountered in a directory controlled by a
81 project file, all targets are queried to see if it should be added.
82 If the value is 'always, then the new file is added to the first
83 target encountered. If the value is 'multi-ask, then if more than one
84 target wants the file, the user is asked. If only one target wants
85 the file, then it is automatically added to that target. If the
86 value is 'ask, then the user is always asked, unless there is no
87 target willing to take the file. 'never means never perform the check."
88 :group 'ede
89 :type '(choice (const always)
90 (const multi-ask)
91 (const ask)
92 (const never)))
94 (defcustom ede-debug-program-function 'gdb
95 "Default Emacs command used to debug a target."
96 :group 'ede
97 :type 'sexp) ; make this be a list of options some day
99 (defcustom ede-project-directories nil
100 "Directories in which EDE may search for project files.
101 If the value is t, EDE may search in any directory.
103 If the value is a function, EDE calls that function with one
104 argument, the directory name; the function should return t if
105 EDE should look for project files in the directory.
107 Otherwise, the value should be a list of fully-expanded directory
108 names. EDE searches for project files only in those directories.
109 If you invoke the commands \\[ede] or \\[ede-new] on a directory
110 that is not listed, Emacs will offer to add it to the list.
112 Any other value disables searching for EDE project files."
113 :group 'ede
114 :type '(choice (const :tag "Any directory" t)
115 (repeat :tag "List of directories"
116 (directory))
117 (function :tag "Predicate"))
118 :version "23.4"
119 :risky t)
121 (defun ede-directory-safe-p (dir)
122 "Return non-nil if DIR is a safe directory to load projects from.
123 Projects that do not load a project definition as Emacs Lisp code
124 are safe, and can be loaded automatically. Other project types,
125 such as those created with Project.ede files, are safe only if
126 specified by `ede-project-directories'."
127 (setq dir (directory-file-name (expand-file-name dir)))
128 ;; Load only if allowed by `ede-project-directories'.
129 (or (eq ede-project-directories t)
130 (and (functionp ede-project-directories)
131 (funcall ede-project-directories dir))
132 (and (listp ede-project-directories)
133 (member dir ede-project-directories))))
136 ;;; Management variables
138 (defvar ede-projects nil
139 "A list of all active projects currently loaded in Emacs.")
141 (defvar ede-object-root-project nil
142 "The current buffer's current root project.
143 If a file is under a project, this specifies the project that is at
144 the root of a project tree.")
145 (make-variable-buffer-local 'ede-object-root-project)
147 (defvar ede-object-project nil
148 "The current buffer's current project at that level.
149 If a file is under a project, this specifies the project that contains the
150 current target.")
151 (make-variable-buffer-local 'ede-object-project)
153 (defvar ede-object nil
154 "The current buffer's target object.
155 This object's class determines how to compile and debug from a buffer.")
156 (make-variable-buffer-local 'ede-object)
158 (defvar ede-selected-object nil
159 "The currently user-selected project or target.
160 If `ede-object' is nil, then commands will operate on this object.")
162 (defvar ede-constructing nil
163 "Non nil when constructing a project hierarchy.
164 If the project is being constructed from an autoload, then the
165 value is the autoload object being used.")
167 (defvar ede-deep-rescan nil
168 "Non nil means scan down a tree, otherwise rescans are top level only.
169 Do not set this to non-nil globally. It is used internally.")
172 ;;; Prompting
174 (defun ede-singular-object (prompt)
175 "Using PROMPT, choose a single object from the current buffer."
176 (if (listp ede-object)
177 (ede-choose-object prompt ede-object)
178 ede-object))
180 (defun ede-choose-object (prompt list-o-o)
181 "Using PROMPT, ask the user which OBJECT to use based on the name field.
182 Argument LIST-O-O is the list of objects to choose from."
183 (let* ((al (object-assoc-list 'name list-o-o))
184 (ans (completing-read prompt al nil t)))
185 (setq ans (assoc ans al))
186 (cdr ans)))
188 ;;; Menu and Keymap
190 (defvar ede-minor-mode-map
191 (let ((map (make-sparse-keymap))
192 (pmap (make-sparse-keymap)))
193 (define-key pmap "e" 'ede-edit-file-target)
194 (define-key pmap "a" 'ede-add-file)
195 (define-key pmap "d" 'ede-remove-file)
196 (define-key pmap "t" 'ede-new-target)
197 (define-key pmap "g" 'ede-rescan-toplevel)
198 (define-key pmap "s" 'ede-speedbar)
199 (define-key pmap "f" 'ede-find-file)
200 (define-key pmap "C" 'ede-compile-project)
201 (define-key pmap "c" 'ede-compile-target)
202 (define-key pmap "\C-c" 'ede-compile-selected)
203 (define-key pmap "D" 'ede-debug-target)
204 (define-key pmap "R" 'ede-run-target)
205 ;; bind our submap into map
206 (define-key map "\C-c." pmap)
207 map)
208 "Keymap used in project minor mode.")
210 (defvar global-ede-mode-map
211 (let ((map (make-sparse-keymap)))
212 (define-key map [menu-bar cedet-menu]
213 (cons "Development" cedet-menu-map))
214 map)
215 "Keymap used in `global-ede-mode'.")
217 ;; Activate the EDE items in cedet-menu-map
219 (define-key cedet-menu-map [ede-find-file]
220 '(menu-item "Find File in Project..." ede-find-file :enable ede-object
221 :visible global-ede-mode))
222 (define-key cedet-menu-map [ede-speedbar]
223 '(menu-item "View Project Tree" ede-speedbar :enable ede-object
224 :visible global-ede-mode))
225 (define-key cedet-menu-map [ede]
226 '(menu-item "Load Project" ede
227 :visible global-ede-mode))
228 (define-key cedet-menu-map [ede-new]
229 '(menu-item "Create Project" ede-new
230 :enable (not ede-object)
231 :visible global-ede-mode))
232 (define-key cedet-menu-map [ede-target-options]
233 '(menu-item "Target Options" ede-target-options
234 :filter ede-target-forms-menu
235 :visible global-ede-mode))
236 (define-key cedet-menu-map [ede-project-options]
237 '(menu-item "Project Options" ede-project-options
238 :filter ede-project-forms-menu
239 :visible global-ede-mode))
240 (define-key cedet-menu-map [ede-build-forms-menu]
241 '(menu-item "Build Project" ede-build-forms-menu
242 :filter ede-build-forms-menu
243 :enable ede-object
244 :visible global-ede-mode))
246 (defun ede-buffer-belongs-to-target-p ()
247 "Return non-nil if this buffer belongs to at least one target."
248 (let ((obj ede-object))
249 (if (consp obj)
250 (setq obj (car obj)))
251 (and obj (obj-of-class-p obj ede-target))))
253 (defun ede-buffer-belongs-to-project-p ()
254 "Return non-nil if this buffer belongs to at least one project."
255 (if (or (null ede-object) (consp ede-object)) nil
256 (obj-of-class-p ede-object-project ede-project)))
258 (defun ede-menu-obj-of-class-p (class)
259 "Return non-nil if some member of `ede-object' is a child of CLASS."
260 (if (listp ede-object)
261 (eval (cons 'or (mapcar (lambda (o) (obj-of-class-p o class)) ede-object)))
262 (obj-of-class-p ede-object class)))
264 (defun ede-build-forms-menu (menu-def)
265 "Create a sub menu for building different parts of an EDE system.
266 Argument MENU-DEF is the menu definition to use."
267 (easy-menu-filter-return
268 (easy-menu-create-menu
269 "Build Forms"
270 (let ((obj (ede-current-project))
271 (newmenu nil) ;'([ "Build Selected..." ede-compile-selected t ]))
272 targets
273 targitems
274 ede-obj
275 (tskip nil))
276 (if (not obj)
278 (setq targets (when (slot-boundp obj 'targets)
279 (oref obj targets))
280 ede-obj (if (listp ede-object) ede-object (list ede-object)))
281 ;; First, collect the build items from the project
282 (setq newmenu (append newmenu (ede-menu-items-build obj t)))
283 ;; Second, declare the current target menu items
284 (if (and ede-obj (ede-menu-obj-of-class-p ede-target))
285 (while ede-obj
286 (setq newmenu (append newmenu
287 (ede-menu-items-build (car ede-obj) t))
288 tskip (car ede-obj)
289 ede-obj (cdr ede-obj))))
290 ;; Third, by name, enable builds for other local targets
291 (while targets
292 (unless (eq tskip (car targets))
293 (setq targitems (ede-menu-items-build (car targets) nil))
294 (setq newmenu
295 (append newmenu
296 (if (= 1 (length targitems))
297 targitems
298 (cons (ede-name (car targets))
299 targitems))))
301 (setq targets (cdr targets)))
302 ;; Fourth, build sub projects.
303 ;; -- nerp
304 ;; Fifth, add make distribution
305 (append newmenu (list [ "Make distribution" ede-make-dist t ]))
306 )))))
308 (defun ede-target-forms-menu (menu-def)
309 "Create a target MENU-DEF based on the object belonging to this buffer."
310 (easy-menu-filter-return
311 (easy-menu-create-menu
312 "Target Forms"
313 (let ((obj (or ede-selected-object ede-object)))
314 (append
315 '([ "Add File" ede-add-file
316 (and (ede-current-project)
317 (oref (ede-current-project) targets)) ]
318 [ "Remove File" ede-remove-file
319 (ede-buffer-belongs-to-project-p) ]
320 "-")
321 (if (not obj)
323 (if (and (not (listp obj)) (oref obj menu))
324 (oref obj menu)
325 (when (listp obj)
326 ;; This is bad, but I'm not sure what else to do.
327 (oref (car obj) menu)))))))))
329 (defun ede-project-forms-menu (menu-def)
330 "Create a target MENU-DEF based on the object belonging to this buffer."
331 (easy-menu-filter-return
332 (easy-menu-create-menu
333 "Project Forms"
334 (let* ((obj (ede-current-project))
335 (class (if obj (eieio-object-class obj)))
336 (menu nil))
337 (condition-case err
338 (progn
339 (while (and class (slot-exists-p class 'menu))
340 ;;(message "Looking at class %S" class)
341 (setq menu (append menu (oref class menu))
342 class (eieio-class-parent class))
343 (if (listp class) (setq class (car class))))
344 (append
345 '( [ "Add Target" ede-new-target (ede-current-project) ]
346 [ "Remove Target" ede-delete-target ede-object ]
347 ( "Default configuration" :filter ede-configuration-forms-menu )
348 "-")
349 menu
351 (error (message "Err found: %S" err)
352 menu)
353 )))))
355 (defun ede-configuration-forms-menu (menu-def)
356 "Create a submenu for selecting the default configuration for this project.
357 The current default is in the current object's CONFIGURATION-DEFAULT slot.
358 All possible configurations are in CONFIGURATIONS.
359 Argument MENU-DEF specifies the menu being created."
360 (easy-menu-filter-return
361 (easy-menu-create-menu
362 "Configurations"
363 (let* ((obj (ede-current-project))
364 (conf (when obj (oref obj configurations)))
365 (cdef (when obj (oref obj configuration-default)))
366 (menu nil))
367 (dolist (C conf)
368 (setq menu (cons (vector C (list 'ede-project-configurations-set C)
369 :style 'toggle
370 :selected (string= C cdef))
371 menu))
373 (nreverse menu)))))
375 (defun ede-project-configurations-set (newconfig)
376 "Set the current project's current configuration to NEWCONFIG.
377 This function is designed to be used by `ede-configuration-forms-menu'
378 but can also be used interactively."
379 (interactive
380 (list (let* ((proj (ede-current-project))
381 (configs (oref proj configurations)))
382 (completing-read "New configuration: "
383 configs nil t
384 (oref proj configuration-default)))))
385 (oset (ede-current-project) configuration-default newconfig)
386 (message "%s will now build in %s mode."
387 (eieio-object-name (ede-current-project))
388 newconfig))
390 (defun ede-customize-forms-menu (menu-def)
391 "Create a menu of the project, and targets that can be customized.
392 Argument MENU-DEF is the definition of the current menu."
393 (easy-menu-filter-return
394 (easy-menu-create-menu
395 "Customize Project"
396 (let* ((obj (ede-current-project))
397 targ)
398 (when obj
399 (setq targ (when (and obj (slot-boundp obj 'targets))
400 (oref obj targets)))
401 ;; Make custom menus for everything here.
402 (append (list
403 (cons (concat "Project " (ede-name obj))
404 (eieio-customize-object-group obj))
405 [ "Reorder Targets" ede-project-sort-targets t ]
407 (mapcar (lambda (o)
408 (cons (concat "Target " (ede-name o))
409 (eieio-customize-object-group o)))
410 targ)))))))
413 (defun ede-apply-object-keymap (&optional default)
414 "Add target specific keybindings into the local map.
415 Optional argument DEFAULT indicates if this should be set to the default
416 version of the keymap."
417 (let ((object (or ede-object ede-selected-object))
418 (proj ede-object-project))
419 (condition-case nil
420 (let ((keys (ede-object-keybindings object)))
421 ;; Add keys for the project to whatever is in the current object
422 ;; so long as it isn't the same.
423 (when (not (eq object proj))
424 (setq keys (append keys (ede-object-keybindings proj))))
425 (while keys
426 (local-set-key (concat "\C-c." (car (car keys)))
427 (cdr (car keys)))
428 (setq keys (cdr keys))))
429 (error nil))))
431 ;;; Menu building methods for building
433 (defmethod ede-menu-items-build ((obj ede-project) &optional current)
434 "Return a list of menu items for building project OBJ.
435 If optional argument CURRENT is non-nil, return sub-menu code."
436 (if current
437 (list [ "Build Current Project" ede-compile-project t ])
438 (list (vector
439 (list
440 (concat "Build Project " (ede-name obj))
441 `(project-compile-project ,obj))))))
443 (defmethod ede-menu-items-build ((obj ede-target) &optional current)
444 "Return a list of menu items for building target OBJ.
445 If optional argument CURRENT is non-nil, return sub-menu code."
446 (if current
447 (list [ "Build Current Target" ede-compile-target t ])
448 (list (vector
449 (concat "Build Target " (ede-name obj))
450 `(project-compile-target ,obj)
451 t))))
453 ;;; Mode Declarations
456 (defun ede-apply-target-options ()
457 "Apply options to the current buffer for the active project/target."
458 (ede-apply-project-local-variables)
459 ;; Apply keymaps and preprocessor symbols.
460 (ede-apply-object-keymap)
461 (ede-apply-preprocessor-map)
464 (defun ede-turn-on-hook ()
465 "Turn on EDE minor mode in the current buffer if needed.
466 To be used in hook functions."
467 (if (or (and (stringp (buffer-file-name))
468 (stringp default-directory))
469 ;; Emacs 21 has no buffer file name for directory edits.
470 ;; so we need to add these hacks in.
471 (eq major-mode 'dired-mode)
472 (eq major-mode 'vc-dired-mode))
473 (ede-minor-mode 1)))
475 (define-minor-mode ede-minor-mode
476 "Toggle EDE (Emacs Development Environment) minor mode.
477 With a prefix argument ARG, enable EDE minor mode if ARG is
478 positive, and disable it otherwise. If called from Lisp, enable
479 EDE minor mode if ARG is omitted or nil.
481 If this file is contained, or could be contained in an EDE
482 controlled project, then this mode is activated automatically
483 provided `global-ede-mode' is enabled."
484 :group 'ede
485 (cond ((or (eq major-mode 'dired-mode)
486 (eq major-mode 'vc-dired-mode))
487 (ede-dired-minor-mode (if ede-minor-mode 1 -1)))
488 (ede-minor-mode
489 (if (not ede-constructing)
490 (ede-initialize-state-current-buffer)
491 ;; If we fail to have a project here, turn it back off.
492 (ede-minor-mode -1)))))
494 (defun ede-initialize-state-current-buffer ()
495 "Initialize the current buffer's state for EDE.
496 Sets buffer local variables for EDE."
497 ;; due to inode recycling, make sure we don't
498 ;; we flush projects deleted off the system.
499 (ede-flush-deleted-projects)
501 ;; Init the buffer.
502 (let* ((ROOT nil)
503 (proj (ede-directory-get-open-project default-directory
504 'ROOT)))
506 (when (not proj)
507 ;; If there is no open project, look up the project
508 ;; autoloader to see if we should initialize.
509 (let ((projdetect (ede-directory-project-cons default-directory)))
511 (when projdetect
512 ;; No project was loaded, but we have a project description
513 ;; object. This means that we try to load it.
515 ;; Before loading, we need to check if it is a safe
516 ;; project to load before requesting it to be loaded.
518 (when (or (oref (cdr projdetect) safe-p)
519 ;; The project style is not safe, so check if it is
520 ;; in `ede-project-directories'.
521 (let ((top (car projdetect)))
522 (ede-directory-safe-p top)))
524 ;; The project is safe, so load it in.
525 (setq proj (ede-load-project-file default-directory projdetect 'ROOT))))))
527 ;; If PROJ is now loaded in, we can initialize our buffer to it.
528 (when proj
530 ;; ede-object represents the specific EDE related class that best
531 ;; represents this buffer. It could be a project (for a project file)
532 ;; or a target. Also save off ede-object-project, the project that
533 ;; the buffer belongs to for the case where ede-object is a target.
534 (setq ede-object (ede-buffer-object (current-buffer)
535 'ede-object-project))
537 ;; Every project has a root. It might be the same as ede-object.
538 ;; Cache that also as the root is a very common thing to need.
539 (setq ede-object-root-project
540 (or ROOT (ede-project-root ede-object-project)))
542 ;; Check to see if we want to add this buffer to a target.
543 (if (and (not ede-object) ede-object-project)
544 (ede-auto-add-to-target))
546 ;; Apply any options from the found target.
547 (ede-apply-target-options))))
549 (defun ede-reset-all-buffers ()
550 "Reset all the buffers due to change in EDE."
551 (interactive)
552 (let ((b (buffer-list)))
553 (while b
554 (when (buffer-file-name (car b))
555 (with-current-buffer (car b)
556 ;; Reset all state variables
557 (setq ede-object nil
558 ede-object-project nil
559 ede-object-root-project nil)
560 ;; Now re-initialize this buffer.
561 (ede-initialize-state-current-buffer)
564 (setq b (cdr b)))))
566 ;;;###autoload
567 (define-minor-mode global-ede-mode
568 "Toggle global EDE (Emacs Development Environment) mode.
569 With a prefix argument ARG, enable global EDE mode if ARG is
570 positive, and disable it otherwise. If called from Lisp, enable
571 the mode if ARG is omitted or nil.
573 This global minor mode enables `ede-minor-mode' in all buffers in
574 an EDE controlled project."
575 :global t
576 :group 'ede
577 (if global-ede-mode
578 ;; Turn on global-ede-mode
579 (progn
580 (if semantic-mode
581 (define-key cedet-menu-map [cedet-menu-separator] '("--")))
582 (add-hook 'semanticdb-project-predicate-functions 'ede-directory-project-p)
583 (add-hook 'semanticdb-project-root-functions 'ede-toplevel-project-or-nil)
584 (add-hook 'ecb-source-path-functions 'ede-ecb-project-paths)
585 ;; Append our hook to the end. This allows mode-local to finish
586 ;; it's stuff before we start doing misc file loads, etc.
587 (add-hook 'find-file-hook 'ede-turn-on-hook t)
588 (add-hook 'dired-mode-hook 'ede-turn-on-hook)
589 (add-hook 'kill-emacs-hook 'ede-save-cache)
590 (ede-load-cache)
591 (ede-reset-all-buffers))
592 ;; Turn off global-ede-mode
593 (define-key cedet-menu-map [cedet-menu-separator] nil)
594 (remove-hook 'semanticdb-project-predicate-functions 'ede-directory-project-p)
595 (remove-hook 'semanticdb-project-root-functions 'ede-toplevel-project-or-nil)
596 (remove-hook 'ecb-source-path-functions 'ede-ecb-project-paths)
597 (remove-hook 'find-file-hook 'ede-turn-on-hook)
598 (remove-hook 'dired-mode-hook 'ede-turn-on-hook)
599 (remove-hook 'kill-emacs-hook 'ede-save-cache)
600 (ede-save-cache)
601 (ede-reset-all-buffers)))
603 (defvar ede-ignored-file-alist
604 '( "\\.cvsignore$"
605 "\\.#"
606 "~$"
608 "List of file name patters that EDE will never ask about.")
610 (defun ede-ignore-file (filename)
611 "Should we ignore FILENAME?"
612 (let ((any nil)
613 (F ede-ignored-file-alist))
614 (while (and (not any) F)
615 (when (string-match (car F) filename)
616 (setq any t))
617 (setq F (cdr F)))
618 any))
620 (defun ede-auto-add-to-target ()
621 "Look for a target that wants to own the current file.
622 Follow the preference set with `ede-auto-add-method' and get the list
623 of objects with the `ede-want-file-p' method."
624 (if ede-object (error "ede-object already defined for %s" (buffer-name)))
625 (if (or (eq ede-auto-add-method 'never)
626 (ede-ignore-file (buffer-file-name)))
628 (let (wants desires)
629 ;; Find all the objects.
630 (setq wants (oref (ede-current-project) targets))
631 (while wants
632 (if (ede-want-file-p (car wants) (buffer-file-name))
633 (setq desires (cons (car wants) desires)))
634 (setq wants (cdr wants)))
635 (if desires
636 (cond ((or (eq ede-auto-add-method 'ask)
637 (and (eq ede-auto-add-method 'multi-ask)
638 (< 1 (length desires))))
639 (let* ((al (append
640 ;; some defaults
641 '(("none" . nil)
642 ("new target" . new))
643 ;; If we are in an unparented subdir,
644 ;; offer new a subproject
645 (if (ede-directory-project-p default-directory)
647 '(("create subproject" . project)))
648 ;; Here are the existing objects we want.
649 (object-assoc-list 'name desires)))
650 (case-fold-search t)
651 (ans (completing-read
652 (format "Add %s to target: " (buffer-file-name))
653 al nil t)))
654 (setq ans (assoc ans al))
655 (cond ((eieio-object-p (cdr ans))
656 (ede-add-file (cdr ans)))
657 ((eq (cdr ans) 'new)
658 (ede-new-target))
659 (t nil))))
660 ((or (eq ede-auto-add-method 'always)
661 (and (eq ede-auto-add-method 'multi-ask)
662 (= 1 (length desires))))
663 (ede-add-file (car desires)))
664 (t nil))))))
667 ;;; Interactive method invocations
669 (defun ede (dir)
670 "Start up EDE for directory DIR.
671 If DIR has an existing project file, load it.
672 Otherwise, create a new project for DIR."
673 (interactive
674 ;; When choosing a directory to turn on, and we see some directory here,
675 ;; provide that as the default.
676 (let* ((top (ede-toplevel-project default-directory))
677 (promptdflt (or top default-directory)))
678 (list (read-directory-name "Project directory: "
679 promptdflt promptdflt t))))
680 (unless (file-directory-p dir)
681 (error "%s is not a directory" dir))
682 (when (ede-directory-get-open-project dir)
683 (error "%s already has an open project associated with it" dir))
685 ;; Check if the directory has been added to the list of safe
686 ;; directories. It can also add the directory to the safe list if
687 ;; the user chooses.
688 (if (ede-check-project-directory dir)
689 (progn
690 ;; Load the project in DIR, or make one.
691 ;; @TODO - IS THIS REAL?
692 (ede-load-project-file dir)
694 ;; Check if we loaded anything on the previous line.
695 (if (ede-current-project dir)
697 ;; We successfully opened an existing project. Some open
698 ;; buffers may also be referring to this project.
699 ;; Resetting all the buffers will get them to also point
700 ;; at this new open project.
701 (ede-reset-all-buffers)
703 ;; ELSE
704 ;; There was no project, so switch to `ede-new' which is how
705 ;; a user can select a new kind of project to create.
706 (let ((default-directory (expand-file-name dir)))
707 (call-interactively 'ede-new))))
709 ;; If the proposed directory isn't safe, then say so.
710 (error "%s is not an allowed project directory in `ede-project-directories'"
711 dir)))
713 (defvar ede-check-project-query-fcn 'y-or-n-p
714 "Function used to ask the user if they want to permit a project to load.
715 This is abstracted out so that tests can answer this question.")
717 (defun ede-check-project-directory (dir)
718 "Check if DIR should be in `ede-project-directories'.
719 If it is not, try asking the user if it should be added; if so,
720 add it and save `ede-project-directories' via Customize.
721 Return nil if DIR should not be in `ede-project-directories'."
722 (setq dir (directory-file-name (expand-file-name dir))) ; strip trailing /
723 (or (eq ede-project-directories t)
724 (and (functionp ede-project-directories)
725 (funcall ede-project-directories dir))
726 ;; If `ede-project-directories' is a list, maybe add it.
727 (when (listp ede-project-directories)
728 (or (member dir ede-project-directories)
729 (when (funcall ede-check-project-query-fcn
730 (format "`%s' is not listed in `ede-project-directories'.
731 Add it to the list of allowed project directories? "
732 dir))
733 (push dir ede-project-directories)
734 ;; If possible, save `ede-project-directories'.
735 (if (or custom-file user-init-file)
736 (let ((coding-system-for-read nil))
737 (customize-save-variable
738 'ede-project-directories
739 ede-project-directories)))
740 t)))))
742 (defun ede-new (type &optional name)
743 "Create a new project starting from project type TYPE.
744 Optional argument NAME is the name to give this project."
745 (interactive
746 (list (completing-read "Project Type: "
747 (object-assoc-list
748 'name
749 (let* ((l ede-project-class-files)
750 (cp (ede-current-project))
751 (cs (when cp (eieio-object-class cp)))
752 (r nil))
753 (while l
754 (if cs
755 (if (eq (oref (car l) :class-sym)
757 (setq r (cons (car l) r)))
758 (if (oref (car l) new-p)
759 (setq r (cons (car l) r))))
760 (setq l (cdr l)))
761 (when (not r)
762 (if cs
763 (error "No valid interactive sub project types for %s"
765 (error "EDE error: Can't fin project types to create")))
768 nil t)))
769 (require 'ede/custom)
770 ;; Make sure we have a valid directory
771 (when (not (file-exists-p default-directory))
772 (error "Cannot create project in non-existent directory %s" default-directory))
773 (when (not (file-writable-p default-directory))
774 (error "No write permissions for %s" default-directory))
775 (unless (ede-check-project-directory default-directory)
776 (error "%s is not an allowed project directory in `ede-project-directories'"
777 default-directory))
778 ;; Make sure the project directory is loadable in the future.
779 (ede-check-project-directory default-directory)
780 ;; Create the project
781 (let* ((obj (object-assoc type 'name ede-project-class-files))
782 (nobj (let ((f (oref obj file))
783 (pf (oref obj proj-file)))
784 ;; We are about to make something new, changing the
785 ;; state of existing directories.
786 (ede-project-directory-remove-hash default-directory)
787 ;; Make sure this class gets loaded!
788 (require f)
789 (make-instance (oref obj class-sym)
790 :name (or name (read-string "Name: "))
791 :directory default-directory
792 :file (cond ((stringp pf)
793 (expand-file-name pf))
794 ((fboundp pf)
795 (funcall pf))
797 (error
798 "Unknown file name specifier %S"
799 pf)))
800 :targets nil)
803 (inits (oref obj initializers)))
804 ;; Force the name to match for new objects.
805 (eieio-object-set-name-string nobj (oref nobj :name))
806 ;; Handle init args.
807 (while inits
808 (eieio-oset nobj (car inits) (car (cdr inits)))
809 (setq inits (cdr (cdr inits))))
810 (let ((pp (ede-parent-project)))
811 (when pp
812 (ede-add-subproject pp nobj)
813 (ede-commit-project pp)))
814 (ede-commit-project nobj))
815 ;; Once the project is created, load it again. This used to happen
816 ;; lazily, but with project loading occurring less often and with
817 ;; security in mind, this is now the safe time to reload.
818 (ede-load-project-file default-directory)
819 ;; Have the menu appear
820 (setq ede-minor-mode t)
821 ;; Allert the user
822 (message "Project created and saved. You may now create targets."))
824 (defmethod ede-add-subproject ((proj-a ede-project) proj-b)
825 "Add into PROJ-A, the subproject PROJ-B."
826 (oset proj-a subproj (cons proj-b (oref proj-a subproj))))
828 (defun ede-invoke-method (sym &rest args)
829 "Invoke method SYM on the current buffer's project object.
830 ARGS are additional arguments to pass to method SYM."
831 (if (not ede-object)
832 (error "Cannot invoke %s for %s" (symbol-name sym)
833 (buffer-name)))
834 ;; Always query a target. There should never be multiple
835 ;; projects in a single buffer.
836 (apply sym (ede-singular-object "Target: ") args))
838 (defun ede-rescan-toplevel ()
839 "Rescan all project files."
840 (interactive)
841 (when (not (ede-toplevel))
842 ;; This directory isn't open. Can't rescan.
843 (error "Attempt to rescan a project that isn't open"))
845 ;; Continue
846 (let ((root (ede-toplevel))
847 (ede-deep-rescan t))
849 (project-rescan root)
850 (ede-reset-all-buffers)
853 (defun ede-new-target (&rest args)
854 "Create a new target specific to this type of project file.
855 Different projects accept different arguments ARGS.
856 Typically you can specify NAME, target TYPE, and AUTOADD, where AUTOADD is
857 a string \"y\" or \"n\", which answers the y/n question done interactively."
858 (interactive)
859 (apply 'project-new-target (ede-current-project) args)
860 (when (and buffer-file-name
861 (not (file-directory-p buffer-file-name)))
862 (setq ede-object nil)
863 (setq ede-object (ede-buffer-object (current-buffer)))
864 (ede-apply-target-options)))
866 (defun ede-new-target-custom ()
867 "Create a new target specific to this type of project file."
868 (interactive)
869 (project-new-target-custom (ede-current-project)))
871 (defun ede-delete-target (target)
872 "Delete TARGET from the current project."
873 (interactive (list
874 (let ((ede-object (ede-current-project)))
875 (ede-invoke-method 'project-interactive-select-target
876 "Target: "))))
877 ;; Find all sources in buffers associated with the condemned buffer.
878 (let ((condemned (ede-target-buffers target)))
879 (project-delete-target target)
880 ;; Loop over all project controlled buffers
881 (save-excursion
882 (while condemned
883 (set-buffer (car condemned))
884 (setq ede-object nil)
885 (setq ede-object (ede-buffer-object (current-buffer)))
886 (setq condemned (cdr condemned))))
887 (ede-apply-target-options)))
889 (defun ede-add-file (target)
890 "Add the current buffer to a TARGET in the current project."
891 (interactive (list
892 (let ((ede-object (ede-current-project)))
893 (ede-invoke-method 'project-interactive-select-target
894 "Target: "))))
895 (when (stringp target)
896 (let* ((proj (ede-current-project))
897 (ob (object-assoc-list 'name (oref proj targets))))
898 (setq target (cdr (assoc target ob)))))
900 (when (not target)
901 (error "Could not find specified target %S" target))
903 (project-add-file target (buffer-file-name))
904 (setq ede-object nil)
906 ;; Setup buffer local variables.
907 (ede-initialize-state-current-buffer)
909 (when (not ede-object)
910 (error "Can't add %s to target %s: Wrong file type"
911 (file-name-nondirectory (buffer-file-name))
912 (eieio-object-name target)))
913 (ede-apply-target-options))
915 (defun ede-remove-file (&optional force)
916 "Remove the current file from targets.
917 Optional argument FORCE forces the file to be removed without asking."
918 (interactive "P")
919 (if (not ede-object)
920 (error "Cannot invoke remove-file for %s" (buffer-name)))
921 (let ((eo (if (listp ede-object)
922 (prog1
923 ede-object
924 (setq force nil))
925 (list ede-object))))
926 (while eo
927 (if (or force (y-or-n-p (format "Remove from %s? " (ede-name (car eo)))))
928 (project-remove-file (car eo) (buffer-file-name)))
929 (setq eo (cdr eo)))
930 (setq ede-object nil)
931 (setq ede-object (ede-buffer-object (current-buffer)))
932 (ede-apply-target-options)))
934 (defun ede-edit-file-target ()
935 "Enter the project file to hand edit the current buffer's target."
936 (interactive)
937 (ede-invoke-method 'project-edit-file-target))
939 ;;; Compilation / Debug / Run
941 (defun ede-compile-project ()
942 "Compile the current project."
943 (interactive)
944 ;; @TODO - This just wants the root. There should be a better way.
945 (let ((cp (ede-current-project)))
946 (while (ede-parent-project cp)
947 (setq cp (ede-parent-project cp)))
948 (let ((ede-object cp))
949 (ede-invoke-method 'project-compile-project))))
951 (defun ede-compile-selected (target)
952 "Compile some TARGET from the current project."
953 (interactive (list (project-interactive-select-target (ede-current-project)
954 "Target to Build: ")))
955 (project-compile-target target))
957 (defun ede-compile-target ()
958 "Compile the current buffer's associated target."
959 (interactive)
960 (ede-invoke-method 'project-compile-target))
962 (defun ede-debug-target ()
963 "Debug the current buffer's associated target."
964 (interactive)
965 (ede-invoke-method 'project-debug-target))
967 (defun ede-run-target ()
968 "Run the current buffer's associated target."
969 (interactive)
970 (ede-invoke-method 'project-run-target))
972 (defun ede-make-dist ()
973 "Create a distribution from the current project."
974 (interactive)
975 (let ((ede-object (ede-toplevel)))
976 (ede-invoke-method 'project-make-dist)))
979 ;;; EDE project target baseline methods.
981 ;; If you are developing a new project type, you need to implement
982 ;; all of these methods, unless, of course, they do not make sense
983 ;; for your particular project.
985 ;; Your targets should inherit from `ede-target', and your project
986 ;; files should inherit from `ede-project'. Create the appropriate
987 ;; methods based on those below.
989 (defmethod project-interactive-select-target ((this ede-project-placeholder) prompt)
990 ; checkdoc-params: (prompt)
991 "Make sure placeholder THIS is replaced with the real thing, and pass through."
992 (project-interactive-select-target this prompt))
994 (defmethod project-interactive-select-target ((this ede-project) prompt)
995 "Interactively query for a target that exists in project THIS.
996 Argument PROMPT is the prompt to use when querying the user for a target."
997 (let ((ob (object-assoc-list 'name (oref this targets))))
998 (cdr (assoc (completing-read prompt ob nil t) ob))))
1000 (defmethod project-add-file ((this ede-project-placeholder) file)
1001 ; checkdoc-params: (file)
1002 "Make sure placeholder THIS is replaced with the real thing, and pass through."
1003 (project-add-file this file))
1005 (defmethod project-add-file ((ot ede-target) file)
1006 "Add the current buffer into project project target OT.
1007 Argument FILE is the file to add."
1008 (error "add-file not supported by %s" (eieio-object-name ot)))
1010 (defmethod project-remove-file ((ot ede-target) fnnd)
1011 "Remove the current buffer from project target OT.
1012 Argument FNND is an argument."
1013 (error "remove-file not supported by %s" (eieio-object-name ot)))
1015 (defmethod project-edit-file-target ((ot ede-target))
1016 "Edit the target OT associated with this file."
1017 (find-file (oref (ede-current-project) file)))
1019 (defmethod project-new-target ((proj ede-project) &rest args)
1020 "Create a new target. It is up to the project PROJ to get the name."
1021 (error "new-target not supported by %s" (eieio-object-name proj)))
1023 (defmethod project-new-target-custom ((proj ede-project))
1024 "Create a new target. It is up to the project PROJ to get the name."
1025 (error "New-target-custom not supported by %s" (eieio-object-name proj)))
1027 (defmethod project-delete-target ((ot ede-target))
1028 "Delete the current target OT from its parent project."
1029 (error "add-file not supported by %s" (eieio-object-name ot)))
1031 (defmethod project-compile-project ((obj ede-project) &optional command)
1032 "Compile the entire current project OBJ.
1033 Argument COMMAND is the command to use when compiling."
1034 (error "compile-project not supported by %s" (eieio-object-name obj)))
1036 (defmethod project-compile-target ((obj ede-target) &optional command)
1037 "Compile the current target OBJ.
1038 Argument COMMAND is the command to use for compiling the target."
1039 (error "compile-target not supported by %s" (eieio-object-name obj)))
1041 (defmethod project-debug-target ((obj ede-target))
1042 "Run the current project target OBJ in a debugger."
1043 (error "debug-target not supported by %s" (eieio-object-name obj)))
1045 (defmethod project-run-target ((obj ede-target))
1046 "Run the current project target OBJ."
1047 (error "run-target not supported by %s" (eieio-object-name obj)))
1049 (defmethod project-make-dist ((this ede-project))
1050 "Build a distribution for the project based on THIS project."
1051 (error "Make-dist not supported by %s" (eieio-object-name this)))
1053 (defmethod project-dist-files ((this ede-project))
1054 "Return a list of files that constitute a distribution of THIS project."
1055 (error "Dist-files is not supported by %s" (eieio-object-name this)))
1057 (defmethod project-rescan ((this ede-project))
1058 "Rescan the EDE project THIS."
1059 (error "Rescanning a project is not supported by %s" (eieio-object-name this)))
1061 (defun ede-ecb-project-paths ()
1062 "Return a list of all paths for all active EDE projects.
1063 This functions is meant for use with ECB."
1064 (let ((p ede-projects)
1065 (d nil))
1066 (while p
1067 (setq d (cons (file-name-directory (oref (car p) file))
1069 p (cdr p)))
1072 ;;; PROJECT LOADING/TRACKING
1074 (defun ede-add-project-to-global-list (proj)
1075 "Add the project PROJ to the master list of projects.
1076 On success, return the added project."
1077 (when (not proj)
1078 (error "No project created to add to master list"))
1079 (when (not (eieio-object-p proj))
1080 (error "Attempt to add non-object to master project list"))
1081 (when (not (obj-of-class-p proj ede-project-placeholder))
1082 (error "Attempt to add a non-project to the ede projects list"))
1083 (add-to-list 'ede-projects proj)
1084 proj)
1086 (defun ede-delete-project-from-global-list (proj)
1087 "Remove project PROJ from the master list of projects."
1088 (setq ede-projects (remove proj ede-projects)))
1090 (defun ede-flush-deleted-projects ()
1091 "Scan the projects list for projects which no longer exist.
1092 Flush the dead projects from the project cache."
1093 (interactive)
1094 (let ((dead nil))
1095 (dolist (P ede-projects)
1096 (when (not (file-exists-p (oref P :file)))
1097 (add-to-list 'dead P)))
1098 (dolist (D dead)
1099 (ede-delete-project-from-global-list D))
1102 (defun ede-global-list-sanity-check ()
1103 "Perform a sanity check to make sure there are no duplicate projects."
1104 (interactive)
1105 (let ((scanned nil))
1106 (dolist (P ede-projects)
1107 (if (member (oref P :directory) scanned)
1108 (error "Duplicate project (by dir) found in %s!" (oref P :directory))
1109 (push (oref P :directory) scanned)))
1110 (unless ede--disable-inode
1111 (setq scanned nil)
1112 (dolist (P ede-projects)
1113 (if (member (ede--project-inode P) scanned)
1114 (error "Duplicate project (by inode) found in %s!" (ede--project-inode P))
1115 (push (ede--project-inode P) scanned))))
1116 (message "EDE by directory %sis still sane." (if ede--disable-inode "" "& inode "))))
1118 (defun ede-load-project-file (dir &optional detectin rootreturn)
1119 "Project file independent way to read a project in from DIR.
1120 Optional DETECTIN is an autoload cons from `ede-detect-directory-for-project'
1121 which can be passed in to save time.
1122 Optional ROOTRETURN will return the root project for DIR."
1123 ;; Don't do anything if we are in the process of
1124 ;; constructing an EDE object.
1126 ;; Prevent recursion.
1127 (unless ede-constructing
1129 ;; Only load if something new is going on. Flush the dirhash.
1130 (ede-project-directory-remove-hash dir)
1132 ;; Do the load
1133 ;;(message "EDE LOAD : %S" file)
1134 (let* ((file dir)
1135 (path (file-name-as-directory (expand-file-name dir)))
1136 (detect (or detectin (ede-directory-project-cons path)))
1137 (autoloader nil)
1138 (toppath nil)
1139 (o nil))
1141 (when detect
1142 (setq toppath (car detect))
1143 (setq autoloader (cdr detect))
1145 ;; See if it's been loaded before. Use exact matching since
1146 ;; know that 'toppath' is the root of the project.
1147 (setq o (ede-directory-get-toplevel-open-project toppath 'exact))
1149 ;; If not open yet, load it.
1150 (unless o
1151 ;; NOTE: We set ede-constructing to the autoloader we are using.
1152 ;; Some project types have one class, but many autoloaders
1153 ;; and this is how we tell the instantiation which kind of
1154 ;; project to make.
1155 (let ((ede-constructing autoloader))
1157 ;; This is the only place `ede-auto-load-project' should be called.
1159 (setq o (ede-auto-load-project autoloader toppath))))
1161 ;; Return the found root project.
1162 (when rootreturn (set rootreturn o))
1164 ;; The project has been found (in the global list) or loaded from
1165 ;; disk (via autoloader.) We can now search for the project asked
1166 ;; for from DIR in the sub-list.
1167 (ede-find-subproject-for-directory o path)
1169 ;; Return the project.
1170 o))))
1172 ;;; PROJECT ASSOCIATIONS
1174 ;; Moving between relative projects. Associating between buffers and
1175 ;; projects.
1176 (defun ede-parent-project (&optional obj)
1177 "Return the project belonging to the parent directory.
1178 Return nil if there is no previous directory.
1179 Optional argument OBJ is an object to find the parent of."
1180 (let* ((proj (or obj ede-object-project)) ;; Current project.
1181 (root (if obj (ede-project-root obj)
1182 ede-object-root-project)))
1183 ;; This case is a SHORTCUT if the project has defined
1184 ;; a way to calculate the project root.
1185 (if (and root proj (eq root proj))
1186 nil ;; we are at the root.
1187 ;; Else, we may have a nil proj or root.
1188 (let* ((thisdir (if obj (oref obj directory)
1189 default-directory))
1190 (updir (ede-up-directory thisdir)))
1191 (when updir
1192 ;; If there was no root, perhaps we can derive it from
1193 ;; updir now.
1194 (let ((root (or root (ede-directory-get-toplevel-open-project updir))))
1196 ;; This lets us find a subproject under root based on updir.
1197 (and root
1198 (ede-find-subproject-for-directory root updir))
1199 ;; Try the all structure based search.
1200 (ede-directory-get-open-project updir))))))))
1202 (defun ede-current-project (&optional dir)
1203 "Return the current project file.
1204 If optional DIR is provided, get the project for DIR instead."
1205 ;; If it matches the current directory, do we have a pre-existing project?
1206 (let ((proj (when (and (or (not dir) (string= dir default-directory))
1207 ede-object-project)
1208 ede-object-project)))
1209 ;; No current project.
1210 (if proj
1211 proj
1212 (let* ((ldir (or dir default-directory)))
1213 (ede-directory-get-open-project ldir)))))
1215 (defun ede-buffer-object (&optional buffer projsym)
1216 "Return the target object for BUFFER.
1217 This function clears cached values and recalculates.
1218 Optional PROJSYM is a symbol, which will be set to the project
1219 that contains the target that becomes buffer's object."
1220 (save-excursion
1221 (if (not buffer) (setq buffer (current-buffer)))
1222 (set-buffer buffer)
1223 (setq ede-object nil)
1224 (let* ((localpo (ede-current-project))
1225 (po localpo)
1226 (top (ede-toplevel po)))
1227 (if po (setq ede-object (ede-find-target po buffer)))
1228 ;; If we get nothing, go with the backup plan of slowly
1229 ;; looping upward
1230 (while (and (not ede-object) (not (eq po top)))
1231 (setq po (ede-parent-project po))
1232 (if po (setq ede-object (ede-find-target po buffer))))
1233 ;; Filter down to 1 project if there are dups.
1234 (if (= (length ede-object) 1)
1235 (setq ede-object (car ede-object)))
1236 ;; Track the project, if needed.
1237 (when (and projsym (symbolp projsym))
1238 (if ede-object
1239 ;; If we found a target, then PO is the
1240 ;; project to use.
1241 (set projsym po)
1242 ;; If there is no ede-object, then the projsym
1243 ;; is whichever part of the project is most local.
1244 (set projsym localpo))
1246 ;; Return our findings.
1247 ede-object))
1249 (defmethod ede-target-in-project-p ((proj ede-project) target)
1250 "Is PROJ the parent of TARGET?
1251 If TARGET belongs to a subproject, return that project file."
1252 (if (and (slot-boundp proj 'targets)
1253 (memq target (oref proj targets)))
1254 proj
1255 (let ((s (oref proj subproj))
1256 (ans nil))
1257 (while (and s (not ans))
1258 (setq ans (ede-target-in-project-p (car s) target))
1259 (setq s (cdr s)))
1260 ans)))
1262 (defun ede-target-parent (target)
1263 "Return the project which is the parent of TARGET.
1264 It is recommended you track the project a different way as this function
1265 could become slow in time."
1266 (or ede-object-project
1267 ;; If not cached, derive it from the current directory of the target.
1268 (let ((ans nil) (projs ede-projects))
1269 (while (and (not ans) projs)
1270 (setq ans (ede-target-in-project-p (car projs) target)
1271 projs (cdr projs)))
1272 ans)))
1274 (defmethod ede-find-target ((proj ede-project) buffer)
1275 "Fetch the target in PROJ belonging to BUFFER or nil."
1276 (with-current-buffer buffer
1278 ;; We can do a short-ut if ede-object local variable is set.
1279 (if ede-object
1280 ;; If the buffer is already loaded with good EDE stuff, make sure the
1281 ;; saved project is the project we're looking for.
1282 (when (and ede-object-project (eq proj ede-object-project)) ede-object)
1284 ;; If the variable wasn't set, then we are probably initializing the buffer.
1285 ;; In that case, search the file system.
1286 (if (ede-buffer-mine proj buffer)
1287 proj
1288 (let ((targets (oref proj targets))
1289 (f nil))
1290 (while targets
1291 (if (ede-buffer-mine (car targets) buffer)
1292 (setq f (cons (car targets) f)))
1293 (setq targets (cdr targets)))
1294 f)))))
1296 (defmethod ede-target-buffer-in-sourcelist ((this ede-target) buffer source)
1297 "Return non-nil if object THIS is in BUFFER to a SOURCE list.
1298 Handles complex path issues."
1299 (member (ede-convert-path this (buffer-file-name buffer)) source))
1301 (defmethod ede-buffer-mine ((this ede-project) buffer)
1302 "Return non-nil if object THIS lays claim to the file in BUFFER."
1303 nil)
1305 (defmethod ede-buffer-mine ((this ede-target) buffer)
1306 "Return non-nil if object THIS lays claim to the file in BUFFER."
1307 (condition-case nil
1308 (ede-target-buffer-in-sourcelist this buffer (oref this source))
1309 ;; An error implies a bad match.
1310 (error nil)))
1313 ;;; Project mapping
1315 (defun ede-project-buffers (project)
1316 "Return a list of all active buffers controlled by PROJECT.
1317 This includes buffers controlled by a specific target of PROJECT."
1318 (let ((bl (buffer-list))
1319 (pl nil))
1320 (while bl
1321 (with-current-buffer (car bl)
1322 (when (and ede-object (ede-find-target project (car bl)))
1323 (setq pl (cons (car bl) pl))))
1324 (setq bl (cdr bl)))
1325 pl))
1327 (defun ede-target-buffers (target)
1328 "Return a list of buffers that are controlled by TARGET."
1329 (let ((bl (buffer-list))
1330 (pl nil))
1331 (while bl
1332 (with-current-buffer (car bl)
1333 (if (if (listp ede-object)
1334 (memq target ede-object)
1335 (eq ede-object target))
1336 (setq pl (cons (car bl) pl))))
1337 (setq bl (cdr bl)))
1338 pl))
1340 (defun ede-buffers ()
1341 "Return a list of all buffers controlled by an EDE object."
1342 (let ((bl (buffer-list))
1343 (pl nil))
1344 (while bl
1345 (with-current-buffer (car bl)
1346 (if ede-object
1347 (setq pl (cons (car bl) pl))))
1348 (setq bl (cdr bl)))
1349 pl))
1351 (defun ede-map-buffers (proc)
1352 "Execute PROC on all buffers controlled by EDE."
1353 (mapcar proc (ede-buffers)))
1355 (defmethod ede-map-project-buffers ((this ede-project) proc)
1356 "For THIS, execute PROC on all buffers belonging to THIS."
1357 (mapcar proc (ede-project-buffers this)))
1359 (defmethod ede-map-target-buffers ((this ede-target) proc)
1360 "For THIS, execute PROC on all buffers belonging to THIS."
1361 (mapcar proc (ede-target-buffers this)))
1363 ;; other types of mapping
1364 (defmethod ede-map-subprojects ((this ede-project) proc)
1365 "For object THIS, execute PROC on all direct subprojects.
1366 This function does not apply PROC to sub-sub projects.
1367 See also `ede-map-all-subprojects'."
1368 (mapcar proc (oref this subproj)))
1370 (defmethod ede-map-all-subprojects ((this ede-project) allproc)
1371 "For object THIS, execute PROC on THIS and all subprojects.
1372 This function also applies PROC to sub-sub projects.
1373 See also `ede-map-subprojects'."
1374 (apply 'append
1375 (list (funcall allproc this))
1376 (ede-map-subprojects
1377 this
1378 (lambda (sp)
1379 (ede-map-all-subprojects sp allproc))
1382 ;; (ede-map-all-subprojects (ede-load-project-file "../semantic/") (lambda (sp) (oref sp file)))
1384 (defmethod ede-map-targets ((this ede-project) proc)
1385 "For object THIS, execute PROC on all targets."
1386 (mapcar proc (oref this targets)))
1388 (defmethod ede-map-any-target-p ((this ede-project) proc)
1389 "For project THIS, map PROC to all targets and return if any non-nil.
1390 Return the first non-nil value returned by PROC."
1391 (eval (cons 'or (ede-map-targets this proc))))
1394 ;;; Some language specific methods.
1396 ;; These items are needed by ede-cpp-root to add better support for
1397 ;; configuring items for Semantic.
1399 ;; Generic paths
1400 (defmethod ede-system-include-path ((this ede-project))
1401 "Get the system include path used by project THIS."
1402 nil)
1404 (defmethod ede-system-include-path ((this ede-target))
1405 "Get the system include path used by project THIS."
1406 nil)
1408 (defmethod ede-source-paths ((this ede-project) mode)
1409 "Get the base to all source trees in the current project for MODE.
1410 For example, <root>/src for sources of c/c++, Java, etc,
1411 and <root>/doc for doc sources."
1412 nil)
1414 ;; C/C++
1415 (defun ede-apply-preprocessor-map ()
1416 "Apply preprocessor tables onto the current buffer."
1417 ;; TODO - what if semantic-mode isn't enabled?
1418 ;; what if we never want to load a C mode? Does this matter?
1419 ;; Note: This require is needed for the case where EDE ends up
1420 ;; in the hook order before Semantic based hooks.
1421 (require 'semantic/lex-spp)
1422 (when (and ede-object
1423 (boundp 'semantic-lex-spp-project-macro-symbol-obarray))
1424 (let* ((objs ede-object)
1425 (map (ede-preprocessor-map (if (consp objs)
1426 (car objs)
1427 objs))))
1428 (when map
1429 ;; We can't do a require for the below symbol.
1430 (setq semantic-lex-spp-project-macro-symbol-obarray
1431 (semantic-lex-make-spp-table map)))
1432 (when (consp objs)
1433 (message "Choosing preprocessor syms for project %s"
1434 (eieio-object-name (car objs)))))))
1436 (defmethod ede-system-include-path ((this ede-project))
1437 "Get the system include path used by project THIS."
1438 nil)
1440 (defmethod ede-preprocessor-map ((this ede-project))
1441 "Get the pre-processor map for project THIS."
1442 nil)
1444 (defmethod ede-preprocessor-map ((this ede-target))
1445 "Get the pre-processor map for project THIS."
1446 nil)
1448 ;; Java
1449 (defmethod ede-java-classpath ((this ede-project))
1450 "Return the classpath for this project."
1451 ;; @TODO - Can JDEE add something here?
1452 nil)
1455 ;;; Project-local variables
1457 (defun ede-set (variable value &optional proj)
1458 "Set the project local VARIABLE to VALUE.
1459 If VARIABLE is not project local, just use set. Optional argument PROJ
1460 is the project to use, instead of `ede-current-project'."
1461 (interactive "sVariable: \nxExpression: ")
1462 (let ((p (or proj (ede-toplevel)))
1464 ;; Make the change
1465 (ede-make-project-local-variable variable p)
1466 (ede-set-project-local-variable variable value p)
1467 (ede-commit-local-variables p)
1469 ;; This is a heavy hammer, but will apply variables properly
1470 ;; based on stacking between the toplevel and child projects.
1471 (ede-map-buffers 'ede-apply-project-local-variables)
1473 value))
1475 (defun ede-apply-project-local-variables (&optional buffer)
1476 "Apply project local variables to the current buffer."
1477 (with-current-buffer (or buffer (current-buffer))
1478 ;; Always apply toplevel variables.
1479 (if (not (eq (ede-current-project) (ede-toplevel)))
1480 (ede-set-project-variables (ede-toplevel)))
1481 ;; Next apply more local project's variables.
1482 (if (ede-current-project)
1483 (ede-set-project-variables (ede-current-project)))
1486 (defun ede-make-project-local-variable (variable &optional project)
1487 "Make VARIABLE project-local to PROJECT."
1488 (if (not project) (setq project (ede-toplevel)))
1489 (if (assoc variable (oref project local-variables))
1491 (oset project local-variables (cons (list variable)
1492 (oref project local-variables)))))
1494 (defun ede-set-project-local-variable (variable value &optional project)
1495 "Set VARIABLE to VALUE for PROJECT.
1496 If PROJ isn't specified, use the current project.
1497 This function only assigns the value within the project structure.
1498 It does not apply the value to buffers."
1499 (if (not project) (setq project (ede-toplevel)))
1500 (let ((va (assoc variable (oref project local-variables))))
1501 (unless va
1502 (error "Cannot set project variable until it is added with `ede-make-project-local-variable'"))
1503 (setcdr va value)))
1505 (defmethod ede-set-project-variables ((project ede-project) &optional buffer)
1506 "Set variables local to PROJECT in BUFFER."
1507 (if (not buffer) (setq buffer (current-buffer)))
1508 (with-current-buffer buffer
1509 (dolist (v (oref project local-variables))
1510 (make-local-variable (car v))
1511 (set (car v) (cdr v)))))
1513 (defmethod ede-commit-local-variables ((proj ede-project))
1514 "Commit change to local variables in PROJ."
1515 nil)
1517 (provide 'ede)
1519 ;; Include this last because it depends on ede.
1520 (require 'ede/files)
1522 ;; If this does not occur after the provide, we can get a recursive
1523 ;; load. Yuck!
1524 (if (featurep 'speedbar)
1525 (ede-speedbar-file-setup)
1526 (add-hook 'speedbar-load-hook 'ede-speedbar-file-setup))
1528 ;;; ede.el ends here