1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2014 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 (eieio--check-type 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 (eieio--check-type class-p this-root)
62 (let ((myname (symbol-name this-root))
63 (chl (eieio--class-children (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
78 (defun eieio-help-class (class)
79 "Print help description for CLASS.
80 If CLASS is actually an object, then also display current values of that object."
84 (if (class-option class :abstract)
88 (let ((location (get class 'class-location)))
91 (help-insert-xref-button
92 (file-name-nondirectory location)
93 'eieio-class-def class location)
97 (let ((pl (eieio-class-parents class))
100 (insert " Inherits from ")
101 (while (setq cur (pop pl))
103 (help-insert-xref-button (symbol-name cur)
105 (insert (if pl "', " "'")))
108 (let ((ch (eieio-class-children class))
111 (insert " Children ")
112 (while (setq cur (pop ch))
114 (help-insert-xref-button (symbol-name cur)
116 (insert (if ch "', " "'")))
118 ;; System documentation
119 (let ((doc (documentation-property class 'variable-documentation)))
121 (insert "\n" doc "\n\n")))
122 ;; Describe all the slots in this class.
123 (eieio-help-class-slots class)
124 ;; Describe all the methods specific to this class.
125 (let ((methods (eieio-all-generic-functions class))
126 (type [":STATIC" ":BEFORE" ":PRIMARY" ":AFTER"])
129 (insert (propertize "Specialized Methods:\n\n" 'face 'bold))
131 (setq doc (eieio-method-documentation (car methods) class))
133 (help-insert-xref-button (symbol-name (car methods))
134 'help-function (car methods))
137 (insert " Undocumented")
141 (insert " " (aref type counter) " "
142 (prin1-to-string (car cur) (current-buffer))
145 (setq counter (1+ counter))))
147 (setq methods (cdr methods))))))
149 (defun eieio-help-class-slots (class)
150 "Print help description for the slots in CLASS.
151 Outputs to the current buffer."
152 (let* ((cv (class-v class))
153 (docs (eieio--class-public-doc cv))
154 (names (eieio--class-public-a cv))
155 (deflt (eieio--class-public-d cv))
156 (types (eieio--class-public-type cv))
157 (publp (eieio--class-public-printer cv))
159 (prot (eieio--class-protection cv))
161 (insert (propertize "Instance Allocated Slots:\n\n"
167 (propertize "Private " 'face 'bold))
168 (propertize "Slot: " 'face 'bold)
169 (prin1-to-string (car names))
170 (unless (eq (aref types i) t)
172 (prin1-to-string (aref types i))))
173 (unless (eq (car deflt) eieio-unbound)
174 (concat " default = "
175 (prin1-to-string (car deflt))))
177 (concat " printer = "
178 (prin1-to-string (car publp))))
180 (concat "\n " (car docs) "\n"))
182 (setq names (cdr names)
188 (setq docs (eieio--class-class-allocation-doc cv)
189 names (eieio--class-class-allocation-a cv)
190 types (eieio--class-class-allocation-type cv)
192 prot (eieio--class-class-allocation-protection cv))
194 (insert (propertize "\nClass Allocated Slots:\n\n" 'face 'bold)))
201 (prin1-to-string (car names))
202 (unless (eq (aref types i) t)
204 (prin1-to-string (aref types i))))
206 (let ((value (eieio-oref class (car names))))
208 (prin1-to-string value)))
211 (concat "\n\n " (car docs) "\n"))
213 (setq names (cdr names)
218 (defun eieio-build-class-list (class)
219 "Return a list of all classes that inherit from CLASS."
224 (append (list c) (eieio-build-class-list c)))
225 (eieio-class-children-fast class)))
228 (defun eieio-build-class-alist (&optional class instantiable-only buildlist)
229 "Return an alist of all currently active classes for completion purposes.
230 Optional argument CLASS is the class to start with.
231 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
232 are not abstract, otherwise allow all classes.
233 Optional argument BUILDLIST is more list to attach and is used internally."
234 (let* ((cc (or class eieio-default-superclass))
235 (sublst (eieio--class-children (class-v cc))))
236 (unless (assoc (symbol-name cc) buildlist)
237 (when (or (not instantiable-only) (not (class-abstract-p cc)))
238 (setq buildlist (cons (cons (symbol-name cc) 1) buildlist))))
240 (setq buildlist (eieio-build-class-alist
241 (car sublst) instantiable-only buildlist))
242 (setq sublst (cdr sublst)))
245 (defvar eieio-read-class nil
246 "History of the function `eieio-read-class' prompt.")
248 (defun eieio-read-class (prompt &optional histvar instantiable-only)
249 "Return a class chosen by the user using PROMPT.
250 Optional argument HISTVAR is a variable to use as history.
251 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
253 (intern (completing-read prompt (eieio-build-class-alist nil instantiable-only)
255 (or histvar 'eieio-read-class))))
257 (defun eieio-read-subclass (prompt class &optional histvar instantiable-only)
258 "Return a class chosen by the user using PROMPT.
259 CLASS is the base class, and completion occurs across all subclasses.
260 Optional argument HISTVAR is a variable to use as history.
261 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
263 (intern (completing-read prompt
264 (eieio-build-class-alist class instantiable-only)
266 (or histvar 'eieio-read-class))))
268 ;;; METHOD COMPLETION / DOC
270 (define-button-type 'eieio-method-def
271 :supertype 'help-xref
272 'help-function (lambda (class method file)
273 (eieio-help-find-method-definition class method file))
274 'help-echo (purecopy "mouse-2, RET: find method's definition"))
276 (define-button-type 'eieio-class-def
277 :supertype 'help-xref
278 'help-function (lambda (class file)
279 (eieio-help-find-class-definition class file))
280 'help-echo (purecopy "mouse-2, RET: find class definition"))
283 (defun eieio-help-constructor (ctr)
284 "Describe CTR if it is a class constructor."
287 (let ((location (get ctr 'class-location))
288 (def (symbol-function ctr)))
289 (goto-char (point-min))
291 (insert (format " is an %s object constructor function"
295 (when (and (autoloadp def)
298 (find-lisp-object-file-name ctr def)))
301 (help-insert-xref-button
302 (file-name-nondirectory location)
303 'eieio-class-def ctr location)
305 (insert ".\nCreates an object of class " (symbol-name ctr) ".")
306 (goto-char (point-max))
308 (insert "\n\n[Class description not available until class definition is loaded.]\n")
310 (insert (propertize "\n\nClass description:\n" 'face 'bold))
311 (eieio-help-class ctr))
316 (defun eieio-help-generic (generic)
317 "Describe GENERIC if it is a generic function."
318 (when (and (symbolp generic) (generic-p generic))
320 (goto-char (point-min))
321 (when (re-search-forward " in `.+'.$" nil t)
322 (replace-match ".")))
324 (insert "\n\nThis is a generic function"
326 ((and (generic-primary-only-p generic)
327 (generic-primary-only-one-p generic))
328 " with only one primary method")
329 ((generic-primary-only-p generic)
330 " with only primary methods")
333 (insert (propertize "Implementations:\n\n" 'face 'bold))
335 (prefix [ ":STATIC" ":BEFORE" ":PRIMARY" ":AFTER" ] ))
336 ;; Loop over fanciful generics
338 (let ((gm (aref (get generic 'eieio-method-tree) i)))
341 (aref prefix (- i 3))
343 (or (nth 2 gm) "Undocumented")
347 ;; Loop over defined class-specific methods
349 (let* ((gm (reverse (aref (get generic 'eieio-method-tree) i)))
352 (setq cname (caar gm))
354 (help-insert-xref-button (symbol-name cname)
355 'help-variable cname)
356 (insert "' " (aref prefix i) " ")
358 (let* ((func (cdr (car gm)))
359 (arglst (help-function-arglist func)))
360 (prin1 arglst (current-buffer)))
362 (or (documentation (cdr (car gm)))
364 ;; Print file location if available
365 (when (and (setq location (get generic 'method-locations))
366 (setq location (assoc cname location)))
367 (setq location (cadr location))
368 (insert "\n\nDefined in `")
369 (help-insert-xref-button
370 (file-name-nondirectory location)
371 'eieio-method-def cname generic location)
377 (defun eieio-all-generic-functions (&optional class)
378 "Return a list of all generic functions.
379 Optional CLASS argument returns only those functions that contain
381 (let ((l nil) tree (cn (if class (symbol-name class) nil)))
384 (setq tree (get symbol 'eieio-method-obarray))
387 ;; A symbol might be interned for that class in one of
388 ;; these three slots in the method-obarray.
390 (fboundp (intern-soft cn (aref tree 0)))
391 (fboundp (intern-soft cn (aref tree 1)))
392 (fboundp (intern-soft cn (aref tree 2))))
393 (setq l (cons symbol l)))))))
396 (defun eieio-method-documentation (generic class)
397 "Return a list of the specific documentation of GENERIC for CLASS.
398 If there is not an explicit method for CLASS in GENERIC, or if that
399 function has no documentation, then return nil."
400 (let ((tree (get generic 'eieio-method-obarray))
401 (cn (symbol-name class))
402 before primary after)
405 ;; A symbol might be interned for that class in one of
406 ;; these three slots in the method-obarray.
407 (setq before (intern-soft cn (aref tree 0))
408 primary (intern-soft cn (aref tree 1))
409 after (intern-soft cn (aref tree 2)))
410 (if (not (or (fboundp before)
414 (list (if (fboundp before)
415 (cons (help-function-arglist before)
416 (documentation before))
418 (if (fboundp primary)
419 (cons (help-function-arglist primary)
420 (documentation primary))
423 (cons (help-function-arglist after)
424 (documentation after))
427 (defvar eieio-read-generic nil
428 "History of the `eieio-read-generic' prompt.")
430 (defun eieio-read-generic-p (fn)
431 "Function used in function `eieio-read-generic'.
432 This is because `generic-p' is a macro.
433 Argument FN is the function to test."
436 (defun eieio-read-generic (prompt &optional historyvar)
437 "Read a generic function from the minibuffer with PROMPT.
438 Optional argument HISTORYVAR is the variable to use as history."
439 (intern (completing-read prompt obarray 'eieio-read-generic-p
440 t nil (or historyvar 'eieio-read-generic))))
444 ;; Dump out statistics about all the active methods in a session.
445 (defun eieio-display-method-list ()
446 "Display a list of all the methods and what features are used."
448 (let* ((meth1 (eieio-all-generic-functions))
449 (meth (sort meth1 (lambda (a b)
450 (string< (symbol-name a)
452 (buff (get-buffer-create "*EIEIO Method List*"))
454 (standard-output buff)
455 (slots '(method-static
459 method-generic-before
460 method-generic-primary
461 method-generic-after))
469 (idxarray (make-vector (length slots) 0))
473 (switch-to-buffer-other-window buff)
479 (princ "Method Name")
481 (princ "--------------------------------------------------------------------")
484 (let ((mtree (get M 'eieio-method-tree))
488 (let ((num (length (aref mtree (symbol-value S)))))
489 (aset idxarray (symbol-value S)
490 (+ num (aref idxarray (symbol-value S))))
494 (if (eq S 'method-primary)
498 ;; Is this a primary-only impl method?
499 (when (and P (not !P))
500 (setq primaryonly (1+ primaryonly))
502 (setq oneprimary (1+ oneprimary))
508 (setq methidx (1+ methidx))
511 (princ "--------------------------------------------------------------------")
514 (prin1 (aref idxarray (symbol-value S)))
518 (princ " Total symbols")
526 (princ "Methods Primary Only: ")
529 (princ (format "%d" (* (/ (float primaryonly) (float methidx)) 100)))
530 (princ "% of total methods")
532 (princ "Only One Primary Impl: ")
535 (princ (format "%d" (* (/ (float oneprimary) (float primaryonly)) 100)))
536 (princ "% of total primary methods")
540 ;;; HELP AUGMENTATION
542 (defun eieio-help-find-method-definition (class method file)
543 (let ((filename (find-library-name file))
545 (when (symbolp class)
546 (setq class (symbol-name class)))
547 (when (symbolp method)
548 (setq method (symbol-name method)))
549 (when (null filename)
550 (error "Cannot find library %s" file))
551 (setq buf (find-file-noselect filename))
552 (with-current-buffer buf
553 (goto-char (point-min))
556 ;; Regexp for searching methods.
557 (concat "(defmethod[ \t\r\n]+" method
558 "\\([ \t\r\n]+:[a-zA-Z]+\\)?"
559 "[ \t\r\n]+(\\s-*(\\(\\sw\\|\\s_\\)+\\s-+"
563 (setq location (match-beginning 0))))
565 (message "Unable to find location in file")
569 (beginning-of-line))))
571 (defun eieio-help-find-class-definition (class file)
572 (when (symbolp class)
573 (setq class (symbol-name class)))
574 (let ((filename (find-library-name file))
576 (when (null filename)
577 (error "Cannot find library %s" file))
578 (setq buf (find-file-noselect filename))
579 (with-current-buffer buf
580 (goto-char (point-min))
583 ;; Regexp for searching a class.
584 (concat "(defclass[ \t\r\n]+" class "[ \t\r\n]+")
586 (setq location (match-beginning 0))))
588 (message "Unable to find location in file")
592 (beginning-of-line))))
597 (defvar eieio-class-speedbar-key-map nil
598 "Keymap used when working with a project in speedbar.")
600 (defun eieio-class-speedbar-make-map ()
601 "Make a keymap for EIEIO under speedbar."
602 (setq eieio-class-speedbar-key-map (speedbar-make-specialized-keymap))
604 ;; General viewing stuff
605 (define-key eieio-class-speedbar-key-map "\C-m" 'speedbar-edit-line)
606 (define-key eieio-class-speedbar-key-map "+" 'speedbar-expand-line)
607 (define-key eieio-class-speedbar-key-map "-" 'speedbar-contract-line)
610 (if eieio-class-speedbar-key-map
612 (if (not (featurep 'speedbar))
613 (add-hook 'speedbar-load-hook (lambda ()
614 (eieio-class-speedbar-make-map)
615 (speedbar-add-expansion-list
617 eieio-class-speedbar-menu
618 eieio-class-speedbar-key-map
619 eieio-class-speedbar))))
620 (eieio-class-speedbar-make-map)
621 (speedbar-add-expansion-list '("EIEIO"
622 eieio-class-speedbar-menu
623 eieio-class-speedbar-key-map
624 eieio-class-speedbar))))
626 (defvar eieio-class-speedbar-menu
628 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
630 (defun eieio-class-speedbar (dir-or-object depth)
631 "Create buttons in speedbar that represents the current project.
632 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
633 current expansion depth."
634 (when (eq (point-min) (point-max))
635 ;; This function is only called once, to start the whole deal.
636 ;; Create and expand the default object.
637 (eieio-class-button eieio-default-superclass 0)
639 (speedbar-expand-line)))
641 (defun eieio-class-button (class depth)
642 "Draw a speedbar button at the current point for CLASS at DEPTH."
643 (eieio--check-type class-p class)
644 (let ((subclasses (eieio--class-children (class-v class))))
646 (speedbar-make-tag-line 'angle ?+
650 'eieio-describe-class-sb
652 'speedbar-directory-face
654 (speedbar-make-tag-line 'angle ? nil nil
656 'eieio-describe-class-sb
658 'speedbar-directory-face
661 (defun eieio-sb-expand (text class indent)
662 "For button TEXT, expand CLASS at the current location.
663 Argument INDENT is the depth of indentation."
664 (cond ((string-match "+" text) ;we have to expand this file
665 (speedbar-change-expand-button-char ?-)
666 (speedbar-with-writable
668 (end-of-line) (forward-char 1)
669 (let ((subclasses (eieio--class-children (class-v class))))
671 (eieio-class-button (car subclasses) (1+ indent))
672 (setq subclasses (cdr subclasses)))))))
673 ((string-match "-" text) ;we have to contract this node
674 (speedbar-change-expand-button-char ?+)
675 (speedbar-delete-subblock indent))
676 (t (error "Ooops... not sure what to do")))
677 (speedbar-center-buffer-smartly))
679 (defun eieio-describe-class-sb (text token indent)
680 "Describe the class TEXT in TOKEN.
681 INDENT is the current indentation level."
682 (dframe-with-attached-buffer
683 (describe-function token))
684 (dframe-maybee-jump-to-attached-frame))
689 ;; generated-autoload-file: "eieio.el"
692 ;;; eieio-opt.el ends here