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))
53 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55 (define-public (average x . lst)
56 (/ (+ x (apply + lst)) (1+ (length lst))))
58 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
59 ;; lily specific variables.
61 (define-public default-script-alist '())
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65 ;; parser <-> output hooks.
68 (define-public (print-music-as-book parser music)
69 (let* ((head (ly:parser-lookup parser '$defaultheader))
70 (book (ly:make-book (ly:parser-lookup parser '$defaultpaper)
71 head (scorify-music music parser))))
72 (print-book-with-defaults parser book)))
74 (define-public (print-score-as-book parser score)
75 (let* ((head (ly:parser-lookup parser '$defaultheader))
76 (book (ly:make-book (ly:parser-lookup parser '$defaultpaper)
78 (print-book-with-defaults parser book)))
80 (define-public (print-score parser score)
81 (let* ((head (ly:parser-lookup parser '$defaultheader))
82 (book (ly:make-book (ly:parser-lookup parser '$defaultpaper)
84 (ly:parser-print-score parser book)))
86 (define-public (collect-scores-for-book parser score)
88 parser 'toplevel-scores
89 (cons score (ly:parser-lookup parser 'toplevel-scores))))
91 (define-public (scorify-music music parser)
92 (for-each (lambda (func)
93 (set! music (func music parser)))
94 toplevel-music-functions)
96 (ly:make-score music))
98 (define-public (collect-music-for-book parser music)
99 ;; discard music if its 'void property is true.
100 (let ((void-music (ly:music-property music 'void)))
101 (if (or (null? void-music) (not void-music))
102 (collect-scores-for-book parser (scorify-music music parser)))))
105 (define (print-book-with parser book process-procedure)
107 ((paper (ly:parser-lookup parser '$defaultpaper))
108 (layout (ly:parser-lookup parser '$defaultlayout))
109 (count (ly:parser-lookup parser 'output-count))
110 (base (ly:parser-output-name parser)))
112 (if (not (integer? count))
116 (set! base (format #f "~a-~a" base count)))
118 (ly:parser-define! parser 'output-count (1+ count))
119 (process-procedure book paper layout base)
122 (define-public (print-book-with-defaults parser book)
123 (print-book-with parser book ly:book-process))
125 (define-public (print-book-with-defaults-as-systems parser book)
126 (print-book-with parser book ly:book-process-to-systems))
131 (define-public assoc-get ly:assoc-get)
133 (define-public (uniqued-alist alist acc)
134 (if (null? alist) acc
135 (if (assoc (caar alist) acc)
136 (uniqued-alist (cdr alist) acc)
137 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
139 (define-public (alist<? x y)
140 (string<? (symbol->string (car x))
141 (symbol->string (car y))))
143 (define-public (chain-assoc x alist-list)
144 (if (null? alist-list)
146 (let* ((handle (assoc x (car alist-list))))
149 (chain-assoc x (cdr alist-list))))))
151 (define-public (chain-assoc-get x alist-list . default)
152 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
155 (define (helper x alist-list default)
156 (if (null? alist-list)
158 (let* ((handle (assoc x (car alist-list))))
161 (helper x (cdr alist-list) default)))))
164 (if (pair? default) (car default) #f)))
166 (define (map-alist-vals func list)
167 "map FUNC over the vals of LIST, leaving the keys."
170 (cons (cons (caar list) (func (cdar list)))
171 (map-alist-vals func (cdr list)))))
173 (define (map-alist-keys func list)
174 "map FUNC over the keys of an alist LIST, leaving the vals. "
177 (cons (cons (func (caar list)) (cdar list))
178 (map-alist-keys func (cdr list)))))
180 (define-public (first-member members lst)
181 "Return first successful MEMBER of member from MEMBERS in LST."
184 (let ((m (member (car members) lst)))
185 (if m m (first-member (cdr members) lst)))))
187 (define-public (first-assoc keys lst)
188 "Return first successful ASSOC of key from KEYS in LST."
191 (let ((k (assoc (car keys) lst)))
192 (if k k (first-assoc (cdr keys) lst)))))
194 (define-public (flatten-alist alist)
199 (flatten-alist (cdr alist))))))
204 (define-public (vector-for-each proc vec)
207 ((>= i (vector-length vec)) vec)
208 (vector-set! vec i (proc (vector-ref vec i)))))
213 (if (not (defined? 'hash-table?)) ;; guile 1.6 compat
215 (define hash-table? vector?)
216 (define-public (hash-for-each proc tab)
217 (hash-fold (lambda (k v prior)
222 (define-public (hash-table->alist t)
223 "Convert table t to list"
224 (apply append (vector->list t))))
228 (define-public (hash-table->alist t)
229 (hash-fold (lambda (k v acc) (acons k v acc))
232 ;; todo: code dup with C++.
233 (define-safe-public (alist->hash-table lst)
234 "Convert alist to table"
235 (let ((m (make-hash-table (length lst))))
236 (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
242 (define (functional-or . rest)
245 (apply functional-and (cdr rest)))
248 (define (functional-and . rest)
251 (apply functional-and (cdr rest)))
254 (define (split-list lst n)
255 "Split LST in N equal sized parts"
257 (define (helper todo acc-vector k)
264 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
265 (helper (cdr todo) acc-vector (1- k)))))
267 (helper lst (make-vector n '()) (1- n)))
269 (define (list-element-index lst x)
270 (define (helper todo k)
273 ((equal? (car todo) x) k)
275 (helper (cdr todo) (1+ k)))))
279 (define-public (count-list lst)
280 "Given lst (E1 E2 .. ) return ((E1 . 1) (E2 . 2) ... ) "
282 (define (helper l acc count)
284 (helper (cdr l) (cons (cons (car l) count) acc) (1+ count))
288 (reverse (helper lst '() 1)))
290 (define-public (list-join lst intermediate)
291 "put INTERMEDIATE between all elts of LST."
296 (cons elem (cons intermediate prev))
300 (define-public (filtered-map proc lst)
306 (define (flatten-list lst)
310 (if (pair? (car lst))
311 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
312 (cons (car lst) (flatten-list (cdr lst))))))
314 (define (list-minus a b)
315 "Return list of elements in A that are not in B."
316 (lset-difference eq? a b))
318 (define-public (uniq-list lst)
319 "Uniq LST, assuming that it is sorted"
322 (fold (lambda (x acc)
325 (if (eq? x (car acc))
330 (define (split-at-predicate predicate lst)
331 "Split LST = (a_1 a_2 ... a_k b_1 ... b_k)
332 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
333 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
334 L1 is copied, L2 not.
336 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
340 (define (inner-split predicate lst acc)
344 (set-car! acc (cons (car lst) (car acc)))
346 ((predicate (car lst) (cadr lst))
347 (set-car! acc (cons (car lst) (car acc)))
348 (inner-split predicate (cdr lst) acc))
350 (set-car! acc (cons (car lst) (car acc)))
351 (set-cdr! acc (cdr lst))
354 (let* ((c (cons '() '())))
355 (inner-split predicate lst c)
356 (set-car! c (reverse! (car c)))
359 (define-public (split-list-by-separator lst sep?)
360 "(display (split-list-by-separator '(a b c / d e f / g) (lambda (x) (equal? x '/))))
362 ((a b c) (d e f) (g))
365 (define (split-one sep? lst acc)
366 "Split off the first parts before separator and return both parts."
371 (split-one sep? (cdr lst) (cons (car lst) acc)))))
375 (let* ((c (split-one sep? lst '())))
376 (cons (reverse! (car c) '()) (split-list-by-separator (cdr c) sep?)))))
378 (define-public (offset-add a b)
379 (cons (+ (car a) (car b))
380 (+ (cdr a) (cdr b))))
382 (define-public (offset-flip-y o)
383 (cons (car o) (- (cdr o))))
385 (define-public (offset-scale o scale)
386 (cons (* (car o) scale)
389 (define-public (ly:list->offsets accum coords)
392 (cons (cons (car coords) (cadr coords))
393 (ly:list->offsets accum (cddr coords)))))
395 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
398 (if (not (defined? 'nan?)) ;; guile 1.6 compat
399 (define-public (nan? x) (not (or (< 0.0 x)
403 (if (not (defined? 'inf?))
404 (define-public (inf? x) (= (/ 1.0 x) 0.0)))
406 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
409 (define-public (interval-length x)
410 "Length of the number-pair X, when an interval"
411 (max 0 (- (cdr x) (car x))))
413 (define-public interval-start car)
414 (define-public (ordered-cons a b)
418 (define-public interval-end cdr)
420 (define-public (interval-index interval dir)
421 "Interpolate INTERVAL between between left (DIR=-1) and right (DIR=+1)"
423 (* (+ (interval-start interval) (interval-end interval)
424 (* dir (- (interval-end interval) (interval-start interval))))
427 (define-public (interval-center x)
428 "Center the number-pair X, when an interval"
429 (if (interval-empty? x)
431 (/ (+ (car x) (cdr x)) 2)))
433 (define-public interval-start car)
434 (define-public interval-end cdr)
435 (define-public (interval-translate iv amount)
436 (cons (+ amount (car iv))
437 (+ amount (cdr iv))))
439 (define (other-axis a)
440 (remainder (+ a 1) 2))
442 (define-public (interval-widen iv amount)
443 (cons (- (car iv) amount)
444 (+ (cdr iv) amount)))
447 (define-public (interval-empty? iv)
448 (> (car iv) (cdr iv)))
450 (define-public (interval-union i1 i2)
451 (cons (min (car i1) (car i2))
452 (max (cdr i1) (cdr i2))))
454 (define-public (interval-sane? i)
455 (not (or (nan? (car i))
459 (> (car i) (cdr i)))))
462 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
466 (define-public (string-encode-integer i)
469 ((< i 0) (string-append "n" (string-encode-integer (- i))))
471 (make-string 1 (integer->char (+ 65 (modulo i 26))))
472 (string-encode-integer (quotient i 26))))))
474 (define-public (ly:numbers->string lst)
475 (string-join (map ly:number->string lst) " "))
477 (define (number->octal-string x)
478 (let* ((n (inexact->exact x))
479 (n64 (quotient n 64))
480 (n8 (quotient (- n (* n64 64)) 8)))
484 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
486 (define-public (ly:inexact->string x radix)
487 (let ((n (inexact->exact x)))
488 (number->string n radix)))
490 (define-public (ly:number-pair->string c)
491 (string-append (ly:number->string (car c)) " "
492 (ly:number->string (cdr c))))
495 (define-public (write-me message x)
496 "Return X. Display MESSAGE and write X. Handy for debugging,
497 possibly turned off."
498 (display message) (write x) (newline) x)
501 (define-public (stderr string . rest)
502 (apply format (cons (current-error-port) (cons string rest)))
503 (force-output (current-error-port)))
505 (define-public (debugf string . rest)
507 (apply stderr (cons string rest))))
509 (define (index-cell cell dir)
514 (define (cons-map f x)
515 "map F to contents of X"
516 (cons (f (car x)) (f (cdr x))))
518 (define-public (list-insert-separator lst between)
519 "Create new list, inserting BETWEEN between elements of LIST"
523 (cons x (cons between y))))
524 (fold-right conc #f lst))
526 (define-public (string-regexp-substitute a b str)
527 (regexp-substitute/global #f a str 'pre b 'post))
529 (define (regexp-split str regex)
531 (define end-of-prev-match 0)
532 (define (notice match)
534 (set! matches (cons (substring (match:string match)
538 (set! end-of-prev-match (match:end match)))
540 (regexp-substitute/global #f regex str notice 'post)
542 (if (< end-of-prev-match (string-length str))
545 (cons (substring str end-of-prev-match (string-length str)) matches)))
556 (define-public (car< a b) (< (car a) (car b)))
558 (define-public (symbol<? lst r)
559 (string<? (symbol->string lst) (symbol->string r)))
561 (define-public (symbol-key<? lst r)
562 (string<? (symbol->string (car lst)) (symbol->string (car r))))
565 ;; don't confuse users with #<procedure .. > syntax.
567 (define-public (scm->string val)
568 (if (and (procedure? val) (symbol? (procedure-name val)))
569 (symbol->string (procedure-name val))
571 (if (self-evaluating? val) "" "'")
572 (call-with-output-string (lambda (port) (display val port))))))
574 (define-public (!= lst r)
577 (define-public lily-unit->bigpoint-factor
579 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
580 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
581 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
583 (define-public lily-unit->mm-factor
584 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
586 ;;; FONT may be font smob, or pango font string...
587 (define-public (font-name-style font)
588 ;; FIXME: ughr, (ly:font-name) sometimes also has Style appended.
589 (let* ((font-name (ly:font-name font))
590 (full-name (if font-name font-name (ly:font-file-name font)))
591 (name-style (string-split full-name #\-)))
592 ;; FIXME: ughr, barf: feta-alphabet is actually emmentaler
593 (if (string-prefix? "feta-alphabet" full-name)
595 (substring full-name (string-length "feta-alphabet")))
596 (if (not (null? (cdr name-style)))
598 (append name-style '("Regular"))))))
600 (define-public (modified-font-metric-font-scaling font)
601 (let* ((designsize (ly:font-design-size font))
602 (magnification (* (ly:font-magnification font)))
603 (scaling (* magnification designsize)))
604 (debugf "scaling:~S\n" scaling)
605 (debugf "magnification:~S\n" magnification)
606 (debugf "design:~S\n" designsize)
609 (define-public (version-not-seen-message input-file-name)
612 input-file-name ": 0: " (_ "warning: ")
614 (_ "no \\version statement found, please add~afor future compatibility")
615 (format #f "\n\n\\version ~s\n\n" (lilypond-version))))))
617 (define-public (old-relative-not-used-message input-file-name)
620 input-file-name ": 0: " (_ "warning: ")
621 (_ "old relative compatibility not used"))))