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 (debug-gc #f "dump memory debugging statistics")
35 (debug-gc-assert-parsed-dead #f "for memory debugging:
36 ensure that all refs to parsed objects are dead. This is an internal option, and is switched on automatically for -ddebug-gc.")
37 (debug-lexer #f "debug the flex lexer")
38 (debug-page-breaking-scoring #f "dump scores for many different page breaking configurations")
39 (debug-parser #f "debug the bison parser")
40 (debug-property-callbacks #f "debug cyclic callback chains")
41 (debug-skylines #f "debug skylines")
42 (delete-intermediate-files #f
43 "delete unusable PostScript files")
44 (dump-profile #f "dump timing information for each file")
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 (gui #f "running from gui; redirect stderr to log file")
51 (help #f "show this help.")
52 (include-book-title-preview #t "include book-titles in preview images.")
53 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
54 (job-count #f "Process in parallel")
55 (log-file #f "redirect output to log FILE.log")
58 "relative for simultaneous music works
59 similar to chord syntax")
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 (preview #f "make a incipit image. ")
64 (print-pages #t "print pages normally. ")
65 (protected-scheme-parsing #t "continue when finding errors in inline
66 scheme are caught in the parser. If off, halt
67 on errors, and print a stack trace.")
68 (profile-property-accesses #f "keep statistics of get_property() calls.")
70 (resolution 101 "resolution for generating PNG bitmaps")
71 (read-file-list #f "Read files to be processed from command line arguments")
73 (safe #f "Run safely")
74 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN.")
75 (separate-log-files #f "Output to FILE.log per file.")
76 (trace-memory-frequency #f "Record Scheme cell usage this many times per second, and dump to file.")
77 (trace-scheme-coverage #f "Record coverage of Scheme files")
79 "how much verbosity for TTF font embedding?")
80 (show-available-fonts #f
81 "List font names available.")
82 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
85 ;; need to do this in the beginning. Other parts of the
86 ;; Scheme init depend on these options.
90 (ly:add-option (car x) (cadr x) (caddr x)))
91 scheme-options-definitions)
95 (ly:set-option (car x) (cdr x)))
96 (eval-string (ly:command-line-options)))
100 (if (defined? 'set-debug-cell-accesses!)
101 (set-debug-cell-accesses! #f))
103 ;(set-debug-cell-accesses! 1000)
105 (use-modules (ice-9 regex)
119 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
122 (define-public (print . args)
123 (apply format (cons (current-output-port) args)))
127 ;;; debugging evaluator is slower. This should
128 ;;; have a more sensible default.
130 (if (or (ly:get-option 'verbose)
131 (ly:get-option 'trace-memory-frequencency)
132 (ly:get-option 'trace-scheme-coverage)
135 (ly:set-option 'protected-scheme-parsing #f)
136 (debug-enable 'debug)
137 (debug-enable 'backtrace)
138 (read-enable 'positions)))
141 (if (ly:get-option 'trace-scheme-coverage)
144 (define-public tex-backend?
145 (member (ly:get-option 'backend) '(texstr tex)))
147 (define-public parser #f)
149 (define-public (lilypond-version)
151 (map (lambda (x) (if (symbol? x)
158 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
159 (define-public TEX_STRING_HASHLIMIT 10000000)
163 ;; gettext wrapper for guile < 1.7.2
164 (if (defined? 'gettext)
165 (define-public _ gettext)
166 (define-public _ ly:gettext))
168 (define-public (ly:load x)
169 (let* ((file-name (%search-load-path x)))
170 (if (ly:get-option 'verbose)
171 (ly:progress "[~A" file-name))
173 (ly:error (_ "cannot find: ~A") x))
174 (primitive-load file-name)
175 (if (ly:get-option 'verbose)
179 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
182 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
185 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
189 (define-public PLATFORM
192 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
195 (let ((platform (string-tokenize
196 (vector-ref (uname) 0) char-set:letter+digit)))
197 (if (null? (cdr platform)) #f
198 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
202 (define native-getcwd getcwd)
204 (if (string-index x #\\)
206 (string-regexp-substitute
208 (string-regexp-substitute "\\\\" "/" x))))
209 ;; FIXME: this prints a warning.
210 (define-public (ly-getcwd)
211 (slashify (native-getcwd))))
212 (else (define-public ly-getcwd getcwd)))
214 (define-public (is-absolute? file-name)
215 (let ((file-name-length (string-length file-name)))
216 (if (= file-name-length 0)
218 (or (eq? (string-ref file-name 0) #\/)
219 (and (eq? PLATFORM 'windows)
220 (> file-name-length 2)
221 (eq? (string-ref file-name 1) #\:)
222 (eq? (string-ref file-name 2) #\/))))))
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 (define (type-check-list location signature arguments)
227 "Typecheck a list of arguments against a list of type
228 predicates. Print a message at LOCATION if any predicate failed."
230 (define (recursion-helper signature arguments count)
231 (define (helper pred? arg count)
232 (if (not (pred? arg))
238 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
239 count (type-name pred?) arg))
243 (if (null? signature)
245 (and (helper (car signature) (car arguments) count)
246 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
248 (recursion-helper signature arguments 1))
250 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 ;;(define-public (output-framework) (write "hello\n"))
256 (define output-tex-module
257 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
258 (define output-ps-module
259 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
261 (define-public (ps-output-expression expr port)
262 (display (eval expr output-ps-module) port))
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 ;; Safe definitions utility
266 (define safe-objects (list))
268 (define-macro (define-safe-public arglist . body)
269 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
270 The syntax is the same as `define*-public'."
271 (define (get-symbol arg)
273 (get-symbol (car arg))
275 (let ((safe-symbol (get-symbol arglist)))
277 (define*-public ,arglist
279 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
298 "define-event-classes.scm"
299 "define-music-types.scm"
302 "chord-ignatzek-names.scm"
304 "chord-generic-names.scm"
307 "music-functions.scm"
310 "define-music-properties.scm"
314 "parser-ly-from-scheme.scm"
315 "ly-syntax-constructors.scm"
317 "define-context-properties.scm"
318 "translation-functions.scm"
328 "define-markup-commands.scm"
329 "define-grob-properties.scm"
331 "define-grob-interfaces.scm"
332 "define-stencil-commands.scm"
336 "backend-library.scm"
339 ;; must be after everything has been defined
343 (for-each ly:load init-scheme-files)
346 (set! type-p-name-alist
348 (,boolean-or-symbol? . "boolean or symbol")
349 (,boolean? . "boolean")
351 (,grob-list? . "list of grobs")
352 (,hash-table? . "hash table")
353 (,input-port? . "input port")
354 (,integer? . "integer")
356 (,ly:context? . "context")
357 (,ly:dimension? . "dimension, in staff space")
358 (,ly:dir? . "direction")
359 (,ly:duration? . "duration")
360 (,ly:grob? . "layout object")
361 (,ly:input-location? . "input location")
362 (,ly:moment? . "moment")
363 (,ly:music? . "music")
364 (,ly:pitch? . "pitch")
365 (,ly:translator? . "translator")
366 (,ly:font-metric? . "font metric")
367 (,ly:simple-closure? . "simple closure")
368 (,markup-list? . "list of markups")
369 (,markup? . "markup")
370 (,ly:music-list? . "list of music")
371 (,number-or-grob? . "number or grob")
372 (,number-or-string? . "number or string")
373 (,number-pair? . "pair of numbers")
374 (,number? . "number")
375 (,output-port? . "output port")
377 (,procedure? . "procedure")
378 (,rhythmic-location? . "rhythmic location")
379 (,scheme? . "any type")
380 (,string? . "string")
381 (,symbol? . "symbol")
382 (,vector? . "vector")))
384 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 (define (profile-measurements)
394 (ly:assoc-get 'gc-time-taken stats))
396 (ly:assoc-get 'total-cells-allocated stats 0)
399 (define (dump-profile base last this)
401 ((outname (format "~a.profile" (basename base ".ly")))
402 (diff (map (lambda (y) (apply - y)) (zip this last))))
404 (ly:progress "\nWriting timing to ~a..." outname)
405 (format (open-file outname "w")
406 "time: ~a\ncells: ~a\n"
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
415 (define gc-dumping #f)
416 (define gc-protect-stat-count 0)
418 (define-public (dump-live-object-stats outfile)
421 (format outfile "~a: ~a\n" (car x) (cdr x)))
422 (sort (gc-live-object-stats)
424 (string<? (car x) (car y))))))
426 (define-public (dump-gc-protects)
427 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
428 (let* ((protects (sort
429 (hash-table->alist (ly:protects))
431 (< (object-address (car a))
432 (object-address (car b))))))
434 (out-file-name (string-append
435 "gcstat-" (number->string gc-protect-stat-count)
437 (outfile (open-file out-file-name "w")))
440 (display (format "Dumping GC statistics ~a...\n" out-file-name))
446 (format "~a (~a) = ~a\n" (object-address x) c x)
450 (not (symbol? (car x))))
454 (format outfile "\nprotected symbols: ~a\n"
455 (length (filter symbol? (map car protects))))
459 ;; (display (ly:smob-protects))
461 (if (defined? 'gc-live-object-stats)
463 (display "Live object statistics: GC'ing\n")
467 (display "Asserting dead objects\n")
468 (ly:set-option 'debug-gc-assert-parsed-dead #t)
470 (ly:set-option 'debug-gc-assert-parsed-dead #f)
472 (set! stats (gc-live-object-stats))
473 (display "Dumping live object statistics.\n")
474 (dump-live-object-stats outfile)))
484 gc-protect-stat-count
486 (let ((sym-stat (assoc sym stats)))
491 '(protected-objects bytes-malloced cell-heap-size
501 (define (check-memory)
502 "read /proc/self to check up on memory use."
503 (define (gulp-file name)
504 (let* ((file (open-input-file name))
505 (text (read-delimited "" file)))
509 ((stat (gulp-file "/proc/self/status"))
510 (lines (string-split stat #\newline))
511 (interesting (filter identity
514 (string-match "^VmData:[ \t]*([0-9]*) kB" l))
516 (mem (string->number (match:substring (car interesting) 1)))
520 (display (format "VMDATA: ~a\n" mem))
529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
532 (define (multi-fork count)
533 (define (helper count acc)
536 ((pid (primitive-fork)))
539 (helper (1- count) (cons pid acc))))
544 (define-public (lilypond-main files)
545 "Entry point for LilyPond."
548 (eval-string (ly:command-line-code))
550 (if (ly:get-option 'help)
555 (if (ly:get-option 'show-available-fonts)
557 (ly:font-config-display-fonts)
562 (if (ly:get-option 'gui)
570 (if (ly:get-option 'read-file-list)
573 (> (string-length s) 0))
575 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
579 (if (and (number? (ly:get-option 'job-count))
580 (>= (length files) (ly:get-option 'job-count)))
583 ((count (ly:get-option 'job-count))
584 (split-todo (split-list files count))
585 (joblist (multi-fork count))
588 (if (not (string-or-symbol? (ly:get-option 'log-file)))
589 (ly:set-option 'log-file "lilypond-multi-run"))
591 (if (number? joblist)
593 (ly:set-option 'log-file (format "~a-~a"
594 (ly:get-option 'log-file) joblist))
595 (set! files (vector-ref split-todo joblist)))
598 (ly:progress "\nForking into jobs: ~a\n" joblist)
601 (let* ((stat (cdr (waitpid pid))))
604 (set! errors (acons (list-element-index joblist pid) stat errors)))))
611 (logfile (format "~a-~a.log"
612 (ly:get-option 'log-file) job))
613 (log (ly:gulp-file logfile))
614 (len (string-length log))
615 (tail (substring log (max 0 (- len 1024)))))
617 (if (status:term-sig state)
618 (ly:message "\n\n~a\n"
619 (format (_ "job ~a terminated with signal: ~a")
621 (status:term-sig state)))
622 (ly:message (_ "logfile ~a (exit ~a):\n~a") logfile (status:exit-val state) tail))))
627 (ly:error "Children ~a exited with errors." (map car errors)))
629 ;; must overwrite individual entries
630 (if (ly:get-option 'dump-profile)
631 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
633 (exit (if (null? errors) 0 1))))))
636 (if (string-or-symbol? (ly:get-option 'log-file))
637 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
640 (let ((failed (lilypond-all files)))
641 (if (ly:get-option 'trace-scheme-coverage)
643 (coverage:show-all (lambda (f) (string-contains f "lilypond"))
649 (ly:error (_ "failed files: ~S") (string-join failed))
652 ;; HACK: be sure to exit with single newline
656 (define-public (lilypond-all files)
658 (separate-logs (ly:get-option 'separate-log-files))
659 (do-measurements (ly:get-option 'dump-profile))
660 (handler (lambda (key failed-file)
661 (set! failed (append (list failed-file) failed)))))
667 ((start-measurements (if do-measurements
668 (profile-measurements)
670 (base (basename x ".ly"))
671 (all-settings (ly:all-options)))
674 (ly:stderr-redirect (format "~a.log" base) "w"))
675 (if (ly:get-option 'trace-memory-frequency)
676 (mtrace:start-trace (ly:get-option 'trace-memory-frequency)))
678 (lilypond-file handler x)
679 (if start-measurements
680 (dump-profile x start-measurements (profile-measurements)))
682 (if (ly:get-option 'trace-memory-frequency)
685 (mtrace:dump-results base)))
689 (ly:set-option (car s) (cdr s)))
692 (ly:clear-anonymous-modules)
693 (ly:set-option 'debug-gc-assert-parsed-dead #t)
695 (ly:set-option 'debug-gc-assert-parsed-dead #f)
698 (if (ly:get-option 'debug-gc)
700 (if (= (random 40) 1)
701 (ly:reset-all-fonts)))))
705 ;; we want the failed-files notice in the aggregrate logfile.
706 (if (ly:get-option 'separate-logs)
708 (if (string-or-symbol? (ly:get-option 'log-file))
709 (format "~a.log" (ly:get-option 'log-file))
712 (if (ly:get-option 'dump-profile)
713 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
717 (define (lilypond-file handler file-name)
718 (catch 'ly-file-failed
719 (lambda () (ly:parse-file file-name))
720 (lambda (x . args) (handler x file-name))))
722 (use-modules (scm editor))
724 (define-public (gui-main files)
726 (gui-no-files-handler))
728 (if (not (string? (ly:get-option 'log-file)))
729 (let* ((base (basename (car files) ".ly"))
730 (log-name (string-append base ".log")))
731 (if (not (ly:get-option 'gui))
732 (ly:message (_ "Redirecting output to ~a...") log-name))
733 (ly:stderr-redirect log-name "w")
734 (ly:message "# -*-compilation-*-"))
736 (let ((failed (lilypond-all files)))
740 (ly:stderr-redirect "foo" "r")
741 (system (get-editor-command log-name 0 0 0))
742 (ly:error (_ "failed files: ~S") (string-join failed))
747 (define (gui-no-files-handler)
748 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
749 ;; FIXME: soft-code, localize
750 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
751 (cmd (get-editor-command welcome-ly 0 0 0)))
752 (ly:message (_ "Invoking `~a'...") cmd)