]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily-library.scm
Make context-defs-from-music use ly:output-find-context-def
[lilypond.git] / scm / lily-library.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19 ; for take, drop, take-while, list-index, and find-tail:
20 (use-modules (srfi srfi-1))
21
22 ; for define-safe-public when byte-compiling using Guile V2
23 (use-modules (scm safe-utility-defs))
24
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;; constants.
27
28 (define-public X 0)
29 (define-public Y 1)
30 (define-safe-public START -1)
31 (define-safe-public STOP 1)
32 (define-public LEFT -1)
33 (define-public RIGHT 1)
34 (define-public UP 1)
35 (define-public DOWN -1)
36 (define-public CENTER 0)
37
38 (define-safe-public DOUBLE-FLAT-QTS -4)
39 (define-safe-public THREE-Q-FLAT-QTS -3)
40 (define-safe-public FLAT-QTS -2)
41 (define-safe-public SEMI-FLAT-QTS -1)
42 (define-safe-public NATURAL-QTS 0)
43 (define-safe-public SEMI-SHARP-QTS 1)
44 (define-safe-public SHARP-QTS 2)
45 (define-safe-public THREE-Q-SHARP-QTS 3)
46 (define-safe-public DOUBLE-SHARP-QTS 4)
47 (define-safe-public SEMI-TONE-QTS 2)
48
49 (define-safe-public DOUBLE-FLAT  -1)
50 (define-safe-public THREE-Q-FLAT -3/4)
51 (define-safe-public FLAT -1/2)
52 (define-safe-public SEMI-FLAT -1/4)
53 (define-safe-public NATURAL 0)
54 (define-safe-public SEMI-SHARP 1/4)
55 (define-safe-public SHARP 1/2)
56 (define-safe-public THREE-Q-SHARP 3/4)
57 (define-safe-public DOUBLE-SHARP 1)
58 (define-safe-public SEMI-TONE 1/2)
59
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;; moments
62
63 (define-public ZERO-MOMENT (ly:make-moment 0 1))
64
65 (define-public (moment-min a b)
66   (if (ly:moment<? a b) a b))
67
68 (define-public (moment<=? a b)
69   (or (equal? a b)
70       (ly:moment<? a b)))
71
72 (define-public (fraction->moment fraction)
73   (if (null? fraction)
74       ZERO-MOMENT
75       (ly:make-moment (car fraction) (cdr fraction))))
76
77 (define-public (moment->fraction moment)
78   (cons (ly:moment-main-numerator moment)
79         (ly:moment-main-denominator moment)))
80
81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82 ;; arithmetic
83 (define-public (average x . lst)
84   (/ (+ x (apply + lst)) (1+ (length lst))))
85
86 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87 ;; parser <-> output hooks.
88
89 (define-public (collect-bookpart-for-book parser book-part)
90   "Toplevel book-part handler."
91   (define (add-bookpart book-part)
92     (ly:parser-define!
93        parser 'toplevel-bookparts
94        (cons book-part (ly:parser-lookup parser 'toplevel-bookparts))))
95   ;; If toplevel scores have been found before this \bookpart,
96   ;; add them first to a dedicated bookpart
97   (if (pair? (ly:parser-lookup parser 'toplevel-scores))
98       (begin
99         (add-bookpart (ly:make-book-part
100                        (ly:parser-lookup parser 'toplevel-scores)))
101         (ly:parser-define! parser 'toplevel-scores (list))))
102   (add-bookpart book-part))
103
104 (define-public (collect-scores-for-book parser score)
105   (ly:parser-define!
106    parser 'toplevel-scores
107    (cons score (ly:parser-lookup parser 'toplevel-scores))))
108
109 (define-public (collect-music-aux score-handler parser music)
110   (define (music-property symbol)
111     (let ((value (ly:music-property music symbol)))
112       (if (not (null? value))
113           value
114           #f)))
115   (cond ((music-property 'page-marker)
116          ;; a page marker: set page break/turn permissions or label
117          (begin
118            (let ((label (music-property 'page-label)))
119              (if (symbol? label)
120                  (score-handler (ly:make-page-label-marker label))))
121            (for-each (lambda (symbol)
122                        (let ((permission (music-property symbol)))
123                          (if (symbol? permission)
124                              (score-handler
125                               (ly:make-page-permission-marker symbol
126                                                               (if (eqv? 'forbid permission)
127                                                                   '()
128                                                                   permission))))))
129                      (list 'line-break-permission 'page-break-permission
130                            'page-turn-permission))))
131         ((not (music-property 'void))
132          ;; a regular music expression: make a score with this music
133          ;; void music is discarded
134          (score-handler (scorify-music music parser)))))
135
136 (define-public (collect-music-for-book parser music)
137   "Top-level music handler."
138   (collect-music-aux (lambda (score)
139                        (collect-scores-for-book parser score))
140                      parser
141                      music))
142
143 (define-public (collect-book-music-for-book parser book music)
144   "Book music handler."
145   (collect-music-aux (lambda (score)
146                        (ly:book-add-score! book score))
147                      parser
148                      music))
149
150 (define-public (scorify-music music parser)
151   "Preprocess @var{music}."
152
153   (for-each (lambda (func)
154               (set! music (func music parser)))
155             toplevel-music-functions)
156
157   (ly:make-score music))
158
159
160 (define (get-current-filename parser book)
161   "return any suffix value for output filename allowing for settings by
162 calls to bookOutputName function"
163   (let ((book-filename (paper-variable parser book 'output-filename)))
164     (if (not book-filename)
165         (ly:parser-output-name parser)
166         book-filename)))
167
168 (define (get-current-suffix parser book)
169   "return any suffix value for output filename allowing for settings by calls to
170 bookoutput function"
171   (let ((book-output-suffix (paper-variable parser book 'output-suffix)))
172     (if (not (string? book-output-suffix))
173         (ly:parser-lookup parser 'output-suffix)
174         book-output-suffix)))
175
176 (define-public current-outfile-name #f)  ; for use by regression tests
177
178 (define (get-outfile-name parser book)
179   "return current filename for generating backend output files"
180   ;; user can now override the base file name, so we have to use
181   ;; the file-name concatenated with any potential output-suffix value
182   ;; as the key to out internal a-list
183   (let* ((base-name (get-current-filename parser book))
184          (output-suffix (get-current-suffix parser book))
185          (alist-key (format #f "~a~a" base-name output-suffix))
186          (counter-alist (ly:parser-lookup parser 'counter-alist))
187          (output-count (assoc-get alist-key counter-alist 0))
188          (result base-name))
189     ;; Allow all ASCII alphanumerics, including accents
190     (if (string? output-suffix)
191         (set! result
192               (format #f "~a-~a"
193                       result
194                       (string-regexp-substitute
195                        "[^-[:alnum:]]"
196                        "_"
197                        output-suffix))))
198
199     ;; assoc-get call will always have returned a number
200     (if (> output-count 0)
201         (set! result (format #f "~a-~a" result output-count)))
202
203     (ly:parser-define!
204      parser 'counter-alist
205      (assoc-set! counter-alist alist-key (1+ output-count)))
206     (set! current-outfile-name result)
207     result))
208
209 (define (print-book-with parser book process-procedure)
210   (let* ((paper (ly:parser-lookup parser '$defaultpaper))
211          (layout (ly:parser-lookup parser '$defaultlayout))
212          (outfile-name (get-outfile-name parser book)))
213     (process-procedure book paper layout outfile-name)))
214
215 (define-public (print-book-with-defaults parser book)
216   (print-book-with parser book ly:book-process))
217
218 (define-public (print-book-with-defaults-as-systems parser book)
219   (print-book-with parser book ly:book-process-to-systems))
220
221 ;; Add a score to the current bookpart, book or toplevel
222 (define-public (add-score parser score)
223     (cond
224       ((ly:parser-lookup parser '$current-bookpart)
225           ((ly:parser-lookup parser 'bookpart-score-handler)
226                 (ly:parser-lookup parser '$current-bookpart) score))
227       ((ly:parser-lookup parser '$current-book)
228           ((ly:parser-lookup parser 'book-score-handler)
229                 (ly:parser-lookup parser '$current-book) score))
230       (else
231           ((ly:parser-lookup parser 'toplevel-score-handler) parser score))))
232
233 (define-public paper-variable
234   (let
235       ((get-papers
236         (lambda (parser book)
237           (append (if (and book (ly:output-def? (ly:book-paper book)))
238                       (list (ly:book-paper book))
239                       '())
240                   (ly:parser-lookup parser '$papers)
241                   (list (ly:parser-lookup parser '$defaultpaper))))))
242     (make-procedure-with-setter
243      (lambda (parser book symbol)
244        (any (lambda (p) (ly:output-def-lookup p symbol #f))
245             (get-papers parser book)))
246      (lambda (parser book symbol value)
247        (ly:output-def-set-variable!
248         (car (get-papers parser book))
249         symbol value)))))
250
251 (define-public (add-text parser text)
252   (add-score parser (list text)))
253
254 (define-public (add-music parser music)
255   (collect-music-aux (lambda (score)
256                        (add-score parser score))
257                      parser
258                      music))
259
260 (define-public (context-mod-from-music parser music)
261   (let ((warn #t) (mods (ly:make-context-mod)))
262     (let loop ((m music) (context #f))
263       (if (music-is-of-type? m 'layout-instruction-event)
264           (let ((symbol (cons context (ly:music-property m 'symbol))))
265             (ly:add-context-mod
266              mods
267              (case (ly:music-property m 'name)
268                ((PropertySet)
269                 (list 'assign
270                       symbol
271                       (ly:music-property m 'value)))
272                ((PropertyUnset)
273                 (list 'unset symbol))
274                ((OverrideProperty)
275                 (cons* 'push
276                        symbol
277                        (ly:music-property m 'grob-value)
278                        (ly:music-property m 'grob-property-path)))
279                ((RevertProperty)
280                 (cons* 'pop
281                        symbol
282                        (ly:music-property m 'grob-property-path))))))
283           (case (ly:music-property m 'name)
284             ((ApplyContext)
285              (ly:add-context-mod mods
286                                  (list 'apply
287                                        (ly:music-property m 'procedure))))
288             ((ContextSpeccedMusic)
289              (loop (ly:music-property m 'element)
290                    (ly:music-property m 'context-type)))
291             (else
292              (let ((callback (ly:music-property m 'elements-callback)))
293                (if (procedure? callback)
294                    (fold loop context (callback m))
295                    (if (and warn (ly:duration? (ly:music-property m 'duration)))
296                        (begin
297                          (ly:music-warning
298                           music
299                           (_ "Music unsuitable for context-mod"))
300                          (set! warn #f))))))))
301       context)
302     mods))
303
304 (define-public (context-defs-from-music parser output-def music)
305   (let ((warn #t))
306     (let loop ((m music) (mods #f))
307       ;; The parser turns all sets, overrides etc into something
308       ;; wrapped in ContextSpeccedMusic.  If we ever get a set,
309       ;; override etc that is not wrapped in ContextSpeccedMusic, the
310       ;; user has created it in Scheme himself without providing the
311       ;; required wrapping.  In that case, using #f in the place of a
312       ;; context modification results in a reasonably recognizable
313       ;; error.
314       (if (music-is-of-type? m 'layout-instruction-event)
315           (ly:add-context-mod
316            mods
317            (case (ly:music-property m 'name)
318              ((PropertySet)
319               (list 'assign
320                     (ly:music-property m 'symbol)
321                     (ly:music-property m 'value)))
322              ((PropertyUnset)
323               (list 'unset
324                     (ly:music-property m 'symbol)))
325              ((OverrideProperty)
326               (cons* 'push
327                      (ly:music-property m 'symbol)
328                      (ly:music-property m 'grob-value)
329                      (ly:music-property m 'grob-property-path)))
330              ((RevertProperty)
331               (cons* 'pop
332                      (ly:music-property m 'symbol)
333                      (ly:music-property m 'grob-property-path)))))
334           (case (ly:music-property m 'name)
335             ((ApplyContext)
336              (ly:add-context-mod mods
337                                  (list 'apply
338                                        (ly:music-property m 'procedure))))
339             ((ContextSpeccedMusic)
340              ;; Use let* here to let defs catch up with modifications
341              ;; to the context defs made in the recursion
342              (let* ((mods (loop (ly:music-property m 'element)
343                                 (ly:make-context-mod)))
344                     (defs (ly:output-find-context-def
345                            output-def (ly:music-property m 'context-type))))
346                (if (null? defs)
347                    (ly:music-warning
348                     music
349                     (ly:format (_ "Cannot find context-def \\~a")
350                                (ly:music-property m 'context-type)))
351                    (for-each
352                     (lambda (entry)
353                       (ly:output-def-set-variable!
354                        output-def (car entry)
355                        (ly:context-def-modify (cdr entry) mods)))
356                     defs))))
357             (else
358              (let ((callback (ly:music-property m 'elements-callback)))
359                (if (procedure? callback)
360                    (fold loop mods (callback m))
361                    (if (and warn (ly:duration? (ly:music-property m 'duration)))
362                        (begin
363                          (ly:music-warning
364                           music
365                           (_ "Music unsuitable for output-def"))
366                          (set! warn #f))))))))
367       mods)))
368
369
370 ;;;;;;;;;;;;;;;;
371 ;; alist
372
373 (define-public assoc-get ly:assoc-get)
374
375 (define-public chain-assoc-get ly:chain-assoc-get)
376
377 (define-public (uniqued-alist alist acc)
378   (if (null? alist) acc
379       (if (assoc (caar alist) acc)
380           (uniqued-alist (cdr alist) acc)
381           (uniqued-alist (cdr alist) (cons (car alist) acc)))))
382
383 (define-public (alist<? x y)
384   (string<? (symbol->string (car x))
385             (symbol->string (car y))))
386
387 (define (map-alist-vals func list)
388   "map FUNC over the vals of  LIST, leaving the keys."
389   (if (null?  list)
390       '()
391       (cons (cons  (caar list) (func (cdar list)))
392             (map-alist-vals func (cdr list)))))
393
394 (define (map-alist-keys func list)
395   "map FUNC over the keys of an alist LIST, leaving the vals."
396   (if (null?  list)
397       '()
398       (cons (cons (func (caar list)) (cdar list))
399             (map-alist-keys func (cdr list)))))
400
401 (define-public (first-member members lst)
402   "Return first successful member (of member) from @var{members} in
403 @var{lst}."
404   (if (null? members)
405       #f
406       (let ((m (member (car members) lst)))
407         (if m m (first-member (cdr members) lst)))))
408
409 (define-public (first-assoc keys lst)
410   "Return first successful assoc of key from @var{keys} in @var{lst}."
411   (if (null? keys)
412       #f
413       (let ((k (assoc (car keys) lst)))
414         (if k k (first-assoc (cdr keys) lst)))))
415
416 (define-public (flatten-alist alist)
417   (if (null? alist)
418       '()
419       (cons (caar alist)
420             (cons (cdar alist)
421                   (flatten-alist (cdr alist))))))
422
423 (define (assoc-remove key alist)
424   "Remove key (and its corresponding value) from an alist.
425    Different than assoc-remove! because it is non-destructive."
426   (define (assoc-crawler key l r)
427     (if (null? r)
428         l
429         (if (equal? (caar r) key)
430             (append l (cdr r))
431             (assoc-crawler key (append l `(,(car r))) (cdr r)))))
432   (assoc-crawler key '() alist))
433
434 (define-public (map-selected-alist-keys function keys alist)
435   "Return @var{alist} with @var{function} applied to all of the values
436 in list @var{keys}.
437
438 For example:
439 @example
440 @code{guile> (map-selected-alist-keys - '(a b) '((a . 1) (b . -2) (c . 3) (d . 4)))}
441 @code{((a . -1) (b . 2) (c . 3) (d . 4)}
442 @end example"
443    (define (map-selected-alist-keys-helper function key alist)
444      (map
445      (lambda (pair)
446        (if (equal? key (car pair))
447            (cons key (function (cdr pair)))
448            pair))
449      alist))
450    (if (null? keys)
451        alist
452        (map-selected-alist-keys
453          function
454          (cdr keys)
455          (map-selected-alist-keys-helper function (car keys) alist))))
456
457 ;;;;;;;;;;;;;;;;
458 ;; vector
459
460 (define-public (vector-for-each proc vec)
461   (do
462       ((i 0 (1+ i)))
463       ((>= i (vector-length vec)) vec)
464     (vector-set! vec i (proc (vector-ref vec i)))))
465
466 ;;;;;;;;;;;;;;;;
467 ;; hash
468
469 (define-public (hash-table->alist t)
470   (hash-fold (lambda (k v acc) (acons  k v  acc))
471              '() t))
472
473 ;; todo: code dup with C++.
474 (define-safe-public (alist->hash-table lst)
475   "Convert alist to table"
476   (let ((m (make-hash-table (length lst))))
477     (map (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
478     m))
479
480 ;;;;;;;;;;;;;;;;
481 ;; list
482
483 (define (functional-or . rest)
484   (if (pair? rest)
485       (or (car rest)
486            (apply functional-or (cdr rest)))
487       #f))
488
489 (define (functional-and . rest)
490   (if (pair? rest)
491       (and (car rest)
492            (apply functional-and (cdr rest)))
493       #t))
494
495 (define (split-list lst n)
496   "Split LST in N equal sized parts"
497
498   (define (helper todo acc-vector k)
499     (if (null? todo)
500         acc-vector
501         (begin
502           (if (< k 0)
503               (set! k (+ n k)))
504
505           (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
506           (helper (cdr todo) acc-vector (1- k)))))
507
508   (helper lst (make-vector n '()) (1- n)))
509
510 (define (list-element-index lst x)
511   (define (helper todo k)
512     (cond
513      ((null? todo) #f)
514      ((equal? (car todo) x) k)
515      (else
516       (helper (cdr todo) (1+ k)))))
517
518   (helper lst 0))
519
520 (define-public (count-list lst)
521   "Given @var{lst} as @code{(E1 E2 .. )}, return
522 @code{((E1 . 1) (E2 . 2) ... )}."
523
524   (define (helper l acc count)
525     (if (pair? l)
526         (helper (cdr l) (cons (cons (car l) count) acc) (1+ count))
527         acc))
528
529
530   (reverse (helper lst '() 1)))
531
532 (define-public (list-join lst intermediate)
533   "Put @var{intermediate} between all elts of @var{lst}."
534
535   (fold-right
536    (lambda (elem prev)
537             (if (pair? prev)
538                 (cons  elem (cons intermediate prev))
539                 (list elem)))
540           '() lst))
541
542 (define-public (filtered-map proc lst)
543   (filter
544    (lambda (x) x)
545    (map proc lst)))
546
547 (define-public (flatten-list x)
548   "Unnest list."
549   (cond ((null? x) '())
550         ((not (pair? x)) (list x))
551         (else (append (flatten-list (car x))
552                       (flatten-list (cdr x))))))
553
554 (define (list-minus a b)
555   "Return list of elements in A that are not in B."
556   (lset-difference eq? a b))
557
558 (define-public (uniq-list lst)
559   "Uniq @var{lst}, assuming that it is sorted.  Uses @code{equal?}
560 for comparisons."
561
562   (reverse!
563    (fold (lambda (x acc)
564            (if (null? acc)
565                (list x)
566                (if (equal? x (car acc))
567                    acc
568                    (cons x acc))))
569          '() lst) '()))
570
571 (define (split-at-predicate pred lst)
572   "Split LST into two lists at the first element that returns #f for
573   (PRED previous_element element).  Return the two parts as a pair.
574   Example: (split-at-predicate < '(1 2 3 2 1)) ==> ((1 2 3) . (2 1))"
575   (if (null? lst)
576       (list lst)
577       (let ((i (list-index (lambda (x y) (not (pred x y)))
578                            lst
579                            (cdr lst))))
580         (if i
581             (cons (take lst (1+ i)) (drop lst (1+ i)))
582             (list lst)))))
583
584 (define-public (split-list-by-separator lst pred)
585   "Split @var{lst} at each element that satisfies @var{pred}, and return
586 the parts (with the separators removed) as a list of lists.  For example,
587 executing @samp{(split-list-by-separator '(a 0 b c 1 d) number?)} returns
588 @samp{((a) (b c) (d))}."
589   (let loop ((result '()) (lst lst))
590     (if (and lst (not (null? lst)))
591         (loop
592           (append result
593                   (list (take-while (lambda (x) (not (pred x))) lst)))
594           (let ((tail (find-tail pred lst)))
595             (if tail (cdr tail) #f)))
596        result)))
597
598 (define-public (offset-add a b)
599   (cons (+ (car a) (car b))
600         (+ (cdr a) (cdr b))))
601
602 (define-public (offset-flip-y o)
603   (cons (car o) (- (cdr o))))
604
605 (define-public (offset-scale o scale)
606   (cons (* (car o) scale)
607         (* (cdr o) scale)))
608
609 (define-public (ly:list->offsets accum coords)
610   (if (null? coords)
611       accum
612       (cons (cons (car coords) (cadr coords))
613             (ly:list->offsets accum (cddr coords)))))
614
615 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
616 ;; intervals
617
618 (define-public empty-interval '(+inf.0 . -inf.0))
619
620 (define-public (symmetric-interval expr)
621   (cons (- expr) expr))
622
623 (define-public (interval-length x)
624   "Length of the number-pair @var{x}, if an interval."
625   (max 0 (- (cdr x) (car x))))
626
627 (define-public (ordered-cons a b)
628   (cons (min a b)
629         (max a b)))
630
631 (define-public (interval-bound interval dir)
632   ((if (= dir RIGHT) cdr car) interval))
633
634 (define-public (interval-index interval dir)
635   "Interpolate @var{interval} between between left (@var{dir}=-1) and
636 right (@var{dir}=+1)."
637
638   (* (+  (interval-start interval) (interval-end interval)
639          (* dir (- (interval-end interval) (interval-start interval))))
640      0.5))
641
642 (define-public (interval-center x)
643   "Center the number-pair @var{x}, if an interval."
644   (if (interval-empty? x)
645       0.0
646       (/ (+ (car x) (cdr x)) 2)))
647
648 (define-public interval-start car)
649
650 (define-public interval-end cdr)
651
652 (define (other-axis a)
653   (remainder (+ a 1) 2))
654
655 (define-public (interval-widen iv amount)
656   (cons (- (car iv) amount)
657     (+ (cdr iv) amount)))
658
659 (define-public (interval-empty? iv)
660    (> (car iv) (cdr iv)))
661
662 (define-public (interval-union i1 i2)
663   (cons
664     (min (car i1) (car i2))
665     (max (cdr i1) (cdr i2))))
666
667 (define-public (interval-intersection i1 i2)
668    (cons
669      (max (car i1) (car i2))
670      (min (cdr i1) (cdr i2))))
671
672 (define-public (interval-sane? i)
673   (not (or  (nan? (car i))
674             (inf? (car i))
675             (nan? (cdr i))
676             (inf? (cdr i))
677             (> (car i) (cdr i)))))
678
679 (define-public (add-point interval p)
680   (cons (min (interval-start interval) p)
681         (max (interval-end interval) p)))
682
683 (define-public (reverse-interval iv)
684   (cons (cdr iv) (car iv)))
685
686 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
687 ;; coordinates
688
689 (define coord-x car)
690 (define coord-y cdr)
691
692 (define (coord-operation operator operand coordinate)
693   (if (pair? operand)
694     (cons (operator (coord-x operand) (coord-x coordinate))
695           (operator (coord-y operand) (coord-y coordinate)))
696     (cons (operator operand (coord-x coordinate))
697           (operator operand (coord-y coordinate)))))
698
699 (define (coord-apply function coordinate)
700   (if (pair? function)
701     (cons
702       ((coord-x function) (coord-x coordinate))
703       ((coord-y function) (coord-y coordinate)))
704     (cons
705       (function (coord-x coordinate))
706       (function (coord-y coordinate)))))
707
708 (define-public (coord-translate coordinate amount)
709   (coord-operation + amount coordinate))
710
711 (define-public (coord-scale coordinate amount)
712   (coord-operation * amount coordinate))
713
714 (define-public (coord-rotate coordinate degrees-in-radians)
715   (let*
716     ((coordinate
717       (cons
718         (exact->inexact (coord-x coordinate))
719         (exact->inexact (coord-y coordinate))))
720      (radius
721       (sqrt
722         (+ (* (coord-x coordinate) (coord-x coordinate))
723            (* (coord-y coordinate) (coord-y coordinate)))))
724     (angle (angle-0-2pi (atan (coord-y coordinate) (coord-x coordinate)))))
725    (cons
726      (* radius (cos (+ angle degrees-in-radians)))
727      (* radius (sin (+ angle degrees-in-radians))))))
728
729 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
730 ;; trig
731
732 (define-public PI (* 4 (atan 1)))
733
734 (define-public TWO-PI (* 2 PI))
735
736 (define-public PI-OVER-TWO (/ PI 2))
737
738 (define-public THREE-PI-OVER-TWO (* 3 PI-OVER-TWO))
739
740 (define-public (cyclic-base-value value cycle)
741   "Take @var{value} and modulo-maps it between 0 and base @var{cycle}."
742   (if (< value 0)
743       (cyclic-base-value (+ value cycle) cycle)
744       (if (>= value cycle)
745           (cyclic-base-value (- value cycle) cycle)
746           value)))
747
748 (define-public (angle-0-2pi angle)
749   "Take @var{angle} (in radians) and maps it between 0 and 2pi."
750   (cyclic-base-value angle TWO-PI))
751
752 (define-public (angle-0-360 angle)
753   "Take @var{angle} (in degrees) and maps it between 0 and 360 degrees."
754   (cyclic-base-value angle 360.0))
755
756 (define-public PI-OVER-180  (/ PI 180))
757
758 (define-public (degrees->radians angle-degrees)
759   "Convert the given angle from degrees to radians."
760   (* angle-degrees PI-OVER-180))
761
762 (define-public (ellipse-radius x-radius y-radius angle)
763   (/
764     (* x-radius y-radius)
765     (sqrt
766       (+ (* (expt y-radius 2)
767             (* (cos angle) (cos angle)))
768         (* (expt x-radius 2)
769            (* (sin angle) (sin angle)))))))
770
771 (define-public (polar->rectangular radius angle-in-degrees)
772   "Return polar coordinates (@var{radius}, @var{angle-in-degrees})
773 as rectangular coordinates @ode{(x-length . y-length)}."
774
775   (let ((complex (make-polar
776                     radius
777                     (degrees->radians angle-in-degrees))))
778      (cons
779        (real-part complex)
780        (imag-part complex))))
781
782 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
783 ;; string
784
785 (define-public (string-endswith s suffix)
786   (equal? suffix (substring s
787                             (max 0 (- (string-length s) (string-length suffix)))
788                             (string-length s))))
789
790 (define-public (string-startswith s prefix)
791   (equal? prefix (substring s 0 (min (string-length s) (string-length prefix)))))
792
793 (define-public (string-encode-integer i)
794   (cond
795    ((= i  0) "o")
796    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
797    (else (string-append
798           (make-string 1 (integer->char (+ 65 (modulo i 26))))
799           (string-encode-integer (quotient i 26))))))
800
801 (define (number->octal-string x)
802   (let* ((n (inexact->exact x))
803          (n64 (quotient n 64))
804          (n8 (quotient (- n (* n64 64)) 8)))
805     (string-append
806      (number->string n64)
807      (number->string n8)
808      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
809
810 (define-public (ly:inexact->string x radix)
811   (let ((n (inexact->exact x)))
812     (number->string n radix)))
813
814 (define-public (ly:number-pair->string c)
815   (string-append (ly:number->string (car c)) " "
816                  (ly:number->string (cdr c))))
817
818 (define-public (dir-basename file . rest)
819   "Strip suffixes in @var{rest}, but leave directory component for
820 @var{file}."
821   (define (inverse-basename x y) (basename y x))
822   (simple-format #f "~a/~a" (dirname file)
823                  (fold inverse-basename file rest)))
824
825 (define-public (write-me message x)
826   "Return @var{x}.  Display @var{message} and write @var{x}.
827 Handy for debugging, possibly turned off."
828   (display message) (write x) (newline) x)
829 ;;  x)
830
831 (define-public (stderr string . rest)
832   (apply format (cons (current-error-port) (cons string rest)))
833   (force-output (current-error-port)))
834
835 (define-public (debugf string . rest)
836   (if #f
837       (apply stderr (cons string rest))))
838
839 (define (index-cell cell dir)
840   (if (equal? dir 1)
841       (cdr cell)
842       (car cell)))
843
844 (define (cons-map f x)
845   "map F to contents of X"
846   (cons (f (car x)) (f (cdr x))))
847
848 (define-public (list-insert-separator lst between)
849   "Create new list, inserting @var{between} between elements of @var{lst}."
850   (define (conc x y )
851     (if (eq? y #f)
852         (list x)
853         (cons x  (cons between y))))
854   (fold-right conc #f lst))
855
856 (define-public (string-regexp-substitute a b str)
857   (regexp-substitute/global #f a str 'pre b 'post))
858
859 (define (regexp-split str regex)
860   (define matches '())
861   (define end-of-prev-match 0)
862   (define (notice match)
863
864     (set! matches (cons (substring (match:string match)
865                                    end-of-prev-match
866                                    (match:start match))
867                         matches))
868     (set! end-of-prev-match (match:end match)))
869
870   (regexp-substitute/global #f regex str notice 'post)
871
872   (if (< end-of-prev-match (string-length str))
873       (set!
874        matches
875        (cons (substring str end-of-prev-match (string-length str)) matches)))
876
877    (reverse matches))
878
879 ;;;;;;;;;;;;;;;;
880 ;; other
881
882 (define (sign x)
883   (if (= x 0)
884       0
885       (if (< x 0) -1 1)))
886
887 (define-public (binary-search start end getter target-val)
888   (_i "Find the index between @var{start} and @var{end} (an integer)
889 which produces the closest match to @var{target-val} if
890 applied to function @var{getter}.")
891   (if (<= end start)
892       start
893       (let* ((compare (quotient (+ start end) 2))
894              (get-val (getter compare)))
895         (cond
896          ((< target-val get-val)
897           (set! end (1- compare)))
898          ((< get-val target-val)
899           (set! start (1+ compare))))
900         (binary-search start end getter target-val))))
901
902 (define-public (car< a b)
903   (< (car a) (car b)))
904
905 (define-public (car<= a b)
906   (<= (car a) (car b)))
907
908 (define-public (symbol<? lst r)
909   (string<? (symbol->string lst) (symbol->string r)))
910
911 (define-public (symbol-key<? lst r)
912   (string<? (symbol->string (car lst)) (symbol->string (car r))))
913
914 (define-public (eval-carefully symbol module . default)
915   "Check whether all symbols in expr @var{symbol} are reachable
916 in module @var{module}.  In that case evaluate, otherwise
917 print a warning and set an optional @var{default}."
918   (let* ((unavailable? (lambda (sym)
919                          (not (module-defined? module sym))))
920          (sym-unavailable (if (pair? symbol)
921                               (filter
922                                 unavailable?
923                                 (filter symbol? (flatten-list symbol)))
924                               (if (unavailable? symbol)
925                                    #t
926                                    '()))))
927     (if (null? sym-unavailable)
928         (eval symbol module)
929         (let* ((def (and (pair? default) (car default))))
930           (ly:programming-error
931             "cannot evaluate ~S in module ~S, setting to ~S"
932             (object->string symbol)
933             (object->string module)
934             (object->string def))
935           def))))
936
937 ;;
938 ;; don't confuse users with #<procedure .. > syntax.
939 ;;
940 (define-public (scm->string val)
941   (if (and (procedure? val)
942            (symbol? (procedure-name val)))
943       (symbol->string (procedure-name val))
944       (string-append
945        (if (self-evaluating? val)
946            (if (string? val)
947                "\""
948                "")
949            "'")
950        (call-with-output-string (lambda (port) (display val port)))
951        (if (string? val)
952            "\""
953            ""))))
954
955 (define-public (!= lst r)
956   (not (= lst r)))
957
958 (define-public lily-unit->bigpoint-factor
959   (cond
960    ((equal? (ly:unit) "mm") (/ 72.0 25.4))
961    ((equal? (ly:unit) "pt") (/ 72.0 72.27))
962    (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
963
964 (define-public lily-unit->mm-factor
965   (* 25.4 (/ lily-unit->bigpoint-factor 72)))
966
967 ;;; FONT may be font smob, or pango font string...
968 (define-public (font-name-style font)
969   (if (string? font)
970       (string-downcase font)
971       (let* ((font-name (ly:font-name font))
972              (full-name (if font-name font-name (ly:font-file-name font))))
973           (string-downcase full-name))))
974
975 (define-public (modified-font-metric-font-scaling font)
976   (let* ((designsize (ly:font-design-size font))
977          (magnification (* (ly:font-magnification font)))
978          (scaling (* magnification designsize)))
979     (debugf "scaling:~S\n" scaling)
980     (debugf "magnification:~S\n" magnification)
981     (debugf "design:~S\n" designsize)
982     scaling))
983
984 (define-public (version-not-seen-message input-file-name)
985   (ly:warning-located
986     (ly:format "~a:0" input-file-name)
987     (_ "no \\version statement found, please add~afor future compatibility")
988     (format #f "\n\n\\version ~s\n\n" (lilypond-version))))
989
990 (define-public (old-relative-not-used-message input-file-name)
991   (ly:warning-located
992     (ly:format "~a:0" input-file-name)
993     (_ "old relative compatibility not used")))