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))
85 (count (ly:parser-lookup parser 'output-count))
86 (base (ly:parser-output-name parser)))
88 (if (not (integer? count))
92 (set! base (format #f "~a-~a" base count)))
94 (ly:parser-define! parser 'output-count (1+ count))
95 (process-procedure book paper layout base)
98 (define-public (print-book-with-defaults parser book)
99 (print-book-with parser book ly:book-process))
101 (define-public (print-book-with-defaults-as-systems parser book)
102 (print-book-with parser book ly:book-process-to-systems))
107 (define-public assoc-get ly:assoc-get)
109 (define-public (uniqued-alist alist acc)
110 (if (null? alist) acc
111 (if (assoc (caar alist) acc)
112 (uniqued-alist (cdr alist) acc)
113 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
115 (define-public (alist<? x y)
116 (string<? (symbol->string (car x))
117 (symbol->string (car y))))
119 (define-public (chain-assoc x alist-list)
120 (if (null? alist-list)
122 (let* ((handle (assoc x (car alist-list))))
125 (chain-assoc x (cdr alist-list))))))
127 (define-public (chain-assoc-get x alist-list . default)
128 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
131 (define (helper x alist-list default)
132 (if (null? alist-list)
134 (let* ((handle (assoc x (car alist-list))))
137 (helper x (cdr alist-list) default)))))
140 (if (pair? default) (car default) #f)))
142 (define (map-alist-vals func list)
143 "map FUNC over the vals of LIST, leaving the keys."
146 (cons (cons (caar list) (func (cdar list)))
147 (map-alist-vals func (cdr list)))))
149 (define (map-alist-keys func list)
150 "map FUNC over the keys of an alist LIST, leaving the vals. "
153 (cons (cons (func (caar list)) (cdar list))
154 (map-alist-keys func (cdr list)))))
156 (define-public (first-member members lst)
157 "Return first successful MEMBER of member from MEMBERS in LST."
160 (let ((m (member (car members) lst)))
161 (if m m (first-member (cdr members) lst)))))
163 (define-public (first-assoc keys lst)
164 "Return first successful ASSOC of key from KEYS in LST."
167 (let ((k (assoc (car keys) lst)))
168 (if k k (first-assoc (cdr keys) lst)))))
170 (define-public (flatten-alist alist)
175 (flatten-alist (cdr alist))))))
180 (define-public (vector-for-each proc vec)
183 ((>= i (vector-length vec)) vec)
184 (vector-set! vec i (proc (vector-ref vec i)))))
189 (if (not (defined? 'hash-table?)) ;; guile 1.6 compat
191 (define hash-table? vector?)
192 (define-public (hash-for-each proc tab)
193 (hash-fold (lambda (k v prior)
198 (define-public (hash-table->alist t)
199 "Convert table t to list"
200 (apply append (vector->list t))))
204 (define-public (hash-table->alist t)
205 (hash-fold (lambda (k v acc) (acons k v acc))
208 ;; todo: code dup with C++.
209 (define-safe-public (alist->hash-table lst)
210 "Convert alist to table"
211 (let ((m (make-hash-table (length lst))))
212 (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
218 (define (functional-or . rest)
221 (apply functional-and (cdr rest)))
224 (define (functional-and . rest)
227 (apply functional-and (cdr rest)))
230 (define (split-list lst n)
231 "Split LST in N equal sized parts"
233 (define (helper todo acc-vector k)
240 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
241 (helper (cdr todo) acc-vector (1- k)))))
243 (helper lst (make-vector n '()) (1- n)))
245 (define (list-element-index lst x)
246 (define (helper todo k)
249 ((equal? (car todo) x) k)
251 (helper (cdr todo) (1+ k)))))
255 (define-public (count-list lst)
256 "Given lst (E1 E2 .. ) return ((E1 . 1) (E2 . 2) ... ) "
258 (define (helper l acc count)
260 (helper (cdr l) (cons (cons (car l) count) acc) (1+ count))
264 (reverse (helper lst '() 1)))
266 (define-public (list-join lst intermediate)
267 "put INTERMEDIATE between all elts of LST."
272 (cons elem (cons intermediate prev))
276 (define-public (filtered-map proc lst)
282 (define (flatten-list lst)
286 (if (pair? (car lst))
287 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
288 (cons (car lst) (flatten-list (cdr lst))))))
290 (define (list-minus a b)
291 "Return list of elements in A that are not in B."
292 (lset-difference eq? a b))
294 (define-public (uniq-list lst)
295 "Uniq LST, assuming that it is sorted"
298 (fold (lambda (x acc)
301 (if (eq? x (car acc))
306 (define (split-at-predicate predicate lst)
307 "Split LST = (a_1 a_2 ... a_k b_1 ... b_k)
308 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
309 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
310 L1 is copied, L2 not.
312 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
316 (define (inner-split predicate lst acc)
320 (set-car! acc (cons (car lst) (car acc)))
322 ((predicate (car lst) (cadr lst))
323 (set-car! acc (cons (car lst) (car acc)))
324 (inner-split predicate (cdr lst) acc))
326 (set-car! acc (cons (car lst) (car acc)))
327 (set-cdr! acc (cdr lst))
330 (let* ((c (cons '() '())))
331 (inner-split predicate lst c)
332 (set-car! c (reverse! (car c)))
335 (define-public (split-list-by-separator lst sep?)
336 "(display (split-list-by-separator '(a b c / d e f / g) (lambda (x) (equal? x '/))))
338 ((a b c) (d e f) (g))
341 (define (split-one sep? lst acc)
342 "Split off the first parts before separator and return both parts."
347 (split-one sep? (cdr lst) (cons (car lst) acc)))))
351 (let* ((c (split-one sep? lst '())))
352 (cons (reverse! (car c) '()) (split-list-by-separator (cdr c) sep?)))))
354 (define-public (offset-add a b)
355 (cons (+ (car a) (car b))
356 (+ (cdr a) (cdr b))))
358 (define-public (offset-flip-y o)
359 (cons (car o) (- (cdr o))))
361 (define-public (offset-scale o scale)
362 (cons (* (car o) scale)
365 (define-public (ly:list->offsets accum coords)
368 (cons (cons (car coords) (cadr coords))
369 (ly:list->offsets accum (cddr coords)))))
371 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374 (if (not (defined? 'nan?)) ;; guile 1.6 compat
375 (define-public (nan? x) (not (or (< 0.0 x)
379 (if (not (defined? 'inf?))
380 (define-public (inf? x) (= (/ 1.0 x) 0.0)))
382 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
385 (define-public (interval-length x)
386 "Length of the number-pair X, when an interval"
387 (max 0 (- (cdr x) (car x))))
389 (define-public interval-start car)
390 (define-public (ordered-cons a b)
394 (define-public interval-end cdr)
396 (define-public (interval-index interval dir)
397 "Interpolate INTERVAL between between left (DIR=-1) and right (DIR=+1)"
399 (* (+ (interval-start interval) (interval-end interval)
400 (* dir (- (interval-end interval) (interval-start interval))))
403 (define-public (interval-center x)
404 "Center the number-pair X, when an interval"
405 (if (interval-empty? x)
407 (/ (+ (car x) (cdr x)) 2)))
409 (define-public interval-start car)
410 (define-public interval-end cdr)
411 (define-public (interval-translate iv amount)
412 (cons (+ amount (car iv))
413 (+ amount (cdr iv))))
415 (define (other-axis a)
416 (remainder (+ a 1) 2))
418 (define-public (interval-widen iv amount)
419 (cons (- (car iv) amount)
420 (+ (cdr iv) amount)))
423 (define-public (interval-empty? iv)
424 (> (car iv) (cdr iv)))
426 (define-public (interval-union i1 i2)
427 (cons (min (car i1) (car i2))
428 (max (cdr i1) (cdr i2))))
430 (define-public (interval-sane? i)
431 (not (or (nan? (car i))
435 (> (car i) (cdr i)))))
438 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 (define-public (string-encode-integer i)
445 ((< i 0) (string-append "n" (string-encode-integer (- i))))
447 (make-string 1 (integer->char (+ 65 (modulo i 26))))
448 (string-encode-integer (quotient i 26))))))
450 (define-public (ly:numbers->string lst)
451 (string-join (map ly:number->string lst) " "))
453 (define (number->octal-string x)
454 (let* ((n (inexact->exact x))
455 (n64 (quotient n 64))
456 (n8 (quotient (- n (* n64 64)) 8)))
460 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
462 (define-public (ly:inexact->string x radix)
463 (let ((n (inexact->exact x)))
464 (number->string n radix)))
466 (define-public (ly:number-pair->string c)
467 (string-append (ly:number->string (car c)) " "
468 (ly:number->string (cdr c))))
471 (define-public (write-me message x)
472 "Return X. Display MESSAGE and write X. Handy for debugging,
473 possibly turned off."
474 (display message) (write x) (newline) x)
477 (define-public (stderr string . rest)
478 (apply format (cons (current-error-port) (cons string rest)))
479 (force-output (current-error-port)))
481 (define-public (debugf string . rest)
483 (apply stderr (cons string rest))))
485 (define (index-cell cell dir)
490 (define (cons-map f x)
491 "map F to contents of X"
492 (cons (f (car x)) (f (cdr x))))
494 (define-public (list-insert-separator lst between)
495 "Create new list, inserting BETWEEN between elements of LIST"
499 (cons x (cons between y))))
500 (fold-right conc #f lst))
502 (define-public (string-regexp-substitute a b str)
503 (regexp-substitute/global #f a str 'pre b 'post))
505 (define (regexp-split str regex)
507 (define end-of-prev-match 0)
508 (define (notice match)
510 (set! matches (cons (substring (match:string match)
514 (set! end-of-prev-match (match:end match)))
516 (regexp-substitute/global #f regex str notice 'post)
518 (if (< end-of-prev-match (string-length str))
521 (cons (substring str end-of-prev-match (string-length str)) matches)))
532 (define-public (car< a b) (< (car a) (car b)))
534 (define-public (symbol<? lst r)
535 (string<? (symbol->string lst) (symbol->string r)))
537 (define-public (symbol-key<? lst r)
538 (string<? (symbol->string (car lst)) (symbol->string (car r))))
541 ;; don't confuse users with #<procedure .. > syntax.
543 (define-public (scm->string val)
544 (if (and (procedure? val) (symbol? (procedure-name val)))
545 (symbol->string (procedure-name val))
547 (if (self-evaluating? val) "" "'")
548 (call-with-output-string (lambda (port) (display val port))))))
550 (define-public (!= lst r)
553 (define-public lily-unit->bigpoint-factor
555 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
556 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
557 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
559 (define-public lily-unit->mm-factor
560 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
562 ;;; FONT may be font smob, or pango font string...
563 (define-public (font-name-style font)
564 ;; FIXME: ughr, (ly:font-name) sometimes also has Style appended.
565 (let* ((font-name (ly:font-name font))
566 (full-name (if font-name font-name (ly:font-file-name font)))
567 (name-style (string-split full-name #\-)))
568 ;; FIXME: ughr, barf: feta-alphabet is actually emmentaler
569 (if (string-prefix? "feta-alphabet" full-name)
571 (substring full-name (string-length "feta-alphabet")))
572 (if (not (null? (cdr name-style)))
574 (append name-style '("Regular"))))))
576 (define-public (modified-font-metric-font-scaling font)
577 (let* ((designsize (ly:font-design-size font))
578 (magnification (* (ly:font-magnification font)))
579 (scaling (* magnification designsize)))
580 (debugf "scaling:~S\n" scaling)
581 (debugf "magnification:~S\n" magnification)
582 (debugf "design:~S\n" designsize)
585 (define-public (version-not-seen-message input-file-name)
591 (_ "no \\version statement found, please add~afor future compatibility")
592 (format #f "\n\n\\version ~s\n\n" (lilypond-version)))))
594 (define-public (old-relative-not-used-message input-file-name)
599 (_ "old relative compatibility not used")))