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
24 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
26 (define-public (print . args)
27 (apply format (cons (current-output-port) args)))
31 ;;; debugging evaluator is slower. This should
32 ;;; have a more sensible default.
34 (if (ly:get-option 'verbose)
37 (debug-enable 'backtrace)
38 (read-enable 'positions)))
40 (define-public (line-column-location file line col)
41 "Print an input location, including column number ."
42 (string-append (number->string line) ":"
43 (number->string col) " " file))
45 (define-public (line-location file line col)
46 "Print an input location, without column number ."
47 (string-append (number->string line) " " file))
49 (define-public point-and-click #f)
51 (define-public parser #f)
53 (define-public (lilypond-version)
55 (map (lambda (x) (if (symbol? x)
63 ;; cpp hack to get useful error message
64 (define ifdef "First run this through cpp.")
65 (define ifndef "First run this through cpp.")
67 ;; gettext wrapper for guile < 1.7.2
68 (if (defined? 'gettext)
69 (define-public _ gettext)
70 (define-public _ ly:gettext))
72 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76 (define-public START -1)
77 (define-public STOP 1)
78 (define-public LEFT -1)
79 (define-public RIGHT 1)
81 (define-public DOWN -1)
82 (define-public CENTER 0)
84 (define-public DOUBLE-FLAT -4)
85 (define-public THREE-Q-FLAT -3)
86 (define-public FLAT -2)
87 (define-public SEMI-FLAT -1)
88 (define-public NATURAL 0)
89 (define-public SEMI-SHARP 1)
90 (define-public SHARP 2)
91 (define-public THREE-Q-SHARP 3)
92 (define-public DOUBLE-SHARP 4)
93 (define-public SEMI-TONE 2)
95 (define-public ZERO-MOMENT (ly:make-moment 0 1))
97 (define-public (moment-min a b)
98 (if (ly:moment<? a b) a b))
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 ;; lily specific variables.
103 (define-public default-script-alist '())
107 (define-public (print-music-as-book parser music)
108 (let* ((head (ly:parser-lookup parser '$globalheader))
109 (book (ly:make-book (ly:parser-lookup parser $defaultbookpaper)
111 (ly:parser-print-book parser book)))
113 (define-public (print-score-as-book parser score)
115 ((head (ly:parser-lookup parser '$globalheader))
116 (book (ly:make-book (ly:parser-lookup parser $defaultbookpaper)
118 (ly:parser-print-book parser book)))
120 (define-public (print-score parser score)
121 (let* ((head (ly:parser-lookup parser '$globalheader))
122 (book (ly:make-book (ly:parser-lookup parser $defaultbookpaper)
124 (ly:parser-print-score parser book)))
126 (define-public (collect-scores-for-book parser score)
128 ((oldval (ly:parser-lookup parser 'toplevel-scores)))
129 (ly:parser-define parser 'toplevel-scores (cons score oldval))
132 (define-public (collect-music-for-book parser music)
133 (collect-scores-for-book parser (ly:music-scorify music parser)))
139 (define-public assoc-get ly:assoc-get)
141 (define-public (uniqued-alist alist acc)
142 (if (null? alist) acc
143 (if (assoc (caar alist) acc)
144 (uniqued-alist (cdr alist) acc)
145 (uniqued-alist (cdr alist) (cons (car alist) acc)))))
147 (define-public (alist<? x y)
148 (string<? (symbol->string (car x))
149 (symbol->string (car y))))
151 (define-public (chain-assoc x alist-list)
152 (if (null? alist-list)
154 (let* ((handle (assoc x (car alist-list))))
157 (chain-assoc x (cdr alist-list))))))
159 (define-public (chain-assoc-get x alist-list . default)
160 "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
163 (define (helper x alist-list default)
164 (if (null? alist-list)
166 (let* ((handle (assoc x (car alist-list))))
169 (helper x (cdr alist-list) default)))))
172 (if (pair? default) (car default) #f)))
174 (define (map-alist-vals func list)
175 "map FUNC over the vals of LIST, leaving the keys."
178 (cons (cons (caar list) (func (cdar list)))
179 (map-alist-vals func (cdr list)))
182 (define (map-alist-keys func list)
183 "map FUNC over the keys of an alist LIST, leaving the vals. "
186 (cons (cons (func (caar list)) (cdar list))
187 (map-alist-keys func (cdr list)))
195 (if (not (defined? 'hash-table?)) ; guile 1.6 compat
197 (define hash-table? vector?)
199 (define-public (hash-table->alist t)
200 "Convert table t to list"
207 (define-public (hash-table->alist t)
209 (hash-fold (lambda (k v acc) (acons k v acc))
214 ;; todo: code dup with C++.
215 (define-public (alist->hash-table l)
216 "Convert alist to table"
218 ((m (make-hash-table (length l))))
221 (hashq-set! m (car k-v) (cdr k-v)))
231 (define (flatten-list lst)
235 (if (pair? (car lst))
236 (append (flatten-list (car lst)) (flatten-list (cdr lst)))
237 (cons (car lst) (flatten-list (cdr lst))))
240 (define (list-minus a b)
241 "Return list of elements in A that are not in B."
242 (lset-difference eq? a b))
245 ;; TODO: use the srfi-1 partition function.
246 (define-public (uniq-list l)
248 "Uniq LIST, assuming that it is sorted"
249 (define (helper acc l)
254 (if (equal? (car l) (cadr l))
256 (helper (cons (car l) acc) (cdr l)))
258 (reverse! (helper '() l) '()))
261 (define (split-at-predicate predicate l)
262 "Split L = (a_1 a_2 ... a_k b_1 ... b_k)
263 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k)
264 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
265 L1 is copied, L2 not.
267 (split-at-predicate (lambda (x y) (= (- y x) 2)) '(1 3 5 9 11) (cons '() '()))"
272 (define (inner-split predicate l acc)
276 (set-car! acc (cons (car l) (car acc)))
278 ((predicate (car l) (cadr l))
279 (set-car! acc (cons (car l) (car acc)))
280 (inner-split predicate (cdr l) acc))
282 (set-car! acc (cons (car l) (car acc)))
283 (set-cdr! acc (cdr l))
290 (inner-split predicate l c)
291 (set-car! c (reverse! (car c)))
296 (define-public (split-list l sep?)
298 (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
300 ((a b c) (d e f) (g))
305 (define (split-one sep? l acc)
306 "Split off the first parts before separator and return both parts."
311 (split-one sep? (cdr l) (cons (car l) acc))
317 (let* ((c (split-one sep? l '())))
318 (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
322 (define-public (interval-length x)
323 "Length of the number-pair X, when an interval"
324 (max 0 (- (cdr x) (car x)))
328 (define (other-axis a)
329 (remainder (+ a 1) 2))
332 (define-public (interval-widen iv amount)
333 (cons (- (car iv) amount)
334 (+ (cdr iv) amount)))
336 (define-public (interval-union i1 i2)
337 (cons (min (car i1) (car i2))
338 (max (cdr i1) (cdr i2))))
341 (define-public (write-me message x)
342 "Return X. Display MESSAGE and write X. Handy for debugging,
343 possibly turned off."
344 (display message) (write x) (newline) x)
347 (define (index-cell cell dir)
352 (define (cons-map f x)
353 "map F to contents of X"
354 (cons (f (car x)) (f (cdr x))))
357 (define-public (list-insert-separator lst between)
358 "Create new list, inserting BETWEEN between elements of LIST"
362 (cons x (cons between y))
364 (fold-right conc #f lst))
373 (define-public (symbol<? l r)
374 (string<? (symbol->string l) (symbol->string r)))
376 (define-public (!= l r)
379 (define-public (ly:load x)
380 (let* ((fn (%search-load-path x)))
381 (if (ly:get-option 'verbose)
382 (format (current-error-port) "[~A]" fn))
383 (primitive-load fn)))
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
390 ;;(define-public (output-framework) (write "hello\n"))
392 (define output-tex-module
393 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
394 (define output-ps-module
395 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
397 (define-public (ps-output-expression expr port)
398 (display (eval expr output-ps-module) port))
400 ;; TODO: generate this list by registering the stencil expressions
401 ;; stencil expressions should have docstrings.
402 (define-public (ly:all-stencil-expressions)
403 "Return list of stencil expressions."
429 ;; - generate this list by registering the output-backend-commands
430 ;; output-backend-commands should have docstrings.
431 ;; - remove hard copies in output-ps output-tex
432 (define-public (ly:all-output-backend-commands)
433 "Return list of output backend commands."
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447 '("define-music-types.scm"
450 "chord-ignatzek-names.scm"
452 "chord-generic-names.scm"
456 "music-functions.scm"
458 "define-music-properties.scm"
464 "define-context-properties.scm"
465 "translation-functions.scm"
475 "define-markup-commands.scm"
476 "define-grob-properties.scm"
478 "define-grob-interfaces.scm"
489 (set! type-p-name-alist
491 (,boolean-or-symbol? . "boolean or symbol")
492 (,boolean? . "boolean")
494 (,grob-list? . "list of grobs")
495 (,hash-table? . "hash table")
496 (,input-port? . "input port")
497 (,integer? . "integer")
499 (,ly:context? . "context")
500 (,ly:dimension? . "dimension, in staff space")
501 (,ly:dir? . "direction")
502 (,ly:duration? . "duration")
503 (,ly:grob? . "layout object")
504 (,ly:input-location? . "input location")
505 (,ly:moment? . "moment")
506 (,ly:music? . "music")
507 (,ly:pitch? . "pitch")
508 (,ly:translator? . "translator")
509 (,ly:font-metric? . "font metric")
510 (,markup-list? . "list of markups")
511 (,markup? . "markup")
512 (,ly:music-list? . "list of music")
513 (,number-or-grob? . "number or grob")
514 (,number-or-string? . "number or string")
515 (,number-pair? . "pair of numbers")
516 (,number? . "number")
517 (,output-port? . "output port")
519 (,procedure? . "procedure")
520 (,scheme? . "any type")
521 (,string? . "string")
522 (,symbol? . "symbol")
523 (,vector? . "vector")
529 (define gc-protect-stat-count 0)
530 (define-public (dump-gc-protects)
531 (set! gc-protect-stat-count (1+ gc-protect-stat-count) )
534 (hash-table->alist (ly:protects))
536 (< (object-address (car a))
537 (object-address (car b))))))
538 (outfile (open-file (string-append
539 "gcstat-" (number->string gc-protect-stat-count)
543 (display "DUMPING...\n")
546 (lambda (x) (not (symbol? x)))
554 (map object->string (list (object-address x) c x))
561 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
563 (define-public (postscript->pdf papersize name)
564 (let* ((cmd (string-append "ps2pdf -sPAPERSIZE=" papersize " " name))
566 (regexp-substitute/global #f "\\.ps" name 'pre ".pdf" 'post)))
568 (newline (current-error-port))
569 (display (format (_ "Converting to `~a'...") output-name)
570 (current-error-port))
571 (newline (current-error-port))
573 (if (ly:get-option 'verbose)
574 (display (format "Invoking `~a'..." cmd) (current-error-port)))
578 (define-public (postscript->png resolution name)
581 "ps2png --resolution="
582 (if (number? resolution)
583 (number->string resolution)
585 (if (ly:get-option 'verbose)
589 (if (ly:get-option 'verbose)
591 (display (format (_ "Invoking `~a'...") cmd) (current-error-port))
592 (newline (current-error-port))))
595 (define-public (lilypond-main files)
596 "Entry point for LilyPond."
598 (handler (lambda (key arg) (set! failed (cons arg failed)))))
601 (catch 'ly-file-failed (lambda () (ly:parse-file f)) handler)
602 ;;; (dump-gc-protects)
608 (newline (current-error-port))
609 (display (_ "error: failed files: ") (current-error-port))
610 (display (string-join failed) (current-error-port))
611 (newline (current-error-port))
612 (newline (current-error-port))