eieio-opt.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
  2. ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2017 Free Software
  3. ;; Foundation, Inc.
  4. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  5. ;; Keywords: OO, lisp
  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 contains support functions to eieio. These functions contain
  21. ;; some small class browser and class printing functions.
  22. ;;
  23. (require 'eieio)
  24. (require 'find-func)
  25. (require 'speedbar)
  26. ;;; Code:
  27. ;;;###autoload
  28. (defun eieio-browse (&optional root-class)
  29. "Create an object browser window to show all objects.
  30. If optional ROOT-CLASS, then start with that, otherwise start with
  31. variable `eieio-default-superclass'."
  32. (interactive (if current-prefix-arg
  33. (list (read (completing-read "Class: "
  34. (eieio-build-class-alist)
  35. nil t)))
  36. nil))
  37. (if (not root-class) (setq root-class 'eieio-default-superclass))
  38. (cl-check-type root-class class)
  39. (display-buffer (get-buffer-create "*EIEIO OBJECT BROWSE*") t)
  40. (with-current-buffer (get-buffer "*EIEIO OBJECT BROWSE*")
  41. (erase-buffer)
  42. (goto-char 0)
  43. (eieio-browse-tree root-class "" "")
  44. ))
  45. (defun eieio-browse-tree (this-root prefix ch-prefix)
  46. "Recursively draw the children of the given class on the screen.
  47. Argument THIS-ROOT is the local root of the tree.
  48. Argument PREFIX is the character prefix to use.
  49. Argument CH-PREFIX is another character prefix to display."
  50. (cl-check-type this-root class)
  51. (let ((myname (symbol-name this-root))
  52. (chl (eieio--class-children (cl--find-class this-root)))
  53. (fprefix (concat ch-prefix " +--"))
  54. (mprefix (concat ch-prefix " | "))
  55. (lprefix (concat ch-prefix " ")))
  56. (insert prefix myname "\n")
  57. (while (cdr chl)
  58. (eieio-browse-tree (car chl) fprefix mprefix)
  59. (setq chl (cdr chl)))
  60. (if chl
  61. (eieio-browse-tree (car chl) fprefix lprefix))
  62. ))
  63. ;;; CLASS COMPLETION / DOCUMENTATION
  64. ;; Called via help-fns-describe-function-functions.
  65. (declare-function help-fns-short-filename "help-fns" (filename))
  66. ;;;###autoload
  67. (define-obsolete-function-alias 'eieio-help-class 'cl--describe-class "25.1")
  68. (defun eieio-build-class-alist (&optional class instantiable-only buildlist)
  69. "Return an alist of all currently active classes for completion purposes.
  70. Optional argument CLASS is the class to start with.
  71. If INSTANTIABLE-ONLY is non nil, only allow names of classes which
  72. are not abstract, otherwise allow all classes.
  73. Optional argument BUILDLIST is more list to attach and is used internally."
  74. (let* ((cc (or class 'eieio-default-superclass))
  75. (sublst (eieio--class-children (cl--find-class cc))))
  76. (unless (assoc (symbol-name cc) buildlist)
  77. (when (or (not instantiable-only) (not (class-abstract-p cc)))
  78. ;; FIXME: Completion tables don't need alists, and ede/generic.el needs
  79. ;; the symbols rather than their names.
  80. (setq buildlist (cons (cons (symbol-name cc) 1) buildlist))))
  81. (dolist (elem sublst)
  82. (setq buildlist (eieio-build-class-alist
  83. elem instantiable-only buildlist)))
  84. buildlist))
  85. (defvar eieio-read-class nil
  86. "History of the function `eieio-read-class' prompt.")
  87. (defun eieio-read-class (prompt &optional histvar instantiable-only)
  88. "Return a class chosen by the user using PROMPT.
  89. Optional argument HISTVAR is a variable to use as history.
  90. If INSTANTIABLE-ONLY is non nil, only allow names of classes which
  91. are not abstract."
  92. (intern (completing-read prompt (eieio-build-class-alist nil instantiable-only)
  93. nil t nil
  94. (or histvar 'eieio-read-class))))
  95. (defun eieio-read-subclass (prompt class &optional histvar instantiable-only)
  96. "Return a class chosen by the user using PROMPT.
  97. CLASS is the base class, and completion occurs across all subclasses.
  98. Optional argument HISTVAR is a variable to use as history.
  99. If INSTANTIABLE-ONLY is non nil, only allow names of classes which
  100. are not abstract."
  101. (intern (completing-read prompt
  102. (eieio-build-class-alist class instantiable-only)
  103. nil t nil
  104. (or histvar 'eieio-read-class))))
  105. ;;; METHOD COMPLETION / DOC
  106. ;;;###autoload
  107. (defun eieio-help-constructor (ctr)
  108. "Describe CTR if it is a class constructor."
  109. (when (class-p ctr)
  110. (erase-buffer)
  111. (let ((location (find-lisp-object-file-name ctr 'define-type))
  112. (def (symbol-function ctr)))
  113. (goto-char (point-min))
  114. (prin1 ctr)
  115. (insert (format " is an %s object constructor function"
  116. (if (autoloadp def)
  117. "autoloaded"
  118. "")))
  119. (when (and (autoloadp def)
  120. (null location))
  121. (setq location
  122. (find-lisp-object-file-name ctr def)))
  123. (when location
  124. (insert (substitute-command-keys " in `"))
  125. (help-insert-xref-button
  126. (help-fns-short-filename location)
  127. 'cl-type-definition ctr location 'define-type)
  128. (insert (substitute-command-keys "'")))
  129. (insert ".\nCreates an object of class " (symbol-name ctr) ".")
  130. (goto-char (point-max))
  131. (if (autoloadp def)
  132. (insert "\n\n[Class description not available until class definition is loaded.]\n")
  133. (save-excursion
  134. (insert (propertize "\n\nClass description:\n" 'face 'bold))
  135. (eieio-help-class ctr))
  136. ))))
  137. ;;; METHOD STATS
  138. ;;
  139. ;; Dump out statistics about all the active methods in a session.
  140. (defun eieio-display-method-list ()
  141. "Display a list of all the methods and what features are used."
  142. (interactive)
  143. (let* ((meth1 (cl-generic-all-functions))
  144. (meth (sort meth1 (lambda (a b)
  145. (string< (symbol-name a)
  146. (symbol-name b)))))
  147. (buff (get-buffer-create "*EIEIO Method List*"))
  148. (methidx 0)
  149. (standard-output buff)
  150. (slots '(method-static
  151. method-before
  152. method-primary
  153. method-after
  154. method-generic-before
  155. method-generic-primary
  156. method-generic-after))
  157. (slotn '("static"
  158. "before"
  159. "primary"
  160. "after"
  161. "G bef"
  162. "G prim"
  163. "G aft"))
  164. (idxarray (make-vector (length slots) 0))
  165. (primaryonly 0)
  166. (oneprimary 0)
  167. )
  168. (switch-to-buffer-other-window buff)
  169. (erase-buffer)
  170. (dolist (S slotn)
  171. (princ S)
  172. (princ "\t")
  173. )
  174. (princ "Method Name")
  175. (terpri)
  176. (princ "--------------------------------------------------------------------")
  177. (terpri)
  178. (dolist (M meth)
  179. (let ((mtree (get M 'eieio-method-tree))
  180. (P nil) (numP)
  181. (!P nil))
  182. (dolist (S slots)
  183. (let ((num (length (aref mtree (symbol-value S)))))
  184. (aset idxarray (symbol-value S)
  185. (+ num (aref idxarray (symbol-value S))))
  186. (prin1 num)
  187. (princ "\t")
  188. (when (< 0 num)
  189. (if (eq S 'method-primary)
  190. (setq P t numP num)
  191. (setq !P t)))
  192. ))
  193. ;; Is this a primary-only impl method?
  194. (when (and P (not !P))
  195. (setq primaryonly (1+ primaryonly))
  196. (when (= numP 1)
  197. (setq oneprimary (1+ oneprimary))
  198. (princ "*"))
  199. (princ "* ")
  200. )
  201. (prin1 M)
  202. (terpri)
  203. (setq methidx (1+ methidx))
  204. )
  205. )
  206. (princ "--------------------------------------------------------------------")
  207. (terpri)
  208. (dolist (S slots)
  209. (prin1 (aref idxarray (symbol-value S)))
  210. (princ "\t")
  211. )
  212. (prin1 methidx)
  213. (princ " Total symbols")
  214. (terpri)
  215. (dolist (S slotn)
  216. (princ S)
  217. (princ "\t")
  218. )
  219. (terpri)
  220. (terpri)
  221. (princ "Methods Primary Only: ")
  222. (prin1 primaryonly)
  223. (princ "\t")
  224. (princ (format "%d" (floor (* 100.0 primaryonly) methidx)))
  225. (princ "% of total methods")
  226. (terpri)
  227. (princ "Only One Primary Impl: ")
  228. (prin1 oneprimary)
  229. (princ "\t")
  230. (princ (format "%d" (floor (* 100.0 oneprimary) primaryonly)))
  231. (princ "% of total primary methods")
  232. (terpri)
  233. ))
  234. ;;; SPEEDBAR SUPPORT
  235. ;;
  236. (defvar eieio-class-speedbar-key-map nil
  237. "Keymap used when working with a project in speedbar.")
  238. (defun eieio-class-speedbar-make-map ()
  239. "Make a keymap for EIEIO under speedbar."
  240. (setq eieio-class-speedbar-key-map (speedbar-make-specialized-keymap))
  241. ;; General viewing stuff
  242. (define-key eieio-class-speedbar-key-map "\C-m" 'speedbar-edit-line)
  243. (define-key eieio-class-speedbar-key-map "+" 'speedbar-expand-line)
  244. (define-key eieio-class-speedbar-key-map "-" 'speedbar-contract-line)
  245. )
  246. (if eieio-class-speedbar-key-map
  247. nil
  248. (if (not (featurep 'speedbar))
  249. (add-hook 'speedbar-load-hook (lambda ()
  250. (eieio-class-speedbar-make-map)
  251. (speedbar-add-expansion-list
  252. '("EIEIO"
  253. eieio-class-speedbar-menu
  254. eieio-class-speedbar-key-map
  255. eieio-class-speedbar))))
  256. (eieio-class-speedbar-make-map)
  257. (speedbar-add-expansion-list '("EIEIO"
  258. eieio-class-speedbar-menu
  259. eieio-class-speedbar-key-map
  260. eieio-class-speedbar))))
  261. (defvar eieio-class-speedbar-menu
  262. ()
  263. "Menu part in easymenu format used in speedbar while in `eieio' mode.")
  264. (defun eieio-class-speedbar (_dir-or-object _depth)
  265. "Create buttons in speedbar that represents the current project.
  266. DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
  267. current expansion depth."
  268. (when (eq (point-min) (point-max))
  269. ;; This function is only called once, to start the whole deal.
  270. ;; Create and expand the default object.
  271. (eieio-class-button 'eieio-default-superclass 0)
  272. (forward-line -1)
  273. (speedbar-expand-line)))
  274. (defun eieio-class-button (class depth)
  275. "Draw a speedbar button at the current point for CLASS at DEPTH."
  276. (cl-check-type class class)
  277. (let ((subclasses (eieio--class-children (cl--find-class class))))
  278. (if subclasses
  279. (speedbar-make-tag-line 'angle ?+
  280. 'eieio-sb-expand
  281. class
  282. (symbol-name class)
  283. 'eieio-describe-class-sb
  284. class
  285. 'speedbar-directory-face
  286. depth)
  287. (speedbar-make-tag-line 'angle ? nil nil
  288. (symbol-name class)
  289. 'eieio-describe-class-sb
  290. class
  291. 'speedbar-directory-face
  292. depth))))
  293. (defun eieio-sb-expand (text class indent)
  294. "For button TEXT, expand CLASS at the current location.
  295. Argument INDENT is the depth of indentation."
  296. (cond ((string-match "+" text) ;we have to expand this file
  297. (speedbar-change-expand-button-char ?-)
  298. (speedbar-with-writable
  299. (save-excursion
  300. (end-of-line) (forward-char 1)
  301. (let ((subclasses (eieio--class-children (cl--find-class class))))
  302. (while subclasses
  303. (eieio-class-button (car subclasses) (1+ indent))
  304. (setq subclasses (cdr subclasses)))))))
  305. ((string-match "-" text) ;we have to contract this node
  306. (speedbar-change-expand-button-char ?+)
  307. (speedbar-delete-subblock indent))
  308. (t (error "Ooops... not sure what to do")))
  309. (speedbar-center-buffer-smartly))
  310. (defun eieio-describe-class-sb (_text token _indent)
  311. "Describe the class TEXT in TOKEN.
  312. INDENT is the current indentation level."
  313. (dframe-with-attached-buffer
  314. (describe-function token))
  315. (dframe-maybee-jump-to-attached-frame))
  316. (provide 'eieio-opt)
  317. ;; Local variables:
  318. ;; generated-autoload-file: "eieio-loaddefs.el"
  319. ;; End:
  320. ;;; eieio-opt.el ends here