1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2008,
4 ;; 2009, 2010, 2011 Free Software Foundation, Inc.
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.
34 (defun eieio-browse (&optional root-class
)
35 "Create an object browser window to show all objects.
36 If optional ROOT-CLASS, then start with that, otherwise start with
37 variable `eieio-default-superclass'."
38 (interactive (if current-prefix-arg
39 (list (read (completing-read "Class: "
40 (eieio-build-class-alist)
43 (if (not root-class
) (setq root-class
'eieio-default-superclass
))
44 (if (not (class-p root-class
)) (signal 'wrong-type-argument
(list 'class-p root-class
)))
45 (display-buffer (get-buffer-create "*EIEIO OBJECT BROWSE*") t
)
46 (with-current-buffer (get-buffer "*EIEIO OBJECT BROWSE*")
49 (eieio-browse-tree root-class
"" "")
52 (defun eieio-browse-tree (this-root prefix ch-prefix
)
53 "Recursively draw the children of the given class on the screen.
54 Argument THIS-ROOT is the local root of the tree.
55 Argument PREFIX is the character prefix to use.
56 Argument CH-PREFIX is another character prefix to display."
57 (if (not (class-p (eval this-root
))) (signal 'wrong-type-argument
(list 'class-p this-root
)))
58 (let ((myname (symbol-name this-root
))
59 (chl (aref (class-v this-root
) class-children
))
60 (fprefix (concat ch-prefix
" +--"))
61 (mprefix (concat ch-prefix
" | "))
62 (lprefix (concat ch-prefix
" ")))
63 (insert prefix myname
"\n")
65 (eieio-browse-tree (car chl
) fprefix mprefix
)
68 (eieio-browse-tree (car chl
) fprefix lprefix
))
71 ;;; CLASS COMPLETION / DOCUMENTATION
73 (defalias 'describe-class
'eieio-describe-class
)
75 (defun eieio-describe-class (class &optional headerfcn
)
76 "Describe a CLASS defined by a string or symbol.
77 If CLASS is actually an object, then also display current values of that object.
78 Optional HEADERFCN should be called to insert a few bits of info first."
79 (interactive (list (eieio-read-class "Class: ")))
80 (with-output-to-temp-buffer (help-buffer) ;"*Help*"
81 (help-setup-xref (list #'eieio-describe-class class headerfcn
)
82 (called-interactively-p 'interactive
))
84 (when headerfcn
(funcall headerfcn
))
86 (if (class-option class
:abstract
)
91 ;; Inheritence tree information
92 (let ((pl (class-parents class
)))
94 (princ " Inherits from ")
96 (princ "`") (prin1 (car pl
)) (princ "'")
100 (let ((ch (class-children class
)))
104 (princ "`") (prin1 (car ch
)) (princ "'")
106 (if ch
(princ ", ")))
109 ;; System documentation
110 (let ((doc (documentation-property class
'variable-documentation
)))
112 (princ "Documentation:")
117 ;; Describe all the slots in this class
118 (eieio-describe-class-slots class
)
119 ;; Describe all the methods specific to this class.
120 (let ((methods (eieio-all-generic-functions class
))
122 (if (not methods
) nil
123 (princ "Specialized Methods:")
127 (setq doc
(eieio-method-documentation (car methods
) class
))
129 (prin1 (car methods
))
132 (princ " Undocumented")
136 (prin1 (car (car doc
)))
138 (princ (cdr (car doc
)))))
143 (prin1 (car (car doc
)))
145 (princ (cdr (car doc
)))))
150 (prin1 (car (car doc
)))
152 (princ (cdr (car doc
)))))
157 (prin1 (car (car doc
)))
159 (princ (cdr (car doc
)))))
162 (setq methods
(cdr methods
))))))
163 (with-current-buffer (help-buffer)
166 (defun eieio-describe-class-slots (class)
167 "Describe the slots in CLASS.
168 Outputs to the standard output."
169 (let* ((cv (class-v class
))
170 (docs (aref cv class-public-doc
))
171 (names (aref cv class-public-a
))
172 (deflt (aref cv class-public-d
))
173 (types (aref cv class-public-type
))
174 (publp (aref cv class-public-printer
))
176 (prot (aref cv class-protection
))
178 (princ "Instance Allocated Slots:")
182 (if (car prot
) (princ "Private "))
185 (when (not (eq (aref types i
) t
))
187 (prin1 (aref types i
)))
188 (unless (eq (car deflt
) eieio-unbound
)
189 (princ " default = ")
192 (princ " printer = ")
200 (setq names
(cdr names
)
206 (setq docs
(aref cv class-class-allocation-doc
)
207 names
(aref cv class-class-allocation-a
)
208 types
(aref cv class-class-allocation-type
)
210 prot
(aref cv class-class-allocation-protection
))
213 (princ "Class Allocated Slots:"))
221 (unless (eq (aref types i
) t
)
223 (prin1 (aref types i
)))
225 (let ((value (eieio-oref class
(car names
))))
235 (setq names
(cdr names
)
240 (defun eieio-describe-constructor (fcn)
241 "Describe the constructor function FCN.
242 Uses `eieio-describe-class' to describe the class being constructed."
244 ;; Use eieio-read-class since all constructors have the same name as
245 ;; the class they create.
246 (list (eieio-read-class "Class: ")))
247 (eieio-describe-class
249 ;; Describe the constructor part.
250 (princ "Object Constructor Function: ")
253 (princ "Creates an object of class ")
261 (defun eieio-build-class-alist (&optional class instantiable-only buildlist
)
262 "Return an alist of all currently active classes for completion purposes.
263 Optional argument CLASS is the class to start with.
264 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
265 are not abstract, otherwise allow all classes.
266 Optional argument BUILDLIST is more list to attach and is used internally."
267 (let* ((cc (or class eieio-default-superclass
))
268 (sublst (aref (class-v cc
) class-children
)))
269 (if (or (not instantiable-only
) (not (class-abstract-p cc
)))
270 (setq buildlist
(cons (cons (symbol-name cc
) 1) buildlist
)))
272 (setq buildlist
(eieio-build-class-alist
273 (car sublst
) instantiable-only buildlist
))
274 (setq sublst
(cdr sublst
)))
277 (defvar eieio-read-class nil
278 "History of the function `eieio-read-class' prompt.")
280 (defun eieio-read-class (prompt &optional histvar instantiable-only
)
281 "Return a class chosen by the user using PROMPT.
282 Optional argument HISTVAR is a variable to use as history.
283 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
285 (intern (completing-read prompt
(eieio-build-class-alist nil instantiable-only
)
287 (or histvar
'eieio-read-class
))))
289 (defun eieio-read-subclass (prompt class
&optional histvar instantiable-only
)
290 "Return a class chosen by the user using PROMPT.
291 CLASS is the base class, and completion occurs across all subclasses.
292 Optional argument HISTVAR is a variable to use as history.
293 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
295 (intern (completing-read prompt
296 (eieio-build-class-alist class instantiable-only
)
298 (or histvar
'eieio-read-class
))))
300 ;;; METHOD COMPLETION / DOC
302 (defalias 'describe-method
'eieio-describe-generic
)
303 (defalias 'describe-generic
'eieio-describe-generic
)
304 (defalias 'eieio-describe-method
'eieio-describe-generic
)
306 (defun eieio-describe-generic (generic)
307 "Describe the generic function GENERIC.
308 Also extracts information about all methods specific to this generic."
309 (interactive (list (eieio-read-generic "Generic Method: ")))
310 (if (not (generic-p generic
))
311 (signal 'wrong-type-argument
'(generic-p generic
)))
312 (with-output-to-temp-buffer (help-buffer) ; "*Help*"
313 (help-setup-xref (list #'eieio-describe-generic generic
)
314 (called-interactively-p 'interactive
))
317 (princ " is a generic function")
318 (when (generic-primary-only-p generic
)
319 (princ " with only ")
320 (when (generic-primary-only-one-p generic
)
322 (princ "primary method")
323 (when (not (generic-primary-only-one-p generic
))
329 (let ((d (documentation generic
)))
331 (princ "The generic is not documented.\n")
332 (princ "Documentation:")
337 (princ "Implementations:")
341 (prefix [ ":STATIC" ":BEFORE" ":PRIMARY" ":AFTER" ] ))
342 ;; Loop over fanciful generics
344 (let ((gm (aref (get generic
'eieio-method-tree
) i
)))
347 (princ (aref prefix
(- i
3)))
349 (princ (or (nth 2 gm
) "Undocumented"))
354 ;; Loop over defined class-specific methods
356 (let ((gm (reverse (aref (get generic
'eieio-method-tree
) i
))))
359 (prin1 (car (car gm
)))
363 (princ (aref prefix i
))
366 (let* ((func (cdr (car gm
)))
367 (arglst (eieio-lambda-arglist func
)))
371 (princ (or (documentation (cdr (car gm
)))
377 (with-current-buffer (help-buffer)
380 (defun eieio-lambda-arglist (func)
381 "Return the argument list of FUNC, a function body."
382 (if (symbolp func
) (setq func
(symbol-function func
)))
383 (if (byte-code-function-p func
)
384 (eieio-compiled-function-arglist func
)
387 (defun eieio-all-generic-functions (&optional class
)
388 "Return a list of all generic functions.
389 Optional CLASS argument returns only those functions that contain
391 (let ((l nil
) tree
(cn (if class
(symbol-name class
) nil
)))
394 (setq tree
(get symbol
'eieio-method-obarray
))
397 ;; A symbol might be interned for that class in one of
398 ;; these three slots in the method-obarray.
400 (fboundp (intern-soft cn
(aref tree
0)))
401 (fboundp (intern-soft cn
(aref tree
1)))
402 (fboundp (intern-soft cn
(aref tree
2))))
403 (setq l
(cons symbol l
)))))))
406 (defun eieio-method-documentation (generic class
)
407 "Return a list of the specific documentation of GENERIC for CLASS.
408 If there is not an explicit method for CLASS in GENERIC, or if that
409 function has no documentation, then return nil."
410 (let ((tree (get generic
'eieio-method-obarray
))
411 (cn (symbol-name class
))
412 before primary after
)
415 ;; A symbol might be interned for that class in one of
416 ;; these three slots in the method-obarray.
417 (setq before
(intern-soft cn
(aref tree
0))
418 primary
(intern-soft cn
(aref tree
1))
419 after
(intern-soft cn
(aref tree
2)))
420 (if (not (or (fboundp before
)
424 (list (if (fboundp before
)
425 (cons (eieio-lambda-arglist before
)
426 (documentation before
))
428 (if (fboundp primary
)
429 (cons (eieio-lambda-arglist primary
)
430 (documentation primary
))
433 (cons (eieio-lambda-arglist after
)
434 (documentation after
))
437 (defvar eieio-read-generic nil
438 "History of the `eieio-read-generic' prompt.")
440 (defun eieio-read-generic-p (fn)
441 "Function used in function `eieio-read-generic'.
442 This is because `generic-p' is a macro.
443 Argument FN is the function to test."
446 (defun eieio-read-generic (prompt &optional historyvar
)
447 "Read a generic function from the minibuffer with PROMPT.
448 Optional argument HISTORYVAR is the variable to use as history."
449 (intern (completing-read prompt obarray
'eieio-read-generic-p
450 t nil
(or historyvar
'eieio-read-generic
))))
454 ;; Dump out statistics about all the active methods in a session.
455 (defun eieio-display-method-list ()
456 "Display a list of all the methods and what features are used."
458 (let* ((meth1 (eieio-all-generic-functions))
459 (meth (sort meth1
(lambda (a b
)
460 (string< (symbol-name a
)
462 (buff (get-buffer-create "*EIEIO Method List*"))
464 (standard-output buff
)
465 (slots '(method-static
469 method-generic-before
470 method-generic-primary
471 method-generic-after
))
479 (idxarray (make-vector (length slots
) 0))
483 (switch-to-buffer-other-window buff
)
489 (princ "Method Name")
491 (princ "--------------------------------------------------------------------")
494 (let ((mtree (get M
'eieio-method-tree
))
498 (let ((num (length (aref mtree
(symbol-value S
)))))
499 (aset idxarray
(symbol-value S
)
500 (+ num
(aref idxarray
(symbol-value S
))))
504 (if (eq S
'method-primary
)
508 ;; Is this a primary-only impl method?
509 (when (and P
(not !P
))
510 (setq primaryonly
(1+ primaryonly
))
512 (setq oneprimary
(1+ oneprimary
))
518 (setq methidx
(1+ methidx
))
521 (princ "--------------------------------------------------------------------")
524 (prin1 (aref idxarray
(symbol-value S
)))
528 (princ " Total symbols")
536 (princ "Methods Primary Only: ")
539 (princ (format "%d" (* (/ (float primaryonly
) (float methidx
)) 100)))
540 (princ "% of total methods")
542 (princ "Only One Primary Impl: ")
545 (princ (format "%d" (* (/ (float oneprimary
) (float primaryonly
)) 100)))
546 (princ "% of total primary methods")
550 ;;; HELP AUGMENTATION
552 (defun eieio-help-mode-augmentation-maybee (&rest unused
)
553 "For buffers thrown into help mode, augment for EIEIO.
554 Arguments UNUSED are not used."
555 ;; Scan created buttons so far if we are in help mode.
556 (when (eq major-mode
'help-mode
)
558 (goto-char (point-min))
559 (let ((pos t
) (inhibit-read-only t
))
561 (if (get-text-property (point) 'help-xref
) ; move off reference
563 (or (next-single-property-change (point) 'help-xref
)
565 (setq pos
(next-single-property-change (point) 'help-xref
))
568 (let* ((help-data (get-text-property (point) 'help-xref
))
569 ;(method (car help-data))
570 (args (cdr help-data
)))
571 (when (symbolp (car args
))
572 (cond ((class-p (car args
))
573 (setcar help-data
'eieio-describe-class
))
574 ((generic-p (car args
))
575 (setcar help-data
'eieio-describe-generic
))
578 ;; start back at the beginning, and highlight some sections
579 (goto-char (point-min))
580 (while (re-search-forward "^\\(Documentation\\|Implementations\\):$" nil t
)
581 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
582 (goto-char (point-min))
583 (if (re-search-forward "^Specialized Methods:$" nil t
)
584 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
585 (goto-char (point-min))
586 (while (re-search-forward "^\\(Instance\\|Class\\) Allocated Slots:$" nil t
)
587 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
588 (goto-char (point-min))
589 (while (re-search-forward ":\\(STATIC\\|BEFORE\\|AFTER\\|PRIMARY\\)" nil t
)
590 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
591 (goto-char (point-min))
592 (while (re-search-forward "^\\(Private \\)?Slot:" nil t
)
593 (put-text-property (match-beginning 0) (match-end 0) 'face
'bold
))
601 (error (message "Error loading speedbar... ignored"))))
603 (defvar eieio-class-speedbar-key-map nil
604 "Keymap used when working with a project in speedbar.")
606 (defun eieio-class-speedbar-make-map ()
607 "Make a keymap for EIEIO under speedbar."
608 (setq eieio-class-speedbar-key-map
(speedbar-make-specialized-keymap))
610 ;; General viewing stuff
611 (define-key eieio-class-speedbar-key-map
"\C-m" 'speedbar-edit-line
)
612 (define-key eieio-class-speedbar-key-map
"+" 'speedbar-expand-line
)
613 (define-key eieio-class-speedbar-key-map
"-" 'speedbar-contract-line
)
616 (if eieio-class-speedbar-key-map
618 (if (not (featurep 'speedbar
))
619 (add-hook 'speedbar-load-hook
(lambda ()
620 (eieio-class-speedbar-make-map)
621 (speedbar-add-expansion-list
623 eieio-class-speedbar-menu
624 eieio-class-speedbar-key-map
625 eieio-class-speedbar
))))
626 (eieio-class-speedbar-make-map)
627 (speedbar-add-expansion-list '("EIEIO"
628 eieio-class-speedbar-menu
629 eieio-class-speedbar-key-map
630 eieio-class-speedbar
))))
632 (defvar eieio-class-speedbar-menu
634 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
636 (defun eieio-class-speedbar (dir-or-object depth
)
637 "Create buttons in speedbar that represents the current project.
638 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
639 current expansion depth."
640 (when (eq (point-min) (point-max))
641 ;; This function is only called once, to start the whole deal.
642 ;; Ceate, and expand the default object.
643 (eieio-class-button eieio-default-superclass
0)
645 (speedbar-expand-line)))
647 (defun eieio-class-button (class depth
)
648 "Draw a speedbar button at the current point for CLASS at DEPTH."
649 (if (not (class-p class
))
650 (signal 'wrong-type-argument
(list 'class-p class
)))
651 (let ((subclasses (aref (class-v class
) class-children
)))
653 (speedbar-make-tag-line 'angle ?
+
657 'eieio-describe-class-sb
659 'speedbar-directory-face
661 (speedbar-make-tag-line 'angle ? nil nil
663 'eieio-describe-class-sb
665 'speedbar-directory-face
668 (defun eieio-sb-expand (text class indent
)
669 "For button TEXT, expand CLASS at the current location.
670 Argument INDENT is the depth of indentation."
671 (cond ((string-match "+" text
) ;we have to expand this file
672 (speedbar-change-expand-button-char ?-
)
673 (speedbar-with-writable
675 (end-of-line) (forward-char 1)
676 (let ((subclasses (aref (class-v class
) class-children
)))
678 (eieio-class-button (car subclasses
) (1+ indent
))
679 (setq subclasses
(cdr subclasses
)))))))
680 ((string-match "-" text
) ;we have to contract this node
681 (speedbar-change-expand-button-char ?
+)
682 (speedbar-delete-subblock indent
))
683 (t (error "Ooops... not sure what to do")))
684 (speedbar-center-buffer-smartly))
686 (defun eieio-describe-class-sb (text token indent
)
687 "Describe the class TEXT in TOKEN.
688 INDENT is the current indentation level."
689 (speedbar-with-attached-buffer
690 (eieio-describe-class token
))
691 (speedbar-maybee-jump-to-attached-frame))
695 ;; arch-tag: 71eab5f5-462f-4fa1-8ed1-f5ca1bf9adb6
696 ;;; eieio-opt.el ends here