1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;;; Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2005 Eric M. Ludlam
5 ;; Author: <zappo@gnu.org>
6 ;; RCS: $Id: eieio-opt.el,v 1.5 2007-11-26 15:01:05 michaels Exp $
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
24 ;; Please send bug reports, etc. to zappo@gnu.org
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)
49 (set-buffer (get-buffer "*EIEIO OBJECT BROWSE*"))
52 (eieio-browse-tree root-class "" "")
55 (defun eieio-browse-tree (this-root prefix ch-prefix)
56 "Recursively, draws 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 (if (not (class-p (eval this-root))) (signal 'wrong-type-argument (list 'class-p this-root)))
61 (let ((myname (symbol-name this-root))
62 (chl (aref (class-v this-root) class-children))
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))
75 (defalias 'describe-class 'eieio-describe-class)
77 (defun eieio-describe-class (class)
78 "Describe a CLASS defined by a string or symbol.
79 If CLASS is actually an object, then also display current values of that obect."
80 (interactive (list (eieio-read-class "Class: ")))
81 (with-output-to-temp-buffer "*Help*"
82 (if (class-option class :abstract)
87 ;; Inheritence tree information
88 (let ((pl (class-parents class)))
90 (princ " Inherits from ")
92 (princ "`") (prin1 (car pl)) (princ "'")
96 (let ((ch (class-children class)))
100 (princ "`") (prin1 (car ch)) (princ "'")
102 (if ch (princ ", ")))
105 ;; System documentation
106 (let ((doc (documentation-property class 'variable-documentation)))
108 (princ "Documentation:")
113 ;; Describe all the slots in this class
114 (eieio-describe-class-slots class)
115 ;; Describe all the methods specific to this class.
116 (let ((methods (eieio-all-generic-functions class))
118 (if (not methods) nil
119 (princ "Specialized Methods:")
123 (setq doc (eieio-method-documentation (car methods) class))
125 (prin1 (car methods))
128 (princ " Undocumented")
132 (prin1 (car (car doc)))
134 (princ (cdr (car doc)))))
139 (prin1 (car (car doc)))
141 (princ (cdr (car doc)))))
146 (prin1 (car (car doc)))
148 (princ (cdr (car doc)))))
153 (prin1 (car (car doc)))
155 (princ (cdr (car doc)))))
158 (setq methods (cdr methods)))))
161 (defun eieio-describe-class-slots (class)
162 "Describe the slots in CLASS.
163 Outputs to the standard output."
164 (let* ((cv (class-v class))
165 (docs (aref cv class-public-doc))
166 (names (aref cv class-public-a))
167 (deflt (aref cv class-public-d))
168 (types (aref cv class-public-type))
170 (prot (aref cv class-protection))
172 (princ "Instance Allocated Slots:")
176 (if (car prot) (princ "Private "))
179 (when (not (eq (aref types i) t))
181 (prin1 (aref types i)))
182 (unless (eq (car deflt) eieio-unbound)
183 (princ " default = ")
191 (setq names (cdr names)
196 (setq docs (aref cv class-class-allocation-doc)
197 names (aref cv class-class-allocation-a)
198 types (aref cv class-class-allocation-type)
200 prot (aref cv class-class-allocation-protection))
203 (princ "Class Allocated Slots:"))
211 (unless (eq (aref types i) t)
213 (prin1 (aref types i)))
220 (setq names (cdr names)
225 (defun eieio-build-class-alist (&optional class instantiable-only buildlist)
226 "Return an alist of all currently active classes for completion purposes.
227 Optional argument CLASS is the class to start with.
228 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
229 are not abstract, otherwise allow all classes.
230 Optional argument BUILDLIST is more list to attach and is used internally."
231 (let* ((cc (or class eieio-default-superclass))
232 (sublst (aref (class-v cc) class-children)))
233 (if (or (not instantiable-only) (not (class-abstract-p cc)))
234 (setq buildlist (cons (cons (symbol-name cc) 1) buildlist)))
236 (setq buildlist (eieio-build-class-alist
237 (car sublst) instantiable-only buildlist))
238 (setq sublst (cdr sublst)))
241 (defvar eieio-read-class nil
242 "History of the function `eieio-read-class' prompt.")
244 (defun eieio-read-class (prompt &optional histvar instantiable-only)
245 "Return a class chosen by the user using PROMPT.
246 Optional argument HISTVAR is a variable to use as history.
247 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
249 (intern (completing-read prompt (eieio-build-class-alist) nil t nil
250 (or histvar 'eieio-read-class))))
252 (defun eieio-read-subclass (prompt class &optional histvar instantiable-only)
253 "Return a class chosen by the user using PROMPT.
254 CLASS is the base class, and completion occurs across all subclasses.
255 Optional argument HISTVAR is a variable to use as history.
256 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
258 (intern (completing-read prompt
259 (eieio-build-class-alist class instantiable-only)
261 (or histvar 'eieio-read-class))))
263 ;;; Collect all the generic functions created so far, and do cool stuff.
266 (defalias 'describe-method 'eieio-describe-generic)
268 (defalias 'describe-generic 'eieio-describe-generic)
270 (defalias 'eieio-describe-method 'eieio-describe-generic)
272 (defun eieio-describe-generic (generic)
273 "Describe the generic function GENERIC.
274 Also extracts information about all methods specific to this generic."
275 (interactive (list (eieio-read-generic "Generic Method: ")))
276 (if (not (generic-p generic))
277 (signal 'wrong-type-argument '(generic-p generic)))
278 (with-output-to-temp-buffer "*Help*"
280 (princ " is a generic function.")
283 (let ((d (documentation generic)))
285 (princ "The generic is not documented.\n")
286 (princ "Documentation:")
291 (princ "Implementations:")
295 (prefix [ ":STATIC" ":BEFORE" ":PRIMARY" ":AFTER" ] ))
296 ;; Loop over fanciful generics
298 (let ((gm (aref (get generic 'eieio-method-tree) i)))
301 (princ (aref prefix (- i 3)))
303 (princ (or (nth 2 gm) "Undocumented"))
308 ;; Loop over defined class-specific methods
310 (let ((gm (reverse (aref (get generic 'eieio-method-tree) i))))
313 (prin1 (car (car gm)))
317 (princ (aref prefix i))
320 (let* ((func (cdr (car gm)))
321 (arglst (eieio-lambda-arglist func)))
325 (princ (or (documentation (cdr (car gm)))
333 (defun eieio-lambda-arglist (func)
334 "Return the argument list of FUNC, a function body."
335 (if (symbolp func) (setq func (symbol-function func)))
336 (if (byte-code-function-p func)
337 (eieio-compiled-function-arglist func)
340 (defun eieio-all-generic-functions (&optional class)
341 "Return a list of all generic functions.
342 Optional CLASS argument returns only those functions that contain methods for CLASS."
343 (let ((l nil) tree (cn (if class (symbol-name class) nil)))
346 (setq tree (get symbol 'eieio-method-obarray))
349 ;; A symbol might be interned for that class in one of
350 ;; these three slots in the method-obarray.
352 (fboundp (intern-soft cn (aref tree 0)))
353 (fboundp (intern-soft cn (aref tree 1)))
354 (fboundp (intern-soft cn (aref tree 2))))
355 (setq l (cons symbol l)))))))
358 (defun eieio-method-documentation (generic class)
359 "Return a list of the specific documentation of GENERIC for CLASS.
360 If there is not an explicit method for CLASS in GENERIC, or if that
361 function has no documentation, then return nil."
362 (let ((tree (get generic 'eieio-method-obarray))
363 (cn (symbol-name class))
364 before primary after)
367 ;; A symbol might be interned for that class in one of
368 ;; these three slots in the method-obarray.
369 (setq before (intern-soft cn (aref tree 0))
370 primary (intern-soft cn (aref tree 1))
371 after (intern-soft cn (aref tree 2)))
372 (if (not (or (fboundp before)
376 (list (if (fboundp before)
377 (cons (eieio-lambda-arglist before)
378 (documentation before))
380 (if (fboundp primary)
381 (cons (eieio-lambda-arglist primary)
382 (documentation primary))
385 (cons (eieio-lambda-arglist after)
386 (documentation after))
389 (defvar eieio-read-generic nil
390 "History of the `eieio-read-generic' prompt.")
392 (defun eieio-read-generic-p (fn)
393 "Function used in function `eieio-read-generic'.
394 This is because `generic-p' is a macro.
395 Argument FN is the function to test."
398 (defun eieio-read-generic (prompt &optional historyvar)
399 "Read a generic function from the minibuffer with PROMPT.
400 Optional argument HISTORYVAR is the variable to use as history."
401 (intern (completing-read prompt obarray 'eieio-read-generic-p
402 t nil (or historyvar 'eieio-read-generic))))
404 ;;; Help system augmentation
406 (defun eieio-help-mode-augmentation-maybee (&rest unused)
407 "For buffers thrown into help mode, augment for eieio.
408 Arguments UNUSED are not used."
409 ;; Scan created buttons so far if we are in help mode.
410 (when (eq major-mode 'help-mode)
412 (goto-char (point-min))
413 (let ((pos t) (inhibit-read-only t))
415 (if (get-text-property (point) 'help-xref) ; move off reference
417 (or (next-single-property-change (point) 'help-xref)
419 (setq pos (next-single-property-change (point) 'help-xref))
422 (let* ((help-data (get-text-property (point) 'help-xref))
423 (method (car help-data))
424 (args (cdr help-data)))
425 (when (symbolp (car args))
426 (cond ((class-p (car args))
427 (setcar help-data 'eieio-describe-class))
428 ((generic-p (car args))
429 (setcar help-data 'eieio-describe-generic))
432 ;; start back at the beginning, and highlight some sections
433 (goto-char (point-min))
434 (while (re-search-forward "^\\(Documentation\\|Implementations\\):$" nil t)
435 (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
436 (goto-char (point-min))
437 (if (re-search-forward "^Specialized Methods:$" nil t)
438 (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
439 (goto-char (point-min))
440 (while (re-search-forward "^\\(Instance\\|Class\\) Allocated Slots:$" nil t)
441 (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
442 (goto-char (point-min))
443 (while (re-search-forward ":\\(STATIC\\|BEFORE\\|AFTER\\|PRIMARY\\)" nil t)
444 (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
445 (goto-char (point-min))
446 (while (re-search-forward "^\\(Private \\)?Slot:" nil t)
447 (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
450 (defun eieio-help-augment-keymap ()
451 "Augment the help keymap for cool EIEIO stuff."
452 (define-key help-map "g" 'describe-generic)
453 (define-key help-map "C" 'describe-class))
455 (if (and (boundp 'help-map) help-map)
456 (eieio-help-augment-keymap)
457 (eval-after-load 'help 'eieio-help-augment-keymap))
459 ;;; How about showing the hierarchy in speedbar? Cool!
464 (error (message "Error loading speedbar... ignored."))))
466 (defvar eieio-class-speedbar-key-map nil
467 "Keymap used when working with a project in speedbar.")
469 (defun eieio-class-speedbar-make-map ()
470 "Make a keymap for eieio under speedbar."
471 (setq eieio-class-speedbar-key-map (speedbar-make-specialized-keymap))
473 ;; General viewing stuff
474 (define-key eieio-class-speedbar-key-map "\C-m" 'speedbar-edit-line)
475 (define-key eieio-class-speedbar-key-map "+" 'speedbar-expand-line)
476 (define-key eieio-class-speedbar-key-map "-" 'speedbar-contract-line)
479 (if eieio-class-speedbar-key-map
481 (if (not (featurep 'speedbar))
482 (add-hook 'speedbar-load-hook (lambda ()
483 (eieio-class-speedbar-make-map)
484 (speedbar-add-expansion-list
486 eieio-class-speedbar-menu
487 eieio-class-speedbar-key-map
488 eieio-class-speedbar))))
489 (eieio-class-speedbar-make-map)
490 (speedbar-add-expansion-list '("EIEIO"
491 eieio-class-speedbar-menu
492 eieio-class-speedbar-key-map
493 eieio-class-speedbar))))
495 (defvar eieio-class-speedbar-menu
497 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
499 (defun eieio-class-speedbar (dir-or-object depth)
500 "Create buttons in speedbar that represents the current project.
501 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the current
503 (when (eq (point-min) (point-max))
504 ;; This function is only called once, to start the whole deal.
505 ;; Ceate, and expand the default object.
506 (eieio-class-button eieio-default-superclass 0)
508 (speedbar-expand-line)))
510 (defun eieio-class-button (class depth)
511 "Draw a speedbar button at the current point for CLASS at DEPTH."
512 (if (not (class-p class))
513 (signal 'wrong-type-argument (list 'class-p class)))
514 (let ((subclasses (aref (class-v class) class-children)))
516 (speedbar-make-tag-line 'angle ?+
520 'eieio-describe-class-sb
522 'speedbar-directory-face
524 (speedbar-make-tag-line 'angle ? nil nil
526 'eieio-describe-class-sb
528 'speedbar-directory-face
531 (defun eieio-sb-expand (text class indent)
532 "For button TEXT, expand CLASS at the current location.
533 Argument INDENT is the depth of indentation."
534 (cond ((string-match "+" text) ;we have to expand this file
535 (speedbar-change-expand-button-char ?-)
536 (speedbar-with-writable
538 (end-of-line) (forward-char 1)
539 (let ((subclasses (aref (class-v class) class-children)))
541 (eieio-class-button (car subclasses) (1+ indent))
542 (setq subclasses (cdr subclasses)))))))
543 ((string-match "-" text) ;we have to contract this node
544 (speedbar-change-expand-button-char ?+)
545 (speedbar-delete-subblock indent))
546 (t (error "Ooops... not sure what to do")))
547 (speedbar-center-buffer-smartly))
549 (defun eieio-describe-class-sb (text token indent)
550 "Describe the class TEXT in TOKEN.
551 INDENT is the current indentation level."
552 (speedbar-with-attached-buffer
553 (eieio-describe-class token))
554 (speedbar-maybee-jump-to-attached-frame))
558 ;;; eieio-opt.el ends here