1 ;;;; lily.scm -- implement Scheme output routines for TeX and PostScript
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 (if (defined? 'set-debug-cell-accesses!)
12 (set-debug-cell-accesses! #f))
14 (use-modules (ice-9 regex)
18 (srfi srfi-13)) ; strings
20 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
23 ;;; debugging evaluator is slower. This should
24 ;;; have a more sensible default.
26 (if (ly:get-option 'verbose)
29 (debug-enable 'backtrace)
30 (read-enable 'positions)))
32 (define-public (line-column-location file line col)
33 "Print an input location, including column number ."
34 (string-append (number->string line) ":"
35 (number->string col) " " file))
37 (define-public (line-location file line col)
38 "Print an input location, without column number ."
39 (string-append (number->string line) " " file))
41 (define-public point-and-click #f)
43 (define-public parser #f)
45 (define-public (lilypond-version)
47 (map (lambda (x) (if (symbol? x)
55 ;; cpp hack to get useful error message
56 (define ifdef "First run this through cpp.")
57 (define ifndef "First run this through cpp.")
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65 (define-public START -1)
66 (define-public STOP 1)
67 (define-public LEFT -1)
68 (define-public RIGHT 1)
70 (define-public DOWN -1)
71 (define-public CENTER 0)
73 (define-public DOUBLE-FLAT -4)
74 (define-public THREE-Q-FLAT -3)
75 (define-public FLAT -2)
76 (define-public SEMI-FLAT -1)
77 (define-public NATURAL 0)
78 (define-public SEMI-SHARP 1)
79 (define-public SHARP 2)
80 (define-public THREE-Q-SHARP 3)
81 (define-public DOUBLE-SHARP 4)
82 (define-public SEMI-TONE 2)
84 (define-public ZERO-MOMENT (ly:make-moment 0 1))
86 (define-public (moment-min a b)
87 (if (ly:moment<? a b) a b))
89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90 ;; lily specific variables.
92 (define-public default-script-alist '())
96 (define-public (print-music-as-book parser music)
97 (let* ((head (ly:parser-lookup parser '$globalheader))
98 (book (ly:make-book (ly:parser-lookup parser $defaultbookpaper)
100 (ly:parser-print-book parser book)))
102 (define-public (print-score-as-book parser score)
104 ((head (ly:parser-lookup parser '$globalheader))
105 (book (ly:make-book (ly:parser-lookup parser $defaultbookpaper)
107 (ly:parser-print-book parser book)))
109 (define-public (print-score parser score)
110 (let* ((head (ly:parser-lookup parser '$globalheader))
111 (book (ly:make-book (ly:parser-lookup parser $defaultbookpaper)
113 (ly:parser-print-score parser book)))
115 (define-public (collect-scores-for-book parser score)
117 ((oldval (ly:parser-lookup parser 'toplevel-scores)))
118 (ly:parser-define parser 'toplevel-scores (cons score oldval))
121 (define-public (collect-music-for-book parser music)
122 (collect-scores-for-book parser (ly:music-scorify music)))
128 (define-public (assoc-get key alist . default)
129 "Return value if KEY in ALIST, else DEFAULT (or #f if not specified)."
130 (let ((entry (assoc key alist)))
133 (if (pair? default) (car default) #f))))
135 (define-public (uniqued-alist alist acc)
136 (if (null? alist) acc
137 (if (assoc (caar alist) acc)
138 (uniqued-alist (cdr alist) acc)
139 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
141 (define-public (alist<? x y)
142 (string<? (symbol->string (car x))
143 (symbol->string (car y))))
145 (define-public (chain-assoc x alist-list)
146 (if (null? alist-list)
148 (let* ((handle (assoc x (car alist-list))))
151 (chain-assoc x (cdr alist-list))))))
153 (define-public (chain-assoc-get x alist-list . default)
154 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
157 (define (helper x alist-list default)
158 (if (null? alist-list)
160 (let* ((handle (assoc x (car alist-list))))
163 (helper x (cdr alist-list) default)))))
166 (if (pair? default) (car default) #f)))
168 (define (map-alist-vals func list)
169 "map FUNC over the vals of LIST, leaving the keys."
172 (cons (cons (caar list) (func (cdar list)))
173 (map-alist-vals func (cdr list)))
176 (define (map-alist-keys func list)
177 "map FUNC over the keys of an alist LIST, leaving the vals. "
180 (cons (cons (func (caar list)) (cdar list))
181 (map-alist-keys func (cdr list)))
189 (if (not (defined? 'hash-table?)) ; guile 1.6 compat
191 (define hash-table? vector?)
193 (define-public (hash-table->alist t)
194 "Convert table t to list"
201 (define-public (hash-table->alist t)
203 (hash-fold (lambda (k v acc) (acons k v acc))
208 ;; todo: code dup with C++.
209 (define-public (alist->hash-table l)
210 "Convert alist to table"
212 ((m (make-hash-table (length l))))
215 (hashq-set! m (car k-v) (cdr k-v)))
225 (define (flatten-list lst)
229 (if (pair? (car lst))
230 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
231 (cons (car lst) (flatten-list (cdr lst))))
234 (define (list-minus a b)
235 "Return list of elements in A that are not in B."
236 (lset-difference eq? a b))
239 ;; TODO: use the srfi-1 partition function.
240 (define-public (uniq-list l)
242 "Uniq LIST, assuming that it is sorted"
243 (define (helper acc l)
248 (if (equal? (car l) (cadr l))
250 (helper (cons (car l) acc) (cdr l)))
252 (reverse! (helper '() l) '()))
255 (define (split-at-predicate predicate l)
256 "Split L = (a_1 a_2 ... a_k b_1 ... b_k)
257 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
258 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
259 L1 is copied, L2 not.
261 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
266 (define (inner-split predicate l acc)
270 (set-car! acc (cons (car l) (car acc)))
272 ((predicate (car l) (cadr l))
273 (set-car! acc (cons (car l) (car acc)))
274 (inner-split predicate (cdr l) acc))
276 (set-car! acc (cons (car l) (car acc)))
277 (set-cdr! acc (cdr l))
284 (inner-split predicate l c)
285 (set-car! c (reverse! (car c)))
290 (define-public (split-list l sep?)
292 (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
294 ((a b c) (d e f) (g))
299 (define (split-one sep? l acc)
300 "Split off the first parts before separator and return both parts."
305 (split-one sep? (cdr l) (cons (car l) acc))
311 (let* ((c (split-one sep? l '())))
312 (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
316 (define-public (interval-length x)
317 "Length of the number-pair X, when an interval"
318 (max 0 (- (cdr x) (car x)))
322 (define (other-axis a)
323 (remainder (+ a 1) 2))
326 (define-public (interval-widen iv amount)
327 (cons (- (car iv) amount)
328 (+ (cdr iv) amount)))
330 (define-public (interval-union i1 i2)
331 (cons (min (car i1) (car i2))
332 (max (cdr i1) (cdr i2))))
335 (define-public (write-me message x)
336 "Return X. Display MESSAGE and write X. Handy for debugging, possibly turned off."
337 (display message) (write x) (newline) x)
340 (define (index-cell cell dir)
345 (define (cons-map f x)
346 "map F to contents of X"
347 (cons (f (car x)) (f (cdr x))))
350 (define-public (list-insert-separator lst between)
351 "Create new list, inserting BETWEEN between elements of LIST"
355 (cons x (cons between y))
357 (fold-right conc #f lst))
366 (define-public (symbol<? l r)
367 (string<? (symbol->string l) (symbol->string r)))
369 (define-public (!= l r)
372 (define-public (ly:load x)
373 (let* ((fn (%search-load-path x)))
374 (if (ly:get-option 'verbose)
375 (format (current-error-port) "[~A]" fn))
376 (primitive-load fn)))
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
383 ;;(define-public (output-framework) (write "hello\n"))
385 (define output-tex-module
386 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
387 (define output-ps-module
388 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
390 (define-public (ps-output-expression expr port)
391 (display (eval expr output-ps-module) port))
393 ;; TODO: generate this list by registering the stencil expressions
394 ;; stencil expressions should have docstrings.
395 (define-public (ly:all-stencil-expressions)
396 "Return list of stencil expressions."
422 ;; - generate this list by registering the output-backend-commands
423 ;; output-backend-commands should have docstrings.
424 ;; - remove hard copies in output-ps output-tex
425 (define-public (ly:all-output-backend-commands)
426 "Return list of output backend commands."
435 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
440 '("define-music-types.scm"
443 "chord-ignatzek-names.scm"
445 "chord-generic-names.scm"
449 "music-functions.scm"
451 "define-music-properties.scm"
457 "define-context-properties.scm"
458 "translation-functions.scm"
468 "define-markup-commands.scm"
469 "define-grob-properties.scm"
471 "define-grob-interfaces.scm"
482 (set! type-p-name-alist
484 (,boolean-or-symbol? . "boolean or symbol")
485 (,boolean? . "boolean")
487 (,grob-list? . "list of grobs")
488 (,hash-table? . "hash table")
489 (,input-port? . "input port")
490 (,integer? . "integer")
492 (,ly:context? . "context")
493 (,ly:dimension? . "dimension, in staff space")
494 (,ly:dir? . "direction")
495 (,ly:duration? . "duration")
496 (,ly:grob? . "layout object")
497 (,ly:input-location? . "input location")
498 (,ly:moment? . "moment")
499 (,ly:music? . "music")
500 (,ly:pitch? . "pitch")
501 (,ly:translator? . "translator")
502 (,ly:font-metric? . "font metric")
503 (,markup-list? . "list of markups")
504 (,markup? . "markup")
505 (,ly:music-list? . "list of music")
506 (,number-or-grob? . "number or grob")
507 (,number-or-string? . "number or string")
508 (,number-pair? . "pair of numbers")
509 (,number? . "number")
510 (,output-port? . "output port")
512 (,procedure? . "procedure")
513 (,scheme? . "any type")
514 (,string? . "string")
515 (,symbol? . "symbol")
516 (,vector? . "vector")
522 (define gc-protect-stat-count 0)
523 (define-public (dump-gc-protects)
524 (set! gc-protect-stat-count (1+ gc-protect-stat-count) )
527 (hash-table->alist (ly:protects))
529 (< (object-address (car a))
530 (object-address (car b))))))
531 (outfile (open-file (string-append
532 "gcstat-" (number->string gc-protect-stat-count)
536 (display "DUMPING...\n")
539 (lambda (x) (not (symbol? x)))
547 (map object->string (list (object-address x) c x))
554 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556 (define-public (postscript->pdf papersize name)
558 ((cmd (string-append "ps2pdf -sPAPERSIZE=" papersize
561 (output-name (regexp-substitute/global #f "\\.ps" name 'pre ".pdf" 'post)))
566 "Converting to " output-name "\n"))
568 (if (ly:get-option 'verbose)
569 (display (format "Invoking ~S" cmd)))
575 (define-public (postscript->png resolution name)
578 "ps2png --resolution="
579 (if (number? resolution)
580 (number->string resolution)
582 (if (ly:get-option 'verbose)
586 (if (ly:get-option 'verbose)
587 (display (format "Invoking `~S'\n" cmd)))
590 (define-public (lilypond-main files)
591 "Entry point for LilyPond."
593 (handler (lambda (key arg) (set! failed (cons arg failed)))))
596 (catch 'ly-file-failed (lambda () (ly:parse-file f)) handler)
604 (string-append "\n *** Failed files: " (string-join failed) "\n"))