+ (let*
+ ((stencils (map (lambda (m) (interpret-markup layout props m)) args))
+ (space (chain-assoc-get 'word-space props))
+ (text-dir (chain-assoc-get 'text-direction props RIGHT))
+ )
+
+ (if (= text-dir LEFT)
+ (set! stencils (reverse stencils)))
+
+
+ (stack-stencil-line
+ space
+ (remove ly:stencil-empty? stencils))))
+
+
+(define (wordwrap-stencils stencils
+ justify base-space line-width text-dir)
+
+ "Perform simple wordwrap, return stencil of each line."
+
+ (define space (if justify
+
+ ;; justify only stretches lines.
+ (* 0.7 base-space)
+ base-space))
+
+ (define (take-list width space stencils
+ accumulator accumulated-width)
+ "Return (head-list . tail) pair, with head-list fitting into width"
+ (if (null? stencils)
+ (cons accumulator stencils)
+ (let*
+ ((first (car stencils))
+ (first-wid (cdr (ly:stencil-extent (car stencils) X)))
+ (newwid (+ space first-wid accumulated-width))
+ )
+
+ (if
+ (or (null? accumulator)
+ (< newwid width))
+
+ (take-list width space
+ (cdr stencils)
+ (cons first accumulator)
+ newwid)
+ (cons accumulator stencils))
+ )))
+
+ (let loop
+ ((lines '())
+ (todo stencils))
+
+ (let*
+ ((line-break (take-list line-width space todo
+ '() 0.0))
+ (line-stencils (car line-break))
+ (space-left (- line-width (apply + (map (lambda (x) (cdr (ly:stencil-extent x X)))
+ line-stencils))))
+
+ (line-word-space (cond
+ ((not justify) space)
+
+ ;; don't stretch last line of paragraph.
+ ;; hmmm . bug - will overstretch the last line in some case.
+ ((null? (cdr line-break))
+ base-space)
+ ((null? line-stencils) 0.0)
+ ((null? (cdr line-stencils)) 0.0)
+ (else (/ space-left (1- (length line-stencils))))))
+
+ (line (stack-stencil-line
+ line-word-space
+ (if (= text-dir RIGHT)
+ (reverse line-stencils)
+ line-stencils))))
+
+ (if (pair? (cdr line-break))
+ (loop (cons line lines)
+ (cdr line-break))
+
+ (begin
+ (if (= text-dir LEFT)
+ (set! line
+ (ly:stencil-translate-axis line
+ (- line-width (interval-end (ly:stencil-extent line X)))
+ X)))
+ (reverse (cons line lines))
+
+ )))
+
+ ))
+
+
+(define (wordwrap-markups layout props args justify)
+ (let*
+ ((baseline-skip (chain-assoc-get 'baseline-skip props))
+ (prop-line-width (chain-assoc-get 'line-width props #f))
+ (line-width (if prop-line-width prop-line-width
+ (ly:output-def-lookup layout 'line-width)))
+ (word-space (chain-assoc-get 'word-space props))
+ (text-dir (chain-assoc-get 'text-direction props RIGHT))
+ (lines (wordwrap-stencils
+ (remove ly:stencil-empty?
+ (map (lambda (m) (interpret-markup layout props m)) args))
+ justify word-space line-width
+ text-dir)
+ ))
+
+ (stack-lines DOWN 0.0 baseline-skip lines)))
+
+(define-markup-command (justify layout props args) (markup-list?)
+ "Like wordwrap, but with lines stretched to justify the margins.
+Use @code{\\override #'(line-width . X)} to set line-width, where X
+is the number of staff spaces."
+
+ (wordwrap-markups layout props args #t))
+
+(define-markup-command (wordwrap layout props args) (markup-list?)
+ "Simple wordwrap. Use @code{\\override #'(line-width . X)} to set
+line-width, where X is the number of staff spaces."
+
+ (wordwrap-markups layout props args #f))
+
+(define (wordwrap-string layout props justify arg)
+ (let*
+ ((baseline-skip (chain-assoc-get 'baseline-skip props))
+ (line-width (chain-assoc-get 'line-width props))
+ (word-space (chain-assoc-get 'word-space props))
+
+ (para-strings (regexp-split
+ (string-regexp-substitute "\r" "\n"
+ (string-regexp-substitute "\r\n" "\n" arg))
+ "\n[ \t\n]*\n[ \t\n]*"))
+
+ (text-dir (chain-assoc-get 'text-direction props RIGHT))
+ (list-para-words (map (lambda (str)
+ (regexp-split str "[ \t\n]+"))
+ para-strings))
+ (para-lines (map (lambda (words)
+ (let*
+ ((stencils
+ (remove
+ ly:stencil-empty? (map
+ (lambda (x)
+ (interpret-markup layout props x))
+ words)))
+ (lines (wordwrap-stencils stencils
+ justify word-space
+ line-width text-dir
+ )))
+
+ lines))
+
+ list-para-words)))
+
+ (stack-lines DOWN 0.0 baseline-skip (apply append para-lines))))
+
+
+(define-markup-command (wordwrap-string layout props arg) (string?)
+ "Wordwrap a string. Paragraphs may be separated with double newlines"
+ (wordwrap-string layout props #f arg))
+
+(define-markup-command (justify-string layout props arg) (string?)
+ "Justify a string. Paragraphs may be separated with double newlines"
+ (wordwrap-string layout props #t arg))
+
+
+(define-markup-command (wordwrap-field layout props symbol) (symbol?)
+ (let* ((m (chain-assoc-get symbol props)))
+ (if (string? m)
+ (interpret-markup layout props
+ (list wordwrap-string-markup m))
+ (ly:make-stencil '() '(1 . -1) '(1 . -1)))))
+
+(define-markup-command (justify-field layout props symbol) (symbol?)
+- (let* ((m (chain-assoc-get symbol props)))
+ (if (string? m)
+ (interpret-markup layout props
+ (list justify-string-markup m))
+ (ly:make-stencil '() '(1 . -1) '(1 . -1)))))
+
+
+
+(define-markup-command (combine layout props m1 m2) (markup? markup?)
+ "Print two markups on top of each other."
+ (let* ((s1 (interpret-markup layout props m1))
+ (s2 (interpret-markup layout props m2)))
+ (ly:stencil-add s1 s2)))
+
+;;
+;; TODO: should extract baseline-skip from each argument somehow..
+;;
+(define-markup-command (column layout props args) (markup-list?)
+ "Stack the markups in @var{args} vertically. The property
+@code{baseline-skip} determines the space between each markup in @var{args}."
+
+ (let*
+ ((arg-stencils (map (lambda (m) (interpret-markup layout props m)) args))
+ (skip (chain-assoc-get 'baseline-skip props)))
+
+
+ (stack-lines
+ -1 0.0 skip
+ (remove ly:stencil-empty? arg-stencils))))
+
+
+(define-markup-command (dir-column layout props args) (markup-list?)
+ "Make a column of args, going up or down, depending on the setting
+of the @code{#'direction} layout property."
+ (let* ((dir (chain-assoc-get 'direction props)))
+ (stack-lines
+ (if (number? dir) dir -1)
+ 0.0
+ (chain-assoc-get 'baseline-skip props)
+ (map (lambda (x) (interpret-markup layout props x)) args))))