dispcond.scm 1.9 KB

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