Merge branch 'master' into comment-cache
[emacs.git] / lisp / emacs-lisp / eieio-opt.el
blobba4331f126b60fa77275071c54d013c1f36a34ab
1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2017 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)
35 ;;; Code:
36 ;;;###autoload
37 (defun eieio-browse (&optional root-class)
38 "Create an object browser window to show all objects.
39 If optional ROOT-CLASS, then start with that, otherwise start with
40 variable `eieio-default-superclass'."
41 (interactive (if current-prefix-arg
42 (list (read (completing-read "Class: "
43 (eieio-build-class-alist)
44 nil t)))
45 nil))
46 (if (not root-class) (setq root-class 'eieio-default-superclass))
47 (cl-check-type root-class class)
48 (display-buffer (get-buffer-create "*EIEIO OBJECT BROWSE*") t)
49 (with-current-buffer (get-buffer "*EIEIO OBJECT BROWSE*")
50 (erase-buffer)
51 (goto-char 0)
52 (eieio-browse-tree root-class "" "")
55 (defun eieio-browse-tree (this-root prefix ch-prefix)
56 "Recursively draw 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 (cl-check-type this-root class)
61 (let ((myname (symbol-name this-root))
62 (chl (eieio--class-children (cl--find-class this-root)))
63 (fprefix (concat ch-prefix " +--"))
64 (mprefix (concat ch-prefix " | "))
65 (lprefix (concat ch-prefix " ")))
66 (insert prefix myname "\n")
67 (while (cdr chl)
68 (eieio-browse-tree (car chl) fprefix mprefix)
69 (setq chl (cdr chl)))
70 (if chl
71 (eieio-browse-tree (car chl) fprefix lprefix))
74 ;;; CLASS COMPLETION / DOCUMENTATION
76 ;; Called via help-fns-describe-function-functions.
77 (declare-function help-fns-short-filename "help-fns" (filename))
79 ;;;###autoload
80 (define-obsolete-function-alias 'eieio-help-class 'cl--describe-class "25.1")
82 (defun eieio-build-class-alist (&optional class instantiable-only buildlist)
83 "Return an alist of all currently active classes for completion purposes.
84 Optional argument CLASS is the class to start with.
85 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
86 are not abstract, otherwise allow all classes.
87 Optional argument BUILDLIST is more list to attach and is used internally."
88 (let* ((cc (or class 'eieio-default-superclass))
89 (sublst (eieio--class-children (cl--find-class cc))))
90 (unless (assoc (symbol-name cc) buildlist)
91 (when (or (not instantiable-only) (not (class-abstract-p cc)))
92 ;; FIXME: Completion tables don't need alists, and ede/generic.el needs
93 ;; the symbols rather than their names.
94 (setq buildlist (cons (cons (symbol-name cc) 1) buildlist))))
95 (dolist (elem sublst)
96 (setq buildlist (eieio-build-class-alist
97 elem instantiable-only buildlist)))
98 buildlist))
100 (defvar eieio-read-class nil
101 "History of the function `eieio-read-class' prompt.")
103 (defun eieio-read-class (prompt &optional histvar instantiable-only)
104 "Return a class chosen by the user using PROMPT.
105 Optional argument HISTVAR is a variable to use as history.
106 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
107 are not abstract."
108 (intern (completing-read prompt (eieio-build-class-alist nil instantiable-only)
109 nil t nil
110 (or histvar 'eieio-read-class))))
112 (defun eieio-read-subclass (prompt class &optional histvar instantiable-only)
113 "Return a class chosen by the user using PROMPT.
114 CLASS is the base class, and completion occurs across all subclasses.
115 Optional argument HISTVAR is a variable to use as history.
116 If INSTANTIABLE-ONLY is non nil, only allow names of classes which
117 are not abstract."
118 (intern (completing-read prompt
119 (eieio-build-class-alist class instantiable-only)
120 nil t nil
121 (or histvar 'eieio-read-class))))
123 ;;; METHOD COMPLETION / DOC
126 ;;;###autoload
127 (defun eieio-help-constructor (ctr)
128 "Describe CTR if it is a class constructor."
129 (when (class-p ctr)
130 (erase-buffer)
131 (let ((location (find-lisp-object-file-name ctr 'define-type))
132 (def (symbol-function ctr)))
133 (goto-char (point-min))
134 (prin1 ctr)
135 (insert (format " is an %s object constructor function"
136 (if (autoloadp def)
137 "autoloaded"
138 "")))
139 (when (and (autoloadp def)
140 (null location))
141 (setq location
142 (find-lisp-object-file-name ctr def)))
143 (when location
144 (insert (substitute-command-keys " in `"))
145 (help-insert-xref-button
146 (help-fns-short-filename location)
147 'cl-type-definition ctr location 'define-type)
148 (insert (substitute-command-keys "'")))
149 (insert ".\nCreates an object of class " (symbol-name ctr) ".")
150 (goto-char (point-max))
151 (if (autoloadp def)
152 (insert "\n\n[Class description not available until class definition is loaded.]\n")
153 (save-excursion
154 (insert (propertize "\n\nClass description:\n" 'face 'bold))
155 (eieio-help-class ctr))
156 ))))
159 ;;; METHOD STATS
161 ;; Dump out statistics about all the active methods in a session.
162 (defun eieio-display-method-list ()
163 "Display a list of all the methods and what features are used."
164 (interactive)
165 (let* ((meth1 (cl-generic-all-functions))
166 (meth (sort meth1 (lambda (a b)
167 (string< (symbol-name a)
168 (symbol-name b)))))
169 (buff (get-buffer-create "*EIEIO Method List*"))
170 (methidx 0)
171 (standard-output buff)
172 (slots '(method-static
173 method-before
174 method-primary
175 method-after
176 method-generic-before
177 method-generic-primary
178 method-generic-after))
179 (slotn '("static"
180 "before"
181 "primary"
182 "after"
183 "G bef"
184 "G prim"
185 "G aft"))
186 (idxarray (make-vector (length slots) 0))
187 (primaryonly 0)
188 (oneprimary 0)
190 (switch-to-buffer-other-window buff)
191 (erase-buffer)
192 (dolist (S slotn)
193 (princ S)
194 (princ "\t")
196 (princ "Method Name")
197 (terpri)
198 (princ "--------------------------------------------------------------------")
199 (terpri)
200 (dolist (M meth)
201 (let ((mtree (get M 'eieio-method-tree))
202 (P nil) (numP)
203 (!P nil))
204 (dolist (S slots)
205 (let ((num (length (aref mtree (symbol-value S)))))
206 (aset idxarray (symbol-value S)
207 (+ num (aref idxarray (symbol-value S))))
208 (prin1 num)
209 (princ "\t")
210 (when (< 0 num)
211 (if (eq S 'method-primary)
212 (setq P t numP num)
213 (setq !P t)))
215 ;; Is this a primary-only impl method?
216 (when (and P (not !P))
217 (setq primaryonly (1+ primaryonly))
218 (when (= numP 1)
219 (setq oneprimary (1+ oneprimary))
220 (princ "*"))
221 (princ "* ")
223 (prin1 M)
224 (terpri)
225 (setq methidx (1+ methidx))
228 (princ "--------------------------------------------------------------------")
229 (terpri)
230 (dolist (S slots)
231 (prin1 (aref idxarray (symbol-value S)))
232 (princ "\t")
234 (prin1 methidx)
235 (princ " Total symbols")
236 (terpri)
237 (dolist (S slotn)
238 (princ S)
239 (princ "\t")
241 (terpri)
242 (terpri)
243 (princ "Methods Primary Only: ")
244 (prin1 primaryonly)
245 (princ "\t")
246 (princ (format "%d" (floor (* 100.0 primaryonly) methidx)))
247 (princ "% of total methods")
248 (terpri)
249 (princ "Only One Primary Impl: ")
250 (prin1 oneprimary)
251 (princ "\t")
252 (princ (format "%d" (floor (* 100.0 oneprimary) primaryonly)))
253 (princ "% of total primary methods")
254 (terpri)
257 ;;; SPEEDBAR SUPPORT
260 (defvar eieio-class-speedbar-key-map nil
261 "Keymap used when working with a project in speedbar.")
263 (defun eieio-class-speedbar-make-map ()
264 "Make a keymap for EIEIO under speedbar."
265 (setq eieio-class-speedbar-key-map (speedbar-make-specialized-keymap))
267 ;; General viewing stuff
268 (define-key eieio-class-speedbar-key-map "\C-m" 'speedbar-edit-line)
269 (define-key eieio-class-speedbar-key-map "+" 'speedbar-expand-line)
270 (define-key eieio-class-speedbar-key-map "-" 'speedbar-contract-line)
273 (if eieio-class-speedbar-key-map
275 (if (not (featurep 'speedbar))
276 (add-hook 'speedbar-load-hook (lambda ()
277 (eieio-class-speedbar-make-map)
278 (speedbar-add-expansion-list
279 '("EIEIO"
280 eieio-class-speedbar-menu
281 eieio-class-speedbar-key-map
282 eieio-class-speedbar))))
283 (eieio-class-speedbar-make-map)
284 (speedbar-add-expansion-list '("EIEIO"
285 eieio-class-speedbar-menu
286 eieio-class-speedbar-key-map
287 eieio-class-speedbar))))
289 (defvar eieio-class-speedbar-menu
291 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
293 (defun eieio-class-speedbar (_dir-or-object _depth)
294 "Create buttons in speedbar that represents the current project.
295 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
296 current expansion depth."
297 (when (eq (point-min) (point-max))
298 ;; This function is only called once, to start the whole deal.
299 ;; Create and expand the default object.
300 (eieio-class-button 'eieio-default-superclass 0)
301 (forward-line -1)
302 (speedbar-expand-line)))
304 (defun eieio-class-button (class depth)
305 "Draw a speedbar button at the current point for CLASS at DEPTH."
306 (cl-check-type class class)
307 (let ((subclasses (eieio--class-children (cl--find-class class))))
308 (if subclasses
309 (speedbar-make-tag-line 'angle ?+
310 'eieio-sb-expand
311 class
312 (symbol-name class)
313 'eieio-describe-class-sb
314 class
315 'speedbar-directory-face
316 depth)
317 (speedbar-make-tag-line 'angle ? nil nil
318 (symbol-name class)
319 'eieio-describe-class-sb
320 class
321 'speedbar-directory-face
322 depth))))
324 (defun eieio-sb-expand (text class indent)
325 "For button TEXT, expand CLASS at the current location.
326 Argument INDENT is the depth of indentation."
327 (cond ((string-match "+" text) ;we have to expand this file
328 (speedbar-change-expand-button-char ?-)
329 (speedbar-with-writable
330 (save-excursion
331 (end-of-line) (forward-char 1)
332 (let ((subclasses (eieio--class-children (cl--find-class class))))
333 (while subclasses
334 (eieio-class-button (car subclasses) (1+ indent))
335 (setq subclasses (cdr subclasses)))))))
336 ((string-match "-" text) ;we have to contract this node
337 (speedbar-change-expand-button-char ?+)
338 (speedbar-delete-subblock indent))
339 (t (error "Ooops... not sure what to do")))
340 (speedbar-center-buffer-smartly))
342 (defun eieio-describe-class-sb (_text token _indent)
343 "Describe the class TEXT in TOKEN.
344 INDENT is the current indentation level."
345 (dframe-with-attached-buffer
346 (describe-function token))
347 (dframe-maybee-jump-to-attached-frame))
349 (provide 'eieio-opt)
351 ;; Local variables:
352 ;; generated-autoload-file: "eieio-loaddefs.el"
353 ;; End:
355 ;;; eieio-opt.el ends here