1 ;;;; lily.scm -- toplevel Scheme stuff
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
9 (define (define-scheme-options)
11 (ly:add-option (car x) (cadr x) (caddr x)))
17 ;; - [subject-]object-object-verb +"ing"
18 ;; - [subject-]-verb-object-object
20 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
21 (check-internal-types #f "check every property assignment for types")
22 (clip-systems #f "Generate cut-out snippets of a score")
24 "dump memory debugging statistics")
25 (debug-gc-assert-parsed-dead #f
26 "for memory debugging: ensure that all refs to parsed objects are dead.")
28 (debug-midi #f "generate human readable MIDI")
29 (delete-intermediate-files #f
30 "delete unusable PostScript files")
31 (dump-signatures #f "dump output signatures of each system")
32 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
34 "load fonts via Ghostscript.")
35 (include-book-title-preview #t "include book-titles in preview images.")
36 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
38 (eps-box-padding #f "Pad EPS bounding box left edge by this much to guarantee alignment between systems")
40 (gui #f "running from gui; redirect stderr to log file")
43 "relative for simultaneous music works
44 similar to chord syntax")
46 "experimental mechanism for remembering tweaks")
47 (point-and-click #t "use point & click")
48 (paper-size "a4" "the default paper size")
49 (protected-scheme-parsing #t "continue when finding errors in inline
50 scheme are caught in the parser. If off, halt
51 on errors, and print a stack trace.")
52 (profile-property-accesses #f "keep statistics of get_property() calls.")
54 (resolution 101 "resolution for generating bitmaps")
55 (read-file-list #f "Read files to be processed from command line arguments")
57 (safe #f "Run safely")
58 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
61 "how much verbosity for TTF font embedding?")
63 (show-available-fonts #f
64 "List font names available.")
66 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
70 ;; need to do this in the beginning. Other parts of the
71 ;; Scheme init depend on these options.
73 (define-scheme-options)
77 (if (defined? 'set-debug-cell-accesses!)
78 (set-debug-cell-accesses! #f))
80 ;(set-debug-cell-accesses! 1000)
82 (use-modules (ice-9 regex)
95 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
97 (define-public (print . args)
98 (apply format (cons (current-output-port) args)))
102 ;;; debugging evaluator is slower. This should
103 ;;; have a more sensible default.
105 (if (ly:get-option 'verbose)
107 (debug-enable 'debug)
108 (debug-enable 'backtrace)
109 (read-enable 'positions)))
111 (define-public tex-backend?
112 (member (ly:output-backend) '("texstr" "tex")))
114 (define-public parser #f)
116 (define-public (lilypond-version)
118 (map (lambda (x) (if (symbol? x)
125 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
126 (define-public TEX_STRING_HASHLIMIT 10000000)
130 ;; gettext wrapper for guile < 1.7.2
131 (if (defined? 'gettext)
132 (define-public _ gettext)
133 (define-public _ ly:gettext))
135 (define-public (ly:load x)
136 (let* ((file-name (%search-load-path x)))
137 (if (ly:get-option 'verbose)
138 (ly:progress "[~A" file-name))
140 (ly:error (_ "Can't find ~A" x)))
141 (primitive-load file-name)
142 (if (ly:get-option 'verbose)
146 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
149 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
152 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
156 (define-public PLATFORM
159 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
162 (let ((platform (string-tokenize
163 (vector-ref (uname) 0) char-set:letter+digit)))
164 (if (null? (cdr platform)) #f
165 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
169 (define native-getcwd getcwd)
171 (if (string-index x #\\)
173 (string-regexp-substitute
175 (string-regexp-substitute "\\\\" "/" x))))
176 ;; FIXME: this prints a warning.
177 (define-public (ly-getcwd)
178 (slashify (native-getcwd))))
179 (else (define-public ly-getcwd getcwd)))
181 (define-public (is-absolute? file-name)
182 (let ((file-name-length (string-length file-name)))
183 (if (= file-name-length 0)
185 (or (eq? (string-ref file-name 0) #\/)
186 (and (eq? PLATFORM 'windows)
187 (> file-name-length 2)
188 (eq? (string-ref file-name 1) #\:)
189 (eq? (string-ref file-name 2) #\/))))))
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 (define (type-check-list location signature arguments)
194 "Typecheck a list of arguments against a list of type
195 predicates. Print a message at LOCATION if any predicate failed."
197 (define (recursion-helper signature arguments count)
198 (define (helper pred? arg count)
199 (if (not (pred? arg))
205 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
206 count (type-name pred?) arg))
210 (if (null? signature)
212 (and (helper (car signature) (car arguments) count)
213 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
215 (recursion-helper signature arguments 1))
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 ;;(define-public (output-framework) (write "hello\n"))
223 (define output-tex-module
224 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
225 (define output-ps-module
226 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
228 (define-public (ps-output-expression expr port)
229 (display (eval expr output-ps-module) port))
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232 ;; Safe definitions utility
233 (define safe-objects (list))
235 (define-macro (define-safe-public arglist . body)
236 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
237 The syntax is the same as `define*-public'."
238 (define (get-symbol arg)
240 (get-symbol (car arg))
242 (let ((safe-symbol (get-symbol arglist)))
244 (define*-public ,arglist
246 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
250 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
253 (ly:set-default-scale (ly:make-scale #(0 2 4 5 7 9 11)))
257 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 "define-event-classes.scm"
265 "define-music-types.scm"
268 "chord-ignatzek-names.scm"
270 "chord-generic-names.scm"
273 "music-functions.scm"
276 "define-music-properties.scm"
280 "parser-ly-from-scheme.scm"
281 "ly-syntax-constructors.scm"
283 "define-context-properties.scm"
284 "translation-functions.scm"
294 "define-markup-commands.scm"
295 "define-grob-properties.scm"
297 "define-grob-interfaces.scm"
298 "define-stencil-commands.scm"
302 "backend-library.scm"
305 ;; must be after everything has been defined
309 (set! type-p-name-alist
311 (,boolean-or-symbol? . "boolean or symbol")
312 (,boolean? . "boolean")
314 (,grob-list? . "list of grobs")
315 (,hash-table? . "hash table")
316 (,input-port? . "input port")
317 (,integer? . "integer")
319 (,ly:context? . "context")
320 (,ly:dimension? . "dimension, in staff space")
321 (,ly:dir? . "direction")
322 (,ly:duration? . "duration")
323 (,ly:grob? . "layout object")
324 (,ly:input-location? . "input location")
325 (,ly:moment? . "moment")
326 (,ly:music? . "music")
327 (,ly:pitch? . "pitch")
328 (,ly:translator? . "translator")
329 (,ly:font-metric? . "font metric")
330 (,ly:simple-closure? . "simple closure")
331 (,markup-list? . "list of markups")
332 (,markup? . "markup")
333 (,ly:music-list? . "list of music")
334 (,number-or-grob? . "number or grob")
335 (,number-or-string? . "number or string")
336 (,number-pair? . "pair of numbers")
337 (,number? . "number")
338 (,output-port? . "output port")
340 (,procedure? . "procedure")
341 (,rhythmic-location? . "rhythmic location")
342 (,scheme? . "any type")
343 (,string? . "string")
344 (,symbol? . "symbol")
345 (,vector? . "vector")))
350 (define gc-protect-stat-count 0)
351 (define-public (dump-gc-protects)
352 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
353 (let* ((protects (sort
354 (hash-table->alist (ly:protects))
356 (< (object-address (car a))
357 (object-address (car b))))))
359 (out-file-name (string-append
360 "gcstat-" (number->string gc-protect-stat-count)
362 (outfile (open-file out-file-name "w")))
364 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
372 (map object->string (list (object-address x) c x))
378 (not (symbol? (car x))))
382 ; (display (ly:smob-protects))
384 (if (defined? 'gc-live-object-stats)
386 (display "Live object statistics: GC'ing\n")
389 (ly:set-option 'debug-gc-assert-parsed-dead #t)
392 (set! stats (gc-live-object-stats))
393 (display "Dumping live object statistics.\n")
397 (format outfile "~a: ~a\n" (car x) (cdr x)))
398 (sort (gc-live-object-stats)
400 (string<? (car x) (car y)))))))))
402 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
405 (define-public (lilypond-main files)
406 "Entry point for LilyPond."
408 (define (no-files-handler)
412 (if (ly:get-option 'gui)
418 (let ((failed (lilypond-all files)))
421 (ly:error (_ "failed files: ~S") (string-join failed))
424 ;; HACK: be sure to exit with single newline
428 (define-public (lilypond-all files)
429 (if (ly:get-option 'read-file-list)
432 (> (string-length s) 0))
434 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
438 (if (ly:get-option 'show-available-fonts)
440 (ly:font-config-display-fonts)
445 (handler (lambda (key failed-file)
446 (set! failed (append (list failed-file) failed)))))
450 (ly:set-option 'debug-gc-assert-parsed-dead #f)
451 (lilypond-file handler x)
452 (ly:clear-anonymous-modules)
453 (if (ly:get-option 'debug-gc)
459 (define (lilypond-file handler file-name)
460 (catch 'ly-file-failed
461 (lambda () (ly:parse-file file-name))
462 (lambda (x . args) (handler x file-name))))
464 (use-modules (scm editor))
466 (define-public (gui-main files)
468 (gui-no-files-handler))
469 (let* ((base (basename (car files) ".ly"))
470 (log-name (string-append base ".log")))
471 (if (not (ly:get-option 'gui))
472 (ly:message (_ "Redirecting output to ~a...") log-name))
473 (ly:stderr-redirect log-name "w")
474 (ly:message "# -*-compilation-*-")
476 (let ((failed (lilypond-all files)))
480 (ly:stderr-redirect "foo" "r")
481 (system (get-editor-command log-name 0 0 0))
482 (ly:error (_ "failed files: ~S") (string-join failed))
487 (define (gui-no-files-handler)
488 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
489 ;; FIXME: soft-code, localize
490 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
491 (cmd (get-editor-command welcome-ly 0 0 0)))
492 (ly:message (_ "Invoking `~a'...") cmd)