1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2015 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.
38 (defun eieio-browse (&optional root-class
)
39 "Create an object browser window to show all objects.
40 If optional ROOT-CLASS, then start with that, otherwise start with
41 variable `eieio-default-superclass'."
42 (interactive (if current-prefix-arg
43 (list (read (completing-read "Class: "
44 (eieio-build-class-alist)
47 (if (not root-class
) (setq root-class
'eieio-default-superclass
))
48 (cl-check-type root-class class
)
49 (display-buffer (get-buffer-create "*EIEIO OBJECT BROWSE*") t
)
50 (with-current-buffer (get-buffer "*EIEIO OBJECT BROWSE*")
53 (eieio-browse-tree root-class
"" "")
56 (defun eieio-browse-tree (this-root prefix ch-prefix
)
57 "Recursively draw the children of the given class on the screen.
58 Argument THIS-ROOT is the local root of the tree.
59 Argument PREFIX is the character prefix to use.
60 Argument CH-PREFIX is another character prefix to display."
61 (cl-check-type this-root class
)
62 (let ((myname (symbol-name this-root
))
63 (chl (eieio--class-children (eieio--class-v this-root
)))
64 (fprefix (concat ch-prefix
" +--"))
65 (mprefix (concat ch-prefix
" | "))
66 (lprefix (concat ch-prefix
" ")))
67 (insert prefix myname
"\n")
69 (eieio-browse-tree (car chl
) fprefix mprefix
)
72 (eieio-browse-tree (car chl
) fprefix lprefix
))
75 ;;; CLASS COMPLETION / DOCUMENTATION
77 ;; Called via help-fns-describe-function-functions.
78 (declare-function help-fns-short-filename
"help-fns" (filename))
81 (defun eieio-help-class (class)
82 "Print help description for CLASS.
83 If CLASS is actually an object, then also display current values of that object."
87 (if (eieio--class-option (eieio--class-v class
) :abstract
)
91 (let ((location (find-lisp-object-file-name class
'eieio-defclass
)))
94 (help-insert-xref-button
95 (help-fns-short-filename location
)
96 'eieio-class-def class location
'eieio-defclass
)
100 (let ((pl (eieio-class-parents class
))
103 (insert " Inherits from ")
104 (while (setq cur
(pop pl
))
105 (setq cur
(eieio--class-name cur
))
107 (help-insert-xref-button (symbol-name cur
)
109 (insert (if pl
"', " "'")))
112 (let ((ch (eieio-class-children class
))
115 (insert " Children ")
116 (while (setq cur
(pop ch
))
118 (help-insert-xref-button (symbol-name cur
)
120 (insert (if ch
"', " "'")))
122 ;; System documentation
123 (let ((doc (documentation-property class
'variable-documentation
)))
125 (insert "\n" doc
"\n\n")))
126 ;; Describe all the slots in this class.
127 (eieio-help-class-slots class
)
128 ;; Describe all the methods specific to this class.
129 (let ((generics (eieio-all-generic-functions class
)))
131 (insert (propertize "Specialized Methods:\n\n" 'face
'bold
))
132 (dolist (generic generics
)
134 (help-insert-xref-button (symbol-name generic
) 'help-function generic
)
136 (pcase-dolist (`(,qualifiers
,args
,doc
)
137 (eieio-method-documentation generic class
))
138 (insert (format " %s%S\n" qualifiers args
)
142 (defun eieio--help-print-slot (slot)
145 (propertize "Slot: " 'face
'bold
)
146 (prin1-to-string (cl--slot-descriptor-name slot
))
147 (unless (eq (cl--slot-descriptor-type slot
) t
)
149 (prin1-to-string (cl--slot-descriptor-type slot
))))
150 (unless (eq (cl--slot-descriptor-initform slot
) eieio-unbound
)
151 (concat " default = "
152 (prin1-to-string (cl--slot-descriptor-initform slot
))))
153 (when (alist-get :printer
(cl--slot-descriptor-props slot
))
154 (concat " printer = "
156 (alist-get :printer
(cl--slot-descriptor-props slot
)))))
157 (when (alist-get :documentation
(cl--slot-descriptor-props slot
))
158 (concat "\n " (alist-get :documentation
(cl--slot-descriptor-props slot
))
162 (defun eieio-help-class-slots (class)
163 "Print help description for the slots in CLASS.
164 Outputs to the current buffer."
165 (let* ((cv (eieio--class-v class
))
166 (slots (eieio--class-slots cv
))
167 (cslots (eieio--class-class-slots cv
)))
168 (insert (propertize "Instance Allocated Slots:\n\n"
170 (dotimes (i (length slots
))
171 (eieio--help-print-slot (aref slots i
)))
172 (when (> (length cslots
) 0)
173 (insert (propertize "\nClass Allocated Slots:\n\n" 'face
'bold
)))
174 (dotimes (i (length cslots
))
175 (eieio--help-print-slot (aref cslots i
)))))
177 (defun eieio-build-class-alist (&optional class instantiable-only buildlist
)
178 "Return an alist of all currently active classes for completion purposes.
179 Optional argument CLASS is the class to start with.
180 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
181 are not abstract, otherwise allow all classes.
182 Optional argument BUILDLIST is more list to attach and is used internally."
183 (let* ((cc (or class
'eieio-default-superclass
))
184 (sublst (eieio--class-children (eieio--class-v cc
))))
185 (unless (assoc (symbol-name cc
) buildlist
)
186 (when (or (not instantiable-only
) (not (class-abstract-p cc
)))
187 ;; FIXME: Completion tables don't need alists, and ede/generic.el needs
188 ;; the symbols rather than their names.
189 (setq buildlist
(cons (cons (symbol-name cc
) 1) buildlist
))))
190 (dolist (elem sublst
)
191 (setq buildlist
(eieio-build-class-alist
192 elem instantiable-only buildlist
)))
195 (defvar eieio-read-class nil
196 "History of the function `eieio-read-class' prompt.")
198 (defun eieio-read-class (prompt &optional histvar instantiable-only
)
199 "Return a class chosen by the user using PROMPT.
200 Optional argument HISTVAR is a variable to use as history.
201 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
203 (intern (completing-read prompt
(eieio-build-class-alist nil instantiable-only
)
205 (or histvar
'eieio-read-class
))))
207 (defun eieio-read-subclass (prompt class
&optional histvar instantiable-only
)
208 "Return a class chosen by the user using PROMPT.
209 CLASS is the base class, and completion occurs across all subclasses.
210 Optional argument HISTVAR is a variable to use as history.
211 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
213 (intern (completing-read prompt
214 (eieio-build-class-alist class instantiable-only
)
216 (or histvar
'eieio-read-class
))))
218 ;;; METHOD COMPLETION / DOC
220 (define-button-type 'eieio-class-def
221 :supertype
'help-function-def
222 'help-echo
(purecopy "mouse-2, RET: find class definition"))
224 (defconst eieio--defclass-regexp
"(defclass[ \t\r\n]+%s[ \t\r\n]+")
225 (with-eval-after-load 'find-func
226 (defvar find-function-regexp-alist
)
227 (add-to-list 'find-function-regexp-alist
228 `(eieio-defclass . eieio--defclass-regexp
)))
231 (defun eieio-help-constructor (ctr)
232 "Describe CTR if it is a class constructor."
235 (let ((location (find-lisp-object-file-name ctr
'eieio-defclass
))
236 (def (symbol-function ctr
)))
237 (goto-char (point-min))
239 (insert (format " is an %s object constructor function"
243 (when (and (autoloadp def
)
246 (find-lisp-object-file-name ctr def
)))
249 (help-insert-xref-button
250 (help-fns-short-filename location
)
251 'eieio-class-def ctr location
'eieio-defclass
)
253 (insert ".\nCreates an object of class " (symbol-name ctr
) ".")
254 (goto-char (point-max))
256 (insert "\n\n[Class description not available until class definition is loaded.]\n")
258 (insert (propertize "\n\nClass description:\n" 'face
'bold
))
259 (eieio-help-class ctr
))
262 (defun eieio--specializers-apply-to-class-p (specializers class
)
263 "Return non-nil if a method with SPECIALIZERS applies to CLASS."
265 (dolist (specializer specializers
)
266 (if (memq (car-safe specializer
) '(subclass eieio--static
))
267 (setq specializer
(nth 1 specializer
)))
268 ;; Don't include the methods that are "too generic", such as those
269 ;; applying to `eieio-default-superclass'.
270 (and (not (memq specializer
'(t eieio-default-superclass
)))
271 (class-p specializer
)
272 (child-of-class-p class specializer
)
276 (defun eieio-all-generic-functions (&optional class
)
277 "Return a list of all generic functions.
278 Optional CLASS argument returns only those functions that contain
283 (let ((generic (and (fboundp symbol
) (cl--generic symbol
))))
286 (if (null class
) (throw 'found t
))
287 (dolist (method (cl--generic-method-table generic
))
288 (if (eieio--specializers-apply-to-class-p
289 (cl--generic-method-specializers method
) class
)
294 (defun eieio-method-documentation (generic class
)
295 "Return info for all methods of GENERIC applicable to CLASS.
296 The value returned is a list of elements of the form
297 \(QUALIFIERS ARGS DOC)."
298 (let ((generic (cl--generic generic
))
301 (dolist (method (cl--generic-method-table generic
))
302 (when (eieio--specializers-apply-to-class-p
303 (cl--generic-method-specializers method
) class
)
304 (push (cl--generic-method-info method
) docs
))))
309 ;; Dump out statistics about all the active methods in a session.
310 (defun eieio-display-method-list ()
311 "Display a list of all the methods and what features are used."
313 (let* ((meth1 (eieio-all-generic-functions))
314 (meth (sort meth1
(lambda (a b
)
315 (string< (symbol-name a
)
317 (buff (get-buffer-create "*EIEIO Method List*"))
319 (standard-output buff
)
320 (slots '(method-static
324 method-generic-before
325 method-generic-primary
326 method-generic-after
))
334 (idxarray (make-vector (length slots
) 0))
338 (switch-to-buffer-other-window buff
)
344 (princ "Method Name")
346 (princ "--------------------------------------------------------------------")
349 (let ((mtree (get M
'eieio-method-tree
))
353 (let ((num (length (aref mtree
(symbol-value S
)))))
354 (aset idxarray
(symbol-value S
)
355 (+ num
(aref idxarray
(symbol-value S
))))
359 (if (eq S
'method-primary
)
363 ;; Is this a primary-only impl method?
364 (when (and P
(not !P
))
365 (setq primaryonly
(1+ primaryonly
))
367 (setq oneprimary
(1+ oneprimary
))
373 (setq methidx
(1+ methidx
))
376 (princ "--------------------------------------------------------------------")
379 (prin1 (aref idxarray
(symbol-value S
)))
383 (princ " Total symbols")
391 (princ "Methods Primary Only: ")
394 (princ (format "%d" (* (/ (float primaryonly
) (float methidx
)) 100)))
395 (princ "% of total methods")
397 (princ "Only One Primary Impl: ")
400 (princ (format "%d" (* (/ (float oneprimary
) (float primaryonly
)) 100)))
401 (princ "% of total primary methods")
408 (defvar eieio-class-speedbar-key-map nil
409 "Keymap used when working with a project in speedbar.")
411 (defun eieio-class-speedbar-make-map ()
412 "Make a keymap for EIEIO under speedbar."
413 (setq eieio-class-speedbar-key-map
(speedbar-make-specialized-keymap))
415 ;; General viewing stuff
416 (define-key eieio-class-speedbar-key-map
"\C-m" 'speedbar-edit-line
)
417 (define-key eieio-class-speedbar-key-map
"+" 'speedbar-expand-line
)
418 (define-key eieio-class-speedbar-key-map
"-" 'speedbar-contract-line
)
421 (if eieio-class-speedbar-key-map
423 (if (not (featurep 'speedbar
))
424 (add-hook 'speedbar-load-hook
(lambda ()
425 (eieio-class-speedbar-make-map)
426 (speedbar-add-expansion-list
428 eieio-class-speedbar-menu
429 eieio-class-speedbar-key-map
430 eieio-class-speedbar
))))
431 (eieio-class-speedbar-make-map)
432 (speedbar-add-expansion-list '("EIEIO"
433 eieio-class-speedbar-menu
434 eieio-class-speedbar-key-map
435 eieio-class-speedbar
))))
437 (defvar eieio-class-speedbar-menu
439 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
441 (defun eieio-class-speedbar (_dir-or-object _depth
)
442 "Create buttons in speedbar that represents the current project.
443 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
444 current expansion depth."
445 (when (eq (point-min) (point-max))
446 ;; This function is only called once, to start the whole deal.
447 ;; Create and expand the default object.
448 (eieio-class-button 'eieio-default-superclass
0)
450 (speedbar-expand-line)))
452 (defun eieio-class-button (class depth
)
453 "Draw a speedbar button at the current point for CLASS at DEPTH."
454 (cl-check-type class class
)
455 (let ((subclasses (eieio--class-children (eieio--class-v class
))))
457 (speedbar-make-tag-line 'angle ?
+
461 'eieio-describe-class-sb
463 'speedbar-directory-face
465 (speedbar-make-tag-line 'angle ? nil nil
467 'eieio-describe-class-sb
469 'speedbar-directory-face
472 (defun eieio-sb-expand (text class indent
)
473 "For button TEXT, expand CLASS at the current location.
474 Argument INDENT is the depth of indentation."
475 (cond ((string-match "+" text
) ;we have to expand this file
476 (speedbar-change-expand-button-char ?-
)
477 (speedbar-with-writable
479 (end-of-line) (forward-char 1)
480 (let ((subclasses (eieio--class-children (eieio--class-v class
))))
482 (eieio-class-button (car subclasses
) (1+ indent
))
483 (setq subclasses
(cdr subclasses
)))))))
484 ((string-match "-" text
) ;we have to contract this node
485 (speedbar-change-expand-button-char ?
+)
486 (speedbar-delete-subblock indent
))
487 (t (error "Ooops... not sure what to do")))
488 (speedbar-center-buffer-smartly))
490 (defun eieio-describe-class-sb (_text token _indent
)
491 "Describe the class TEXT in TOKEN.
492 INDENT is the current indentation level."
493 (dframe-with-attached-buffer
494 (describe-function token
))
495 (dframe-maybee-jump-to-attached-frame))
500 ;; generated-autoload-file: "eieio.el"
503 ;;; eieio-opt.el ends here