eieio-speedbar.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. ;;; eieio-speedbar.el -- Classes for managing speedbar displays.
  2. ;; Copyright (C) 1999-2002, 2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Version: 0.2
  5. ;; Keywords: OO, tools
  6. ;; Package: eieio
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;
  20. ;; This provides some classes that can be used as a parent which
  21. ;; will automatically provide SPEEDBAR support for any list of objects
  22. ;; of that type.
  23. ;;
  24. ;; This file requires speedbar version 0.10 or later.
  25. ;;; Creating a new speedbar mode based on a pre-existing object hierarchy
  26. ;;
  27. ;; To create a new speedbar mode based on lists of objects is easier
  28. ;; than creating a whole new speedbar mode from scratch.
  29. ;;
  30. ;; 1) Objects that will have lists of items that can be expanded
  31. ;; should also inherit from the classes:
  32. ;; * `eieio-speedbar' - specify your own button behavior
  33. ;; * `eieio-speedbar-directory-button' - objects that behave like directories
  34. ;; * `eieio-speedbar-file-button' - objects that behave like files
  35. ;;
  36. ;; 2) Objects that have lists of children should implement the method
  37. ;; `eieio-speedbar-object-children' which returns a list of more
  38. ;; objects, or a list of strings.
  39. ;;
  40. ;; 3) Objects that return a list of strings should also implement these
  41. ;; methods:
  42. ;; * `eieio-speedbar-child-make-tag-lines' - make tag lines for a child.
  43. ;; * `eieio-speedbar-child-description' - describe non-object children
  44. ;;
  45. ;; 4) Objects which have expanded information should implement the method
  46. ;; `eieio-speedbar-description' to produce more information.
  47. ;;
  48. ;; 5) Objects that are associated with a directory should implement
  49. ;; the method `eieio-speedbar-derive-line-path' which returns a
  50. ;; path.
  51. ;;
  52. ;; 6) Objects that have a specialized behavior when clicked should
  53. ;; define the method `eieio-speedbar-handle-click'.
  54. ;;
  55. ;; To initialize a new eieio based speedbar display, do the following.
  56. ;;
  57. ;; 1) Create a keymap variable `foo-speedbar-key-map'.
  58. ;; This keymap variable should be initialized in a function.
  59. ;; If you have no special needs, use `eieio-speedbar-key-map'
  60. ;;
  61. ;; 2) Create a variable containing an easymenu definition compatible
  62. ;; with speedbar. if you have no special needs, use
  63. ;; `eieio-speedbar-menu'.
  64. ;;
  65. ;; 3) Create a function which returns the top-level list of children
  66. ;; objects to be displayed in speedbar.
  67. ;;
  68. ;; 4) Call `eieio-speedbar-create' as specified in it's documentation
  69. ;; string. This will automatically handle cases when speedbar is
  70. ;; not already loaded, and specifying all overload functions.
  71. ;;
  72. ;; 5) Create an initializer function which looks like this:
  73. ;;
  74. ;; (defun my-speedbar-mode-initialize ()
  75. ;; "documentation"
  76. ;; (interactive)
  77. ;; (speedbar-frame-mode 1)
  78. ;; (speedbar-change-initial-expansion-list mymodename)
  79. ;; (speedbar-get-focus))
  80. ;;
  81. ;; where `mymodename' is the same value as passed to `eieio-speedbar-create'
  82. ;; as the MODENAME parameter.
  83. ;; @todo - Can we make this ECB friendly?
  84. ;;; Code:
  85. (require 'eieio)
  86. (require 'eieio-custom)
  87. (require 'speedbar)
  88. ;;; Support a way of adding generic object based modes into speedbar.
  89. ;;
  90. (defun eieio-speedbar-make-map ()
  91. "Make the generic object based speedbar keymap."
  92. (let ((map (speedbar-make-specialized-keymap)))
  93. ;; General viewing things
  94. (define-key map "\C-m" 'speedbar-edit-line)
  95. (define-key map "+" 'speedbar-expand-line)
  96. (define-key map "=" 'speedbar-expand-line)
  97. (define-key map "-" 'speedbar-contract-line)
  98. ;; Some object based things
  99. (define-key map "C" 'eieio-speedbar-customize-line)
  100. map))
  101. (defvar eieio-speedbar-key-map (eieio-speedbar-make-map)
  102. "A generic object based speedbar display keymap.")
  103. (defvar eieio-speedbar-menu
  104. '([ "Edit Object/Field" speedbar-edit-line t]
  105. [ "Expand Object" speedbar-expand-line
  106. (save-excursion (beginning-of-line)
  107. (looking-at "[0-9]+: *.\\+. "))]
  108. [ "Contract Object" speedbar-contract-line
  109. (save-excursion (beginning-of-line)
  110. (looking-at "[0-9]+: *.-. "))]
  111. "---"
  112. [ "Customize Object" eieio-speedbar-customize-line
  113. (eieio-object-p (speedbar-line-token)) ]
  114. )
  115. "Menu part in easymenu format used in speedbar while browsing objects.")
  116. ;; Note to self: Fix this silly thing!
  117. (defalias 'eieio-speedbar-customize-line 'speedbar-edit-line)
  118. (defun eieio-speedbar-create (map-fn map-var menu-var modename fetcher)
  119. "Create a speedbar mode for displaying an object hierarchy.
  120. MAP-FN is the keymap generator function used for extra keys.
  121. MAP-VAR is the keymap variable used.
  122. MENU-VAR is the symbol containing an easymenu compatible menu part to use.
  123. MODENAME is a string used to identify this browser mode.
  124. FETCHER is a generic function used to fetch the base object list used when
  125. creating the speedbar display."
  126. (if (not (featurep 'speedbar))
  127. (add-hook 'speedbar-load-hook
  128. (list 'lambda nil
  129. (list 'eieio-speedbar-create-engine
  130. map-fn map-var menu-var modename fetcher)))
  131. (eieio-speedbar-create-engine map-fn map-var menu-var modename fetcher)))
  132. (defun eieio-speedbar-create-engine (map-fn map-var menu-var modename fetcher)
  133. "Create a speedbar mode for displaying an object hierarchy.
  134. Called from `eieio-speedbar-create', or the speedbar load-hook.
  135. MAP-FN, MAP-VAR, MENU-VAR, MODENAME, and FETCHER are the same as in
  136. `eieio-speedbar-create'."
  137. ;; make sure the keymap exists
  138. (funcall map-fn)
  139. ;; Add to the expansion list.
  140. (speedbar-add-expansion-list
  141. (list modename
  142. menu-var
  143. map-var
  144. (list 'lambda '(dir depth)
  145. (list 'eieio-speedbar-buttons 'dir 'depth
  146. (list 'quote fetcher)))))
  147. ;; Set the special functions.
  148. (speedbar-add-mode-functions-list
  149. (list modename
  150. '(speedbar-item-info . eieio-speedbar-item-info)
  151. '(speedbar-line-directory . eieio-speedbar-line-path))))
  152. (defun eieio-speedbar-buttons (dir-or-object depth fetcher)
  153. "Create buttons for the speedbar display.
  154. Start in directory DIR-OR-OBJECT. If it is an object, just display that
  155. object's subelements.
  156. Argument DEPTH specifies how far down we have already been displayed.
  157. If it is a directory, use FETCHER to fetch all objects associated with
  158. that path."
  159. (let ((objlst (cond ((eieio-object-p dir-or-object)
  160. (list dir-or-object))
  161. ((stringp dir-or-object)
  162. (funcall fetcher dir-or-object))
  163. (t dir-or-object))))
  164. (if (not objlst)
  165. (speedbar-make-tag-line nil nil nil nil "Empty display" nil nil nil
  166. depth)
  167. ;; Dump all objects into speedbar
  168. (while objlst
  169. (eieio-speedbar-make-tag-line (car objlst) depth)
  170. (setq objlst (cdr objlst))))))
  171. ;;; DEFAULT SUPERCLASS baseline methods
  172. ;;
  173. ;; First, define methods onto the superclass so all classes
  174. ;; will have some minor support.
  175. (defmethod eieio-speedbar-description ((object eieio-default-superclass))
  176. "Return a string describing OBJECT."
  177. (object-name-string object))
  178. (defmethod eieio-speedbar-derive-line-path ((object eieio-default-superclass))
  179. "Return the path which OBJECT has something to do with."
  180. nil)
  181. (defmethod eieio-speedbar-object-buttonname ((object eieio-default-superclass))
  182. "Return a string to use as a speedbar button for OBJECT."
  183. (object-name-string object))
  184. (defmethod eieio-speedbar-make-tag-line ((object eieio-default-superclass)
  185. depth)
  186. "Insert a tag line into speedbar at point for OBJECT.
  187. By default, all objects appear as simple TAGS with no need to inherit from
  188. the special `eieio-speedbar' classes. Child classes should redefine this
  189. method to create more accurate tag lines.
  190. Argument DEPTH is the depth at which the tag line is inserted."
  191. (speedbar-make-tag-line nil nil nil nil
  192. (eieio-speedbar-object-buttonname object)
  193. 'eieio-speedbar-object-click
  194. object
  195. 'speedbar-tag-face
  196. depth))
  197. (defmethod eieio-speedbar-handle-click ((object eieio-default-superclass))
  198. "Handle a click action on OBJECT in speedbar.
  199. Any object can be represented as a tag in SPEEDBAR without special
  200. attributes. These default objects will be pulled up in a custom
  201. object edit buffer doing an in-place edit.
  202. If your object represents some other item, override this method
  203. and take the appropriate action."
  204. (require 'eieio-custom)
  205. (speedbar-with-attached-buffer
  206. (eieio-customize-object object))
  207. (speedbar-maybee-jump-to-attached-frame))
  208. ;;; Class definitions
  209. ;;
  210. ;; Now define a special speedbar class with some
  211. ;; variables with :allocation class which can be attached into
  212. ;; object hierarchies.
  213. ;;
  214. ;; These more complex types are for objects which wish to display
  215. ;; lists of children buttons.
  216. (defclass eieio-speedbar nil
  217. ((buttontype :initform nil
  218. :type symbol
  219. :documentation
  220. "The type of expansion button used for objects of this class.
  221. Possible values are those symbols supported by the `exp-button-type' argument
  222. to `speedbar-make-tag-line'."
  223. :allocation :class)
  224. (buttonface :initform speedbar-tag-face
  225. :type (or symbol face)
  226. :documentation
  227. "The face used on the textual part of the button for this class.
  228. See `speedbar-make-tag-line' for details."
  229. :allocation :class)
  230. (expanded :initform nil
  231. :type boolean
  232. :documentation
  233. "State of an object being expanded in speedbar.")
  234. )
  235. "Class which provides basic speedbar support for child classes.
  236. Add one of the child classes to this class to the parent list of a class."
  237. :method-invocation-order :depth-first
  238. :abstract t)
  239. (defclass eieio-speedbar-directory-button (eieio-speedbar)
  240. ((buttontype :initform angle)
  241. (buttonface :initform speedbar-directory-face))
  242. "Class providing support for objects which behave like a directory."
  243. :method-invocation-order :depth-first
  244. :abstract t)
  245. (defclass eieio-speedbar-file-button (eieio-speedbar)
  246. ((buttontype :initform bracket)
  247. (buttonface :initform speedbar-file-face))
  248. "Class providing support for objects which behave like a file."
  249. :method-invocation-order :depth-first
  250. :abstract t)
  251. ;;; Methods to eieio-speedbar-* which do not need to be overridden
  252. ;;
  253. (defmethod eieio-speedbar-make-tag-line ((object eieio-speedbar)
  254. depth)
  255. "Insert a tag line into speedbar at point for OBJECT.
  256. All objects a child of symbol `eieio-speedbar' can be created from
  257. this method. Override this if you need non-traditional tag lines.
  258. Argument DEPTH is the depth at which the tag line is inserted."
  259. (let ((children (eieio-speedbar-object-children object))
  260. (exp (oref object expanded)))
  261. (if (not children)
  262. (if (eq (oref object buttontype) 'expandtag)
  263. (speedbar-make-tag-line 'statictag
  264. ? nil nil
  265. (eieio-speedbar-object-buttonname object)
  266. 'eieio-speedbar-object-click
  267. object
  268. (oref object buttonface)
  269. depth)
  270. (speedbar-make-tag-line (oref object buttontype)
  271. ? nil nil
  272. (eieio-speedbar-object-buttonname object)
  273. 'eieio-speedbar-object-click
  274. object
  275. (oref object buttonface)
  276. depth))
  277. (speedbar-make-tag-line (oref object buttontype)
  278. (if exp ?- ?+)
  279. 'eieio-speedbar-object-expand
  280. object
  281. (eieio-speedbar-object-buttonname object)
  282. 'eieio-speedbar-object-click
  283. object
  284. (oref object buttonface)
  285. depth)
  286. (if exp
  287. (eieio-speedbar-expand object (1+ depth))))))
  288. (defmethod eieio-speedbar-child-make-tag-lines ((object eieio-speedbar) depth)
  289. "Base method for creating tag lines for non-object children."
  290. (error "You must implement `eieio-speedbar-child-make-tag-lines' for %s"
  291. (object-name object)))
  292. (defmethod eieio-speedbar-expand ((object eieio-speedbar) depth)
  293. "Expand OBJECT at indentation DEPTH.
  294. Inserts a list of new tag lines representing expanded elements within
  295. OBJECT."
  296. (let ((children (eieio-speedbar-object-children object)))
  297. (cond ((eieio-object-p (car children))
  298. (mapcar (lambda (car)
  299. (eieio-speedbar-make-tag-line car depth))
  300. children))
  301. (children (eieio-speedbar-child-make-tag-lines object depth)))))
  302. ;;; Speedbar specific function callbacks.
  303. ;;
  304. (defun eieio-speedbar-object-click (text token indent)
  305. "Handle a user click on TEXT representing object TOKEN.
  306. The object is at indentation level INDENT."
  307. (eieio-speedbar-handle-click token))
  308. (defun eieio-speedbar-object-expand (text token indent)
  309. "Expand object represented by TEXT.
  310. TOKEN is the object. INDENT is the current indentation level."
  311. (cond ((string-match "+" text) ;we have to expand this file
  312. (speedbar-change-expand-button-char ?-)
  313. (oset token expanded t)
  314. (speedbar-with-writable
  315. (save-excursion
  316. (end-of-line) (forward-char 1)
  317. (eieio-speedbar-expand token (1+ indent)))))
  318. ((string-match "-" text) ;we have to contract this node
  319. (speedbar-change-expand-button-char ?+)
  320. (oset token expanded nil)
  321. (speedbar-delete-subblock indent))
  322. (t (error "Ooops... not sure what to do")))
  323. (speedbar-center-buffer-smartly))
  324. (defmethod eieio-speedbar-child-description ((obj eieio-speedbar))
  325. "Return a description for a child of OBJ which is not an object."
  326. (error "You must implement `eieio-speedbar-child-description' for %s"
  327. (object-name obj)))
  328. (defun eieio-speedbar-item-info ()
  329. "Display info for the current line when in EDE display mode."
  330. ;; Switch across the types of the tokens.
  331. (let ((tok (speedbar-line-token)))
  332. (cond ((eieio-object-p tok)
  333. (message (eieio-speedbar-description tok)))
  334. (t
  335. (let ((no (eieio-speedbar-find-nearest-object)))
  336. (if no
  337. (eieio-speedbar-child-description no)))))))
  338. (defun eieio-speedbar-find-nearest-object (&optional depth)
  339. "Search backwards to the first line associated with an object.
  340. Optional argument DEPTH is the current depth of the search."
  341. (save-excursion
  342. (if (not depth)
  343. (progn
  344. (beginning-of-line)
  345. (when (looking-at "^\\([0-9]+\\):")
  346. (setq depth (string-to-number (match-string 1))))))
  347. (when depth
  348. (while (and (not (eieio-object-p (speedbar-line-token)))
  349. (> depth 0))
  350. (setq depth (1- depth))
  351. (re-search-backward (format "^%d:" depth) nil t))
  352. (speedbar-line-token))))
  353. (defun eieio-speedbar-line-path (&optional depth)
  354. "If applicable, return the path to the file the cursor is on.
  355. Optional DEPTH is the depth we start at."
  356. (save-match-data
  357. (if (not depth)
  358. (progn
  359. (beginning-of-line)
  360. (looking-at "^\\([0-9]+\\):")
  361. (setq depth (string-to-number (match-string 1)))))
  362. ;; This whole function is presently bogus. Make it better later.
  363. (let ((tok (eieio-speedbar-find-nearest-object depth)))
  364. (if (eieio-object-p tok)
  365. (eieio-speedbar-derive-line-path tok)
  366. default-directory))))
  367. ;;; Methods to the eieio-speedbar-* classes which need to be overridden.
  368. ;;
  369. (defmethod eieio-speedbar-object-children ((object eieio-speedbar))
  370. "Return a list of children to be displayed in speedbar.
  371. If the return value is a list of OBJECTs, then those objects are
  372. queried for details. If the return list is made of strings,
  373. then this object will be queried for the details needed
  374. to create a speedbar button."
  375. nil)
  376. (provide 'eieio-speedbar)
  377. ;;; eieio-speedbar.el ends here