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