2 ;;;; lily-library.scm -- utilities
4 ;;;; source file of the GNU LilyPond music typesetter
6 ;;;; (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
9 ; for take, drop, take-while, list-index, and find-tail:
10 (use-modules (srfi srfi-1))
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17 (define-safe-public START -1)
18 (define-safe-public STOP 1)
19 (define-public LEFT -1)
20 (define-public RIGHT 1)
22 (define-public DOWN -1)
23 (define-public CENTER 0)
25 (define-safe-public DOUBLE-FLAT-QTS -4)
26 (define-safe-public THREE-Q-FLAT-QTS -3)
27 (define-safe-public FLAT-QTS -2)
28 (define-safe-public SEMI-FLAT-QTS -1)
29 (define-safe-public NATURAL-QTS 0)
30 (define-safe-public SEMI-SHARP-QTS 1)
31 (define-safe-public SHARP-QTS 2)
32 (define-safe-public THREE-Q-SHARP-QTS 3)
33 (define-safe-public DOUBLE-SHARP-QTS 4)
34 (define-safe-public SEMI-TONE-QTS 2)
36 (define-safe-public DOUBLE-FLAT -1)
37 (define-safe-public THREE-Q-FLAT -3/4)
38 (define-safe-public FLAT -1/2)
39 (define-safe-public SEMI-FLAT -1/4)
40 (define-safe-public NATURAL 0)
41 (define-safe-public SEMI-SHARP 1/4)
42 (define-safe-public SHARP 1/2)
43 (define-safe-public THREE-Q-SHARP 3/4)
44 (define-safe-public DOUBLE-SHARP 1)
45 (define-safe-public SEMI-TONE 1/2)
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 (define-public ZERO-MOMENT (ly:make-moment 0 1))
52 (define-public (moment-min a b)
53 (if (ly:moment<? a b) a b))
55 (define-public (moment<=? a b)
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 (define-public (average x . lst)
62 (/ (+ x (apply + lst)) (1+ (length lst))))
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65 ;; parser <-> output hooks.
67 (define-public (collect-bookpart-for-book parser book-part)
68 "Toplevel book-part handler"
69 (define (add-bookpart book-part)
71 parser 'toplevel-bookparts
72 (cons book-part (ly:parser-lookup parser 'toplevel-bookparts))))
73 ;; If toplevel scores have been found before this \bookpart,
74 ;; add them first to a dedicated bookpart
75 (if (pair? (ly:parser-lookup parser 'toplevel-scores))
77 (add-bookpart (ly:make-book-part
78 (ly:parser-lookup parser 'toplevel-scores)))
79 (ly:parser-define! parser 'toplevel-scores (list))))
80 (add-bookpart book-part))
82 (define-public (collect-scores-for-book parser score)
84 parser 'toplevel-scores
85 (cons score (ly:parser-lookup parser 'toplevel-scores))))
87 (define-public (collect-music-aux score-handler parser music)
88 (define (music-property symbol)
89 (let ((value (ly:music-property music symbol)))
90 (if (not (null? value))
93 (cond ((music-property 'page-marker)
94 ;; a page marker: set page break/turn permissions or label
96 (let ((label (music-property 'page-label)))
98 (score-handler (ly:make-page-label-marker label))))
99 (for-each (lambda (symbol)
100 (let ((permission (music-property symbol)))
101 (if (symbol? permission)
103 (ly:make-page-permission-marker symbol
104 (if (eqv? 'forbid permission)
107 (list 'line-break-permission 'page-break-permission
108 'page-turn-permission))))
109 ((not (music-property 'void))
110 ;; a regular music expression: make a score with this music
111 ;; void music is discarded
112 (score-handler (scorify-music music parser)))))
114 (define-public (collect-music-for-book parser music)
115 "Top-level music handler"
116 (collect-music-aux (lambda (score)
117 (collect-scores-for-book parser score))
121 (define-public (collect-book-music-for-book parser book music)
123 (collect-music-aux (lambda (score)
124 (ly:book-add-score! book score))
128 (define-public (scorify-music music parser)
131 (for-each (lambda (func)
132 (set! music (func music parser)))
133 toplevel-music-functions)
135 (ly:make-score music))
137 (define (print-book-with parser book process-procedure)
139 ((paper (ly:parser-lookup parser '$defaultpaper))
140 (layout (ly:parser-lookup parser '$defaultlayout))
141 (count (ly:parser-lookup parser 'output-count))
142 (base (ly:parser-output-name parser))
143 (output-suffix (ly:parser-lookup parser 'output-suffix)) )
145 (if (string? output-suffix)
146 (set! base (format "~a-~a" base (string-regexp-substitute
147 "[^a-zA-Z0-9-]" "_" output-suffix))))
149 ;; must be careful: output-count is under user control.
150 (if (not (integer? count))
154 (set! base (format #f "~a-~a" base count)))
155 (ly:parser-define! parser 'output-count (1+ count))
156 (process-procedure book paper layout base)
159 (define-public (print-book-with-defaults parser book)
160 (print-book-with parser book ly:book-process))
162 (define-public (print-book-with-defaults-as-systems parser book)
163 (print-book-with parser book ly:book-process-to-systems))
168 (define-public assoc-get ly:assoc-get)
170 (define-public (uniqued-alist alist acc)
171 (if (null? alist) acc
172 (if (assoc (caar alist) acc)
173 (uniqued-alist (cdr alist) acc)
174 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
176 (define-public (alist<? x y)
177 (string<? (symbol->string (car x))
178 (symbol->string (car y))))
180 (define-public (chain-assoc-get x alist-list . default)
181 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
184 (define (helper x alist-list default)
185 (if (null? alist-list)
187 (let* ((handle (assoc x (car alist-list))))
190 (helper x (cdr alist-list) default)))))
193 (if (pair? default) (car default) #f)))
195 (define (map-alist-vals func list)
196 "map FUNC over the vals of LIST, leaving the keys."
199 (cons (cons (caar list) (func (cdar list)))
200 (map-alist-vals func (cdr list)))))
202 (define (map-alist-keys func list)
203 "map FUNC over the keys of an alist LIST, leaving the vals. "
206 (cons (cons (func (caar list)) (cdar list))
207 (map-alist-keys func (cdr list)))))
209 (define-public (first-member members lst)
210 "Return first successful MEMBER of member from MEMBERS in LST."
213 (let ((m (member (car members) lst)))
214 (if m m (first-member (cdr members) lst)))))
216 (define-public (first-assoc keys lst)
217 "Return first successful ASSOC of key from KEYS in LST."
220 (let ((k (assoc (car keys) lst)))
221 (if k k (first-assoc (cdr keys) lst)))))
223 (define-public (flatten-alist alist)
228 (flatten-alist (cdr alist))))))
233 (define-public (vector-for-each proc vec)
236 ((>= i (vector-length vec)) vec)
237 (vector-set! vec i (proc (vector-ref vec i)))))
242 (define-public (hash-table->alist t)
243 (hash-fold (lambda (k v acc) (acons k v acc))
246 ;; todo: code dup with C++.
247 (define-safe-public (alist->hash-table lst)
248 "Convert alist to table"
249 (let ((m (make-hash-table (length lst))))
250 (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
256 (define (functional-or . rest)
259 (apply functional-and (cdr rest)))
262 (define (functional-and . rest)
265 (apply functional-and (cdr rest)))
268 (define (split-list lst n)
269 "Split LST in N equal sized parts"
271 (define (helper todo acc-vector k)
278 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
279 (helper (cdr todo) acc-vector (1- k)))))
281 (helper lst (make-vector n '()) (1- n)))
283 (define (list-element-index lst x)
284 (define (helper todo k)
287 ((equal? (car todo) x) k)
289 (helper (cdr todo) (1+ k)))))
293 (define-public (count-list lst)
294 "Given lst (E1 E2 .. ) return ((E1 . 1) (E2 . 2) ... ) "
296 (define (helper l acc count)
298 (helper (cdr l) (cons (cons (car l) count) acc) (1+ count))
302 (reverse (helper lst '() 1)))
304 (define-public (list-join lst intermediate)
305 "put INTERMEDIATE between all elts of LST."
310 (cons elem (cons intermediate prev))
314 (define-public (filtered-map proc lst)
320 (define (flatten-list lst)
324 (if (pair? (car lst))
325 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
326 (cons (car lst) (flatten-list (cdr lst))))))
328 (define (list-minus a b)
329 "Return list of elements in A that are not in B."
330 (lset-difference eq? a b))
332 (define-public (uniq-list lst)
333 "Uniq LST, assuming that it is sorted. Uses equal? for comparisons."
336 (fold (lambda (x acc)
339 (if (equal? x (car acc))
344 (define (split-at-predicate pred lst)
345 "Split LST into two lists at the first element that returns #f for
346 (PRED previous_element element). Return the two parts as a pair.
347 Example: (split-at-predicate < '(1 2 3 2 1)) ==> ((1 2 3) . (2 1))"
350 (let ((i (list-index pred (cdr lst) lst)))
352 (cons (take lst (1+ i)) (drop lst (1+ i)))
355 (define-public (split-list-by-separator lst pred)
356 "Split LST at each element that satisfies PRED, and return the parts
357 (with the separators removed) as a list of lists. Example:
358 (split-list-by-separator '(a 0 b c 1 d) number?) ==> ((a) (b c) (d))"
359 (let loop ((result '()) (lst lst))
360 (if (and lst (not (null? lst)))
363 (list (take-while (lambda (x) (not (pred x))) lst)))
364 (let ((tail (find-tail pred lst)))
365 (if tail (cdr tail) #f)))
368 (define-public (offset-add a b)
369 (cons (+ (car a) (car b))
370 (+ (cdr a) (cdr b))))
372 (define-public (offset-flip-y o)
373 (cons (car o) (- (cdr o))))
375 (define-public (offset-scale o scale)
376 (cons (* (car o) scale)
379 (define-public (ly:list->offsets accum coords)
382 (cons (cons (car coords) (cadr coords))
383 (ly:list->offsets accum (cddr coords)))))
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 (if (not (defined? 'nan?)) ;; guile 1.6 compat
389 (define-public (nan? x) (not (or (< 0.0 x)
393 (if (not (defined? 'inf?))
394 (define-public (inf? x) (= (/ 1.0 x) 0.0)))
396 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
399 (define-public empty-interval '(+inf.0 . -inf.0))
401 (define-public (interval-length x)
402 "Length of the number-pair X, when an interval"
403 (max 0 (- (cdr x) (car x))))
405 (define-public interval-start car)
406 (define-public (ordered-cons a b)
410 (define-public interval-end cdr)
412 (define-public (interval-bound interval dir)
413 ((if (= dir RIGHT) cdr car) interval))
415 (define-public (interval-index interval dir)
416 "Interpolate INTERVAL between between left (DIR=-1) and right (DIR=+1)"
418 (* (+ (interval-start interval) (interval-end interval)
419 (* dir (- (interval-end interval) (interval-start interval))))
422 (define-public (interval-center x)
423 "Center the number-pair X, when an interval"
424 (if (interval-empty? x)
426 (/ (+ (car x) (cdr x)) 2)))
428 (define-public interval-start car)
429 (define-public interval-end cdr)
430 (define-public (interval-translate iv amount)
431 (cons (+ amount (car iv))
432 (+ amount (cdr iv))))
434 (define (other-axis a)
435 (remainder (+ a 1) 2))
437 (define-public (interval-widen iv amount)
438 (cons (- (car iv) amount)
439 (+ (cdr iv) amount)))
442 (define-public (interval-empty? iv)
443 (> (car iv) (cdr iv)))
445 (define-public (interval-union i1 i2)
446 (cons (min (car i1) (car i2))
447 (max (cdr i1) (cdr i2))))
449 (define-public (interval-sane? i)
450 (not (or (nan? (car i))
454 (> (car i) (cdr i)))))
456 (define-public (add-point interval p)
457 (cons (min (interval-start interval) p)
458 (max (interval-end interval) p)))
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
464 (define-public (string-endswith s suffix)
465 (equal? suffix (substring s
466 (max 0 (- (string-length s) (string-length suffix)))
469 (define-public (string-startswith s prefix)
470 (equal? prefix (substring s 0 (min (string-length s) (string-length prefix)))))
472 (define-public (string-encode-integer i)
475 ((< i 0) (string-append "n" (string-encode-integer (- i))))
477 (make-string 1 (integer->char (+ 65 (modulo i 26))))
478 (string-encode-integer (quotient i 26))))))
480 (define (number->octal-string x)
481 (let* ((n (inexact->exact x))
482 (n64 (quotient n 64))
483 (n8 (quotient (- n (* n64 64)) 8)))
487 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
489 (define-public (ly:inexact->string x radix)
490 (let ((n (inexact->exact x)))
491 (number->string n radix)))
493 (define-public (ly:number-pair->string c)
494 (string-append (ly:number->string (car c)) " "
495 (ly:number->string (cdr c))))
497 (define-public (dir-basename file . rest)
498 "Strip suffixes in REST, but leave directory component for FILE."
499 (define (inverse-basename x y) (basename y x))
500 (simple-format #f "~a/~a" (dirname file)
501 (fold inverse-basename file rest)))
503 (define-public (write-me message x)
504 "Return X. Display MESSAGE and write X. Handy for debugging,
505 possibly turned off."
506 (display message) (write x) (newline) x)
509 (define-public (stderr string . rest)
510 (apply format (cons (current-error-port) (cons string rest)))
511 (force-output (current-error-port)))
513 (define-public (debugf string . rest)
515 (apply stderr (cons string rest))))
517 (define (index-cell cell dir)
522 (define (cons-map f x)
523 "map F to contents of X"
524 (cons (f (car x)) (f (cdr x))))
526 (define-public (list-insert-separator lst between)
527 "Create new list, inserting BETWEEN between elements of LIST"
531 (cons x (cons between y))))
532 (fold-right conc #f lst))
534 (define-public (string-regexp-substitute a b str)
535 (regexp-substitute/global #f a str 'pre b 'post))
537 (define (regexp-split str regex)
539 (define end-of-prev-match 0)
540 (define (notice match)
542 (set! matches (cons (substring (match:string match)
546 (set! end-of-prev-match (match:end match)))
548 (regexp-substitute/global #f regex str notice 'post)
550 (if (< end-of-prev-match (string-length str))
553 (cons (substring str end-of-prev-match (string-length str)) matches)))
565 (define-public (binary-search start end getter target-val)
566 (_i "Find the index between @var{start} and @var{end} (an integer)
567 which will produce the closest match to @var{target-val} when
568 applied to function @var{getter}.")
571 (let* ((compare (quotient (+ start end) 2))
572 (get-val (getter compare)))
574 ((< target-val get-val)
575 (set! end (1- compare)))
576 ((< get-val target-val)
577 (set! start (1+ compare))))
578 (binary-search start end getter target-val))))
580 (define-public (car< a b)
583 (define-public (symbol<? lst r)
584 (string<? (symbol->string lst) (symbol->string r)))
586 (define-public (symbol-key<? lst r)
587 (string<? (symbol->string (car lst)) (symbol->string (car r))))
590 ;; don't confuse users with #<procedure .. > syntax.
592 (define-public (scm->string val)
593 (if (and (procedure? val)
594 (symbol? (procedure-name val)))
595 (symbol->string (procedure-name val))
597 (if (self-evaluating? val)
602 (call-with-output-string (lambda (port) (display val port)))
607 (define-public (!= lst r)
610 (define-public lily-unit->bigpoint-factor
612 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
613 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
614 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
616 (define-public lily-unit->mm-factor
617 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
619 ;;; FONT may be font smob, or pango font string...
620 (define-public (font-name-style font)
621 ;; FIXME: ughr, barf: feta-alphabet is actually emmentaler
622 (if (and (string? font)
623 (string-prefix? "feta-alphabet" font))
624 (string-append "emmentaler"
627 (string-length "feta-alphabet")
628 (string-length font)))
629 (let* ((font-name (ly:font-name font))
630 (full-name (if font-name font-name (ly:font-file-name font))))
631 (if (string-prefix? "Aybabtu" full-name)
633 (string-downcase full-name)))))
635 (define-public (modified-font-metric-font-scaling font)
636 (let* ((designsize (ly:font-design-size font))
637 (magnification (* (ly:font-magnification font)))
638 (scaling (* magnification designsize)))
639 (debugf "scaling:~S\n" scaling)
640 (debugf "magnification:~S\n" magnification)
641 (debugf "design:~S\n" designsize)
644 (define-public (version-not-seen-message input-file-name)
650 (_ "no \\version statement found, please add~afor future compatibility")
651 (format #f "\n\n\\version ~s\n\n" (lilypond-version)))))
653 (define-public (old-relative-not-used-message input-file-name)
658 (_ "old relative compatibility not used")))