eieio-speedbar.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. ;;; eieio-speedbar.el -- Classes for managing speedbar displays. -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1999-2002, 2005, 2007-2017 Free Software Foundation,
  3. ;; Inc.
  4. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  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 with no class defined. These will work as if
  174. ;; on the default superclass. Specifying no class will allow these to be used
  175. ;; when no other methods are found, allowing multiple inheritance to work
  176. ;; reliably with eieio-speedbar.
  177. (cl-defmethod eieio-speedbar-description (object)
  178. "Return a string describing OBJECT."
  179. (eieio-object-name-string object))
  180. (cl-defmethod eieio-speedbar-derive-line-path (_object)
  181. "Return the path which OBJECT has something to do with."
  182. nil)
  183. (cl-defmethod eieio-speedbar-object-buttonname (object)
  184. "Return a string to use as a speedbar button for OBJECT."
  185. (eieio-object-name-string object))
  186. (cl-defmethod eieio-speedbar-make-tag-line (object depth)
  187. "Insert a tag line into speedbar at point for OBJECT.
  188. By default, all objects appear as simple TAGS with no need to inherit from
  189. the special `eieio-speedbar' classes. Child classes should redefine this
  190. method to create more accurate tag lines.
  191. Argument DEPTH is the depth at which the tag line is inserted."
  192. (speedbar-make-tag-line nil nil nil nil
  193. (eieio-speedbar-object-buttonname object)
  194. 'eieio-speedbar-object-click
  195. object
  196. 'speedbar-tag-face
  197. depth))
  198. (cl-defmethod eieio-speedbar-handle-click (object)
  199. "Handle a click action on OBJECT in speedbar.
  200. Any object can be represented as a tag in SPEEDBAR without special
  201. attributes. These default objects will be pulled up in a custom
  202. object edit buffer doing an in-place edit.
  203. If your object represents some other item, override this method
  204. and take the appropriate action."
  205. (require 'eieio-custom)
  206. (dframe-with-attached-buffer
  207. (eieio-customize-object object))
  208. (dframe-maybee-jump-to-attached-frame))
  209. ;;; Class definitions
  210. ;;
  211. ;; Now define a special speedbar class with some
  212. ;; variables with :allocation class which can be attached into
  213. ;; object hierarchies.
  214. ;;
  215. ;; These more complex types are for objects which wish to display
  216. ;; lists of children buttons.
  217. (defclass eieio-speedbar nil
  218. ((buttontype :initform nil
  219. :type symbol
  220. :documentation
  221. "The type of expansion button used for objects of this class.
  222. Possible values are those symbols supported by the `exp-button-type' argument
  223. to `speedbar-make-tag-line'."
  224. :allocation :class)
  225. (buttonface :initform speedbar-tag-face
  226. :type (or symbol face)
  227. :documentation
  228. "The face used on the textual part of the button for this class.
  229. See `speedbar-make-tag-line' for details."
  230. :allocation :class)
  231. (expanded :initform nil
  232. :type boolean
  233. :documentation
  234. "State of an object being expanded in speedbar.")
  235. )
  236. "Class which provides basic speedbar support for child classes.
  237. Add one of the child classes to this class to the parent list of a class."
  238. :method-invocation-order :depth-first
  239. :abstract t)
  240. (defclass eieio-speedbar-directory-button (eieio-speedbar)
  241. ((buttontype :initform angle)
  242. (buttonface :initform speedbar-directory-face))
  243. "Class providing support for objects which behave like a directory."
  244. :method-invocation-order :depth-first
  245. :abstract t)
  246. (defclass eieio-speedbar-file-button (eieio-speedbar)
  247. ((buttontype :initform bracket)
  248. (buttonface :initform speedbar-file-face))
  249. "Class providing support for objects which behave like a file."
  250. :method-invocation-order :depth-first
  251. :abstract t)
  252. ;;; Methods to eieio-speedbar-* which do not need to be overridden
  253. ;;
  254. (cl-defmethod eieio-speedbar-make-tag-line ((object eieio-speedbar)
  255. depth)
  256. "Insert a tag line into speedbar at point for OBJECT.
  257. All objects a child of symbol `eieio-speedbar' can be created from
  258. this method. Override this if you need non-traditional tag lines.
  259. Argument DEPTH is the depth at which the tag line is inserted."
  260. (let ((children (eieio-speedbar-object-children object))
  261. (exp (oref object expanded)))
  262. (if (not children)
  263. (if (eq (oref object buttontype) 'expandtag)
  264. (speedbar-make-tag-line 'statictag
  265. ? nil nil
  266. (eieio-speedbar-object-buttonname object)
  267. 'eieio-speedbar-object-click
  268. object
  269. (oref object buttonface)
  270. depth)
  271. (speedbar-make-tag-line (oref object buttontype)
  272. ? nil nil
  273. (eieio-speedbar-object-buttonname object)
  274. 'eieio-speedbar-object-click
  275. object
  276. (oref object buttonface)
  277. depth))
  278. (speedbar-make-tag-line (oref object buttontype)
  279. (if exp ?- ?+)
  280. 'eieio-speedbar-object-expand
  281. object
  282. (eieio-speedbar-object-buttonname object)
  283. 'eieio-speedbar-object-click
  284. object
  285. (oref object buttonface)
  286. depth)
  287. (if exp
  288. (eieio-speedbar-expand object (1+ depth))))))
  289. (cl-defmethod eieio-speedbar-child-make-tag-lines ((object eieio-speedbar) _depth)
  290. "Base method for creating tag lines for non-object children."
  291. (error "You must implement `eieio-speedbar-child-make-tag-lines' for %s"
  292. (eieio-object-name object)))
  293. (cl-defmethod eieio-speedbar-expand ((object eieio-speedbar) depth)
  294. "Expand OBJECT at indentation DEPTH.
  295. Inserts a list of new tag lines representing expanded elements within
  296. OBJECT."
  297. (let ((children (eieio-speedbar-object-children object)))
  298. (cond ((eieio-object-p (car children))
  299. (mapcar (lambda (car)
  300. (eieio-speedbar-make-tag-line car depth))
  301. children))
  302. (children (eieio-speedbar-child-make-tag-lines object depth)))))
  303. ;;; Speedbar specific function callbacks.
  304. ;;
  305. (defun eieio-speedbar-object-click (_text token _indent)
  306. "Handle a user click on TEXT representing object TOKEN.
  307. The object is at indentation level INDENT."
  308. (eieio-speedbar-handle-click token))
  309. (defun eieio-speedbar-object-expand (text token indent)
  310. "Expand object represented by TEXT.
  311. TOKEN is the object. INDENT is the current indentation level."
  312. (cond ((string-match "+" text) ;we have to expand this file
  313. (speedbar-change-expand-button-char ?-)
  314. (oset token expanded t)
  315. (speedbar-with-writable
  316. (save-excursion
  317. (end-of-line) (forward-char 1)
  318. (eieio-speedbar-expand token (1+ indent)))))
  319. ((string-match "-" text) ;we have to contract this node
  320. (speedbar-change-expand-button-char ?+)
  321. (oset token expanded nil)
  322. (speedbar-delete-subblock indent))
  323. (t (error "Ooops... not sure what to do")))
  324. (speedbar-center-buffer-smartly))
  325. (cl-defmethod eieio-speedbar-child-description ((obj eieio-speedbar))
  326. "Return a description for a child of OBJ which is not an object."
  327. (error "You must implement `eieio-speedbar-child-description' for %s"
  328. (eieio-object-name obj)))
  329. (defun eieio-speedbar-item-info ()
  330. "Display info for the current line when in EDE display mode."
  331. ;; Switch across the types of the tokens.
  332. (let ((tok (speedbar-line-token)))
  333. (cond ((eieio-object-p tok)
  334. (message (eieio-speedbar-description tok)))
  335. (t
  336. (let ((no (eieio-speedbar-find-nearest-object)))
  337. (if no
  338. (eieio-speedbar-child-description no)))))))
  339. (defun eieio-speedbar-find-nearest-object (&optional depth)
  340. "Search backwards to the first line associated with an object.
  341. Optional argument DEPTH is the current depth of the search."
  342. (save-excursion
  343. (if (not depth)
  344. (progn
  345. (beginning-of-line)
  346. (when (looking-at "^\\([0-9]+\\):")
  347. (setq depth (string-to-number (match-string 1))))))
  348. (when depth
  349. (while (and (not (eieio-object-p (speedbar-line-token)))
  350. (> depth 0))
  351. (setq depth (1- depth))
  352. (re-search-backward (format "^%d:" depth) nil t))
  353. (speedbar-line-token))))
  354. (defun eieio-speedbar-line-path (&optional depth)
  355. "If applicable, return the path to the file the cursor is on.
  356. Optional DEPTH is the depth we start at."
  357. (save-match-data
  358. (if (not depth)
  359. (progn
  360. (beginning-of-line)
  361. (looking-at "^\\([0-9]+\\):")
  362. (setq depth (string-to-number (match-string 1)))))
  363. ;; This whole function is presently bogus. Make it better later.
  364. (let ((tok (eieio-speedbar-find-nearest-object depth)))
  365. (if (eieio-object-p tok)
  366. (eieio-speedbar-derive-line-path tok)
  367. default-directory))))
  368. ;;; Methods to the eieio-speedbar-* classes which need to be overridden.
  369. ;;
  370. (cl-defmethod eieio-speedbar-object-children ((_object eieio-speedbar))
  371. "Return a list of children to be displayed in speedbar.
  372. If the return value is a list of OBJECTs, then those objects are
  373. queried for details. If the return list is made of strings,
  374. then this object will be queried for the details needed
  375. to create a speedbar button."
  376. nil)
  377. (provide 'eieio-speedbar)
  378. ;;; eieio-speedbar.el ends here