File: inspect.lsp

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (617 lines) | stat: -rw-r--r-- 20,135 bytes parent folder | download | duplicates (3)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
; XLISP INSPECTOR/EDITOR by TOM ALMY
; This is a rewritten and improved version of "REPAIR"
; Revised 10/96 to include functions INSPECT and DESCRIBE

; Use as (ins <symbol>)  or (insf <symbol>) to repair only the function
; binding, with the capability of changing the argument list and type
; (MACRO or LAMBDA). Functions INS and INSF are interned in package TOOLS.

; Common Lisp standard functions (inspect <object>) and (describe <object>)
; do not quote their arguements and work with any arbitrary object.

; Editor alters the "selection" by copying so that aborting  all changes
;  is generally posible.
; Exception: when editing a closure, if the closure is Backed out of, the
;   change is permanent.

; Do not create new closures, because the environment will be incorrect.

; Closures become LAMBDA or MACRO expressions when edited.  Only
; the closure body may be changed; the argument list cannot be successfully
; modified, nor can the environment.

; For all commands taking a numeric argument, the first element of the
; selection is the 0th (as in NTH function).

; Structure elements, class methods, instance variables, and properties
; are selected by name, using the E command.

; For class objects, only the methods, selectors and class variables
; can be edited. Class variables can only be changed if instance variables
; of class instances can be changed (see next paragraph).

; For instance objects, instance variables can be examine
; (if the object understands the message :<ivar> for the particular ivar),
; and changed if :SET-IVAR is defined for that class (as it is if CLASSES.LSP
; is used)

; COMMANDS (case is significant):
;	A -- select the CAR of the current selection.
;	D -- select the CDR of the current selection.
;	e n -- select element n
;	r n x -- replaces element n with (quoted) x.
;	X -- exit, saving all changes
;	Q -- exit, without changes
;	b -- go back one level (as before A, D or e commands)
;       B n -- go back n levels.
;	l -- display selection using pprint; if selection is symbol, give
;	     short description
;	v -- verbosity toggle
;       . n -- change maximum print length (default 10)
;       # n -- change maximum print depth (default 3)    
;	! x -- evaluates x and prints result
;		The symbol tools:@ is bound to the selection
;	R x -- replaces the selection with evaluated x.
;		The symbol tools:@ is bound to the selection
; additional commands if selection is a list or array:
;	( n m -- inserts parenthesis starting with the nth element,
;		for m elements.
;	) n -- removes parenthesis surrounding nth element of selection,
;              which may be array or list
;	[ n m -- as in (, but makes elements into an array
;	i n x -- inserts (quoted) x before nth element in selection.
;	d n -- deletes nth element in selection.
; additional command if selection is a list:
;	S x y -- all occurances of (quoted) y are replaced with 
;		(quoted) x.  EQUAL is used for the comparison.


#+:packages
(unless (find-package "TOOLS")
	(make-package "TOOLS" :use '("XLISP")))

(in-package "XLISP")
(export '(inspect describe)) ;; Common Lisp standard functions we'll define

(in-package "TOOLS")

(export '(ins insf @))

; Global variable used by inspect functions

(defparameter *ins-exit* 0)   ; "returning" flag
(defparameter *ins-name* nil) ; name of what we are editing

(defvar *ins-plev* 3)	; initial print level used
(defvar *ins-plen* 10)	; initial print length used
(defvar *verbosity* t)  ; printing verbosity flag					   
(defconstant *LPAR* #\()
(defconstant *RPAR* #\))

			     
; inspect a symbol -- the generic entry point

(defmacro ins (a)
    (unless (symbolp a) (error "~s is not a symbol" a))
    (let
     ((*breakenable* t)
      (*ins-exit* 0)
      (*ins-name* (cons "symbol" a))
      (*print-level* *ins-plev*)
      (*print-length* *ins-plen*))
     (catch 'abort (ins-ins a)))
     `',a)

; inspect a function, with editable arguments

(defmacro insf (a)
    (let
     ((*breakenable* nil)
      (*ins-exit* 0) 
      (*ins-name* (cons "function" a)) 
      (*print-level* *ins-plev*)
      (*print-length* *ins-plen*))
     (catch 'abort
	    (if (and (fboundp a) (typep (symbol-function a) 'closure))
		(let ((x (ins-ins(get-lambda-expression(symbol-function a)))))
		     (case (first x)
			   (lambda `(defun ,a ,@(rest x)))
			   (macro  `(defmacro ,a ,@(rest x)))
			   (t (error "not a closure!"))))
		(error "can't repair")))))

; Inspect anything

(defun xlisp::inspect (a)
       (let
	((*breakenable* t)
	 (*ins-exit* 0)
	 (*ins-name* (cons (if (symbolp a) "symbol" "expression") a))
	 (*print-level* *ins-plev*)
	 (*print-length* *ins-plen*))
	(catch 'abort (ins-ins a)))
       a)


; Describe anything

(defun xlisp::describe (a)
       (let ((*print-level* *ins-plev*)
	     (*print-length* *ins-plen*))
	    (format t "~&~s is " a)
	    (if	(or (typep a 'struct)
		    (objectp a))
		(ins-display a)
		(case (type-of a)
		      (list (format t "NIL"))
		      (cons (if (list-length a)
				(format t "a list of length ~s" (length a))
				(format t "a circular list")))
		      (array (format t "an array of length ~s" (length a)))
		      (string (format t "a string of length ~s" (length a)))
		      (hash-table (format t "a hash table with ~s entries"
					  (hash-table-count a)))
		      (symbol
		       (ins-display a)
		       (if (symbol-package a)
			   (format t "Home package: ~a~%" (package-name (symbol-package a)))
			   (format t "No home package~%"))
		       (format t "Visible in: ")
		       (map nil
			    (lambda (x)
				    (multiple-value-bind
				     (val type)
				     (find-symbol (symbol-name a) x)
				     (when (and type (eq val a))
					   (format t "~a (~a)  "
						   (package-name x)
						   (case type
							 (:external "ext")
							 (:internal "int")
							 (t "inh"))))))
			    (list-all-packages))
		       (terpri))
		      (t (format t "a ~(~a~)" (type-of a)))))))

; ins-propp returns T if p is a property of a

(defun ins-propp (a p)
	(do 	((plist (symbol-plist a) (cddr plist)))
		((or (null plist) (eq (car plist) p))
		 (not (null plist)))))

; terminate input line

(defun ins-teread (error) 
       (fresh-line)
       (if (not (eq (peek-char) #\Newline))
	   (read-line))
       (if error
	   (format t "Try again:")
	   (format t "~a ~a>" (car *ins-name*) (cdr *ins-name*))))

(defmacro ins-protread () ;;Protected read -- we handle errors
	  '(do ((val (errset (read)) 
		     (progn (ins-teread t) (errset (read)))))
	       ((consp val) (car val))))

(defmacro ins-proteval () ;;protected eval -- we handle errors
			  ;; we also use evalhook so environment is global
			  ;;  plus a local @, which cannot be changed!
	'(do* ((env (cons (list (list (cons '@ list))) nil))
	       (val (errset (evalhook (read) nil nil env))
		    (progn (ins-teread t) 
			   (errset (evalhook (read) nil nil env)))))
	      ((consp val) (car val))))


; New methods so that we can inspect and repair messages.

(send Class :answer :messages '() '(messages))

; new methods so that we can inspect and repair instance variables

(send Class :answer :ivars '() '(ivars))

(send Class :answer :cvars '() '((map 'list #'cons cvars cvals)))

(send Class :answer :superclass '() '(superclass))

#+:packages (import '(xlisp::%struct-ref xlisp::%struct-set))

(defun ins-struct (struct name)  ; get structure element
       (%struct-ref struct
		    (1+ (position name
				  (get (type-of struct) '*struct-slots*)
				  :key #'first))))

(defun ins-set-struct (struct name value)  ; set structure element
       (%struct-set struct
		    (1+ (position name
				  (get (type-of struct) '*struct-slots*)
				  :key #'first))
		    value))

#+:packages (unintern 'xlisp::%struct-ref)
#+:packages (unintern 'xlisp::%struct-set)


(defun ins-ivar (obj name)
       (funcall #'send obj
#+:packages	(intern (string name) :keyword)
#-:packages      (intern (strcat ":" name))
		))

(defun ins-set-ivar (obj name value)
       (funcall #'send obj :set-ivar
#+:packages	(intern (string name) :keyword)
#-:packages     (intern (strcat ":" name))
		value))

; help function
(defun ins-help (list)
       (format t "~%Available commands:~2%")
       (format t "e n~8tselect element n~%")
       (format t "r n x~8treplaces element n with (quoted) x.~%")
       (format t "X~8texit, saving all changes~%")
       (format t "Q~8texit, without changes~%")
       (format t "b~8tgo back one level (as before A, D or e commands)~%")
       (format t "B n~8tgo back n levels.~%")
       (format t (if (symbolp list)
		     "l~8tshow symbol~%"
		     "l~8tdisplay selection using pprint~%"))
       (format t "v~8tverbosity toggle~%")
       (format t ". n~8tchange maximum print length (default 10)~%")
       (format t "# n~8tchange maximum print depth (default 3)~%")
       (format t "! x~8tevaluates x and prints result.~%~8tThe symbol tools:@ is bound to the selection~%")
       (format t "R x~8treplaces the selection with evaluated x.~%~8tThe symbol tools:@ is bound to the selection~%")
       (unless (typep list '(or cons array)) (return-from ins-help list))
       (format t "A~8tselect the CAR of the current selection.~%")
       (format t "D~8tselect the CDR of the current selection.~%")
       (format t "( n m~8tinserts parens from nth element for m elements.~%")
       (format t ") n~8tremoves parens around nth element of selection.~%")
       (format t "[ n m~8tas in [, but makes elements into an array.~%")
       (format t "i n x~8tinserts (quoted) x before nth element in selection.~%")
       (format t "d n~8tdeletes nth element in selection.~%")
       (unless (typep list 'cons) (return-from ins-help list))
       (format t "S x y~8tall occurances of (quoted) y are replaced with~%~8t(quoted) x.  EQUAL is used for the comparison.~%")
       list)


; Display current selection
(defun ins-display (list)
       (fresh-line)
       (cond ((typep list '(or cons array))
	      (let ((n 0))
		   (if (or (arrayp list) (list-length list))
		       (map nil #'(lambda (l)
					  (format t "~3@s ~s~%" n l)
					  (setq n (1+ n)))
			    list)
		       (format t "circular list ~s~%" list))))
	     ((typep list 'struct)
	      (format t "~a structure ~%" (type-of list))
	      (mapc #'(lambda (n)
			      (format t
				      "~10s~s~%"
				      (car n)
				      (ins-struct list (car n))))
		    (get (type-of list) '*struct-slots*)))
	     ((classp list)
	      (format t "Class ~s, messages:~%" (send list :pname))
	      (mapc #'(lambda (n) (format t " ~s" (car n)))
		    (send list :messages))
	      (terpri)
	      (when (send list :cvars)
		    (format t "~%cvars:~%")
		    (mapc #'(lambda (n) (format t " ~s ~s~%" (car n) (cdr n)))
			  (send list :cvars))))
	     ((objectp list)
	      (format t "A ~s, ivars:~%" (send (send list :class) :pname))
	      (mapc #'(lambda (n) (format t " ~s ~s~%" n (ins-ivar list n)))
		    (send (send list :class) :ivars))
	      (terpri))
	     ((symbolp list)
	      (format t "Symbol ~s:~%" (symbol-name list))
	      (when (fboundp list) (format t "Function binding~%"))
	      (when (boundp list)
		    (format t "Value binding~a: ~s~%"
			    (cond ((constantp list) " (constant)")
				  ((specialp list) "  (special)")
				  (t ""))
			    (symbol-value list)))
	      (when (symbol-plist list)
		    (format t "Properties:~%")
		    (do ((l (symbol-plist list) (cddr l)))
			((null l) nil)
			(format t " ~s ~s~%" (first l) (second l)))))
	     (t (pprint list)))
       list)


; Bad command
(defun ins-bad (list)
       (format t "~&What??~%")
       list)
	  
;; Expects number >=min and <max (if max non-nil)
;; returns valid number, or prints message and returns nil
(defun ins-number (min max err &aux (n (ins-protread)))
       (if (and (numberp n)
		(>= n min)
		(or (null max) (< n max)))
	   n
	   (if err (ins-bad nil) nil)))

; inspect and replace list/array/structure/object/symbol elements
(defun ins-list-spec (list)
       (princ " element #? ")
       (ins-number 0 (length list) nil))

(defun ins-struct-spec (list &aux name)
       (princ " element name? ")
       (when (assoc (setq name (ins-protread))
		    (get (type-of list) '*struct-slots*))
	     name))

(defun ins-class-spec (list &aux name)
       (princ " message/cvar name? ")
       (cond ((assoc (setq name (ins-protread))
		     (send list :messages))
	      (cons t name))
	     ((assoc name (send list :cvars))
	      (cons nil name))))
       
(defun ins-object-spec (list &aux name)
       (princ " ivar name? ")
       (when (member (setq name (ins-protread))
		    (send (send list :class) :ivars))
	     name))

(defun ins-symbol-spec (list &aux name)
       (if (and (boundp list) (not (fboundp list)) (not (symbol-plist list)))
	   :v
	   (if (and (not (boundp list)) (fboundp list)
		    (not (symbol-plist list)))
	       :f
	       (progn
		(princ " :f :v or propname?")
		(if (ins-propp list (setq name (ins-protread)))
		    name
		    (case name ((:f :v) name)))))))

(defun ins-enter (list &aux val)
       (cond ((typep list '(or cons array))
	      (if (setq val (ins-list-spec list))
		  (concatenate (type-of list)
			       (subseq list 0 val)
			       (list (ins-ins (elt list val)))
			       (subseq list (1+ val)))
		  (ins-bad list)))
	     ((typep list 'struct)
	      (if (setq val (ins-struct-spec list))
		  (progn (ins-set-struct list val
					 (ins-ins (ins-struct list val)))
			 list)
		  (ins-bad list)))
	     ((classp list)
	      (if (setq val (ins-class-spec list))
		  (if (car val)
		      (let ((closure (cdr (assoc (cdr val)
						 (send list :messages))))
			    closure2 result)
			   (unless (typep closure 'closure)
				   (ins-bad list)
				   (return-from ins-enter list))
			   (setq closure2 (get-lambda-expression closure))
			   (setq result (ins-ins closure2))
			   (setf (cdr (cddr closure2)) (cdddr result))
			   (setf (car (cddr closure2)) (caddr result))
			   list)
		      (progn (ins-set-ivar (send list :new)
					   (cdr val)
					   (ins-ins (ins-ivar (send list :new)
							      (cdr val))))
			     list))
		  (ins-bad list)))
	     ((objectp list)
	      (if (setq val (ins-object-spec list))
		  (progn
		   (ins-set-ivar list val (ins-ins (ins-ivar list val)))
		   list)
		  (ins-bad list)))
	     ((typep list 'closure)
	      (let* ((x (get-lambda-expression list))
		     (y (ins-ins x)))
		    (setf (cdr (cddr x)) (cdddr y))
		    (setf (car (cddr x)) (caddr y))
		    list))
	     ((symbolp list)
	      (if (setq val (ins-symbol-spec list))
		  (case val
			(:f
			 (if (and (fboundp list)
				  (typep (symbol-function list) 'closure))
			     (let* ((x
				     (get-lambda-expression
				      (symbol-function list)))
				    (y (ins-ins x)))
				   (setf (cdr (cddr x)) (cdddr y))
				   (setf (car (cddr x)) (caddr y))
				   list)
			     (ins-bad list)))
			(:v
			 (if (boundp list)
			     (let* ((*ins-name* (cons "symbol" list))
				    (result (ins-ins (symbol-value list))))
				   (if (constantp list)
				       list
				       (progn (set list result) list)))
			     (ins-bad list)))
			(t (setf (get list val)
				 (ins-ins (get list val)))
			   list))))))
			      
(defun ins-repwith (list)
       (format t "~&Replace with: ")
       (ins-protread))

(defun ins-replace (list &aux val)
       (cond ((typep list '(or cons array))
	      (if (setq val (ins-list-spec list))
		  (concatenate (type-of list)
			       (subseq list 0 val)
			       (list (ins-repwith list))
			       (subseq list (1+ val)))
		  (ins-bad list)))
	     ((typep list 'struct)
	      (if (setq val (ins-struct-spec list))
		  (progn (ins-set-struct list val
					 (ins-repwith list))
			 list)
		  (ins-bad list)))
	     ((classp list)	; gotta catch this error here
	      (ins-bad list))
	     ((objectp list)
	      (if (setq val (ins-object-spec list))
		  (progn
		   (ins-set-ivar list val (ins-repwith list))
		   list)
		  (ins-bad list)))
	     ((symbolp list)
	      (if (setq val (ins-symbol-spec list))
		  (case val
			(:f
			 (ins-bad list))
			(:v
			 (if (not (constantp list))
			     (progn (setf (symbol-value list)
					  (ins-repwith list))
				    list)
			     (ins-bad list)))
			(t (setf (get list val)
				 (ins-repwith list))
			   list))))))

; main list repair interface
(defun ins-ins (list) 
       (ins-display list)
       (prog (command n newlist)
	     y (ins-teread nil)
	     (setq command (int-char (get-key)))  ;; Works with most systems
	     (princ command)
	     (setq
	      newlist  ;; new list value, if any
	      (case
	       command
	       (#\? (ins-help list))
	       (#\v (if (setq *verbosity* (not *verbosity*))
			(ins-display list)
			list))
	       (#\X (setq *ins-exit* -1) list)
	       (#\Q (throw 'abort))
	       (#\b (format t "ack") (return list))
	       (#\B (format t "ack #? ")
		    (when (setq n (ins-number 1 nil t))
			  (setq *ins-exit* n))
		    list)
	       (#\l (if *verbosity*
			(if (symbolp list)
			    (ins-display list)
			    (progn (terpri) (pprint list)))
			(format t "~%~s~%" list))
		    list)
	       (#\! (format t " Eval:~%")
		    (print (ins-proteval))
		    list)
	       (#\R (format t "eplace w. evaled:~%")
		    (ins-proteval))
	       (#\# (format t " print-level? ")
		    (when (setq n (ins-number 1 nil t))
			  (format t "Was ~s\n" *print-level*)
			  (setq *print-level* n))
		    list)
	       (#\. (format t " print-length? ")
		    (when (setq n (ins-number 1 nil t))
			  (format t "Was ~s\n" *print-length*)
			  (setq *print-length* n))
		    list)
; cons only commands
	       (#\A (if (consp list)
			(cons (ins-ins (car list)) (cdr list))
			(ins-bad list)))
	       (#\D (if (consp list)
			(cons (car list) (ins-ins (cdr list)))
			(ins-bad list)))
; various special commands
	       (#\e (if (typep list '(or cons struct array object symbol closure))
			(ins-enter list)
			(ins-bad list)))
	       ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
		(setq n (- (char-int command) (char-int #\0)))
		(if (and (typep list '(or cons array))
			 (< n (length list)))
		    (concatenate (type-of list)
				 (subseq list 0 n)
				 (list (ins-ins (elt list n)))
				 (subseq list (1+ n)))
		    (ins-bad list)))
	       (#\r (if (typep list '(or cons struct array object symbol))
			(ins-replace list)
			(ins-bad list)))
	       (#.*RPAR* (if (and (typep list '(or cons array))
			      (princ " remove nesting at #? ")
			      (setq n (ins-number 0 (length list) nil))
			      (typep (elt list n) '(or cons array)))
			 (concatenate (type-of list)
				      (subseq list 0 n)
				      (elt list n)
				      (subseq list (1+ n)))
			 (ins-bad list)))
	       ((#.*LPAR* #\[)
		(if (and (typep list '(or cons array))
			 (princ " insert nesting starting at # and length? ")
			 (setq n (ins-number 0 nil nil))
			 (setq n2 (ins-number 1
					      (- (1+ (length list)) n)
					      nil)))
		    (concatenate (type-of list)
				 (subseq list 0 n)
				 (list (coerce (subseq list n (+ n n2))
					       (if (eq command *LPAR*)
						   'list
						   'array)))
				 (subseq list (+ n n2)))
		    (ins-bad list)))
	       (#\i (if (and (typep list '(or array cons))
			     (princ "nsert before # and value? ")
			     (setq n (ins-number 0 (1+ (length list)) nil)))
			(concatenate (type-of list)
				     (subseq list 0 n)
				     (list (ins-protread))
				     (subseq list n))
			(ins-bad list)))
	       (#\d (if (and (typep list '(or array cons))
			     (princ "elete #? ")
			     (setq n (ins-number 0 (length list) nil)))
			(concatenate (type-of list)
				     (subseq list 0 n)
				     (subseq list (1+ n)))
			(ins-bad list)))
	       (#\S (if (typep list 'cons)
			(progn
			 (princ "ubstitute expr with expr\n")
			 (subst (ins-protread) 
				(ins-protread) 
				list
				:test #'equal))
			(ins-bad list)))
		   (#\Newline list)
		   (t (ins-bad list))))
	     (when (not (eq list newlist))	    ;; show any changes
		   (setq list newlist)
		   (when (and (zerop *ins-exit*) *verbosity*)
			 (ins-display list)))
	     (when (zerop *ins-exit*) (go y))
	     (setq *ins-exit* (1- *ins-exit*)) ;; return a level
	     (return list)))