1 ;;;; lily-library.scm -- utilities
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 (define-safe-public START -1)
12 (define-safe-public STOP 1)
13 (define-public LEFT -1)
14 (define-public RIGHT 1)
16 (define-public DOWN -1)
17 (define-public CENTER 0)
19 (define-safe-public DOUBLE-FLAT -4)
20 (define-safe-public THREE-Q-FLAT -3)
21 (define-safe-public FLAT -2)
22 (define-safe-public SEMI-FLAT -1)
23 (define-safe-public NATURAL 0)
24 (define-safe-public SEMI-SHARP 1)
25 (define-safe-public SHARP 2)
26 (define-safe-public THREE-Q-SHARP 3)
27 (define-safe-public DOUBLE-SHARP 4)
28 (define-safe-public SEMI-TONE 2)
30 (define-public ZERO-MOMENT (ly:make-moment 0 1))
32 (define-public (moment-min a b)
33 (if (ly:moment<? a b) a b))
35 (define-public (average x . lst)
36 (/ (+ x (apply + lst)) (1+ (length lst))))
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;; lily specific variables.
41 (define-public default-script-alist '())
45 (define-public (print-music-as-book parser music)
46 (let* ((head (ly:parser-lookup parser '$globalheader))
47 (book (ly:make-book (ly:parser-lookup parser $defaultpaper)
49 (ly:parser-print-book parser book)))
51 (define-public (print-score-as-book parser score)
52 (let* ((head (ly:parser-lookup parser '$globalheader))
53 (book (ly:make-book (ly:parser-lookup parser $defaultpaper)
55 (ly:parser-print-book parser book)))
57 (define-public (print-score parser score)
58 (let* ((head (ly:parser-lookup parser '$globalheader))
59 (book (ly:make-book (ly:parser-lookup parser $defaultpaper)
61 (ly:parser-print-score parser book)))
63 (define-public (collect-scores-for-book parser score)
65 parser 'toplevel-scores
66 (cons score (ly:parser-lookup parser 'toplevel-scores))))
69 (define-public (scorify-music music parser)
71 (for-each (lambda (func)
72 (set! music (func music parser)))
73 toplevel-music-functions)
75 (ly:make-score music))
77 (define-public (collect-music-for-book parser music)
78 (collect-scores-for-book parser (scorify-music music parser)))
81 (define-public (print-book-with-defaults parser book)
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))
96 (ly:book-process book paper layout base)
99 (define-public (print-score-with-defaults parser score)
101 ((paper (ly:parser-lookup parser '$defaultpaper))
102 (layout (ly:parser-lookup parser '$defaultlayout))
103 (header (ly:parser-lookup parser '$globalheader))
104 (count (ly:parser-lookup parser 'output-count))
105 (base (ly:parser-output-name parser)))
107 (if (not (integer? count))
111 (set! (base (format #f "~a-~a" count))))
113 (ly:parser-define! parser 'output-count (1+ count))
116 (ly:score-process score header paper layout base)
121 (define-public assoc-get ly:assoc-get)
123 (define-public (uniqued-alist alist acc)
124 (if (null? alist) acc
125 (if (assoc (caar alist) acc)
126 (uniqued-alist (cdr alist) acc)
127 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
129 (define-public (alist<? x y)
130 (string<? (symbol->string (car x))
131 (symbol->string (car y))))
133 (define-public (chain-assoc x alist-list)
134 (if (null? alist-list)
136 (let* ((handle (assoc x (car alist-list))))
139 (chain-assoc x (cdr alist-list))))))
141 (define-public (chain-assoc-get x alist-list . default)
142 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
145 (define (helper x alist-list default)
146 (if (null? alist-list)
148 (let* ((handle (assoc x (car alist-list))))
151 (helper x (cdr alist-list) default)))))
154 (if (pair? default) (car default) #f)))
156 (define (map-alist-vals func list)
157 "map FUNC over the vals of LIST, leaving the keys."
160 (cons (cons (caar list) (func (cdar list)))
161 (map-alist-vals func (cdr list)))))
163 (define (map-alist-keys func list)
164 "map FUNC over the keys of an alist LIST, leaving the vals. "
167 (cons (cons (func (caar list)) (cdar list))
168 (map-alist-keys func (cdr list)))))
170 (define-public (first-member members lst)
171 "Return first successful MEMBER of member from MEMBERS in LST."
174 (let ((m (member (car members) lst)))
175 (if m m (first-member (cdr members) lst)))))
177 (define-public (first-assoc keys lst)
178 "Return first successful ASSOC of key from KEYS in LST."
181 (let ((k (assoc (car keys) lst)))
182 (if k k (first-assoc (cdr keys) lst)))))
184 (define-public (flatten-alist alist)
189 (flatten-alist (cdr alist))))))
193 (define-public (vector-for-each proc vec)
196 ((>= i (vector-length vec)) vec)
197 (vector-set! vec i (proc (vector-ref vec i)))))
202 (if (not (defined? 'hash-table?)) ;; guile 1.6 compat
204 (define hash-table? vector?)
206 (define-public (hash-table->alist t)
207 "Convert table t to list"
208 (apply append (vector->list t))))
212 (define-public (hash-table->alist t)
213 (hash-fold (lambda (k v acc) (acons k v acc))
216 ;; todo: code dup with C++.
217 (define-safe-public (alist->hash-table lst)
218 "Convert alist to table"
219 (let ((m (make-hash-table (length lst))))
220 (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
226 (define (flatten-list lst)
230 (if (pair? (car lst))
231 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
232 (cons (car lst) (flatten-list (cdr lst))))))
234 (define (list-minus a b)
235 "Return list of elements in A that are not in B."
236 (lset-difference eq? a b))
238 ;; TODO: use the srfi-1 partition function.
239 (define-public (uniq-list lst)
241 "Uniq LST, assuming that it is sorted"
242 (define (helper acc lst)
245 (if (null? (cdr lst))
247 (if (equal? (car lst) (cadr lst))
248 (helper acc (cdr lst))
249 (helper (cons (car lst) acc) (cdr lst))))))
250 (reverse! (helper '() lst) '()))
252 (define (split-at-predicate predicate lst)
253 "Split LST = (a_1 a_2 ... a_k b_1 ... b_k)
254 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
255 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
256 L1 is copied, L2 not.
258 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
261 (define (inner-split predicate lst acc)
265 (set-car! acc (cons (car lst) (car acc)))
267 ((predicate (car lst) (cadr lst))
268 (set-car! acc (cons (car lst) (car acc)))
269 (inner-split predicate (cdr lst) acc))
271 (set-car! acc (cons (car lst) (car acc)))
272 (set-cdr! acc (cdr lst))
275 (let* ((c (cons '() '())))
276 (inner-split predicate lst c)
277 (set-car! c (reverse! (car c)))
280 (define-public (split-list lst sep?)
281 "(display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))))
283 ((a b c) (d e f) (g))
286 (define (split-one sep? lst acc)
287 "Split off the first parts before separator and return both parts."
292 (split-one sep? (cdr lst) (cons (car lst) acc)))))
296 (let* ((c (split-one sep? lst '())))
297 (cons (reverse! (car c) '()) (split-list (cdr c) sep?)))))
299 (define-public (offset-add a b)
300 (cons (+ (car a) (car b))
301 (+ (cdr a) (cdr b))))
303 (define-public (offset-flip-y o)
304 (cons (car o) (- (cdr o))))
306 (define-public (ly:list->offsets accum coords)
309 (cons (cons (car coords) (cadr coords))
310 (ly:list->offsets accum (cddr coords)))))
312 (define-public (interval-length x)
313 "Length of the number-pair X, when an interval"
314 (max 0 (- (cdr x) (car x))))
316 (define-public interval-start car)
317 (define-public interval-end cdr)
319 (define (other-axis a)
320 (remainder (+ a 1) 2))
322 (define-public (interval-widen iv amount)
323 (cons (- (car iv) amount)
324 (+ (cdr iv) amount)))
326 (define-public (interval-union i1 i2)
327 (cons (min (car i1) (car i2))
328 (max (cdr i1) (cdr i2))))
330 (define-public (write-me message x)
331 "Return X. Display MESSAGE and write X. Handy for debugging,
332 possibly turned off."
333 (display message) (write x) (newline) x)
336 (define-public (stderr string . rest)
337 (apply format (cons (current-error-port) (cons string rest)))
338 (force-output (current-error-port)))
340 (define-public (debugf string . rest)
342 (apply stderr (cons string rest))))
344 (define (index-cell cell dir)
349 (define (cons-map f x)
350 "map F to contents of X"
351 (cons (f (car x)) (f (cdr x))))
353 (define-public (list-insert-separator lst between)
354 "Create new list, inserting BETWEEN between elements of LIST"
358 (cons x (cons between y))))
359 (fold-right conc #f lst))
361 (define-public (string-regexp-substitute a b str)
362 (regexp-substitute/global #f a str 'pre b 'post))
365 (define (regexp-split str regex)
367 (define end-of-prev-match 0)
368 (define (notice match)
369 (set! matches (cons (substring (match:string match)
373 (set! end-of-prev-match (match:end match)))
375 (regexp-substitute/global #f regex str notice 'post)
377 (if (< end-of-prev-match (string-length str))
380 (cons (substring str end-of-prev-match (string-length str)) matches)))
391 (define-public (symbol<? lst r)
392 (string<? (symbol->string lst) (symbol->string r)))
395 ;; don't confuse users with #<procedure .. > syntax.
397 (define-public (scm->string val)
398 (if (and (procedure? val) (symbol? (procedure-name val)))
399 (symbol->string (procedure-name val))
401 (if (self-evaluating? val) "" "'")
402 (call-with-output-string (lambda (port) (display val port))))))
404 (define-public (!= lst r)
407 (define-public lily-unit->bigpoint-factor
409 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
410 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
411 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
413 (define-public lily-unit->mm-factor
414 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
416 ;;; FONT may be font smob, or pango font string...
417 (define-public (font-name-style font)
418 ;; FIXME: ughr, (ly:font-name) sometimes also has Style appended.
419 (let* ((font-name (ly:font-name font))
420 (full-name (if font-name font-name (ly:font-file-name font)))
421 (name-style (string-split full-name #\-)))
422 ;; FIXME: ughr, barf: feta-alphabet is actually emmentaler
423 (if (string-prefix? "feta-alphabet" full-name)
425 (substring full-name (string-length "feta-alphabet")))
426 (if (not (null? (cdr name-style)))
428 (append name-style '("Regular"))))))
430 (define-public (modified-font-metric-font-scaling font)
431 (let* ((designsize (ly:font-design-size font))
432 (magnification (* (ly:font-magnification font)))
433 (scaling (* magnification designsize)))
434 (debugf "scaling:~S\n" scaling)
435 (debugf "magnification:~S\n" magnification)
436 (debugf "design:~S\n" designsize)
439 (define-public (version-not-seen-message input-file-name)
442 input-file-name ": 0: " (_ "warning: ")
444 (_ "no \\version statement found, add~afor future compatibility")
445 (format #f "\n\n\\version ~s\n\n" (lilypond-version))))))
447 (define-public (old-relative-not-used-message input-file-name)
450 input-file-name ": 0: " (_ "warning: ")
451 (_ "old relative compatibility not used"))))