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>
9 (define (define-scheme-options)
11 (ly:add-option (car x) (cadr x) (caddr x)))
17 ;; - [subject-]object-object-verb +"ing"
18 ;; - [subject-]-verb-object-object
20 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
21 (check-internal-types #f "check every property assignment for types")
22 (clip-systems #f "Generate cut-out snippets of a score")
23 (debug-gc #f "dump memory debugging statistics")
24 (debug-gc-assert-parsed-dead #f "for memory debugging:
25 ensure that all refs to parsed objects are dead. This is an internal option, and is switched on automatically for -ddebug-gc.")
26 (debug-lexer #f "debug the flex lexer")
27 (debug-midi #f "generate human readable MIDI")
28 (debug-parser #f "debug the bison parser")
29 (debug-skylines #f "debug skylines")
30 (delete-intermediate-files #f
31 "delete unusable PostScript files")
32 (dump-profile #f "dump timing information for each file")
33 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
34 (dump-signatures #f "dump output signatures of each system")
36 (eps-box-padding #f "Pad EPS bounding box left edge by this much to guarantee alignment between systems")
39 "load fonts via Ghostscript.")
40 (gui #f "running from gui; redirect stderr to log file")
42 (include-book-title-preview #t "include book-titles in preview images.")
43 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
44 (job-count #f "Process in parallel")
45 (log-file #f "redirect output to log FILE.log")
48 "relative for simultaneous music works
49 similar to chord syntax")
51 "experimental mechanism for remembering tweaks")
52 (point-and-click #t "use point & click")
53 (paper-size "a4" "the default paper size")
54 (pixmap-format "png16m" "GS format to use for pixel images")
55 (protected-scheme-parsing #t "continue when finding errors in inline
56 scheme are caught in the parser. If off, halt
57 on errors, and print a stack trace.")
58 (profile-property-accesses #f "keep statistics of get_property() calls.")
60 (resolution 101 "resolution for generating PNG bitmaps")
61 (read-file-list #f "Read files to be processed from command line arguments")
63 (safe #f "Run safely")
64 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
67 "how much verbosity for TTF font embedding?")
69 (show-available-fonts #f
70 "List font names available.")
72 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
76 ;; need to do this in the beginning. Other parts of the
77 ;; Scheme init depend on these options.
79 (define-scheme-options)
83 (if (defined? 'set-debug-cell-accesses!)
84 (set-debug-cell-accesses! #f))
86 ;(set-debug-cell-accesses! 1000)
88 (use-modules (ice-9 regex)
101 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
103 (define-public (print . args)
104 (apply format (cons (current-output-port) args)))
108 ;;; debugging evaluator is slower. This should
109 ;;; have a more sensible default.
111 (if (ly:get-option 'verbose)
113 (ly:set-option 'protected-scheme-parsing #f)
114 (debug-enable 'debug)
115 (debug-enable 'backtrace)
116 (read-enable 'positions)))
118 (define-public tex-backend?
119 (member (ly:output-backend) '("texstr" "tex")))
121 (define-public parser #f)
123 (define-public (lilypond-version)
125 (map (lambda (x) (if (symbol? x)
132 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
133 (define-public TEX_STRING_HASHLIMIT 10000000)
137 ;; gettext wrapper for guile < 1.7.2
138 (if (defined? 'gettext)
139 (define-public _ gettext)
140 (define-public _ ly:gettext))
142 (define-public (ly:load x)
143 (let* ((file-name (%search-load-path x)))
144 (if (ly:get-option 'verbose)
145 (ly:progress "[~A" file-name))
147 (ly:error (_ "cannot find: ~A") x))
148 (primitive-load file-name)
149 (if (ly:get-option 'verbose)
153 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
156 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
159 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
163 (define-public PLATFORM
166 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
169 (let ((platform (string-tokenize
170 (vector-ref (uname) 0) char-set:letter+digit)))
171 (if (null? (cdr platform)) #f
172 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
176 (define native-getcwd getcwd)
178 (if (string-index x #\\)
180 (string-regexp-substitute
182 (string-regexp-substitute "\\\\" "/" x))))
183 ;; FIXME: this prints a warning.
184 (define-public (ly-getcwd)
185 (slashify (native-getcwd))))
186 (else (define-public ly-getcwd getcwd)))
188 (define-public (is-absolute? file-name)
189 (let ((file-name-length (string-length file-name)))
190 (if (= file-name-length 0)
192 (or (eq? (string-ref file-name 0) #\/)
193 (and (eq? PLATFORM 'windows)
194 (> file-name-length 2)
195 (eq? (string-ref file-name 1) #\:)
196 (eq? (string-ref file-name 2) #\/))))))
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
200 (define (type-check-list location signature arguments)
201 "Typecheck a list of arguments against a list of type
202 predicates. Print a message at LOCATION if any predicate failed."
204 (define (recursion-helper signature arguments count)
205 (define (helper pred? arg count)
206 (if (not (pred? arg))
212 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
213 count (type-name pred?) arg))
217 (if (null? signature)
219 (and (helper (car signature) (car arguments) count)
220 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
222 (recursion-helper signature arguments 1))
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
228 ;;(define-public (output-framework) (write "hello\n"))
230 (define output-tex-module
231 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
232 (define output-ps-module
233 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
235 (define-public (ps-output-expression expr port)
236 (display (eval expr output-ps-module) port))
238 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
239 ;; Safe definitions utility
240 (define safe-objects (list))
242 (define-macro (define-safe-public arglist . body)
243 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
244 The syntax is the same as `define*-public'."
245 (define (get-symbol arg)
247 (get-symbol (car arg))
249 (let ((safe-symbol (get-symbol arglist)))
251 (define*-public ,arglist
253 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
257 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
260 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271 "define-event-classes.scm"
272 "define-music-types.scm"
275 "chord-ignatzek-names.scm"
277 "chord-generic-names.scm"
280 "music-functions.scm"
283 "define-music-properties.scm"
287 "parser-ly-from-scheme.scm"
288 "ly-syntax-constructors.scm"
290 "define-context-properties.scm"
291 "translation-functions.scm"
301 "define-markup-commands.scm"
302 "define-grob-properties.scm"
304 "define-grob-interfaces.scm"
305 "define-stencil-commands.scm"
309 "backend-library.scm"
312 ;; must be after everything has been defined
316 (set! type-p-name-alist
318 (,boolean-or-symbol? . "boolean or symbol")
319 (,boolean? . "boolean")
321 (,grob-list? . "list of grobs")
322 (,hash-table? . "hash table")
323 (,input-port? . "input port")
324 (,integer? . "integer")
326 (,ly:context? . "context")
327 (,ly:dimension? . "dimension, in staff space")
328 (,ly:dir? . "direction")
329 (,ly:duration? . "duration")
330 (,ly:grob? . "layout object")
331 (,ly:input-location? . "input location")
332 (,ly:moment? . "moment")
333 (,ly:music? . "music")
334 (,ly:pitch? . "pitch")
335 (,ly:translator? . "translator")
336 (,ly:font-metric? . "font metric")
337 (,ly:simple-closure? . "simple closure")
338 (,markup-list? . "list of markups")
339 (,markup? . "markup")
340 (,ly:music-list? . "list of music")
341 (,number-or-grob? . "number or grob")
342 (,number-or-string? . "number or string")
343 (,number-pair? . "pair of numbers")
344 (,number? . "number")
345 (,output-port? . "output port")
347 (,procedure? . "procedure")
348 (,rhythmic-location? . "rhythmic location")
349 (,scheme? . "any type")
350 (,string? . "string")
351 (,symbol? . "symbol")
352 (,vector? . "vector")))
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 (define (profile-measurements)
364 (ly:assoc-get 'gc-time-taken stats))
366 ;; difficult to put memory amount stats into here.
370 (define (dump-profile name last this)
372 ((outname (format "~a.profile" (basename name ".ly")))
373 (diff (map (lambda (y) (apply - y)) (zip this last))))
376 (ly:progress "\nWriting timing to ~a..." outname)
377 (format (open-file outname "w")
382 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
385 (define gc-dumping #f)
386 (define gc-protect-stat-count 0)
387 (define-public (dump-gc-protects)
388 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
389 (let* ((protects (sort
390 (hash-table->alist (ly:protects))
392 (< (object-address (car a))
393 (object-address (car b))))))
395 (out-file-name (string-append
396 "gcstat-" (number->string gc-protect-stat-count)
398 (outfile (open-file out-file-name "w")))
401 (display (format "Dumping GC statistics ~a...\n" out-file-name))
407 (format "~a (~a) = ~a\n" (object-address x) c x)
411 (not (symbol? (car x))))
415 (format outfile "\nprotected symbols: ~a\n"
416 (length (filter symbol? (map car protects))))
420 ;; (display (ly:smob-protects))
422 (if (defined? 'gc-live-object-stats)
424 (display "Live object statistics: GC'ing\n")
428 (display "Asserting dead objects\n")
429 (ly:set-option 'debug-gc-assert-parsed-dead #t)
431 (ly:set-option 'debug-gc-assert-parsed-dead #f)
433 (set! stats (gc-live-object-stats))
434 (display "Dumping live object statistics.\n")
438 (format outfile "~a: ~a\n" (car x) (cdr x)))
439 (sort (gc-live-object-stats)
441 (string<? (car x) (car y)))))))
452 gc-protect-stat-count
454 (let ((sym-stat (assoc sym stats)))
459 '(protected-objects bytes-malloced cell-heap-size
467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
470 (define (multi-fork count)
471 (define (helper count acc)
474 ((pid (primitive-fork)))
477 (helper (1- count) (cons pid acc))))
482 (define-public (lilypond-main files)
483 "Entry point for LilyPond."
485 (define (no-files-handler)
489 (if (ly:get-option 'gui)
495 (if (ly:get-option 'read-file-list)
498 (> (string-length s) 0))
500 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
504 (if (and (number? (ly:get-option 'job-count))
505 (> (length files) (ly:get-option 'job-count)))
508 ((count (ly:get-option 'job-count))
509 (split-todo (split-list files count))
510 (joblist (multi-fork count))
513 (if (not (string-or-symbol? (ly:get-option 'log-file)))
514 (ly:set-option 'log-file "lilypond-multi-run"))
516 (if (number? joblist)
518 (ly:set-option 'log-file (format "~a-~a"
519 (ly:get-option 'log-file) joblist))
520 (set! files (vector-ref split-todo joblist)))
523 (ly:progress "\nForking into jobs: ~a\n" joblist)
526 (let* ((stat (cdr (waitpid pid))))
529 (set! errors (cons (list-element-index joblist pid) errors)))))
534 (let* ((logfile (format "~a-~a.log"
535 (ly:get-option 'log-file) x))
536 (log (ly:gulp-file logfile))
537 (len (string-length log))
538 (tail (substring log (max 0 (- len 1024)))))
540 (display (format "\n\nlogfile ~a:\n\n ~a" logfile tail))))
545 (ly:error "Children ~a exited with errors." errors))
547 (exit (if (null? errors) 0 1))))))
550 (if (string-or-symbol? (ly:get-option 'log-file))
551 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
553 (let ((failed (lilypond-all files)))
556 (ly:error (_ "failed files: ~S") (string-join failed))
559 ;; HACK: be sure to exit with single newline
563 (define-public (lilypond-all files)
566 (if (ly:get-option 'show-available-fonts)
568 (ly:font-config-display-fonts)
573 (start-measurements #f)
574 (handler (lambda (key failed-file)
575 (set! failed (append (list failed-file) failed)))))
581 (set! start-measurements (profile-measurements))
582 (lilypond-file handler x)
583 (if (ly:get-option 'dump-profile)
584 (dump-profile x start-measurements (profile-measurements)))
587 (ly:clear-anonymous-modules)
588 (if (ly:get-option 'debug-gc)
590 (if (= (random 40) 1)
591 (ly:reset-all-fonts))))
596 (define (lilypond-file handler file-name)
597 (catch 'ly-file-failed
598 (lambda () (ly:parse-file file-name))
599 (lambda (x . args) (handler x file-name))))
601 (use-modules (scm editor))
603 (define-public (gui-main files)
605 (gui-no-files-handler))
607 (if (not (string? (ly:get-option 'log-file)))
608 (let* ((base (basename (car files) ".ly"))
609 (log-name (string-append base ".log")))
610 (if (not (ly:get-option 'gui))
611 (ly:message (_ "Redirecting output to ~a...") log-name))
612 (ly:stderr-redirect log-name "w")
613 (ly:message "# -*-compilation-*-"))
615 (let ((failed (lilypond-all files)))
619 (ly:stderr-redirect "foo" "r")
620 (system (get-editor-command log-name 0 0 0))
621 (ly:error (_ "failed files: ~S") (string-join failed))
626 (define (gui-no-files-handler)
627 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
628 ;; FIXME: soft-code, localize
629 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
630 (cmd (get-editor-command welcome-ly 0 0 0)))
631 (ly:message (_ "Invoking `~a'...") cmd)