1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2017 Free Software
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This contains support functions to eieio. These functions contain
28 ;; some small class browser and class printing functions.
37 (defun eieio-browse (&optional root-class
)
38 "Create an object browser window to show all objects.
39 If optional ROOT-CLASS, then start with that, otherwise start with
40 variable `eieio-default-superclass'."
41 (interactive (if current-prefix-arg
42 (list (read (completing-read "Class: "
43 (eieio-build-class-alist)
46 (if (not root-class
) (setq root-class
'eieio-default-superclass
))
47 (cl-check-type root-class class
)
48 (display-buffer (get-buffer-create "*EIEIO OBJECT BROWSE*") t
)
49 (with-current-buffer (get-buffer "*EIEIO OBJECT BROWSE*")
52 (eieio-browse-tree root-class
"" "")
55 (defun eieio-browse-tree (this-root prefix ch-prefix
)
56 "Recursively draw the children of the given class on the screen.
57 Argument THIS-ROOT is the local root of the tree.
58 Argument PREFIX is the character prefix to use.
59 Argument CH-PREFIX is another character prefix to display."
60 (cl-check-type this-root class
)
61 (let ((myname (symbol-name this-root
))
62 (chl (eieio--class-children (cl--find-class this-root
)))
63 (fprefix (concat ch-prefix
" +--"))
64 (mprefix (concat ch-prefix
" | "))
65 (lprefix (concat ch-prefix
" ")))
66 (insert prefix myname
"\n")
68 (eieio-browse-tree (car chl
) fprefix mprefix
)
71 (eieio-browse-tree (car chl
) fprefix lprefix
))
74 ;;; CLASS COMPLETION / DOCUMENTATION
76 ;; Called via help-fns-describe-function-functions.
77 (declare-function help-fns-short-filename
"help-fns" (filename))
80 (define-obsolete-function-alias 'eieio-help-class
'cl--describe-class
"25.1")
82 (defun eieio-build-class-alist (&optional class instantiable-only buildlist
)
83 "Return an alist of all currently active classes for completion purposes.
84 Optional argument CLASS is the class to start with.
85 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
86 are not abstract, otherwise allow all classes.
87 Optional argument BUILDLIST is more list to attach and is used internally."
88 (let* ((cc (or class
'eieio-default-superclass
))
89 (sublst (eieio--class-children (cl--find-class cc
))))
90 (unless (assoc (symbol-name cc
) buildlist
)
91 (when (or (not instantiable-only
) (not (class-abstract-p cc
)))
92 ;; FIXME: Completion tables don't need alists, and ede/generic.el needs
93 ;; the symbols rather than their names.
94 (setq buildlist
(cons (cons (symbol-name cc
) 1) buildlist
))))
96 (setq buildlist
(eieio-build-class-alist
97 elem instantiable-only buildlist
)))
100 (defvar eieio-read-class nil
101 "History of the function `eieio-read-class' prompt.")
103 (defun eieio-read-class (prompt &optional histvar instantiable-only
)
104 "Return a class chosen by the user using PROMPT.
105 Optional argument HISTVAR is a variable to use as history.
106 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
108 (intern (completing-read prompt
(eieio-build-class-alist nil instantiable-only
)
110 (or histvar
'eieio-read-class
))))
112 (defun eieio-read-subclass (prompt class
&optional histvar instantiable-only
)
113 "Return a class chosen by the user using PROMPT.
114 CLASS is the base class, and completion occurs across all subclasses.
115 Optional argument HISTVAR is a variable to use as history.
116 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
118 (intern (completing-read prompt
119 (eieio-build-class-alist class instantiable-only
)
121 (or histvar
'eieio-read-class
))))
123 ;;; METHOD COMPLETION / DOC
127 (defun eieio-help-constructor (ctr)
128 "Describe CTR if it is a class constructor."
131 (let ((location (find-lisp-object-file-name ctr
'define-type
))
132 (def (symbol-function ctr
)))
133 (goto-char (point-min))
135 (insert (format " is an %s object constructor function"
139 (when (and (autoloadp def
)
142 (find-lisp-object-file-name ctr def
)))
144 (insert (substitute-command-keys " in `"))
145 (help-insert-xref-button
146 (help-fns-short-filename location
)
147 'cl-type-definition ctr location
'define-type
)
148 (insert (substitute-command-keys "'")))
149 (insert ".\nCreates an object of class " (symbol-name ctr
) ".")
150 (goto-char (point-max))
152 (insert "\n\n[Class description not available until class definition is loaded.]\n")
154 (insert (propertize "\n\nClass description:\n" 'face
'bold
))
155 (eieio-help-class ctr
))
161 ;; Dump out statistics about all the active methods in a session.
162 (defun eieio-display-method-list ()
163 "Display a list of all the methods and what features are used."
165 (let* ((meth1 (cl-generic-all-functions))
166 (meth (sort meth1
(lambda (a b
)
167 (string< (symbol-name a
)
169 (buff (get-buffer-create "*EIEIO Method List*"))
171 (standard-output buff
)
172 (slots '(method-static
176 method-generic-before
177 method-generic-primary
178 method-generic-after
))
186 (idxarray (make-vector (length slots
) 0))
190 (switch-to-buffer-other-window buff
)
196 (princ "Method Name")
198 (princ "--------------------------------------------------------------------")
201 (let ((mtree (get M
'eieio-method-tree
))
205 (let ((num (length (aref mtree
(symbol-value S
)))))
206 (aset idxarray
(symbol-value S
)
207 (+ num
(aref idxarray
(symbol-value S
))))
211 (if (eq S
'method-primary
)
215 ;; Is this a primary-only impl method?
216 (when (and P
(not !P
))
217 (setq primaryonly
(1+ primaryonly
))
219 (setq oneprimary
(1+ oneprimary
))
225 (setq methidx
(1+ methidx
))
228 (princ "--------------------------------------------------------------------")
231 (prin1 (aref idxarray
(symbol-value S
)))
235 (princ " Total symbols")
243 (princ "Methods Primary Only: ")
246 (princ (format "%d" (floor (* 100.0 primaryonly
) methidx
)))
247 (princ "% of total methods")
249 (princ "Only One Primary Impl: ")
252 (princ (format "%d" (floor (* 100.0 oneprimary
) primaryonly
)))
253 (princ "% of total primary methods")
260 (defvar eieio-class-speedbar-key-map nil
261 "Keymap used when working with a project in speedbar.")
263 (defun eieio-class-speedbar-make-map ()
264 "Make a keymap for EIEIO under speedbar."
265 (setq eieio-class-speedbar-key-map
(speedbar-make-specialized-keymap))
267 ;; General viewing stuff
268 (define-key eieio-class-speedbar-key-map
"\C-m" 'speedbar-edit-line
)
269 (define-key eieio-class-speedbar-key-map
"+" 'speedbar-expand-line
)
270 (define-key eieio-class-speedbar-key-map
"-" 'speedbar-contract-line
)
273 (if eieio-class-speedbar-key-map
275 (if (not (featurep 'speedbar
))
276 (add-hook 'speedbar-load-hook
(lambda ()
277 (eieio-class-speedbar-make-map)
278 (speedbar-add-expansion-list
280 eieio-class-speedbar-menu
281 eieio-class-speedbar-key-map
282 eieio-class-speedbar
))))
283 (eieio-class-speedbar-make-map)
284 (speedbar-add-expansion-list '("EIEIO"
285 eieio-class-speedbar-menu
286 eieio-class-speedbar-key-map
287 eieio-class-speedbar
))))
289 (defvar eieio-class-speedbar-menu
291 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
293 (defun eieio-class-speedbar (_dir-or-object _depth
)
294 "Create buttons in speedbar that represents the current project.
295 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
296 current expansion depth."
297 (when (eq (point-min) (point-max))
298 ;; This function is only called once, to start the whole deal.
299 ;; Create and expand the default object.
300 (eieio-class-button 'eieio-default-superclass
0)
302 (speedbar-expand-line)))
304 (defun eieio-class-button (class depth
)
305 "Draw a speedbar button at the current point for CLASS at DEPTH."
306 (cl-check-type class class
)
307 (let ((subclasses (eieio--class-children (cl--find-class class
))))
309 (speedbar-make-tag-line 'angle ?
+
313 'eieio-describe-class-sb
315 'speedbar-directory-face
317 (speedbar-make-tag-line 'angle ? nil nil
319 'eieio-describe-class-sb
321 'speedbar-directory-face
324 (defun eieio-sb-expand (text class indent
)
325 "For button TEXT, expand CLASS at the current location.
326 Argument INDENT is the depth of indentation."
327 (cond ((string-match "+" text
) ;we have to expand this file
328 (speedbar-change-expand-button-char ?-
)
329 (speedbar-with-writable
331 (end-of-line) (forward-char 1)
332 (let ((subclasses (eieio--class-children (cl--find-class class
))))
334 (eieio-class-button (car subclasses
) (1+ indent
))
335 (setq subclasses
(cdr subclasses
)))))))
336 ((string-match "-" text
) ;we have to contract this node
337 (speedbar-change-expand-button-char ?
+)
338 (speedbar-delete-subblock indent
))
339 (t (error "Ooops... not sure what to do")))
340 (speedbar-center-buffer-smartly))
342 (defun eieio-describe-class-sb (_text token _indent
)
343 "Describe the class TEXT in TOKEN.
344 INDENT is the current indentation level."
345 (dframe-with-attached-buffer
346 (describe-function token
))
347 (dframe-maybee-jump-to-attached-frame))
352 ;; generated-autoload-file: "eieio-loaddefs.el"
355 ;;; eieio-opt.el ends here