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 -4)
23 (define-safe-public THREE-Q-FLAT -3)
24 (define-safe-public FLAT -2)
25 (define-safe-public SEMI-FLAT -1)
26 (define-safe-public NATURAL 0)
27 (define-safe-public SEMI-SHARP 1)
28 (define-safe-public SHARP 2)
29 (define-safe-public THREE-Q-SHARP 3)
30 (define-safe-public DOUBLE-SHARP 4)
31 (define-safe-public SEMI-TONE 2)
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 (define-public ZERO-MOMENT (ly:make-moment 0 1))
38 (define-public (moment-min a b)
39 (if (ly:moment<? a b) a b))
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 (define-public (average x . lst)
45 (/ (+ x (apply + lst)) (1+ (length lst))))
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 ;; lily specific variables.
50 (define-public default-script-alist '())
54 (define-public (print-music-as-book parser music)
55 (let* ((head (ly:parser-lookup parser '$defaultheader))
56 (book (ly:make-book (ly:parser-lookup parser '$defaultpaper)
57 head (scorify-music music parser))))
58 (print-book-with-defaults parser book)))
60 (define-public (print-score-as-book parser score)
61 (let* ((head (ly:parser-lookup parser '$defaultheader))
62 (book (ly:make-book (ly:parser-lookup parser '$defaultpaper)
64 (print-book-with-defaults parser book)))
66 (define-public (print-score parser score)
67 (let* ((head (ly:parser-lookup parser '$defaultheader))
68 (book (ly:make-book (ly:parser-lookup parser '$defaultpaper)
70 (ly:parser-print-score parser book)))
72 (define-public (collect-scores-for-book parser score)
74 parser 'toplevel-scores
75 (cons score (ly:parser-lookup parser 'toplevel-scores))))
78 (define-public (scorify-music music parser)
80 (for-each (lambda (func)
81 (set! music (func music parser)))
82 toplevel-music-functions)
84 (ly:make-score music))
86 (define-public (collect-music-for-book parser music)
87 ;; discard music if its 'void property is true.
88 (let ((void-music (ly:music-property music 'void)))
89 (if (or (null? void-music) (not void-music))
90 (collect-scores-for-book parser (scorify-music music parser)))))
93 (define-public (print-book-with-defaults parser book)
95 ((paper (ly:parser-lookup parser '$defaultpaper))
96 (layout (ly:parser-lookup parser '$defaultlayout))
97 (count (ly:parser-lookup parser 'output-count))
98 (base (ly:parser-output-name parser)))
100 (if (not (integer? count))
104 (set! base (format #f "~a-~a" base count)))
106 (ly:parser-define! parser 'output-count (1+ count))
107 (ly:book-process book paper layout base)
110 (define-public (print-score-with-defaults parser score)
112 ((paper (ly:parser-lookup parser '$defaultpaper))
113 (layout (ly:parser-lookup parser '$defaultlayout))
114 (header (ly:parser-lookup parser '$defaultheader))
115 (count (ly:parser-lookup parser 'output-count))
116 (base (ly:parser-output-name parser)))
118 (if (not (integer? count))
122 (set! base (format #f "~a-~a" base count)))
124 (ly:parser-define! parser 'output-count (1+ count))
125 (ly:score-process score header paper layout base)
132 (define-public assoc-get ly:assoc-get)
134 (define-public (uniqued-alist alist acc)
135 (if (null? alist) acc
136 (if (assoc (caar alist) acc)
137 (uniqued-alist (cdr alist) acc)
138 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
140 (define-public (alist<? x y)
141 (string<? (symbol->string (car x))
142 (symbol->string (car y))))
144 (define-public (chain-assoc x alist-list)
145 (if (null? alist-list)
147 (let* ((handle (assoc x (car alist-list))))
150 (chain-assoc x (cdr alist-list))))))
152 (define-public (chain-assoc-get x alist-list . default)
153 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
156 (define (helper x alist-list default)
157 (if (null? alist-list)
159 (let* ((handle (assoc x (car alist-list))))
162 (helper x (cdr alist-list) default)))))
165 (if (pair? default) (car default) #f)))
167 (define (map-alist-vals func list)
168 "map FUNC over the vals of LIST, leaving the keys."
171 (cons (cons (caar list) (func (cdar list)))
172 (map-alist-vals func (cdr list)))))
174 (define (map-alist-keys func list)
175 "map FUNC over the keys of an alist LIST, leaving the vals. "
178 (cons (cons (func (caar list)) (cdar list))
179 (map-alist-keys func (cdr list)))))
181 (define-public (first-member members lst)
182 "Return first successful MEMBER of member from MEMBERS in LST."
185 (let ((m (member (car members) lst)))
186 (if m m (first-member (cdr members) lst)))))
188 (define-public (first-assoc keys lst)
189 "Return first successful ASSOC of key from KEYS in LST."
192 (let ((k (assoc (car keys) lst)))
193 (if k k (first-assoc (cdr keys) lst)))))
195 (define-public (flatten-alist alist)
200 (flatten-alist (cdr alist))))))
205 (define-public (vector-for-each proc vec)
208 ((>= i (vector-length vec)) vec)
209 (vector-set! vec i (proc (vector-ref vec i)))))
214 (if (not (defined? 'hash-table?)) ;; guile 1.6 compat
216 (define hash-table? vector?)
217 (define-public (hash-for-each proc tab)
218 (hash-fold (lambda (k v prior)
223 (define-public (hash-table->alist t)
224 "Convert table t to list"
225 (apply append (vector->list t))))
229 (define-public (hash-table->alist t)
230 (hash-fold (lambda (k v acc) (acons k v acc))
233 ;; todo: code dup with C++.
234 (define-safe-public (alist->hash-table lst)
235 "Convert alist to table"
236 (let ((m (make-hash-table (length lst))))
237 (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
243 (define (functional-or . rest)
246 (apply functional-and (cdr rest)))
249 (define (functional-and . rest)
252 (apply functional-and (cdr rest)))
255 (define (split-list lst n)
256 "Split LST in N equal sized parts"
258 (define (helper todo acc-vector k)
265 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
266 (helper (cdr todo) acc-vector (1- k)))))
268 (helper lst (make-vector n '()) (1- n)))
270 (define (list-element-index lst x)
271 (define (helper todo k)
274 ((equal? (car todo) x) k)
276 (helper (cdr todo) (1+ k)))))
280 (define-public (count-list lst)
281 "Given lst (E1 E2 .. ) return ((E1 . 1) (E2 . 2) ... ) "
283 (define (helper l acc count)
285 (helper (cdr l) (cons (cons (car l) count) acc) (1+ count))
289 (reverse (helper lst '() 1)))
291 (define-public (list-join lst intermediate)
292 "put INTERMEDIATE between all elts of LST."
297 (cons elem (cons intermediate prev))
301 (define-public (filtered-map proc lst)
307 (define (flatten-list lst)
311 (if (pair? (car lst))
312 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
313 (cons (car lst) (flatten-list (cdr lst))))))
315 (define (list-minus a b)
316 "Return list of elements in A that are not in B."
317 (lset-difference eq? a b))
319 (define-public (uniq-list lst)
320 "Uniq LST, assuming that it is sorted"
323 (fold (lambda (x acc)
326 (if (eq? x (car acc))
331 (define (split-at-predicate predicate lst)
332 "Split LST = (a_1 a_2 ... a_k b_1 ... b_k)
333 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
334 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
335 L1 is copied, L2 not.
337 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
341 (define (inner-split predicate lst acc)
345 (set-car! acc (cons (car lst) (car acc)))
347 ((predicate (car lst) (cadr lst))
348 (set-car! acc (cons (car lst) (car acc)))
349 (inner-split predicate (cdr lst) acc))
351 (set-car! acc (cons (car lst) (car acc)))
352 (set-cdr! acc (cdr lst))
355 (let* ((c (cons '() '())))
356 (inner-split predicate lst c)
357 (set-car! c (reverse! (car c)))
360 (define-public (split-list-by-separator lst sep?)
361 "(display (split-list-by-separator '(a b c / d e f / g) (lambda (x) (equal? x '/))))
363 ((a b c) (d e f) (g))
366 (define (split-one sep? lst acc)
367 "Split off the first parts before separator and return both parts."
372 (split-one sep? (cdr lst) (cons (car lst) acc)))))
376 (let* ((c (split-one sep? lst '())))
377 (cons (reverse! (car c) '()) (split-list-by-separator (cdr c) sep?)))))
379 (define-public (offset-add a b)
380 (cons (+ (car a) (car b))
381 (+ (cdr a) (cdr b))))
383 (define-public (offset-flip-y o)
384 (cons (car o) (- (cdr o))))
386 (define-public (offset-scale o scale)
387 (cons (* (car o) scale)
390 (define-public (ly:list->offsets accum coords)
393 (cons (cons (car coords) (cadr coords))
394 (ly:list->offsets accum (cddr coords)))))
396 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
399 (if (not (defined? 'nan?)) ;; guile 1.6 compat
400 (define-public (nan? x) (not (or (< 0.0 x)
404 (if (not (defined? 'inf?))
405 (define-public (inf? x) (= (/ 1.0 x) 0.0)))
407 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410 (define-public (interval-length x)
411 "Length of the number-pair X, when an interval"
412 (max 0 (- (cdr x) (car x))))
414 (define-public interval-start car)
415 (define-public (ordered-cons a b)
419 (define-public interval-end cdr)
421 (define-public (interval-index interval dir)
422 "Interpolate INTERVAL between between left (DIR=-1) and right (DIR=+1)"
424 (* (+ (interval-start interval) (interval-end interval)
425 (* dir (- (interval-end interval) (interval-start interval))))
428 (define-public (interval-center x)
429 "Center the number-pair X, when an interval"
430 (if (interval-empty? x)
432 (/ (+ (car x) (cdr x)) 2)))
434 (define-public interval-start car)
435 (define-public interval-end cdr)
436 (define-public (interval-translate iv amount)
437 (cons (+ amount (car iv))
438 (+ amount (cdr iv))))
440 (define (other-axis a)
441 (remainder (+ a 1) 2))
443 (define-public (interval-widen iv amount)
444 (cons (- (car iv) amount)
445 (+ (cdr iv) amount)))
448 (define-public (interval-empty? iv)
449 (> (car iv) (cdr iv)))
451 (define-public (interval-union i1 i2)
452 (cons (min (car i1) (car i2))
453 (max (cdr i1) (cdr i2))))
455 (define-public (interval-sane? i)
456 (not (or (nan? (car i))
460 (> (car i) (cdr i)))))
463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 (define-public (string-encode-integer i)
470 ((< i 0) (string-append "n" (string-encode-integer (- i))))
472 (make-string 1 (integer->char (+ 65 (modulo i 26))))
473 (string-encode-integer (quotient i 26))))))
475 (define-public (ly:numbers->string lst)
476 (string-join (map ly:number->string lst) " "))
478 (define (number->octal-string x)
479 (let* ((n (inexact->exact x))
480 (n64 (quotient n 64))
481 (n8 (quotient (- n (* n64 64)) 8)))
485 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
487 (define-public (ly:inexact->string x radix)
488 (let ((n (inexact->exact x)))
489 (number->string n radix)))
491 (define-public (ly:number-pair->string c)
492 (string-append (ly:number->string (car c)) " "
493 (ly:number->string (cdr c))))
496 (define-public (write-me message x)
497 "Return X. Display MESSAGE and write X. Handy for debugging,
498 possibly turned off."
499 (display message) (write x) (newline) x)
502 (define-public (stderr string . rest)
503 (apply format (cons (current-error-port) (cons string rest)))
504 (force-output (current-error-port)))
506 (define-public (debugf string . rest)
508 (apply stderr (cons string rest))))
510 (define (index-cell cell dir)
515 (define (cons-map f x)
516 "map F to contents of X"
517 (cons (f (car x)) (f (cdr x))))
519 (define-public (list-insert-separator lst between)
520 "Create new list, inserting BETWEEN between elements of LIST"
524 (cons x (cons between y))))
525 (fold-right conc #f lst))
527 (define-public (string-regexp-substitute a b str)
528 (regexp-substitute/global #f a str 'pre b 'post))
530 (define (regexp-split str regex)
532 (define end-of-prev-match 0)
533 (define (notice match)
535 (set! matches (cons (substring (match:string match)
539 (set! end-of-prev-match (match:end match)))
541 (regexp-substitute/global #f regex str notice 'post)
543 (if (< end-of-prev-match (string-length str))
546 (cons (substring str end-of-prev-match (string-length str)) matches)))
557 (define-public (car< a b) (< (car a) (car b)))
559 (define-public (symbol<? lst r)
560 (string<? (symbol->string lst) (symbol->string r)))
562 (define-public (symbol-key<? lst r)
563 (string<? (symbol->string (car lst)) (symbol->string (car r))))
566 ;; don't confuse users with #<procedure .. > syntax.
568 (define-public (scm->string val)
569 (if (and (procedure? val) (symbol? (procedure-name val)))
570 (symbol->string (procedure-name val))
572 (if (self-evaluating? val) "" "'")
573 (call-with-output-string (lambda (port) (display val port))))))
575 (define-public (!= lst r)
578 (define-public lily-unit->bigpoint-factor
580 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
581 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
582 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
584 (define-public lily-unit->mm-factor
585 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
587 ;;; FONT may be font smob, or pango font string...
588 (define-public (font-name-style font)
589 ;; FIXME: ughr, (ly:font-name) sometimes also has Style appended.
590 (let* ((font-name (ly:font-name font))
591 (full-name (if font-name font-name (ly:font-file-name font)))
592 (name-style (string-split full-name #\-)))
593 ;; FIXME: ughr, barf: feta-alphabet is actually emmentaler
594 (if (string-prefix? "feta-alphabet" full-name)
596 (substring full-name (string-length "feta-alphabet")))
597 (if (not (null? (cdr name-style)))
599 (append name-style '("Regular"))))))
601 (define-public (modified-font-metric-font-scaling font)
602 (let* ((designsize (ly:font-design-size font))
603 (magnification (* (ly:font-magnification font)))
604 (scaling (* magnification designsize)))
605 (debugf "scaling:~S\n" scaling)
606 (debugf "magnification:~S\n" magnification)
607 (debugf "design:~S\n" designsize)
610 (define-public (version-not-seen-message input-file-name)
613 input-file-name ": 0: " (_ "warning: ")
615 (_ "no \\version statement found, please add~afor future compatibility")
616 (format #f "\n\n\\version ~s\n\n" (lilypond-version))))))
618 (define-public (old-relative-not-used-message input-file-name)
621 input-file-name ": 0: " (_ "warning: ")
622 (_ "old relative compatibility not used"))))