1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 1998--2014 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 (define-public (unity-if-multimeasure context dur)
125 "Given a context and a duration, return @code{1} if the duration is
126 longer than the @code{measureLength} in that context, and @code{#f} otherwise.
127 This supports historic use of @code{Completion_heads_engraver} to split
128 @code{c1*3} into three whole notes."
129 (if (ly:moment<? (ly:context-property context 'measureLength)
130 (ly:duration-length dur))
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136 (define-public (average x . lst)
137 (/ (+ x (apply + lst)) (1+ (length lst))))
139 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
140 ;; parser <-> output hooks.
142 (define-public (collect-bookpart-for-book parser book-part)
143 "Toplevel book-part handler."
144 (define (add-bookpart book-part)
146 parser 'toplevel-bookparts
147 (cons book-part (ly:parser-lookup parser 'toplevel-bookparts))))
148 ;; If toplevel scores have been found before this \bookpart,
149 ;; add them first to a dedicated bookpart
150 (if (pair? (ly:parser-lookup parser 'toplevel-scores))
152 (add-bookpart (ly:make-book-part
153 (ly:parser-lookup parser 'toplevel-scores)))
154 (ly:parser-define! parser 'toplevel-scores (list))))
155 (add-bookpart book-part))
157 (define-public (collect-scores-for-book parser score)
159 parser 'toplevel-scores
160 (cons score (ly:parser-lookup parser 'toplevel-scores))))
162 (define-public (collect-music-aux score-handler parser music)
163 (define (music-property symbol)
164 (ly:music-property music symbol #f))
165 (cond ((music-property 'page-marker)
166 ;; a page marker: set page break/turn permissions or label
167 (let ((label (music-property 'page-label)))
169 (score-handler (ly:make-page-label-marker label))))
170 (for-each (lambda (symbol)
171 (let ((permission (music-property symbol)))
172 (if (symbol? permission)
174 (ly:make-page-permission-marker symbol
175 (if (eq? 'forbid permission)
178 '(line-break-permission page-break-permission
179 page-turn-permission)))
180 ((not (music-property 'void))
181 ;; a regular music expression: make a score with this music
182 ;; void music is discarded
183 (score-handler (scorify-music music parser)))))
185 (define-public (collect-music-for-book parser music)
186 "Top-level music handler."
187 (collect-music-aux (lambda (score)
188 (collect-scores-for-book parser score))
192 (define-public (collect-book-music-for-book parser book music)
193 "Book music handler."
194 (collect-music-aux (lambda (score)
195 (ly:book-add-score! book score))
199 (define-public (scorify-music music parser)
200 "Preprocess @var{music}."
202 (fold (lambda (f m) (f m parser))
204 toplevel-music-functions)))
206 (define (get-current-filename parser book)
207 "return any suffix value for output filename allowing for settings by
208 calls to bookOutputName function"
209 (or (paper-variable parser book 'output-filename)
210 (ly:parser-output-name parser)))
212 (define (get-current-suffix parser book)
213 "return any suffix value for output filename allowing for settings by calls to
215 (let ((book-output-suffix (paper-variable parser book 'output-suffix)))
216 (if (not (string? book-output-suffix))
217 (ly:parser-lookup parser 'output-suffix)
218 book-output-suffix)))
220 (define-public current-outfile-name #f) ; for use by regression tests
222 (define (get-outfile-name parser book)
223 "return current filename for generating backend output files"
224 ;; user can now override the base file name, so we have to use
225 ;; the file-name concatenated with any potential output-suffix value
226 ;; as the key to out internal a-list
227 (let* ((base-name (get-current-filename parser book))
228 (output-suffix (get-current-suffix parser book))
229 (alist-key (format #f "~a~a" base-name output-suffix))
230 (counter-alist (ly:parser-lookup parser 'counter-alist))
231 (output-count (assoc-get alist-key counter-alist 0))
233 ;; Allow all ASCII alphanumerics, including accents
234 (if (string? output-suffix)
238 (string-regexp-substitute
243 ;; assoc-get call will always have returned a number
244 (if (> output-count 0)
245 (set! result (format #f "~a-~a" result output-count)))
248 parser 'counter-alist
249 (assoc-set! counter-alist alist-key (1+ output-count)))
250 (set! current-outfile-name result)
253 (define (print-book-with parser book process-procedure)
254 (let* ((paper (ly:parser-lookup parser '$defaultpaper))
255 (layout (ly:parser-lookup parser '$defaultlayout))
256 (outfile-name (get-outfile-name parser book)))
257 (process-procedure book paper layout outfile-name)))
259 (define-public (print-book-with-defaults parser book)
260 (print-book-with parser book ly:book-process))
262 (define-public (print-book-with-defaults-as-systems parser book)
263 (print-book-with parser book ly:book-process-to-systems))
265 ;; Add a score to the current bookpart, book or toplevel
266 (define-public (add-score parser score)
268 ((ly:parser-lookup parser '$current-bookpart)
269 ((ly:parser-lookup parser 'bookpart-score-handler)
270 (ly:parser-lookup parser '$current-bookpart) score))
271 ((ly:parser-lookup parser '$current-book)
272 ((ly:parser-lookup parser 'book-score-handler)
273 (ly:parser-lookup parser '$current-book) score))
275 ((ly:parser-lookup parser 'toplevel-score-handler) parser score))))
277 (define-public paper-variable
280 (lambda (parser book)
281 (append (if (and book (ly:output-def? (ly:book-paper book)))
282 (list (ly:book-paper book))
284 (ly:parser-lookup parser '$papers)
285 (list (ly:parser-lookup parser '$defaultpaper))))))
286 (make-procedure-with-setter
287 (lambda (parser book symbol)
288 (any (lambda (p) (ly:output-def-lookup p symbol #f))
289 (get-papers parser book)))
290 (lambda (parser book symbol value)
291 (ly:output-def-set-variable!
292 (car (get-papers parser book))
295 (define-public (add-text parser text)
296 (add-score parser (list text)))
298 (define-public (add-music parser music)
299 (collect-music-aux (lambda (score)
300 (add-score parser score))
304 (define-public (context-mod-from-music parser music)
305 (let ((warn #t) (mods (ly:make-context-mod)))
306 (let loop ((m music))
307 (if (music-is-of-type? m 'layout-instruction-event)
308 (let ((symbol (ly:music-property m 'symbol)))
311 (case (ly:music-property m 'name)
315 (ly:music-property m 'value)))
317 (list 'unset symbol))
321 (ly:music-property m 'grob-value)
323 ((ly:music-property m 'grob-property #f) => list)
325 (ly:music-property m 'grob-property-path)))))
330 ((ly:music-property m 'grob-property #f) => list)
332 (ly:music-property m 'grob-property-path))))))))
333 (case (ly:music-property m 'name)
335 (ly:add-context-mod mods
337 (ly:music-property m 'procedure))))
338 ((ContextSpeccedMusic)
339 (loop (ly:music-property m 'element)))
341 (let ((callback (ly:music-property m 'elements-callback)))
342 (if (procedure? callback)
343 (for-each loop (callback m))
344 (if (and warn (ly:duration? (ly:music-property m 'duration)))
348 (_ "Music unsuitable for context-mod"))
349 (set! warn #f)))))))))
352 (define-public (context-defs-from-music parser output-def music)
354 (let loop ((m music) (mods #f))
355 ;; The parser turns all sets, overrides etc into something
356 ;; wrapped in ContextSpeccedMusic. If we ever get a set,
357 ;; override etc that is not wrapped in ContextSpeccedMusic, the
358 ;; user has created it in Scheme himself without providing the
359 ;; required wrapping. In that case, using #f in the place of a
360 ;; context modification results in a reasonably recognizable
362 (if (music-is-of-type? m 'layout-instruction-event)
365 (case (ly:music-property m 'name)
368 (ly:music-property m 'symbol)
369 (ly:music-property m 'value)))
372 (ly:music-property m 'symbol)))
375 (ly:music-property m 'symbol)
376 (ly:music-property m 'grob-value)
378 ((ly:music-property m 'grob-property #f) => list)
380 (ly:music-property m 'grob-property-path)))))
383 (ly:music-property m 'symbol)
385 ((ly:music-property m 'grob-property #f) => list)
387 (ly:music-property m 'grob-property-path)))))))
388 (case (ly:music-property m 'name)
390 (ly:add-context-mod mods
392 (ly:music-property m 'procedure))))
393 ((ContextSpeccedMusic)
394 ;; Use let* here to let defs catch up with modifications
395 ;; to the context defs made in the recursion
396 (let* ((mods (loop (ly:music-property m 'element)
397 (ly:make-context-mod)))
398 (defs (ly:output-find-context-def
399 output-def (ly:music-property m 'context-type))))
403 (ly:format (_ "Cannot find context-def \\~a")
404 (ly:music-property m 'context-type)))
407 (ly:output-def-set-variable!
408 output-def (car entry)
409 (ly:context-def-modify (cdr entry) mods)))
412 (let ((callback (ly:music-property m 'elements-callback)))
413 (if (procedure? callback)
414 (fold loop mods (callback m))
415 (if (and warn (ly:duration? (ly:music-property m 'duration)))
419 (_ "Music unsuitable for output-def"))
420 (set! warn #f))))))))
427 (define-public assoc-get ly:assoc-get)
429 (define-public chain-assoc-get ly:chain-assoc-get)
431 (define-public (uniqued-alist alist acc)
432 (if (null? alist) acc
433 (if (assoc (caar alist) acc)
434 (uniqued-alist (cdr alist) acc)
435 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
437 (define-public (alist<? x y)
438 (string<? (symbol->string (car x))
439 (symbol->string (car y))))
441 (define (map-alist-vals func list)
442 "map FUNC over the vals of LIST, leaving the keys."
445 (cons (cons (caar list) (func (cdar list)))
446 (map-alist-vals func (cdr list)))))
448 (define (map-alist-keys func list)
449 "map FUNC over the keys of an alist LIST, leaving the vals."
452 (cons (cons (func (caar list)) (cdar list))
453 (map-alist-keys func (cdr list)))))
455 (define-public (first-member members lst)
456 "Return first successful member (of member) from @var{members} in
458 (any (lambda (m) (member m lst)) members))
460 (define-public (first-assoc keys lst)
461 "Return first successful assoc of key from @var{keys} in @var{lst}."
462 (any (lambda (k) (assoc k lst)) keys))
464 (define-public (flatten-alist alist)
469 (flatten-alist (cdr alist))))))
471 (define-public (map-selected-alist-keys function keys alist)
472 "Return @var{alist} with @var{function} applied to all of the values
477 @code{guile> (map-selected-alist-keys - '(a b) '((a . 1) (b . -2) (c . 3) (d . 4)))}
478 @code{((a . -1) (b . 2) (c . 3) (d . 4)}
480 (define (map-selected-alist-keys-helper key alist)
483 (if (equal? key (car pair))
484 (cons key (function (cdr pair)))
487 (fold map-selected-alist-keys-helper alist keys))
492 (define-public (vector-for-each proc vec)
495 ((>= i (vector-length vec)) vec)
496 (vector-set! vec i (proc (vector-ref vec i)))))
501 (define-public (hash-table->alist t)
502 (hash-fold acons '() t))
504 ;; todo: code dup with C++.
505 (define-safe-public (alist->hash-table lst)
506 "Convert alist to table"
507 (let ((m (make-hash-table (length lst))))
508 (for-each (lambda (k-v) (hashq-set! m (car k-v) (cdr k-v))) lst)
514 (define (functional-or . rest)
517 (define (functional-and . rest)
518 (every identity rest))
520 (define (split-list lst n)
521 "Split LST in N equal sized parts"
523 (define (helper todo acc-vector k)
530 (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k)))
531 (helper (cdr todo) acc-vector (1- k)))))
533 (helper lst (make-vector n '()) (1- n)))
535 (define (list-element-index lst x)
536 (list-index (lambda (m) (equal? m x)) lst))
538 (define-public (count-list lst)
539 "Given @var{lst} as @code{(E1 E2 .. )}, return
540 @code{((E1 . 1) (E2 . 2) ... )}."
541 (map cons lst (iota (length lst) 1)))
543 (define-public (list-join lst intermediate)
544 "Put @var{intermediate} between all elts of @var{lst}."
549 (cons elem (cons intermediate prev))
553 (define-public filtered-map filter-map)
555 (define-public (flatten-list x)
557 (let loop ((x x) (tail '()))
558 (cond ((list? x) (fold-right loop tail x))
559 ((not (pair? x)) (cons x tail))
560 (else (loop (car x) (loop (cdr x) tail))))))
562 (define (list-minus a b)
563 "Return list of elements in A that are not in B."
564 (lset-difference eq? a b))
566 (define-public (uniq-list lst)
567 "Uniq @var{lst}, assuming that it is sorted. Uses @code{equal?}
571 (fold (lambda (x acc)
574 (if (equal? x (car acc))
579 (define (split-at-predicate pred lst)
580 "Split LST into two lists at the first element that returns #f for
581 (PRED previous_element element). Return the two parts as a pair.
582 Example: (split-at-predicate < '(1 2 3 2 1)) ==> ((1 2 3) . (2 1))"
583 (let ((i (and (pair? lst)
584 (list-index (lambda (x y) (not (pred x y)))
589 (lambda () (split-at lst (1+ i)))
593 (define-public (split-list-by-separator lst pred)
594 "Split @var{lst} at each element that satisfies @var{pred}, and return
595 the parts (with the separators removed) as a list of lists. For example,
596 executing @samp{(split-list-by-separator '(a 0 b c 1 d) number?)} returns
597 @samp{((a) (b c) (d))}."
598 (call-with-values (lambda () (break pred lst))
603 (split-list-by-separator (cdr tail) pred))))))
605 (define-public (offset-add a b)
606 (cons (+ (car a) (car b))
607 (+ (cdr a) (cdr b))))
609 (define-public (offset-flip-y o)
610 (cons (car o) (- (cdr o))))
612 (define-public (offset-scale o scale)
613 (cons (* (car o) scale)
616 (define-public (ly:list->offsets accum coords)
619 (cons (cons (car coords) (cadr coords))
620 (ly:list->offsets accum (cddr coords)))))
622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
625 (define-public empty-interval '(+inf.0 . -inf.0))
627 (define-public (symmetric-interval expr)
628 (cons (- expr) expr))
630 (define-public (interval-length x)
631 "Length of the number-pair @var{x}, if an interval."
632 (max 0 (- (cdr x) (car x))))
634 (define-public (ordered-cons a b)
638 (define-public (interval-bound interval dir)
639 ((if (= dir RIGHT) cdr car) interval))
641 (define-public (interval-index interval dir)
642 "Interpolate @var{interval} between between left (@var{dir}=-1) and
643 right (@var{dir}=+1)."
645 (* (+ (interval-start interval) (interval-end interval)
646 (* dir (- (interval-end interval) (interval-start interval))))
649 (define-public (interval-center x)
650 "Center the number-pair @var{x}, if an interval."
651 (if (interval-empty? x)
653 (/ (+ (car x) (cdr x)) 2)))
655 (define-public interval-start car)
657 (define-public interval-end cdr)
659 (define (other-axis a)
660 (remainder (+ a 1) 2))
662 (define-public (interval-scale iv factor)
663 (cons (* (car iv) factor)
664 (* (cdr iv) factor)))
666 (define-public (interval-widen iv amount)
667 (cons (- (car iv) amount)
668 (+ (cdr iv) amount)))
670 (define-public (interval-empty? iv)
671 (> (car iv) (cdr iv)))
673 (define-public (interval-union i1 i2)
675 (min (car i1) (car i2))
676 (max (cdr i1) (cdr i2))))
678 (define-public (interval-intersection i1 i2)
680 (max (car i1) (car i2))
681 (min (cdr i1) (cdr i2))))
683 (define-public (interval-sane? i)
684 (not (or (nan? (car i))
688 (> (car i) (cdr i)))))
690 (define-public (add-point interval p)
691 (cons (min (interval-start interval) p)
692 (max (interval-end interval) p)))
694 (define-public (reverse-interval iv)
695 (cons (cdr iv) (car iv)))
697 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
703 (define (coord-operation operator operand coordinate)
705 (cons (operator (coord-x operand) (coord-x coordinate))
706 (operator (coord-y operand) (coord-y coordinate)))
707 (cons (operator operand (coord-x coordinate))
708 (operator operand (coord-y coordinate)))))
710 (define (coord-apply function coordinate)
713 ((coord-x function) (coord-x coordinate))
714 ((coord-y function) (coord-y coordinate)))
716 (function (coord-x coordinate))
717 (function (coord-y coordinate)))))
719 (define-public (coord-translate coordinate amount)
720 (coord-operation + amount coordinate))
722 (define-public (coord-scale coordinate amount)
723 (coord-operation * amount coordinate))
725 (define-public (coord-rotate coordinate degrees-in-radians)
729 (exact->inexact (coord-x coordinate))
730 (exact->inexact (coord-y coordinate))))
733 (+ (* (coord-x coordinate) (coord-x coordinate))
734 (* (coord-y coordinate) (coord-y coordinate)))))
735 (angle (angle-0-2pi (atan (coord-y coordinate) (coord-x coordinate)))))
737 (* radius (cos (+ angle degrees-in-radians)))
738 (* radius (sin (+ angle degrees-in-radians))))))
740 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
743 (define-public PI (* 4 (atan 1)))
745 (define-public TWO-PI (* 2 PI))
747 (define-public PI-OVER-TWO (/ PI 2))
749 (define-public THREE-PI-OVER-TWO (* 3 PI-OVER-TWO))
751 (define-public (cyclic-base-value value cycle)
752 "Take @var{value} and modulo-maps it between 0 and base @var{cycle}."
754 (cyclic-base-value (+ value cycle) cycle)
756 (cyclic-base-value (- value cycle) cycle)
759 (define-public (angle-0-2pi angle)
760 "Take @var{angle} (in radians) and maps it between 0 and 2pi."
761 (cyclic-base-value angle TWO-PI))
763 (define-public (angle-0-360 angle)
764 "Take @var{angle} (in degrees) and maps it between 0 and 360 degrees."
765 (cyclic-base-value angle 360.0))
767 (define-public PI-OVER-180 (/ PI 180))
769 (define-public (degrees->radians angle-degrees)
770 "Convert the given angle from degrees to radians."
771 (* angle-degrees PI-OVER-180))
773 (define-public (ellipse-radius x-radius y-radius angle)
775 (* x-radius y-radius)
777 (+ (* (expt y-radius 2)
778 (* (cos angle) (cos angle)))
780 (* (sin angle) (sin angle)))))))
782 (define-public (polar->rectangular radius angle-in-degrees)
783 "Return polar coordinates (@var{radius}, @var{angle-in-degrees})
784 as rectangular coordinates @ode{(x-length . y-length)}."
786 (let ((complex (make-polar
788 (degrees->radians angle-in-degrees))))
791 (imag-part complex))))
793 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
796 (define-public (string-endswith s suffix)
797 (equal? suffix (substring s
798 (max 0 (- (string-length s) (string-length suffix)))
801 (define-public (string-startswith s prefix)
802 (equal? prefix (substring s 0 (min (string-length s) (string-length prefix)))))
804 (define-public (string-encode-integer i)
807 ((< i 0) (string-append "n" (string-encode-integer (- i))))
809 (make-string 1 (integer->char (+ 65 (modulo i 26))))
810 (string-encode-integer (quotient i 26))))))
812 (define (number->octal-string x)
813 (let* ((n (inexact->exact x))
814 (n64 (quotient n 64))
815 (n8 (quotient (- n (* n64 64)) 8)))
819 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
821 (define-public (ly:inexact->string x radix)
822 (let ((n (inexact->exact x)))
823 (number->string n radix)))
825 (define-public (ly:number-pair->string c)
826 (string-append (ly:number->string (car c)) " "
827 (ly:number->string (cdr c))))
829 (define-public (dir-basename file . rest)
830 "Strip suffixes in @var{rest}, but leave directory component for
832 (define (inverse-basename x y) (basename y x))
833 (simple-format #f "~a/~a" (dirname file)
834 (fold inverse-basename file rest)))
836 (define-public (write-me message x)
837 "Return @var{x}. Display @var{message} and write @var{x}.
838 Handy for debugging, possibly turned off."
839 (display message) (write x) (newline) x)
842 (define-public (stderr string . rest)
843 (apply format (cons (current-error-port) (cons string rest)))
844 (force-output (current-error-port)))
846 (define-public (debugf string . rest)
848 (apply stderr (cons string rest))))
850 (define (index-cell cell dir)
855 (define (cons-map f x)
856 "map F to contents of X"
857 (cons (f (car x)) (f (cdr x))))
859 (define-public (list-insert-separator lst between)
860 "Create new list, inserting @var{between} between elements of @var{lst}."
864 (cons x (cons between y))))
865 (fold-right conc #f lst))
867 (define-public (string-regexp-substitute a b str)
868 (regexp-substitute/global #f a str 'pre b 'post))
870 (define (regexp-split str regex)
872 (define end-of-prev-match 0)
873 (define (notice match)
875 (set! matches (cons (substring (match:string match)
879 (set! end-of-prev-match (match:end match)))
881 (regexp-substitute/global #f regex str notice 'post)
883 (if (< end-of-prev-match (string-length str))
886 (cons (substring str end-of-prev-match (string-length str)) matches)))
898 (define-public (binary-search start end getter target-val)
899 (_i "Find the index between @var{start} and @var{end} (an integer)
900 which produces the closest match to @var{target-val} if
901 applied to function @var{getter}.")
904 (let* ((compare (quotient (+ start end) 2))
905 (get-val (getter compare)))
907 ((< target-val get-val)
908 (set! end (1- compare)))
909 ((< get-val target-val)
910 (set! start (1+ compare))))
911 (binary-search start end getter target-val))))
913 (define-public (car< a b)
916 (define-public (car<= a b)
917 (<= (car a) (car b)))
919 (define-public (symbol<? lst r)
920 (string<? (symbol->string lst) (symbol->string r)))
922 (define-public (symbol-key<? lst r)
923 (string<? (symbol->string (car lst)) (symbol->string (car r))))
925 (define-public (eval-carefully symbol module . default)
926 "Check whether all symbols in expr @var{symbol} are reachable
927 in module @var{module}. In that case evaluate, otherwise
928 print a warning and set an optional @var{default}."
929 (let* ((unavailable? (lambda (sym)
930 (not (module-defined? module sym))))
934 (filter symbol? (flatten-list symbol)))))
935 (if (null? sym-unavailable)
937 (let* ((def (and (pair? default) (car default))))
938 (ly:programming-error
939 "cannot evaluate ~S in module ~S, setting to ~S"
940 (object->string symbol)
941 (object->string module)
942 (object->string def))
946 ;; don't confuse users with #<procedure .. > syntax.
948 (define-public (scm->string val)
949 (if (and (procedure? val)
950 (symbol? (procedure-name val)))
951 (symbol->string (procedure-name val))
953 (if (self-evaluating? val)
958 (call-with-output-string (lambda (port) (display val port)))
963 (define-public (!= lst r)
966 (define-public lily-unit->bigpoint-factor
968 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
969 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
970 (else (ly:error (_ "unknown unit: ~S") (ly:unit)))))
972 (define-public lily-unit->mm-factor
973 (* 25.4 (/ lily-unit->bigpoint-factor 72)))
975 ;;; FONT may be font smob, or pango font string...
976 (define-public (font-name-style font)
978 (string-downcase font)
979 (let* ((font-name (ly:font-name font))
980 (full-name (if font-name font-name (ly:font-file-name font))))
981 (string-downcase full-name))))
983 (define-public (modified-font-metric-font-scaling font)
984 (let* ((designsize (ly:font-design-size font))
985 (magnification (* (ly:font-magnification font)))
986 (scaling (* magnification designsize)))
987 (debugf "scaling:~S\n" scaling)
988 (debugf "magnification:~S\n" magnification)
989 (debugf "design:~S\n" designsize)
992 (define-public (version-not-seen-message input-file-name)
994 (ly:format "~a:1" input-file-name)
995 (_ "no \\version statement found, please add~afor future compatibility")
996 (format #f "\n\n\\version ~s\n\n" (lilypond-version))))