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 (define (define-scheme-options)
19 (ly:add-option (car x) (cadr x) (caddr x)))
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 (check-internal-types #f "check every property assignment for types")
30 (clip-systems #f "Generate cut-out snippets of a score")
31 (debug-gc #f "dump memory debugging statistics")
32 (debug-gc-assert-parsed-dead #f "for memory debugging:
33 ensure that all refs to parsed objects are dead. This is an internal option, and is switched on automatically for -ddebug-gc.")
34 (debug-lexer #f "debug the flex lexer")
35 (debug-midi #f "generate human readable MIDI")
36 (debug-parser #f "debug the bison parser")
37 (debug-skylines #f "debug skylines")
38 (delete-intermediate-files #f
39 "delete unusable PostScript files")
40 (dump-profile #f "dump timing information for each file")
41 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
42 (dump-signatures #f "dump output signatures of each system. Used for regression testing.")
44 (eps-box-padding #f "Pad EPS bounding box left edge. Guarantee alignment between systems in LaTeX.")
46 "load fonts via Ghostscript.")
47 (gui #f "running from gui; redirect stderr to log file")
49 (include-book-title-preview #t "include book-titles in preview images.")
50 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
51 (job-count #f "Process in parallel")
52 (log-file #f "redirect output to log FILE.log")
55 "relative for simultaneous music works
56 similar to chord syntax")
58 "experimental mechanism for remembering tweaks")
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.")
74 "how much verbosity for TTF font embedding?")
75 (show-available-fonts #f
76 "List font names available.")
77 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
81 ;; need to do this in the beginning. Other parts of the
82 ;; Scheme init depend on these options.
84 (define-scheme-options)
88 (if (defined? 'set-debug-cell-accesses!)
89 (set-debug-cell-accesses! #f))
91 ;(set-debug-cell-accesses! 1000)
93 (use-modules (ice-9 regex)
104 (define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
107 (define-public (print . args)
108 (apply format (cons (current-output-port) args)))
112 ;;; debugging evaluator is slower. This should
113 ;;; have a more sensible default.
115 (if (ly:get-option 'verbose)
117 (ly:set-option 'protected-scheme-parsing #f)
118 (debug-enable 'debug)
119 (debug-enable 'backtrace)
120 (read-enable 'positions)))
122 (define-public tex-backend?
123 (member (ly:output-backend) '("texstr" "tex")))
125 (define-public parser #f)
127 (define-public (lilypond-version)
129 (map (lambda (x) (if (symbol? x)
136 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
137 (define-public TEX_STRING_HASHLIMIT 10000000)
141 ;; gettext wrapper for guile < 1.7.2
142 (if (defined? 'gettext)
143 (define-public _ gettext)
144 (define-public _ ly:gettext))
146 (define-public (ly:load x)
147 (let* ((file-name (%search-load-path x)))
148 (if (ly:get-option 'verbose)
149 (ly:progress "[~A" file-name))
151 (ly:error (_ "cannot find: ~A") x))
152 (primitive-load file-name)
153 (if (ly:get-option 'verbose)
157 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
160 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
163 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
167 (define-public PLATFORM
170 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
173 (let ((platform (string-tokenize
174 (vector-ref (uname) 0) char-set:letter+digit)))
175 (if (null? (cdr platform)) #f
176 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
180 (define native-getcwd getcwd)
182 (if (string-index x #\\)
184 (string-regexp-substitute
186 (string-regexp-substitute "\\\\" "/" x))))
187 ;; FIXME: this prints a warning.
188 (define-public (ly-getcwd)
189 (slashify (native-getcwd))))
190 (else (define-public ly-getcwd getcwd)))
192 (define-public (is-absolute? file-name)
193 (let ((file-name-length (string-length file-name)))
194 (if (= file-name-length 0)
196 (or (eq? (string-ref file-name 0) #\/)
197 (and (eq? PLATFORM 'windows)
198 (> file-name-length 2)
199 (eq? (string-ref file-name 1) #\:)
200 (eq? (string-ref file-name 2) #\/))))))
202 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 (define (type-check-list location signature arguments)
205 "Typecheck a list of arguments against a list of type
206 predicates. Print a message at LOCATION if any predicate failed."
208 (define (recursion-helper signature arguments count)
209 (define (helper pred? arg count)
210 (if (not (pred? arg))
216 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
217 count (type-name pred?) arg))
221 (if (null? signature)
223 (and (helper (car signature) (car arguments) count)
224 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
226 (recursion-helper signature arguments 1))
228 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232 ;;(define-public (output-framework) (write "hello\n"))
234 (define output-tex-module
235 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
236 (define output-ps-module
237 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
239 (define-public (ps-output-expression expr port)
240 (display (eval expr output-ps-module) port))
242 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 ;; Safe definitions utility
244 (define safe-objects (list))
246 (define-macro (define-safe-public arglist . body)
247 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
248 The syntax is the same as `define*-public'."
249 (define (get-symbol arg)
251 (get-symbol (car arg))
253 (let ((safe-symbol (get-symbol arglist)))
255 (define*-public ,arglist
257 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
268 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275 "define-event-classes.scm"
276 "define-music-types.scm"
279 "chord-ignatzek-names.scm"
281 "chord-generic-names.scm"
284 "music-functions.scm"
287 "define-music-properties.scm"
291 "parser-ly-from-scheme.scm"
292 "ly-syntax-constructors.scm"
294 "define-context-properties.scm"
295 "translation-functions.scm"
305 "define-markup-commands.scm"
306 "define-grob-properties.scm"
308 "define-grob-interfaces.scm"
309 "define-stencil-commands.scm"
313 "backend-library.scm"
316 ;; must be after everything has been defined
320 (set! type-p-name-alist
322 (,boolean-or-symbol? . "boolean or symbol")
323 (,boolean? . "boolean")
325 (,grob-list? . "list of grobs")
326 (,hash-table? . "hash table")
327 (,input-port? . "input port")
328 (,integer? . "integer")
330 (,ly:context? . "context")
331 (,ly:dimension? . "dimension, in staff space")
332 (,ly:dir? . "direction")
333 (,ly:duration? . "duration")
334 (,ly:grob? . "layout object")
335 (,ly:input-location? . "input location")
336 (,ly:moment? . "moment")
337 (,ly:music? . "music")
338 (,ly:pitch? . "pitch")
339 (,ly:translator? . "translator")
340 (,ly:font-metric? . "font metric")
341 (,ly:simple-closure? . "simple closure")
342 (,markup-list? . "list of markups")
343 (,markup? . "markup")
344 (,ly:music-list? . "list of music")
345 (,number-or-grob? . "number or grob")
346 (,number-or-string? . "number or string")
347 (,number-pair? . "pair of numbers")
348 (,number? . "number")
349 (,output-port? . "output port")
351 (,procedure? . "procedure")
352 (,rhythmic-location? . "rhythmic location")
353 (,scheme? . "any type")
354 (,string? . "string")
355 (,symbol? . "symbol")
356 (,vector? . "vector")))
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361 (define (profile-measurements)
368 (ly:assoc-get 'gc-time-taken stats))
370 (ly:assoc-get 'total-cells-allocated stats 0)
373 (define (dump-profile base last this)
375 ((outname (format "~a.profile" (basename base ".ly")))
376 (diff (map (lambda (y) (apply - y)) (zip this last))))
378 (ly:progress "\nWriting timing to ~a..." outname)
379 (format (open-file outname "w")
380 "time: ~a\ncells: ~a\n"
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 (define gc-dumping #f)
390 (define gc-protect-stat-count 0)
391 (define-public (dump-gc-protects)
392 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
393 (let* ((protects (sort
394 (hash-table->alist (ly:protects))
396 (< (object-address (car a))
397 (object-address (car b))))))
399 (out-file-name (string-append
400 "gcstat-" (number->string gc-protect-stat-count)
402 (outfile (open-file out-file-name "w")))
405 (display (format "Dumping GC statistics ~a...\n" out-file-name))
411 (format "~a (~a) = ~a\n" (object-address x) c x)
415 (not (symbol? (car x))))
419 (format outfile "\nprotected symbols: ~a\n"
420 (length (filter symbol? (map car protects))))
424 ;; (display (ly:smob-protects))
426 (if (defined? 'gc-live-object-stats)
428 (display "Live object statistics: GC'ing\n")
432 (display "Asserting dead objects\n")
433 (ly:set-option 'debug-gc-assert-parsed-dead #t)
435 (ly:set-option 'debug-gc-assert-parsed-dead #f)
437 (set! stats (gc-live-object-stats))
438 (display "Dumping live object statistics.\n")
442 (format outfile "~a: ~a\n" (car x) (cdr x)))
443 (sort (gc-live-object-stats)
445 (string<? (car x) (car y)))))))
456 gc-protect-stat-count
458 (let ((sym-stat (assoc sym stats)))
463 '(protected-objects bytes-malloced cell-heap-size
471 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 (define (multi-fork count)
475 (define (helper count acc)
478 ((pid (primitive-fork)))
481 (helper (1- count) (cons pid acc))))
486 (define-public (lilypond-main files)
487 "Entry point for LilyPond."
489 (define (no-files-handler)
493 (if (ly:get-option 'gui)
499 (if (ly:get-option 'read-file-list)
502 (> (string-length s) 0))
504 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
508 (if (and (number? (ly:get-option 'job-count))
509 (>= (length files) (ly:get-option 'job-count)))
512 ((count (ly:get-option 'job-count))
513 (split-todo (split-list files count))
514 (joblist (multi-fork count))
517 (if (not (string-or-symbol? (ly:get-option 'log-file)))
518 (ly:set-option 'log-file "lilypond-multi-run"))
520 (if (number? joblist)
522 (ly:set-option 'log-file (format "~a-~a"
523 (ly:get-option 'log-file) joblist))
524 (set! files (vector-ref split-todo joblist)))
527 (ly:progress "\nForking into jobs: ~a\n" joblist)
530 (let* ((stat (cdr (waitpid pid))))
533 (set! errors (acons (list-element-index joblist pid) stat errors)))))
540 (logfile (format "~a-~a.log"
541 (ly:get-option 'log-file) job))
542 (log (ly:gulp-file logfile))
543 (len (string-length log))
544 (tail (substring log (max 0 (- len 1024)))))
546 (if (status:term-sig state)
547 (ly:message "\n\n~a\n"
548 (format (_ "job ~a terminated with signal: ~a")
550 (status:term-sig state)))
551 (ly:message (_ "logfile ~a (exit ~a):\n~a") logfile (status:exit-val state) tail))))
556 (ly:error "Children ~a exited with errors." (map car errors)))
558 ;; must overwrite individual entries
559 (if (ly:get-option 'dump-profile)
560 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
562 (exit (if (null? errors) 0 1))))))
565 (if (string-or-symbol? (ly:get-option 'log-file))
566 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
568 (let ((failed (lilypond-all files)))
571 (ly:error (_ "failed files: ~S") (string-join failed))
574 ;; HACK: be sure to exit with single newline
578 (define-public (lilypond-all files)
581 (if (ly:get-option 'show-available-fonts)
583 (ly:font-config-display-fonts)
588 (separate-logs (ly:get-option 'separate-log-files))
589 (do-measurements (ly:get-option 'dump-profile))
590 (handler (lambda (key failed-file)
591 (set! failed (append (list failed-file) failed)))))
597 ((start-measurements (if do-measurements
598 (profile-measurements)
600 (base (basename x ".ly"))
601 (all-settings (ly:all-options)))
604 (ly:stderr-redirect (format "~a.log" base) "w"))
606 (lilypond-file handler x)
607 (if start-measurements
608 (dump-profile x start-measurements (profile-measurements)))
612 (ly:set-option (car s) (cdr s)))
615 (ly:clear-anonymous-modules)
616 (ly:set-option 'debug-gc-assert-parsed-dead #t)
618 (ly:set-option 'debug-gc-assert-parsed-dead #f)
621 (if (ly:get-option 'debug-gc)
623 (if (= (random 40) 1)
624 (ly:reset-all-fonts)))))
628 ;; we want the failed-files notice in the aggregrate logfile.
629 (if (ly:get-option 'separate-logs)
631 (if (string-or-symbol? (ly:get-option 'log-file))
632 (format "~a.log" (ly:get-option 'log-file))
635 (if (ly:get-option 'dump-profile)
636 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
640 (define (lilypond-file handler file-name)
641 (catch 'ly-file-failed
642 (lambda () (ly:parse-file file-name))
643 (lambda (x . args) (handler x file-name))))
645 (use-modules (scm editor))
647 (define-public (gui-main files)
649 (gui-no-files-handler))
651 (if (not (string? (ly:get-option 'log-file)))
652 (let* ((base (basename (car files) ".ly"))
653 (log-name (string-append base ".log")))
654 (if (not (ly:get-option 'gui))
655 (ly:message (_ "Redirecting output to ~a...") log-name))
656 (ly:stderr-redirect log-name "w")
657 (ly:message "# -*-compilation-*-"))
659 (let ((failed (lilypond-all files)))
663 (ly:stderr-redirect "foo" "r")
664 (system (get-editor-command log-name 0 0 0))
665 (ly:error (_ "failed files: ~S") (string-join failed))
670 (define (gui-no-files-handler)
671 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
672 ;; FIXME: soft-code, localize
673 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
674 (cmd (get-editor-command welcome-ly 0 0 0)))
675 (ly:message (_ "Invoking `~a'...") cmd)