2 ;;;; lily-library.scm -- utilities
4 ;;;; source file of the GNU LilyPond music typesetter
6 ;;;; (c) 1998--2007 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 (collect-music-aux score-handler parser music)
67 (define (music-property symbol)
68 (let ((value (ly:music-property music symbol)))
69 (if (not (null? value))
72 (cond ((music-property 'page-marker)
73 ;; a page marker: set page break/turn permissions or label
75 (let ((label (music-property 'page-label)))
77 (score-handler (ly:make-page-label-marker label))))
78 (for-each (lambda (symbol)
79 (let ((permission (music-property symbol)))
80 (if (symbol? permission)
82 (ly:make-page-permission-marker symbol
83 (if (eqv? 'forbid permission)
86 (list 'line-break-permission 'page-break-permission
87 'page-turn-permission))))
88 ((not (music-property 'void))
89 ;; a regular music expression: make a score with this music
90 ;; void music is discarded
91 (score-handler (scorify-music music parser)))))
93 (define-public (collect-music-for-book parser music)
94 "Top-level music handler"
95 (collect-music-aux (lambda (score)
96 (collect-scores-for-book parser score))
100 (define-public (collect-book-music-for-book parser book music)
102 (collect-music-aux (lambda (score)
103 (ly:book-add-score! book score))
107 (define-public (scorify-music music parser)
110 (for-each (lambda (func)
111 (set! music (func music parser)))
112 toplevel-music-functions)
114 (ly:make-score music))
116 (define (print-book-with parser book process-procedure)
118 ((paper (ly:parser-lookup parser '$defaultpaper))
119 (layout (ly:parser-lookup parser '$defaultlayout))
121 (count (ly:parser-lookup parser 'output-count))
122 (base (ly:parser-output-name parser)))
124 ;; must be careful: output-count is under user control.
125 (if (not (integer? count))
129 (set! base (format #f "~a-~a" base count)))
131 (ly:parser-define! parser 'output-count (1+ count))
132 (process-procedure book paper layout base)
135 (define-public (print-book-with-defaults parser book)
136 (print-book-with parser book ly:book-process))
138 (define-public (print-book-with-defaults-as-systems parser book)
139 (print-book-with parser book ly:book-process-to-systems))
144 (define-public assoc-get ly:assoc-get)
146 (define-public (uniqued-alist alist acc)
147 (if (null? alist) acc
148 (if (assoc (caar alist) acc)
149 (uniqued-alist (cdr alist) acc)
150 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
152 (define-public (alist<? x y)
153 (string<? (symbol->string (car x))
154 (symbol->string (car y))))
156 (define-public (chain-assoc-get x alist-list . default)
157 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
160 (define (helper x alist-list default)
161 (if (null? alist-list)
163 (let* ((handle (assoc x (car alist-list))))
166 (helper x (cdr alist-list) default)))))
169 (if (pair? default) (car default) #f)))
171 (define (map-alist-vals func list)
172 "map FUNC over the vals of LIST, leaving the keys."
175 (cons (cons (caar list) (func (cdar list)))
176 (map-alist-vals func (cdr list)))))
178 (define (map-alist-keys func list)
179 "map FUNC over the keys of an alist LIST, leaving the vals. "
182 (cons (cons (func (caar list)) (cdar list))
183 (map-alist-keys func (cdr list)))))
185 (define-public (first-member members lst)
186 "Return first successful MEMBER of member from MEMBERS in LST."
189 (let ((m (member (car members) lst)))
190 (if m m (first-member (cdr members) lst)))))
192 (define-public (first-assoc keys lst)
193 "Return first successful ASSOC of key from KEYS in LST."
196 (let ((k (assoc (car keys) lst)))
197 (if k k (first-assoc (cdr keys) lst)))))
199 (define-public (flatten-alist alist)
204 (flatten-alist (cdr alist))))))
209 (define-public (vector-for-each proc vec)
212 ((>= i (vector-length vec)) vec)
213 (vector-set! vec i (proc (vector-ref vec i)))))
218 (define-public (hash-table->alist t)
219 (hash-fold (lambda (k v acc) (acons k v acc))
222 ;; todo: code dup with C++.
223 (define-safe-public (alist->hash-table lst)
224 "Convert alist to table"
225 (let ((m (make-hash-table (length lst))))
226 (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
232 (define (functional-or . rest)
235 (apply functional-and (cdr rest)))
238 (define (functional-and . rest)
241 (apply functional-and (cdr rest)))
244 (define (split-list lst n)
245 "Split LST in N equal sized parts"
247 (define (helper todo acc-vector k)
254 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
255 (helper (cdr todo) acc-vector (1- k)))))
257 (helper lst (make-vector n '()) (1- n)))
259 (define (list-element-index lst x)
260 (define (helper todo k)
263 ((equal? (car todo) x) k)
265 (helper (cdr todo) (1+ k)))))
269 (define-public (count-list lst)
270 "Given lst (E1 E2 .. ) return ((E1 . 1) (E2 . 2) ... ) "
272 (define (helper l acc count)
274 (helper (cdr l) (cons (cons (car l) count) acc) (1+ count))
278 (reverse (helper lst '() 1)))
280 (define-public (list-join lst intermediate)
281 "put INTERMEDIATE between all elts of LST."
286 (cons elem (cons intermediate prev))
290 (define-public (filtered-map proc lst)
296 (define (flatten-list lst)
300 (if (pair? (car lst))
301 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
302 (cons (car lst) (flatten-list (cdr lst))))))
304 (define (list-minus a b)
305 "Return list of elements in A that are not in B."
306 (lset-difference eq? a b))
308 (define-public (uniq-list lst)
309 "Uniq LST, assuming that it is sorted"
312 (fold (lambda (x acc)
315 (if (eq? x (car acc))
320 (define (split-at-predicate predicate lst)
321 "Split LST = (a_1 a_2 ... a_k b_1 ... b_k)
322 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
323 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
324 L1 is copied, L2 not.
326 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
330 (define (inner-split predicate lst acc)
334 (set-car! acc (cons (car lst) (car acc)))
336 ((predicate (car lst) (cadr lst))
337 (set-car! acc (cons (car lst) (car acc)))
338 (inner-split predicate (cdr lst) acc))
340 (set-car! acc (cons (car lst) (car acc)))
341 (set-cdr! acc (cdr lst))
344 (let* ((c (cons '() '())))
345 (inner-split predicate lst c)
346 (set-car! c (reverse! (car c)))
349 (define-public (split-list-by-separator lst sep?)
350 "(display (split-list-by-separator '(a b c / d e f / g) (lambda (x) (equal? x '/))))
352 ((a b c) (d e f) (g))
355 (define (split-one sep? lst acc)
356 "Split off the first parts before separator and return both parts."
361 (split-one sep? (cdr lst) (cons (car lst) acc)))))
365 (let* ((c (split-one sep? lst '())))
366 (cons (reverse! (car c) '()) (split-list-by-separator (cdr c) sep?)))))
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 (interval-length x)
400 "Length of the number-pair X, when an interval"
401 (max 0 (- (cdr x) (car x))))
403 (define-public interval-start car)
404 (define-public (ordered-cons a b)
408 (define-public interval-end cdr)
410 (define-public (interval-bound interval dir)
411 ((if (= dir RIGHT) cdr car) interval))
413 (define-public (interval-index interval dir)
414 "Interpolate INTERVAL between between left (DIR=-1) and right (DIR=+1)"
416 (* (+ (interval-start interval) (interval-end interval)
417 (* dir (- (interval-end interval) (interval-start interval))))
420 (define-public (interval-center x)
421 "Center the number-pair X, when an interval"
422 (if (interval-empty? x)
424 (/ (+ (car x) (cdr x)) 2)))
426 (define-public interval-start car)
427 (define-public interval-end cdr)
428 (define-public (interval-translate iv amount)
429 (cons (+ amount (car iv))
430 (+ amount (cdr iv))))
432 (define (other-axis a)
433 (remainder (+ a 1) 2))
435 (define-public (interval-widen iv amount)
436 (cons (- (car iv) amount)
437 (+ (cdr iv) amount)))
440 (define-public (interval-empty? iv)
441 (> (car iv) (cdr iv)))
443 (define-public (interval-union i1 i2)
444 (cons (min (car i1) (car i2))
445 (max (cdr i1) (cdr i2))))
447 (define-public (interval-sane? i)
448 (not (or (nan? (car i))
452 (> (car i) (cdr i)))))
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
458 (define-public (string-endswith s suffix)
459 (equal? suffix (substring s
460 (max 0 (- (string-length s) (string-length suffix)))
463 (define-public (string-startswith s prefix)
464 (equal? prefix (substring s 0 (min (string-length s) (string-length prefix)))))
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 (number->octal-string x)
475 (let* ((n (inexact->exact x))
476 (n64 (quotient n 64))
477 (n8 (quotient (- n (* n64 64)) 8)))
481 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
483 (define-public (ly:inexact->string x radix)
484 (let ((n (inexact->exact x)))
485 (number->string n radix)))
487 (define-public (ly:number-pair->string c)
488 (string-append (ly:number->string (car c)) " "
489 (ly:number->string (cdr c))))
492 (define-public (write-me message x)
493 "Return X. Display MESSAGE and write X. Handy for debugging,
494 possibly turned off."
495 (display message) (write x) (newline) x)
498 (define-public (stderr string . rest)
499 (apply format (cons (current-error-port) (cons string rest)))
500 (force-output (current-error-port)))
502 (define-public (debugf string . rest)
504 (apply stderr (cons string rest))))
506 (define (index-cell cell dir)
511 (define (cons-map f x)
512 "map F to contents of X"
513 (cons (f (car x)) (f (cdr x))))
515 (define-public (list-insert-separator lst between)
516 "Create new list, inserting BETWEEN between elements of LIST"
520 (cons x (cons between y))))
521 (fold-right conc #f lst))
523 (define-public (string-regexp-substitute a b str)
524 (regexp-substitute/global #f a str 'pre b 'post))
526 (define (regexp-split str regex)
528 (define end-of-prev-match 0)
529 (define (notice match)
531 (set! matches (cons (substring (match:string match)
535 (set! end-of-prev-match (match:end match)))
537 (regexp-substitute/global #f regex str notice 'post)
539 (if (< end-of-prev-match (string-length str))
542 (cons (substring str end-of-prev-match (string-length str)) matches)))
554 (define-public (car< a b)
557 (define-public (symbol<? lst r)
558 (string<? (symbol->string lst) (symbol->string r)))
560 (define-public (symbol-key<? lst r)
561 (string<? (symbol->string (car lst)) (symbol->string (car r))))
564 ;; don't confuse users with #<procedure .. > syntax.
566 (define-public (scm->string val)
567 (if (and (procedure? val) (symbol? (procedure-name val)))
568 (symbol->string (procedure-name val))
570 (if (self-evaluating? val) "" "'")
571 (call-with-output-string (lambda (port) (display val port))))))
573 (define-public (!= lst r)
576 (define-public lily-unit->bigpoint-factor
578 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
579 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
580 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
582 (define-public lily-unit->mm-factor
583 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
585 ;;; FONT may be font smob, or pango font string...
586 (define-public (font-name-style font)
587 ;; FIXME: ughr, (ly:font-name) sometimes also has Style appended.
588 (let* ((font-name (ly:font-name font))
589 (full-name (if font-name font-name (ly:font-file-name font)))
590 (name-style (string-split full-name #\-)))
591 ;; FIXME: ughr, barf: feta-alphabet is actually emmentaler
592 (if (string-prefix? "feta-alphabet" full-name)
594 (substring full-name (string-length "feta-alphabet")))
595 (if (not (null? (cdr name-style)))
597 (append name-style '("Regular"))))))
599 (define-public (modified-font-metric-font-scaling font)
600 (let* ((designsize (ly:font-design-size font))
601 (magnification (* (ly:font-magnification font)))
602 (scaling (* magnification designsize)))
603 (debugf "scaling:~S\n" scaling)
604 (debugf "magnification:~S\n" magnification)
605 (debugf "design:~S\n" designsize)
608 (define-public (version-not-seen-message input-file-name)
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)
622 (_ "old relative compatibility not used")))