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