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@cs.uu.nl>
12 (define-safe-public START -1)
13 (define-safe-public STOP 1)
14 (define-public LEFT -1)
15 (define-public RIGHT 1)
17 (define-public DOWN -1)
18 (define-public CENTER 0)
20 (define-safe-public DOUBLE-FLAT -4)
21 (define-safe-public THREE-Q-FLAT -3)
22 (define-safe-public FLAT -2)
23 (define-safe-public SEMI-FLAT -1)
24 (define-safe-public NATURAL 0)
25 (define-safe-public SEMI-SHARP 1)
26 (define-safe-public SHARP 2)
27 (define-safe-public THREE-Q-SHARP 3)
28 (define-safe-public DOUBLE-SHARP 4)
29 (define-safe-public SEMI-TONE 2)
31 (define-public ZERO-MOMENT (ly:make-moment 0 1))
33 (define-public (moment-min a b)
34 (if (ly:moment<? a b) a b))
36 (define-public (average x . lst)
37 (/ (+ x (apply + lst)) (1+ (length lst))))
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 ;; lily specific variables.
42 (define-public default-script-alist '())
46 (define-public (print-music-as-book parser music)
47 (let* ((head (ly:parser-lookup parser '$defaultheader))
48 (book (ly:make-book (ly:parser-lookup parser '$defaultpaper)
49 head (scorify-music music parser))))
50 (print-book-with-defaults parser book)))
52 (define-public (print-score-as-book parser score)
53 (let* ((head (ly:parser-lookup parser '$defaultheader))
54 (book (ly:make-book (ly:parser-lookup parser '$defaultpaper)
56 (print-book-with-defaults parser book)))
58 (define-public (print-score parser score)
59 (let* ((head (ly:parser-lookup parser '$defaultheader))
60 (book (ly:make-book (ly:parser-lookup parser '$defaultpaper)
62 (ly:parser-print-score parser book)))
64 (define-public (collect-scores-for-book parser score)
66 parser 'toplevel-scores
67 (cons score (ly:parser-lookup parser 'toplevel-scores))))
70 (define-public (scorify-music music parser)
72 (for-each (lambda (func)
73 (set! music (func music parser)))
74 toplevel-music-functions)
76 (ly:make-score music))
78 (define-public (collect-music-for-book parser music)
79 ;; discard music if its 'void property is true.
80 (let ((void-music (ly:music-property music 'void)))
81 (if (or (null? void-music) (not void-music))
82 (collect-scores-for-book parser (scorify-music music parser)))))
85 (define-public (print-book-with-defaults parser book)
87 ((paper (ly:parser-lookup parser '$defaultpaper))
88 (layout (ly:parser-lookup parser '$defaultlayout))
89 (count (ly:parser-lookup parser 'output-count))
90 (base (ly:parser-output-name parser)))
92 (if (not (integer? count))
96 (set! base (format #f "~a-~a" base count)))
98 (ly:parser-define! parser 'output-count (1+ count))
99 (ly:book-process book paper layout base)
102 (define-public (print-score-with-defaults parser score)
104 ((paper (ly:parser-lookup parser '$defaultpaper))
105 (layout (ly:parser-lookup parser '$defaultlayout))
106 (header (ly:parser-lookup parser '$defaultheader))
107 (count (ly:parser-lookup parser 'output-count))
108 (base (ly:parser-output-name parser)))
110 (if (not (integer? count))
114 (set! base (format #f "~a-~a" base count)))
116 (ly:parser-define! parser 'output-count (1+ count))
117 (ly:score-process score header paper layout base)
123 (define-public assoc-get ly:assoc-get)
125 (define-public (uniqued-alist alist acc)
126 (if (null? alist) acc
127 (if (assoc (caar alist) acc)
128 (uniqued-alist (cdr alist) acc)
129 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
131 (define-public (alist<? x y)
132 (string<? (symbol->string (car x))
133 (symbol->string (car y))))
135 (define-public (chain-assoc x alist-list)
136 (if (null? alist-list)
138 (let* ((handle (assoc x (car alist-list))))
141 (chain-assoc x (cdr alist-list))))))
143 (define-public (chain-assoc-get x alist-list . default)
144 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
147 (define (helper x alist-list default)
148 (if (null? alist-list)
150 (let* ((handle (assoc x (car alist-list))))
153 (helper x (cdr alist-list) default)))))
156 (if (pair? default) (car default) #f)))
158 (define (map-alist-vals func list)
159 "map FUNC over the vals of LIST, leaving the keys."
162 (cons (cons (caar list) (func (cdar list)))
163 (map-alist-vals func (cdr list)))))
165 (define (map-alist-keys func list)
166 "map FUNC over the keys of an alist LIST, leaving the vals. "
169 (cons (cons (func (caar list)) (cdar list))
170 (map-alist-keys func (cdr list)))))
172 (define-public (first-member members lst)
173 "Return first successful MEMBER of member from MEMBERS in LST."
176 (let ((m (member (car members) lst)))
177 (if m m (first-member (cdr members) lst)))))
179 (define-public (first-assoc keys lst)
180 "Return first successful ASSOC of key from KEYS in LST."
183 (let ((k (assoc (car keys) lst)))
184 (if k k (first-assoc (cdr keys) lst)))))
186 (define-public (flatten-alist alist)
191 (flatten-alist (cdr alist))))))
195 (define-public (vector-for-each proc vec)
198 ((>= i (vector-length vec)) vec)
199 (vector-set! vec i (proc (vector-ref vec i)))))
204 (if (not (defined? 'hash-table?)) ;; guile 1.6 compat
206 (define hash-table? vector?)
207 (define-public (hash-for-each proc tab)
208 (hash-fold (lambda (k v prior)
213 (define-public (hash-table->alist t)
214 "Convert table t to list"
215 (apply append (vector->list t))))
219 (define-public (hash-table->alist t)
220 (hash-fold (lambda (k v acc) (acons k v acc))
223 ;; todo: code dup with C++.
224 (define-safe-public (alist->hash-table lst)
225 "Convert alist to table"
226 (let ((m (make-hash-table (length lst))))
227 (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
233 (define (flatten-list lst)
237 (if (pair? (car lst))
238 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
239 (cons (car lst) (flatten-list (cdr lst))))))
241 (define (list-minus a b)
242 "Return list of elements in A that are not in B."
243 (lset-difference eq? a b))
245 ;; TODO: use the srfi-1 partition function.
246 (define-public (uniq-list lst)
248 "Uniq LST, assuming that it is sorted"
249 (define (helper acc lst)
252 (if (null? (cdr lst))
254 (if (equal? (car lst) (cadr lst))
255 (helper acc (cdr lst))
256 (helper (cons (car lst) acc) (cdr lst))))))
257 (reverse! (helper '() lst) '()))
259 (define (split-at-predicate predicate lst)
260 "Split LST = (a_1 a_2 ... a_k b_1 ... b_k)
261 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
262 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
263 L1 is copied, L2 not.
265 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
268 (define (inner-split predicate lst acc)
272 (set-car! acc (cons (car lst) (car acc)))
274 ((predicate (car lst) (cadr lst))
275 (set-car! acc (cons (car lst) (car acc)))
276 (inner-split predicate (cdr lst) acc))
278 (set-car! acc (cons (car lst) (car acc)))
279 (set-cdr! acc (cdr lst))
282 (let* ((c (cons '() '())))
283 (inner-split predicate lst c)
284 (set-car! c (reverse! (car c)))
287 (define-public (split-list lst sep?)
288 "(display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))))
290 ((a b c) (d e f) (g))
293 (define (split-one sep? lst acc)
294 "Split off the first parts before separator and return both parts."
299 (split-one sep? (cdr lst) (cons (car lst) acc)))))
303 (let* ((c (split-one sep? lst '())))
304 (cons (reverse! (car c) '()) (split-list (cdr c) sep?)))))
306 (define-public (offset-add a b)
307 (cons (+ (car a) (car b))
308 (+ (cdr a) (cdr b))))
310 (define-public (offset-flip-y o)
311 (cons (car o) (- (cdr o))))
313 (define-public (offset-scale o scale)
314 (cons (* (car o) scale)
317 (define-public (ly:list->offsets accum coords)
320 (cons (cons (car coords) (cadr coords))
321 (ly:list->offsets accum (cddr coords)))))
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326 (if (not (defined? 'nan?)) ;; guile 1.6 compat
327 (define-public (nan? x) (not (or (< 0.0 x)
331 (if (not (defined? 'inf?))
332 (define-public (inf? x) (= (/ 1.0 x) 0.0)))
334 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337 (define-public (interval-length x)
338 "Length of the number-pair X, when an interval"
339 (max 0 (- (cdr x) (car x))))
341 (define-public interval-start car)
342 (define-public (ordered-cons a b)
346 (define-public interval-end cdr)
348 (define-public (interval-index interval dir)
349 "Interpolate INTERVAL between between left (DIR=-1) and right (DIR=+1)"
351 (* (+ (interval-start interval) (interval-end interval)
352 (* dir (- (interval-end interval) (interval-start interval))))
355 (define-public (interval-center x)
356 "Center the number-pair X, when an interval"
357 (if (interval-empty? x)
359 (/ (+ (car x) (cdr x)) 2)))
361 (define-public interval-start car)
362 (define-public interval-end cdr)
363 (define-public (interval-translate iv amount)
364 (cons (+ amount (car iv))
365 (+ amount (cdr iv))))
367 (define (other-axis a)
368 (remainder (+ a 1) 2))
370 (define-public (interval-widen iv amount)
371 (cons (- (car iv) amount)
372 (+ (cdr iv) amount)))
375 (define-public (interval-empty? iv)
376 (> (car iv) (cdr iv)))
378 (define-public (interval-union i1 i2)
379 (cons (min (car i1) (car i2))
380 (max (cdr i1) (cdr i2))))
382 (define-public (interval-sane? i)
383 (not (or (nan? (car i))
387 (> (car i) (cdr i)))))
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394 (define-public (write-me message x)
395 "Return X. Display MESSAGE and write X. Handy for debugging,
396 possibly turned off."
397 (display message) (write x) (newline) x)
400 (define-public (stderr string . rest)
401 (apply format (cons (current-error-port) (cons string rest)))
402 (force-output (current-error-port)))
404 (define-public (debugf string . rest)
406 (apply stderr (cons string rest))))
408 (define (index-cell cell dir)
413 (define (cons-map f x)
414 "map F to contents of X"
415 (cons (f (car x)) (f (cdr x))))
417 (define-public (list-insert-separator lst between)
418 "Create new list, inserting BETWEEN between elements of LIST"
422 (cons x (cons between y))))
423 (fold-right conc #f lst))
425 (define-public (string-regexp-substitute a b str)
426 (regexp-substitute/global #f a str 'pre b 'post))
428 (define (regexp-split str regex)
430 (define end-of-prev-match 0)
431 (define (notice match)
433 (set! matches (cons (substring (match:string match)
437 (set! end-of-prev-match (match:end match)))
439 (regexp-substitute/global #f regex str notice 'post)
441 (if (< end-of-prev-match (string-length str))
444 (cons (substring str end-of-prev-match (string-length str)) matches)))
455 (define-public (car< a b) (< (car a) (car b)))
458 (define-public (symbol<? lst r)
459 (string<? (symbol->string lst) (symbol->string r)))
461 (define-public (symbol-key<? lst r)
462 (string<? (symbol->string (car lst)) (symbol->string (car r))))
465 ;; don't confuse users with #<procedure .. > syntax.
467 (define-public (scm->string val)
468 (if (and (procedure? val) (symbol? (procedure-name val)))
469 (symbol->string (procedure-name val))
471 (if (self-evaluating? val) "" "'")
472 (call-with-output-string (lambda (port) (display val port))))))
474 (define-public (!= lst r)
477 (define-public lily-unit->bigpoint-factor
479 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
480 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
481 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
483 (define-public lily-unit->mm-factor
484 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
486 ;;; FONT may be font smob, or pango font string...
487 (define-public (font-name-style font)
488 ;; FIXME: ughr, (ly:font-name) sometimes also has Style appended.
489 (let* ((font-name (ly:font-name font))
490 (full-name (if font-name font-name (ly:font-file-name font)))
491 (name-style (string-split full-name #\-)))
492 ;; FIXME: ughr, barf: feta-alphabet is actually emmentaler
493 (if (string-prefix? "feta-alphabet" full-name)
495 (substring full-name (string-length "feta-alphabet")))
496 (if (not (null? (cdr name-style)))
498 (append name-style '("Regular"))))))
500 (define-public (modified-font-metric-font-scaling font)
501 (let* ((designsize (ly:font-design-size font))
502 (magnification (* (ly:font-magnification font)))
503 (scaling (* magnification designsize)))
504 (debugf "scaling:~S\n" scaling)
505 (debugf "magnification:~S\n" magnification)
506 (debugf "design:~S\n" designsize)
509 (define-public (version-not-seen-message input-file-name)
512 input-file-name ": 0: " (_ "warning: ")
514 (_ "no \\version statement found, please add~afor future compatibility")
515 (format #f "\n\n\\version ~s\n\n" (lilypond-version))))))
517 (define-public (old-relative-not-used-message input-file-name)
520 input-file-name ": 0: " (_ "warning: ")
521 (_ "old relative compatibility not used"))))