Debian Codegraph @

%3a166/column.scm

raw symbols SCIP
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 (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 (fn () (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 (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 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 ((orig-width width))
209 (with ((width (scale-width orig-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 (cond
241 ((null? res) nl)
242 ((pair? strs)
243 (finish (cons (cons (caar res)
244 (cons #t (cons (append (reverse strs)
245 (cadr (cdar res)))
246 (cddr (cdar res)))))
247 (cdr res))
248 border-width))
249 (else
250 (finish (cons (cons (caar res) (cons #t (cddr (car res)))) (cdr res))
251 border-width))))
252 ((char? (car ls))
253 (lp (cons (string (car ls)) (cdr ls)) strs align infinite?
254 width border-width res))
255 ((string? (car ls))
256 (if (string-contains "\n" (car ls))
257 (error "column string literals can't contain newlines")
258 (lp (cdr ls) (cons (car ls) strs) align infinite?
259 width (+ border-width (string-length (car ls))) res)))
260 ((number? (car ls))
261 (lp (cdr ls) strs align infinite? (car ls) border-width res))
262 ((eq? (car ls) 'infinite)
263 (lp (cdr ls) strs align #t width border-width res))
264 ((symbol? (car ls))
265 (lp (cdr ls) strs (car ls) infinite? width border-width res))
266 ((procedure? (car ls))
267 (lp (cdr ls) '() 'left #f #t border-width
268 (cons (list width #f '() (car ls) (reverse strs) align infinite?)
269 res)))
270 (else
271 (error "invalid column" (car ls))))))
272
273 (define (max-line-width string-width str)
274 (let ((end (string-cursor-end str)))
275 (let lp ((i (string-cursor-start str)) (hi 0))
276 (let ((j (string-index str #\newline i)))
277 (if (string-cursor<? j end)
278 (lp (string-cursor-next str j)
279 (max hi (string-width (substring/cursors str i j))))
280 (max hi (string-width (substring/cursors str i end))))))))
281
282 (define (pad-finite proc width string-width k)
283 (call-with-output
284 proc
285 (lambda (str)
286 (let ((w (max-line-width (or string-width string-length) str)))
287 (k (displayed str)
288 (if (and (integer? width) (exact? width))
289 (max width w)
290 w))))))
291
292 (define (tabular . ls)
293 (fn (string-width)
294 (let lp ((ls ls) (infinite? #f) (width #t) (res '()))
295 (cond
296 ((null? ls)
297 (apply columnar (reverse res)))
298 ((number? (car ls))
299 (lp (cdr ls) infinite? (car ls) res))
300 ((eq? 'infinite (car ls))
301 (lp (cdr ls) #t width (cons (car ls) res)))
302 ((procedure? (car ls))
303 (if infinite?
304 (if width
305 (lp (cdr ls) #f #t (cons (car ls) (cons width res)))
306 (lp (cdr ls) #f #t (cons (car ls) res)))
307 (pad-finite (car ls) width string-width
308 (lambda (gen width)
309 (lp (cdr ls) #f #t (cons gen (cons width res)))))))
310 (else
311 (lp (cdr ls) infinite? width (cons (car ls) res)))))))
312
313 ;; break lines only, don't join short lines or justify
314 (define (wrapped/char . ls)
315 (fn ((orig-output output) width string-width substring/width)
316 (define (kons-in-line str)
317 (fn (col)
318 (let ((len ((or string-width string-length) str))
319 (space (- width col)))
320 (cond
321 ((equal? "" str)
322 nothing)
323 ((or (<= len space) (not (positive? space)))
324 (each (orig-output str) (orig-output "\n")))
325 (else
326 (each
327 ;; TODO: when splitting by string-width, substring needs
328 ;; to be provided
329 (orig-output (substring/width str 0 space))
330 (orig-output "\n")
331 (fn () (kons-in-line (substring/width str space len)))))))))
332 (with ((output
333 (lambda (str)
334 (let ((end (string-cursor-end str)))
335 (let lp ((i (string-cursor-start str)))
336 (let ((nli (string-index str #\newline i)))
337 (cond
338 ((string-cursor>=? i end)
339 nothing)
340 ((string-cursor>=? nli end)
341 (kons-in-line (substring/cursors str i end)))
342 (else
343 (each
344 (fn () (kons-in-line (substring/cursors str i nli)))
345 (fn () (lp (string-cursor-next str nli))))))))))))
346 (each-in-list ls))))
347
348 ;; `seq' is a list or vector of pre-tokenized words. `line' is called
349 ;; on each wrapped line and the accumulator, starting with `knil'.
350 ;; The optional `last-line' is used instead on the last line of the
351 ;; paragraph.
352 (define (wrap-fold-words seq knil max-width get-width line . o)
353 (let* ((last-line (if (pair? o) (car o) line))
354 (vec (if (list? seq) (list->vector seq) seq))
355 (len (vector-length vec))
356 (len-1 (- len 1))
357 (breaks (make-vector len #f))
358 (penalties (make-vector len #f))
359 (widths
360 (list->vector
361 (map get-width (if (list? seq) seq (vector->list vec))))))
362 (define (largest-fit i)
363 (let lp ((j (+ i 1)) (width (vector-ref widths i)))
364 (let ((width (+ width 1 (vector-ref widths j))))
365 (cond
366 ((>= width max-width) (- j 1))
367 ((>= j len-1) len-1)
368 (else (lp (+ j 1) width))))))
369 (define (min-penalty! i)
370 (cond
371 ((>= i len-1) 0)
372 ((vector-ref penalties i))
373 (else
374 (vector-set! penalties i (expt (+ max-width 1) 3))
375 (vector-set! breaks i i)
376 (let ((k (largest-fit i)))
377 (let lp ((j i) (width 0))
378 (if (<= j k)
379 (let* ((width (+ width (vector-ref widths j)))
380 (break-penalty
381 (+ (max 0 (expt (- max-width (+ width (- j i))) 3))
382 (min-penalty! (+ j 1)))))
383 (cond
384 ((< break-penalty (vector-ref penalties i))
385 (vector-set! breaks i j)
386 (vector-set! penalties i break-penalty)))
387 (lp (+ j 1) width)))))
388 (if (>= (vector-ref breaks i) len-1)
389 (vector-set! penalties i 0))
390 (vector-ref penalties i))))
391 (define (sub-list i j)
392 (let lp ((i i) (res '()))
393 (if (> i j)
394 (reverse res)
395 (lp (+ i 1) (cons (vector-ref vec i) res)))))
396 (cond
397 ((zero? len)
398 ;; degenerate case
399 (last-line '() knil))
400 (else
401 ;; compute optimum breaks
402 (vector-set! breaks len-1 len-1)
403 (vector-set! penalties len-1 0)
404 (min-penalty! 0)
405 ;; fold
406 (let lp ((i 0) (acc knil))
407 (let ((break (vector-ref breaks i)))
408 (if (>= break len-1)
409 (last-line (sub-list i len-1) acc)
410 (lp (+ break 1) (line (sub-list i break) acc)))))))))
411
412 (define (wrapped/list ls)
413 (fn (width string-width pad-char)
414 (joined
415 (lambda (ls) (joined displayed ls pad-char))
416 (reverse
417 (wrap-fold-words ls '() width (or string-width string-length) cons))
418 "\n")))
419
420 (define (wrapped . ls)
421 (call-with-output
422 (each-in-list ls)
423 (lambda (str)
424 (fn (word-separator?)
425 (wrapped/list
426 (string-split-words str (or word-separator? char-whitespace?)))))))
427
428 (define (justified . ls)
429 (fn (output width string-width)
430 (define (justify-line ls)
431 (if (null? ls)
432 nl
433 (let* ((sum (fold (lambda (s n)
434 (+ n ((or string-width string-length) s)))
435 0 ls))
436 (len (length ls))
437 (diff (max 0 (- width sum)))
438 (sep (make-string (if (= len 1)
439 0
440 (quotient diff (- len 1)))
441 #\space))
442 (rem (if (= len 1)
443 diff
444 (remainder diff (- len 1))))
445 (p (open-output-string)))
446 (write-string (car ls) p)
447 (let lp ((ls (cdr ls)) (i 1))
448 (when (pair? ls)
449 (write-string sep p)
450 (if (<= i rem) (write-char #\space p))
451 (write-string (car ls) p)
452 (lp (cdr ls) (+ i 1))))
453 (displayed (get-output-string p)))))
454 (define (justify-last ls)
455 (each (joined displayed ls " ") "\n"))
456 (call-with-output
457 (each-in-list ls)
458 (lambda (str)
459 (fn (word-separator?)
460 (joined/last
461 justify-line
462 justify-last
463 (reverse
464 (wrap-fold-words
465 (string-split-words str (or word-separator? char-whitespace?))
466 '() width (or string-width string-length)
467 cons))
468 "\n"))))))
469
470 (define (from-file path . ls)
471 (let-optionals* ls ((sep nl))
472 (fn ()
473 (let ((in (open-input-file path)))
474 (let lp ()
475 (let ((line (read-line in)))
476 (if (eof-object? line)
477 (begin (close-input-port in) nothing)
478 (each line sep
479 (fn () (lp))))))))))
480
481 (define (line-numbers . o)
482 (let ((start (if (pair? o) (car o) 1)))
483 (joined/range displayed start #f "\n")))