Update Unicode data files to version 11.0.0 of Unicode
[emacs.git] / lisp / emacs-lisp / eieio-opt.el
blobeeb0bc95d68a51fe8e3721197417def1d77d43c8
1 ;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)
3 ;; Copyright (C) 1996, 1998-2003, 2005, 2008-2018 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 <https://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 ;; The `cl-type-definition' button type can't be autoloaded
146 ;; due to circularity during bootstrap (Bug#28899).
147 (require 'cl-extra)
148 (help-insert-xref-button
149 (help-fns-short-filename location)
150 'cl-type-definition ctr location 'define-type)
151 (insert (substitute-command-keys "'")))
152 (insert ".\nCreates an object of class " (symbol-name ctr) ".")
153 (goto-char (point-max))
154 (if (autoloadp def)
155 (insert "\n\n[Class description not available until class definition is loaded.]\n")
156 (save-excursion
157 (insert (propertize "\n\nClass description:\n" 'face 'bold))
158 (eieio-help-class ctr))
159 ))))
162 ;;; METHOD STATS
164 ;; Dump out statistics about all the active methods in a session.
165 (defun eieio-display-method-list ()
166 "Display a list of all the methods and what features are used."
167 (interactive)
168 (let* ((meth1 (cl-generic-all-functions))
169 (meth (sort meth1 (lambda (a b)
170 (string< (symbol-name a)
171 (symbol-name b)))))
172 (buff (get-buffer-create "*EIEIO Method List*"))
173 (methidx 0)
174 (standard-output buff)
175 (slots '(method-static
176 method-before
177 method-primary
178 method-after
179 method-generic-before
180 method-generic-primary
181 method-generic-after))
182 (slotn '("static"
183 "before"
184 "primary"
185 "after"
186 "G bef"
187 "G prim"
188 "G aft"))
189 (idxarray (make-vector (length slots) 0))
190 (primaryonly 0)
191 (oneprimary 0)
193 (switch-to-buffer-other-window buff)
194 (erase-buffer)
195 (dolist (S slotn)
196 (princ S)
197 (princ "\t")
199 (princ "Method Name")
200 (terpri)
201 (princ "--------------------------------------------------------------------")
202 (terpri)
203 (dolist (M meth)
204 (let ((mtree (get M 'eieio-method-tree))
205 (P nil) (numP)
206 (!P nil))
207 (dolist (S slots)
208 (let ((num (length (aref mtree (symbol-value S)))))
209 (aset idxarray (symbol-value S)
210 (+ num (aref idxarray (symbol-value S))))
211 (prin1 num)
212 (princ "\t")
213 (when (< 0 num)
214 (if (eq S 'method-primary)
215 (setq P t numP num)
216 (setq !P t)))
218 ;; Is this a primary-only impl method?
219 (when (and P (not !P))
220 (setq primaryonly (1+ primaryonly))
221 (when (= numP 1)
222 (setq oneprimary (1+ oneprimary))
223 (princ "*"))
224 (princ "* ")
226 (prin1 M)
227 (terpri)
228 (setq methidx (1+ methidx))
231 (princ "--------------------------------------------------------------------")
232 (terpri)
233 (dolist (S slots)
234 (prin1 (aref idxarray (symbol-value S)))
235 (princ "\t")
237 (prin1 methidx)
238 (princ " Total symbols")
239 (terpri)
240 (dolist (S slotn)
241 (princ S)
242 (princ "\t")
244 (terpri)
245 (terpri)
246 (princ "Methods Primary Only: ")
247 (prin1 primaryonly)
248 (princ "\t")
249 (princ (format "%d" (floor (* 100.0 primaryonly) methidx)))
250 (princ "% of total methods")
251 (terpri)
252 (princ "Only One Primary Impl: ")
253 (prin1 oneprimary)
254 (princ "\t")
255 (princ (format "%d" (floor (* 100.0 oneprimary) primaryonly)))
256 (princ "% of total primary methods")
257 (terpri)
260 ;;; SPEEDBAR SUPPORT
263 (defvar eieio-class-speedbar-key-map nil
264 "Keymap used when working with a project in speedbar.")
266 (defun eieio-class-speedbar-make-map ()
267 "Make a keymap for EIEIO under speedbar."
268 (setq eieio-class-speedbar-key-map (speedbar-make-specialized-keymap))
270 ;; General viewing stuff
271 (define-key eieio-class-speedbar-key-map "\C-m" 'speedbar-edit-line)
272 (define-key eieio-class-speedbar-key-map "+" 'speedbar-expand-line)
273 (define-key eieio-class-speedbar-key-map "-" 'speedbar-contract-line)
276 (if eieio-class-speedbar-key-map
278 (if (not (featurep 'speedbar))
279 (add-hook 'speedbar-load-hook (lambda ()
280 (eieio-class-speedbar-make-map)
281 (speedbar-add-expansion-list
282 '("EIEIO"
283 eieio-class-speedbar-menu
284 eieio-class-speedbar-key-map
285 eieio-class-speedbar))))
286 (eieio-class-speedbar-make-map)
287 (speedbar-add-expansion-list '("EIEIO"
288 eieio-class-speedbar-menu
289 eieio-class-speedbar-key-map
290 eieio-class-speedbar))))
292 (defvar eieio-class-speedbar-menu
294 "Menu part in easymenu format used in speedbar while in `eieio' mode.")
296 (defun eieio-class-speedbar (_dir-or-object _depth)
297 "Create buttons in speedbar that represents the current project.
298 DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
299 current expansion depth."
300 (when (eq (point-min) (point-max))
301 ;; This function is only called once, to start the whole deal.
302 ;; Create and expand the default object.
303 (eieio-class-button 'eieio-default-superclass 0)
304 (forward-line -1)
305 (speedbar-expand-line)))
307 (defun eieio-class-button (class depth)
308 "Draw a speedbar button at the current point for CLASS at DEPTH."
309 (cl-check-type class class)
310 (let ((subclasses (eieio--class-children (cl--find-class class))))
311 (if subclasses
312 (speedbar-make-tag-line 'angle ?+
313 'eieio-sb-expand
314 class
315 (symbol-name class)
316 'eieio-describe-class-sb
317 class
318 'speedbar-directory-face
319 depth)
320 (speedbar-make-tag-line 'angle ? nil nil
321 (symbol-name class)
322 'eieio-describe-class-sb
323 class
324 'speedbar-directory-face
325 depth))))
327 (defun eieio-sb-expand (text class indent)
328 "For button TEXT, expand CLASS at the current location.
329 Argument INDENT is the depth of indentation."
330 (cond ((string-match "+" text) ;we have to expand this file
331 (speedbar-change-expand-button-char ?-)
332 (speedbar-with-writable
333 (save-excursion
334 (end-of-line) (forward-char 1)
335 (let ((subclasses (eieio--class-children (cl--find-class class))))
336 (while subclasses
337 (eieio-class-button (car subclasses) (1+ indent))
338 (setq subclasses (cdr subclasses)))))))
339 ((string-match "-" text) ;we have to contract this node
340 (speedbar-change-expand-button-char ?+)
341 (speedbar-delete-subblock indent))
342 (t (error "Ooops... not sure what to do")))
343 (speedbar-center-buffer-smartly))
345 (defun eieio-describe-class-sb (_text token _indent)
346 "Describe the class TEXT in TOKEN.
347 INDENT is the current indentation level."
348 (dframe-with-attached-buffer
349 (describe-function token))
350 (dframe-maybee-jump-to-attached-frame))
352 (provide 'eieio-opt)
354 ;; Local variables:
355 ;; generated-autoload-file: "eieio-loaddefs.el"
356 ;; End:
358 ;;; eieio-opt.el ends here