1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2013 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 (if (not (class-p root-class
)) (signal 'wrong-type-argument
(list 'class-p root-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 (if (not (class-p (eval this-root
))) (signal 'wrong-type-argument
(list 'class-p this-root
)))
62 (let ((myname (symbol-name this-root
))
63 (chl (aref (class-v this-root
) class-children
))
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 ;;;###autoload(defalias 'describe-class 'eieio-describe-class)
80 (defun eieio-describe-class (class &optional headerfcn
)
81 "Describe a CLASS defined by a string or symbol.
82 If CLASS is actually an object, then also display current values of that object.
83 Optional HEADERFCN should be called to insert a few bits of info first."
84 (interactive (list (eieio-read-class "Class: ")))
85 (with-output-to-temp-buffer (help-buffer) ;"*Help*"
86 (help-setup-xref (list #'eieio-describe-class class headerfcn
)
87 (called-interactively-p 'interactive
))
89 (when headerfcn
(funcall headerfcn
))
92 (if (class-option class
:abstract
)
95 ;; Print file location
96 (when (get class
'class-location
)
98 (princ (file-name-nondirectory (get class
'class-location
)))
101 ;; Inheritance tree information
102 (let ((pl (class-parents class
)))
104 (princ " Inherits from ")
106 (princ "`") (prin1 (car pl
)) (princ "'")
108 (if pl
(princ ", ")))
110 (let ((ch (class-children class
)))
114 (princ "`") (prin1 (car ch
)) (princ "'")
116 (if ch
(princ ", ")))
119 ;; System documentation
120 (let ((doc (documentation-property class
'variable-documentation
)))
122 (princ "Documentation:")
127 ;; Describe all the slots in this class
128 (eieio-describe-class-slots class
)
129 ;; Describe all the methods specific to this class.
130 (let ((methods (eieio-all-generic-functions class
))
132 (if (not methods
) nil
133 (princ "Specialized Methods:")
137 (setq doc
(eieio-method-documentation (car methods
) class
))
139 (prin1 (car methods
))
142 (princ " Undocumented")
146 (prin1 (car (car doc
)))
148 (princ (cdr (car doc
)))))
153 (prin1 (car (car doc
)))
155 (princ (cdr (car doc
)))))
160 (prin1 (car (car doc
)))
162 (princ (cdr (car doc
)))))
167 (prin1 (car (car doc
)))
169 (princ (cdr (car doc
)))))
172 (setq methods
(cdr methods
))))))
173 (with-current-buffer (help-buffer)
176 (defun eieio-describe-class-slots (class)
177 "Describe the slots in CLASS.
178 Outputs to the standard output."
179 (let* ((cv (class-v class
))
180 (docs (aref cv class-public-doc
))
181 (names (aref cv class-public-a
))
182 (deflt (aref cv class-public-d
))
183 (types (aref cv class-public-type
))
184 (publp (aref cv class-public-printer
))
186 (prot (aref cv class-protection
))
188 (princ "Instance Allocated Slots:")
192 (if (car prot
) (princ "Private "))
195 (when (not (eq (aref types i
) t
))
197 (prin1 (aref types i
)))
198 (unless (eq (car deflt
) eieio-unbound
)
199 (princ " default = ")
202 (princ " printer = ")
210 (setq names
(cdr names
)
216 (setq docs
(aref cv class-class-allocation-doc
)
217 names
(aref cv class-class-allocation-a
)
218 types
(aref cv class-class-allocation-type
)
220 prot
(aref cv class-class-allocation-protection
))
223 (princ "Class Allocated Slots:"))
231 (unless (eq (aref types i
) t
)
233 (prin1 (aref types i
)))
235 (let ((value (eieio-oref class
(car names
))))
245 (setq names
(cdr names
)
251 (defun eieio-describe-constructor (fcn)
252 "Describe the constructor function FCN.
253 Uses `eieio-describe-class' to describe the class being constructed."
255 ;; Use eieio-read-class since all constructors have the same name as
256 ;; the class they create.
257 (list (eieio-read-class "Class: ")))
258 (eieio-describe-class
260 ;; Describe the constructor part.
262 (princ " is an object constructor function")
263 ;; Print file location
264 (when (get fcn
'class-location
)
266 (princ (file-name-nondirectory (get fcn
'class-location
)))
269 (princ "Creates an object of class ")
277 (defun eieio-build-class-list (class)
278 "Return a list of all classes that inherit from CLASS."
283 (append (list c
) (eieio-build-class-list c
)))
284 (class-children-fast class
)))
287 (defun eieio-build-class-alist (&optional class instantiable-only buildlist
)
288 "Return an alist of all currently active classes for completion purposes.
289 Optional argument CLASS is the class to start with.
290 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
291 are not abstract, otherwise allow all classes.
292 Optional argument BUILDLIST is more list to attach and is used internally."
293 (let* ((cc (or class eieio-default-superclass
))
294 (sublst (aref (class-v cc
) class-children
)))
295 (unless (assoc (symbol-name cc
) buildlist
)
296 (when (or (not instantiable-only
) (not (class-abstract-p cc
)))
297 (setq buildlist
(cons (cons (symbol-name cc
) 1) buildlist
))))
299 (setq buildlist
(eieio-build-class-alist
300 (car sublst
) instantiable-only buildlist
))
301 (setq sublst
(cdr sublst
)))
304 (defvar eieio-read-class nil
305 "History of the function `eieio-read-class' prompt.")
307 (defun eieio-read-class (prompt &optional histvar instantiable-only
)
308 "Return a class chosen by the user using PROMPT.
309 Optional argument HISTVAR is a variable to use as history.
310 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
312 (intern (completing-read prompt
(eieio-build-class-alist nil instantiable-only
)
314 (or histvar
'eieio-read-class
))))
316 (defun eieio-read-subclass (prompt class
&optional histvar instantiable-only
)
317 "Return a class chosen by the user using PROMPT.
318 CLASS is the base class, and completion occurs across all subclasses.
319 Optional argument HISTVAR is a variable to use as history.
320 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
322 (intern (completing-read prompt
323 (eieio-build-class-alist class instantiable-only
)
325 (or histvar
'eieio-read-class
))))
327 ;;; METHOD COMPLETION / DOC
329 (defalias 'describe-method
'eieio-describe-generic
)
330 ;;;###autoload(defalias 'describe-generic 'eieio-describe-generic)
331 (defalias 'eieio-describe-method
'eieio-describe-generic
)
334 (defun eieio-describe-generic (generic)
335 "Describe the generic function GENERIC.
336 Also extracts information about all methods specific to this generic."
337 (interactive (list (eieio-read-generic "Generic Method: ")))
338 (if (not (generic-p generic
))
339 (signal 'wrong-type-argument
'(generic-p generic
)))
340 (with-output-to-temp-buffer (help-buffer) ; "*Help*"
341 (help-setup-xref (list #'eieio-describe-generic generic
)
342 (called-interactively-p 'interactive
))
345 (princ " is a generic function")
346 (when (generic-primary-only-p generic
)
347 (princ " with only ")
348 (when (generic-primary-only-one-p generic
)
350 (princ "primary method")
351 (when (not (generic-primary-only-one-p generic
))
357 (let ((d (documentation generic
)))
359 (princ "The generic is not documented.\n")
360 (princ "Documentation:")
365 (princ "Implementations:")
369 (prefix [ ":STATIC" ":BEFORE" ":PRIMARY" ":AFTER" ] ))
370 ;; Loop over fanciful generics
372 (let ((gm (aref (get generic
'eieio-method-tree
) i
)))
375 (princ (aref prefix
(- i
3)))
377 (princ (or (nth 2 gm
) "Undocumented"))
382 ;; Loop over defined class-specific methods
384 (let ((gm (reverse (aref (get generic
'eieio-method-tree
) i
)))
388 (prin1 (car (car gm
)))
392 (princ (aref prefix i
))
395 (let* ((func (cdr (car gm
)))
396 (arglst (eieio-lambda-arglist func
)))
400 (princ (or (documentation (cdr (car gm
)))
402 ;; Print file location if available
403 (when (and (setq location
(get generic
'method-locations
))
404 (setq location
(assoc (caar gm
) location
)))
405 (setq location
(cadr location
))
406 (princ "\n\nDefined in `")
407 (princ (file-name-nondirectory location
))
413 (with-current-buffer (help-buffer)
416 (defun eieio-lambda-arglist (func)
417 "Return the argument list of FUNC, a function body."
418 (if (symbolp func
) (setq func
(symbol-function func
)))
419 (if (byte-code-function-p func
)
420 (eieio-compiled-function-arglist func
)
423 (defun eieio-all-generic-functions (&optional class
)
424 "Return a list of all generic functions.
425 Optional CLASS argument returns only those functions that contain
427 (let ((l nil
) tree
(cn (if class
(symbol-name class
) nil
)))
430 (setq tree
(get symbol
'eieio-method-obarray
))
433 ;; A symbol might be interned for that class in one of
434 ;; these three slots in the method-obarray.
436 (fboundp (intern-soft cn
(aref tree
0)))
437 (fboundp (intern-soft cn
(aref tree
1)))
438 (fboundp (intern-soft cn
(aref tree
2))))
439 (setq l
(cons symbol l
)))))))
442 (defun eieio-method-documentation (generic class
)
443 "Return a list of the specific documentation of GENERIC for CLASS.
444 If there is not an explicit method for CLASS in GENERIC, or if that
445 function has no documentation, then return nil."
446 (let ((tree (get generic
'eieio-method-obarray
))
447 (cn (symbol-name class
))
448 before primary after
)
451 ;; A symbol might be interned for that class in one of
452 ;; these three slots in the method-obarray.
453 (setq before
(intern-soft cn
(aref tree
0))
454 primary
(intern-soft cn
(aref tree
1))
455 after
(intern-soft cn
(aref tree
2)))
456 (if (not (or (fboundp before
)
460 (list (if (fboundp before
)
461 (cons (eieio-lambda-arglist before
)
462 (documentation before
))
464 (if (fboundp primary
)
465 (cons (eieio-lambda-arglist primary
)
466 (documentation primary
))
469 (cons (eieio-lambda-arglist after
)
470 (documentation after
))
473 (defvar eieio-read-generic nil
474 "History of the `eieio-read-generic' prompt.")
476 (defun eieio-read-generic-p (fn)
477 "Function used in function `eieio-read-generic'.
478 This is because `generic-p' is a macro.
479 Argument FN is the function to test."
482 (defun eieio-read-generic (prompt &optional historyvar
)
483 "Read a generic function from the minibuffer with PROMPT.
484 Optional argument HISTORYVAR is the variable to use as history."
485 (intern (completing-read prompt obarray
'eieio-read-generic-p
486 t nil
(or historyvar
'eieio-read-generic
))))
490 ;; Dump out statistics about all the active methods in a session.
491 (defun eieio-display-method-list ()
492 "Display a list of all the methods and what features are used."
494 (let* ((meth1 (eieio-all-generic-functions))
495 (meth (sort meth1
(lambda (a b
)
496 (string< (symbol-name a
)
498 (buff (get-buffer-create "*EIEIO Method List*"))
500 (standard-output buff
)
501 (slots '(method-static
505 method-generic-before
506 method-generic-primary
507 method-generic-after
))
515 (idxarray (make-vector (length slots
) 0))
519 (switch-to-buffer-other-window buff
)
525 (princ "Method Name")
527 (princ "--------------------------------------------------------------------")
530 (let ((mtree (get M
'eieio-method-tree
))
534 (let ((num (length (aref mtree
(symbol-value S
)))))
535 (aset idxarray
(symbol-value S
)
536 (+ num
(aref idxarray
(symbol-value S
))))
540 (if (eq S
'method-primary
)
544 ;; Is this a primary-only impl method?
545 (when (and P
(not !P
))
546 (setq primaryonly
(1+ primaryonly
))
548 (setq oneprimary
(1+ oneprimary
))
554 (setq methidx
(1+ methidx
))
557 (princ "--------------------------------------------------------------------")
560 (prin1 (aref idxarray
(symbol-value S
)))
564 (princ " Total symbols")
572 (princ "Methods Primary Only: ")
575 (princ (format "%d" (* (/ (float primaryonly
) (float methidx
)) 100)))
576 (princ "% of total methods")
578 (princ "Only One Primary Impl: ")
581 (princ (format "%d" (* (/ (float oneprimary
) (float primaryonly
)) 100)))
582 (princ "% of total primary methods")
586 ;;; HELP AUGMENTATION
588 (define-button-type 'eieio-method-def
589 :supertype
'help-xref
590 'help-function
(lambda (class method file
)
591 (eieio-help-find-method-definition class method file
))
592 'help-echo
(purecopy "mouse-2, RET: find method's definition"))
594 (define-button-type 'eieio-class-def
595 :supertype
'help-xref
596 'help-function
(lambda (class file
)
597 (eieio-help-find-class-definition class file
))
598 'help-echo
(purecopy "mouse-2, RET: find class definition"))
600 (defun eieio-help-find-method-definition (class method file
)
601 (let ((filename (find-library-name file
))
603 (when (null filename
)
604 (error "Cannot find library %s" file
))
605 (setq buf
(find-file-noselect filename
))
606 (with-current-buffer buf
607 (goto-char (point-min))
610 ;; Regexp for searching methods.
611 (concat "(defmethod[ \t\r\n]+" method
612 "\\([ \t\r\n]+:[a-zA-Z]+\\)?"
613 "[ \t\r\n]+(\\s-*(\\(\\sw\\|\\s_\\)+\\s-+"
617 (setq location
(match-beginning 0))))
619 (message "Unable to find location in file")
623 (beginning-of-line))))
625 (defun eieio-help-find-class-definition (class file
)
626 (let ((filename (find-library-name file
))
628 (when (null filename
)
629 (error "Cannot find library %s" file
))
630 (setq buf
(find-file-noselect filename
))
631 (with-current-buffer buf
632 (goto-char (point-min))
635 ;; Regexp for searching a class.
636 (concat "(defclass[ \t\r\n]+" class
"[ \t\r\n]+")
638 (setq location
(match-beginning 0))))
640 (message "Unable to find location in file")
644 (beginning-of-line))))
647 (defun eieio-help-mode-augmentation-maybee (&rest unused
)
648 "For buffers thrown into help mode, augment for EIEIO.
649 Arguments UNUSED are not used."
650 ;; Scan created buttons so far if we are in help mode.
651 (when (eq major-mode
'help-mode
)
653 (goto-char (point-min))
654 (let ((pos t
) (inhibit-read-only t
))
656 (if (get-text-property (point) 'help-xref
) ; move off reference
658 (or (next-single-property-change (point) 'help-xref
)
660 (setq pos
(next-single-property-change (point) 'help-xref
))
663 (let* ((help-data (get-text-property (point) 'help-xref
))
664 ;(method (car help-data))
665 (args (cdr help-data
)))
666 (when (symbolp (car args
))
667 (cond ((class-p (car args
))
668 (setcar help-data
'eieio-describe-class
))
669 ((generic-p (car args
))
670 (setcar help-data
'eieio-describe-generic
))
673 ;; start back at the beginning, and highlight some sections
674 (goto-char (point-min))
675 (while (re-search-forward "^\\(Documentation\\|Implementations\\):$" nil t
)
676 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
677 (goto-char (point-min))
678 (if (re-search-forward "^Specialized Methods:$" nil t
)
679 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
680 (goto-char (point-min))
681 (while (re-search-forward "^\\(Instance\\|Class\\) Allocated Slots:$" nil t
)
682 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
683 (goto-char (point-min))
684 (while (re-search-forward ":\\(STATIC\\|BEFORE\\|AFTER\\|PRIMARY\\)" nil t
)
685 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
686 (goto-char (point-min))
687 (while (re-search-forward "^\\(Private \\)?Slot:" nil t
)
688 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
689 (goto-char (point-min))
691 ((looking-at "\\(.+\\) is a generic function")
692 (let ((mname (match-string 1))
694 (while (re-search-forward "^`\\(.+\\)'[^\0]+?Defined in `\\(.+\\)'" nil t
)
695 (setq cname
(match-string-no-properties 1))
696 (help-xref-button 2 'eieio-method-def cname
698 (cadr (assoc (intern cname
)
700 'method-locations
)))))))
701 ((looking-at "\\(.+\\) is an object constructor function in `\\(.+\\)'")
702 (let ((cname (match-string-no-properties 1)))
703 (help-xref-button 2 'eieio-class-def cname
704 (get (intern cname
) 'class-location
))))
705 ((looking-at "\\(.+\\) is a\\(n abstract\\)? class in `\\(.+\\)'")
706 (let ((cname (match-string-no-properties 1)))
707 (help-xref-button 3 'eieio-class-def cname
708 (get (intern cname
) 'class-location
)))))
714 (defvar eieio-class-speedbar-key-map nil
715 "Keymap used when working with a project in speedbar.")
717 (defun eieio-class-speedbar-make-map ()
718 "Make a keymap for EIEIO under speedbar."
719 (setq eieio-class-speedbar-key-map
(speedbar-make-specialized-keymap))
721 ;; General viewing stuff
722 (define-key eieio-class-speedbar-key-map
"\C-m" 'speedbar-edit-line
)
723 (define-key eieio-class-speedbar-key-map
"+" 'speedbar-expand-line
)
724 (define-key eieio-class-speedbar-key-map
"-" 'speedbar-contract-line
)
727 (if eieio-class-speedbar-key-map
729 (if (not (featurep 'speedbar
))
730 (add-hook 'speedbar-load-hook
(lambda ()
731 (eieio-class-speedbar-make-map)
732 (speedbar-add-expansion-list
734 eieio-class-speedbar-menu
735 eieio-class-speedbar-key-map
736 eieio-class-speedbar
))))
737 (eieio-class-speedbar-make-map)
738 (speedbar-add-expansion-list '("EIEIO"
739 eieio-class-speedbar-menu
740 eieio-class-speedbar-key-map
741 eieio-class-speedbar
))))
743 (defvar eieio-class-speedbar-menu
745 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
747 (defun eieio-class-speedbar (dir-or-object depth
)
748 "Create buttons in speedbar that represents the current project.
749 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
750 current expansion depth."
751 (when (eq (point-min) (point-max))
752 ;; This function is only called once, to start the whole deal.
753 ;; Ceate, and expand the default object.
754 (eieio-class-button eieio-default-superclass
0)
756 (speedbar-expand-line)))
758 (defun eieio-class-button (class depth
)
759 "Draw a speedbar button at the current point for CLASS at DEPTH."
760 (if (not (class-p class
))
761 (signal 'wrong-type-argument
(list 'class-p class
)))
762 (let ((subclasses (aref (class-v class
) class-children
)))
764 (speedbar-make-tag-line 'angle ?
+
768 'eieio-describe-class-sb
770 'speedbar-directory-face
772 (speedbar-make-tag-line 'angle ? nil nil
774 'eieio-describe-class-sb
776 'speedbar-directory-face
779 (defun eieio-sb-expand (text class indent
)
780 "For button TEXT, expand CLASS at the current location.
781 Argument INDENT is the depth of indentation."
782 (cond ((string-match "+" text
) ;we have to expand this file
783 (speedbar-change-expand-button-char ?-
)
784 (speedbar-with-writable
786 (end-of-line) (forward-char 1)
787 (let ((subclasses (aref (class-v class
) class-children
)))
789 (eieio-class-button (car subclasses
) (1+ indent
))
790 (setq subclasses
(cdr subclasses
)))))))
791 ((string-match "-" text
) ;we have to contract this node
792 (speedbar-change-expand-button-char ?
+)
793 (speedbar-delete-subblock indent
))
794 (t (error "Ooops... not sure what to do")))
795 (speedbar-center-buffer-smartly))
797 (defun eieio-describe-class-sb (text token indent
)
798 "Describe the class TEXT in TOKEN.
799 INDENT is the current indentation level."
800 (speedbar-with-attached-buffer
801 (eieio-describe-class token
))
802 (speedbar-maybee-jump-to-attached-frame))
806 ;;; eieio-opt.el ends here