auto.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ;;; ede/auto.el --- Autoload features for EDE
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; EDE Autoloads are a way to refer to different project types without
  18. ;; loading those projects into Emacs.
  19. ;;
  20. ;; These routines are used to detect a project in a filesystem before
  21. ;; handing over control to the usual EDE project system.
  22. ;;; Code:
  23. (require 'eieio)
  24. (declare-function ede-directory-safe-p "ede")
  25. (declare-function ede-add-project-to-global-list "ede")
  26. (defclass ede-project-autoload ()
  27. ((name :initarg :name
  28. :documentation "Name of this project type")
  29. (file :initarg :file
  30. :documentation "The lisp file belonging to this class.")
  31. (proj-file :initarg :proj-file
  32. :documentation "Name of a project file of this type.")
  33. (proj-root :initarg :proj-root
  34. :type function
  35. :documentation "A function symbol to call for the project root.
  36. This function takes no arguments, and returns the current directories
  37. root, if available. Leave blank to use the EDE directory walking
  38. routine instead.")
  39. (initializers :initarg :initializers
  40. :initform nil
  41. :documentation
  42. "Initializers passed to the project object.
  43. These are used so there can be multiple types of projects
  44. associated with a single object class, based on the initializers used.")
  45. (load-type :initarg :load-type
  46. :documentation "Fn symbol used to load this project file.")
  47. (class-sym :initarg :class-sym
  48. :documentation "Symbol representing the project class to use.")
  49. (new-p :initarg :new-p
  50. :initform t
  51. :documentation
  52. "Non-nil if this is an option when a user creates a project.")
  53. (safe-p :initarg :safe-p
  54. :initform t
  55. :documentation
  56. "Non-nil if the project load files are \"safe\".
  57. An unsafe project is one that loads project variables via Emacs
  58. Lisp code. A safe project is one that loads project variables by
  59. scanning files without loading Lisp code from them.")
  60. )
  61. "Class representing minimal knowledge set to run preliminary EDE functions.
  62. When more advanced functionality is needed from a project type, that projects
  63. type is required and the load function used.")
  64. (defvar ede-project-class-files
  65. (list
  66. (ede-project-autoload "edeproject-makefile"
  67. :name "Make" :file 'ede/proj
  68. :proj-file "Project.ede"
  69. :load-type 'ede-proj-load
  70. :class-sym 'ede-proj-project
  71. :safe-p nil)
  72. (ede-project-autoload "edeproject-automake"
  73. :name "Automake" :file 'ede/proj
  74. :proj-file "Project.ede"
  75. :initializers '(:makefile-type Makefile.am)
  76. :load-type 'ede-proj-load
  77. :class-sym 'ede-proj-project
  78. :safe-p nil)
  79. (ede-project-autoload "automake"
  80. :name "automake" :file 'ede/project-am
  81. :proj-file "Makefile.am"
  82. :load-type 'project-am-load
  83. :class-sym 'project-am-makefile
  84. :new-p nil))
  85. "List of vectors defining how to determine what type of projects exist.")
  86. (put 'ede-project-class-files 'risky-local-variable t)
  87. ;;; EDE project-autoload methods
  88. ;;
  89. (defmethod ede-project-root ((this ede-project-autoload))
  90. "If a project knows its root, return it here.
  91. Allows for one-project-object-for-a-tree type systems."
  92. nil)
  93. (defmethod ede-project-root-directory ((this ede-project-autoload)
  94. &optional file)
  95. "If a project knows its root, return it here.
  96. Allows for one-project-object-for-a-tree type systems.
  97. Optional FILE is the file to test. If there is no FILE, use
  98. the current buffer."
  99. (when (not file)
  100. (setq file default-directory))
  101. (when (slot-boundp this :proj-root)
  102. (let ((rootfcn (oref this proj-root)))
  103. (when rootfcn
  104. (condition-case nil
  105. (funcall rootfcn file)
  106. (error
  107. (funcall rootfcn)))
  108. ))))
  109. (defmethod ede-dir-to-projectfile ((this ede-project-autoload) dir)
  110. "Return a full file name of project THIS found in DIR.
  111. Return nil if the project file does not exist."
  112. (let* ((d (file-name-as-directory dir))
  113. (root (ede-project-root-directory this d))
  114. (pf (oref this proj-file))
  115. (f (cond ((stringp pf)
  116. (expand-file-name pf (or root d)))
  117. ((and (symbolp pf) (fboundp pf))
  118. (funcall pf (or root d)))))
  119. )
  120. (when (and f (file-exists-p f))
  121. f)))
  122. (defmethod ede-auto-load-project ((this ede-project-autoload) dir)
  123. "Load in the project associated with THIS project autoload description.
  124. THIS project description should be valid for DIR, where the project will
  125. be loaded."
  126. ;; Last line of defense: don't load unsafe projects.
  127. (when (not (or (oref this :safe-p)
  128. (ede-directory-safe-p dir)))
  129. (error "Attempt to load an unsafe project (bug elsewhere in EDE)"))
  130. ;; Things are good - so load the project.
  131. (let ((o (funcall (oref this load-type) dir)))
  132. (when (not o)
  133. (error "Project type error: :load-type failed to create a project"))
  134. (ede-add-project-to-global-list o)))
  135. (provide 'ede/auto)
  136. ;;; ede/auto.el ends here