1 ;;;; lily.scm -- toplevel Scheme stuff
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2007 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 memory and time information for each file")
44 (dump-cpu-profile #f "dump timing information (system-dependent)")
45 (dump-signatures #f "dump output signatures of each system. Used for regression testing.")
47 (eps-box-padding #f "Pad EPS bounding box left edge. Guarantee alignment between systems in LaTeX.")
49 "load fonts via Ghostscript.")
50 (gs-load-lily-fonts #f
51 "load only lilypond fonts via Ghostscript.")
52 (gui #f "running from gui; redirect stderr to log file")
53 (help #f "show this help.")
54 (include-book-title-preview #t "include book-titles in preview images.")
55 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
56 (job-count #f "Process in parallel")
57 (log-file #f "redirect output to log FILE.log")
60 "relative for simultaneous music works
61 similar to chord syntax")
62 (point-and-click #t "use point & click")
63 (paper-size "a4" "the default paper size")
64 (pixmap-format "png16m" "GS format to use for pixel images")
65 (preview #f "make a incipit image. ")
66 (print-pages #t "print pages normally. ")
67 (protected-scheme-parsing #t "continue when finding errors in inline
68 scheme are caught in the parser. If off, halt
69 on errors, and print a stack trace.")
70 (profile-property-accesses #f "keep statistics of get_property() calls.")
72 (resolution 101 "resolution for generating PNG bitmaps")
73 (read-file-list #f "Read files to be processed from command line arguments")
75 (safe #f "Run safely")
76 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN.")
77 (strip-output-dir #t "If yes, strip directories from input files.")
78 (separate-log-files #f "Output to FILE.log per file.")
79 (trace-memory-frequency #f "Record Scheme cell usage this many times per second, and dump to file.")
80 (trace-scheme-coverage #f "Record coverage of Scheme files")
82 "how much verbosity for TTF font embedding?")
83 (show-available-fonts #f
84 "List font names available.")
85 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
88 ;; need to do this in the beginning. Other parts of the
89 ;; Scheme init depend on these options.
93 (ly:add-option (car x) (cadr x) (caddr x)))
94 scheme-options-definitions)
98 (ly:set-option (car x) (cdr x)))
99 (eval-string (ly:command-line-options)))
103 (if (defined? 'set-debug-cell-accesses!)
104 (set-debug-cell-accesses! #f))
106 ;(set-debug-cell-accesses! 1000)
108 (use-modules (ice-9 regex)
121 (define-public fancy-format format)
122 (define-public (ergonomic-simple-format dest . rest)
123 "Like ice-9 format, but without the memory consumption."
126 (apply simple-format (cons #f (cons dest rest)))
127 (apply simple-format (cons dest rest))))
129 (define format ergonomic-simple-format)
132 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
135 (define-public (print . args)
136 (apply format (cons (current-output-port) args)))
140 ;;; debugging evaluator is slower. This should
141 ;;; have a more sensible default.
143 (if (or (ly:get-option 'verbose)
144 (ly:get-option 'trace-memory-frequency)
145 (ly:get-option 'trace-scheme-coverage)
148 (ly:set-option 'protected-scheme-parsing #f)
149 (debug-enable 'debug)
150 (debug-enable 'backtrace)
151 (read-enable 'positions)))
154 (if (ly:get-option 'trace-scheme-coverage)
157 (define-public tex-backend?
158 (member (ly:get-option 'backend) '(texstr tex)))
160 (define-public parser #f)
163 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
164 (define-public TEX_STRING_HASHLIMIT 10000000)
168 ;; gettext wrapper for guile < 1.7.2
169 (if (defined? 'gettext)
170 (define-public _ gettext)
171 (define-public _ ly:gettext))
173 (define-public (ly:load x)
174 (let* ((file-name (%search-load-path x)))
175 (if (ly:get-option 'verbose)
176 (ly:progress "[~A" file-name))
178 (ly:error (_ "cannot find: ~A") x))
179 (primitive-load file-name)
180 (if (ly:get-option 'verbose)
184 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
187 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
190 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
194 (define-public PLATFORM
197 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
200 (let ((platform (string-tokenize
201 (vector-ref (uname) 0) char-set:letter+digit)))
202 (if (null? (cdr platform)) #f
203 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
207 (define native-getcwd getcwd)
209 (if (string-index x #\\)
211 (string-regexp-substitute
213 (string-regexp-substitute "\\\\" "/" x))))
214 ;; FIXME: this prints a warning.
215 (define-public (ly-getcwd)
216 (slashify (native-getcwd))))
217 (else (define-public ly-getcwd getcwd)))
219 (define-public (is-absolute? file-name)
220 (let ((file-name-length (string-length file-name)))
221 (if (= file-name-length 0)
223 (or (eq? (string-ref file-name 0) #\/)
224 (and (eq? PLATFORM 'windows)
225 (> file-name-length 2)
226 (eq? (string-ref file-name 1) #\:)
227 (eq? (string-ref file-name 2) #\/))))))
229 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 (define (type-check-list location signature arguments)
232 "Typecheck a list of arguments against a list of type
233 predicates. Print a message at LOCATION if any predicate failed."
235 (define (recursion-helper signature arguments count)
236 (define (helper pred? arg count)
237 (if (not (pred? arg))
243 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
244 count (type-name pred?) arg))
248 (if (null? signature)
250 (and (helper (car signature) (car arguments) count)
251 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
253 (recursion-helper signature arguments 1))
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259 ;;(define-public (output-framework) (write "hello\n"))
261 (define output-tex-module
262 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
263 (define output-ps-module
264 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
266 (define-public (ps-output-expression expr port)
267 (display (eval expr output-ps-module) port))
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 ;; Safe definitions utility
271 (define safe-objects (list))
273 (define-macro (define-safe-public arglist . body)
274 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
275 The syntax is the same as `define*-public'."
276 (define (get-symbol arg)
278 (get-symbol (car arg))
280 (let ((safe-symbol (get-symbol arglist)))
282 (define*-public ,arglist
284 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
288 (define-safe-public (lilypond-version)
290 (map (lambda (x) (if (symbol? x)
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 "define-event-classes.scm"
312 "define-music-types.scm"
315 "chord-ignatzek-names.scm"
317 "chord-generic-names.scm"
320 "music-functions.scm"
323 "define-music-properties.scm"
327 "parser-ly-from-scheme.scm"
328 "ly-syntax-constructors.scm"
330 "define-context-properties.scm"
331 "translation-functions.scm"
342 "predefined-fretboards.scm"
343 "define-markup-commands.scm"
344 "define-grob-properties.scm"
346 "define-grob-interfaces.scm"
347 "define-stencil-commands.scm"
351 "backend-library.scm"
354 ;; must be after everything has been defined
358 (for-each ly:load init-scheme-files)
361 (set! type-p-name-alist
363 (,boolean-or-symbol? . "boolean or symbol")
364 (,boolean? . "boolean")
366 (,grob-list? . "list of grobs")
367 (,hash-table? . "hash table")
368 (,input-port? . "input port")
369 (,integer? . "integer")
371 (,ly:context? . "context")
372 (,ly:dimension? . "dimension, in staff space")
373 (,ly:dir? . "direction")
374 (,ly:duration? . "duration")
375 (,ly:grob? . "layout object")
376 (,ly:input-location? . "input location")
377 (,ly:moment? . "moment")
378 (,ly:music? . "music")
379 (,ly:pitch? . "pitch")
380 (,ly:translator? . "translator")
381 (,ly:font-metric? . "font metric")
382 (,ly:simple-closure? . "simple closure")
383 (,markup-list? . "list of markups")
384 (,markup? . "markup")
385 (,ly:music-list? . "list of music")
386 (,number-or-grob? . "number or grob")
387 (,number-or-string? . "number or string")
388 (,number-pair? . "pair of numbers")
389 (,number? . "number")
390 (,output-port? . "output port")
392 (,procedure? . "procedure")
393 (,rhythmic-location? . "rhythmic location")
394 (,scheme? . "any type")
395 (,string? . "string")
396 (,symbol? . "symbol")
397 (,vector? . "vector")))
399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
402 (define (profile-measurements)
409 (ly:assoc-get 'gc-time-taken stats))
411 (ly:assoc-get 'total-cells-allocated stats 0)
414 (define (dump-profile base last this)
416 ((outname (format "~a.profile" (dir-basename base ".ly")))
417 (diff (map (lambda (y) (apply - y)) (zip this last))))
419 (ly:progress "\nWriting timing to ~a..." outname)
420 (format (open-file outname "w")
421 "time: ~a\ncells: ~a\n"
422 (if (ly:get-option 'dump-cpu-profile)
429 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
432 (define gc-dumping #f)
433 (define gc-protect-stat-count 0)
435 (define-public (dump-live-object-stats outfile)
438 (format outfile "~a: ~a\n" (car x) (cdr x)))
439 (sort (gc-live-object-stats)
441 (string<? (car x) (car y))))))
443 (define-public (dump-gc-protects)
444 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
445 (let* ((protects (sort
446 (hash-table->alist (ly:protects))
448 (< (object-address (car a))
449 (object-address (car b))))))
451 (out-file-name (string-append
452 "gcstat-" (number->string gc-protect-stat-count)
454 (outfile (open-file out-file-name "w")))
457 (display (format "Dumping GC statistics ~a...\n" out-file-name))
463 (format "~a (~a) = ~a\n" (object-address x) c x)
467 (not (symbol? (car x))))
471 (format outfile "\nprotected symbols: ~a\n"
472 (apply + (map (lambda (obj-count) (if (symbol? (car obj-count))
477 ;; (display (ly:smob-protects))
479 (if (defined? 'gc-live-object-stats)
481 (display "Live object statistics: GC'ing\n")
485 (display "Asserting dead objects\n")
486 (ly:set-option 'debug-gc-assert-parsed-dead #t)
488 (ly:set-option 'debug-gc-assert-parsed-dead #f)
490 (set! stats (gc-live-object-stats))
491 (display "Dumping live object statistics.\n")
492 (dump-live-object-stats outfile)))
502 gc-protect-stat-count
504 (let ((sym-stat (assoc sym stats)))
509 '(protected-objects bytes-malloced cell-heap-size
519 (define (check-memory)
520 "read /proc/self to check up on memory use."
521 (define (gulp-file name)
522 (let* ((file (open-input-file name))
523 (text (read-delimited "" file)))
527 ((stat (gulp-file "/proc/self/status"))
528 (lines (string-split stat #\newline))
529 (interesting (filter identity
532 (string-match "^VmData:[ \t]*([0-9]*) kB" l))
534 (mem (string->number (match:substring (car interesting) 1)))
538 (display (format "VMDATA: ~a\n" mem))
547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
550 (define (multi-fork count)
551 "Split this process in COUNT helpers. Returns either a list of pids,
552 or the number of the process."
553 (define (helper count acc)
556 ((pid (primitive-fork)))
559 (helper (1- count) (cons pid acc))))
564 (define-public (lilypond-main files)
565 "Entry point for LilyPond."
567 (eval-string (ly:command-line-code))
569 (if (ly:get-option 'help)
574 (if (ly:get-option 'show-available-fonts)
576 (ly:font-config-display-fonts)
581 (if (ly:get-option 'gui)
589 (if (ly:get-option 'read-file-list)
592 (> (string-length s) 0))
594 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
598 (if (and (number? (ly:get-option 'job-count))
599 (>= (length files) (ly:get-option 'job-count)))
601 ((count (ly:get-option 'job-count))
602 (split-todo (split-list files count))
603 (joblist (multi-fork count))
606 (if (not (string-or-symbol? (ly:get-option 'log-file)))
607 (ly:set-option 'log-file "lilypond-multi-run"))
609 (if (number? joblist)
612 'log-file (format "~a-~a"
613 (ly:get-option 'log-file) joblist))
614 (set! files (vector-ref split-todo joblist)))
617 (ly:progress "\nForking into jobs: ~a\n" joblist)
620 (let* ((stat (cdr (waitpid pid))))
624 (acons (list-element-index joblist pid)
632 (logfile (format "~a-~a.log"
633 (ly:get-option 'log-file) job))
634 (log (ly:gulp-file logfile))
635 (len (string-length log))
636 (tail (substring log (max 0 (- len 1024)))))
638 (if (status:term-sig state)
641 (format (_ "job ~a terminated with signal: ~a")
642 job (status:term-sig state)))
644 (_ "logfile ~a (exit ~a):\n~a")
645 logfile (status:exit-val state) tail))))
650 (ly:error "Children ~a exited with errors." (map car errors)))
652 ;; must overwrite individual entries
653 (if (ly:get-option 'dump-profile)
654 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
656 (exit (if (null? errors) 0 1))))))
658 (if (string-or-symbol? (ly:get-option 'log-file))
659 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
661 (let ((failed (lilypond-all files)))
662 (if (ly:get-option 'trace-scheme-coverage)
664 (coverage:show-all (lambda (f) (string-contains f "lilypond"))
669 (ly:error (_ "failed files: ~S") (string-join failed))
672 ;; HACK: be sure to exit with single newline
676 (define-public (lilypond-all files)
678 (separate-logs (ly:get-option 'separate-log-files))
681 (open-file (if (string-or-symbol? (ly:get-option 'log-file))
682 (format "~a.log" (ly:get-option 'log-file))
683 "/dev/tty") "a") #f))
684 (do-measurements (ly:get-option 'dump-profile))
685 (handler (lambda (key failed-file)
686 (set! failed (append (list failed-file) failed)))))
692 ((start-measurements (if do-measurements
693 (profile-measurements)
695 (base (dir-basename x ".ly"))
696 (all-settings (ly:all-options)))
699 (ly:stderr-redirect (format "~a.log" base) "w"))
701 (format ping-log "Procesing ~a\n" base))
703 (if (ly:get-option 'trace-memory-frequency)
704 (mtrace:start-trace (ly:get-option 'trace-memory-frequency)))
706 (lilypond-file handler x)
707 (if start-measurements
708 (dump-profile x start-measurements (profile-measurements)))
710 (if (ly:get-option 'trace-memory-frequency)
713 (mtrace:dump-results base)))
717 (ly:set-option (car s) (cdr s)))
720 (ly:clear-anonymous-modules)
721 (ly:set-option 'debug-gc-assert-parsed-dead #t)
723 (ly:set-option 'debug-gc-assert-parsed-dead #f)
726 (if (ly:get-option 'debug-gc)
728 (if (= (random 40) 1)
729 (ly:reset-all-fonts)))))
733 ;; we want the failed-files notice in the aggregrate logfile.
735 (format ping-log "Failed files: ~a\n" failed))
737 (if (ly:get-option 'dump-profile)
738 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
742 (define (lilypond-file handler file-name)
743 (catch 'ly-file-failed
744 (lambda () (ly:parse-file file-name))
745 (lambda (x . args) (handler x file-name))))
747 (use-modules (scm editor))
749 (define-public (gui-main files)
751 (gui-no-files-handler))
753 (if (not (string? (ly:get-option 'log-file)))
754 (let* ((base (dir-basename (car files) ".ly"))
755 (log-name (string-append base ".log")))
756 (if (not (ly:get-option 'gui))
757 (ly:message (_ "Redirecting output to ~a...") log-name))
758 (ly:stderr-redirect log-name "w")
759 (ly:message "# -*-compilation-*-"))
761 (let ((failed (lilypond-all files)))
765 (ly:stderr-redirect "foo" "r")
766 (system (get-editor-command log-name 0 0 0))
767 (ly:error (_ "failed files: ~S") (string-join failed))
772 (define (gui-no-files-handler)
773 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
774 ;; FIXME: soft-code, localize
775 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
776 (cmd (get-editor-command welcome-ly 0 0 0)))
777 (ly:message (_ "Invoking `~a'...") cmd)