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)
20 (define scheme-options-definitions
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 (backend ps "which backend to use by default; Options: eps, ps [default], scm, svg, tex, texstr)")
30 (check-internal-types #f "check every property assignment for types")
31 (clip-systems #f "Generate cut-out snippets of a score")
32 (datadir #f "LilyPond prefix for data files (Readonly).")
33 (debug-gc #f "dump memory debugging statistics")
34 (debug-gc-assert-parsed-dead #f "for memory debugging:
35 ensure that all refs to parsed objects are dead. This is an internal option, and is switched on automatically for -ddebug-gc.")
36 (debug-lexer #f "debug the flex lexer")
37 (debug-page-breaking-scoring #f "dump scores for many different page breaking configurations")
38 (debug-parser #f "debug the bison parser")
39 (debug-property-callbacks #f "debug cyclic callback chains")
40 (debug-skylines #f "debug skylines")
41 (delete-intermediate-files #f
42 "delete unusable PostScript files")
43 (dump-profile #f "dump timing information for each file")
44 (dump-signatures #f "dump output signatures of each system. Used for regression testing.")
46 (eps-box-padding #f "Pad EPS bounding box left edge. Guarantee alignment between systems in LaTeX.")
48 "load fonts via Ghostscript.")
49 (gui #f "running from gui; redirect stderr to log file")
50 (help #f "show this help.")
51 (include-book-title-preview #t "include book-titles in preview images.")
52 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
53 (job-count #f "Process in parallel")
54 (log-file #f "redirect output to log FILE.log")
57 "relative for simultaneous music works
58 similar to chord syntax")
59 (point-and-click #t "use point & click")
60 (paper-size "a4" "the default paper size")
61 (pixmap-format "png16m" "GS format to use for pixel images")
62 (preview #f "make a incipit image. ")
63 (print-pages #t "print pages normally. ")
64 (protected-scheme-parsing #t "continue when finding errors in inline
65 scheme are caught in the parser. If off, halt
66 on errors, and print a stack trace.")
67 (profile-property-accesses #f "keep statistics of get_property() calls.")
69 (resolution 101 "resolution for generating PNG bitmaps")
70 (read-file-list #f "Read files to be processed from command line arguments")
72 (safe #f "Run safely")
73 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN.")
74 (separate-log-files #f "Output to FILE.log per file.")
75 (trace-memory-frequency #f "Record Scheme cell usage this many times per second, and dump to file.")
76 (trace-scheme-coverage #f "Record coverage of Scheme files")
78 "how much verbosity for TTF font embedding?")
79 (show-available-fonts #f
80 "List font names available.")
81 (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.
89 (ly:add-option (car x) (cadr x) (caddr x)))
90 scheme-options-definitions)
94 (ly:set-option (car x) (cdr x)))
95 (eval-string (ly:command-line-options)))
99 (if (defined? 'set-debug-cell-accesses!)
100 (set-debug-cell-accesses! #f))
102 ;(set-debug-cell-accesses! 1000)
104 (use-modules (ice-9 regex)
117 (define-public fancy-format format)
118 (define-public (ergonomic-simple-format dest . rest)
119 "Like ice-9 format, but without the memory consumption."
122 (apply simple-format (cons #f (cons dest rest)))
123 (apply simple-format (cons dest rest))))
125 (define format ergonomic-simple-format)
128 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
131 (define-public (print . args)
132 (apply format (cons (current-output-port) args)))
136 ;;; debugging evaluator is slower. This should
137 ;;; have a more sensible default.
139 (if (or (ly:get-option 'verbose)
140 (ly:get-option 'trace-memory-frequencency)
141 (ly:get-option 'trace-scheme-coverage)
144 (ly:set-option 'protected-scheme-parsing #f)
145 (debug-enable 'debug)
146 (debug-enable 'backtrace)
147 (read-enable 'positions)))
150 (if (ly:get-option 'trace-scheme-coverage)
153 (define-public tex-backend?
154 (member (ly:get-option 'backend) '(texstr tex)))
156 (define-public parser #f)
158 (define-public (lilypond-version)
160 (map (lambda (x) (if (symbol? x)
167 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
168 (define-public TEX_STRING_HASHLIMIT 10000000)
172 ;; gettext wrapper for guile < 1.7.2
173 (if (defined? 'gettext)
174 (define-public _ gettext)
175 (define-public _ ly:gettext))
177 (define-public (ly:load x)
178 (let* ((file-name (%search-load-path x)))
179 (if (ly:get-option 'verbose)
180 (ly:progress "[~A" file-name))
182 (ly:error (_ "cannot find: ~A") x))
183 (primitive-load file-name)
184 (if (ly:get-option 'verbose)
188 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
191 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
194 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
198 (define-public PLATFORM
201 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
204 (let ((platform (string-tokenize
205 (vector-ref (uname) 0) char-set:letter+digit)))
206 (if (null? (cdr platform)) #f
207 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
211 (define native-getcwd getcwd)
213 (if (string-index x #\\)
215 (string-regexp-substitute
217 (string-regexp-substitute "\\\\" "/" x))))
218 ;; FIXME: this prints a warning.
219 (define-public (ly-getcwd)
220 (slashify (native-getcwd))))
221 (else (define-public ly-getcwd getcwd)))
223 (define-public (is-absolute? file-name)
224 (let ((file-name-length (string-length file-name)))
225 (if (= file-name-length 0)
227 (or (eq? (string-ref file-name 0) #\/)
228 (and (eq? PLATFORM 'windows)
229 (> file-name-length 2)
230 (eq? (string-ref file-name 1) #\:)
231 (eq? (string-ref file-name 2) #\/))))))
233 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
235 (define (type-check-list location signature arguments)
236 "Typecheck a list of arguments against a list of type
237 predicates. Print a message at LOCATION if any predicate failed."
239 (define (recursion-helper signature arguments count)
240 (define (helper pred? arg count)
241 (if (not (pred? arg))
247 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
248 count (type-name pred?) arg))
252 (if (null? signature)
254 (and (helper (car signature) (car arguments) count)
255 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
257 (recursion-helper signature arguments 1))
259 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263 ;;(define-public (output-framework) (write "hello\n"))
265 (define output-tex-module
266 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
267 (define output-ps-module
268 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
270 (define-public (ps-output-expression expr port)
271 (display (eval expr output-ps-module) port))
273 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
274 ;; Safe definitions utility
275 (define safe-objects (list))
277 (define-macro (define-safe-public arglist . body)
278 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
279 The syntax is the same as `define*-public'."
280 (define (get-symbol arg)
282 (get-symbol (car arg))
284 (let ((safe-symbol (get-symbol arglist)))
286 (define*-public ,arglist
288 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
292 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
295 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
307 "define-event-classes.scm"
308 "define-music-types.scm"
311 "chord-ignatzek-names.scm"
313 "chord-generic-names.scm"
316 "music-functions.scm"
319 "define-music-properties.scm"
323 "parser-ly-from-scheme.scm"
324 "ly-syntax-constructors.scm"
326 "define-context-properties.scm"
327 "translation-functions.scm"
337 "define-markup-commands.scm"
338 "define-grob-properties.scm"
340 "define-grob-interfaces.scm"
341 "define-stencil-commands.scm"
345 "backend-library.scm"
348 ;; must be after everything has been defined
352 (for-each ly:load init-scheme-files)
355 (set! type-p-name-alist
357 (,boolean-or-symbol? . "boolean or symbol")
358 (,boolean? . "boolean")
360 (,grob-list? . "list of grobs")
361 (,hash-table? . "hash table")
362 (,input-port? . "input port")
363 (,integer? . "integer")
365 (,ly:context? . "context")
366 (,ly:dimension? . "dimension, in staff space")
367 (,ly:dir? . "direction")
368 (,ly:duration? . "duration")
369 (,ly:grob? . "layout object")
370 (,ly:input-location? . "input location")
371 (,ly:moment? . "moment")
372 (,ly:music? . "music")
373 (,ly:pitch? . "pitch")
374 (,ly:translator? . "translator")
375 (,ly:font-metric? . "font metric")
376 (,ly:simple-closure? . "simple closure")
377 (,markup-list? . "list of markups")
378 (,markup? . "markup")
379 (,ly:music-list? . "list of music")
380 (,number-or-grob? . "number or grob")
381 (,number-or-string? . "number or string")
382 (,number-pair? . "pair of numbers")
383 (,number? . "number")
384 (,output-port? . "output port")
386 (,procedure? . "procedure")
387 (,rhythmic-location? . "rhythmic location")
388 (,scheme? . "any type")
389 (,string? . "string")
390 (,symbol? . "symbol")
391 (,vector? . "vector")))
393 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
396 (define (profile-measurements)
403 (ly:assoc-get 'gc-time-taken stats))
405 (ly:assoc-get 'total-cells-allocated stats 0)
408 (define (dump-profile base last this)
410 ((outname (format "~a.profile" (basename base ".ly")))
411 (diff (map (lambda (y) (apply - y)) (zip this last))))
413 (ly:progress "\nWriting timing to ~a..." outname)
414 (format (open-file outname "w")
415 "time: ~a\ncells: ~a\n"
421 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
424 (define gc-dumping #f)
425 (define gc-protect-stat-count 0)
427 (define-public (dump-live-object-stats outfile)
430 (format outfile "~a: ~a\n" (car x) (cdr x)))
431 (sort (gc-live-object-stats)
433 (string<? (car x) (car y))))))
435 (define-public (dump-gc-protects)
436 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
437 (let* ((protects (sort
438 (hash-table->alist (ly:protects))
440 (< (object-address (car a))
441 (object-address (car b))))))
443 (out-file-name (string-append
444 "gcstat-" (number->string gc-protect-stat-count)
446 (outfile (open-file out-file-name "w")))
449 (display (format "Dumping GC statistics ~a...\n" out-file-name))
455 (format "~a (~a) = ~a\n" (object-address x) c x)
459 (not (symbol? (car x))))
463 (format outfile "\nprotected symbols: ~a\n"
464 (apply + (map (lambda (obj-count) (if (symbol? (car obj-count))
469 ;; (display (ly:smob-protects))
471 (if (defined? 'gc-live-object-stats)
473 (display "Live object statistics: GC'ing\n")
477 (display "Asserting dead objects\n")
478 (ly:set-option 'debug-gc-assert-parsed-dead #t)
480 (ly:set-option 'debug-gc-assert-parsed-dead #f)
482 (set! stats (gc-live-object-stats))
483 (display "Dumping live object statistics.\n")
484 (dump-live-object-stats outfile)))
494 gc-protect-stat-count
496 (let ((sym-stat (assoc sym stats)))
501 '(protected-objects bytes-malloced cell-heap-size
511 (define (check-memory)
512 "read /proc/self to check up on memory use."
513 (define (gulp-file name)
514 (let* ((file (open-input-file name))
515 (text (read-delimited "" file)))
519 ((stat (gulp-file "/proc/self/status"))
520 (lines (string-split stat #\newline))
521 (interesting (filter identity
524 (string-match "^VmData:[ \t]*([0-9]*) kB" l))
526 (mem (string->number (match:substring (car interesting) 1)))
530 (display (format "VMDATA: ~a\n" mem))
539 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
542 (define (multi-fork count)
543 (define (helper count acc)
546 ((pid (primitive-fork)))
549 (helper (1- count) (cons pid acc))))
554 (define-public (lilypond-main files)
555 "Entry point for LilyPond."
558 (eval-string (ly:command-line-code))
560 (if (ly:get-option 'help)
565 (if (ly:get-option 'show-available-fonts)
567 (ly:font-config-display-fonts)
572 (if (ly:get-option 'gui)
580 (if (ly:get-option 'read-file-list)
583 (> (string-length s) 0))
585 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
589 (if (and (number? (ly:get-option 'job-count))
590 (>= (length files) (ly:get-option 'job-count)))
593 ((count (ly:get-option 'job-count))
594 (split-todo (split-list files count))
595 (joblist (multi-fork count))
598 (if (not (string-or-symbol? (ly:get-option 'log-file)))
599 (ly:set-option 'log-file "lilypond-multi-run"))
601 (if (number? joblist)
603 (ly:set-option 'log-file (format "~a-~a"
604 (ly:get-option 'log-file) joblist))
605 (set! files (vector-ref split-todo joblist)))
608 (ly:progress "\nForking into jobs: ~a\n" joblist)
611 (let* ((stat (cdr (waitpid pid))))
614 (set! errors (acons (list-element-index joblist pid) stat errors)))))
621 (logfile (format "~a-~a.log"
622 (ly:get-option 'log-file) job))
623 (log (ly:gulp-file logfile))
624 (len (string-length log))
625 (tail (substring log (max 0 (- len 1024)))))
627 (if (status:term-sig state)
628 (ly:message "\n\n~a\n"
629 (format (_ "job ~a terminated with signal: ~a")
631 (status:term-sig state)))
632 (ly:message (_ "logfile ~a (exit ~a):\n~a") logfile (status:exit-val state) tail))))
637 (ly:error "Children ~a exited with errors." (map car errors)))
639 ;; must overwrite individual entries
640 (if (ly:get-option 'dump-profile)
641 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
643 (exit (if (null? errors) 0 1))))))
646 (if (string-or-symbol? (ly:get-option 'log-file))
647 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
650 (let ((failed (lilypond-all files)))
651 (if (ly:get-option 'trace-scheme-coverage)
653 (coverage:show-all (lambda (f) (string-contains f "lilypond"))
659 (ly:error (_ "failed files: ~S") (string-join failed))
662 ;; HACK: be sure to exit with single newline
666 (define-public (lilypond-all files)
668 (separate-logs (ly:get-option 'separate-log-files))
669 (do-measurements (ly:get-option 'dump-profile))
670 (handler (lambda (key failed-file)
671 (set! failed (append (list failed-file) failed)))))
677 ((start-measurements (if do-measurements
678 (profile-measurements)
680 (base (basename x ".ly"))
681 (all-settings (ly:all-options)))
684 (ly:stderr-redirect (format "~a.log" base) "w"))
685 (if (ly:get-option 'trace-memory-frequency)
686 (mtrace:start-trace (ly:get-option 'trace-memory-frequency)))
688 (lilypond-file handler x)
689 (if start-measurements
690 (dump-profile x start-measurements (profile-measurements)))
692 (if (ly:get-option 'trace-memory-frequency)
695 (mtrace:dump-results base)))
699 (ly:set-option (car s) (cdr s)))
702 (ly:clear-anonymous-modules)
703 (ly:set-option 'debug-gc-assert-parsed-dead #t)
705 (ly:set-option 'debug-gc-assert-parsed-dead #f)
708 (if (ly:get-option 'debug-gc)
710 (if (= (random 40) 1)
711 (ly:reset-all-fonts)))))
715 ;; we want the failed-files notice in the aggregrate logfile.
716 (if (ly:get-option 'separate-logs)
718 (if (string-or-symbol? (ly:get-option 'log-file))
719 (format "~a.log" (ly:get-option 'log-file))
722 (if (ly:get-option 'dump-profile)
723 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
727 (define (lilypond-file handler file-name)
728 (catch 'ly-file-failed
729 (lambda () (ly:parse-file file-name))
730 (lambda (x . args) (handler x file-name))))
732 (use-modules (scm editor))
734 (define-public (gui-main files)
736 (gui-no-files-handler))
738 (if (not (string? (ly:get-option 'log-file)))
739 (let* ((base (basename (car files) ".ly"))
740 (log-name (string-append base ".log")))
741 (if (not (ly:get-option 'gui))
742 (ly:message (_ "Redirecting output to ~a...") log-name))
743 (ly:stderr-redirect log-name "w")
744 (ly:message "# -*-compilation-*-"))
746 (let ((failed (lilypond-all files)))
750 (ly:stderr-redirect "foo" "r")
751 (system (get-editor-command log-name 0 0 0))
752 (ly:error (_ "failed files: ~S") (string-join failed))
757 (define (gui-no-files-handler)
758 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
759 ;; FIXME: soft-code, localize
760 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
761 (cmd (get-editor-command welcome-ly 0 0 0)))
762 (ly:message (_ "Invoking `~a'...") cmd)