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)
21 (define (define-scheme-options)
23 (ly:add-option (car x) (cadr x) (caddr x)))
29 ;; - [subject-]object-object-verb +"ing"
30 ;; - [subject-]-verb-object-object
32 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
33 (check-internal-types #f "check every property assignment for types")
34 (clip-systems #f "Generate cut-out snippets of a score")
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-parser #f "debug the bison parser")
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")
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 (protected-scheme-parsing #t "continue when finding errors in inline
63 scheme are caught in the parser. If off, halt
64 on errors, and print a stack trace.")
65 (profile-property-accesses #f "keep statistics of get_property() calls.")
67 (resolution 101 "resolution for generating PNG bitmaps")
68 (read-file-list #f "Read files to be processed from command line arguments")
70 (safe #f "Run safely")
71 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN.")
72 (separate-log-files #f "Output to FILE.log per file.")
73 (trace-memory-frequency #f "Record Scheme cell usage this many times per second, and dump to file.")
75 "how much verbosity for TTF font embedding?")
76 (show-available-fonts #f
77 "List font names available.")
78 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
83 (ly:set-option (car x) (cdr x)))
84 (eval-string (ly:command-line-options))))
87 ;; need to do this in the beginning. Other parts of the
88 ;; Scheme init depend on these options.
90 (define-scheme-options)
96 (if (defined? 'set-debug-cell-accesses!)
97 (set-debug-cell-accesses! #f))
99 ;(set-debug-cell-accesses! 1000)
101 (use-modules (ice-9 regex)
114 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
117 (define-public (print . args)
118 (apply format (cons (current-output-port) args)))
122 ;;; debugging evaluator is slower. This should
123 ;;; have a more sensible default.
125 (if (or (ly:get-option 'verbose)
126 (ly:get-option 'trace-memory-frequencency))
128 (ly:set-option 'protected-scheme-parsing #f)
129 (debug-enable 'debug)
130 (debug-enable 'backtrace)
131 (read-enable 'positions)))
133 (define-public tex-backend?
134 (member (ly:output-backend) '("texstr" "tex")))
136 (define-public parser #f)
138 (define-public (lilypond-version)
140 (map (lambda (x) (if (symbol? x)
147 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
148 (define-public TEX_STRING_HASHLIMIT 10000000)
152 ;; gettext wrapper for guile < 1.7.2
153 (if (defined? 'gettext)
154 (define-public _ gettext)
155 (define-public _ ly:gettext))
157 (define-public (ly:load x)
158 (let* ((file-name (%search-load-path x)))
159 (if (ly:get-option 'verbose)
160 (ly:progress "[~A" file-name))
162 (ly:error (_ "cannot find: ~A") x))
163 (primitive-load file-name)
164 (if (ly:get-option 'verbose)
168 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
171 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
174 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
178 (define-public PLATFORM
181 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
184 (let ((platform (string-tokenize
185 (vector-ref (uname) 0) char-set:letter+digit)))
186 (if (null? (cdr platform)) #f
187 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
191 (define native-getcwd getcwd)
193 (if (string-index x #\\)
195 (string-regexp-substitute
197 (string-regexp-substitute "\\\\" "/" x))))
198 ;; FIXME: this prints a warning.
199 (define-public (ly-getcwd)
200 (slashify (native-getcwd))))
201 (else (define-public ly-getcwd getcwd)))
203 (define-public (is-absolute? file-name)
204 (let ((file-name-length (string-length file-name)))
205 (if (= file-name-length 0)
207 (or (eq? (string-ref file-name 0) #\/)
208 (and (eq? PLATFORM 'windows)
209 (> file-name-length 2)
210 (eq? (string-ref file-name 1) #\:)
211 (eq? (string-ref file-name 2) #\/))))))
213 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 (define (type-check-list location signature arguments)
216 "Typecheck a list of arguments against a list of type
217 predicates. Print a message at LOCATION if any predicate failed."
219 (define (recursion-helper signature arguments count)
220 (define (helper pred? arg count)
221 (if (not (pred? arg))
227 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
228 count (type-name pred?) arg))
232 (if (null? signature)
234 (and (helper (car signature) (car arguments) count)
235 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
237 (recursion-helper signature arguments 1))
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 ;;(define-public (output-framework) (write "hello\n"))
245 (define output-tex-module
246 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
247 (define output-ps-module
248 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
250 (define-public (ps-output-expression expr port)
251 (display (eval expr output-ps-module) port))
253 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 ;; Safe definitions utility
255 (define safe-objects (list))
257 (define-macro (define-safe-public arglist . body)
258 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
259 The syntax is the same as `define*-public'."
260 (define (get-symbol arg)
262 (get-symbol (car arg))
264 (let ((safe-symbol (get-symbol arglist)))
266 (define*-public ,arglist
268 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
272 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 "define-event-classes.scm"
288 "define-music-types.scm"
291 "chord-ignatzek-names.scm"
293 "chord-generic-names.scm"
296 "music-functions.scm"
299 "define-music-properties.scm"
303 "parser-ly-from-scheme.scm"
304 "ly-syntax-constructors.scm"
306 "define-context-properties.scm"
307 "translation-functions.scm"
317 "define-markup-commands.scm"
318 "define-grob-properties.scm"
320 "define-grob-interfaces.scm"
321 "define-stencil-commands.scm"
325 "backend-library.scm"
328 ;; must be after everything has been defined
334 (for-each ly:load init-scheme-files)
337 (set! type-p-name-alist
339 (,boolean-or-symbol? . "boolean or symbol")
340 (,boolean? . "boolean")
342 (,grob-list? . "list of grobs")
343 (,hash-table? . "hash table")
344 (,input-port? . "input port")
345 (,integer? . "integer")
347 (,ly:context? . "context")
348 (,ly:dimension? . "dimension, in staff space")
349 (,ly:dir? . "direction")
350 (,ly:duration? . "duration")
351 (,ly:grob? . "layout object")
352 (,ly:input-location? . "input location")
353 (,ly:moment? . "moment")
354 (,ly:music? . "music")
355 (,ly:pitch? . "pitch")
356 (,ly:translator? . "translator")
357 (,ly:font-metric? . "font metric")
358 (,ly:simple-closure? . "simple closure")
359 (,markup-list? . "list of markups")
360 (,markup? . "markup")
361 (,ly:music-list? . "list of music")
362 (,number-or-grob? . "number or grob")
363 (,number-or-string? . "number or string")
364 (,number-pair? . "pair of numbers")
365 (,number? . "number")
366 (,output-port? . "output port")
368 (,procedure? . "procedure")
369 (,rhythmic-location? . "rhythmic location")
370 (,scheme? . "any type")
371 (,string? . "string")
372 (,symbol? . "symbol")
373 (,vector? . "vector")))
375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 (define (profile-measurements)
385 (ly:assoc-get 'gc-time-taken stats))
387 (ly:assoc-get 'total-cells-allocated stats 0)
390 (define (dump-profile base last this)
392 ((outname (format "~a.profile" (basename base ".ly")))
393 (diff (map (lambda (y) (apply - y)) (zip this last))))
395 (ly:progress "\nWriting timing to ~a..." outname)
396 (format (open-file outname "w")
397 "time: ~a\ncells: ~a\n"
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
406 (define gc-dumping #f)
407 (define gc-protect-stat-count 0)
409 (define-public (dump-live-object-stats outfile)
412 (format outfile "~a: ~a\n" (car x) (cdr x)))
413 (sort (gc-live-object-stats)
415 (string<? (car x) (car y))))))
417 (define-public (dump-gc-protects)
418 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
419 (let* ((protects (sort
420 (hash-table->alist (ly:protects))
422 (< (object-address (car a))
423 (object-address (car b))))))
425 (out-file-name (string-append
426 "gcstat-" (number->string gc-protect-stat-count)
428 (outfile (open-file out-file-name "w")))
431 (display (format "Dumping GC statistics ~a...\n" out-file-name))
437 (format "~a (~a) = ~a\n" (object-address x) c x)
441 (not (symbol? (car x))))
445 (format outfile "\nprotected symbols: ~a\n"
446 (length (filter symbol? (map car protects))))
450 ;; (display (ly:smob-protects))
452 (if (defined? 'gc-live-object-stats)
454 (display "Live object statistics: GC'ing\n")
458 (display "Asserting dead objects\n")
459 (ly:set-option 'debug-gc-assert-parsed-dead #t)
461 (ly:set-option 'debug-gc-assert-parsed-dead #f)
463 (set! stats (gc-live-object-stats))
464 (display "Dumping live object statistics.\n")
465 (dump-live-object-stats outfile)))
475 gc-protect-stat-count
477 (let ((sym-stat (assoc sym stats)))
482 '(protected-objects bytes-malloced cell-heap-size
492 (define (check-memory)
493 "read /proc/self to check up on memory use."
494 (define (gulp-file name)
495 (let* ((file (open-input-file name))
496 (text (read-delimited "" file)))
500 ((stat (gulp-file "/proc/self/status"))
501 (lines (string-split stat #\newline))
502 (interesting (filter identity
505 (string-match "^VmData:[ \t]*([0-9]*) kB" l))
507 (mem (string->number (match:substring (car interesting) 1)))
511 (display (format "VMDATA: ~a\n" mem))
520 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
523 (define (multi-fork count)
524 (define (helper count acc)
527 ((pid (primitive-fork)))
530 (helper (1- count) (cons pid acc))))
535 (define-public (lilypond-main files)
536 "Entry point for LilyPond."
538 (define (no-files-handler)
542 (eval-string (ly:command-line-code))
544 (if (ly:get-option 'gui)
550 (if (ly:get-option 'read-file-list)
553 (> (string-length s) 0))
555 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
559 (if (and (number? (ly:get-option 'job-count))
560 (>= (length files) (ly:get-option 'job-count)))
563 ((count (ly:get-option 'job-count))
564 (split-todo (split-list files count))
565 (joblist (multi-fork count))
568 (if (not (string-or-symbol? (ly:get-option 'log-file)))
569 (ly:set-option 'log-file "lilypond-multi-run"))
571 (if (number? joblist)
573 (ly:set-option 'log-file (format "~a-~a"
574 (ly:get-option 'log-file) joblist))
575 (set! files (vector-ref split-todo joblist)))
578 (ly:progress "\nForking into jobs: ~a\n" joblist)
581 (let* ((stat (cdr (waitpid pid))))
584 (set! errors (acons (list-element-index joblist pid) stat errors)))))
591 (logfile (format "~a-~a.log"
592 (ly:get-option 'log-file) job))
593 (log (ly:gulp-file logfile))
594 (len (string-length log))
595 (tail (substring log (max 0 (- len 1024)))))
597 (if (status:term-sig state)
598 (ly:message "\n\n~a\n"
599 (format (_ "job ~a terminated with signal: ~a")
601 (status:term-sig state)))
602 (ly:message (_ "logfile ~a (exit ~a):\n~a") logfile (status:exit-val state) tail))))
607 (ly:error "Children ~a exited with errors." (map car errors)))
609 ;; must overwrite individual entries
610 (if (ly:get-option 'dump-profile)
611 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
613 (exit (if (null? errors) 0 1))))))
616 (if (string-or-symbol? (ly:get-option 'log-file))
617 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
620 (let ((failed (lilypond-all files)))
623 (ly:error (_ "failed files: ~S") (string-join failed))
626 ;; HACK: be sure to exit with single newline
630 (define-public (lilypond-all files)
633 (if (ly:get-option 'show-available-fonts)
635 (ly:font-config-display-fonts)
640 (separate-logs (ly:get-option 'separate-log-files))
641 (do-measurements (ly:get-option 'dump-profile))
642 (handler (lambda (key failed-file)
643 (set! failed (append (list failed-file) failed)))))
649 ((start-measurements (if do-measurements
650 (profile-measurements)
652 (base (basename x ".ly"))
653 (all-settings (ly:all-options)))
656 (ly:stderr-redirect (format "~a.log" base) "w"))
657 (if (ly:get-option 'trace-memory-frequency)
658 (mtrace:start-trace (ly:get-option 'trace-memory-frequency)))
660 (lilypond-file handler x)
661 (if start-measurements
662 (dump-profile x start-measurements (profile-measurements)))
664 (if (ly:get-option 'trace-memory-frequency)
667 (mtrace:dump-results base)))
671 (ly:set-option (car s) (cdr s)))
674 (ly:clear-anonymous-modules)
675 (ly:set-option 'debug-gc-assert-parsed-dead #t)
677 (ly:set-option 'debug-gc-assert-parsed-dead #f)
680 (if (ly:get-option 'debug-gc)
682 (if (= (random 40) 1)
683 (ly:reset-all-fonts)))))
687 ;; we want the failed-files notice in the aggregrate logfile.
688 (if (ly:get-option 'separate-logs)
690 (if (string-or-symbol? (ly:get-option 'log-file))
691 (format "~a.log" (ly:get-option 'log-file))
694 (if (ly:get-option 'dump-profile)
695 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
699 (define (lilypond-file handler file-name)
700 (catch 'ly-file-failed
701 (lambda () (ly:parse-file file-name))
702 (lambda (x . args) (handler x file-name))))
704 (use-modules (scm editor))
706 (define-public (gui-main files)
708 (gui-no-files-handler))
710 (if (not (string? (ly:get-option 'log-file)))
711 (let* ((base (basename (car files) ".ly"))
712 (log-name (string-append base ".log")))
713 (if (not (ly:get-option 'gui))
714 (ly:message (_ "Redirecting output to ~a...") log-name))
715 (ly:stderr-redirect log-name "w")
716 (ly:message "# -*-compilation-*-"))
718 (let ((failed (lilypond-all files)))
722 (ly:stderr-redirect "foo" "r")
723 (system (get-editor-command log-name 0 0 0))
724 (ly:error (_ "failed files: ~S") (string-join failed))
729 (define (gui-no-files-handler)
730 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
731 ;; FIXME: soft-code, localize
732 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
733 (cmd (get-editor-command welcome-ly 0 0 0)))
734 (ly:message (_ "Invoking `~a'...") cmd)