forked from slyrus/mcclim-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
describe.lisp
172 lines (154 loc) · 7.11 KB
/
describe.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
;;; -*- Mode: Lisp; Package: COMMON-LISP -*-
;;; (c) copyright 2002 by Michael McDonald ([email protected])
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
(in-package :clim-lisp)
(defun describe (thing &optional stream)
(if (null stream)
(setq stream *standard-output*)
(if (eq stream t)
(setq stream *terminal-io*)))
(describe-object thing stream)
(values))
(defgeneric describe-object (thing stream))
;;; For these methods, stream should be of type
;;; (or EXTENDED-OUTPUT-STREAM OUTPUT-RECORDING-STREAM)
;;; but CLIM-STREAM-PANE is used instead.
(clim-internals::with-system-redefinition-allowed
(defmethod describe-object ((thing t) stream)
(let ((*print-array* nil))
(clim:present thing (clim:presentation-type-of thing)
:stream stream)
(format stream " is of type ")
(clim:present (type-of thing) (clim:presentation-type-of (type-of thing))
:stream stream)
(terpri stream)))
)
(defmethod describe-object ((thing symbol) stream)
(clim:present thing (clim:presentation-type-of thing)
:stream stream)
(format stream " is of type ")
(clim:present (type-of thing) (clim:presentation-type-of (type-of thing))
:stream stream)
(terpri stream)
(cond
((not (boundp thing))
(format stream " it is unbound~%"))
(t
(format stream " it has a value of ")
(clim:present (symbol-value thing) (clim:presentation-type-of (symbol-value thing))
:stream stream)
(terpri)))
(format stream " it is in the ")
(clim:present (symbol-package thing) (clim:presentation-type-of (symbol-package thing))
:stream stream)
(format stream " package~%")
(when (fboundp thing)
(format stream " it has a function definition of ~S~%" (symbol-function thing))
(format stream " which has the argument list ")
(let ((arglist #+excl (excl:arglist (symbol-function thing))
#+cmu (kernel:%function-arglist (symbol-function thing))
#+sbcl (sb-kernel:%simple-fun-arglist (symbol-function thing))
#+clisp (ext:arglist (symbol-function thing))
#-(or excl cmu sbcl clisp) "( ??? )"))
(when arglist
(clim:present arglist
(clim:presentation-type-of arglist)
:stream stream)))
(terpri))
(format stream " it has a property list of ~S~%" (symbol-plist thing)))
(defmethod describe-object ((thing number) stream)
(clim:present thing (clim:presentation-type-of thing)
:stream stream)
(format stream " is a number of type ")
(clim:present (type-of thing) (clim:presentation-type-of (type-of thing))
:stream stream)
(terpri stream))
(defmethod describe-object ((thing string) stream)
(clim:present thing (clim:presentation-type-of thing)
:stream stream)
(format stream " is of type ")
(clim:present (type-of thing) (clim:presentation-type-of (type-of thing))
:stream stream)
(format stream " with a length of ")
(clim:present (length thing) 'clim:integer
:stream stream)
(terpri stream))
(defmethod describe-object ((thing package) stream)
(clim:present thing (clim:presentation-type-of thing)
:stream stream)
(format stream " is a package named ")
(clim:present (package-name thing) (clim:presentation-type-of (package-name thing))
:stream stream)
(terpri stream)
(format stream " it has the nicknames of ")
(clim:present (package-nicknames thing) 'clim:expression
:stream stream)
(terpri stream)
(format stream " it uses these packages: ")
(clim:present (package-use-list thing) 'clim:expression
:stream stream)
(terpri stream)
(format stream " it is used by the packages: ")
(clim:present (package-used-by-list thing) 'clim:expression
:stream stream)
(terpri stream))
(labels ((present-instance-slots-text (thing stream)
(let* ((slots (clim-mop:class-slots (class-of thing)))
(width (loop for slot in slots
maximizing (length (symbol-name (clim-mop:slot-definition-name slot))))))
(loop for slot in slots
do (cond
((slot-boundp thing (clim-mop:slot-definition-name slot))
(format stream " ~v@A: " width
(clim-mop:slot-definition-name slot))
(clim:present (slot-value thing (clim-mop:slot-definition-name slot))
'clim:expression
:stream stream)
(terpri stream))
(t
(format stream " ~v@A: <unbound>~%" width
(clim-mop:slot-definition-name slot)))))))
(present-instance-slots-clim (thing stream)
(let ((slots (clim-mop:class-slots (class-of thing))))
(clim:formatting-table (stream)
(dolist (slot slots)
(clim:formatting-row (stream)
(clim:formatting-cell (stream :align-x :right)
(clim:present (clim-mop:slot-definition-name slot)
'clim:symbol
:stream stream)
(write-char #\: stream))
(clim:formatting-cell (stream)
(if (slot-boundp thing (clim-mop:slot-definition-name slot))
(clim:present (slot-value thing (clim-mop:slot-definition-name slot))
'clim:expression
:stream stream)
(format stream "<unbound>"))))))))
(describe-instance (thing a-what stream)
(clim:present thing (clim:presentation-type-of thing)
:stream stream)
(format stream " is ~A of type " a-what)
(clim:present (type-of thing) (clim:presentation-type-of (type-of thing))
:stream stream)
(terpri stream)
(format stream " it has the following slots:~%")
(if (typep stream 'clim:output-recording-stream)
(present-instance-slots-clim thing stream)
(present-instance-slots-text thing stream))))
(defmethod describe-object ((thing standard-object) stream)
(describe-instance thing "an instance" stream))
(defmethod describe-object ((thing structure-object) stream)
(describe-instance thing "a structure" stream)))