1 ;;; lily.scm -- implement Scheme output routines for TeX and PostScript
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2003 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 (use-modules (ice-9 regex)
15 (srfi srfi-13)) ; strings
17 (define-public safe-module (make-safe-module))
19 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
22 ;;; debugging evaluator is slower. This should
23 ;;; have a more sensible default.
26 (if (ly:get-option 'verbose)
29 (debug-enable 'backtrace)
30 (read-enable 'positions) ))
33 (define-public (line-column-location line col file)
34 "Print an input location, including column number ."
35 (string-append (number->string line) ":"
36 (number->string col) " " file)
39 (define-public (line-location line col file)
40 "Print an input location, without column number ."
41 (string-append (number->string line) " " file)
44 (define-public point-and-click #f)
46 (define-public (lilypond-version)
48 (map (lambda (x) (if (symbol? x)
56 ;; cpp hack to get useful error message
57 (define ifdef "First run this through cpp.")
58 (define ifndef "First run this through cpp.")
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66 (define-public START -1)
67 (define-public STOP 1)
68 (define-public LEFT -1)
69 (define-public RIGHT 1)
71 (define-public DOWN -1)
72 (define-public CENTER 0)
74 (define-public DOUBLE-FLAT -4)
75 (define-public THREE-Q-FLAT -3)
76 (define-public FLAT -2)
77 (define-public SEMI-FLAT -1)
78 (define-public NATURAL 0)
79 (define-public SEMI-SHARP 1)
80 (define-public SHARP 2)
81 (define-public THREE-Q-SHARP 3)
82 (define-public DOUBLE-SHARP 4)
83 (define-public SEMI-TONE 2)
85 (define-public ZERO-MOMENT (ly:make-moment 0 1))
87 (define-public (moment-min a b)
88 (if (ly:moment<? a b) a b))
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 ;; lily specific variables.
92 (define-public default-script-alist '())
94 (define-public security-paranoia #f)
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 ;;; Unassorted utility functions.
102 (define (uniqued-alist alist acc)
103 (if (null? alist) acc
104 (if (assoc (caar alist) acc)
105 (uniqued-alist (cdr alist) acc)
106 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
109 (define (assoc-get key alist)
110 "Return value if KEY in ALIST, else #f."
111 (let ((entry (assoc key alist)))
112 (if entry (cdr entry) #f)))
114 (define (assoc-get-default key alist default)
115 "Return value if KEY in ALIST, else DEFAULT."
116 (let ((entry (assoc key alist)))
117 (if entry (cdr entry) default)))
120 (define-public (uniqued-alist alist acc)
121 (if (null? alist) acc
122 (if (assoc (caar alist) acc)
123 (uniqued-alist (cdr alist) acc)
124 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
126 (define-public (alist<? x y)
127 (string<? (symbol->string (car x))
128 (symbol->string (car y))))
132 (define (chain-assoc x alist-list)
133 (if (null? alist-list)
135 (let* ((handle (assoc x (car alist-list))))
138 (chain-assoc x (cdr alist-list))))))
140 (define (chain-assoc-get x alist-list default)
141 (if (null? alist-list)
143 (let* ((handle (assoc x (car alist-list))))
146 (chain-assoc-get x (cdr alist-list) default)))))
149 (define (map-alist-vals func list)
150 "map FUNC over the vals of LIST, leaving the keys."
153 (cons (cons (caar list) (func (cdar list)))
154 (map-alist-vals func (cdr list)))
157 (define (map-alist-keys func list)
158 "map FUNC over the keys of an alist LIST, leaving the vals. "
161 (cons (cons (func (caar list)) (cdar list))
162 (map-alist-keys func (cdr list)))
170 (if (not (defined? 'hash-table?)) ; guile 1.6 compat
172 (define hash-table? vector?)
174 (define-public (hash-table->alist t)
175 "Convert table t to list"
182 (define-public (hash-table->alist t)
184 (hash-fold (lambda (k v acc) (acons k v acc))
189 ;; todo: code dup with C++.
190 (define-public (alist->hash-table l)
191 "Convert alist to table"
193 ((m (make-hash-table (length l))))
196 (hashq-set! m (car k-v) (cdr k-v)))
206 (define (flatten-list lst)
210 (if (pair? (car lst))
211 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
212 (cons (car lst) (flatten-list (cdr lst))))
215 (define (list-minus a b)
216 "Return list of elements in A that are not in B."
217 (lset-difference eq? a b))
220 ;; TODO: use the srfi-1 partition function.
221 (define-public (uniq-list l)
223 "Uniq LIST, assuming that it is sorted"
224 (define (helper acc l)
229 (if (equal? (car l) (cadr l))
231 (helper (cons (car l) acc) (cdr l)))
233 (reverse! (helper '() l) '()))
236 (define (split-at-predicate predicate l)
237 "Split L = (a_1 a_2 ... a_k b_1 ... b_k)
238 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
239 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
240 L1 is copied, L2 not.
242 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
247 (define (inner-split predicate l acc)
251 (set-car! acc (cons (car l) (car acc)))
253 ((predicate (car l) (cadr l))
254 (set-car! acc (cons (car l) (car acc)))
255 (inner-split predicate (cdr l) acc))
257 (set-car! acc (cons (car l) (car acc)))
258 (set-cdr! acc (cdr l))
265 (inner-split predicate l c)
266 (set-car! c (reverse! (car c)))
271 (define-public (split-list l sep?)
273 (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
275 ((a b c) (d e f) (g))
280 (define (split-one sep? l acc)
281 "Split off the first parts before separator and return both parts."
286 (split-one sep? (cdr l) (cons (car l) acc))
292 (let* ((c (split-one sep? l '())))
293 (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
297 (define-public (interval-length x)
298 "Length of the number-pair X, when an interval"
299 (max 0 (- (cdr x) (car x)))
303 (define (other-axis a)
304 (remainder (+ a 1) 2))
307 (define-public (interval-widen iv amount)
308 (cons (- (car iv) amount)
309 (+ (cdr iv) amount)))
311 (define-public (interval-union i1 i2)
312 (cons (min (car i1) (car i2))
313 (max (cdr i1) (cdr i2))))
316 (define-public (write-me message x)
317 "Return X. Display MESSAGE and write X. Handy for debugging, possibly turned off."
318 (display message) (write x) (newline) x)
321 (define (index-cell cell dir)
326 (define (cons-map f x)
327 "map F to contents of X"
328 (cons (f (car x)) (f (cdr x))))
331 (define-public (list-insert-separator lst between)
332 "Create new list, inserting BETWEEN between elements of LIST"
336 (cons x (cons between y))
338 (fold-right conc #f lst))
347 (define-public (symbol<? l r)
348 (string<? (symbol->string l) (symbol->string r)))
350 (define-public (!= l r)
353 (define-public (ly:load x)
355 (fn (%search-load-path x))
358 (if (ly:get-option 'verbose)
359 (format (current-error-port) "[~A]" fn))
360 (primitive-load fn)))
363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
365 (use-modules (scm output-tex)
368 (scm output-sodipodi)
374 ("tex" . ("TeX output. The default output form." ,tex-output-expression))
375 ("ps" . ("Direct postscript. Requires setting GS_LIB and GS_FONTPATH" ,ps-output-expression))
376 ("scm" . ("Scheme dump: debug scheme molecule expressions" ,write))
377 ("sketch" . ("Bare bones Sketch output." ,sketch-output-expression))
378 ("sodipodi" . ("Bare bones Sodipodi output." ,sodipodi-output-expression))
379 ("pdftex" . ("PDFTeX output. Was last seen nonfunctioning." ,pdftex-output-expression))
383 (define (document-format-dumpers)
386 (display (string-append (pad-string-to 5 (car x)) (cadr x) "\n"))
390 (define-public (find-dumper format )
392 ((d (assoc format output-alist)))
396 (scm-error "Could not find dumper for format ~s" format))
399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
404 '("define-music-types.scm"
407 "chord-ignatzek-names.scm"
409 "chord-generic-names.scm"
413 "music-functions.scm"
415 "define-music-properties.scm"
419 "define-translator-properties.scm"
420 "translation-functions.scm"
429 "define-grob-properties.scm"
431 "define-grob-interfaces.scm"
440 (set! type-p-name-alist
442 (,boolean-or-symbol? . "boolean or symbol")
443 (,boolean? . "boolean")
445 (,grob-list? . "list of grobs")
446 (,input-port? . "input port")
447 (,integer? . "integer")
449 (,ly:context? . "context")
450 (,ly:dimension? . "dimension, in staff space")
451 (,ly:dir? . "direction")
452 (,ly:duration? . "duration")
453 (,ly:grob? . "grob (GRaphical OBject)")
454 (,ly:input-location? . "input location")
455 (,ly:input-location? . "input location")
456 (,ly:moment? . "moment")
457 (,ly:music? . "music")
458 (,ly:pitch? . "pitch")
459 (,ly:translator? . "translator")
460 (,markup-list? . "list of markups")
461 (,markup? . "markup")
462 (,music-list? . "list of music")
463 (,number-or-grob? . "number or grob")
464 (,number-or-string? . "number or string")
465 (,number-pair? . "pair of numbers")
466 (,number? . "number")
467 (,output-port? . "output port")
469 (,procedure? . "procedure")
470 (,scheme? . "any type")
471 (,string? . "string")
472 (,symbol? . "symbol")
473 (,vector? . "vector")
479 (define gc-protect-stat-count 0)
480 (define-public (dump-gc-protects)
481 (set! gc-protect-stat-count (1+ gc-protect-stat-count) )
491 (map object->string (list (object-address x) c x))
496 (hash-table->alist (ly:protects))
498 (< (object-address (car a))
499 (object-address (car b)))))
502 (open-file (string-append
503 "gcstat-" (number->string gc-protect-stat-count)