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 ;(set-debug-cell-accesses! 5000)
16 (use-modules (ice-9 regex)
20 (srfi srfi-13)) ; strings
22 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
25 ;;; debugging evaluator is slower. This should
26 ;;; have a more sensible default.
28 (if (ly:get-option 'verbose)
31 (debug-enable 'backtrace)
32 (read-enable 'positions)))
34 (define-public (line-column-location file line col)
35 "Print an input location, including column number ."
36 (string-append (number->string line) ":"
37 (number->string col) " " file))
39 (define-public (line-location file line col)
40 "Print an input location, without column number ."
41 (string-append (number->string line) " " file))
43 (define-public point-and-click #f)
45 (define-public parser #f)
47 (define-public (lilypond-version)
49 (map (lambda (x) (if (symbol? x)
57 ;; cpp hack to get useful error message
58 (define ifdef "First run this through cpp.")
59 (define ifndef "First run this through cpp.")
62 (define-public _ ly:gettext)
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68 (define-public START -1)
69 (define-public STOP 1)
70 (define-public LEFT -1)
71 (define-public RIGHT 1)
73 (define-public DOWN -1)
74 (define-public CENTER 0)
76 (define-public DOUBLE-FLAT -4)
77 (define-public THREE-Q-FLAT -3)
78 (define-public FLAT -2)
79 (define-public SEMI-FLAT -1)
80 (define-public NATURAL 0)
81 (define-public SEMI-SHARP 1)
82 (define-public SHARP 2)
83 (define-public THREE-Q-SHARP 3)
84 (define-public DOUBLE-SHARP 4)
85 (define-public SEMI-TONE 2)
87 (define-public ZERO-MOMENT (ly:make-moment 0 1))
89 (define-public (moment-min a b)
90 (if (ly:moment<? a b) a b))
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 ;; lily specific variables.
95 (define-public default-script-alist '())
99 (define-public (print-music-as-book parser music)
100 (let* ((head (ly:parser-lookup parser '$globalheader))
101 (book (ly:make-book (ly:parser-lookup parser $defaultbookpaper)
103 (ly:parser-print-book parser book)))
105 (define-public (print-score-as-book parser score)
107 ((head (ly:parser-lookup parser '$globalheader))
108 (book (ly:make-book (ly:parser-lookup parser $defaultbookpaper)
110 (ly:parser-print-book parser book)))
112 (define-public (print-score parser score)
113 (let* ((head (ly:parser-lookup parser '$globalheader))
114 (book (ly:make-book (ly:parser-lookup parser $defaultbookpaper)
116 (ly:parser-print-score parser book)))
118 (define-public (collect-scores-for-book parser score)
120 ((oldval (ly:parser-lookup parser 'toplevel-scores)))
121 (ly:parser-define parser 'toplevel-scores (cons score oldval))
124 (define-public (collect-music-for-book parser music)
125 (collect-scores-for-book parser (ly:music-scorify music)))
131 (define-public (assoc-get key alist . default)
132 "Return value if KEY in ALIST, else DEFAULT (or #f if not specified)."
133 (let ((entry (assoc key alist)))
136 (if (pair? default) (car default) #f))))
138 (define-public (uniqued-alist alist acc)
139 (if (null? alist) acc
140 (if (assoc (caar alist) acc)
141 (uniqued-alist (cdr alist) acc)
142 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
144 (define-public (alist<? x y)
145 (string<? (symbol->string (car x))
146 (symbol->string (car y))))
148 (define-public (chain-assoc x alist-list)
149 (if (null? alist-list)
151 (let* ((handle (assoc x (car alist-list))))
154 (chain-assoc x (cdr alist-list))))))
156 (define-public (chain-assoc-get x alist-list . default)
157 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
160 (define (helper x alist-list default)
161 (if (null? alist-list)
163 (let* ((handle (assoc x (car alist-list))))
166 (helper x (cdr alist-list) default)))))
169 (if (pair? default) (car default) #f)))
171 (define (map-alist-vals func list)
172 "map FUNC over the vals of LIST, leaving the keys."
175 (cons (cons (caar list) (func (cdar list)))
176 (map-alist-vals func (cdr list)))
179 (define (map-alist-keys func list)
180 "map FUNC over the keys of an alist LIST, leaving the vals. "
183 (cons (cons (func (caar list)) (cdar list))
184 (map-alist-keys func (cdr list)))
192 (if (not (defined? 'hash-table?)) ; guile 1.6 compat
194 (define hash-table? vector?)
196 (define-public (hash-table->alist t)
197 "Convert table t to list"
204 (define-public (hash-table->alist t)
206 (hash-fold (lambda (k v acc) (acons k v acc))
211 ;; todo: code dup with C++.
212 (define-public (alist->hash-table l)
213 "Convert alist to table"
215 ((m (make-hash-table (length l))))
218 (hashq-set! m (car k-v) (cdr k-v)))
228 (define (flatten-list lst)
232 (if (pair? (car lst))
233 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
234 (cons (car lst) (flatten-list (cdr lst))))
237 (define (list-minus a b)
238 "Return list of elements in A that are not in B."
239 (lset-difference eq? a b))
242 ;; TODO: use the srfi-1 partition function.
243 (define-public (uniq-list l)
245 "Uniq LIST, assuming that it is sorted"
246 (define (helper acc l)
251 (if (equal? (car l) (cadr l))
253 (helper (cons (car l) acc) (cdr l)))
255 (reverse! (helper '() l) '()))
258 (define (split-at-predicate predicate l)
259 "Split L = (a_1 a_2 ... a_k b_1 ... b_k)
260 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
261 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
262 L1 is copied, L2 not.
264 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
269 (define (inner-split predicate l acc)
273 (set-car! acc (cons (car l) (car acc)))
275 ((predicate (car l) (cadr l))
276 (set-car! acc (cons (car l) (car acc)))
277 (inner-split predicate (cdr l) acc))
279 (set-car! acc (cons (car l) (car acc)))
280 (set-cdr! acc (cdr l))
287 (inner-split predicate l c)
288 (set-car! c (reverse! (car c)))
293 (define-public (split-list l sep?)
295 (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
297 ((a b c) (d e f) (g))
302 (define (split-one sep? l acc)
303 "Split off the first parts before separator and return both parts."
308 (split-one sep? (cdr l) (cons (car l) acc))
314 (let* ((c (split-one sep? l '())))
315 (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
319 (define-public (interval-length x)
320 "Length of the number-pair X, when an interval"
321 (max 0 (- (cdr x) (car x)))
325 (define (other-axis a)
326 (remainder (+ a 1) 2))
329 (define-public (interval-widen iv amount)
330 (cons (- (car iv) amount)
331 (+ (cdr iv) amount)))
333 (define-public (interval-union i1 i2)
334 (cons (min (car i1) (car i2))
335 (max (cdr i1) (cdr i2))))
338 (define-public (write-me message x)
339 "Return X. Display MESSAGE and write X. Handy for debugging,
340 possibly turned off."
341 (display message) (write x) (newline) x)
344 (define (index-cell cell dir)
349 (define (cons-map f x)
350 "map F to contents of X"
351 (cons (f (car x)) (f (cdr x))))
354 (define-public (list-insert-separator lst between)
355 "Create new list, inserting BETWEEN between elements of LIST"
359 (cons x (cons between y))
361 (fold-right conc #f lst))
370 (define-public (symbol<? l r)
371 (string<? (symbol->string l) (symbol->string r)))
373 (define-public (!= l r)
376 (define-public (ly:load x)
377 (let* ((fn (%search-load-path x)))
378 (if (ly:get-option 'verbose)
379 (format (current-error-port) "[~A]" fn))
380 (primitive-load fn)))
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ;;(define-public (output-framework) (write "hello\n"))
389 (define output-tex-module
390 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
391 (define output-ps-module
392 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
394 (define-public (ps-output-expression expr port)
395 (display (eval expr output-ps-module) port))
397 ;; TODO: generate this list by registering the stencil expressions
398 ;; stencil expressions should have docstrings.
399 (define-public (ly:all-stencil-expressions)
400 "Return list of stencil expressions."
426 ;; - generate this list by registering the output-backend-commands
427 ;; output-backend-commands should have docstrings.
428 ;; - remove hard copies in output-ps output-tex
429 (define-public (ly:all-output-backend-commands)
430 "Return list of output backend commands."
439 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444 '("define-music-types.scm"
447 "chord-ignatzek-names.scm"
449 "chord-generic-names.scm"
453 "music-functions.scm"
455 "define-music-properties.scm"
461 "define-context-properties.scm"
462 "translation-functions.scm"
472 "define-markup-commands.scm"
473 "define-grob-properties.scm"
475 "define-grob-interfaces.scm"
486 (set! type-p-name-alist
488 (,boolean-or-symbol? . "boolean or symbol")
489 (,boolean? . "boolean")
491 (,grob-list? . "list of grobs")
492 (,hash-table? . "hash table")
493 (,input-port? . "input port")
494 (,integer? . "integer")
496 (,ly:context? . "context")
497 (,ly:dimension? . "dimension, in staff space")
498 (,ly:dir? . "direction")
499 (,ly:duration? . "duration")
500 (,ly:grob? . "layout object")
501 (,ly:input-location? . "input location")
502 (,ly:moment? . "moment")
503 (,ly:music? . "music")
504 (,ly:pitch? . "pitch")
505 (,ly:translator? . "translator")
506 (,ly:font-metric? . "font metric")
507 (,markup-list? . "list of markups")
508 (,markup? . "markup")
509 (,ly:music-list? . "list of music")
510 (,number-or-grob? . "number or grob")
511 (,number-or-string? . "number or string")
512 (,number-pair? . "pair of numbers")
513 (,number? . "number")
514 (,output-port? . "output port")
516 (,procedure? . "procedure")
517 (,scheme? . "any type")
518 (,string? . "string")
519 (,symbol? . "symbol")
520 (,vector? . "vector")
526 (define gc-protect-stat-count 0)
527 (define-public (dump-gc-protects)
528 (set! gc-protect-stat-count (1+ gc-protect-stat-count) )
531 (hash-table->alist (ly:protects))
533 (< (object-address (car a))
534 (object-address (car b))))))
535 (outfile (open-file (string-append
536 "gcstat-" (number->string gc-protect-stat-count)
540 (display "DUMPING...\n")
543 (lambda (x) (not (symbol? x)))
551 (map object->string (list (object-address x) c x))
558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
560 (define-public (postscript->pdf papersize name)
561 (let* ((cmd (string-append "ps2pdf -sPAPERSIZE=" papersize " " name))
563 (regexp-substitute/global #f "\\.ps" name 'pre ".pdf" 'post)))
565 (newline (current-error-port))
566 (display (format (_ "Converting to `~a'...") output-name)
567 (current-error-port))
568 (newline (current-error-port))
570 (if (ly:get-option 'verbose)
571 (display (format "Invoking `~a'..." cmd) (current-error-port)))
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)
588 (display (format (_ "Invoking `~a'...") cmd) (current-error-port))
589 (newline (current-error-port))))
592 (define-public (lilypond-main files)
593 "Entry point for LilyPond."
595 (handler (lambda (key arg) (set! failed (cons arg failed)))))
598 (catch 'ly-file-failed (lambda () (ly:parse-file f)) handler)
599 ;;; (dump-gc-protects)
605 (newline (current-error-port))
606 (display (_ "error: failed files: ") (current-error-port))
607 (display (string-join failed) (current-error-port))
608 (newline (current-error-port))
609 (newline (current-error-port))