1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2015 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 (cl-check-type root-class 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 (cl-check-type this-root class)
62 (let ((myname (symbol-name this-root))
63 (chl (eieio--class-children (eieio--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 (eieio--class-option (eieio--class-v class) :abstract)
88 (let ((location (find-lisp-object-file-name class 'eieio-defclass)))
91 (help-insert-xref-button
92 (help-fns-short-filename location)
93 'eieio-class-def class location 'eieio-defclass)
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 ((generics (eieio-all-generic-functions class)))
127 (insert (propertize "Specialized Methods:\n\n" 'face 'bold))
128 (dolist (generic generics)
130 (help-insert-xref-button (symbol-name generic) 'help-function generic)
132 (pcase-dolist (`(,qualifiers ,args ,doc)
133 (eieio-method-documentation generic class))
134 (insert (format " %s%S\n" qualifiers args)
138 (defun eieio-help-class-slots (class)
139 "Print help description for the slots in CLASS.
140 Outputs to the current buffer."
141 (let* ((cv (eieio--class-v class))
142 (docs (eieio--class-public-doc cv))
143 (names (eieio--class-public-a cv))
144 (deflt (eieio--class-public-d cv))
145 (types (eieio--class-public-type cv))
146 (publp (eieio--class-public-printer cv))
148 (prot (eieio--class-protection cv))
150 (insert (propertize "Instance Allocated Slots:\n\n"
156 (propertize "Private " 'face 'bold))
157 (propertize "Slot: " 'face 'bold)
158 (prin1-to-string (car names))
159 (unless (eq (aref types i) t)
161 (prin1-to-string (aref types i))))
162 (unless (eq (car deflt) eieio-unbound)
163 (concat " default = "
164 (prin1-to-string (car deflt))))
166 (concat " printer = "
167 (prin1-to-string (car publp))))
169 (concat "\n " (car docs) "\n"))
171 (setq names (cdr names)
177 (setq docs (eieio--class-class-allocation-doc cv)
178 names (eieio--class-class-allocation-a cv)
179 types (eieio--class-class-allocation-type cv)
181 prot (eieio--class-class-allocation-protection cv))
183 (insert (propertize "\nClass Allocated Slots:\n\n" 'face 'bold)))
190 (prin1-to-string (car names))
191 (unless (eq (aref types i) t)
193 (prin1-to-string (aref types i))))
195 (let ((value (eieio-oref class (car names))))
197 (prin1-to-string value)))
200 (concat "\n\n " (car docs) "\n"))
202 (setq names (cdr names)
207 (defun eieio-build-class-alist (&optional class instantiable-only buildlist)
208 "Return an alist of all currently active classes for completion purposes.
209 Optional argument CLASS is the class to start with.
210 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
211 are not abstract, otherwise allow all classes.
212 Optional argument BUILDLIST is more list to attach and is used internally."
213 (let* ((cc (or class 'eieio-default-superclass))
214 (sublst (eieio--class-children (eieio--class-v cc))))
215 (unless (assoc (symbol-name cc) buildlist)
216 (when (or (not instantiable-only) (not (class-abstract-p cc)))
217 ;; FIXME: Completion tables don't need alists, and ede/generic.el needs
218 ;; the symbols rather than their names.
219 (setq buildlist (cons (cons (symbol-name cc) 1) buildlist))))
220 (dolist (elem sublst)
221 (setq buildlist (eieio-build-class-alist
222 elem instantiable-only buildlist)))
225 (defvar eieio-read-class nil
226 "History of the function `eieio-read-class' prompt.")
228 (defun eieio-read-class (prompt &optional histvar instantiable-only)
229 "Return a class chosen by the user using PROMPT.
230 Optional argument HISTVAR is a variable to use as history.
231 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
233 (intern (completing-read prompt (eieio-build-class-alist nil instantiable-only)
235 (or histvar 'eieio-read-class))))
237 (defun eieio-read-subclass (prompt class &optional histvar instantiable-only)
238 "Return a class chosen by the user using PROMPT.
239 CLASS is the base class, and completion occurs across all subclasses.
240 Optional argument HISTVAR is a variable to use as history.
241 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
243 (intern (completing-read prompt
244 (eieio-build-class-alist class instantiable-only)
246 (or histvar 'eieio-read-class))))
248 ;;; METHOD COMPLETION / DOC
250 (define-button-type 'eieio-class-def
251 :supertype 'help-function-def
252 'help-echo (purecopy "mouse-2, RET: find class definition"))
254 (defconst eieio--defclass-regexp "(defclass[ \t\r\n]+%s[ \t\r\n]+")
255 (with-eval-after-load 'find-func
256 (defvar find-function-regexp-alist)
257 (add-to-list 'find-function-regexp-alist
258 `(eieio-defclass . eieio--defclass-regexp)))
261 (defun eieio-help-constructor (ctr)
262 "Describe CTR if it is a class constructor."
265 (let ((location (find-lisp-object-file-name ctr 'eieio-defclass))
266 (def (symbol-function ctr)))
267 (goto-char (point-min))
269 (insert (format " is an %s object constructor function"
273 (when (and (autoloadp def)
276 (find-lisp-object-file-name ctr def)))
279 (help-insert-xref-button
280 (help-fns-short-filename location)
281 'eieio-class-def ctr location 'eieio-defclass)
283 (insert ".\nCreates an object of class " (symbol-name ctr) ".")
284 (goto-char (point-max))
286 (insert "\n\n[Class description not available until class definition is loaded.]\n")
288 (insert (propertize "\n\nClass description:\n" 'face 'bold))
289 (eieio-help-class ctr))
292 (defun eieio--specializers-apply-to-class-p (specializers class)
293 "Return non-nil if a method with SPECIALIZERS applies to CLASS."
295 (dolist (specializer specializers)
296 (if (memq (car-safe specializer) '(subclass eieio--static))
297 (setq specializer (nth 1 specializer)))
298 ;; Don't include the methods that are "too generic", such as those
299 ;; applying to `eieio-default-superclass'.
300 (and (not (memq specializer '(t eieio-default-superclass)))
301 (class-p specializer)
302 (child-of-class-p class specializer)
306 (defun eieio-all-generic-functions (&optional class)
307 "Return a list of all generic functions.
308 Optional CLASS argument returns only those functions that contain
313 (let ((generic (and (fboundp symbol) (cl--generic symbol))))
316 (if (null class) (throw 'found t))
317 (dolist (method (cl--generic-method-table generic))
318 (if (eieio--specializers-apply-to-class-p
319 (cl--generic-method-specializers method) class)
324 (defun eieio-method-documentation (generic class)
325 "Return info for all methods of GENERIC applicable to CLASS.
326 The value returned is a list of elements of the form
327 \(QUALIFIERS ARGS DOC)."
328 (let ((generic (cl--generic generic))
331 (dolist (method (cl--generic-method-table generic))
332 (when (eieio--specializers-apply-to-class-p
333 (cl--generic-method-specializers method) class)
334 (push (cl--generic-method-info method) docs))))
339 ;; Dump out statistics about all the active methods in a session.
340 (defun eieio-display-method-list ()
341 "Display a list of all the methods and what features are used."
343 (let* ((meth1 (eieio-all-generic-functions))
344 (meth (sort meth1 (lambda (a b)
345 (string< (symbol-name a)
347 (buff (get-buffer-create "*EIEIO Method List*"))
349 (standard-output buff)
350 (slots '(method-static
354 method-generic-before
355 method-generic-primary
356 method-generic-after))
364 (idxarray (make-vector (length slots) 0))
368 (switch-to-buffer-other-window buff)
374 (princ "Method Name")
376 (princ "--------------------------------------------------------------------")
379 (let ((mtree (get M 'eieio-method-tree))
383 (let ((num (length (aref mtree (symbol-value S)))))
384 (aset idxarray (symbol-value S)
385 (+ num (aref idxarray (symbol-value S))))
389 (if (eq S 'method-primary)
393 ;; Is this a primary-only impl method?
394 (when (and P (not !P))
395 (setq primaryonly (1+ primaryonly))
397 (setq oneprimary (1+ oneprimary))
403 (setq methidx (1+ methidx))
406 (princ "--------------------------------------------------------------------")
409 (prin1 (aref idxarray (symbol-value S)))
413 (princ " Total symbols")
421 (princ "Methods Primary Only: ")
424 (princ (format "%d" (* (/ (float primaryonly) (float methidx)) 100)))
425 (princ "% of total methods")
427 (princ "Only One Primary Impl: ")
430 (princ (format "%d" (* (/ (float oneprimary) (float primaryonly)) 100)))
431 (princ "% of total primary methods")
438 (defvar eieio-class-speedbar-key-map nil
439 "Keymap used when working with a project in speedbar.")
441 (defun eieio-class-speedbar-make-map ()
442 "Make a keymap for EIEIO under speedbar."
443 (setq eieio-class-speedbar-key-map (speedbar-make-specialized-keymap))
445 ;; General viewing stuff
446 (define-key eieio-class-speedbar-key-map "\C-m" 'speedbar-edit-line)
447 (define-key eieio-class-speedbar-key-map "+" 'speedbar-expand-line)
448 (define-key eieio-class-speedbar-key-map "-" 'speedbar-contract-line)
451 (if eieio-class-speedbar-key-map
453 (if (not (featurep 'speedbar))
454 (add-hook 'speedbar-load-hook (lambda ()
455 (eieio-class-speedbar-make-map)
456 (speedbar-add-expansion-list
458 eieio-class-speedbar-menu
459 eieio-class-speedbar-key-map
460 eieio-class-speedbar))))
461 (eieio-class-speedbar-make-map)
462 (speedbar-add-expansion-list '("EIEIO"
463 eieio-class-speedbar-menu
464 eieio-class-speedbar-key-map
465 eieio-class-speedbar))))
467 (defvar eieio-class-speedbar-menu
469 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
471 (defun eieio-class-speedbar (_dir-or-object _depth)
472 "Create buttons in speedbar that represents the current project.
473 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
474 current expansion depth."
475 (when (eq (point-min) (point-max))
476 ;; This function is only called once, to start the whole deal.
477 ;; Create and expand the default object.
478 (eieio-class-button 'eieio-default-superclass 0)
480 (speedbar-expand-line)))
482 (defun eieio-class-button (class depth)
483 "Draw a speedbar button at the current point for CLASS at DEPTH."
484 (cl-check-type class class)
485 (let ((subclasses (eieio--class-children (eieio--class-v class))))
487 (speedbar-make-tag-line 'angle ?+
491 'eieio-describe-class-sb
493 'speedbar-directory-face
495 (speedbar-make-tag-line 'angle ? nil nil
497 'eieio-describe-class-sb
499 'speedbar-directory-face
502 (defun eieio-sb-expand (text class indent)
503 "For button TEXT, expand CLASS at the current location.
504 Argument INDENT is the depth of indentation."
505 (cond ((string-match "+" text) ;we have to expand this file
506 (speedbar-change-expand-button-char ?-)
507 (speedbar-with-writable
509 (end-of-line) (forward-char 1)
510 (let ((subclasses (eieio--class-children (eieio--class-v class))))
512 (eieio-class-button (car subclasses) (1+ indent))
513 (setq subclasses (cdr subclasses)))))))
514 ((string-match "-" text) ;we have to contract this node
515 (speedbar-change-expand-button-char ?+)
516 (speedbar-delete-subblock indent))
517 (t (error "Ooops... not sure what to do")))
518 (speedbar-center-buffer-smartly))
520 (defun eieio-describe-class-sb (_text token _indent)
521 "Describe the class TEXT in TOKEN.
522 INDENT is the current indentation level."
523 (dframe-with-attached-buffer
524 (describe-function token))
525 (dframe-maybee-jump-to-attached-frame))
530 ;; generated-autoload-file: "eieio.el"
533 ;;; eieio-opt.el ends here