2 ;;;; lily-library.scm -- utilities
4 ;;;; source file of the GNU LilyPond music typesetter
6 ;;;; (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 (define-safe-public START -1)
15 (define-safe-public STOP 1)
16 (define-public LEFT -1)
17 (define-public RIGHT 1)
19 (define-public DOWN -1)
20 (define-public CENTER 0)
22 (define-safe-public DOUBLE-FLAT-QTS -4)
23 (define-safe-public THREE-Q-FLAT-QTS -3)
24 (define-safe-public FLAT-QTS -2)
25 (define-safe-public SEMI-FLAT-QTS -1)
26 (define-safe-public NATURAL-QTS 0)
27 (define-safe-public SEMI-SHARP-QTS 1)
28 (define-safe-public SHARP-QTS 2)
29 (define-safe-public THREE-Q-SHARP-QTS 3)
30 (define-safe-public DOUBLE-SHARP-QTS 4)
31 (define-safe-public SEMI-TONE-QTS 2)
33 (define-safe-public DOUBLE-FLAT -1)
34 (define-safe-public THREE-Q-FLAT -3/4)
35 (define-safe-public FLAT -1/2)
36 (define-safe-public SEMI-FLAT -1/4)
37 (define-safe-public NATURAL 0)
38 (define-safe-public SEMI-SHARP 1/4)
39 (define-safe-public SHARP 1/2)
40 (define-safe-public THREE-Q-SHARP 3/4)
41 (define-safe-public DOUBLE-SHARP 1)
42 (define-safe-public SEMI-TONE 1/2)
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 (define-public ZERO-MOMENT (ly:make-moment 0 1))
49 (define-public (moment-min a b)
50 (if (ly:moment<? a b) a b))
52 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
54 (define-public (average x . lst)
55 (/ (+ x (apply + lst)) (1+ (length lst))))
57 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
58 ;; parser <-> output hooks.
61 (define-public (collect-scores-for-book parser score)
63 parser 'toplevel-scores
64 (cons score (ly:parser-lookup parser 'toplevel-scores))))
66 (define-public (collect-music-for-book parser music)
67 ;; discard music if its 'void property is true.
68 (let ((void-music (ly:music-property music 'void)))
69 (if (or (null? void-music) (not void-music))
70 (collect-scores-for-book parser (scorify-music music parser)))))
72 (define-public (scorify-music music parser)
75 (for-each (lambda (func)
76 (set! music (func music parser)))
77 toplevel-music-functions)
79 (ly:make-score music))
81 (define (print-book-with parser book process-procedure)
83 ((paper (ly:parser-lookup parser '$defaultpaper))
84 (layout (ly:parser-lookup parser '$defaultlayout))
86 (count (ly:parser-lookup parser 'output-count))
87 (base (ly:parser-output-name parser)))
89 ;; must be careful: output-count is under user control.
90 (if (not (integer? count))
94 (set! base (format #f "~a-~a" base count)))
96 (ly:parser-define! parser 'output-count (1+ count))
97 (process-procedure book paper layout base)
100 (define-public (print-book-with-defaults parser book)
101 (print-book-with parser book ly:book-process))
103 (define-public (print-book-with-defaults-as-systems parser book)
104 (print-book-with parser book ly:book-process-to-systems))
109 (define-public assoc-get ly:assoc-get)
111 (define-public (uniqued-alist alist acc)
112 (if (null? alist) acc
113 (if (assoc (caar alist) acc)
114 (uniqued-alist (cdr alist) acc)
115 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
117 (define-public (alist<? x y)
118 (string<? (symbol->string (car x))
119 (symbol->string (car y))))
121 (define-public (chain-assoc x alist-list)
122 (if (null? alist-list)
124 (let* ((handle (assoc x (car alist-list))))
127 (chain-assoc x (cdr alist-list))))))
129 (define-public (chain-assoc-get x alist-list . default)
130 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
133 (define (helper x alist-list default)
134 (if (null? alist-list)
136 (let* ((handle (assoc x (car alist-list))))
139 (helper x (cdr alist-list) default)))))
142 (if (pair? default) (car default) #f)))
144 (define (map-alist-vals func list)
145 "map FUNC over the vals of LIST, leaving the keys."
148 (cons (cons (caar list) (func (cdar list)))
149 (map-alist-vals func (cdr list)))))
151 (define (map-alist-keys func list)
152 "map FUNC over the keys of an alist LIST, leaving the vals. "
155 (cons (cons (func (caar list)) (cdar list))
156 (map-alist-keys func (cdr list)))))
158 (define-public (first-member members lst)
159 "Return first successful MEMBER of member from MEMBERS in LST."
162 (let ((m (member (car members) lst)))
163 (if m m (first-member (cdr members) lst)))))
165 (define-public (first-assoc keys lst)
166 "Return first successful ASSOC of key from KEYS in LST."
169 (let ((k (assoc (car keys) lst)))
170 (if k k (first-assoc (cdr keys) lst)))))
172 (define-public (flatten-alist alist)
177 (flatten-alist (cdr alist))))))
182 (define-public (vector-for-each proc vec)
185 ((>= i (vector-length vec)) vec)
186 (vector-set! vec i (proc (vector-ref vec i)))))
191 (define-public (hash-table->alist t)
192 (hash-fold (lambda (k v acc) (acons k v acc))
195 ;; todo: code dup with C++.
196 (define-safe-public (alist->hash-table lst)
197 "Convert alist to table"
198 (let ((m (make-hash-table (length lst))))
199 (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
205 (define (functional-or . rest)
208 (apply functional-and (cdr rest)))
211 (define (functional-and . rest)
214 (apply functional-and (cdr rest)))
217 (define (split-list lst n)
218 "Split LST in N equal sized parts"
220 (define (helper todo acc-vector k)
227 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
228 (helper (cdr todo) acc-vector (1- k)))))
230 (helper lst (make-vector n '()) (1- n)))
232 (define (list-element-index lst x)
233 (define (helper todo k)
236 ((equal? (car todo) x) k)
238 (helper (cdr todo) (1+ k)))))
242 (define-public (count-list lst)
243 "Given lst (E1 E2 .. ) return ((E1 . 1) (E2 . 2) ... ) "
245 (define (helper l acc count)
247 (helper (cdr l) (cons (cons (car l) count) acc) (1+ count))
251 (reverse (helper lst '() 1)))
253 (define-public (list-join lst intermediate)
254 "put INTERMEDIATE between all elts of LST."
259 (cons elem (cons intermediate prev))
263 (define-public (filtered-map proc lst)
269 (define (flatten-list lst)
273 (if (pair? (car lst))
274 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
275 (cons (car lst) (flatten-list (cdr lst))))))
277 (define (list-minus a b)
278 "Return list of elements in A that are not in B."
279 (lset-difference eq? a b))
281 (define-public (uniq-list lst)
282 "Uniq LST, assuming that it is sorted"
285 (fold (lambda (x acc)
288 (if (eq? x (car acc))
293 (define (split-at-predicate predicate lst)
294 "Split LST = (a_1 a_2 ... a_k b_1 ... b_k)
295 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
296 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
297 L1 is copied, L2 not.
299 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
303 (define (inner-split predicate lst acc)
307 (set-car! acc (cons (car lst) (car acc)))
309 ((predicate (car lst) (cadr lst))
310 (set-car! acc (cons (car lst) (car acc)))
311 (inner-split predicate (cdr lst) acc))
313 (set-car! acc (cons (car lst) (car acc)))
314 (set-cdr! acc (cdr lst))
317 (let* ((c (cons '() '())))
318 (inner-split predicate lst c)
319 (set-car! c (reverse! (car c)))
322 (define-public (split-list-by-separator lst sep?)
323 "(display (split-list-by-separator '(a b c / d e f / g) (lambda (x) (equal? x '/))))
325 ((a b c) (d e f) (g))
328 (define (split-one sep? lst acc)
329 "Split off the first parts before separator and return both parts."
334 (split-one sep? (cdr lst) (cons (car lst) acc)))))
338 (let* ((c (split-one sep? lst '())))
339 (cons (reverse! (car c) '()) (split-list-by-separator (cdr c) sep?)))))
341 (define-public (offset-add a b)
342 (cons (+ (car a) (car b))
343 (+ (cdr a) (cdr b))))
345 (define-public (offset-flip-y o)
346 (cons (car o) (- (cdr o))))
348 (define-public (offset-scale o scale)
349 (cons (* (car o) scale)
352 (define-public (ly:list->offsets accum coords)
355 (cons (cons (car coords) (cadr coords))
356 (ly:list->offsets accum (cddr coords)))))
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361 (if (not (defined? 'nan?)) ;; guile 1.6 compat
362 (define-public (nan? x) (not (or (< 0.0 x)
366 (if (not (defined? 'inf?))
367 (define-public (inf? x) (= (/ 1.0 x) 0.0)))
369 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 (define-public (interval-length x)
373 "Length of the number-pair X, when an interval"
374 (max 0 (- (cdr x) (car x))))
376 (define-public interval-start car)
377 (define-public (ordered-cons a b)
381 (define-public interval-end cdr)
383 (define-public (interval-index interval dir)
384 "Interpolate INTERVAL between between left (DIR=-1) and right (DIR=+1)"
386 (* (+ (interval-start interval) (interval-end interval)
387 (* dir (- (interval-end interval) (interval-start interval))))
390 (define-public (interval-center x)
391 "Center the number-pair X, when an interval"
392 (if (interval-empty? x)
394 (/ (+ (car x) (cdr x)) 2)))
396 (define-public interval-start car)
397 (define-public interval-end cdr)
398 (define-public (interval-translate iv amount)
399 (cons (+ amount (car iv))
400 (+ amount (cdr iv))))
402 (define (other-axis a)
403 (remainder (+ a 1) 2))
405 (define-public (interval-widen iv amount)
406 (cons (- (car iv) amount)
407 (+ (cdr iv) amount)))
410 (define-public (interval-empty? iv)
411 (> (car iv) (cdr iv)))
413 (define-public (interval-union i1 i2)
414 (cons (min (car i1) (car i2))
415 (max (cdr i1) (cdr i2))))
417 (define-public (interval-sane? i)
418 (not (or (nan? (car i))
422 (> (car i) (cdr i)))))
425 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 (define-public (string-encode-integer i)
432 ((< i 0) (string-append "n" (string-encode-integer (- i))))
434 (make-string 1 (integer->char (+ 65 (modulo i 26))))
435 (string-encode-integer (quotient i 26))))))
437 (define-public (ly:numbers->string lst)
438 (string-join (map ly:number->string lst) " "))
440 (define (number->octal-string x)
441 (let* ((n (inexact->exact x))
442 (n64 (quotient n 64))
443 (n8 (quotient (- n (* n64 64)) 8)))
447 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
449 (define-public (ly:inexact->string x radix)
450 (let ((n (inexact->exact x)))
451 (number->string n radix)))
453 (define-public (ly:number-pair->string c)
454 (string-append (ly:number->string (car c)) " "
455 (ly:number->string (cdr c))))
458 (define-public (write-me message x)
459 "Return X. Display MESSAGE and write X. Handy for debugging,
460 possibly turned off."
461 (display message) (write x) (newline) x)
464 (define-public (stderr string . rest)
465 (apply format (cons (current-error-port) (cons string rest)))
466 (force-output (current-error-port)))
468 (define-public (debugf string . rest)
470 (apply stderr (cons string rest))))
472 (define (index-cell cell dir)
477 (define (cons-map f x)
478 "map F to contents of X"
479 (cons (f (car x)) (f (cdr x))))
481 (define-public (list-insert-separator lst between)
482 "Create new list, inserting BETWEEN between elements of LIST"
486 (cons x (cons between y))))
487 (fold-right conc #f lst))
489 (define-public (string-regexp-substitute a b str)
490 (regexp-substitute/global #f a str 'pre b 'post))
492 (define (regexp-split str regex)
494 (define end-of-prev-match 0)
495 (define (notice match)
497 (set! matches (cons (substring (match:string match)
501 (set! end-of-prev-match (match:end match)))
503 (regexp-substitute/global #f regex str notice 'post)
505 (if (< end-of-prev-match (string-length str))
508 (cons (substring str end-of-prev-match (string-length str)) matches)))
519 (define-public (car< a b) (< (car a) (car b)))
521 (define-public (symbol<? lst r)
522 (string<? (symbol->string lst) (symbol->string r)))
524 (define-public (symbol-key<? lst r)
525 (string<? (symbol->string (car lst)) (symbol->string (car r))))
528 ;; don't confuse users with #<procedure .. > syntax.
530 (define-public (scm->string val)
531 (if (and (procedure? val) (symbol? (procedure-name val)))
532 (symbol->string (procedure-name val))
534 (if (self-evaluating? val) "" "'")
535 (call-with-output-string (lambda (port) (display val port))))))
537 (define-public (!= lst r)
540 (define-public lily-unit->bigpoint-factor
542 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
543 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
544 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
546 (define-public lily-unit->mm-factor
547 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
549 ;;; FONT may be font smob, or pango font string...
550 (define-public (font-name-style font)
551 ;; FIXME: ughr, (ly:font-name) sometimes also has Style appended.
552 (let* ((font-name (ly:font-name font))
553 (full-name (if font-name font-name (ly:font-file-name font)))
554 (name-style (string-split full-name #\-)))
555 ;; FIXME: ughr, barf: feta-alphabet is actually emmentaler
556 (if (string-prefix? "feta-alphabet" full-name)
558 (substring full-name (string-length "feta-alphabet")))
559 (if (not (null? (cdr name-style)))
561 (append name-style '("Regular"))))))
563 (define-public (modified-font-metric-font-scaling font)
564 (let* ((designsize (ly:font-design-size font))
565 (magnification (* (ly:font-magnification font)))
566 (scaling (* magnification designsize)))
567 (debugf "scaling:~S\n" scaling)
568 (debugf "magnification:~S\n" magnification)
569 (debugf "design:~S\n" designsize)
572 (define-public (version-not-seen-message input-file-name)
578 (_ "no \\version statement found, please add~afor future compatibility")
579 (format #f "\n\n\\version ~s\n\n" (lilypond-version)))))
581 (define-public (old-relative-not-used-message input-file-name)
586 (_ "old relative compatibility not used")))