1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2018 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 <https://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 ;; The `cl-type-definition' button type can't be autoloaded
146 ;; due to circularity during bootstrap (Bug#28899).
148 (help-insert-xref-button
149 (help-fns-short-filename location
)
150 'cl-type-definition ctr location
'define-type
)
151 (insert (substitute-command-keys "'")))
152 (insert ".\nCreates an object of class " (symbol-name ctr
) ".")
153 (goto-char (point-max))
155 (insert "\n\n[Class description not available until class definition is loaded.]\n")
157 (insert (propertize "\n\nClass description:\n" 'face
'bold
))
158 (eieio-help-class ctr
))
164 ;; Dump out statistics about all the active methods in a session.
165 (defun eieio-display-method-list ()
166 "Display a list of all the methods and what features are used."
168 (let* ((meth1 (cl-generic-all-functions))
169 (meth (sort meth1
(lambda (a b
)
170 (string< (symbol-name a
)
172 (buff (get-buffer-create "*EIEIO Method List*"))
174 (standard-output buff
)
175 (slots '(method-static
179 method-generic-before
180 method-generic-primary
181 method-generic-after
))
189 (idxarray (make-vector (length slots
) 0))
193 (switch-to-buffer-other-window buff
)
199 (princ "Method Name")
201 (princ "--------------------------------------------------------------------")
204 (let ((mtree (get M
'eieio-method-tree
))
208 (let ((num (length (aref mtree
(symbol-value S
)))))
209 (aset idxarray
(symbol-value S
)
210 (+ num
(aref idxarray
(symbol-value S
))))
214 (if (eq S
'method-primary
)
218 ;; Is this a primary-only impl method?
219 (when (and P
(not !P
))
220 (setq primaryonly
(1+ primaryonly
))
222 (setq oneprimary
(1+ oneprimary
))
228 (setq methidx
(1+ methidx
))
231 (princ "--------------------------------------------------------------------")
234 (prin1 (aref idxarray
(symbol-value S
)))
238 (princ " Total symbols")
246 (princ "Methods Primary Only: ")
249 (princ (format "%d" (floor (* 100.0 primaryonly
) methidx
)))
250 (princ "% of total methods")
252 (princ "Only One Primary Impl: ")
255 (princ (format "%d" (floor (* 100.0 oneprimary
) primaryonly
)))
256 (princ "% of total primary methods")
263 (defvar eieio-class-speedbar-key-map nil
264 "Keymap used when working with a project in speedbar.")
266 (defun eieio-class-speedbar-make-map ()
267 "Make a keymap for EIEIO under speedbar."
268 (setq eieio-class-speedbar-key-map
(speedbar-make-specialized-keymap))
270 ;; General viewing stuff
271 (define-key eieio-class-speedbar-key-map
"\C-m" 'speedbar-edit-line
)
272 (define-key eieio-class-speedbar-key-map
"+" 'speedbar-expand-line
)
273 (define-key eieio-class-speedbar-key-map
"-" 'speedbar-contract-line
)
276 (if eieio-class-speedbar-key-map
278 (if (not (featurep 'speedbar
))
279 (add-hook 'speedbar-load-hook
(lambda ()
280 (eieio-class-speedbar-make-map)
281 (speedbar-add-expansion-list
283 eieio-class-speedbar-menu
284 eieio-class-speedbar-key-map
285 eieio-class-speedbar
))))
286 (eieio-class-speedbar-make-map)
287 (speedbar-add-expansion-list '("EIEIO"
288 eieio-class-speedbar-menu
289 eieio-class-speedbar-key-map
290 eieio-class-speedbar
))))
292 (defvar eieio-class-speedbar-menu
294 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
296 (defun eieio-class-speedbar (_dir-or-object _depth
)
297 "Create buttons in speedbar that represents the current project.
298 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
299 current expansion depth."
300 (when (eq (point-min) (point-max))
301 ;; This function is only called once, to start the whole deal.
302 ;; Create and expand the default object.
303 (eieio-class-button 'eieio-default-superclass
0)
305 (speedbar-expand-line)))
307 (defun eieio-class-button (class depth
)
308 "Draw a speedbar button at the current point for CLASS at DEPTH."
309 (cl-check-type class class
)
310 (let ((subclasses (eieio--class-children (cl--find-class class
))))
312 (speedbar-make-tag-line 'angle ?
+
316 'eieio-describe-class-sb
318 'speedbar-directory-face
320 (speedbar-make-tag-line 'angle ? nil nil
322 'eieio-describe-class-sb
324 'speedbar-directory-face
327 (defun eieio-sb-expand (text class indent
)
328 "For button TEXT, expand CLASS at the current location.
329 Argument INDENT is the depth of indentation."
330 (cond ((string-match "+" text
) ;we have to expand this file
331 (speedbar-change-expand-button-char ?-
)
332 (speedbar-with-writable
334 (end-of-line) (forward-char 1)
335 (let ((subclasses (eieio--class-children (cl--find-class class
))))
337 (eieio-class-button (car subclasses
) (1+ indent
))
338 (setq subclasses
(cdr subclasses
)))))))
339 ((string-match "-" text
) ;we have to contract this node
340 (speedbar-change-expand-button-char ?
+)
341 (speedbar-delete-subblock indent
))
342 (t (error "Ooops... not sure what to do")))
343 (speedbar-center-buffer-smartly))
345 (defun eieio-describe-class-sb (_text token _indent
)
346 "Describe the class TEXT in TOKEN.
347 INDENT is the current indentation level."
348 (dframe-with-attached-buffer
349 (describe-function token
))
350 (dframe-maybee-jump-to-attached-frame))
355 ;; generated-autoload-file: "eieio-loaddefs.el"
358 ;;; eieio-opt.el ends here