2 ;;;; lily-library.scm -- utilities
4 ;;;; source file of the GNU LilyPond music typesetter
6 ;;;; (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
9 ; for take, drop, take-while, list-index, and find-tail:
10 (use-modules (srfi srfi-1))
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17 (define-safe-public START -1)
18 (define-safe-public STOP 1)
19 (define-public LEFT -1)
20 (define-public RIGHT 1)
22 (define-public DOWN -1)
23 (define-public CENTER 0)
25 (define-safe-public DOUBLE-FLAT-QTS -4)
26 (define-safe-public THREE-Q-FLAT-QTS -3)
27 (define-safe-public FLAT-QTS -2)
28 (define-safe-public SEMI-FLAT-QTS -1)
29 (define-safe-public NATURAL-QTS 0)
30 (define-safe-public SEMI-SHARP-QTS 1)
31 (define-safe-public SHARP-QTS 2)
32 (define-safe-public THREE-Q-SHARP-QTS 3)
33 (define-safe-public DOUBLE-SHARP-QTS 4)
34 (define-safe-public SEMI-TONE-QTS 2)
36 (define-safe-public DOUBLE-FLAT -1)
37 (define-safe-public THREE-Q-FLAT -3/4)
38 (define-safe-public FLAT -1/2)
39 (define-safe-public SEMI-FLAT -1/4)
40 (define-safe-public NATURAL 0)
41 (define-safe-public SEMI-SHARP 1/4)
42 (define-safe-public SHARP 1/2)
43 (define-safe-public THREE-Q-SHARP 3/4)
44 (define-safe-public DOUBLE-SHARP 1)
45 (define-safe-public SEMI-TONE 1/2)
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 (define-public ZERO-MOMENT (ly:make-moment 0 1))
52 (define-public (moment-min a b)
53 (if (ly:moment<? a b) a b))
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57 (define-public (average x . lst)
58 (/ (+ x (apply + lst)) (1+ (length lst))))
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;; parser <-> output hooks.
63 (define-public (collect-bookpart-for-book parser book-part)
64 "Toplevel book-part handler"
65 (define (add-bookpart book-part)
67 parser 'toplevel-bookparts
68 (cons book-part (ly:parser-lookup parser 'toplevel-bookparts))))
69 ;; If toplevel scores have been found before this \bookpart,
70 ;; add them first to a dedicated bookpart
71 (if (pair? (ly:parser-lookup parser 'toplevel-scores))
73 (add-bookpart (ly:make-book-part
74 (ly:parser-lookup parser 'toplevel-scores)))
75 (ly:parser-define! parser 'toplevel-scores (list))))
76 (add-bookpart book-part))
78 (define-public (collect-scores-for-book parser score)
80 parser 'toplevel-scores
81 (cons score (ly:parser-lookup parser 'toplevel-scores))))
83 (define-public (collect-music-aux score-handler parser music)
84 (define (music-property symbol)
85 (let ((value (ly:music-property music symbol)))
86 (if (not (null? value))
89 (cond ((music-property 'page-marker)
90 ;; a page marker: set page break/turn permissions or label
92 (let ((label (music-property 'page-label)))
94 (score-handler (ly:make-page-label-marker label))))
95 (for-each (lambda (symbol)
96 (let ((permission (music-property symbol)))
97 (if (symbol? permission)
99 (ly:make-page-permission-marker symbol
100 (if (eqv? 'forbid permission)
103 (list 'line-break-permission 'page-break-permission
104 'page-turn-permission))))
105 ((not (music-property 'void))
106 ;; a regular music expression: make a score with this music
107 ;; void music is discarded
108 (score-handler (scorify-music music parser)))))
110 (define-public (collect-music-for-book parser music)
111 "Top-level music handler"
112 (collect-music-aux (lambda (score)
113 (collect-scores-for-book parser score))
117 (define-public (collect-book-music-for-book parser book music)
119 (collect-music-aux (lambda (score)
120 (ly:book-add-score! book score))
124 (define-public (scorify-music music parser)
127 (for-each (lambda (func)
128 (set! music (func music parser)))
129 toplevel-music-functions)
131 (ly:make-score music))
133 (define (print-book-with parser book process-procedure)
135 ((paper (ly:parser-lookup parser '$defaultpaper))
136 (layout (ly:parser-lookup parser '$defaultlayout))
137 (count (ly:parser-lookup parser 'output-count))
138 (base (ly:parser-output-name parser))
139 (output-suffix (ly:parser-lookup parser 'output-suffix)) )
141 (if (string? output-suffix)
142 (set! base (format "~a-~a" base (string-regexp-substitute
143 "[^a-zA-Z0-9-]" "_" output-suffix))))
145 ;; must be careful: output-count is under user control.
146 (if (not (integer? count))
150 (set! base (format #f "~a-~a" base count)))
151 (ly:parser-define! parser 'output-count (1+ count))
152 (process-procedure book paper layout base)
155 (define-public (print-book-with-defaults parser book)
156 (print-book-with parser book ly:book-process))
158 (define-public (print-book-with-defaults-as-systems parser book)
159 (print-book-with parser book ly:book-process-to-systems))
164 (define-public assoc-get ly:assoc-get)
166 (define-public (uniqued-alist alist acc)
167 (if (null? alist) acc
168 (if (assoc (caar alist) acc)
169 (uniqued-alist (cdr alist) acc)
170 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
172 (define-public (alist<? x y)
173 (string<? (symbol->string (car x))
174 (symbol->string (car y))))
176 (define-public (chain-assoc-get x alist-list . default)
177 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
180 (define (helper x alist-list default)
181 (if (null? alist-list)
183 (let* ((handle (assoc x (car alist-list))))
186 (helper x (cdr alist-list) default)))))
189 (if (pair? default) (car default) #f)))
191 (define (map-alist-vals func list)
192 "map FUNC over the vals of LIST, leaving the keys."
195 (cons (cons (caar list) (func (cdar list)))
196 (map-alist-vals func (cdr list)))))
198 (define (map-alist-keys func list)
199 "map FUNC over the keys of an alist LIST, leaving the vals. "
202 (cons (cons (func (caar list)) (cdar list))
203 (map-alist-keys func (cdr list)))))
205 (define-public (first-member members lst)
206 "Return first successful MEMBER of member from MEMBERS in LST."
209 (let ((m (member (car members) lst)))
210 (if m m (first-member (cdr members) lst)))))
212 (define-public (first-assoc keys lst)
213 "Return first successful ASSOC of key from KEYS in LST."
216 (let ((k (assoc (car keys) lst)))
217 (if k k (first-assoc (cdr keys) lst)))))
219 (define-public (flatten-alist alist)
224 (flatten-alist (cdr alist))))))
229 (define-public (vector-for-each proc vec)
232 ((>= i (vector-length vec)) vec)
233 (vector-set! vec i (proc (vector-ref vec i)))))
238 (define-public (hash-table->alist t)
239 (hash-fold (lambda (k v acc) (acons k v acc))
242 ;; todo: code dup with C++.
243 (define-safe-public (alist->hash-table lst)
244 "Convert alist to table"
245 (let ((m (make-hash-table (length lst))))
246 (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
252 (define (functional-or . rest)
255 (apply functional-and (cdr rest)))
258 (define (functional-and . rest)
261 (apply functional-and (cdr rest)))
264 (define (split-list lst n)
265 "Split LST in N equal sized parts"
267 (define (helper todo acc-vector k)
274 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
275 (helper (cdr todo) acc-vector (1- k)))))
277 (helper lst (make-vector n '()) (1- n)))
279 (define (list-element-index lst x)
280 (define (helper todo k)
283 ((equal? (car todo) x) k)
285 (helper (cdr todo) (1+ k)))))
289 (define-public (count-list lst)
290 "Given lst (E1 E2 .. ) return ((E1 . 1) (E2 . 2) ... ) "
292 (define (helper l acc count)
294 (helper (cdr l) (cons (cons (car l) count) acc) (1+ count))
298 (reverse (helper lst '() 1)))
300 (define-public (list-join lst intermediate)
301 "put INTERMEDIATE between all elts of LST."
306 (cons elem (cons intermediate prev))
310 (define-public (filtered-map proc lst)
316 (define (flatten-list lst)
320 (if (pair? (car lst))
321 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
322 (cons (car lst) (flatten-list (cdr lst))))))
324 (define (list-minus a b)
325 "Return list of elements in A that are not in B."
326 (lset-difference eq? a b))
328 (define-public (uniq-list lst)
329 "Uniq LST, assuming that it is sorted. Uses equal? for comparisons."
332 (fold (lambda (x acc)
335 (if (equal? x (car acc))
340 (define (split-at-predicate pred lst)
341 "Split LST into two lists at the first element that returns #f for
342 (PRED previous_element element). Return the two parts as a pair.
343 Example: (split-at-predicate < '(1 2 3 2 1)) ==> ((1 2 3) . (2 1))"
346 (let ((i (list-index pred (cdr lst) lst)))
348 (cons (take lst (1+ i)) (drop lst (1+ i)))
351 (define-public (split-list-by-separator lst pred)
352 "Split LST at each element that satisfies PRED, and return the parts
353 (with the separators removed) as a list of lists. Example:
354 (split-list-by-separator '(a 0 b c 1 d) number?) ==> ((a) (b c) (d))"
355 (let loop ((result '()) (lst lst))
356 (if (and lst (not (null? lst)))
359 (list (take-while (lambda (x) (not (pred x))) lst)))
360 (let ((tail (find-tail pred lst)))
361 (if tail (cdr tail) #f)))
364 (define-public (offset-add a b)
365 (cons (+ (car a) (car b))
366 (+ (cdr a) (cdr b))))
368 (define-public (offset-flip-y o)
369 (cons (car o) (- (cdr o))))
371 (define-public (offset-scale o scale)
372 (cons (* (car o) scale)
375 (define-public (ly:list->offsets accum coords)
378 (cons (cons (car coords) (cadr coords))
379 (ly:list->offsets accum (cddr coords)))))
381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
384 (if (not (defined? 'nan?)) ;; guile 1.6 compat
385 (define-public (nan? x) (not (or (< 0.0 x)
389 (if (not (defined? 'inf?))
390 (define-public (inf? x) (= (/ 1.0 x) 0.0)))
392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
395 (define-public (interval-length x)
396 "Length of the number-pair X, when an interval"
397 (max 0 (- (cdr x) (car x))))
399 (define-public interval-start car)
400 (define-public (ordered-cons a b)
404 (define-public interval-end cdr)
406 (define-public (interval-bound interval dir)
407 ((if (= dir RIGHT) cdr car) interval))
409 (define-public (interval-index interval dir)
410 "Interpolate INTERVAL between between left (DIR=-1) and right (DIR=+1)"
412 (* (+ (interval-start interval) (interval-end interval)
413 (* dir (- (interval-end interval) (interval-start interval))))
416 (define-public (interval-center x)
417 "Center the number-pair X, when an interval"
418 (if (interval-empty? x)
420 (/ (+ (car x) (cdr x)) 2)))
422 (define-public interval-start car)
423 (define-public interval-end cdr)
424 (define-public (interval-translate iv amount)
425 (cons (+ amount (car iv))
426 (+ amount (cdr iv))))
428 (define (other-axis a)
429 (remainder (+ a 1) 2))
431 (define-public (interval-widen iv amount)
432 (cons (- (car iv) amount)
433 (+ (cdr iv) amount)))
436 (define-public (interval-empty? iv)
437 (> (car iv) (cdr iv)))
439 (define-public (interval-union i1 i2)
440 (cons (min (car i1) (car i2))
441 (max (cdr i1) (cdr i2))))
443 (define-public (interval-sane? i)
444 (not (or (nan? (car i))
448 (> (car i) (cdr i)))))
451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454 (define-public (string-endswith s suffix)
455 (equal? suffix (substring s
456 (max 0 (- (string-length s) (string-length suffix)))
459 (define-public (string-startswith s prefix)
460 (equal? prefix (substring s 0 (min (string-length s) (string-length prefix)))))
462 (define-public (string-encode-integer i)
465 ((< i 0) (string-append "n" (string-encode-integer (- i))))
467 (make-string 1 (integer->char (+ 65 (modulo i 26))))
468 (string-encode-integer (quotient i 26))))))
470 (define (number->octal-string x)
471 (let* ((n (inexact->exact x))
472 (n64 (quotient n 64))
473 (n8 (quotient (- n (* n64 64)) 8)))
477 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
479 (define-public (ly:inexact->string x radix)
480 (let ((n (inexact->exact x)))
481 (number->string n radix)))
483 (define-public (ly:number-pair->string c)
484 (string-append (ly:number->string (car c)) " "
485 (ly:number->string (cdr c))))
487 (define-public (dir-basename file . rest)
488 "Strip suffixes in REST, but leave directory component for FILE."
489 (define (inverse-basename x y) (basename y x))
490 (simple-format #f "~a/~a" (dirname file)
491 (fold inverse-basename file rest)))
493 (define-public (write-me message x)
494 "Return X. Display MESSAGE and write X. Handy for debugging,
495 possibly turned off."
496 (display message) (write x) (newline) x)
499 (define-public (stderr string . rest)
500 (apply format (cons (current-error-port) (cons string rest)))
501 (force-output (current-error-port)))
503 (define-public (debugf string . rest)
505 (apply stderr (cons string rest))))
507 (define (index-cell cell dir)
512 (define (cons-map f x)
513 "map F to contents of X"
514 (cons (f (car x)) (f (cdr x))))
516 (define-public (list-insert-separator lst between)
517 "Create new list, inserting BETWEEN between elements of LIST"
521 (cons x (cons between y))))
522 (fold-right conc #f lst))
524 (define-public (string-regexp-substitute a b str)
525 (regexp-substitute/global #f a str 'pre b 'post))
527 (define (regexp-split str regex)
529 (define end-of-prev-match 0)
530 (define (notice match)
532 (set! matches (cons (substring (match:string match)
536 (set! end-of-prev-match (match:end match)))
538 (regexp-substitute/global #f regex str notice 'post)
540 (if (< end-of-prev-match (string-length str))
543 (cons (substring str end-of-prev-match (string-length str)) matches)))
555 (define-public (binary-search start end getter target-val)
556 (_i "Find the index between @var{start} and @var{end} (an integer)
557 which will produce the closest match to @var{target-val} when
558 applied to function @var{getter}.")
561 (let* ((compare (quotient (+ start end) 2))
562 (get-val (getter compare)))
564 ((< target-val get-val)
565 (set! end (1- compare)))
566 ((< get-val target-val)
567 (set! start (1+ compare))))
568 (binary-search start end getter target-val))))
570 (define-public (car< a b)
573 (define-public (symbol<? lst r)
574 (string<? (symbol->string lst) (symbol->string r)))
576 (define-public (symbol-key<? lst r)
577 (string<? (symbol->string (car lst)) (symbol->string (car r))))
580 ;; don't confuse users with #<procedure .. > syntax.
582 (define-public (scm->string val)
583 (if (and (procedure? val)
584 (symbol? (procedure-name val)))
585 (symbol->string (procedure-name val))
587 (if (self-evaluating? val)
592 (call-with-output-string (lambda (port) (display val port)))
597 (define-public (!= lst r)
600 (define-public lily-unit->bigpoint-factor
602 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
603 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
604 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
606 (define-public lily-unit->mm-factor
607 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
609 ;;; FONT may be font smob, or pango font string...
610 (define-public (font-name-style font)
611 ;; FIXME: ughr, barf: feta-alphabet is actually emmentaler
612 (if (and (string? font)
613 (string-prefix? "feta-alphabet" font))
614 (string-append "emmentaler"
617 (string-length "feta-alphabet")
618 (string-length font)))
619 (let* ((font-name (ly:font-name font))
620 (full-name (if font-name font-name (ly:font-file-name font))))
621 (if (string-prefix? "Aybabtu" full-name)
623 (string-downcase full-name)))))
625 (define-public (modified-font-metric-font-scaling font)
626 (let* ((designsize (ly:font-design-size font))
627 (magnification (* (ly:font-magnification font)))
628 (scaling (* magnification designsize)))
629 (debugf "scaling:~S\n" scaling)
630 (debugf "magnification:~S\n" magnification)
631 (debugf "design:~S\n" designsize)
634 (define-public (version-not-seen-message input-file-name)
640 (_ "no \\version statement found, please add~afor future compatibility")
641 (format #f "\n\n\\version ~s\n\n" (lilypond-version)))))
643 (define-public (old-relative-not-used-message input-file-name)
648 (_ "old relative compatibility not used")))