Use the new wrap-line' in scribble/eval'.

This might have output that is a little better in cases where the
wrapped string is longer than `maxlen', for example, with an input of:

  "    x xxxxxxxxxxxxxxxx..."

and wrapping at 10 characters, the output was

  "    x\nxxxxxxxxxx\nxxxxxx..."

and now it's

  "    x xxxx\nxxxxxxxxxx\nxx..."

original commit: efda1706d87045cbe2882c9c4b77b34a72e3594b
This commit is contained in:
Eli Barzilay 2012-05-06 06:15:04 -04:00
parent 63427f262e
commit c3d02f1416
2 changed files with 10 additions and 16 deletions

View File

@ -6,7 +6,8 @@
racket/pretty ;; attached into new namespace via anchor
racket/sandbox racket/promise racket/port
racket/gui/dynamic
(for-syntax racket/base))
(for-syntax racket/base)
scribble/text/wrap)
(provide interaction
interaction0
@ -102,19 +103,12 @@
(loop #f (cons v (or (add-string string-accum line-accum) null))
flow-accum)]))))
;; This is probably good to make into some library function at some
;; point (but in that case will need to improve, eg, wrapped lines
;; should start at the same indentation level, etc)
(define (string->wrapped-lines str)
(define (wrap-line str)
(if ((string-length str) . <= . maxlen)
(if (equal? str "") '() (list str))
(let* ([m (cond [(regexp-match-positions #px"^.*\\S(\\s+).*" str 0 maxlen)
=> cadr]
[else (cons maxlen maxlen)])]
[r (wrap-line (substring str (cdr m)))])
(if (= 0 (car m)) r (cons (substring str 0 (car m)) r)))))
(append-map wrap-line (regexp-split #px"\\s*\n" str)))
(wrap-line str maxlen
(λ (word fits)
(if ((string-length word) . > . maxlen)
(values (substring word 0 fits) (substring word fits) #f)
(values #f word #f)))))
(define (interleave inset? title expr-paras val-list+outputs)
(let ([lines

View File

@ -21,7 +21,7 @@
;; element is used for the rest. For example, (cons 72 70) indicates a width
;; of 72 characters for the first line, and 70 for the rest.
;;
;; `split-word' controls what happens when a word is split: it is ivoked with
;; `split-word' controls what happens when a word is split: it is invoked with
;; the word that is to be split, and an integer indicating how many characters
;; could fit on the first line. In most cases the string will be just a word,
;; but if it was the first/last word with only spaces before/after it, then the
@ -36,13 +36,13 @@
;; words; spaces between a word that moves to the next line and the preceding
;; word are usually dropped, but they can be preserved too if the first result
;; is "" rather than #f (and same for the space between a word that stays on
;; the same line when the following word moves doen). If the first result is
;; the same line when the following word moves down). If the first result is
;; `#f' and it was the only word on the line, then the line that would
;; otherwise be empty is dropped. Note that depending on what `split-word'
;; chooses to do, the result may still have lines that are longer than `width'
;; characters. The default `split-word' returns (values #f the-word #t).
;;
;; Sidenote: never returns an empty list.
;; Side note: never returns an empty list.
;;
;; Caveats: considers only spaces as whitespace; not too efficient since it
;; constructs intermediate strings for its processing; the need for the third