1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
19 ;; for take, drop, take-while, list-index, and find-tail:
20 (use-modules (srfi srfi-1))
22 ;; for define-safe-public when byte-compiling using Guile V2
23 (use-modules (scm safe-utility-defs))
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 (define-safe-public START -1)
31 (define-safe-public STOP 1)
32 (define-public LEFT -1)
33 (define-public RIGHT 1)
35 (define-public DOWN -1)
36 (define-public CENTER 0)
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)
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)
60 (define-safe-public INFINITY-INT 1000000)
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65 (define-public ZERO-MOMENT (ly:make-moment 0 1))
67 (define-public (moment-min a b)
68 (if (ly:moment<? a b) a b))
70 (define-public (moment<=? a b)
74 (define-public (fraction->moment fraction)
77 (ly:make-moment (car fraction) (cdr fraction))))
79 (define-public (moment->fraction moment)
80 (cons (ly:moment-main-numerator moment)
81 (ly:moment-main-denominator moment)))
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 (define-public (duration-log-factor lognum)
87 "Given a logarithmic duration number, return the length of the duration,
88 as a number of whole notes."
89 (or (and (exact? lognum) (integer? lognum))
90 (scm-error 'wrong-type-arg "duration-log-factor" "Not an integer: ~S" (list lognum) #f))
95 (define-public (duration-dot-factor dotcount)
96 "Given a count of the dots used to extend a musical duration, return
97 the numeric factor by which they increase the duration."
98 (or (and (exact? dotcount) (integer? dotcount) (>= dotcount 0))
99 (scm-error 'wrong-type-arg "duration-dot-factor" "Not a count: ~S" (list dotcount) #f))
100 (- 2 (/ (ash 1 dotcount))))
102 (define-public (duration-length dur)
103 "Return the overall length of a duration, as a number of whole
104 notes. (Not to be confused with ly:duration-length, which returns a
105 less-useful moment object.)"
106 (ly:moment-main (ly:duration-length dur)))
108 (define-public (duration-visual dur)
109 "Given a duration object, return the visual part of the duration (base
110 note length and dot count), in the form of a duration object with
111 non-visual scale factor 1."
112 (ly:make-duration (ly:duration-log dur) (ly:duration-dot-count dur) 1))
114 (define-public (duration-visual-length dur)
115 "Given a duration object, return the length of the visual part of the
116 duration (base note length and dot count), as a number of whole notes."
117 (duration-length (duration-visual dur)))
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121 (define-public (average x . lst)
122 (/ (+ x (apply + lst)) (1+ (length lst))))
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ;; parser <-> output hooks.
127 (define-public (collect-bookpart-for-book parser book-part)
128 "Toplevel book-part handler."
129 (define (add-bookpart book-part)
131 parser 'toplevel-bookparts
132 (cons book-part (ly:parser-lookup parser 'toplevel-bookparts))))
133 ;; If toplevel scores have been found before this \bookpart,
134 ;; add them first to a dedicated bookpart
135 (if (pair? (ly:parser-lookup parser 'toplevel-scores))
137 (add-bookpart (ly:make-book-part
138 (ly:parser-lookup parser 'toplevel-scores)))
139 (ly:parser-define! parser 'toplevel-scores (list))))
140 (add-bookpart book-part))
142 (define-public (collect-scores-for-book parser score)
144 parser 'toplevel-scores
145 (cons score (ly:parser-lookup parser 'toplevel-scores))))
147 (define-public (collect-music-aux score-handler parser music)
148 (define (music-property symbol)
149 (ly:music-property music symbol #f))
150 (cond ((music-property 'page-marker)
151 ;; a page marker: set page break/turn permissions or label
152 (let ((label (music-property 'page-label)))
154 (score-handler (ly:make-page-label-marker label))))
155 (for-each (lambda (symbol)
156 (let ((permission (music-property symbol)))
157 (if (symbol? permission)
159 (ly:make-page-permission-marker symbol
160 (if (eq? 'forbid permission)
163 '(line-break-permission page-break-permission
164 page-turn-permission)))
165 ((not (music-property 'void))
166 ;; a regular music expression: make a score with this music
167 ;; void music is discarded
168 (score-handler (scorify-music music parser)))))
170 (define-public (collect-music-for-book parser music)
171 "Top-level music handler."
172 (collect-music-aux (lambda (score)
173 (collect-scores-for-book parser score))
177 (define-public (collect-book-music-for-book parser book music)
178 "Book music handler."
179 (collect-music-aux (lambda (score)
180 (ly:book-add-score! book score))
184 (define-public (scorify-music music parser)
185 "Preprocess @var{music}."
187 (fold (lambda (f m) (f m parser))
189 toplevel-music-functions)))
191 (define (get-current-filename parser book)
192 "return any suffix value for output filename allowing for settings by
193 calls to bookOutputName function"
194 (or (paper-variable parser book 'output-filename)
195 (ly:parser-output-name parser)))
197 (define (get-current-suffix parser book)
198 "return any suffix value for output filename allowing for settings by calls to
200 (let ((book-output-suffix (paper-variable parser book 'output-suffix)))
201 (if (not (string? book-output-suffix))
202 (ly:parser-lookup parser 'output-suffix)
203 book-output-suffix)))
205 (define-public current-outfile-name #f) ; for use by regression tests
207 (define (get-outfile-name parser book)
208 "return current filename for generating backend output files"
209 ;; user can now override the base file name, so we have to use
210 ;; the file-name concatenated with any potential output-suffix value
211 ;; as the key to out internal a-list
212 (let* ((base-name (get-current-filename parser book))
213 (output-suffix (get-current-suffix parser book))
214 (alist-key (format #f "~a~a" base-name output-suffix))
215 (counter-alist (ly:parser-lookup parser 'counter-alist))
216 (output-count (assoc-get alist-key counter-alist 0))
218 ;; Allow all ASCII alphanumerics, including accents
219 (if (string? output-suffix)
223 (string-regexp-substitute
228 ;; assoc-get call will always have returned a number
229 (if (> output-count 0)
230 (set! result (format #f "~a-~a" result output-count)))
233 parser 'counter-alist
234 (assoc-set! counter-alist alist-key (1+ output-count)))
235 (set! current-outfile-name result)
238 (define (print-book-with parser book process-procedure)
239 (let* ((paper (ly:parser-lookup parser '$defaultpaper))
240 (layout (ly:parser-lookup parser '$defaultlayout))
241 (outfile-name (get-outfile-name parser book)))
242 (process-procedure book paper layout outfile-name)))
244 (define-public (print-book-with-defaults parser book)
245 (print-book-with parser book ly:book-process))
247 (define-public (print-book-with-defaults-as-systems parser book)
248 (print-book-with parser book ly:book-process-to-systems))
250 ;; Add a score to the current bookpart, book or toplevel
251 (define-public (add-score parser score)
253 ((ly:parser-lookup parser '$current-bookpart)
254 ((ly:parser-lookup parser 'bookpart-score-handler)
255 (ly:parser-lookup parser '$current-bookpart) score))
256 ((ly:parser-lookup parser '$current-book)
257 ((ly:parser-lookup parser 'book-score-handler)
258 (ly:parser-lookup parser '$current-book) score))
260 ((ly:parser-lookup parser 'toplevel-score-handler) parser score))))
262 (define-public paper-variable
265 (lambda (parser book)
266 (append (if (and book (ly:output-def? (ly:book-paper book)))
267 (list (ly:book-paper book))
269 (ly:parser-lookup parser '$papers)
270 (list (ly:parser-lookup parser '$defaultpaper))))))
271 (make-procedure-with-setter
272 (lambda (parser book symbol)
273 (any (lambda (p) (ly:output-def-lookup p symbol #f))
274 (get-papers parser book)))
275 (lambda (parser book symbol value)
276 (ly:output-def-set-variable!
277 (car (get-papers parser book))
280 (define-public (add-text parser text)
281 (add-score parser (list text)))
283 (define-public (add-music parser music)
284 (collect-music-aux (lambda (score)
285 (add-score parser score))
289 (define-public (context-mod-from-music parser music)
290 (let ((warn #t) (mods (ly:make-context-mod)))
291 (let loop ((m music))
292 (if (music-is-of-type? m 'layout-instruction-event)
293 (let ((symbol (ly:music-property m 'symbol)))
296 (case (ly:music-property m 'name)
300 (ly:music-property m 'value)))
302 (list 'unset symbol))
306 (ly:music-property m 'grob-value)
308 ((ly:music-property m 'grob-property #f) => list)
310 (ly:music-property m 'grob-property-path)))))
315 ((ly:music-property m 'grob-property #f) => list)
317 (ly:music-property m 'grob-property-path))))))))
318 (case (ly:music-property m 'name)
320 (ly:add-context-mod mods
322 (ly:music-property m 'procedure))))
323 ((ContextSpeccedMusic)
324 (loop (ly:music-property m 'element)))
326 (let ((callback (ly:music-property m 'elements-callback)))
327 (if (procedure? callback)
328 (for-each loop (callback m))
329 (if (and warn (ly:duration? (ly:music-property m 'duration)))
333 (_ "Music unsuitable for context-mod"))
334 (set! warn #f)))))))))
337 (define-public (context-defs-from-music parser output-def music)
339 (let loop ((m music) (mods #f))
340 ;; The parser turns all sets, overrides etc into something
341 ;; wrapped in ContextSpeccedMusic. If we ever get a set,
342 ;; override etc that is not wrapped in ContextSpeccedMusic, the
343 ;; user has created it in Scheme himself without providing the
344 ;; required wrapping. In that case, using #f in the place of a
345 ;; context modification results in a reasonably recognizable
347 (if (music-is-of-type? m 'layout-instruction-event)
350 (case (ly:music-property m 'name)
353 (ly:music-property m 'symbol)
354 (ly:music-property m 'value)))
357 (ly:music-property m 'symbol)))
360 (ly:music-property m 'symbol)
361 (ly:music-property m 'grob-value)
363 ((ly:music-property m 'grob-property #f) => list)
365 (ly:music-property m 'grob-property-path)))))
368 (ly:music-property m 'symbol)
370 ((ly:music-property m 'grob-property #f) => list)
372 (ly:music-property m 'grob-property-path)))))))
373 (case (ly:music-property m 'name)
375 (ly:add-context-mod mods
377 (ly:music-property m 'procedure))))
378 ((ContextSpeccedMusic)
379 ;; Use let* here to let defs catch up with modifications
380 ;; to the context defs made in the recursion
381 (let* ((mods (loop (ly:music-property m 'element)
382 (ly:make-context-mod)))
383 (defs (ly:output-find-context-def
384 output-def (ly:music-property m 'context-type))))
388 (ly:format (_ "Cannot find context-def \\~a")
389 (ly:music-property m 'context-type)))
392 (ly:output-def-set-variable!
393 output-def (car entry)
394 (ly:context-def-modify (cdr entry) mods)))
397 (let ((callback (ly:music-property m 'elements-callback)))
398 (if (procedure? callback)
399 (fold loop mods (callback m))
400 (if (and warn (ly:duration? (ly:music-property m 'duration)))
404 (_ "Music unsuitable for output-def"))
405 (set! warn #f))))))))
412 (define-public assoc-get ly:assoc-get)
414 (define-public chain-assoc-get ly:chain-assoc-get)
416 (define-public (uniqued-alist alist acc)
417 (if (null? alist) acc
418 (if (assoc (caar alist) acc)
419 (uniqued-alist (cdr alist) acc)
420 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
422 (define-public (alist<? x y)
423 (string<? (symbol->string (car x))
424 (symbol->string (car y))))
426 (define (map-alist-vals func list)
427 "map FUNC over the vals of LIST, leaving the keys."
430 (cons (cons (caar list) (func (cdar list)))
431 (map-alist-vals func (cdr list)))))
433 (define (map-alist-keys func list)
434 "map FUNC over the keys of an alist LIST, leaving the vals."
437 (cons (cons (func (caar list)) (cdar list))
438 (map-alist-keys func (cdr list)))))
440 (define-public (first-member members lst)
441 "Return first successful member (of member) from @var{members} in
443 (any (lambda (m) (member m lst)) members))
445 (define-public (first-assoc keys lst)
446 "Return first successful assoc of key from @var{keys} in @var{lst}."
447 (any (lambda (k) (assoc k lst)) keys))
449 (define-public (flatten-alist alist)
454 (flatten-alist (cdr alist))))))
456 (define-public (map-selected-alist-keys function keys alist)
457 "Return @var{alist} with @var{function} applied to all of the values
462 @code{guile> (map-selected-alist-keys - '(a b) '((a . 1) (b . -2) (c . 3) (d . 4)))}
463 @code{((a . -1) (b . 2) (c . 3) (d . 4)}
465 (define (map-selected-alist-keys-helper key alist)
468 (if (equal? key (car pair))
469 (cons key (function (cdr pair)))
472 (fold map-selected-alist-keys-helper alist keys))
477 (define-public (vector-for-each proc vec)
480 ((>= i (vector-length vec)) vec)
481 (vector-set! vec i (proc (vector-ref vec i)))))
486 (define-public (hash-table->alist t)
487 (hash-fold acons '() t))
489 ;; todo: code dup with C++.
490 (define-safe-public (alist->hash-table lst)
491 "Convert alist to table"
492 (let ((m (make-hash-table (length lst))))
493 (for-each (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
499 (define (functional-or . rest)
502 (define (functional-and . rest)
503 (every identity rest))
505 (define (split-list lst n)
506 "Split LST in N equal sized parts"
508 (define (helper todo acc-vector k)
515 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
516 (helper (cdr todo) acc-vector (1- k)))))
518 (helper lst (make-vector n '()) (1- n)))
520 (define (list-element-index lst x)
521 (list-index (lambda (m) (equal? m x)) lst))
523 (define-public (count-list lst)
524 "Given @var{lst} as @code{(E1 E2 .. )}, return
525 @code{((E1 . 1) (E2 . 2) ... )}."
526 (map cons lst (iota (length lst) 1)))
528 (define-public (list-join lst intermediate)
529 "Put @var{intermediate} between all elts of @var{lst}."
534 (cons elem (cons intermediate prev))
538 (define-public (filtered-map proc lst)
543 (define-public (flatten-list x)
545 (let loop ((x x) (tail '()))
546 (cond ((list? x) (fold-right loop tail x))
547 ((not (pair? x)) (cons x tail))
548 (else (loop (car x) (loop (cdr x) tail))))))
550 (define (list-minus a b)
551 "Return list of elements in A that are not in B."
552 (lset-difference eq? a b))
554 (define-public (uniq-list lst)
555 "Uniq @var{lst}, assuming that it is sorted. Uses @code{equal?}
559 (fold (lambda (x acc)
562 (if (equal? x (car acc))
567 (define (split-at-predicate pred lst)
568 "Split LST into two lists at the first element that returns #f for
569 (PRED previous_element element). Return the two parts as a pair.
570 Example: (split-at-predicate < '(1 2 3 2 1)) ==> ((1 2 3) . (2 1))"
571 (let ((i (and (pair? lst)
572 (list-index (lambda (x y) (not (pred x y)))
577 (lambda () (split-at lst (1+ i)))
581 (define-public (split-list-by-separator lst pred)
582 "Split @var{lst} at each element that satisfies @var{pred}, and return
583 the parts (with the separators removed) as a list of lists. For example,
584 executing @samp{(split-list-by-separator '(a 0 b c 1 d) number?)} returns
585 @samp{((a) (b c) (d))}."
586 (call-with-values (lambda () (break pred lst))
591 (split-list-by-separator (cdr tail) pred))))))
593 (define-public (offset-add a b)
594 (cons (+ (car a) (car b))
595 (+ (cdr a) (cdr b))))
597 (define-public (offset-flip-y o)
598 (cons (car o) (- (cdr o))))
600 (define-public (offset-scale o scale)
601 (cons (* (car o) scale)
604 (define-public (ly:list->offsets accum coords)
607 (cons (cons (car coords) (cadr coords))
608 (ly:list->offsets accum (cddr coords)))))
610 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
613 (define-public empty-interval '(+inf.0 . -inf.0))
615 (define-public (symmetric-interval expr)
616 (cons (- expr) expr))
618 (define-public (interval-length x)
619 "Length of the number-pair @var{x}, if an interval."
620 (max 0 (- (cdr x) (car x))))
622 (define-public (ordered-cons a b)
626 (define-public (interval-bound interval dir)
627 ((if (= dir RIGHT) cdr car) interval))
629 (define-public (interval-index interval dir)
630 "Interpolate @var{interval} between between left (@var{dir}=-1) and
631 right (@var{dir}=+1)."
633 (* (+ (interval-start interval) (interval-end interval)
634 (* dir (- (interval-end interval) (interval-start interval))))
637 (define-public (interval-center x)
638 "Center the number-pair @var{x}, if an interval."
639 (if (interval-empty? x)
641 (/ (+ (car x) (cdr x)) 2)))
643 (define-public interval-start car)
645 (define-public interval-end cdr)
647 (define (other-axis a)
648 (remainder (+ a 1) 2))
650 (define-public (interval-scale iv factor)
651 (cons (* (car iv) factor)
652 (* (cdr iv) factor)))
654 (define-public (interval-widen iv amount)
655 (cons (- (car iv) amount)
656 (+ (cdr iv) amount)))
658 (define-public (interval-empty? iv)
659 (> (car iv) (cdr iv)))
661 (define-public (interval-union i1 i2)
663 (min (car i1) (car i2))
664 (max (cdr i1) (cdr i2))))
666 (define-public (interval-intersection i1 i2)
668 (max (car i1) (car i2))
669 (min (cdr i1) (cdr i2))))
671 (define-public (interval-sane? i)
672 (not (or (nan? (car i))
676 (> (car i) (cdr i)))))
678 (define-public (add-point interval p)
679 (cons (min (interval-start interval) p)
680 (max (interval-end interval) p)))
682 (define-public (reverse-interval iv)
683 (cons (cdr iv) (car iv)))
685 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
691 (define (coord-operation operator operand coordinate)
693 (cons (operator (coord-x operand) (coord-x coordinate))
694 (operator (coord-y operand) (coord-y coordinate)))
695 (cons (operator operand (coord-x coordinate))
696 (operator operand (coord-y coordinate)))))
698 (define (coord-apply function coordinate)
701 ((coord-x function) (coord-x coordinate))
702 ((coord-y function) (coord-y coordinate)))
704 (function (coord-x coordinate))
705 (function (coord-y coordinate)))))
707 (define-public (coord-translate coordinate amount)
708 (coord-operation + amount coordinate))
710 (define-public (coord-scale coordinate amount)
711 (coord-operation * amount coordinate))
713 (define-public (coord-rotate coordinate degrees-in-radians)
717 (exact->inexact (coord-x coordinate))
718 (exact->inexact (coord-y coordinate))))
721 (+ (* (coord-x coordinate) (coord-x coordinate))
722 (* (coord-y coordinate) (coord-y coordinate)))))
723 (angle (angle-0-2pi (atan (coord-y coordinate) (coord-x coordinate)))))
725 (* radius (cos (+ angle degrees-in-radians)))
726 (* radius (sin (+ angle degrees-in-radians))))))
728 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
731 (define-public PI (* 4 (atan 1)))
733 (define-public TWO-PI (* 2 PI))
735 (define-public PI-OVER-TWO (/ PI 2))
737 (define-public THREE-PI-OVER-TWO (* 3 PI-OVER-TWO))
739 (define-public (cyclic-base-value value cycle)
740 "Take @var{value} and modulo-maps it between 0 and base @var{cycle}."
742 (cyclic-base-value (+ value cycle) cycle)
744 (cyclic-base-value (- value cycle) cycle)
747 (define-public (angle-0-2pi angle)
748 "Take @var{angle} (in radians) and maps it between 0 and 2pi."
749 (cyclic-base-value angle TWO-PI))
751 (define-public (angle-0-360 angle)
752 "Take @var{angle} (in degrees) and maps it between 0 and 360 degrees."
753 (cyclic-base-value angle 360.0))
755 (define-public PI-OVER-180 (/ PI 180))
757 (define-public (degrees->radians angle-degrees)
758 "Convert the given angle from degrees to radians."
759 (* angle-degrees PI-OVER-180))
761 (define-public (ellipse-radius x-radius y-radius angle)
763 (* x-radius y-radius)
765 (+ (* (expt y-radius 2)
766 (* (cos angle) (cos angle)))
768 (* (sin angle) (sin angle)))))))
770 (define-public (polar->rectangular radius angle-in-degrees)
771 "Return polar coordinates (@var{radius}, @var{angle-in-degrees})
772 as rectangular coordinates @ode{(x-length . y-length)}."
774 (let ((complex (make-polar
776 (degrees->radians angle-in-degrees))))
779 (imag-part complex))))
781 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
784 (define-public (string-endswith s suffix)
785 (equal? suffix (substring s
786 (max 0 (- (string-length s) (string-length suffix)))
789 (define-public (string-startswith s prefix)
790 (equal? prefix (substring s 0 (min (string-length s) (string-length prefix)))))
792 (define-public (string-encode-integer i)
795 ((< i 0) (string-append "n" (string-encode-integer (- i))))
797 (make-string 1 (integer->char (+ 65 (modulo i 26))))
798 (string-encode-integer (quotient i 26))))))
800 (define (number->octal-string x)
801 (let* ((n (inexact->exact x))
802 (n64 (quotient n 64))
803 (n8 (quotient (- n (* n64 64)) 8)))
807 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
809 (define-public (ly:inexact->string x radix)
810 (let ((n (inexact->exact x)))
811 (number->string n radix)))
813 (define-public (ly:number-pair->string c)
814 (string-append (ly:number->string (car c)) " "
815 (ly:number->string (cdr c))))
817 (define-public (dir-basename file . rest)
818 "Strip suffixes in @var{rest}, but leave directory component for
820 (define (inverse-basename x y) (basename y x))
821 (simple-format #f "~a/~a" (dirname file)
822 (fold inverse-basename file rest)))
824 (define-public (write-me message x)
825 "Return @var{x}. Display @var{message} and write @var{x}.
826 Handy for debugging, possibly turned off."
827 (display message) (write x) (newline) x)
830 (define-public (stderr string . rest)
831 (apply format (cons (current-error-port) (cons string rest)))
832 (force-output (current-error-port)))
834 (define-public (debugf string . rest)
836 (apply stderr (cons string rest))))
838 (define (index-cell cell dir)
843 (define (cons-map f x)
844 "map F to contents of X"
845 (cons (f (car x)) (f (cdr x))))
847 (define-public (list-insert-separator lst between)
848 "Create new list, inserting @var{between} between elements of @var{lst}."
852 (cons x (cons between y))))
853 (fold-right conc #f lst))
855 (define-public (string-regexp-substitute a b str)
856 (regexp-substitute/global #f a str 'pre b 'post))
858 (define (regexp-split str regex)
860 (define end-of-prev-match 0)
861 (define (notice match)
863 (set! matches (cons (substring (match:string match)
867 (set! end-of-prev-match (match:end match)))
869 (regexp-substitute/global #f regex str notice 'post)
871 (if (< end-of-prev-match (string-length str))
874 (cons (substring str end-of-prev-match (string-length str)) matches)))
886 (define-public (binary-search start end getter target-val)
887 (_i "Find the index between @var{start} and @var{end} (an integer)
888 which produces the closest match to @var{target-val} if
889 applied to function @var{getter}.")
892 (let* ((compare (quotient (+ start end) 2))
893 (get-val (getter compare)))
895 ((< target-val get-val)
896 (set! end (1- compare)))
897 ((< get-val target-val)
898 (set! start (1+ compare))))
899 (binary-search start end getter target-val))))
901 (define-public (car< a b)
904 (define-public (car<= a b)
905 (<= (car a) (car b)))
907 (define-public (symbol<? lst r)
908 (string<? (symbol->string lst) (symbol->string r)))
910 (define-public (symbol-key<? lst r)
911 (string<? (symbol->string (car lst)) (symbol->string (car r))))
913 (define-public (eval-carefully symbol module . default)
914 "Check whether all symbols in expr @var{symbol} are reachable
915 in module @var{module}. In that case evaluate, otherwise
916 print a warning and set an optional @var{default}."
917 (let* ((unavailable? (lambda (sym)
918 (not (module-defined? module sym))))
922 (filter symbol? (flatten-list symbol)))))
923 (if (null? sym-unavailable)
925 (let* ((def (and (pair? default) (car default))))
926 (ly:programming-error
927 "cannot evaluate ~S in module ~S, setting to ~S"
928 (object->string symbol)
929 (object->string module)
930 (object->string def))
934 ;; don't confuse users with #<procedure .. > syntax.
936 (define-public (scm->string val)
937 (if (and (procedure? val)
938 (symbol? (procedure-name val)))
939 (symbol->string (procedure-name val))
941 (if (self-evaluating? val)
946 (call-with-output-string (lambda (port) (display val port)))
951 (define-public (!= lst r)
954 (define-public lily-unit->bigpoint-factor
956 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
957 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
958 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
960 (define-public lily-unit->mm-factor
961 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
963 ;;; FONT may be font smob, or pango font string...
964 (define-public (font-name-style font)
966 (string-downcase font)
967 (let* ((font-name (ly:font-name font))
968 (full-name (if font-name font-name (ly:font-file-name font))))
969 (string-downcase full-name))))
971 (define-public (modified-font-metric-font-scaling font)
972 (let* ((designsize (ly:font-design-size font))
973 (magnification (* (ly:font-magnification font)))
974 (scaling (* magnification designsize)))
975 (debugf "scaling:~S\n" scaling)
976 (debugf "magnification:~S\n" magnification)
977 (debugf "design:~S\n" designsize)
980 (define-public (version-not-seen-message input-file-name)
982 (ly:format "~a:1" input-file-name)
983 (_ "no \\version statement found, please add~afor future compatibility")
984 (format #f "\n\n\\version ~s\n\n" (lilypond-version))))