1 ;;;; lily.scm -- toplevel Scheme stuff
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>
9 (if (defined? 'set-debug-cell-accesses!)
10 (set-debug-cell-accesses! #f))
12 ;;(set-debug-cell-accesses! 5000)
14 (use-modules (ice-9 regex)
18 (srfi srfi-13)) ; strings
22 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
24 (define-public (print . args)
25 (apply format (cons (current-output-port) args)))
29 ;;; debugging evaluator is slower. This should
30 ;;; have a more sensible default.
32 (if (ly:get-option 'verbose)
35 (debug-enable 'backtrace)
36 (read-enable 'positions)))
38 (define-public (line-column-location file line col)
39 "Print an input location, including column number ."
40 (string-append (number->string line) ":"
41 (number->string col) " " file))
43 (define-public (line-location file line col)
44 "Print an input location, without column number ."
45 (string-append (number->string line) " " file))
47 (define-public point-and-click #f)
49 (define-public tex-backend?
50 (memq (ly:output-backend) '("texstr" "tex")))
52 (define-public parser #f)
54 (define-public (lilypond-version)
56 (map (lambda (x) (if (symbol? x)
64 ;; cpp hack to get useful error message
65 (define ifdef "First run this through cpp.")
66 (define ifndef "First run this through cpp.")
68 ;; gettext wrapper for guile < 1.7.2
69 (if (defined? 'gettext)
70 (define-public _ gettext)
71 (define-public _ ly:gettext))
73 (define-public (ly:load x)
74 (let* ((fn (%search-load-path x)))
75 (if (ly:get-option 'verbose)
76 (format (current-error-port) "[~A]" fn))
79 (define-public TEX_STRING_HASHLIMIT 10000000)
81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83 (define (type-check-list location signature arguments)
84 "Typecheck a list of arguments against a list of type
85 predicates. Print a message at LOCATION if any predicate failed."
86 (define (recursion-helper signature arguments count)
87 (define (helper pred? arg count)
91 (ly:input-message location
93 (_ "wrong type for argument ~a. Expecting ~a, found ~s")
94 count (type-name pred?) arg))
100 (and (helper (car signature) (car arguments) count)
101 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
102 (recursion-helper signature arguments 1))
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
108 ;;(define-public (output-framework) (write "hello\n"))
110 (define output-tex-module
111 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
112 (define output-ps-module
113 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
115 (define-public (ps-output-expression expr port)
116 (display (eval expr output-ps-module) port))
118 ;; TODO: generate this list by registering the stencil expressions
119 ;; stencil expressions should have docstrings.
120 (define-public (ly:all-stencil-expressions)
121 "Return list of stencil expressions."
146 ;; - generate this list by registering the output-backend-commands
147 ;; output-backend-commands should have docstrings.
148 ;; - remove hard copies in output-ps output-tex
149 (define-public (ly:all-output-backend-commands)
150 "Return list of output backend commands."
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 "define-music-types.scm"
167 "chord-ignatzek-names.scm"
169 "chord-generic-names.scm"
173 "music-functions.scm"
175 "define-music-properties.scm"
181 "define-context-properties.scm"
182 "translation-functions.scm"
192 "define-markup-commands.scm"
193 "define-grob-properties.scm"
195 "define-grob-interfaces.scm"
205 (set! type-p-name-alist
207 (,boolean-or-symbol? . "boolean or symbol")
208 (,boolean? . "boolean")
210 (,grob-list? . "list of grobs")
211 (,hash-table? . "hash table")
212 (,input-port? . "input port")
213 (,integer? . "integer")
215 (,ly:context? . "context")
216 (,ly:dimension? . "dimension, in staff space")
217 (,ly:dir? . "direction")
218 (,ly:duration? . "duration")
219 (,ly:grob? . "layout object")
220 (,ly:input-location? . "input location")
221 (,ly:moment? . "moment")
222 (,ly:music? . "music")
223 (,ly:pitch? . "pitch")
224 (,ly:translator? . "translator")
225 (,ly:font-metric? . "font metric")
226 (,markup-list? . "list of markups")
227 (,markup? . "markup")
228 (,ly:music-list? . "list of music")
229 (,number-or-grob? . "number or grob")
230 (,number-or-string? . "number or string")
231 (,number-pair? . "pair of numbers")
232 (,number? . "number")
233 (,output-port? . "output port")
235 (,procedure? . "procedure")
236 (,scheme? . "any type")
237 (,string? . "string")
238 (,symbol? . "symbol")
239 (,vector? . "vector")))
244 (define gc-protect-stat-count 0)
245 (define-public (dump-gc-protects)
246 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
247 (let* ((protects (sort
248 (hash-table->alist (ly:protects))
250 (< (object-address (car a))
251 (object-address (car b))))))
252 (out-file-name (string-append
253 "gcstat-" (number->string gc-protect-stat-count)
255 (outfile (open-file out-file-name "w")))
257 (display "Dumping gc protected objs to ...\n")
260 (lambda (x) (not (symbol? x)))
267 (map object->string (list (object-address x) c x))
274 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 (define-public (ly:system command)
281 (string-append command (if (ly:get-option 'verbose)
283 " > /dev/null 2>&1 "))))
285 (if (ly:get-option 'verbose)
286 (format (current-error-port) (_ "Invoking `~a'...\n") command))
288 (set! status (system silenced))
291 (format (current-error-port)
292 (_ "Error invoking `~a'. Return value ~a") silenced status)
293 (newline (current-error-port))))))
295 (define-public (sanitize-command-option str)
298 (regexp-substitute/global #f "[^- 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
301 (define-public (postscript->pdf papersizename name)
302 (let* ((cmd (string-append "ps2pdf "
305 (sanitize-command-option papersizename)
308 (pdf-name (string-append (basename name ".ps") ".pdf" )))
310 (if (access? pdf-name W_OK)
311 (delete-file pdf-name))
313 (format (current-error-port) (_ "Converting to `~a'...") pdf-name)
316 (define-public (postscript->png resolution name)
317 (let ((cmd (string-append
318 "ps2png --resolution="
319 (if (number? resolution)
320 (number->string resolution)
322 (if (ly:get-option 'verbose)
328 (define-public (postprocess-output paper-book module filename formats)
329 (for-each (lambda (f)
330 ((eval (string->symbol (string-append "convert-to-" f))
332 paper-book filename))
336 (define-public (completize-formats formats)
337 (if (member "png" formats)
338 (set! formats (cons "ps" formats)))
339 (if (member "pdf" formats)
340 (set! formats (cons "ps" formats)))
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 (define-public (lilypond-main files)
347 "Entry point for LilyPond."
349 (handler (lambda (key arg) (set! failed (cons arg failed)))))
352 (catch 'ly-file-failed (lambda () (ly:parse-file f)) handler)
359 (newline (current-error-port))
360 (display (_ "error: failed files: ") (current-error-port))
361 (display (string-join failed) (current-error-port))
362 (newline (current-error-port))
363 (newline (current-error-port))
367 (define-public (tweak-grob-property grob sym val)
368 (set! (ly:grob-property grob sym) val))