1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2011
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This contains support functions to eieio. These functions contain
29 ;; some small class browser and class printing functions.
36 (defun eieio-browse (&optional root-class
)
37 "Create an object browser window to show all objects.
38 If optional ROOT-CLASS, then start with that, otherwise start with
39 variable `eieio-default-superclass'."
40 (interactive (if current-prefix-arg
41 (list (read (completing-read "Class: "
42 (eieio-build-class-alist)
45 (if (not root-class
) (setq root-class
'eieio-default-superclass
))
46 (if (not (class-p root-class
)) (signal 'wrong-type-argument
(list 'class-p root-class
)))
47 (display-buffer (get-buffer-create "*EIEIO OBJECT BROWSE*") t
)
48 (with-current-buffer (get-buffer "*EIEIO OBJECT BROWSE*")
51 (eieio-browse-tree root-class
"" "")
54 (defun eieio-browse-tree (this-root prefix ch-prefix
)
55 "Recursively draw the children of the given class on the screen.
56 Argument THIS-ROOT is the local root of the tree.
57 Argument PREFIX is the character prefix to use.
58 Argument CH-PREFIX is another character prefix to display."
59 (if (not (class-p (eval this-root
))) (signal 'wrong-type-argument
(list 'class-p this-root
)))
60 (let ((myname (symbol-name this-root
))
61 (chl (aref (class-v this-root
) class-children
))
62 (fprefix (concat ch-prefix
" +--"))
63 (mprefix (concat ch-prefix
" | "))
64 (lprefix (concat ch-prefix
" ")))
65 (insert prefix myname
"\n")
67 (eieio-browse-tree (car chl
) fprefix mprefix
)
70 (eieio-browse-tree (car chl
) fprefix lprefix
))
73 ;;; CLASS COMPLETION / DOCUMENTATION
76 (defalias 'describe-class
'eieio-describe-class
)
79 (defun eieio-describe-class (class &optional headerfcn
)
80 "Describe a CLASS defined by a string or symbol.
81 If CLASS is actually an object, then also display current values of that object.
82 Optional HEADERFCN should be called to insert a few bits of info first."
83 (interactive (list (eieio-read-class "Class: ")))
84 (with-output-to-temp-buffer (help-buffer) ;"*Help*"
85 (help-setup-xref (list #'eieio-describe-class class headerfcn
)
86 (called-interactively-p 'interactive
))
88 (when headerfcn
(funcall headerfcn
))
90 (if (class-option class
:abstract
)
95 ;; Inheritence tree information
96 (let ((pl (class-parents class
)))
98 (princ " Inherits from ")
100 (princ "`") (prin1 (car pl
)) (princ "'")
102 (if pl
(princ ", ")))
104 (let ((ch (class-children class
)))
108 (princ "`") (prin1 (car ch
)) (princ "'")
110 (if ch
(princ ", ")))
113 ;; System documentation
114 (let ((doc (documentation-property class
'variable-documentation
)))
116 (princ "Documentation:")
121 ;; Describe all the slots in this class
122 (eieio-describe-class-slots class
)
123 ;; Describe all the methods specific to this class.
124 (let ((methods (eieio-all-generic-functions class
))
126 (if (not methods
) nil
127 (princ "Specialized Methods:")
131 (setq doc
(eieio-method-documentation (car methods
) class
))
133 (prin1 (car methods
))
136 (princ " Undocumented")
140 (prin1 (car (car doc
)))
142 (princ (cdr (car doc
)))))
147 (prin1 (car (car doc
)))
149 (princ (cdr (car doc
)))))
154 (prin1 (car (car doc
)))
156 (princ (cdr (car doc
)))))
161 (prin1 (car (car doc
)))
163 (princ (cdr (car doc
)))))
166 (setq methods
(cdr methods
))))))
167 (with-current-buffer (help-buffer)
170 (defun eieio-describe-class-slots (class)
171 "Describe the slots in CLASS.
172 Outputs to the standard output."
173 (let* ((cv (class-v class
))
174 (docs (aref cv class-public-doc
))
175 (names (aref cv class-public-a
))
176 (deflt (aref cv class-public-d
))
177 (types (aref cv class-public-type
))
178 (publp (aref cv class-public-printer
))
180 (prot (aref cv class-protection
))
182 (princ "Instance Allocated Slots:")
186 (if (car prot
) (princ "Private "))
189 (when (not (eq (aref types i
) t
))
191 (prin1 (aref types i
)))
192 (unless (eq (car deflt
) eieio-unbound
)
193 (princ " default = ")
196 (princ " printer = ")
204 (setq names
(cdr names
)
210 (setq docs
(aref cv class-class-allocation-doc
)
211 names
(aref cv class-class-allocation-a
)
212 types
(aref cv class-class-allocation-type
)
214 prot
(aref cv class-class-allocation-protection
))
217 (princ "Class Allocated Slots:"))
225 (unless (eq (aref types i
) t
)
227 (prin1 (aref types i
)))
229 (let ((value (eieio-oref class
(car names
))))
239 (setq names
(cdr names
)
245 (defun eieio-describe-constructor (fcn)
246 "Describe the constructor function FCN.
247 Uses `eieio-describe-class' to describe the class being constructed."
249 ;; Use eieio-read-class since all constructors have the same name as
250 ;; the class they create.
251 (list (eieio-read-class "Class: ")))
252 (eieio-describe-class
254 ;; Describe the constructor part.
255 (princ "Object Constructor Function: ")
258 (princ "Creates an object of class ")
266 (defun eieio-build-class-alist (&optional class instantiable-only buildlist
)
267 "Return an alist of all currently active classes for completion purposes.
268 Optional argument CLASS is the class to start with.
269 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
270 are not abstract, otherwise allow all classes.
271 Optional argument BUILDLIST is more list to attach and is used internally."
272 (let* ((cc (or class eieio-default-superclass
))
273 (sublst (aref (class-v cc
) class-children
)))
274 (if (or (not instantiable-only
) (not (class-abstract-p cc
)))
275 (setq buildlist
(cons (cons (symbol-name cc
) 1) buildlist
)))
277 (setq buildlist
(eieio-build-class-alist
278 (car sublst
) instantiable-only buildlist
))
279 (setq sublst
(cdr sublst
)))
282 (defvar eieio-read-class nil
283 "History of the function `eieio-read-class' prompt.")
285 (defun eieio-read-class (prompt &optional histvar instantiable-only
)
286 "Return a class chosen by the user using PROMPT.
287 Optional argument HISTVAR is a variable to use as history.
288 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
290 (intern (completing-read prompt
(eieio-build-class-alist nil instantiable-only
)
292 (or histvar
'eieio-read-class
))))
294 (defun eieio-read-subclass (prompt class
&optional histvar instantiable-only
)
295 "Return a class chosen by the user using PROMPT.
296 CLASS is the base class, and completion occurs across all subclasses.
297 Optional argument HISTVAR is a variable to use as history.
298 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
300 (intern (completing-read prompt
301 (eieio-build-class-alist class instantiable-only
)
303 (or histvar
'eieio-read-class
))))
305 ;;; METHOD COMPLETION / DOC
307 (defalias 'describe-method
'eieio-describe-generic
)
309 (defalias 'describe-generic
'eieio-describe-generic
)
310 (defalias 'eieio-describe-method
'eieio-describe-generic
)
313 (defun eieio-describe-generic (generic)
314 "Describe the generic function GENERIC.
315 Also extracts information about all methods specific to this generic."
316 (interactive (list (eieio-read-generic "Generic Method: ")))
317 (if (not (generic-p generic
))
318 (signal 'wrong-type-argument
'(generic-p generic
)))
319 (with-output-to-temp-buffer (help-buffer) ; "*Help*"
320 (help-setup-xref (list #'eieio-describe-generic generic
)
321 (called-interactively-p 'interactive
))
324 (princ " is a generic function")
325 (when (generic-primary-only-p generic
)
326 (princ " with only ")
327 (when (generic-primary-only-one-p generic
)
329 (princ "primary method")
330 (when (not (generic-primary-only-one-p generic
))
336 (let ((d (documentation generic
)))
338 (princ "The generic is not documented.\n")
339 (princ "Documentation:")
344 (princ "Implementations:")
348 (prefix [ ":STATIC" ":BEFORE" ":PRIMARY" ":AFTER" ] ))
349 ;; Loop over fanciful generics
351 (let ((gm (aref (get generic
'eieio-method-tree
) i
)))
354 (princ (aref prefix
(- i
3)))
356 (princ (or (nth 2 gm
) "Undocumented"))
361 ;; Loop over defined class-specific methods
363 (let ((gm (reverse (aref (get generic
'eieio-method-tree
) i
))))
366 (prin1 (car (car gm
)))
370 (princ (aref prefix i
))
373 (let* ((func (cdr (car gm
)))
374 (arglst (eieio-lambda-arglist func
)))
378 (princ (or (documentation (cdr (car gm
)))
384 (with-current-buffer (help-buffer)
387 (defun eieio-lambda-arglist (func)
388 "Return the argument list of FUNC, a function body."
389 (if (symbolp func
) (setq func
(symbol-function func
)))
390 (if (byte-code-function-p func
)
391 (eieio-compiled-function-arglist func
)
394 (defun eieio-all-generic-functions (&optional class
)
395 "Return a list of all generic functions.
396 Optional CLASS argument returns only those functions that contain
398 (let ((l nil
) tree
(cn (if class
(symbol-name class
) nil
)))
401 (setq tree
(get symbol
'eieio-method-obarray
))
404 ;; A symbol might be interned for that class in one of
405 ;; these three slots in the method-obarray.
407 (fboundp (intern-soft cn
(aref tree
0)))
408 (fboundp (intern-soft cn
(aref tree
1)))
409 (fboundp (intern-soft cn
(aref tree
2))))
410 (setq l
(cons symbol l
)))))))
413 (defun eieio-method-documentation (generic class
)
414 "Return a list of the specific documentation of GENERIC for CLASS.
415 If there is not an explicit method for CLASS in GENERIC, or if that
416 function has no documentation, then return nil."
417 (let ((tree (get generic
'eieio-method-obarray
))
418 (cn (symbol-name class
))
419 before primary after
)
422 ;; A symbol might be interned for that class in one of
423 ;; these three slots in the method-obarray.
424 (setq before
(intern-soft cn
(aref tree
0))
425 primary
(intern-soft cn
(aref tree
1))
426 after
(intern-soft cn
(aref tree
2)))
427 (if (not (or (fboundp before
)
431 (list (if (fboundp before
)
432 (cons (eieio-lambda-arglist before
)
433 (documentation before
))
435 (if (fboundp primary
)
436 (cons (eieio-lambda-arglist primary
)
437 (documentation primary
))
440 (cons (eieio-lambda-arglist after
)
441 (documentation after
))
444 (defvar eieio-read-generic nil
445 "History of the `eieio-read-generic' prompt.")
447 (defun eieio-read-generic-p (fn)
448 "Function used in function `eieio-read-generic'.
449 This is because `generic-p' is a macro.
450 Argument FN is the function to test."
453 (defun eieio-read-generic (prompt &optional historyvar
)
454 "Read a generic function from the minibuffer with PROMPT.
455 Optional argument HISTORYVAR is the variable to use as history."
456 (intern (completing-read prompt obarray
'eieio-read-generic-p
457 t nil
(or historyvar
'eieio-read-generic
))))
461 ;; Dump out statistics about all the active methods in a session.
462 (defun eieio-display-method-list ()
463 "Display a list of all the methods and what features are used."
465 (let* ((meth1 (eieio-all-generic-functions))
466 (meth (sort meth1
(lambda (a b
)
467 (string< (symbol-name a
)
469 (buff (get-buffer-create "*EIEIO Method List*"))
471 (standard-output buff
)
472 (slots '(method-static
476 method-generic-before
477 method-generic-primary
478 method-generic-after
))
486 (idxarray (make-vector (length slots
) 0))
490 (switch-to-buffer-other-window buff
)
496 (princ "Method Name")
498 (princ "--------------------------------------------------------------------")
501 (let ((mtree (get M
'eieio-method-tree
))
505 (let ((num (length (aref mtree
(symbol-value S
)))))
506 (aset idxarray
(symbol-value S
)
507 (+ num
(aref idxarray
(symbol-value S
))))
511 (if (eq S
'method-primary
)
515 ;; Is this a primary-only impl method?
516 (when (and P
(not !P
))
517 (setq primaryonly
(1+ primaryonly
))
519 (setq oneprimary
(1+ oneprimary
))
525 (setq methidx
(1+ methidx
))
528 (princ "--------------------------------------------------------------------")
531 (prin1 (aref idxarray
(symbol-value S
)))
535 (princ " Total symbols")
543 (princ "Methods Primary Only: ")
546 (princ (format "%d" (* (/ (float primaryonly
) (float methidx
)) 100)))
547 (princ "% of total methods")
549 (princ "Only One Primary Impl: ")
552 (princ (format "%d" (* (/ (float oneprimary
) (float primaryonly
)) 100)))
553 (princ "% of total primary methods")
557 ;;; HELP AUGMENTATION
560 (defun eieio-help-mode-augmentation-maybee (&rest unused
)
561 "For buffers thrown into help mode, augment for EIEIO.
562 Arguments UNUSED are not used."
563 ;; Scan created buttons so far if we are in help mode.
564 (when (eq major-mode
'help-mode
)
566 (goto-char (point-min))
567 (let ((pos t
) (inhibit-read-only t
))
569 (if (get-text-property (point) 'help-xref
) ; move off reference
571 (or (next-single-property-change (point) 'help-xref
)
573 (setq pos
(next-single-property-change (point) 'help-xref
))
576 (let* ((help-data (get-text-property (point) 'help-xref
))
577 ;(method (car help-data))
578 (args (cdr help-data
)))
579 (when (symbolp (car args
))
580 (cond ((class-p (car args
))
581 (setcar help-data
'eieio-describe-class
))
582 ((generic-p (car args
))
583 (setcar help-data
'eieio-describe-generic
))
586 ;; start back at the beginning, and highlight some sections
587 (goto-char (point-min))
588 (while (re-search-forward "^\\(Documentation\\|Implementations\\):$" nil t
)
589 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
590 (goto-char (point-min))
591 (if (re-search-forward "^Specialized Methods:$" nil t
)
592 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
593 (goto-char (point-min))
594 (while (re-search-forward "^\\(Instance\\|Class\\) Allocated Slots:$" nil t
)
595 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
596 (goto-char (point-min))
597 (while (re-search-forward ":\\(STATIC\\|BEFORE\\|AFTER\\|PRIMARY\\)" nil t
)
598 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
599 (goto-char (point-min))
600 (while (re-search-forward "^\\(Private \\)?Slot:" nil t
)
601 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
609 (error (message "Error loading speedbar... ignored"))))
611 (defvar eieio-class-speedbar-key-map nil
612 "Keymap used when working with a project in speedbar.")
614 (defun eieio-class-speedbar-make-map ()
615 "Make a keymap for EIEIO under speedbar."
616 (setq eieio-class-speedbar-key-map
(speedbar-make-specialized-keymap))
618 ;; General viewing stuff
619 (define-key eieio-class-speedbar-key-map
"\C-m" 'speedbar-edit-line
)
620 (define-key eieio-class-speedbar-key-map
"+" 'speedbar-expand-line
)
621 (define-key eieio-class-speedbar-key-map
"-" 'speedbar-contract-line
)
624 (if eieio-class-speedbar-key-map
626 (if (not (featurep 'speedbar
))
627 (add-hook 'speedbar-load-hook
(lambda ()
628 (eieio-class-speedbar-make-map)
629 (speedbar-add-expansion-list
631 eieio-class-speedbar-menu
632 eieio-class-speedbar-key-map
633 eieio-class-speedbar
))))
634 (eieio-class-speedbar-make-map)
635 (speedbar-add-expansion-list '("EIEIO"
636 eieio-class-speedbar-menu
637 eieio-class-speedbar-key-map
638 eieio-class-speedbar
))))
640 (defvar eieio-class-speedbar-menu
642 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
644 (defun eieio-class-speedbar (dir-or-object depth
)
645 "Create buttons in speedbar that represents the current project.
646 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
647 current expansion depth."
648 (when (eq (point-min) (point-max))
649 ;; This function is only called once, to start the whole deal.
650 ;; Ceate, and expand the default object.
651 (eieio-class-button eieio-default-superclass
0)
653 (speedbar-expand-line)))
655 (defun eieio-class-button (class depth
)
656 "Draw a speedbar button at the current point for CLASS at DEPTH."
657 (if (not (class-p class
))
658 (signal 'wrong-type-argument
(list 'class-p class
)))
659 (let ((subclasses (aref (class-v class
) class-children
)))
661 (speedbar-make-tag-line 'angle ?
+
665 'eieio-describe-class-sb
667 'speedbar-directory-face
669 (speedbar-make-tag-line 'angle ? nil nil
671 'eieio-describe-class-sb
673 'speedbar-directory-face
676 (defun eieio-sb-expand (text class indent
)
677 "For button TEXT, expand CLASS at the current location.
678 Argument INDENT is the depth of indentation."
679 (cond ((string-match "+" text
) ;we have to expand this file
680 (speedbar-change-expand-button-char ?-
)
681 (speedbar-with-writable
683 (end-of-line) (forward-char 1)
684 (let ((subclasses (aref (class-v class
) class-children
)))
686 (eieio-class-button (car subclasses
) (1+ indent
))
687 (setq subclasses
(cdr subclasses
)))))))
688 ((string-match "-" text
) ;we have to contract this node
689 (speedbar-change-expand-button-char ?
+)
690 (speedbar-delete-subblock indent
))
691 (t (error "Ooops... not sure what to do")))
692 (speedbar-center-buffer-smartly))
694 (defun eieio-describe-class-sb (text token indent
)
695 "Describe the class TEXT in TOKEN.
696 INDENT is the current indentation level."
697 (speedbar-with-attached-buffer
698 (eieio-describe-class token
))
699 (speedbar-maybee-jump-to-attached-frame))
704 ;; generated-autoload-file: "eieio.el"
707 ;;; eieio-opt.el ends here