ede.el 48 KB

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