dispcond.scm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Displaying conditions
  3. (define display-condition
  4. (let ((display display) (newline newline))
  5. (lambda (c port)
  6. (if (ignore-errors (lambda ()
  7. (newline port)
  8. (really-display-condition c port)
  9. #f))
  10. (begin (display "<Error while displaying condition.>" port)
  11. (newline port))))))
  12. (define (really-display-condition c port)
  13. (let* ((stuff (disclose-condition c))
  14. (stuff (if (and (list? stuff)
  15. (not (null? stuff))
  16. (symbol? (car stuff)))
  17. stuff
  18. (list 'condition stuff))))
  19. (display-type-name (car stuff) port)
  20. (if (not (null? (cdr stuff)))
  21. (begin (display ": " port)
  22. (let ((message (cadr stuff)))
  23. (if (string? message)
  24. (display message port)
  25. (limited-write message port *depth* *length*)))
  26. (let ((spaces
  27. (make-string (+ (string-length
  28. (symbol->string (car stuff)))
  29. 2)
  30. #\space)))
  31. (for-each (lambda (irritant)
  32. (newline port)
  33. (display spaces port)
  34. (limited-write irritant port *depth* *length*))
  35. (cddr stuff)))))
  36. (newline port)))
  37. (define *depth* 5)
  38. (define (condition-display-depth) *depth*)
  39. (define (set-condition-display-depth! new)
  40. (set! *depth* new))
  41. (define *length* 6)
  42. (define (condition-display-length) *length*)
  43. (define (set-condition-display-length! new)
  44. (set! *length* new))
  45. (define-generic disclose-condition &disclose-condition)
  46. (define-method &disclose-condition (c) c)
  47. (define (limited-write obj port max-depth max-length)
  48. (let recur ((obj obj) (depth 0))
  49. (if (and (= depth max-depth)
  50. (not (or (boolean? obj)
  51. (null? obj)
  52. (number? obj)
  53. (symbol? obj)
  54. (char? obj)
  55. (string? obj))))
  56. (display "#" port)
  57. (call-with-current-continuation
  58. (lambda (escape)
  59. (recurring-write obj port
  60. (let ((count 0))
  61. (lambda (sub)
  62. (if (= count max-length)
  63. (begin (display "---" port)
  64. (write-char
  65. (if (or (pair? obj) (vector? obj))
  66. #\)
  67. #\})
  68. port)
  69. (escape #t))
  70. (begin (set! count (+ count 1))
  71. (recur sub (+ depth 1))))))))))))