| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
(define-syntax fn |
| 7 |
(syntax-rules () |
| 8 |
((fn . x) |
| 9 |
(computation-fn . x)))) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
(define (substring-length str . o) |
| 16 |
(let ((start (if (pair? o) (car o) 0)) |
| 17 |
(end (if (and (pair? o) (pair? (cdr o))) (cadr o) (string-length str)))) |
| 18 |
(- end start))) |
| 19 |
|
| 20 |
(define (call-with-output-string proc) |
| 21 |
(let ((out (open-output-string))) |
| 22 |
(proc out) |
| 23 |
(let ((res (get-output-string out))) |
| 24 |
(close-output-port out) |
| 25 |
res))) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
(define (output-default str) |
| 30 |
(fn (port (r row) (c col) string-width) |
| 31 |
(let ((nl-index (string-index-right str #\newline))) |
| 32 |
(write-string str port) |
| 33 |
(if (string-cursor>? nl-index (string-cursor-start str)) |
| 34 |
(with! (row (+ r (string-count str (lambda (ch) (eqv? ch #\newline))))) |
| 35 |
(col (string-width str (string-cursor->index str nl-index)))) |
| 36 |
(with! (col (+ c (string-width str)))))))) |
| 37 |
|
| 38 |
(define-computation-type make-show-env show-run |
| 39 |
(port (current-output-port)) |
| 40 |
(col 0) |
| 41 |
(row 0) |
| 42 |
(width 78) |
| 43 |
(radix 10) |
| 44 |
(pad-char #\space) |
| 45 |
(output output-default) |
| 46 |
(string-width substring-length) |
| 47 |
(substring/width substring) |
| 48 |
(substring/preserve #f) |
| 49 |
(word-separator? char-whitespace?) |
| 50 |
(ambiguous-is-wide? #f) |
| 51 |
(ellipsis "") |
| 52 |
(decimal-align #f) |
| 53 |
(decimal-sep #f) |
| 54 |
(comma-sep #f) |
| 55 |
(comma-rule #f) |
| 56 |
(sign-rule #f) |
| 57 |
(precision #f) |
| 58 |
(writer #f) |
| 59 |
(pretty-environment (interaction-environment)) |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
(define (show out . args) |
| 71 |
(let ((proc (each-in-list args))) |
| 72 |
(cond |
| 73 |
((output-port? out) |
| 74 |
(show-run (sequence (with! (port out)) proc))) |
| 75 |
((eq? #t out) |
| 76 |
(show-run (sequence (with! (port (current-output-port))) proc))) |
| 77 |
((eq? #f out) |
| 78 |
(call-with-output-string |
| 79 |
(lambda (out) |
| 80 |
(show-run (sequence (with! (port out)) proc))))) |
| 81 |
(else |
| 82 |
(error "unknown output to show" out))))) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
(define-syntax with |
| 88 |
(syntax-rules () |
| 89 |
((with params x ... y) |
| 90 |
(computation-with params (each x ... y))))) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
(define nothing (fn () (with!))) |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
(define (displayed x) |
| 100 |
(cond |
| 101 |
((procedure? x) x) |
| 102 |
((string? x) (fn ((output1 output)) (output1 x))) |
| 103 |
((char? x) (displayed (string x))) |
| 104 |
(else (written x)))) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
(define (written x) |
| 109 |
(fn (writer) ((or writer written-default) x))) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
(define (each-in-list args) |
| 114 |
(if (pair? args) |
| 115 |
(if (pair? (cdr args)) |
| 116 |
(sequence (displayed (car args)) (each-in-list (cdr args))) |
| 117 |
(fn () (displayed (car args)))) |
| 118 |
nothing)) |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
(define (each . args) |
| 124 |
(each-in-list args)) |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
(define (call-with-output producer consumer) |
| 129 |
(let ((out (open-output-string))) |
| 130 |
(forked (with ((port out) (output output-default)) producer) |
| 131 |
(fn () (consumer (get-output-string out)))))) |