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