Debian Codegraph @

src/lsp/describe.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 ;;; DESCRIBE and INSPECT
12
13 (in-package "SYSTEM")
14
15 (pushnew :cdr-6 *features*)
16
17 (defparameter *inspect-level* 0)
18 (defparameter *inspect-history* nil)
19 (defparameter *inspect-mode* nil)
20
21 (defparameter *old-print-level* nil)
22 (defparameter *old-print-length* nil)
23
24
25 ;; Either the inspector reads and writes everything on *standard-output*,
26 ;; or reads and writes everything on *query-io* but not a mix of each!
27 ;; If this rule is not followed only severe confusion can result. JCB
28
29 (defun inspect-read-line ()
30 (do ((char (read-char *query-io*) (read-char *query-io*)))
31 ((or (char= char #\Newline) (char= char #\Return)))))
32
33 (defun select-P (object)
34 (let* ((*print-pretty* t) (*print-level* nil) (*print-length* nil))
35 (prin1 object)
36 (terpri)))
37
38 (defun select-E ()
39 (dolist (x (multiple-value-list
40 (multiple-value-prog1
41 (eval (read-preserving-whitespace *query-io*))
42 (inspect-read-line))))
43 (write x
44 :level *old-print-level*
45 :length *old-print-length*)
46 (terpri)))
47
48 (defun select-U ()
49 (prog1
50 (eval (read-preserving-whitespace *query-io*))
51 (inspect-read-line)))
52
53 (defun select-? ()
54 (terpri)
55 (format t
56 "Inspect commands:~%~
57 n (or N or Newline): inspects the field (recursively).~%~
58 s (or S): skips the field.~%~
59 p (or P): pretty-prints the field.~%~
60 a (or A): aborts the inspection ~
61 of the rest of the fields.~%~
62 u (or U) form: updates the field ~
63 with the value of the form.~%~
64 e (or E) form: evaluates and prints the form.~%~
65 q (or Q): quits the inspection.~%~
66 ?: prints this.~%~%"))
67
68 (defun read-inspect-command (label object allow-recursive)
69 (unless *inspect-mode*
70 ;; This is "describe" mode. So we stay non-interactive.
71 (inspect-indent-1)
72 (if allow-recursive
73 (progn (princ label) (inspect-object object))
74 (format t label object))
75 (return-from read-inspect-command nil))
76 (let* ((*quit-tags* (cons *quit-tag* *quit-tag*)) ;; as seen in top.lsp
77 (*quit-tag* *quit-tags*))
78 (declare (special *quit-tags* *quit-tags*))
79 (loop
80 (when
81 (catch *quit-tag* ;; as seen in top.lsp
82 (with-simple-restart (inspect "Go back to inspector.")
83 (inspect-indent-1)
84 (if allow-recursive
85 (progn (princ label)
86 (inspect-indent)
87 (prin1 object))
88 (format t label object))
89 (write-char #\Space) ;; Inspector prompt!?
90 (princ " >> ") ;; This one is more suggestive.
91 ;;(force-output) ;; not quite enough.
92 (finish-output) ;; this one is stronger.
93 (case (do ((char (read-char *query-io*) (read-char *query-io*)))
94 ((and (char/= char #\Space) (char/= char #\Tab))
95 (cond
96 ((char= char #\Newline) char)
97 ((char= char #\Return) char)
98 ((alphanumericp (peek-char)) #\!) ;; Invalid command on purpose.
99 (t char))
100 ))
101 ((#\Newline #\Return)
102 (when allow-recursive (inspect-object object))
103 (return nil))
104 ((#\n #\N)
105 (inspect-read-line)
106 (when allow-recursive (inspect-object object))
107 (return nil))
108 ((#\s #\S)
109 (inspect-read-line)
110 (return nil))
111 ((#\p #\P)
112 (inspect-read-line)
113 (select-P object))
114 ((#\a #\A)
115 (inspect-read-line)
116 (throw 'ABORT-INSPECT nil))
117 ((#\u #\U)
118 (return (values t (select-U))))
119 ((#\e #\E)
120 (select-E))
121 ((#\q #\Q)
122 (inspect-read-line)
123 (throw 'QUIT-INSPECT nil))
124 ((#\?)
125 (inspect-read-line)
126 (select-?))
127 (t
128 (inspect-read-line)
129 (inspect-indent)
130 (format t "Unknown inspector command. ~
131 Type ? followed by #\\Newline for help."))
132 )
133 )
134 nil
135 )
136 (format t "~&Back to Inspection mode: ~
137 Type ? followed by #\\Newline for help.~%")
138 ))))
139
140 #+ecl-min
141 (defmacro inspect-recursively (label object &optional place)
142 (if place
143 `(multiple-value-bind (update-flag new-value)
144 (read-inspect-command ,label ,object t)
145 (when update-flag (setf ,place new-value)))
146 `(when (read-inspect-command ,label ,object t)
147 (princ "Not updated.")
148 (terpri))))
149
150 #+ecl-min
151 (defmacro inspect-print (label object &optional place)
152 (if place
153 `(multiple-value-bind (update-flag new-value)
154 (read-inspect-command ,label ,object nil)
155 (when update-flag (setf ,place new-value)))
156 `(when (read-inspect-command ,label ,object nil)
157 (princ "Not updated.")
158 (terpri))))
159
160 (defun inspect-indent ()
161 (fresh-line)
162 (format t "~V@T"
163 (* 4 (if (< *inspect-level* 8) *inspect-level* 8))))
164
165 (defun inspect-indent-1 ()
166 (fresh-line)
167 (format t "~V@T"
168 (- (* 4 (if (< *inspect-level* 8) *inspect-level* 8)) 3)))
169
170
171 (defun inspect-symbol (symbol)
172 (declare (si::c-local))
173 (let* ((p (symbol-package symbol)))
174 (cond ((null p)
175 (format t "~:@(~S~) - uninterned symbol" symbol))
176 ((eq p (find-package "KEYWORD"))
177 (format t "~:@(~S~) - keyword" symbol))
178 (t
179 (format t "~:@(~S~) - ~:[internal~;external~] symbol in ~A package"
180 symbol
181 (multiple-value-bind (b f)
182 (find-symbol (symbol-name symbol) p)
183 (declare (ignore b))
184 (eq f :external))
185 (package-name p)))))
186
187 (when (print-doc symbol t)
188 (format t "~&-----------------------------------------------------------------------------~%~%"))
189
190 (if (or (eq t symbol) (eq nil symbol) (keywordp symbol))
191 (progn (inspect-indent-1) (format t "value: ~S" (symbol-value symbol)))
192 (when (boundp symbol)
193 (if *inspect-mode*
194 (inspect-recursively "value:"
195 (symbol-value symbol)
196 (symbol-value symbol))
197 (inspect-print "value:~% ~S"
198 (symbol-value symbol)
199 (symbol-value symbol)))))
200
201 (do ((pl (symbol-plist symbol) (cddr pl)))
202 ((endp pl))
203 (unless (and (symbolp (car pl))
204 (or (eq (symbol-package (car pl)) (find-package 'SYSTEM))
205 (eq (symbol-package (car pl)) (find-package 'COMPILER))))
206 (if *inspect-mode*
207 (inspect-recursively (format nil "property ~S:" (car pl))
208 (cadr pl)
209 (get symbol (car pl)))
210 (inspect-print (format nil "property ~:@(~S~):~% ~~S" (car pl))
211 (cadr pl)
212 (get symbol (car pl))))))
213
214 )
215
216 (defun inspect-package (package)
217 (declare (si::c-local))
218 (format t "~S - package" package)
219 (when (package-nicknames package)
220 (inspect-print "nicknames: ~S" (package-nicknames package)))
221 (when (package-use-list package)
222 (inspect-print "use list: ~S" (package-use-list package)))
223 (when (package-used-by-list package)
224 (inspect-print "used-by list: ~S" (package-used-by-list package)))
225 (when (package-shadowing-symbols package)
226 (inspect-print "shadowing symbols: ~S"
227 (package-shadowing-symbols package))))
228
229 (defun inspect-character (character)
230 (declare (si::c-local))
231 (format t
232 (cond ((standard-char-p character) "~S - standard character")
233 (t "~S - character"))
234 character)
235 (inspect-print "code: #x~X" (char-code character)))
236
237 (defun inspect-number (number)
238 (declare (si::c-local))
239 (let ((type (type-of number)))
240 (when (consp type) ;; Range types, as (INTEGER 0 0)
241 (setf type (first type)))
242 (format t "~S - ~a" number (string-downcase type))
243 (case type
244 (INTEGER)
245 (RATIO
246 (inspect-recursively "numerator:" (numerator number))
247 (inspect-recursively "denominator:" (denominator number)))
248 (COMPLEX
249 (inspect-recursively "real part:" (realpart number))
250 (inspect-recursively "imaginary part:" (imagpart number)))
251 ((SHORT-FLOAT SINGLE-FLOAT LONG-FLOAT DOUBLE-FLOAT)
252 (multiple-value-bind (signif expon sign)
253 (integer-decode-float number)
254 (declare (ignore sign))
255 (inspect-print "exponent: ~D" expon)
256 (inspect-print "mantissa: ~D" signif))))))
257
258 (defun inspect-cons (cons)
259 (declare (si::c-local))
260 (format t "~S - cons" cons)
261 (when *inspect-mode*
262 (do ((i 0 (1+ i))
263 (l cons (cdr l)))
264 ((atom l)
265 (case l
266 ((t nil) ;; no point in inspecting recursively t nor nil.
267 (inspect-print (format nil "nthcdr ~D: ~~S" i) l))
268 (t
269 (inspect-recursively (format nil "nthcdr ~D:" i)
270 l (cdr (nthcdr (1- i) cons))))))
271 (inspect-recursively (format nil "nth ~D:" i)
272 (car l) (nth i cons)))))
273
274 (defun inspect-string (string)
275 (declare (si::c-local))
276 (format t (if (simple-string-p string) "~S - simple string" "~S - string")
277 string)
278 (inspect-print "dimension: ~D"(array-dimension string 0))
279 (when (array-has-fill-pointer-p string)
280 (inspect-print "fill pointer: ~D"
281 (fill-pointer string)
282 (fill-pointer string)))
283 (when *inspect-mode*
284 (dotimes (i (array-dimension string 0))
285 (inspect-recursively (format nil "aref ~D:" i)
286 (char string i)
287 (char string i)))))
288
289 (defun inspect-vector (vector)
290 (declare (si::c-local))
291 (format t (if (simple-vector-p vector) "~S - simple vector" "~S - vector")
292 vector)
293 (inspect-print "dimension: ~D" (array-dimension vector 0))
294 (when (array-has-fill-pointer-p vector)
295 (inspect-print "fill pointer: ~D"
296 (fill-pointer vector)
297 (fill-pointer vector)))
298 (when *inspect-mode*
299 (dotimes (i (array-dimension vector 0))
300 (inspect-recursively (format nil "aref ~D:" i)
301 (aref vector i)
302 (aref vector i)))))
303
304 (defun inspect-array (array)
305 (declare (si::c-local))
306 (format t (if (adjustable-array-p array)
307 "~S - adjustable aray"
308 "~S - array")
309 array)
310 (inspect-print "rank: ~D" (array-rank array))
311 (inspect-print "dimensions: ~D" (array-dimensions array))
312 (inspect-print "total size: ~D" (array-total-size array)))
313
314 (defun select-ht-N (hashtable)
315 (declare (si::c-local))
316 (incf *inspect-level*)
317 (maphash #'(lambda (key val)
318 (inspect-indent-1)
319 (format t "key : ~S" key)
320 (inspect-recursively "value:" val (gethash key hashtable)))
321 hashtable)
322 (decf *inspect-level*))
323
324 (defun select-ht-L (hashtable)
325 (declare (si::c-local))
326 (terpri)
327 (format t "The keys of the hash table are:~%")
328 (maphash #'(lambda (key val)
329 (declare (ignore val))
330 (format t " ~S~%" key))
331 hashtable)
332 (terpri))
333
334 (defun select-ht-J (hashtable)
335 (declare (si::c-local))
336 (let* ((key (prog1
337 (read-preserving-whitespace *query-io*)
338 (inspect-read-line)))
339 (val (gethash key hashtable)))
340 (if val
341 (progn
342 (incf *inspect-level*)
343 (inspect-indent-1)
344 (format t "key : ~S" key)
345 (inspect-recursively "value:" val (gethash key hashtable))
346 (decf *inspect-level*))
347 (progn
348 (terpri)
349 (format t "The key ~S is not present or the value associated is NIL." key)
350 (terpri)
351 (terpri)))))
352
353 (defun select-ht-? ()
354 (declare (si::c-local))
355 (terpri)
356 (format t
357 "Inspect commands for hash tables:~%~
358 n (or N or #\\Newline): inspects the keys/values of the hashtable (recursively).~%~
359 s (or S): skips the field.~%~
360 p (or P): pretty-prints the field.~%~
361 a (or A): aborts the inspection of the rest of the fields.~%~
362 e (or E) form: evaluates and prints the form.~%~
363 l (or L): show the keys of the hash table.~%~
364 j (or J) key: inspect the value associated to the key requested.~%~
365 q (or Q): quits the inspection.~%~
366 ?: prints this help message.~%~%"
367 ))
368
369 (defun inspect-hashtable (hashtable)
370 (declare (si::c-local))
371 (if *inspect-mode*
372 (progn
373 (decf *inspect-level*)
374 (loop
375 (format t "~S - hash table: " hashtable)
376 ;;(force-output) ;; not quite enough.
377 (finish-output) ;; this one is stronger.
378 (case (do ((char (read-char *query-io*) (read-char *query-io*)))
379 ((and (char/= char #\Space) (char/= #\Tab)) char))
380 ((#\Newline #\Return)
381 (select-ht-N hashtable)
382 (return nil))
383 ((#\n #\N)
384 (inspect-read-line)
385 (select-ht-N hashtable)
386 (return nil))
387 ((#\s #\S)
388 (inspect-read-line)
389 (return nil))
390 ((#\p #\P)
391 (inspect-read-line)
392 (select-P hashtable))
393 ((#\a #\A)
394 (inspect-read-line)
395 (throw 'ABORT-INSPECT nil))
396 ((#\e #\E)
397 (select-E))
398 ((#\q #\Q)
399 (inspect-read-line)
400 (throw 'QUIT-INSPECT nil))
401 ((#\l #\L)
402 (inspect-read-line)
403 (select-ht-L hashtable))
404 ((#\j #\J)
405 (select-ht-J hashtable))
406 ((#\?)
407 (inspect-read-line)
408 (select-ht-?)))
409 (inspect-indent)))
410 (progn
411 (format t "~S - hash table: " hashtable)
412 (maphash #'(lambda (key val)
413 (inspect-indent-1)
414 (format t "key : ~S" key)
415 (inspect-indent-1)
416 (format t "value:")
417 (inspect-object val))
418 hashtable))))
419
420 #+CLOS
421 (defun inspect-instance (instance)
422 (if *inspect-mode*
423 (clos::inspect-obj instance)
424 (clos::describe-object instance *standard-output*)))
425
426 (defun inspect-object (object &aux (*inspect-level* *inspect-level*))
427 (inspect-indent)
428 (when (and (not *inspect-mode*)
429 (or (> *inspect-level* 5)
430 (member object *inspect-history*)))
431 (prin1 object)
432 (return-from inspect-object))
433 (incf *inspect-level*)
434 (push object *inspect-history*)
435 (catch 'ABORT-INSPECT
436 (cond
437 ((symbolp object) (inspect-symbol object))
438 ((packagep object) (inspect-package object))
439 ((characterp object) (inspect-character object))
440 ((numberp object) (inspect-number object))
441 ((consp object) (inspect-cons object))
442 ((stringp object) (inspect-string object))
443 ((vectorp object) (inspect-vector object))
444 ((arrayp object) (inspect-array object))
445 ((hash-table-p object) (inspect-hashtable object))
446 #+clos
447 ((sys:instancep object) (inspect-instance object))
448 (t (format t "~S - ~S" object (type-of object))))))
449
450 (defun default-inspector (object)
451 "Args: (object)
452 Shows the information about OBJECT interactively. See the ECL Report for the
453 inspect commands, or type '?' to the inspector."
454 (declare (si::c-local))
455 ;;(read-line)
456 (let ((*inspect-mode* t)
457 (*inspect-level* 0)
458 (*inspect-history* nil)
459 (*old-print-level* *print-level*)
460 (*old-print-length* *print-length*)
461 (*print-level* 3)
462 (*print-length* 3))
463 (terpri)
464 (princ "Inspection mode: Type ? followed by #\\Newline for help.")
465 (terpri)
466 (terpri)
467 (catch 'QUIT-INSPECT (inspect-object object))
468 (terpri)
469 (values)))
470
471 (defun inspect (object)
472 (if ext:*inspector-hook*
473 (funcall *inspector-hook* object)
474 (default-inspector object))
475 object)
476
477 (defun describe (object &optional (stream *standard-output*)
478 &aux (*inspect-mode* nil)
479 (*inspect-level* 0)
480 (*inspect-history* nil)
481 (*print-level* nil)
482 (*print-length* nil)
483 (*standard-output* (cond ((streamp stream) stream)
484 ((null stream) *standard-output*)
485 ((eq stream t) *terminal-io*)
486 (t (error 'type-error
487 :datum stream
488 :expected-type '(or stream t nil))))))
489 "Args: (object &optional (stream *standard-output*))
490 Prints information about OBJECT to STREAM."
491 (terpri)
492 (catch 'QUIT-INSPECT (inspect-object object))
493 (terpri)
494 (values))
495
496 (defun print-doc (symbol &optional (called-from-apropos-doc-p nil)
497 &aux (f nil) x)
498 (flet ((head (ind)
499 (setq f t)
500 (format t
501 "~&-----------------------------------------------------------------------------~%~53S~24@A~%"
502 symbol ind))
503 (doc (str)
504 (format t "~A~%" str))
505 (arg (sym)
506 (format t "Arguments: ~:A~%" (ext:function-lambda-list sym)))
507 (src (sym)
508 (when-let ((obj (ignore-errors (fdefinition sym))))
509 (if-let ((src (ext:compiled-function-file obj)))
510 (format t "Source: ~s~%" (pathname src))
511 (format t "Source: (not available)~%"))))
512 (good-package ()
513 (if (eq (symbol-package symbol) (find-package "CL"))
514 (find-package "SYSTEM")
515 *package*)))
516
517 (cond ((special-operator-p symbol)
518 (head (if (macro-function symbol)
519 "[Special form and Macro]"
520 "[Special form]"))
521 (doc (or (si::get-documentation symbol 'FUNCTION) ""))
522 (arg symbol)
523 (src symbol))
524 ((macro-function symbol)
525 (head "[Macro]")
526 (doc (or (si::get-documentation symbol 'FUNCTION) ""))
527 (arg symbol)
528 (src symbol))
529 ((fboundp symbol)
530 (head "[Function]")
531 (doc (or (si::get-documentation symbol 'FUNCTION) ""))
532 (arg symbol)
533 (src symbol))
534 ((setq x (si::get-documentation symbol 'FUNCTION))
535 (head "[Macro or Function]")
536 (doc x)
537 (arg symbol)
538 (src symbol)))
539
540 (cond ((constantp symbol)
541 (unless (and (eq (symbol-package symbol) (find-package "KEYWORD"))
542 (null (si::get-documentation symbol 'VARIABLE)))
543 (head "[Constant]")
544 (doc (or (si::get-documentation symbol 'VARIABLE) ""))))
545 ((sys:specialp symbol)
546 (head "[Special variable]")
547 (doc (or (si::get-documentation symbol 'VARIABLE) "")))
548 ((or (setq x (si::get-documentation symbol 'VARIABLE)) (boundp symbol))
549 (head "[Variable]")
550 (doc (or x ""))))
551
552 (cond ((setq x (si::get-documentation symbol 'TYPE))
553 (head "[Type]")
554 (doc x))
555 ((setq x (get-sysprop symbol 'DEFTYPE-FORM))
556 (let ((*package* (good-package)))
557 (head "[Type]")
558 (doc (format nil "~%Defined as: ~S~%See the doc of DEFTYPE." x)))))
559
560 (cond ((setq x (si::get-documentation symbol 'STRUCTURE))
561 (head "[Structure]")
562 (doc x))
563 ((setq x (get-sysprop symbol 'DEFSTRUCT-FORM))
564 (head "[Structure]")
565 (doc (format nil "~%Defined as: ~S~%See the doc of DEFSTRUCT." x))))
566
567 (cond ((setq x (si::get-documentation symbol 'SETF))
568 (head "[Setf]")
569 (doc x))
570 ((setq x (get-sysprop symbol 'SETF-METHOD))
571 (let ((*package* (good-package)))
572 (head "[Setf]")
573 (doc
574 (format nil
575 "~@[~%Defined as: ~S~%See the doc of DEFINE-SETF-EXPANDER.~]"
576 (if (consp x)
577 (case (car x)
578 (LAMBDA `(define-setf-expander ,@(cdr x)))
579 (EXT::LAMBDA-BLOCK `(define-setf-expander ,@(cddr x)))
580 (t nil))
581 nil)))))))
582
583 (if called-from-apropos-doc-p
584 f
585 (progn (if f
586 (format t "~&-----------------------------------------------------------------------------")
587 (format t "~&No documentation for ~:@(~S~)." symbol))
588 (values))))
589
590 (defun help* (string &optional (package "CL"))
591 "Args: (string &optional (package-spec 'common-lisp))
592 ECL specific.
593 Prints the documentation associated with those symbols in the specified
594 package whose print names contain STRING as substring. STRING may be a
595 symbol, in which case the print-name of that symbol is used. If PACKAGE is
596 NIL, then all packages are searched."
597 (do* ((f nil)
598 (l (apropos-list string package) (cdr l)))
599 ((endp l)
600 (format t (if f
601 "~&-----------------------------------------------------------------------------"
602 "~&No documentation for ~S in ~:[any~;~A~] package.")
603 string package (and package (package-name (coerce-to-package package)))))
604 (when (print-doc (first l) t)
605 (setf f t)))
606 (values))