| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
(define (take* ls n) |
| 9 |
(cond ((zero? n) '()) |
| 10 |
((pair? ls) (cons (car ls) (take* (cdr ls) (- n 1)))) |
| 11 |
(else '()))) |
| 12 |
|
| 13 |
(define (drop* ls n) |
| 14 |
(cond ((zero? n) ls) |
| 15 |
((pair? ls) (drop* (cdr ls) (- n 1))) |
| 16 |
(else ls))) |
| 17 |
|
| 18 |
(define (make-space n) (make-string n #\space)) |
| 19 |
(define (make-nl-space n) (string-append "\n" (make-string n #\space))) |
| 20 |
|
| 21 |
(define (call-with-output-string proc) |
| 22 |
(let ((out (open-output-string))) |
| 23 |
(proc out) |
| 24 |
(let ((res (get-output-string out))) |
| 25 |
(close-output-port out) |
| 26 |
res))) |
| 27 |
|
| 28 |
(define (joined/shares fmt ls shares . o) |
| 29 |
(let ((sep (displayed (if (pair? o) (car o) " ")))) |
| 30 |
(fn () |
| 31 |
(cond |
| 32 |
((null? ls) |
| 33 |
nothing) |
| 34 |
((pair? ls) |
| 35 |
(fn () |
| 36 |
(let lp ((ls ls)) |
| 37 |
(each |
| 38 |
(fmt (car ls)) |
| 39 |
(let ((rest (cdr ls))) |
| 40 |
(cond |
| 41 |
((null? rest) nothing) |
| 42 |
((pair? rest) |
| 43 |
(call-with-shared-ref/cdr rest |
| 44 |
shares |
| 45 |
each |
| 46 |
(fn () (lp rest)) |
| 47 |
sep)) |
| 48 |
(else (each sep ". " (fmt rest))))))))) |
| 49 |
(else (fmt ls)))))) |
| 50 |
|
| 51 |
(define (string-find/index str pred i) |
| 52 |
(string-cursor->index |
| 53 |
str |
| 54 |
(string-index str pred (string-index->cursor str i)))) |
| 55 |
|
| 56 |
(define (write-to-string x) |
| 57 |
(call-with-output-string (lambda (out) (write x out)))) |
| 58 |
|
| 59 |
(define (try-fitted2 proc fail) |
| 60 |
(fn (width string-width (orig-output output)) |
| 61 |
(let ((out (open-output-string))) |
| 62 |
(call-with-current-continuation |
| 63 |
(lambda (abort) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
(define (output* str) |
| 68 |
(fn (col) |
| 69 |
(let lp ((i 0) (col col)) |
| 70 |
(let ((nli (string-find/index str #\newline i)) |
| 71 |
(len (string-width str))) |
| 72 |
(if (< nli len) |
| 73 |
(if (> (+ (- nli i) col) width) |
| 74 |
(abort fail) |
| 75 |
(lp (+ nli 1) 0)) |
| 76 |
(let ((col (+ (- len i) col))) |
| 77 |
(cond |
| 78 |
((> col width) |
| 79 |
(abort fail)) |
| 80 |
(else |
| 81 |
(output-default str))))))))) |
| 82 |
(forked |
| 83 |
(with ((output output*) |
| 84 |
(port out)) |
| 85 |
proc) |
| 86 |
|
| 87 |
(fn () (orig-output (get-output-string out))))))))) |
| 88 |
|
| 89 |
(define (try-fitted proc . fail) |
| 90 |
(let lp ((proc proc) (ls fail)) |
| 91 |
(if (null? ls) |
| 92 |
proc |
| 93 |
(try-fitted2 proc (lp (car ls) (cdr ls)))))) |
| 94 |
|
| 95 |
(define (fits-in-width width proc set-failed!) |
| 96 |
(call-with-current-continuation |
| 97 |
(lambda (abort) |
| 98 |
(fn ((orig-output output)) |
| 99 |
(define (output* str) |
| 100 |
(each (orig-output str) |
| 101 |
(fn (col) |
| 102 |
(if (>= col width) |
| 103 |
(begin (set-failed! #t) (abort #f)) |
| 104 |
nothing)))) |
| 105 |
(with ((output output*)) |
| 106 |
proc))))) |
| 107 |
|
| 108 |
(define (fits-in-columns width ls writer set-result!) |
| 109 |
(let ((max-w (quotient width 2))) |
| 110 |
(fn (string-width) |
| 111 |
(let lp ((ls ls) (res '()) (widest 0)) |
| 112 |
(cond |
| 113 |
((pair? ls) |
| 114 |
(let ((failed? #f)) |
| 115 |
(call-with-output |
| 116 |
(fits-in-width max-w |
| 117 |
(writer (car ls)) |
| 118 |
(lambda (x) (set! failed? x))) |
| 119 |
(lambda (str) |
| 120 |
(if failed? |
| 121 |
(begin |
| 122 |
(set-result! #f) |
| 123 |
nothing) |
| 124 |
(lp (cdr ls) |
| 125 |
(cons str res) |
| 126 |
(max (string-width str) widest))))))) |
| 127 |
((null? ls) (set-result! (cons widest (reverse res))) nothing) |
| 128 |
(else (set-result! #f) nothing)))))) |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
(define syntax-abbrevs |
| 134 |
'((quote . "'") (quasiquote . "`") |
| 135 |
(unquote . ",") (unquote-splicing . ",@") |
| 136 |
)) |
| 137 |
|
| 138 |
(define (pp-let ls pp shares color?) |
| 139 |
(if (and (pair? (cdr ls)) (symbol? (cadr ls))) |
| 140 |
(pp-with-indent 2 ls pp shares color?) |
| 141 |
(pp-with-indent 1 ls pp shares color?))) |
| 142 |
|
| 143 |
(define indent-rules |
| 144 |
`((lambda . 1) (define . 1) (define-syntax . 1) |
| 145 |
(let . ,pp-let) (loop . ,pp-let) |
| 146 |
(let* . 1) (letrec . 1) (letrec* . 1) (and-let* . 1) (let1 . 2) |
| 147 |
(let-values . 1) (let*-values . 1) (receive . 2) (parameterize . 1) |
| 148 |
(let-syntax . 1) (letrec-syntax . 1) (syntax-rules . 1) (syntax-case . 2) |
| 149 |
(match . 1) (match-let . 1) (match-let* . 1) |
| 150 |
(if . 3) (when . 1) (unless . 1) (case . 1) (while . 1) (until . 1) |
| 151 |
(do . 2) (dotimes . 1) (dolist . 1) (test . 1) |
| 152 |
(condition-case . 1) (guard . 1) (rec . 1) |
| 153 |
(call-with-current-continuation . 0) |
| 154 |
)) |
| 155 |
|
| 156 |
(define indent-prefix-rules |
| 157 |
`(("with-" . -1) ("call-with-" . -1) ("define-" . 1)) |
| 158 |
) |
| 159 |
|
| 160 |
(define indent-suffix-rules |
| 161 |
`(("-case" . 1)) |
| 162 |
) |
| 163 |
|
| 164 |
(define pp-macros |
| 165 |
(append |
| 166 |
(map car indent-rules) |
| 167 |
'(quote quasiquote unquote unquote-splicing set! cond-expand cond ))) |
| 168 |
|
| 169 |
(define (pp-indentation form) |
| 170 |
(let ((indent |
| 171 |
(cond |
| 172 |
((assq (car form) indent-rules) => cdr) |
| 173 |
((and (symbol? (car form)) |
| 174 |
(let ((str (symbol->string (car form)))) |
| 175 |
(or (find (lambda (rx) (string-prefix? (car rx) str)) |
| 176 |
indent-prefix-rules) |
| 177 |
(find (lambda (rx) (string-suffix? (car rx) str)) |
| 178 |
indent-suffix-rules)))) |
| 179 |
=> cdr) |
| 180 |
(else #f)))) |
| 181 |
(if (and (number? indent) (negative? indent)) |
| 182 |
(max 0 (- (+ (or (length+ form) +inf.0) indent) 1)) |
| 183 |
indent))) |
| 184 |
|
| 185 |
(define (with-reset-shares shares proc) |
| 186 |
(let ((orig-count (cdr shares))) |
| 187 |
(fn () |
| 188 |
(let ((new-count (cdr shares))) |
| 189 |
(when (> new-count orig-count) |
| 190 |
(hash-table-walk |
| 191 |
(car shares) |
| 192 |
(lambda (k v) |
| 193 |
(if (and (cdr v) (>= (car v) orig-count)) |
| 194 |
(set-cdr! v #f)))) |
| 195 |
(set-cdr! shares orig-count)) |
| 196 |
proc)))) |
| 197 |
|
| 198 |
(define (pp-with-indent indent-rule ls pp shares color?) |
| 199 |
(fn ((col1 col)) |
| 200 |
(each |
| 201 |
"(" |
| 202 |
((if (and color? (memq (car ls) pp-macros)) as-blue displayed) |
| 203 |
(pp (car ls))) |
| 204 |
(fn ((col2 col) width string-width) |
| 205 |
(let ((fixed (take* (cdr ls) (or indent-rule 1))) |
| 206 |
(tail (drop* (cdr ls) (or indent-rule 1))) |
| 207 |
(default |
| 208 |
(let ((sep (make-nl-space (+ col1 1)))) |
| 209 |
(fn () (each sep (joined/shares pp (cdr ls) shares sep))))) |
| 210 |
|
| 211 |
(reset-shares (with-reset-shares shares nothing))) |
| 212 |
(call-with-output |
| 213 |
(trimmed/lazy (- width col2) |
| 214 |
(each (if (or (null? fixed) (pair? fixed)) " " " . ") |
| 215 |
(joined/shares |
| 216 |
(lambda (x) (pp-flat x pp shares color?)) |
| 217 |
fixed shares " "))) |
| 218 |
(lambda (first-line) |
| 219 |
(cond |
| 220 |
((< (+ col2 (string-width first-line)) width) |
| 221 |
|
| 222 |
(let ((sep (make-nl-space |
| 223 |
(if indent-rule (+ col1 2) (+ col2 1))))) |
| 224 |
(each first-line |
| 225 |
(cond |
| 226 |
((not (or (null? tail) (pair? tail))) |
| 227 |
(each ". " (pp tail))) |
| 228 |
((> (or (length+ (cdr ls)) +inf.0) (or indent-rule 1)) |
| 229 |
(each sep (joined/shares pp tail shares sep))) |
| 230 |
(else |
| 231 |
nothing))))) |
| 232 |
(indent-rule |
| 233 |
|
| 234 |
(try-fitted |
| 235 |
(each |
| 236 |
reset-shares |
| 237 |
" " |
| 238 |
(joined/shares pp fixed shares (make-nl-space (+ col2 1))) |
| 239 |
(if (pair? tail) |
| 240 |
(let ((sep (make-nl-space (+ col1 2)))) |
| 241 |
(each sep (joined/shares pp tail shares sep))) |
| 242 |
nothing)) |
| 243 |
(each reset-shares default))) |
| 244 |
(else |
| 245 |
|
| 246 |
(each reset-shares default))))))) |
| 247 |
")"))) |
| 248 |
|
| 249 |
(define (pp-app ls pp shares color?) |
| 250 |
(let ((indent-rule (pp-indentation ls))) |
| 251 |
(if (procedure? indent-rule) |
| 252 |
(indent-rule ls pp shares color?) |
| 253 |
(pp-with-indent indent-rule ls pp shares color?)))) |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
(define (proper-non-shared-list? ls shares) |
| 258 |
(let ((tab (car shares))) |
| 259 |
(let lp ((ls ls)) |
| 260 |
(or (null? ls) |
| 261 |
(and (pair? ls) |
| 262 |
(not (hash-table-ref/default tab ls #f)) |
| 263 |
(lp (cdr ls))))))) |
| 264 |
|
| 265 |
(define (non-app? x) |
| 266 |
(if (pair? x) |
| 267 |
(or (not (or (null? (cdr x)) (pair? (cdr x)))) |
| 268 |
(non-app? (car x))) |
| 269 |
(not (symbol? x)))) |
| 270 |
|
| 271 |
(define (pp-data-list ls pp shares) |
| 272 |
(each |
| 273 |
"(" |
| 274 |
(fn (col width string-width) |
| 275 |
(let ((avail (- width col))) |
| 276 |
(cond |
| 277 |
((and (pair? (cdr ls)) (pair? (cddr ls)) (pair? (cdr (cddr ls)))) |
| 278 |
(let ((out (open-output-string)) |
| 279 |
(result #f)) |
| 280 |
(call-with-output |
| 281 |
(fits-in-columns width ls (lambda (x) (pp-flat x pp shares #f)) |
| 282 |
(lambda (res) (set! result res))) |
| 283 |
(lambda (str) |
| 284 |
(fn () |
| 285 |
(if (not result) |
| 286 |
|
| 287 |
(joined/shares pp ls shares (make-nl-space col)) |
| 288 |
|
| 289 |
(let* ((prefix (make-nl-space col)) |
| 290 |
(widest (+ 1 (car result))) |
| 291 |
(columns (quotient width widest))) |
| 292 |
(let lp ((ls (cdr result)) (i 1)) |
| 293 |
(cond |
| 294 |
((null? ls) |
| 295 |
nothing) |
| 296 |
((null? (cdr ls)) |
| 297 |
(displayed (car ls))) |
| 298 |
((>= i columns) |
| 299 |
(each (car ls) |
| 300 |
prefix |
| 301 |
(fn () (lp (cdr ls) 1)))) |
| 302 |
(else |
| 303 |
(let ((pad (- widest (string-width (car ls))))) |
| 304 |
(each (car ls) |
| 305 |
(make-space pad) |
| 306 |
(lp (cdr ls) (+ i 1)))))))))))))) |
| 307 |
(else |
| 308 |
|
| 309 |
(joined/shares pp ls shares (make-nl-space col)))))) |
| 310 |
")")) |
| 311 |
|
| 312 |
(define (pp-flat x pp shares color?) |
| 313 |
(define (ppf x) |
| 314 |
(pp-flat x pp shares color?)) |
| 315 |
(cond |
| 316 |
((pair? x) |
| 317 |
(cond |
| 318 |
((and (pair? (cdr x)) (null? (cddr x)) |
| 319 |
(assq (car x) syntax-abbrevs)) |
| 320 |
=> (lambda (abbrev) |
| 321 |
(each (cdr abbrev) |
| 322 |
(call-with-shared-ref |
| 323 |
(cadr x) |
| 324 |
shares |
| 325 |
each |
| 326 |
(pp-flat (cadr x) pp shares color?))))) |
| 327 |
(else |
| 328 |
(fn () |
| 329 |
(each "(" |
| 330 |
((if (and color? (memq (car x) pp-macros)) as-blue displayed) |
| 331 |
(pp (car x))) |
| 332 |
(if (null? (cdr x)) |
| 333 |
nothing |
| 334 |
(call-with-shared-ref/cdr |
| 335 |
(cdr x) |
| 336 |
shares |
| 337 |
each |
| 338 |
(cond |
| 339 |
((pair? (cdr x)) |
| 340 |
(each "" (joined/shares ppf (cdr x) shares " "))) |
| 341 |
(else |
| 342 |
(each ". " (joined/shares ppf (cdr x) shares " ")))) |
| 343 |
" ")) |
| 344 |
")"))))) |
| 345 |
((vector? x) |
| 346 |
(each "#(" |
| 347 |
(joined/shares ppf (vector->list x) shares " ") |
| 348 |
")")) |
| 349 |
(else |
| 350 |
(pp x)))) |
| 351 |
|
| 352 |
(define (pp-pair ls pp shares color?) |
| 353 |
(cond |
| 354 |
|
| 355 |
((null? (cdr ls)) |
| 356 |
(each "(" (pp (car ls)) ")")) |
| 357 |
|
| 358 |
((and (pair? (cdr ls)) (null? (cddr ls)) |
| 359 |
(assq (car ls) syntax-abbrevs)) |
| 360 |
=> (lambda (abbrev) |
| 361 |
(each (cdr abbrev) (pp (cadr ls))))) |
| 362 |
(else |
| 363 |
(let ((reset-shares (with-reset-shares shares nothing))) |
| 364 |
(try-fitted |
| 365 |
(pp-flat ls pp shares color?) |
| 366 |
(each |
| 367 |
reset-shares |
| 368 |
(fn () |
| 369 |
(if (and (non-app? ls) |
| 370 |
(proper-non-shared-list? ls shares)) |
| 371 |
(pp-data-list ls pp shares) |
| 372 |
(pp-app ls pp shares color?))))))))) |
| 373 |
|
| 374 |
(define (pp-vector vec pp shares) |
| 375 |
(each "#" (pp-data-list (vector->list vec) pp shares))) |
| 376 |
|
| 377 |
|
| 378 |
(define (pp obj shares color?) |
| 379 |
(fn (radix precision) |
| 380 |
(let ((write-number |
| 381 |
(cond |
| 382 |
((and (not precision) |
| 383 |
(assv radix '((16 . "#x") (10 . "") (8 . "#o") (2 . "#b")))) |
| 384 |
=> (lambda (cell) |
| 385 |
(lambda (n) |
| 386 |
(if (or (exact? n) (eqv? radix 10)) |
| 387 |
(each (cdr cell) (number->string n (car cell))) |
| 388 |
(with ((radix 10)) (numeric n)))))) |
| 389 |
(else (lambda (n) (with ((radix 10)) (numeric n))))))) |
| 390 |
(let pp ((obj obj)) |
| 391 |
(call-with-shared-ref |
| 392 |
obj shares each |
| 393 |
(fn () |
| 394 |
(cond |
| 395 |
((pair? obj) |
| 396 |
(pp-pair obj pp shares color?)) |
| 397 |
((vector? obj) |
| 398 |
(pp-vector obj pp shares)) |
| 399 |
((number? obj) |
| 400 |
(write-number obj)) |
| 401 |
((and color? (string? obj)) |
| 402 |
(as-green (write-to-string obj))) |
| 403 |
(else |
| 404 |
(displayed (write-to-string obj)))))))))) |
| 405 |
|
| 406 |
(define (pretty obj) |
| 407 |
(fn () |
| 408 |
(call-with-output |
| 409 |
(each (pp obj (extract-shared-objects obj #t) #f) |
| 410 |
fl) |
| 411 |
displayed))) |
| 412 |
|
| 413 |
(define (pretty-shared obj) |
| 414 |
(fn () |
| 415 |
(call-with-output |
| 416 |
(each (pp obj (extract-shared-objects obj #f) #f) |
| 417 |
fl) |
| 418 |
displayed))) |
| 419 |
|
| 420 |
(define (pretty-simply obj) |
| 421 |
(fn () |
| 422 |
(each (pp obj (extract-shared-objects #f #f) #f) |
| 423 |
fl))) |
| 424 |
|
| 425 |
(define (pretty-with-color obj) |
| 426 |
(fn () |
| 427 |
(call-with-output |
| 428 |
(each (pp obj (extract-shared-objects obj #t) #t) |
| 429 |
fl) |
| 430 |
displayed))) |