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-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)
118 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
121 (define-public (print . args)
122 (apply format (cons (current-output-port) args)))
126 ;;; debugging evaluator is slower. This should
127 ;;; have a more sensible default.
129 (if (or (ly:get-option 'verbose)
130 (ly:get-option 'trace-memory-frequencency)
131 (ly:get-option 'trace-scheme-coverage)
134 (ly:set-option 'protected-scheme-parsing #f)
135 (debug-enable 'debug)
136 (debug-enable 'backtrace)
137 (read-enable 'positions)))
140 (if (ly:get-option 'trace-scheme-coverage)
143 (define-public tex-backend?
144 (member (ly:get-option 'backend) '(texstr tex)))
146 (define-public parser #f)
148 (define-public (lilypond-version)
150 (map (lambda (x) (if (symbol? x)
157 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
158 (define-public TEX_STRING_HASHLIMIT 10000000)
162 ;; gettext wrapper for guile < 1.7.2
163 (if (defined? 'gettext)
164 (define-public _ gettext)
165 (define-public _ ly:gettext))
167 (define-public (ly:load x)
168 (let* ((file-name (%search-load-path x)))
169 (if (ly:get-option 'verbose)
170 (ly:progress "[~A" file-name))
172 (ly:error (_ "cannot find: ~A") x))
173 (primitive-load file-name)
174 (if (ly:get-option 'verbose)
178 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
181 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
184 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
188 (define-public PLATFORM
191 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
194 (let ((platform (string-tokenize
195 (vector-ref (uname) 0) char-set:letter+digit)))
196 (if (null? (cdr platform)) #f
197 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
201 (define native-getcwd getcwd)
203 (if (string-index x #\\)
205 (string-regexp-substitute
207 (string-regexp-substitute "\\\\" "/" x))))
208 ;; FIXME: this prints a warning.
209 (define-public (ly-getcwd)
210 (slashify (native-getcwd))))
211 (else (define-public ly-getcwd getcwd)))
213 (define-public (is-absolute? file-name)
214 (let ((file-name-length (string-length file-name)))
215 (if (= file-name-length 0)
217 (or (eq? (string-ref file-name 0) #\/)
218 (and (eq? PLATFORM 'windows)
219 (> file-name-length 2)
220 (eq? (string-ref file-name 1) #\:)
221 (eq? (string-ref file-name 2) #\/))))))
223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
225 (define (type-check-list location signature arguments)
226 "Typecheck a list of arguments against a list of type
227 predicates. Print a message at LOCATION if any predicate failed."
229 (define (recursion-helper signature arguments count)
230 (define (helper pred? arg count)
231 (if (not (pred? arg))
237 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
238 count (type-name pred?) arg))
242 (if (null? signature)
244 (and (helper (car signature) (car arguments) count)
245 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
247 (recursion-helper signature arguments 1))
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
253 ;;(define-public (output-framework) (write "hello\n"))
255 (define output-tex-module
256 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
257 (define output-ps-module
258 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
260 (define-public (ps-output-expression expr port)
261 (display (eval expr output-ps-module) port))
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 ;; Safe definitions utility
265 (define safe-objects (list))
267 (define-macro (define-safe-public arglist . body)
268 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
269 The syntax is the same as `define*-public'."
270 (define (get-symbol arg)
272 (get-symbol (car arg))
274 (let ((safe-symbol (get-symbol arglist)))
276 (define*-public ,arglist
278 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 "define-event-classes.scm"
298 "define-music-types.scm"
301 "chord-ignatzek-names.scm"
303 "chord-generic-names.scm"
306 "music-functions.scm"
309 "define-music-properties.scm"
313 "parser-ly-from-scheme.scm"
314 "ly-syntax-constructors.scm"
316 "define-context-properties.scm"
317 "translation-functions.scm"
327 "define-markup-commands.scm"
328 "define-grob-properties.scm"
330 "define-grob-interfaces.scm"
331 "define-stencil-commands.scm"
335 "backend-library.scm"
338 ;; must be after everything has been defined
342 (for-each ly:load init-scheme-files)
345 (set! type-p-name-alist
347 (,boolean-or-symbol? . "boolean or symbol")
348 (,boolean? . "boolean")
350 (,grob-list? . "list of grobs")
351 (,hash-table? . "hash table")
352 (,input-port? . "input port")
353 (,integer? . "integer")
355 (,ly:context? . "context")
356 (,ly:dimension? . "dimension, in staff space")
357 (,ly:dir? . "direction")
358 (,ly:duration? . "duration")
359 (,ly:grob? . "layout object")
360 (,ly:input-location? . "input location")
361 (,ly:moment? . "moment")
362 (,ly:music? . "music")
363 (,ly:pitch? . "pitch")
364 (,ly:translator? . "translator")
365 (,ly:font-metric? . "font metric")
366 (,ly:simple-closure? . "simple closure")
367 (,markup-list? . "list of markups")
368 (,markup? . "markup")
369 (,ly:music-list? . "list of music")
370 (,number-or-grob? . "number or grob")
371 (,number-or-string? . "number or string")
372 (,number-pair? . "pair of numbers")
373 (,number? . "number")
374 (,output-port? . "output port")
376 (,procedure? . "procedure")
377 (,rhythmic-location? . "rhythmic location")
378 (,scheme? . "any type")
379 (,string? . "string")
380 (,symbol? . "symbol")
381 (,vector? . "vector")))
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 (define (profile-measurements)
393 (ly:assoc-get 'gc-time-taken stats))
395 (ly:assoc-get 'total-cells-allocated stats 0)
398 (define (dump-profile base last this)
400 ((outname (format "~a.profile" (basename base ".ly")))
401 (diff (map (lambda (y) (apply - y)) (zip this last))))
403 (ly:progress "\nWriting timing to ~a..." outname)
404 (format (open-file outname "w")
405 "time: ~a\ncells: ~a\n"
411 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
414 (define gc-dumping #f)
415 (define gc-protect-stat-count 0)
417 (define-public (dump-live-object-stats outfile)
420 (format outfile "~a: ~a\n" (car x) (cdr x)))
421 (sort (gc-live-object-stats)
423 (string<? (car x) (car y))))))
425 (define-public (dump-gc-protects)
426 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
427 (let* ((protects (sort
428 (hash-table->alist (ly:protects))
430 (< (object-address (car a))
431 (object-address (car b))))))
433 (out-file-name (string-append
434 "gcstat-" (number->string gc-protect-stat-count)
436 (outfile (open-file out-file-name "w")))
439 (display (format "Dumping GC statistics ~a...\n" out-file-name))
445 (format "~a (~a) = ~a\n" (object-address x) c x)
449 (not (symbol? (car x))))
453 (format outfile "\nprotected symbols: ~a\n"
454 (length (filter symbol? (map car protects))))
458 ;; (display (ly:smob-protects))
460 (if (defined? 'gc-live-object-stats)
462 (display "Live object statistics: GC'ing\n")
466 (display "Asserting dead objects\n")
467 (ly:set-option 'debug-gc-assert-parsed-dead #t)
469 (ly:set-option 'debug-gc-assert-parsed-dead #f)
471 (set! stats (gc-live-object-stats))
472 (display "Dumping live object statistics.\n")
473 (dump-live-object-stats outfile)))
483 gc-protect-stat-count
485 (let ((sym-stat (assoc sym stats)))
490 '(protected-objects bytes-malloced cell-heap-size
500 (define (check-memory)
501 "read /proc/self to check up on memory use."
502 (define (gulp-file name)
503 (let* ((file (open-input-file name))
504 (text (read-delimited "" file)))
508 ((stat (gulp-file "/proc/self/status"))
509 (lines (string-split stat #\newline))
510 (interesting (filter identity
513 (string-match "^VmData:[ \t]*([0-9]*) kB" l))
515 (mem (string->number (match:substring (car interesting) 1)))
519 (display (format "VMDATA: ~a\n" mem))
528 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
531 (define (multi-fork count)
532 (define (helper count acc)
535 ((pid (primitive-fork)))
538 (helper (1- count) (cons pid acc))))
543 (define-public (lilypond-main files)
544 "Entry point for LilyPond."
547 (eval-string (ly:command-line-code))
549 (if (ly:get-option 'help)
554 (if (ly:get-option 'show-available-fonts)
556 (ly:font-config-display-fonts)
561 (if (ly:get-option 'gui)
569 (if (ly:get-option 'read-file-list)
572 (> (string-length s) 0))
574 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
578 (if (and (number? (ly:get-option 'job-count))
579 (>= (length files) (ly:get-option 'job-count)))
582 ((count (ly:get-option 'job-count))
583 (split-todo (split-list files count))
584 (joblist (multi-fork count))
587 (if (not (string-or-symbol? (ly:get-option 'log-file)))
588 (ly:set-option 'log-file "lilypond-multi-run"))
590 (if (number? joblist)
592 (ly:set-option 'log-file (format "~a-~a"
593 (ly:get-option 'log-file) joblist))
594 (set! files (vector-ref split-todo joblist)))
597 (ly:progress "\nForking into jobs: ~a\n" joblist)
600 (let* ((stat (cdr (waitpid pid))))
603 (set! errors (acons (list-element-index joblist pid) stat errors)))))
610 (logfile (format "~a-~a.log"
611 (ly:get-option 'log-file) job))
612 (log (ly:gulp-file logfile))
613 (len (string-length log))
614 (tail (substring log (max 0 (- len 1024)))))
616 (if (status:term-sig state)
617 (ly:message "\n\n~a\n"
618 (format (_ "job ~a terminated with signal: ~a")
620 (status:term-sig state)))
621 (ly:message (_ "logfile ~a (exit ~a):\n~a") logfile (status:exit-val state) tail))))
626 (ly:error "Children ~a exited with errors." (map car errors)))
628 ;; must overwrite individual entries
629 (if (ly:get-option 'dump-profile)
630 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
632 (exit (if (null? errors) 0 1))))))
635 (if (string-or-symbol? (ly:get-option 'log-file))
636 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
639 (let ((failed (lilypond-all files)))
640 (if (ly:get-option 'trace-scheme-coverage)
642 (coverage:show-all (lambda (f) (string-contains f "lilypond"))
648 (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)