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>
8 ;; Internationalisation: (_i "to be translated") gets an entry in the
9 ;; POT file (gettext ) must be invoked explicitely to do the actual
11 ;;(define-macro (_i x) x)
12 ;;(define-macro-public _i (x) x)
13 ;;(define-public-macro _i (x) x)
15 (defmacro-public _i (x) x)
17 (define (define-scheme-options)
19 (ly:add-option (car x) (cadr x) (caddr x)))
25 ;; - [subject-]object-object-verb +"ing"
26 ;; - [subject-]-verb-object-object
28 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
29 (check-internal-types #f "check every property assignment for types")
30 (clip-systems #f "Generate cut-out snippets of a score")
31 (debug-gc #f "dump memory debugging statistics")
32 (debug-gc-assert-parsed-dead #f "for memory debugging:
33 ensure that all refs to parsed objects are dead. This is an internal option, and is switched on automatically for -ddebug-gc.")
34 (debug-lexer #f "debug the flex lexer")
35 (debug-midi #f "generate human readable MIDI")
36 (debug-parser #f "debug the bison parser")
37 (debug-skylines #f "debug skylines")
38 (delete-intermediate-files #f
39 "delete unusable PostScript files")
40 (dump-profile #f "dump timing information for each file")
41 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
42 (dump-signatures #f "dump output signatures of each system")
44 (eps-box-padding #f "Pad EPS bounding box left edge by this much to guarantee alignment between systems")
47 "load fonts via Ghostscript.")
48 (gui #f "running from gui; redirect stderr to log file")
50 (include-book-title-preview #t "include book-titles in preview images.")
51 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
52 (job-count #f "Process in parallel")
53 (log-file #f "redirect output to log FILE.log")
56 "relative for simultaneous music works
57 similar to chord syntax")
59 "experimental mechanism for remembering tweaks")
60 (point-and-click #t "use point & click")
61 (paper-size "a4" "the default paper size")
62 (pixmap-format "png16m" "GS format to use for pixel images")
63 (protected-scheme-parsing #t "continue when finding errors in inline
64 scheme are caught in the parser. If off, halt
65 on errors, and print a stack trace.")
66 (profile-property-accesses #f "keep statistics of get_property() calls.")
68 (resolution 101 "resolution for generating PNG bitmaps")
69 (read-file-list #f "Read files to be processed from command line arguments")
71 (safe #f "Run safely")
72 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN.")
73 (separate-log-files #f "Output to FILE.log per file.")
75 "how much verbosity for TTF font embedding?")
77 (show-available-fonts #f
78 "List font names available.")
80 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
84 ;; need to do this in the beginning. Other parts of the
85 ;; Scheme init depend on these options.
87 (define-scheme-options)
91 (if (defined? 'set-debug-cell-accesses!)
92 (set-debug-cell-accesses! #f))
94 ;(set-debug-cell-accesses! 1000)
96 (use-modules (ice-9 regex)
109 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
112 (define-public (print . args)
113 (apply format (cons (current-output-port) args)))
117 ;;; debugging evaluator is slower. This should
118 ;;; have a more sensible default.
120 (if (ly:get-option 'verbose)
122 (ly:set-option 'protected-scheme-parsing #f)
123 (debug-enable 'debug)
124 (debug-enable 'backtrace)
125 (read-enable 'positions)))
127 (define-public tex-backend?
128 (member (ly:output-backend) '("texstr" "tex")))
130 (define-public parser #f)
132 (define-public (lilypond-version)
134 (map (lambda (x) (if (symbol? x)
141 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
142 (define-public TEX_STRING_HASHLIMIT 10000000)
146 ;; gettext wrapper for guile < 1.7.2
147 (if (defined? 'gettext)
148 (define-public _ gettext)
149 (define-public _ ly:gettext))
151 (define-public (ly:load x)
152 (let* ((file-name (%search-load-path x)))
153 (if (ly:get-option 'verbose)
154 (ly:progress "[~A" file-name))
156 (ly:error (_ "cannot find: ~A") x))
157 (primitive-load file-name)
158 (if (ly:get-option 'verbose)
162 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
165 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
168 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
172 (define-public PLATFORM
175 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
178 (let ((platform (string-tokenize
179 (vector-ref (uname) 0) char-set:letter+digit)))
180 (if (null? (cdr platform)) #f
181 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
185 (define native-getcwd getcwd)
187 (if (string-index x #\\)
189 (string-regexp-substitute
191 (string-regexp-substitute "\\\\" "/" x))))
192 ;; FIXME: this prints a warning.
193 (define-public (ly-getcwd)
194 (slashify (native-getcwd))))
195 (else (define-public ly-getcwd getcwd)))
197 (define-public (is-absolute? file-name)
198 (let ((file-name-length (string-length file-name)))
199 (if (= file-name-length 0)
201 (or (eq? (string-ref file-name 0) #\/)
202 (and (eq? PLATFORM 'windows)
203 (> file-name-length 2)
204 (eq? (string-ref file-name 1) #\:)
205 (eq? (string-ref file-name 2) #\/))))))
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
209 (define (type-check-list location signature arguments)
210 "Typecheck a list of arguments against a list of type
211 predicates. Print a message at LOCATION if any predicate failed."
213 (define (recursion-helper signature arguments count)
214 (define (helper pred? arg count)
215 (if (not (pred? arg))
221 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
222 count (type-name pred?) arg))
226 (if (null? signature)
228 (and (helper (car signature) (car arguments) count)
229 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
231 (recursion-helper signature arguments 1))
233 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
237 ;;(define-public (output-framework) (write "hello\n"))
239 (define output-tex-module
240 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
241 (define output-ps-module
242 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
244 (define-public (ps-output-expression expr port)
245 (display (eval expr output-ps-module) port))
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 ;; Safe definitions utility
249 (define safe-objects (list))
251 (define-macro (define-safe-public arglist . body)
252 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
253 The syntax is the same as `define*-public'."
254 (define (get-symbol arg)
256 (get-symbol (car arg))
258 (let ((safe-symbol (get-symbol arglist)))
260 (define*-public ,arglist
262 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
269 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
273 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
280 "define-event-classes.scm"
281 "define-music-types.scm"
284 "chord-ignatzek-names.scm"
286 "chord-generic-names.scm"
289 "music-functions.scm"
292 "define-music-properties.scm"
296 "parser-ly-from-scheme.scm"
297 "ly-syntax-constructors.scm"
299 "define-context-properties.scm"
300 "translation-functions.scm"
310 "define-markup-commands.scm"
311 "define-grob-properties.scm"
313 "define-grob-interfaces.scm"
314 "define-stencil-commands.scm"
318 "backend-library.scm"
321 ;; must be after everything has been defined
325 (set! type-p-name-alist
327 (,boolean-or-symbol? . "boolean or symbol")
328 (,boolean? . "boolean")
330 (,grob-list? . "list of grobs")
331 (,hash-table? . "hash table")
332 (,input-port? . "input port")
333 (,integer? . "integer")
335 (,ly:context? . "context")
336 (,ly:dimension? . "dimension, in staff space")
337 (,ly:dir? . "direction")
338 (,ly:duration? . "duration")
339 (,ly:grob? . "layout object")
340 (,ly:input-location? . "input location")
341 (,ly:moment? . "moment")
342 (,ly:music? . "music")
343 (,ly:pitch? . "pitch")
344 (,ly:translator? . "translator")
345 (,ly:font-metric? . "font metric")
346 (,ly:simple-closure? . "simple closure")
347 (,markup-list? . "list of markups")
348 (,markup? . "markup")
349 (,ly:music-list? . "list of music")
350 (,number-or-grob? . "number or grob")
351 (,number-or-string? . "number or string")
352 (,number-pair? . "pair of numbers")
353 (,number? . "number")
354 (,output-port? . "output port")
356 (,procedure? . "procedure")
357 (,rhythmic-location? . "rhythmic location")
358 (,scheme? . "any type")
359 (,string? . "string")
360 (,symbol? . "symbol")
361 (,vector? . "vector")))
363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
366 (define (profile-measurements)
372 (ly:assoc-get 'gc-time-taken stats))
375 (ly:assoc-get 'total-cells-allocated stats 0)
376 ;; difficult to put memory amount stats into here.
380 (define (dump-profile name last this)
382 ((outname (format "~a.profile" (basename name ".ly")))
383 (diff (map (lambda (y) (apply - y)) (zip this last))))
385 (ly:progress "\nWriting timing to ~a..." outname)
386 (format (open-file outname "w")
387 "time: ~a\ncells: ~a\n"
393 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
396 (define gc-dumping #f)
397 (define gc-protect-stat-count 0)
398 (define-public (dump-gc-protects)
399 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
400 (let* ((protects (sort
401 (hash-table->alist (ly:protects))
403 (< (object-address (car a))
404 (object-address (car b))))))
406 (out-file-name (string-append
407 "gcstat-" (number->string gc-protect-stat-count)
409 (outfile (open-file out-file-name "w")))
412 (display (format "Dumping GC statistics ~a...\n" out-file-name))
418 (format "~a (~a) = ~a\n" (object-address x) c x)
422 (not (symbol? (car x))))
426 (format outfile "\nprotected symbols: ~a\n"
427 (length (filter symbol? (map car protects))))
431 ;; (display (ly:smob-protects))
433 (if (defined? 'gc-live-object-stats)
435 (display "Live object statistics: GC'ing\n")
439 (display "Asserting dead objects\n")
440 (ly:set-option 'debug-gc-assert-parsed-dead #t)
442 (ly:set-option 'debug-gc-assert-parsed-dead #f)
444 (set! stats (gc-live-object-stats))
445 (display "Dumping live object statistics.\n")
449 (format outfile "~a: ~a\n" (car x) (cdr x)))
450 (sort (gc-live-object-stats)
452 (string<? (car x) (car y)))))))
463 gc-protect-stat-count
465 (let ((sym-stat (assoc sym stats)))
470 '(protected-objects bytes-malloced cell-heap-size
478 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
481 (define (multi-fork count)
482 (define (helper count acc)
485 ((pid (primitive-fork)))
488 (helper (1- count) (cons pid acc))))
493 (define-public (lilypond-main files)
494 "Entry point for LilyPond."
496 (define (no-files-handler)
500 (if (ly:get-option 'gui)
506 (if (ly:get-option 'read-file-list)
509 (> (string-length s) 0))
511 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
515 (if (and (number? (ly:get-option 'job-count))
516 (> (length files) (ly:get-option 'job-count)))
519 ((count (ly:get-option 'job-count))
520 (split-todo (split-list files count))
521 (joblist (multi-fork count))
524 (if (not (string-or-symbol? (ly:get-option 'log-file)))
525 (ly:set-option 'log-file "lilypond-multi-run"))
527 (if (number? joblist)
529 (ly:set-option 'log-file (format "~a-~a"
530 (ly:get-option 'log-file) joblist))
531 (set! files (vector-ref split-todo joblist)))
534 (ly:progress "\nForking into jobs: ~a\n" joblist)
537 (let* ((stat (cdr (waitpid pid))))
540 (set! errors (cons (list-element-index joblist pid) errors)))))
545 (let* ((logfile (format "~a-~a.log"
546 (ly:get-option 'log-file) x))
547 (log (ly:gulp-file logfile))
548 (len (string-length log))
549 (tail (substring log (max 0 (- len 1024)))))
551 (display (format "\n\nlogfile ~a:\n\n ~a" logfile tail))))
556 (ly:error "Children ~a exited with errors." errors))
558 (exit (if (null? errors) 0 1))))))
561 (if (string-or-symbol? (ly:get-option 'log-file))
562 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
564 (let ((failed (lilypond-all files)))
567 (ly:error (_ "failed files: ~S") (string-join failed))
570 ;; HACK: be sure to exit with single newline
574 (define-public (lilypond-all files)
577 (if (ly:get-option 'show-available-fonts)
579 (ly:font-config-display-fonts)
584 (separate-logs (ly:get-option 'separate-log-files))
585 (do-measurements (ly:get-option 'dump-profile))
586 (handler (lambda (key failed-file)
587 (set! failed (append (list failed-file) failed)))))
592 ((start-measurements (if do-measurements
595 (profile-measurements))
597 (base (basename x ".ly"))
598 (all-settings (ly:all-options)))
601 (ly:stderr-redirect (format "~a.log" base) "w"))
603 (lilypond-file handler x)
604 (if start-measurements
605 (dump-profile x start-measurements (profile-measurements)))
609 (ly:set-option (car s) (cdr s)))
612 (ly:clear-anonymous-modules)
613 (if (ly:get-option 'debug-gc)
615 (if (= (random 40) 1)
616 (ly:reset-all-fonts)))))
620 ;; we want the failed-files notice in the aggregrate logfile.
621 (if (ly:get-option 'separate-logs)
623 (if (string-or-symbol? (ly:get-option 'log-file))
624 (format "~a.log" (ly:get-option 'log-file))
630 (define (lilypond-file handler file-name)
631 (catch 'ly-file-failed
632 (lambda () (ly:parse-file file-name))
633 (lambda (x . args) (handler x file-name))))
635 (use-modules (scm editor))
637 (define-public (gui-main files)
639 (gui-no-files-handler))
641 (if (not (string? (ly:get-option 'log-file)))
642 (let* ((base (basename (car files) ".ly"))
643 (log-name (string-append base ".log")))
644 (if (not (ly:get-option 'gui))
645 (ly:message (_ "Redirecting output to ~a...") log-name))
646 (ly:stderr-redirect log-name "w")
647 (ly:message "# -*-compilation-*-"))
649 (let ((failed (lilypond-all files)))
653 (ly:stderr-redirect "foo" "r")
654 (system (get-editor-command log-name 0 0 0))
655 (ly:error (_ "failed files: ~S") (string-join failed))
660 (define (gui-no-files-handler)
661 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
662 ;; FIXME: soft-code, localize
663 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
664 (cmd (get-editor-command welcome-ly 0 0 0)))
665 (ly:message (_ "Invoking `~a'...") cmd)