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 (read-enable 'positions)
22 (define scheme-options-definitions
27 ;; - [subject-]object-object-verb +"ing"
28 ;; - [subject-]-verb-object-object
30 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
31 (backend ps "which backend to use by default; Options: eps, ps [default], scm, svg, tex, texstr)")
32 (check-internal-types #f "check every property assignment for types")
33 (clip-systems #f "Generate cut-out snippets of a score")
34 (datadir #f "LilyPond prefix for data files (Readonly).")
35 (debug-gc #f "dump memory debugging statistics")
36 (debug-gc-assert-parsed-dead #f "for memory debugging:
37 ensure that all refs to parsed objects are dead. This is an internal option, and is switched on automatically for -ddebug-gc.")
38 (debug-lexer #f "debug the flex lexer")
39 (debug-page-breaking-scoring #f "dump scores for many different page breaking configurations")
40 (debug-parser #f "debug the bison parser")
41 (debug-property-callbacks #f "debug cyclic callback chains")
42 (debug-skylines #f "debug skylines")
43 (delete-intermediate-files #f
44 "delete unusable PostScript files")
45 (dump-profile #f "dump timing information for each file")
46 (dump-signatures #f "dump output signatures of each system. Used for regression testing.")
48 (eps-box-padding #f "Pad EPS bounding box left edge. Guarantee alignment between systems in LaTeX.")
50 "load fonts via Ghostscript.")
51 (gui #f "running from gui; redirect stderr to log file")
52 (help #f "show this help.")
53 (include-book-title-preview #t "include book-titles in preview images.")
54 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
55 (job-count #f "Process in parallel")
56 (log-file #f "redirect output to log FILE.log")
59 "relative for simultaneous music works
60 similar to chord syntax")
61 (point-and-click #t "use point & click")
62 (paper-size "a4" "the default paper size")
63 (pixmap-format "png16m" "GS format to use for pixel images")
64 (preview #f "make a incipit image. ")
65 (print-pages #t "print pages normally. ")
66 (protected-scheme-parsing #t "continue when finding errors in inline
67 scheme are caught in the parser. If off, halt
68 on errors, and print a stack trace.")
69 (profile-property-accesses #f "keep statistics of get_property() calls.")
71 (resolution 101 "resolution for generating PNG bitmaps")
72 (read-file-list #f "Read files to be processed from command line arguments")
74 (safe #f "Run safely")
75 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN.")
76 (separate-log-files #f "Output to FILE.log per file.")
77 (trace-memory-frequency #f "Record Scheme cell usage this many times per second, and dump to file.")
78 (trace-scheme-coverage #f "Record coverage of Scheme files")
80 "how much verbosity for TTF font embedding?")
81 (show-available-fonts #f
82 "List font names available.")
83 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
86 ;; need to do this in the beginning. Other parts of the
87 ;; Scheme init depend on these options.
91 (ly:add-option (car x) (cadr x) (caddr x)))
92 scheme-options-definitions)
96 (ly:set-option (car x) (cdr x)))
97 (eval-string (ly:command-line-options)))
101 (if (defined? 'set-debug-cell-accesses!)
102 (set-debug-cell-accesses! #f))
104 ;(set-debug-cell-accesses! 1000)
106 (use-modules (ice-9 regex)
120 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
123 (define-public (print . args)
124 (apply format (cons (current-output-port) args)))
128 ;;; debugging evaluator is slower. This should
129 ;;; have a more sensible default.
131 (if (or (ly:get-option 'verbose)
132 (ly:get-option 'trace-memory-frequencency)
133 (ly:get-option 'trace-scheme-coverage)
136 (ly:set-option 'protected-scheme-parsing #f)
137 (debug-enable 'debug)
138 (debug-enable 'backtrace)
139 (read-enable 'positions)))
142 (if (ly:get-option 'trace-scheme-coverage)
145 (define-public tex-backend?
146 (member (ly:get-option 'backend) '(texstr tex)))
148 (define-public parser #f)
150 (define-public (lilypond-version)
152 (map (lambda (x) (if (symbol? x)
159 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
160 (define-public TEX_STRING_HASHLIMIT 10000000)
164 ;; gettext wrapper for guile < 1.7.2
165 (if (defined? 'gettext)
166 (define-public _ gettext)
167 (define-public _ ly:gettext))
169 (define-public (ly:load x)
170 (let* ((file-name (%search-load-path x)))
171 (if (ly:get-option 'verbose)
172 (ly:progress "[~A" file-name))
174 (ly:error (_ "cannot find: ~A") x))
175 (primitive-load file-name)
176 (if (ly:get-option 'verbose)
180 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
183 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
186 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
190 (define-public PLATFORM
193 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
196 (let ((platform (string-tokenize
197 (vector-ref (uname) 0) char-set:letter+digit)))
198 (if (null? (cdr platform)) #f
199 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
203 (define native-getcwd getcwd)
205 (if (string-index x #\\)
207 (string-regexp-substitute
209 (string-regexp-substitute "\\\\" "/" x))))
210 ;; FIXME: this prints a warning.
211 (define-public (ly-getcwd)
212 (slashify (native-getcwd))))
213 (else (define-public ly-getcwd getcwd)))
215 (define-public (is-absolute? file-name)
216 (let ((file-name-length (string-length file-name)))
217 (if (= file-name-length 0)
219 (or (eq? (string-ref file-name 0) #\/)
220 (and (eq? PLATFORM 'windows)
221 (> file-name-length 2)
222 (eq? (string-ref file-name 1) #\:)
223 (eq? (string-ref file-name 2) #\/))))))
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 (define (type-check-list location signature arguments)
228 "Typecheck a list of arguments against a list of type
229 predicates. Print a message at LOCATION if any predicate failed."
231 (define (recursion-helper signature arguments count)
232 (define (helper pred? arg count)
233 (if (not (pred? arg))
239 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
240 count (type-name pred?) arg))
244 (if (null? signature)
246 (and (helper (car signature) (car arguments) count)
247 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
249 (recursion-helper signature arguments 1))
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 ;;(define-public (output-framework) (write "hello\n"))
257 (define output-tex-module
258 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
259 (define output-ps-module
260 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
262 (define-public (ps-output-expression expr port)
263 (display (eval expr output-ps-module) port))
265 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 ;; Safe definitions utility
267 (define safe-objects (list))
269 (define-macro (define-safe-public arglist . body)
270 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
271 The syntax is the same as `define*-public'."
272 (define (get-symbol arg)
274 (get-symbol (car arg))
276 (let ((safe-symbol (get-symbol arglist)))
278 (define*-public ,arglist
280 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 "define-event-classes.scm"
300 "define-music-types.scm"
303 "chord-ignatzek-names.scm"
305 "chord-generic-names.scm"
308 "music-functions.scm"
311 "define-music-properties.scm"
315 "parser-ly-from-scheme.scm"
316 "ly-syntax-constructors.scm"
318 "define-context-properties.scm"
319 "translation-functions.scm"
329 "define-markup-commands.scm"
330 "define-grob-properties.scm"
332 "define-grob-interfaces.scm"
333 "define-stencil-commands.scm"
337 "backend-library.scm"
340 ;; must be after everything has been defined
344 (for-each ly:load init-scheme-files)
347 (set! type-p-name-alist
349 (,boolean-or-symbol? . "boolean or symbol")
350 (,boolean? . "boolean")
352 (,grob-list? . "list of grobs")
353 (,hash-table? . "hash table")
354 (,input-port? . "input port")
355 (,integer? . "integer")
357 (,ly:context? . "context")
358 (,ly:dimension? . "dimension, in staff space")
359 (,ly:dir? . "direction")
360 (,ly:duration? . "duration")
361 (,ly:grob? . "layout object")
362 (,ly:input-location? . "input location")
363 (,ly:moment? . "moment")
364 (,ly:music? . "music")
365 (,ly:pitch? . "pitch")
366 (,ly:translator? . "translator")
367 (,ly:font-metric? . "font metric")
368 (,ly:simple-closure? . "simple closure")
369 (,markup-list? . "list of markups")
370 (,markup? . "markup")
371 (,ly:music-list? . "list of music")
372 (,number-or-grob? . "number or grob")
373 (,number-or-string? . "number or string")
374 (,number-pair? . "pair of numbers")
375 (,number? . "number")
376 (,output-port? . "output port")
378 (,procedure? . "procedure")
379 (,rhythmic-location? . "rhythmic location")
380 (,scheme? . "any type")
381 (,string? . "string")
382 (,symbol? . "symbol")
383 (,vector? . "vector")))
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 (define (profile-measurements)
395 (ly:assoc-get 'gc-time-taken stats))
397 (ly:assoc-get 'total-cells-allocated stats 0)
400 (define (dump-profile base last this)
402 ((outname (format "~a.profile" (basename base ".ly")))
403 (diff (map (lambda (y) (apply - y)) (zip this last))))
405 (ly:progress "\nWriting timing to ~a..." outname)
406 (format (open-file outname "w")
407 "time: ~a\ncells: ~a\n"
413 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
416 (define gc-dumping #f)
417 (define gc-protect-stat-count 0)
419 (define-public (dump-live-object-stats outfile)
422 (format outfile "~a: ~a\n" (car x) (cdr x)))
423 (sort (gc-live-object-stats)
425 (string<? (car x) (car y))))))
427 (define-public (dump-gc-protects)
428 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
429 (let* ((protects (sort
430 (hash-table->alist (ly:protects))
432 (< (object-address (car a))
433 (object-address (car b))))))
435 (out-file-name (string-append
436 "gcstat-" (number->string gc-protect-stat-count)
438 (outfile (open-file out-file-name "w")))
441 (display (format "Dumping GC statistics ~a...\n" out-file-name))
447 (format "~a (~a) = ~a\n" (object-address x) c x)
451 (not (symbol? (car x))))
455 (format outfile "\nprotected symbols: ~a\n"
456 (apply + (map (lambda (obj-count) (if (symbol? (car obj-count))
461 ;; (display (ly:smob-protects))
463 (if (defined? 'gc-live-object-stats)
465 (display "Live object statistics: GC'ing\n")
469 (display "Asserting dead objects\n")
470 (ly:set-option 'debug-gc-assert-parsed-dead #t)
472 (ly:set-option 'debug-gc-assert-parsed-dead #f)
474 (set! stats (gc-live-object-stats))
475 (display "Dumping live object statistics.\n")
476 (dump-live-object-stats outfile)))
486 gc-protect-stat-count
488 (let ((sym-stat (assoc sym stats)))
493 '(protected-objects bytes-malloced cell-heap-size
503 (define (check-memory)
504 "read /proc/self to check up on memory use."
505 (define (gulp-file name)
506 (let* ((file (open-input-file name))
507 (text (read-delimited "" file)))
511 ((stat (gulp-file "/proc/self/status"))
512 (lines (string-split stat #\newline))
513 (interesting (filter identity
516 (string-match "^VmData:[ \t]*([0-9]*) kB" l))
518 (mem (string->number (match:substring (car interesting) 1)))
522 (display (format "VMDATA: ~a\n" mem))
531 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
534 (define (multi-fork count)
535 (define (helper count acc)
538 ((pid (primitive-fork)))
541 (helper (1- count) (cons pid acc))))
546 (define-public (lilypond-main files)
547 "Entry point for LilyPond."
550 (eval-string (ly:command-line-code))
552 (if (ly:get-option 'help)
557 (if (ly:get-option 'show-available-fonts)
559 (ly:font-config-display-fonts)
564 (if (ly:get-option 'gui)
572 (if (ly:get-option 'read-file-list)
575 (> (string-length s) 0))
577 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
581 (if (and (number? (ly:get-option 'job-count))
582 (>= (length files) (ly:get-option 'job-count)))
585 ((count (ly:get-option 'job-count))
586 (split-todo (split-list files count))
587 (joblist (multi-fork count))
590 (if (not (string-or-symbol? (ly:get-option 'log-file)))
591 (ly:set-option 'log-file "lilypond-multi-run"))
593 (if (number? joblist)
595 (ly:set-option 'log-file (format "~a-~a"
596 (ly:get-option 'log-file) joblist))
597 (set! files (vector-ref split-todo joblist)))
600 (ly:progress "\nForking into jobs: ~a\n" joblist)
603 (let* ((stat (cdr (waitpid pid))))
606 (set! errors (acons (list-element-index joblist pid) stat errors)))))
613 (logfile (format "~a-~a.log"
614 (ly:get-option 'log-file) job))
615 (log (ly:gulp-file logfile))
616 (len (string-length log))
617 (tail (substring log (max 0 (- len 1024)))))
619 (if (status:term-sig state)
620 (ly:message "\n\n~a\n"
621 (format (_ "job ~a terminated with signal: ~a")
623 (status:term-sig state)))
624 (ly:message (_ "logfile ~a (exit ~a):\n~a") logfile (status:exit-val state) tail))))
629 (ly:error "Children ~a exited with errors." (map car errors)))
631 ;; must overwrite individual entries
632 (if (ly:get-option 'dump-profile)
633 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
635 (exit (if (null? errors) 0 1))))))
638 (if (string-or-symbol? (ly:get-option 'log-file))
639 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
642 (let ((failed (lilypond-all files)))
643 (if (ly:get-option 'trace-scheme-coverage)
645 (coverage:show-all (lambda (f) (string-contains f "lilypond"))
651 (ly:error (_ "failed files: ~S") (string-join failed))
654 ;; HACK: be sure to exit with single newline
658 (define-public (lilypond-all files)
660 (separate-logs (ly:get-option 'separate-log-files))
661 (do-measurements (ly:get-option 'dump-profile))
662 (handler (lambda (key failed-file)
663 (set! failed (append (list failed-file) failed)))))
669 ((start-measurements (if do-measurements
670 (profile-measurements)
672 (base (basename x ".ly"))
673 (all-settings (ly:all-options)))
676 (ly:stderr-redirect (format "~a.log" base) "w"))
677 (if (ly:get-option 'trace-memory-frequency)
678 (mtrace:start-trace (ly:get-option 'trace-memory-frequency)))
680 (lilypond-file handler x)
681 (if start-measurements
682 (dump-profile x start-measurements (profile-measurements)))
684 (if (ly:get-option 'trace-memory-frequency)
687 (mtrace:dump-results base)))
691 (ly:set-option (car s) (cdr s)))
694 (ly:clear-anonymous-modules)
695 (ly:set-option 'debug-gc-assert-parsed-dead #t)
697 (ly:set-option 'debug-gc-assert-parsed-dead #f)
700 (if (ly:get-option 'debug-gc)
702 (if (= (random 40) 1)
703 (ly:reset-all-fonts)))))
707 ;; we want the failed-files notice in the aggregrate logfile.
708 (if (ly:get-option 'separate-logs)
710 (if (string-or-symbol? (ly:get-option 'log-file))
711 (format "~a.log" (ly:get-option 'log-file))
714 (if (ly:get-option 'dump-profile)
715 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
719 (define (lilypond-file handler file-name)
720 (catch 'ly-file-failed
721 (lambda () (ly:parse-file file-name))
722 (lambda (x . args) (handler x file-name))))
724 (use-modules (scm editor))
726 (define-public (gui-main files)
728 (gui-no-files-handler))
730 (if (not (string? (ly:get-option 'log-file)))
731 (let* ((base (basename (car files) ".ly"))
732 (log-name (string-append base ".log")))
733 (if (not (ly:get-option 'gui))
734 (ly:message (_ "Redirecting output to ~a...") log-name))
735 (ly:stderr-redirect log-name "w")
736 (ly:message "# -*-compilation-*-"))
738 (let ((failed (lilypond-all files)))
742 (ly:stderr-redirect "foo" "r")
743 (system (get-editor-command log-name 0 0 0))
744 (ly:error (_ "failed files: ~S") (string-join failed))
749 (define (gui-no-files-handler)
750 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
751 ;; FIXME: soft-code, localize
752 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
753 (cmd (get-editor-command welcome-ly 0 0 0)))
754 (ly:message (_ "Invoking `~a'...") cmd)