Shrink EIEIO object header. Move generics to eieio-generic.el.
[emacs.git] / lisp / emacs-lisp / eieio-opt.el
blob13ad120a9b5a81bdcc9be173bef39eb96a6e4b67
1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2015 Free Software
4 ;; Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: OO, lisp
8 ;; Package: eieio
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/>.
25 ;;; Commentary:
27 ;; This contains support functions to eieio. These functions contain
28 ;; some small class browser and class printing functions.
31 (require 'eieio)
32 (require 'find-func)
33 (require 'speedbar)
34 (require 'help-mode)
36 ;;; Code:
37 ;;;###autoload
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)
45 nil t)))
46 nil))
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*")
51 (erase-buffer)
52 (goto-char 0)
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 (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")
68 (while (cdr chl)
69 (eieio-browse-tree (car chl) fprefix mprefix)
70 (setq chl (cdr chl)))
71 (if chl
72 (eieio-browse-tree (car chl) fprefix lprefix))
75 ;;; CLASS COMPLETION / DOCUMENTATION
77 ;;;###autoload
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."
81 ;; Header line
82 (prin1 class)
83 (insert " is a"
84 (if (eieio--class-option (eieio--class-v class) :abstract)
85 "n abstract"
86 "")
87 " class")
88 (let ((location (get class 'class-location)))
89 (when location
90 (insert " in `")
91 (help-insert-xref-button
92 (file-name-nondirectory location)
93 'eieio-class-def class location)
94 (insert "'")))
95 (insert ".\n")
96 ;; Parents
97 (let ((pl (eieio-class-parents class))
98 cur)
99 (when pl
100 (insert " Inherits from ")
101 (while (setq cur (pop pl))
102 (insert "`")
103 (help-insert-xref-button (symbol-name cur)
104 'help-function cur)
105 (insert (if pl "', " "'")))
106 (insert ".\n")))
107 ;; Children
108 (let ((ch (eieio-class-children class))
109 cur)
110 (when ch
111 (insert " Children ")
112 (while (setq cur (pop ch))
113 (insert "`")
114 (help-insert-xref-button (symbol-name cur)
115 'help-function cur)
116 (insert (if ch "', " "'")))
117 (insert ".\n")))
118 ;; System documentation
119 (let ((doc (documentation-property class 'variable-documentation)))
120 (when doc
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"])
127 counter doc)
128 (when methods
129 (insert (propertize "Specialized Methods:\n\n" 'face 'bold))
130 (while methods
131 (setq doc (eieio-method-documentation (car methods) class))
132 (insert "`")
133 (help-insert-xref-button (symbol-name (car methods))
134 'help-function (car methods))
135 (insert "'")
136 (if (not doc)
137 (insert " Undocumented")
138 (setq counter 0)
139 (dolist (cur doc)
140 (when cur
141 (insert " " (aref type counter) " "
142 (prin1-to-string (car cur) (current-buffer))
143 "\n"
144 (or (cdr cur) "")))
145 (setq counter (1+ counter))))
146 (insert "\n\n")
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 (eieio--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))
158 (i 0)
159 (prot (eieio--class-protection cv))
161 (insert (propertize "Instance Allocated Slots:\n\n"
162 'face 'bold))
163 (while names
164 (insert
165 (concat
166 (when (car prot)
167 (propertize "Private " 'face 'bold))
168 (propertize "Slot: " 'face 'bold)
169 (prin1-to-string (car names))
170 (unless (eq (aref types i) t)
171 (concat " type = "
172 (prin1-to-string (aref types i))))
173 (unless (eq (car deflt) eieio-unbound)
174 (concat " default = "
175 (prin1-to-string (car deflt))))
176 (when (car publp)
177 (concat " printer = "
178 (prin1-to-string (car publp))))
179 (when (car docs)
180 (concat "\n " (car docs) "\n"))
181 "\n"))
182 (setq names (cdr names)
183 docs (cdr docs)
184 deflt (cdr deflt)
185 publp (cdr publp)
186 prot (cdr prot)
187 i (1+ i)))
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))
193 (when names
194 (insert (propertize "\nClass Allocated Slots:\n\n" 'face 'bold)))
195 (while names
196 (insert
197 (concat
198 (when (car prot)
199 "Private ")
200 "Slot: "
201 (prin1-to-string (car names))
202 (unless (eq (aref types i) t)
203 (concat " type = "
204 (prin1-to-string (aref types i))))
205 (condition-case nil
206 (let ((value (eieio-oref class (car names))))
207 (concat " value = "
208 (prin1-to-string value)))
209 (error nil))
210 (when (car docs)
211 (concat "\n\n " (car docs) "\n"))
212 "\n"))
213 (setq names (cdr names)
214 docs (cdr docs)
215 prot (cdr prot)
216 i (1+ i)))))
218 (defun eieio-build-class-list (class)
219 "Return a list of all classes that inherit from CLASS."
220 (if (class-p class)
221 (cl-mapcan
222 (lambda (c)
223 (append (list c) (eieio-build-class-list c)))
224 (eieio--class-children (eieio--class-v class)))
225 (list class)))
227 (defun eieio-build-class-alist (&optional class instantiable-only buildlist)
228 "Return an alist of all currently active classes for completion purposes.
229 Optional argument CLASS is the class to start with.
230 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
231 are not abstract, otherwise allow all classes.
232 Optional argument BUILDLIST is more list to attach and is used internally."
233 (let* ((cc (or class 'eieio-default-superclass))
234 (sublst (eieio--class-children (eieio--class-v cc))))
235 (unless (assoc (symbol-name cc) buildlist)
236 (when (or (not instantiable-only) (not (class-abstract-p cc)))
237 ;; FIXME: Completion tables don't need alists, and ede/generic.el needs
238 ;; the symbols rather than their names.
239 (setq buildlist (cons (cons (symbol-name cc) 1) buildlist))))
240 (dolist (elem sublst)
241 (setq buildlist (eieio-build-class-alist
242 elem instantiable-only buildlist)))
243 buildlist))
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
252 are not abstract."
253 (intern (completing-read prompt (eieio-build-class-alist nil instantiable-only)
254 nil t nil
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
262 are not abstract."
263 (intern (completing-read prompt
264 (eieio-build-class-alist class instantiable-only)
265 nil t nil
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"))
282 ;;;###autoload
283 (defun eieio-help-constructor (ctr)
284 "Describe CTR if it is a class constructor."
285 (when (class-p ctr)
286 (erase-buffer)
287 (let ((location (get ctr 'class-location))
288 (def (symbol-function ctr)))
289 (goto-char (point-min))
290 (prin1 ctr)
291 (insert (format " is an %s object constructor function"
292 (if (autoloadp def)
293 "autoloaded"
294 "")))
295 (when (and (autoloadp def)
296 (null location))
297 (setq location
298 (find-lisp-object-file-name ctr def)))
299 (when location
300 (insert " in `")
301 (help-insert-xref-button
302 (file-name-nondirectory location)
303 'eieio-class-def ctr location)
304 (insert "'"))
305 (insert ".\nCreates an object of class " (symbol-name ctr) ".")
306 (goto-char (point-max))
307 (if (autoloadp def)
308 (insert "\n\n[Class description not available until class definition is loaded.]\n")
309 (save-excursion
310 (insert (propertize "\n\nClass description:\n" 'face 'bold))
311 (eieio-help-class ctr))
312 ))))
314 (defun eieio-all-generic-functions (&optional class)
315 "Return a list of all generic functions.
316 Optional CLASS argument returns only those functions that contain
317 methods for CLASS."
318 (let ((l nil))
319 (mapatoms
320 (lambda (symbol)
321 (let ((tree (get symbol 'eieio-method-hashtable)))
322 (when tree
323 ;; A symbol might be interned for that class in one of
324 ;; these three slots in the method-obarray.
325 (if (or (not class)
326 (car (gethash class (aref tree 0)))
327 (car (gethash class (aref tree 1)))
328 (car (gethash class (aref tree 2))))
329 (setq l (cons symbol l)))))))
332 (defun eieio-method-documentation (generic class)
333 "Return a list of the specific documentation of GENERIC for CLASS.
334 If there is not an explicit method for CLASS in GENERIC, or if that
335 function has no documentation, then return nil."
336 (let ((tree (get generic 'eieio-method-hashtable)))
337 (when tree
338 ;; A symbol might be interned for that class in one of
339 ;; these three slots in the method-hashtable.
340 ;; FIXME: Where do these 0/1/2 come from? Isn't 0 for :static,
341 ;; 1 for before, and 2 for primary (and 3 for after)?
342 (let ((before (car (gethash class (aref tree 0))))
343 (primary (car (gethash class (aref tree 1))))
344 (after (car (gethash class (aref tree 2)))))
345 (if (not (or before primary after))
347 (list (if before
348 (cons (help-function-arglist before)
349 (documentation before))
350 nil)
351 (if primary
352 (cons (help-function-arglist primary)
353 (documentation primary))
354 nil)
355 (if after
356 (cons (help-function-arglist after)
357 (documentation after))
358 nil)))))))
360 (defvar eieio-read-generic nil
361 "History of the `eieio-read-generic' prompt.")
363 (defun eieio-read-generic (prompt &optional historyvar)
364 "Read a generic function from the minibuffer with PROMPT.
365 Optional argument HISTORYVAR is the variable to use as history."
366 (intern (completing-read prompt obarray #'generic-p
367 t nil (or historyvar 'eieio-read-generic))))
369 ;;; METHOD STATS
371 ;; Dump out statistics about all the active methods in a session.
372 (defun eieio-display-method-list ()
373 "Display a list of all the methods and what features are used."
374 (interactive)
375 (let* ((meth1 (eieio-all-generic-functions))
376 (meth (sort meth1 (lambda (a b)
377 (string< (symbol-name a)
378 (symbol-name b)))))
379 (buff (get-buffer-create "*EIEIO Method List*"))
380 (methidx 0)
381 (standard-output buff)
382 (slots '(method-static
383 method-before
384 method-primary
385 method-after
386 method-generic-before
387 method-generic-primary
388 method-generic-after))
389 (slotn '("static"
390 "before"
391 "primary"
392 "after"
393 "G bef"
394 "G prim"
395 "G aft"))
396 (idxarray (make-vector (length slots) 0))
397 (primaryonly 0)
398 (oneprimary 0)
400 (switch-to-buffer-other-window buff)
401 (erase-buffer)
402 (dolist (S slotn)
403 (princ S)
404 (princ "\t")
406 (princ "Method Name")
407 (terpri)
408 (princ "--------------------------------------------------------------------")
409 (terpri)
410 (dolist (M meth)
411 (let ((mtree (get M 'eieio-method-tree))
412 (P nil) (numP)
413 (!P nil))
414 (dolist (S slots)
415 (let ((num (length (aref mtree (symbol-value S)))))
416 (aset idxarray (symbol-value S)
417 (+ num (aref idxarray (symbol-value S))))
418 (prin1 num)
419 (princ "\t")
420 (when (< 0 num)
421 (if (eq S 'method-primary)
422 (setq P t numP num)
423 (setq !P t)))
425 ;; Is this a primary-only impl method?
426 (when (and P (not !P))
427 (setq primaryonly (1+ primaryonly))
428 (when (= numP 1)
429 (setq oneprimary (1+ oneprimary))
430 (princ "*"))
431 (princ "* ")
433 (prin1 M)
434 (terpri)
435 (setq methidx (1+ methidx))
438 (princ "--------------------------------------------------------------------")
439 (terpri)
440 (dolist (S slots)
441 (prin1 (aref idxarray (symbol-value S)))
442 (princ "\t")
444 (prin1 methidx)
445 (princ " Total symbols")
446 (terpri)
447 (dolist (S slotn)
448 (princ S)
449 (princ "\t")
451 (terpri)
452 (terpri)
453 (princ "Methods Primary Only: ")
454 (prin1 primaryonly)
455 (princ "\t")
456 (princ (format "%d" (* (/ (float primaryonly) (float methidx)) 100)))
457 (princ "% of total methods")
458 (terpri)
459 (princ "Only One Primary Impl: ")
460 (prin1 oneprimary)
461 (princ "\t")
462 (princ (format "%d" (* (/ (float oneprimary) (float primaryonly)) 100)))
463 (princ "% of total primary methods")
464 (terpri)
467 ;;; HELP AUGMENTATION
469 (defun eieio-help-find-method-definition (class method file)
470 (let ((filename (find-library-name file))
471 location buf)
472 (when (symbolp class)
473 (setq class (symbol-name class)))
474 (when (symbolp method)
475 (setq method (symbol-name method)))
476 (when (null filename)
477 (error "Cannot find library %s" file))
478 (setq buf (find-file-noselect filename))
479 (with-current-buffer buf
480 (goto-char (point-min))
481 (when
482 (re-search-forward
483 ;; Regexp for searching methods.
484 (concat "(defmethod[ \t\r\n]+" method
485 "\\([ \t\r\n]+:[a-zA-Z]+\\)?"
486 "[ \t\r\n]+(\\s-*(\\(\\sw\\|\\s_\\)+\\s-+"
487 class
488 "\\s-*)")
489 nil t)
490 (setq location (match-beginning 0))))
491 (if (null location)
492 (message "Unable to find location in file")
493 (pop-to-buffer buf)
494 (goto-char location)
495 (recenter)
496 (beginning-of-line))))
498 (defun eieio-help-find-class-definition (class file)
499 (when (symbolp class)
500 (setq class (symbol-name class)))
501 (let ((filename (find-library-name file))
502 location buf)
503 (when (null filename)
504 (error "Cannot find library %s" file))
505 (setq buf (find-file-noselect filename))
506 (with-current-buffer buf
507 (goto-char (point-min))
508 (when
509 (re-search-forward
510 ;; Regexp for searching a class.
511 (concat "(defclass[ \t\r\n]+" class "[ \t\r\n]+")
512 nil t)
513 (setq location (match-beginning 0))))
514 (if (null location)
515 (message "Unable to find location in file")
516 (pop-to-buffer buf)
517 (goto-char location)
518 (recenter)
519 (beginning-of-line))))
521 ;;; SPEEDBAR SUPPORT
524 (defvar eieio-class-speedbar-key-map nil
525 "Keymap used when working with a project in speedbar.")
527 (defun eieio-class-speedbar-make-map ()
528 "Make a keymap for EIEIO under speedbar."
529 (setq eieio-class-speedbar-key-map (speedbar-make-specialized-keymap))
531 ;; General viewing stuff
532 (define-key eieio-class-speedbar-key-map "\C-m" 'speedbar-edit-line)
533 (define-key eieio-class-speedbar-key-map "+" 'speedbar-expand-line)
534 (define-key eieio-class-speedbar-key-map "-" 'speedbar-contract-line)
537 (if eieio-class-speedbar-key-map
539 (if (not (featurep 'speedbar))
540 (add-hook 'speedbar-load-hook (lambda ()
541 (eieio-class-speedbar-make-map)
542 (speedbar-add-expansion-list
543 '("EIEIO"
544 eieio-class-speedbar-menu
545 eieio-class-speedbar-key-map
546 eieio-class-speedbar))))
547 (eieio-class-speedbar-make-map)
548 (speedbar-add-expansion-list '("EIEIO"
549 eieio-class-speedbar-menu
550 eieio-class-speedbar-key-map
551 eieio-class-speedbar))))
553 (defvar eieio-class-speedbar-menu
555 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
557 (defun eieio-class-speedbar (_dir-or-object _depth)
558 "Create buttons in speedbar that represents the current project.
559 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
560 current expansion depth."
561 (when (eq (point-min) (point-max))
562 ;; This function is only called once, to start the whole deal.
563 ;; Create and expand the default object.
564 (eieio-class-button 'eieio-default-superclass 0)
565 (forward-line -1)
566 (speedbar-expand-line)))
568 (defun eieio-class-button (class depth)
569 "Draw a speedbar button at the current point for CLASS at DEPTH."
570 (eieio--check-type class-p class)
571 (let ((subclasses (eieio--class-children (eieio--class-v class))))
572 (if subclasses
573 (speedbar-make-tag-line 'angle ?+
574 'eieio-sb-expand
575 class
576 (symbol-name class)
577 'eieio-describe-class-sb
578 class
579 'speedbar-directory-face
580 depth)
581 (speedbar-make-tag-line 'angle ? nil nil
582 (symbol-name class)
583 'eieio-describe-class-sb
584 class
585 'speedbar-directory-face
586 depth))))
588 (defun eieio-sb-expand (text class indent)
589 "For button TEXT, expand CLASS at the current location.
590 Argument INDENT is the depth of indentation."
591 (cond ((string-match "+" text) ;we have to expand this file
592 (speedbar-change-expand-button-char ?-)
593 (speedbar-with-writable
594 (save-excursion
595 (end-of-line) (forward-char 1)
596 (let ((subclasses (eieio--class-children (eieio--class-v class))))
597 (while subclasses
598 (eieio-class-button (car subclasses) (1+ indent))
599 (setq subclasses (cdr subclasses)))))))
600 ((string-match "-" text) ;we have to contract this node
601 (speedbar-change-expand-button-char ?+)
602 (speedbar-delete-subblock indent))
603 (t (error "Ooops... not sure what to do")))
604 (speedbar-center-buffer-smartly))
606 (defun eieio-describe-class-sb (_text token _indent)
607 "Describe the class TEXT in TOKEN.
608 INDENT is the current indentation level."
609 (dframe-with-attached-buffer
610 (describe-function token))
611 (dframe-maybee-jump-to-attached-frame))
613 (provide 'eieio-opt)
615 ;; Local variables:
616 ;; generated-autoload-file: "eieio.el"
617 ;; End:
619 ;;; eieio-opt.el ends here