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 (define-public (seconds->moment s context)
84 "Return a moment equivalent to s seconds at the current tempo."
85 (ly:moment-mul (ly:context-property context 'tempoWholesPerMinute)
86 (ly:make-moment (/ s 60))))
88 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 (define-public (duration-log-factor lognum)
92 "Given a logarithmic duration number, return the length of the duration,
93 as a number of whole notes."
94 (or (and (exact? lognum) (integer? lognum))
95 (scm-error 'wrong-type-arg "duration-log-factor" "Not an integer: ~S" (list lognum) #f))
100 (define-public (duration-dot-factor dotcount)
101 "Given a count of the dots used to extend a musical duration, return
102 the numeric factor by which they increase the duration."
103 (or (and (exact? dotcount) (integer? dotcount) (>= dotcount 0))
104 (scm-error 'wrong-type-arg "duration-dot-factor" "Not a count: ~S" (list dotcount) #f))
105 (- 2 (/ (ash 1 dotcount))))
107 (define-public (duration-length dur)
108 "Return the overall length of a duration, as a number of whole
109 notes. (Not to be confused with ly:duration-length, which returns a
110 less-useful moment object.)"
111 (ly:moment-main (ly:duration-length dur)))
113 (define-public (duration-visual dur)
114 "Given a duration object, return the visual part of the duration (base
115 note length and dot count), in the form of a duration object with
116 non-visual scale factor 1."
117 (ly:make-duration (ly:duration-log dur) (ly:duration-dot-count dur) 1))
119 (define-public (duration-visual-length dur)
120 "Given a duration object, return the length of the visual part of the
121 duration (base note length and dot count), as a number of whole notes."
122 (duration-length (duration-visual dur)))
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 (define-public (average x . lst)
127 (/ (+ x (apply + lst)) (1+ (length lst))))
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130 ;; parser <-> output hooks.
132 (define-public (collect-bookpart-for-book parser book-part)
133 "Toplevel book-part handler."
134 (define (add-bookpart book-part)
136 parser 'toplevel-bookparts
137 (cons book-part (ly:parser-lookup parser 'toplevel-bookparts))))
138 ;; If toplevel scores have been found before this \bookpart,
139 ;; add them first to a dedicated bookpart
140 (if (pair? (ly:parser-lookup parser 'toplevel-scores))
142 (add-bookpart (ly:make-book-part
143 (ly:parser-lookup parser 'toplevel-scores)))
144 (ly:parser-define! parser 'toplevel-scores (list))))
145 (add-bookpart book-part))
147 (define-public (collect-scores-for-book parser score)
149 parser 'toplevel-scores
150 (cons score (ly:parser-lookup parser 'toplevel-scores))))
152 (define-public (collect-music-aux score-handler parser music)
153 (define (music-property symbol)
154 (ly:music-property music symbol #f))
155 (cond ((music-property 'page-marker)
156 ;; a page marker: set page break/turn permissions or label
157 (let ((label (music-property 'page-label)))
159 (score-handler (ly:make-page-label-marker label))))
160 (for-each (lambda (symbol)
161 (let ((permission (music-property symbol)))
162 (if (symbol? permission)
164 (ly:make-page-permission-marker symbol
165 (if (eq? 'forbid permission)
168 '(line-break-permission page-break-permission
169 page-turn-permission)))
170 ((not (music-property 'void))
171 ;; a regular music expression: make a score with this music
172 ;; void music is discarded
173 (score-handler (scorify-music music parser)))))
175 (define-public (collect-music-for-book parser music)
176 "Top-level music handler."
177 (collect-music-aux (lambda (score)
178 (collect-scores-for-book parser score))
182 (define-public (collect-book-music-for-book parser book music)
183 "Book music handler."
184 (collect-music-aux (lambda (score)
185 (ly:book-add-score! book score))
189 (define-public (scorify-music music parser)
190 "Preprocess @var{music}."
192 (fold (lambda (f m) (f m parser))
194 toplevel-music-functions)))
196 (define (get-current-filename parser book)
197 "return any suffix value for output filename allowing for settings by
198 calls to bookOutputName function"
199 (or (paper-variable parser book 'output-filename)
200 (ly:parser-output-name parser)))
202 (define (get-current-suffix parser book)
203 "return any suffix value for output filename allowing for settings by calls to
205 (let ((book-output-suffix (paper-variable parser book 'output-suffix)))
206 (if (not (string? book-output-suffix))
207 (ly:parser-lookup parser 'output-suffix)
208 book-output-suffix)))
210 (define-public current-outfile-name #f) ; for use by regression tests
212 (define (get-outfile-name parser book)
213 "return current filename for generating backend output files"
214 ;; user can now override the base file name, so we have to use
215 ;; the file-name concatenated with any potential output-suffix value
216 ;; as the key to out internal a-list
217 (let* ((base-name (get-current-filename parser book))
218 (output-suffix (get-current-suffix parser book))
219 (alist-key (format #f "~a~a" base-name output-suffix))
220 (counter-alist (ly:parser-lookup parser 'counter-alist))
221 (output-count (assoc-get alist-key counter-alist 0))
223 ;; Allow all ASCII alphanumerics, including accents
224 (if (string? output-suffix)
228 (string-regexp-substitute
233 ;; assoc-get call will always have returned a number
234 (if (> output-count 0)
235 (set! result (format #f "~a-~a" result output-count)))
238 parser 'counter-alist
239 (assoc-set! counter-alist alist-key (1+ output-count)))
240 (set! current-outfile-name result)
243 (define (print-book-with parser book process-procedure)
244 (let* ((paper (ly:parser-lookup parser '$defaultpaper))
245 (layout (ly:parser-lookup parser '$defaultlayout))
246 (outfile-name (get-outfile-name parser book)))
247 (process-procedure book paper layout outfile-name)))
249 (define-public (print-book-with-defaults parser book)
250 (print-book-with parser book ly:book-process))
252 (define-public (print-book-with-defaults-as-systems parser book)
253 (print-book-with parser book ly:book-process-to-systems))
255 ;; Add a score to the current bookpart, book or toplevel
256 (define-public (add-score parser score)
258 ((ly:parser-lookup parser '$current-bookpart)
259 ((ly:parser-lookup parser 'bookpart-score-handler)
260 (ly:parser-lookup parser '$current-bookpart) score))
261 ((ly:parser-lookup parser '$current-book)
262 ((ly:parser-lookup parser 'book-score-handler)
263 (ly:parser-lookup parser '$current-book) score))
265 ((ly:parser-lookup parser 'toplevel-score-handler) parser score))))
267 (define-public paper-variable
270 (lambda (parser book)
271 (append (if (and book (ly:output-def? (ly:book-paper book)))
272 (list (ly:book-paper book))
274 (ly:parser-lookup parser '$papers)
275 (list (ly:parser-lookup parser '$defaultpaper))))))
276 (make-procedure-with-setter
277 (lambda (parser book symbol)
278 (any (lambda (p) (ly:output-def-lookup p symbol #f))
279 (get-papers parser book)))
280 (lambda (parser book symbol value)
281 (ly:output-def-set-variable!
282 (car (get-papers parser book))
285 (define-public (add-text parser text)
286 (add-score parser (list text)))
288 (define-public (add-music parser music)
289 (collect-music-aux (lambda (score)
290 (add-score parser score))
294 (define-public (context-mod-from-music parser music)
295 (let ((warn #t) (mods (ly:make-context-mod)))
296 (let loop ((m music))
297 (if (music-is-of-type? m 'layout-instruction-event)
298 (let ((symbol (ly:music-property m 'symbol)))
301 (case (ly:music-property m 'name)
305 (ly:music-property m 'value)))
307 (list 'unset symbol))
311 (ly:music-property m 'grob-value)
313 ((ly:music-property m 'grob-property #f) => list)
315 (ly:music-property m 'grob-property-path)))))
320 ((ly:music-property m 'grob-property #f) => list)
322 (ly:music-property m 'grob-property-path))))))))
323 (case (ly:music-property m 'name)
325 (ly:add-context-mod mods
327 (ly:music-property m 'procedure))))
328 ((ContextSpeccedMusic)
329 (loop (ly:music-property m 'element)))
331 (let ((callback (ly:music-property m 'elements-callback)))
332 (if (procedure? callback)
333 (for-each loop (callback m))
334 (if (and warn (ly:duration? (ly:music-property m 'duration)))
338 (_ "Music unsuitable for context-mod"))
339 (set! warn #f)))))))))
342 (define-public (context-defs-from-music parser output-def music)
344 (let loop ((m music) (mods #f))
345 ;; The parser turns all sets, overrides etc into something
346 ;; wrapped in ContextSpeccedMusic. If we ever get a set,
347 ;; override etc that is not wrapped in ContextSpeccedMusic, the
348 ;; user has created it in Scheme himself without providing the
349 ;; required wrapping. In that case, using #f in the place of a
350 ;; context modification results in a reasonably recognizable
352 (if (music-is-of-type? m 'layout-instruction-event)
355 (case (ly:music-property m 'name)
358 (ly:music-property m 'symbol)
359 (ly:music-property m 'value)))
362 (ly:music-property m 'symbol)))
365 (ly:music-property m 'symbol)
366 (ly:music-property m 'grob-value)
368 ((ly:music-property m 'grob-property #f) => list)
370 (ly:music-property m 'grob-property-path)))))
373 (ly:music-property m 'symbol)
375 ((ly:music-property m 'grob-property #f) => list)
377 (ly:music-property m 'grob-property-path)))))))
378 (case (ly:music-property m 'name)
380 (ly:add-context-mod mods
382 (ly:music-property m 'procedure))))
383 ((ContextSpeccedMusic)
384 ;; Use let* here to let defs catch up with modifications
385 ;; to the context defs made in the recursion
386 (let* ((mods (loop (ly:music-property m 'element)
387 (ly:make-context-mod)))
388 (defs (ly:output-find-context-def
389 output-def (ly:music-property m 'context-type))))
393 (ly:format (_ "Cannot find context-def \\~a")
394 (ly:music-property m 'context-type)))
397 (ly:output-def-set-variable!
398 output-def (car entry)
399 (ly:context-def-modify (cdr entry) mods)))
402 (let ((callback (ly:music-property m 'elements-callback)))
403 (if (procedure? callback)
404 (fold loop mods (callback m))
405 (if (and warn (ly:duration? (ly:music-property m 'duration)))
409 (_ "Music unsuitable for output-def"))
410 (set! warn #f))))))))
417 (define-public assoc-get ly:assoc-get)
419 (define-public chain-assoc-get ly:chain-assoc-get)
421 (define-public (uniqued-alist alist acc)
422 (if (null? alist) acc
423 (if (assoc (caar alist) acc)
424 (uniqued-alist (cdr alist) acc)
425 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
427 (define-public (alist<? x y)
428 (string<? (symbol->string (car x))
429 (symbol->string (car y))))
431 (define (map-alist-vals func list)
432 "map FUNC over the vals of LIST, leaving the keys."
435 (cons (cons (caar list) (func (cdar list)))
436 (map-alist-vals func (cdr list)))))
438 (define (map-alist-keys func list)
439 "map FUNC over the keys of an alist LIST, leaving the vals."
442 (cons (cons (func (caar list)) (cdar list))
443 (map-alist-keys func (cdr list)))))
445 (define-public (first-member members lst)
446 "Return first successful member (of member) from @var{members} in
448 (any (lambda (m) (member m lst)) members))
450 (define-public (first-assoc keys lst)
451 "Return first successful assoc of key from @var{keys} in @var{lst}."
452 (any (lambda (k) (assoc k lst)) keys))
454 (define-public (flatten-alist alist)
459 (flatten-alist (cdr alist))))))
461 (define-public (map-selected-alist-keys function keys alist)
462 "Return @var{alist} with @var{function} applied to all of the values
467 @code{guile> (map-selected-alist-keys - '(a b) '((a . 1) (b . -2) (c . 3) (d . 4)))}
468 @code{((a . -1) (b . 2) (c . 3) (d . 4)}
470 (define (map-selected-alist-keys-helper key alist)
473 (if (equal? key (car pair))
474 (cons key (function (cdr pair)))
477 (fold map-selected-alist-keys-helper alist keys))
482 (define-public (vector-for-each proc vec)
485 ((>= i (vector-length vec)) vec)
486 (vector-set! vec i (proc (vector-ref vec i)))))
491 (define-public (hash-table->alist t)
492 (hash-fold acons '() t))
494 ;; todo: code dup with C++.
495 (define-safe-public (alist->hash-table lst)
496 "Convert alist to table"
497 (let ((m (make-hash-table (length lst))))
498 (for-each (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
504 (define (functional-or . rest)
507 (define (functional-and . rest)
508 (every identity rest))
510 (define (split-list lst n)
511 "Split LST in N equal sized parts"
513 (define (helper todo acc-vector k)
520 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
521 (helper (cdr todo) acc-vector (1- k)))))
523 (helper lst (make-vector n '()) (1- n)))
525 (define (list-element-index lst x)
526 (list-index (lambda (m) (equal? m x)) lst))
528 (define-public (count-list lst)
529 "Given @var{lst} as @code{(E1 E2 .. )}, return
530 @code{((E1 . 1) (E2 . 2) ... )}."
531 (map cons lst (iota (length lst) 1)))
533 (define-public (list-join lst intermediate)
534 "Put @var{intermediate} between all elts of @var{lst}."
539 (cons elem (cons intermediate prev))
543 (define-public filtered-map filter-map)
545 (define-public (flatten-list x)
547 (let loop ((x x) (tail '()))
548 (cond ((list? x) (fold-right loop tail x))
549 ((not (pair? x)) (cons x tail))
550 (else (loop (car x) (loop (cdr x) tail))))))
552 (define (list-minus a b)
553 "Return list of elements in A that are not in B."
554 (lset-difference eq? a b))
556 (define-public (uniq-list lst)
557 "Uniq @var{lst}, assuming that it is sorted. Uses @code{equal?}
561 (fold (lambda (x acc)
564 (if (equal? x (car acc))
569 (define (split-at-predicate pred lst)
570 "Split LST into two lists at the first element that returns #f for
571 (PRED previous_element element). Return the two parts as a pair.
572 Example: (split-at-predicate < '(1 2 3 2 1)) ==> ((1 2 3) . (2 1))"
573 (let ((i (and (pair? lst)
574 (list-index (lambda (x y) (not (pred x y)))
579 (lambda () (split-at lst (1+ i)))
583 (define-public (split-list-by-separator lst pred)
584 "Split @var{lst} at each element that satisfies @var{pred}, and return
585 the parts (with the separators removed) as a list of lists. For example,
586 executing @samp{(split-list-by-separator '(a 0 b c 1 d) number?)} returns
587 @samp{((a) (b c) (d))}."
588 (call-with-values (lambda () (break pred lst))
593 (split-list-by-separator (cdr tail) pred))))))
595 (define-public (offset-add a b)
596 (cons (+ (car a) (car b))
597 (+ (cdr a) (cdr b))))
599 (define-public (offset-flip-y o)
600 (cons (car o) (- (cdr o))))
602 (define-public (offset-scale o scale)
603 (cons (* (car o) scale)
606 (define-public (ly:list->offsets accum coords)
609 (cons (cons (car coords) (cadr coords))
610 (ly:list->offsets accum (cddr coords)))))
612 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
615 (define-public empty-interval '(+inf.0 . -inf.0))
617 (define-public (symmetric-interval expr)
618 (cons (- expr) expr))
620 (define-public (interval-length x)
621 "Length of the number-pair @var{x}, if an interval."
622 (max 0 (- (cdr x) (car x))))
624 (define-public (ordered-cons a b)
628 (define-public (interval-bound interval dir)
629 ((if (= dir RIGHT) cdr car) interval))
631 (define-public (interval-index interval dir)
632 "Interpolate @var{interval} between between left (@var{dir}=-1) and
633 right (@var{dir}=+1)."
635 (* (+ (interval-start interval) (interval-end interval)
636 (* dir (- (interval-end interval) (interval-start interval))))
639 (define-public (interval-center x)
640 "Center the number-pair @var{x}, if an interval."
641 (if (interval-empty? x)
643 (/ (+ (car x) (cdr x)) 2)))
645 (define-public interval-start car)
647 (define-public interval-end cdr)
649 (define (other-axis a)
650 (remainder (+ a 1) 2))
652 (define-public (interval-scale iv factor)
653 (cons (* (car iv) factor)
654 (* (cdr iv) factor)))
656 (define-public (interval-widen iv amount)
657 (cons (- (car iv) amount)
658 (+ (cdr iv) amount)))
660 (define-public (interval-empty? iv)
661 (> (car iv) (cdr iv)))
663 (define-public (interval-union i1 i2)
665 (min (car i1) (car i2))
666 (max (cdr i1) (cdr i2))))
668 (define-public (interval-intersection i1 i2)
670 (max (car i1) (car i2))
671 (min (cdr i1) (cdr i2))))
673 (define-public (interval-sane? i)
674 (not (or (nan? (car i))
678 (> (car i) (cdr i)))))
680 (define-public (add-point interval p)
681 (cons (min (interval-start interval) p)
682 (max (interval-end interval) p)))
684 (define-public (reverse-interval iv)
685 (cons (cdr iv) (car iv)))
687 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
693 (define (coord-operation operator operand coordinate)
695 (cons (operator (coord-x operand) (coord-x coordinate))
696 (operator (coord-y operand) (coord-y coordinate)))
697 (cons (operator operand (coord-x coordinate))
698 (operator operand (coord-y coordinate)))))
700 (define (coord-apply function coordinate)
703 ((coord-x function) (coord-x coordinate))
704 ((coord-y function) (coord-y coordinate)))
706 (function (coord-x coordinate))
707 (function (coord-y coordinate)))))
709 (define-public (coord-translate coordinate amount)
710 (coord-operation + amount coordinate))
712 (define-public (coord-scale coordinate amount)
713 (coord-operation * amount coordinate))
715 (define-public (coord-rotate coordinate degrees-in-radians)
719 (exact->inexact (coord-x coordinate))
720 (exact->inexact (coord-y coordinate))))
723 (+ (* (coord-x coordinate) (coord-x coordinate))
724 (* (coord-y coordinate) (coord-y coordinate)))))
725 (angle (angle-0-2pi (atan (coord-y coordinate) (coord-x coordinate)))))
727 (* radius (cos (+ angle degrees-in-radians)))
728 (* radius (sin (+ angle degrees-in-radians))))))
730 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
733 (define-public PI (* 4 (atan 1)))
735 (define-public TWO-PI (* 2 PI))
737 (define-public PI-OVER-TWO (/ PI 2))
739 (define-public THREE-PI-OVER-TWO (* 3 PI-OVER-TWO))
741 (define-public (cyclic-base-value value cycle)
742 "Take @var{value} and modulo-maps it between 0 and base @var{cycle}."
744 (cyclic-base-value (+ value cycle) cycle)
746 (cyclic-base-value (- value cycle) cycle)
749 (define-public (angle-0-2pi angle)
750 "Take @var{angle} (in radians) and maps it between 0 and 2pi."
751 (cyclic-base-value angle TWO-PI))
753 (define-public (angle-0-360 angle)
754 "Take @var{angle} (in degrees) and maps it between 0 and 360 degrees."
755 (cyclic-base-value angle 360.0))
757 (define-public PI-OVER-180 (/ PI 180))
759 (define-public (degrees->radians angle-degrees)
760 "Convert the given angle from degrees to radians."
761 (* angle-degrees PI-OVER-180))
763 (define-public (ellipse-radius x-radius y-radius angle)
765 (* x-radius y-radius)
767 (+ (* (expt y-radius 2)
768 (* (cos angle) (cos angle)))
770 (* (sin angle) (sin angle)))))))
772 (define-public (polar->rectangular radius angle-in-degrees)
773 "Return polar coordinates (@var{radius}, @var{angle-in-degrees})
774 as rectangular coordinates @ode{(x-length . y-length)}."
776 (let ((complex (make-polar
778 (degrees->radians angle-in-degrees))))
781 (imag-part complex))))
783 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
786 (define-public (string-endswith s suffix)
787 (equal? suffix (substring s
788 (max 0 (- (string-length s) (string-length suffix)))
791 (define-public (string-startswith s prefix)
792 (equal? prefix (substring s 0 (min (string-length s) (string-length prefix)))))
794 (define-public (string-encode-integer i)
797 ((< i 0) (string-append "n" (string-encode-integer (- i))))
799 (make-string 1 (integer->char (+ 65 (modulo i 26))))
800 (string-encode-integer (quotient i 26))))))
802 (define (number->octal-string x)
803 (let* ((n (inexact->exact x))
804 (n64 (quotient n 64))
805 (n8 (quotient (- n (* n64 64)) 8)))
809 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
811 (define-public (ly:inexact->string x radix)
812 (let ((n (inexact->exact x)))
813 (number->string n radix)))
815 (define-public (ly:number-pair->string c)
816 (string-append (ly:number->string (car c)) " "
817 (ly:number->string (cdr c))))
819 (define-public (dir-basename file . rest)
820 "Strip suffixes in @var{rest}, but leave directory component for
822 (define (inverse-basename x y) (basename y x))
823 (simple-format #f "~a/~a" (dirname file)
824 (fold inverse-basename file rest)))
826 (define-public (write-me message x)
827 "Return @var{x}. Display @var{message} and write @var{x}.
828 Handy for debugging, possibly turned off."
829 (display message) (write x) (newline) x)
832 (define-public (stderr string . rest)
833 (apply format (cons (current-error-port) (cons string rest)))
834 (force-output (current-error-port)))
836 (define-public (debugf string . rest)
838 (apply stderr (cons string rest))))
840 (define (index-cell cell dir)
845 (define (cons-map f x)
846 "map F to contents of X"
847 (cons (f (car x)) (f (cdr x))))
849 (define-public (list-insert-separator lst between)
850 "Create new list, inserting @var{between} between elements of @var{lst}."
854 (cons x (cons between y))))
855 (fold-right conc #f lst))
857 (define-public (string-regexp-substitute a b str)
858 (regexp-substitute/global #f a str 'pre b 'post))
860 (define (regexp-split str regex)
862 (define end-of-prev-match 0)
863 (define (notice match)
865 (set! matches (cons (substring (match:string match)
869 (set! end-of-prev-match (match:end match)))
871 (regexp-substitute/global #f regex str notice 'post)
873 (if (< end-of-prev-match (string-length str))
876 (cons (substring str end-of-prev-match (string-length str)) matches)))
888 (define-public (binary-search start end getter target-val)
889 (_i "Find the index between @var{start} and @var{end} (an integer)
890 which produces the closest match to @var{target-val} if
891 applied to function @var{getter}.")
894 (let* ((compare (quotient (+ start end) 2))
895 (get-val (getter compare)))
897 ((< target-val get-val)
898 (set! end (1- compare)))
899 ((< get-val target-val)
900 (set! start (1+ compare))))
901 (binary-search start end getter target-val))))
903 (define-public (car< a b)
906 (define-public (car<= a b)
907 (<= (car a) (car b)))
909 (define-public (symbol<? lst r)
910 (string<? (symbol->string lst) (symbol->string r)))
912 (define-public (symbol-key<? lst r)
913 (string<? (symbol->string (car lst)) (symbol->string (car r))))
915 (define-public (eval-carefully symbol module . default)
916 "Check whether all symbols in expr @var{symbol} are reachable
917 in module @var{module}. In that case evaluate, otherwise
918 print a warning and set an optional @var{default}."
919 (let* ((unavailable? (lambda (sym)
920 (not (module-defined? module sym))))
924 (filter symbol? (flatten-list symbol)))))
925 (if (null? sym-unavailable)
927 (let* ((def (and (pair? default) (car default))))
928 (ly:programming-error
929 "cannot evaluate ~S in module ~S, setting to ~S"
930 (object->string symbol)
931 (object->string module)
932 (object->string def))
936 ;; don't confuse users with #<procedure .. > syntax.
938 (define-public (scm->string val)
939 (if (and (procedure? val)
940 (symbol? (procedure-name val)))
941 (symbol->string (procedure-name val))
943 (if (self-evaluating? val)
948 (call-with-output-string (lambda (port) (display val port)))
953 (define-public (!= lst r)
956 (define-public lily-unit->bigpoint-factor
958 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
959 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
960 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
962 (define-public lily-unit->mm-factor
963 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
965 ;;; FONT may be font smob, or pango font string...
966 (define-public (font-name-style font)
968 (string-downcase font)
969 (let* ((font-name (ly:font-name font))
970 (full-name (if font-name font-name (ly:font-file-name font))))
971 (string-downcase full-name))))
973 (define-public (modified-font-metric-font-scaling font)
974 (let* ((designsize (ly:font-design-size font))
975 (magnification (* (ly:font-magnification font)))
976 (scaling (* magnification designsize)))
977 (debugf "scaling:~S\n" scaling)
978 (debugf "magnification:~S\n" magnification)
979 (debugf "design:~S\n" designsize)
982 (define-public (version-not-seen-message input-file-name)
984 (ly:format "~a:1" input-file-name)
985 (_ "no \\version statement found, please add~afor future compatibility")
986 (format #f "\n\n\\version ~s\n\n" (lilypond-version))))