| 1 |
;; column.scm -- formatting columns and tables |
| 2 |
;; Copyright (c) 2006-2017 Alex Shinn. All rights reserved. |
| 3 |
;; BSD-style license: http://synthcode.com/license.txt |
| 4 |
|
| 5 |
(define (string-split-words str separator?) |
| 6 |
(let ((start (string-cursor-start str)) |
| 7 |
(end (string-cursor-end str))) |
| 8 |
(let lp ((sc start) (res '())) |
| 9 |
(cond |
| 10 |
((string-cursor>=? sc end) |
| 11 |
(reverse res)) |
| 12 |
(else |
| 13 |
(let ((sc2 (string-index str separator? sc))) |
| 14 |
(lp (if (string-cursor=? sc2 end) sc2 (string-cursor-next str sc2)) |
| 15 |
(if (string-cursor=? sc sc2) |
| 16 |
res |
| 17 |
(cons (substring/cursors str sc sc2) res))))))))) |
| 18 |
|
| 19 |
(define (call-with-output-generator producer consumer) |
| 20 |
(fn () |
| 21 |
(let ((out (open-output-string)) |
| 22 |
(queue (list-queue)) |
| 23 |
(return #f) |
| 24 |
(resume #f)) |
| 25 |
(define eof (read-char (open-input-string ""))) |
| 26 |
(define (output* str) |
| 27 |
(fn (row col string-width) |
| 28 |
(list-queue-add-back! queue str) |
| 29 |
(each |
| 30 |
(let ((nl-index |
| 31 |
(string-index-right str (lambda (ch) (eqv? ch #\newline))))) |
| 32 |
(if (string-cursor>? nl-index (string-cursor-start str)) |
| 33 |
(with! |
| 34 |
(row (+ row (string-count str (lambda (ch) (eqv? ch #\newline))))) |
| 35 |
(col (string-width str (string-cursor->index str nl-index)))) |
| 36 |
(with! (col (+ col (string-width str)))))) |
| 37 |
(call-with-current-continuation |
| 38 |
(lambda (cc) |
| 39 |
(set! resume cc) |
| 40 |
(return nothing)))) |
| 41 |
nothing)) |
| 42 |
(define (generate) |
| 43 |
(when (and resume (list-queue-empty? queue)) |
| 44 |
(call-with-current-continuation |
| 45 |
(lambda (cc) |
| 46 |
(set! return cc) |
| 47 |
(resume nothing)))) |
| 48 |
(if (list-queue-empty? queue) |
| 49 |
eof |
| 50 |
(list-queue-remove-front! queue))) |
| 51 |
(forked (fn () (with ((port out) (output output*)) |
| 52 |
(call-with-current-continuation |
| 53 |
(lambda (cc) |
| 54 |
(set! return cc) |
| 55 |
(each producer |
| 56 |
(fn (output) |
| 57 |
(set! resume #f) |
| 58 |
(fn () (return nothing) nothing))))))) |
| 59 |
(consumer generate))))) |
| 60 |
|
| 61 |
(define (call-with-output-generators producers consumer) |
| 62 |
(let lp ((ls producers) (generators '())) |
| 63 |
(if (null? ls) |
| 64 |
(consumer (reverse generators)) |
| 65 |
(call-with-output-generator |
| 66 |
(car ls) |
| 67 |
(lambda (generator) |
| 68 |
(lp (cdr ls) (cons generator generators))))))) |
| 69 |
|
| 70 |
(define (string->line-generator source) |
| 71 |
(let ((str '()) |
| 72 |
(scanned? #f)) |
| 73 |
(define (gen) |
| 74 |
(if (pair? str) |
| 75 |
(if scanned? |
| 76 |
(let ((res (source))) |
| 77 |
(cond |
| 78 |
((eof-object? res) |
| 79 |
(let ((res (string-concatenate (reverse str)))) |
| 80 |
(set! str '()) |
| 81 |
res)) |
| 82 |
((equal? res "") |
| 83 |
(gen)) |
| 84 |
(else |
| 85 |
(set! str (cons res str)) |
| 86 |
(set! scanned? #f) |
| 87 |
(gen)))) |
| 88 |
(let ((nl (string-index (car str) #\newline)) |
| 89 |
(end (string-cursor-end (car str)))) |
| 90 |
(cond |
| 91 |
((string-cursor<? nl end) |
| 92 |
(let* ((left (substring/cursors |
| 93 |
(car str) |
| 94 |
(string-cursor-start (car str)) |
| 95 |
nl)) |
| 96 |
(right (substring/cursors |
| 97 |
(car str) |
| 98 |
(string-cursor-next (car str) nl) |
| 99 |
end)) |
| 100 |
(res (string-concatenate |
| 101 |
(reverse (cons left (cdr str)))))) |
| 102 |
(set! str (if (equal? right "") '() (list right))) |
| 103 |
res)) |
| 104 |
(else |
| 105 |
(set! scanned? #t) |
| 106 |
(gen))))) |
| 107 |
(let ((res (source))) |
| 108 |
(cond |
| 109 |
((eof-object? res) |
| 110 |
res) |
| 111 |
((equal? res "") |
| 112 |
(gen)) |
| 113 |
(else |
| 114 |
(set! str (cons res str)) |
| 115 |
(set! scanned? #f) |
| 116 |
(gen)))))) |
| 117 |
gen)) |
| 118 |
|
| 119 |
(define-record-type Column |
| 120 |
(make-column format generate infinite?) |
| 121 |
column? |
| 122 |
(format column-format) |
| 123 |
(generate column-generate) |
| 124 |
(infinite? column-infinite?)) |
| 125 |
|
| 126 |
;; (show-columns (fmt gen [infinite?]) ...) |
| 127 |
(define (show-columns . ls) |
| 128 |
(fn () |
| 129 |
(let* ((cols (map (lambda (x) |
| 130 |
(make-column (or (car x) displayed) |
| 131 |
(displayed (cadr x)) |
| 132 |
(and (pair? (cddr x)) (car (cddr x))))) |
| 133 |
ls)) |
| 134 |
(num-infinite (count column-infinite? cols))) |
| 135 |
(call-with-output-generators |
| 136 |
(map column-generate cols) |
| 137 |
(lambda (gens) |
| 138 |
(let ((gens (map string->line-generator gens))) |
| 139 |
(let lp () |
| 140 |
(let* ((lines (map (lambda (gen) (gen)) gens)) |
| 141 |
(num-present (count string? lines))) |
| 142 |
(if (<= num-present num-infinite) |
| 143 |
nothing |
| 144 |
(each |
| 145 |
(each-in-list |
| 146 |
(map (lambda (col line) |
| 147 |
((column-format col) |
| 148 |
(if (eof-object? line) "" line))) |
| 149 |
cols |
| 150 |
lines)) |
| 151 |
"\n" |
| 152 |
(fn () (lp)))))))))))) |
| 153 |
|
| 154 |
;; (columnar ['infinite|'right|'left|'center|width] string-or-formatter ...) |
| 155 |
(define (columnar . ls) |
| 156 |
(define (proportional-width? w) |
| 157 |
(and (number? w) |
| 158 |
(or (< 0 w 1) |
| 159 |
(and (inexact? w) (= w 1.0))))) |
| 160 |
(define (build-column ls) |
| 161 |
(let-optionals* ls ((fixed-width #f) |
| 162 |
(col-width #f) |
| 163 |
(last? #t) |
| 164 |
(tail '()) |
| 165 |
(gen #f) |
| 166 |
(prefix '()) |
| 167 |
(align 'left) |
| 168 |
(infinite? #f)) |
| 169 |
(define (scale-width width) |
| 170 |
(max 1 (exact (truncate (* col-width (- width fixed-width)))))) |
| 171 |
(define (padder) |
| 172 |
(if (proportional-width? col-width) |
| 173 |
(case align |
| 174 |
((right) |
| 175 |
(lambda (str) (fn (width) (padded/left (scale-width width) str)))) |
| 176 |
((center) |
| 177 |
(lambda (str) (fn (width) (padded/both (scale-width width) str)))) |
| 178 |
(else |
| 179 |
(lambda (str) (fn (width) (padded/right (scale-width width) str))))) |
| 180 |
(case align |
| 181 |
((right) (lambda (str) (padded/left col-width str))) |
| 182 |
((center) (lambda (str) (padded/both col-width str))) |
| 183 |
(else (lambda (str) (padded/right col-width str)))))) |
| 184 |
(define (affix x) |
| 185 |
(cond |
| 186 |
((pair? tail) |
| 187 |
(lambda (str) |
| 188 |
(each (each-in-list prefix) |
| 189 |
(x str) |
| 190 |
(each-in-list tail)))) |
| 191 |
((pair? prefix) |
| 192 |
(lambda (str) (each (each-in-list prefix) (x str)))) |
| 193 |
(else (displayed x)))) |
| 194 |
(list |
| 195 |
;; line formatter |
| 196 |
(affix |
| 197 |
(let ((pad (padder))) |
| 198 |
(if (and last? (not (pair? tail)) (eq? align 'left)) |
| 199 |
(lambda (str) |
| 200 |
(fn (pad-char) |
| 201 |
((if (or (not pad-char) (char-whitespace? pad-char)) |
| 202 |
displayed |
| 203 |
pad) |
| 204 |
str))) |
| 205 |
pad))) |
| 206 |
;; generator |
| 207 |
(if (proportional-width? col-width) |
| 208 |
(fn (width) |
| 209 |
(with ((width (scale-width width))) |
| 210 |
gen)) |
| 211 |
(with ((width col-width)) gen)) |
| 212 |
infinite?))) |
| 213 |
(define (adjust-widths ls border-width) |
| 214 |
(let* ((fixed-ls |
| 215 |
(filter (lambda (x) (and (number? (car x)) (>= (car x) 1))) ls)) |
| 216 |
(fixed-total (fold + border-width (map car fixed-ls))) |
| 217 |
(scaled-ls (filter (lambda (x) (proportional-width? (car x))) ls)) |
| 218 |
(denom (- (length ls) (+ (length fixed-ls) (length scaled-ls)))) |
| 219 |
(rest (if (zero? denom) |
| 220 |
0 |
| 221 |
(inexact |
| 222 |
(/ (- 1 (fold + 0 (map car scaled-ls))) denom))))) |
| 223 |
(if (negative? rest) |
| 224 |
(error "fractional widths must sum to less than 1" |
| 225 |
(map car scaled-ls))) |
| 226 |
(map |
| 227 |
(lambda (col) |
| 228 |
(cons fixed-total |
| 229 |
(if (not (number? (car col))) |
| 230 |
(cons rest (cdr col)) |
| 231 |
col))) |
| 232 |
ls))) |
| 233 |
(define (finish ls border-width) |
| 234 |
(apply show-columns |
| 235 |
(map build-column (adjust-widths (reverse ls) border-width)))) |
| 236 |
(let lp ((ls ls) (strs '()) (align 'left) (infinite? #f) |
| 237 |
(width #t) (border-width 0) (res '())) |
| 238 |
(cond |
| 239 |
((null? ls) |
| 240 |
(if (pair? strs) |
| 241 |
(finish (cons (cons (caar res) |
| 242 |
(cons #t (cons (append (reverse strs) |
| 243 |
(cadr (cdar res))) |
| 244 |
(cddr (cdar res))))) |
| 245 |
(cdr res)) |
| 246 |
border-width) |
| 247 |
(finish (cons (cons (caar res) (cons #t (cddr (car res)))) (cdr res)) |
| 248 |
border-width))) |
| 249 |
((char? (car ls)) |
| 250 |
(lp (cons (string (car ls)) (cdr ls)) strs align infinite? |
| 251 |
width border-width res)) |
| 252 |
((string? (car ls)) |
| 253 |
(if (string-contains "\n" (car ls)) |
| 254 |
(error "column string literals can't contain newlines") |
| 255 |
(lp (cdr ls) (cons (car ls) strs) align infinite? |
| 256 |
width (+ border-width (string-length (car ls))) res))) |
| 257 |
((number? (car ls)) |
| 258 |
(lp (cdr ls) strs align infinite? (car ls) border-width res)) |
| 259 |
((eq? (car ls) 'infinite) |
| 260 |
(lp (cdr ls) strs align #t width border-width res)) |
| 261 |
((symbol? (car ls)) |
| 262 |
(lp (cdr ls) strs (car ls) infinite? width border-width res)) |
| 263 |
((procedure? (car ls)) |
| 264 |
(lp (cdr ls) '() 'left #f #t border-width |
| 265 |
(cons (list width #f '() (car ls) (reverse strs) align infinite?) |
| 266 |
res))) |
| 267 |
(else |
| 268 |
(error "invalid column" (car ls)))))) |
| 269 |
|
| 270 |
(define (max-line-width string-width str) |
| 271 |
(let ((end (string-cursor-end str))) |
| 272 |
(let lp ((i (string-cursor-start str)) (hi 0)) |
| 273 |
(let ((j (string-index str #\newline i))) |
| 274 |
(if (string-cursor<? j end) |
| 275 |
(lp (string-cursor-next str j) |
| 276 |
(max hi (string-width (substring/cursors str i j)))) |
| 277 |
(max hi (string-width (substring/cursors str i end)))))))) |
| 278 |
|
| 279 |
(define (pad-finite proc width string-width k) |
| 280 |
(call-with-output |
| 281 |
proc |
| 282 |
(lambda (str) |
| 283 |
(let ((w (max-line-width (or string-width string-length) str))) |
| 284 |
(k (displayed str) |
| 285 |
(if (and (integer? width) (exact? width)) |
| 286 |
(max width w) |
| 287 |
w)))))) |
| 288 |
|
| 289 |
(define (tabular . ls) |
| 290 |
(fn (string-width) |
| 291 |
(let lp ((ls ls) (infinite? #f) (width #t) (res '())) |
| 292 |
(cond |
| 293 |
((null? ls) |
| 294 |
(apply columnar (reverse res))) |
| 295 |
((number? (car ls)) |
| 296 |
(lp (cdr ls) infinite? (car ls) res)) |
| 297 |
((eq? 'infinite (car ls)) |
| 298 |
(lp (cdr ls) #t width (cons (car ls) res))) |
| 299 |
((procedure? (car ls)) |
| 300 |
(if infinite? |
| 301 |
(if width |
| 302 |
(lp (cdr ls) #f #t (cons (car ls) (cons width res))) |
| 303 |
(lp (cdr ls) #f #t (cons (car ls) res))) |
| 304 |
(pad-finite (car ls) width string-width |
| 305 |
(lambda (gen width) |
| 306 |
(lp (cdr ls) #f #t (cons gen (cons width res))))))) |
| 307 |
(else |
| 308 |
(lp (cdr ls) infinite? width (cons (car ls) res))))))) |
| 309 |
|
| 310 |
;; break lines only, don't join short lines or justify |
| 311 |
(define (wrapped/char . ls) |
| 312 |
(fn (output width string-width) |
| 313 |
(define (kons-in-line str) |
| 314 |
(fn (col) |
| 315 |
(let ((len ((or string-width string-length) str)) |
| 316 |
(space (- width col))) |
| 317 |
(cond |
| 318 |
((equal? "" str) |
| 319 |
nothing) |
| 320 |
((or (<= len space) (not (positive? space))) |
| 321 |
(each (output str) (output "\n"))) |
| 322 |
(else |
| 323 |
(each |
| 324 |
;; TODO: when splitting by string-width, substring needs |
| 325 |
;; to be provided |
| 326 |
(output (substring str 0 space)) |
| 327 |
(output "\n") |
| 328 |
(fn () (kons-in-line (substring str space len))))))))) |
| 329 |
(with ((output |
| 330 |
(lambda (str) |
| 331 |
(let ((end (string-cursor-end str))) |
| 332 |
(let lp ((i (string-cursor-start str))) |
| 333 |
(let ((nli (string-index str #\newline i))) |
| 334 |
(cond |
| 335 |
((string-cursor>=? i end) |
| 336 |
nothing) |
| 337 |
((string-cursor>=? nli end) |
| 338 |
(kons-in-line (substring/cursors str i end))) |
| 339 |
(else |
| 340 |
(each |
| 341 |
(fn () (kons-in-line (substring/cursors str i nli))) |
| 342 |
(fn () (lp (string-cursor-next str nli)))))))))))) |
| 343 |
(each-in-list ls)))) |
| 344 |
|
| 345 |
;; `seq' is a list or vector of pre-tokenized words. `line' is called |
| 346 |
;; on each wrapped line and the accumulator, starting with `knil'. |
| 347 |
;; The optional `last-line' is used instead on the last line of the |
| 348 |
;; paragraph. |
| 349 |
(define (wrap-fold-words seq knil max-width get-width line . o) |
| 350 |
(let* ((last-line (if (pair? o) (car o) line)) |
| 351 |
(vec (if (list? seq) (list->vector seq) seq)) |
| 352 |
(len (vector-length vec)) |
| 353 |
(len-1 (- len 1)) |
| 354 |
(breaks (make-vector len #f)) |
| 355 |
(penalties (make-vector len #f)) |
| 356 |
(widths |
| 357 |
(list->vector |
| 358 |
(map get-width (if (list? seq) seq (vector->list vec)))))) |
| 359 |
(define (largest-fit i) |
| 360 |
(let lp ((j (+ i 1)) (width (vector-ref widths i))) |
| 361 |
(let ((width (+ width 1 (vector-ref widths j)))) |
| 362 |
(cond |
| 363 |
((>= width max-width) (- j 1)) |
| 364 |
((>= j len-1) len-1) |
| 365 |
(else (lp (+ j 1) width)))))) |
| 366 |
(define (min-penalty! i) |
| 367 |
(cond |
| 368 |
((>= i len-1) 0) |
| 369 |
((vector-ref penalties i)) |
| 370 |
(else |
| 371 |
(vector-set! penalties i (expt (+ max-width 1) 3)) |
| 372 |
(vector-set! breaks i i) |
| 373 |
(let ((k (largest-fit i))) |
| 374 |
(let lp ((j i) (width 0)) |
| 375 |
(if (<= j k) |
| 376 |
(let* ((width (+ width (vector-ref widths j))) |
| 377 |
(break-penalty |
| 378 |
(+ (max 0 (expt (- max-width (+ width (- j i))) 3)) |
| 379 |
(min-penalty! (+ j 1))))) |
| 380 |
(cond |
| 381 |
((< break-penalty (vector-ref penalties i)) |
| 382 |
(vector-set! breaks i j) |
| 383 |
(vector-set! penalties i break-penalty))) |
| 384 |
(lp (+ j 1) width))))) |
| 385 |
(if (>= (vector-ref breaks i) len-1) |
| 386 |
(vector-set! penalties i 0)) |
| 387 |
(vector-ref penalties i)))) |
| 388 |
(define (sub-list i j) |
| 389 |
(let lp ((i i) (res '())) |
| 390 |
(if (> i j) |
| 391 |
(reverse res) |
| 392 |
(lp (+ i 1) (cons (vector-ref vec i) res))))) |
| 393 |
(cond |
| 394 |
((zero? len) |
| 395 |
;; degenerate case |
| 396 |
(last-line '() knil)) |
| 397 |
(else |
| 398 |
;; compute optimum breaks |
| 399 |
(vector-set! breaks len-1 len-1) |
| 400 |
(vector-set! penalties len-1 0) |
| 401 |
(min-penalty! 0) |
| 402 |
;; fold |
| 403 |
(let lp ((i 0) (acc knil)) |
| 404 |
(let ((break (vector-ref breaks i))) |
| 405 |
(if (>= break len-1) |
| 406 |
(last-line (sub-list i len-1) acc) |
| 407 |
(lp (+ break 1) (line (sub-list i break) acc))))))))) |
| 408 |
|
| 409 |
(define (wrapped/list ls) |
| 410 |
(fn (width string-width pad-char) |
| 411 |
(joined/suffix |
| 412 |
(lambda (ls) (joined displayed ls pad-char)) |
| 413 |
(reverse |
| 414 |
(wrap-fold-words ls '() width (or string-width string-length) cons)) |
| 415 |
"\n"))) |
| 416 |
|
| 417 |
(define (wrapped . ls) |
| 418 |
(call-with-output |
| 419 |
(each-in-list ls) |
| 420 |
(lambda (str) |
| 421 |
(fn (word-separator?) |
| 422 |
(wrapped/list |
| 423 |
(string-split-words str (or word-separator? char-whitespace?))))))) |
| 424 |
|
| 425 |
(define (justified . ls) |
| 426 |
(fn (output width string-width) |
| 427 |
(define (justify-line ls) |
| 428 |
(if (null? ls) |
| 429 |
nl |
| 430 |
(let* ((sum (fold (lambda (s n) |
| 431 |
(+ n ((or string-width string-length) s))) |
| 432 |
0 ls)) |
| 433 |
(len (length ls)) |
| 434 |
(diff (max 0 (- width sum))) |
| 435 |
(sep (make-string (if (= len 1) |
| 436 |
0 |
| 437 |
(quotient diff (- len 1))) |
| 438 |
#\space)) |
| 439 |
(rem (if (= len 1) |
| 440 |
diff |
| 441 |
(remainder diff (- len 1)))) |
| 442 |
(p (open-output-string))) |
| 443 |
(display (car ls) p) |
| 444 |
(let lp ((ls (cdr ls)) (i 1)) |
| 445 |
(when (pair? ls) |
| 446 |
(display sep p) |
| 447 |
(if (<= i rem) (write-char #\space p)) |
| 448 |
(display (car ls) p) |
| 449 |
(lp (cdr ls) (+ i 1)))) |
| 450 |
(displayed (get-output-string p))))) |
| 451 |
(define (justify-last ls) |
| 452 |
(each (joined displayed ls " ") "\n")) |
| 453 |
(call-with-output |
| 454 |
(each-in-list ls) |
| 455 |
(lambda (str) |
| 456 |
(fn (word-separator?) |
| 457 |
(joined/last |
| 458 |
justify-line |
| 459 |
justify-last |
| 460 |
(reverse |
| 461 |
(wrap-fold-words |
| 462 |
(string-split-words str (or word-separator? char-whitespace?)) |
| 463 |
'() width (or string-width string-length) |
| 464 |
cons)) |
| 465 |
"\n")))))) |
| 466 |
|
| 467 |
(define (from-file path . ls) |
| 468 |
(let-optionals* ls ((sep nl)) |
| 469 |
(fn () |
| 470 |
(let ((in (open-input-file path))) |
| 471 |
(let lp () |
| 472 |
(let ((line (read-line in))) |
| 473 |
(if (eof-object? line) |
| 474 |
(begin (close-input-port in) nothing) |
| 475 |
(each line sep |
| 476 |
(fn () (lp)))))))))) |
| 477 |
|
| 478 |
(define (line-numbers . o) |
| 479 |
(let ((start (if (pair? o) (car o) 1))) |
| 480 |
(joined/range displayed start #f "\n"))) |